fix code generation for using struct vars in arrays and such

This commit is contained in:
Irmen de Jong 2020-10-02 22:21:18 +02:00
parent bac51f4b31
commit 8647a8290e
4 changed files with 35 additions and 32 deletions

View File

@ -774,6 +774,18 @@ data class IdentifierReference(val nameInSource: List<String>, override val posi
else -> throw FatalAstException("requires a reference value")
}
}
fun firstStructVarName(namespace: INameScope): String? {
// take the name of the first struct member of the structvariable instead
// if it's just a regular variable, return null.
val struct = memberOfStruct(namespace) ?: return null
val decl = targetVarDecl(namespace)!!
val firstStructMember = struct.nameOfFirstMember()
// find the flattened var that belongs to this first struct member
val firstVarName = listOf(decl.name, firstStructMember)
val firstVar = definingScope().lookup(firstVarName, this) as VarDecl
return firstVar.name
}
}
class FunctionCall(override var target: IdentifierReference,

View File

@ -411,10 +411,10 @@ internal class AsmGen(private val program: Program,
"$" + it.number.toInt().toString(16).padStart(4, '0')
}
is AddressOf -> {
it.identifier.nameInSource.joinToString(".")
it.identifier.firstStructVarName(program.namespace) ?: asmSymbolName(it.identifier)
}
is IdentifierReference -> {
it.nameInSource.joinToString(".")
it.firstStructVarName(program.namespace) ?: asmSymbolName(it)
}
else -> throw AssemblyError("weird array elt dt")
}

View File

@ -320,18 +320,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
private fun assignAddressOf(target: AsmAssignTarget, name: IdentifierReference) {
val struct = name.memberOfStruct(program.namespace)
val sourceName = if (struct != null) {
// take the address of the first struct member instead
val decl = name.targetVarDecl(program.namespace)!!
val firstStructMember = struct.nameOfFirstMember()
// find the flattened var that belongs to this first struct member
val firstVarName = listOf(decl.name, firstStructMember)
val firstVar = name.definingScope().lookup(firstVarName, name) as VarDecl
firstVar.name
} else {
asmgen.fixNameSymbols(name.nameInSource.joinToString("."))
}
val sourceName = name.firstStructVarName(program.namespace) ?: asmgen.fixNameSymbols(name.nameInSource.joinToString("."))
when(target.kind) {
TargetStorageKind.VARIABLE -> {

View File

@ -6,34 +6,36 @@
main {
struct Color {
ubyte red
ubyte green
ubyte blue
}
struct Color {
ubyte red
ubyte green
ubyte blue
}
Color c1 = [11,22,33]
Color c2 = [11,22,33]
Color c3 = [11,22,33]
uword[] colors = [ c1, c2, c3] ; TODO should contain pointers to (the first element) of each struct
uword[] colors = [ c1, c2, c3]
sub start() {
ubyte a
ubyte b
ubyte c
c = a==b
Color c1 = [11,22,33]
Color c2 = [11,22,33]
Color c3 = [11,22,33]
uword[] colors = [ c1, c2, c3] ; TODO should contain pointers to (the first element) of each struct
txt.print_uwhex(&c1, true)
txt.chrout('\n')
txt.print_uwhex(&c2, true)
txt.chrout('\n')
txt.print_uwhex(&c3, true)
txt.chrout('\n')
txt.print_uwhex(colors[0], true)
txt.chrout('\n')
txt.print_uwhex(colors[1], true)
txt.chrout('\n')
txt.print_uwhex(colors[2], true)
txt.chrout('\n')
c1 = c2
; c1 = [11,22,33] ; TODO implement rewrite into individual struct member assignments
c1 = [11,22,33] ; TODO implement rewrite into individual struct member assignments
testX()
}
asmsub testX() {