This commit is contained in:
Irmen de Jong 2020-08-24 02:56:22 +02:00
parent 3ada0fdf84
commit aef4598cec
4 changed files with 46 additions and 7 deletions

View File

@ -184,6 +184,7 @@ internal class AsmGen(private val program: Program,
blockLevelVarInits.getValue(block).forEach { decl ->
val scopedFullName = decl.makeScopedName(decl.name).split('.')
require(scopedFullName.first()==block.name)
// TODO use AsmAssignment
val target = AssignTarget(IdentifierReference(scopedFullName.drop(1), decl.position), null, null, decl.position)
val assign = Assignment(target, decl.value!!, decl.position)
assign.linkParents(decl.parent)
@ -1035,6 +1036,7 @@ $counterVar .byte 0""")
} else {
val next = (stmt.parent as INameScope).nextSibling(stmt)
if (next !is ForLoop || next.loopVar.nameInSource.single() != stmt.name) {
// TODO use AsmAssignment
val target = AssignTarget(IdentifierReference(listOf(stmt.name), stmt.position), null, null, stmt.position)
val assign = Assignment(target, stmt.value!!, stmt.position)
assign.linkParents(stmt.parent)

View File

@ -116,6 +116,7 @@ $endLabel inx""")
stepsize == 1 || stepsize == -1 -> {
asmgen.translateExpression(range.to)
val varname = asmgen.asmIdentifierName(stmt.loopVar)
// TODO use AsmAssignment
val assignLoopvar = Assignment(AssignTarget(stmt.loopVar, null, null, stmt.loopVar.position), range.from, range.position)
assignLoopvar.linkParents(stmt)
asmgen.translate(assignLoopvar)
@ -162,6 +163,7 @@ $modifiedLabel2 cmp #0 ; modified
sta $modifiedLabel2+1
""")
val varname = asmgen.asmIdentifierName(stmt.loopVar)
// TODO use AsmAssignment
val assignLoopvar = Assignment(AssignTarget(stmt.loopVar, null, null, stmt.loopVar.position), range.from, range.position)
assignLoopvar.linkParents(stmt)
asmgen.translate(assignLoopvar)
@ -215,6 +217,7 @@ $endLabel inx""")
sta $modifiedLabel2+1
""")
val varname = asmgen.asmIdentifierName(stmt.loopVar)
// TODO use AsmAssignment
val assignLoopvar = Assignment(AssignTarget(stmt.loopVar, null, null, stmt.loopVar.position), range.from, range.position)
assignLoopvar.linkParents(stmt)
asmgen.translate(assignLoopvar)

View File

@ -10,6 +10,7 @@ import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.codegen.AsmGen
import prog8.compiler.toHex
import prog8.functions.BuiltinFunctions
internal class AssignmentAsmGen(private val program: Program, private val asmgen: AsmGen) {
@ -115,13 +116,42 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
SourceStorageKind.EXPRESSION -> {
val value = assign.source.expression!!
if (value is AddressOf) {
assignAddressOf(assign.target, value.identifier)
}
// TODO more special cases to avoid stack eval?
else {
asmgen.translateExpression(value)
assignStackValue(assign.target)
when(value) {
is AddressOf -> assignAddressOf(assign.target, value.identifier)
is NumericLiteralValue -> throw AssemblyError("source kind should have been literalnumber")
is IdentifierReference -> throw AssemblyError("source kind should have been variable")
is ArrayIndexedExpression -> throw AssemblyError("source kind should have been array")
is DirectMemoryRead -> throw AssemblyError("source kind should have been memory")
// is TypecastExpression -> {
// if(assign.target.kind == TargetStorageKind.STACK) {
// asmgen.translateExpression(value)
// assignStackValue(assign.target)
// } else {
// println("!!!!TYPECAST to ${assign.target.kind} $value")
// // TODO maybe we can do the typecast on the target directly instead of on the stack?
// asmgen.translateExpression(value)
// assignStackValue(assign.target)
// }
// }
// is FunctionCall -> {
// if (assign.target.kind == TargetStorageKind.STACK) {
// asmgen.translateExpression(value)
// assignStackValue(assign.target)
// } else {
// val functionName = value.target.nameInSource.last()
// val builtinFunc = BuiltinFunctions[functionName]
// if (builtinFunc != null) {
// println("!!!!BUILTIN-FUNCCALL target=${assign.target.kind} $value") // TODO optimize certain functions?
// }
// asmgen.translateExpression(value)
// assignStackValue(assign.target)
// }
// }
else -> {
// everything else just evaluate via the stack.
asmgen.translateExpression(value)
assignStackValue(assign.target)
}
}
}
SourceStorageKind.REGISTER -> {

View File

@ -7,6 +7,10 @@ main {
uword lines = 1
uword score = $1000
ubyte x
ubyte y
lines = mkword(x, y)
c64scr.print_uw(lines)
c64.CHROUT('\n')
c64scr.print_uw(score)