diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt index 616a9fbd7..674803d6e 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt @@ -106,8 +106,7 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express require(origAssign.operator.endsWith('=')) if(codeGen.options.useNewExprCode) { // X += Y -> temp = X, temp += Y, X = temp - val tempvarname = "some_random_tempvar" // TODO create proper tempvar - val tempvar = PtIdentifier(tempvarname, origAssign.target.type, origAssign.position) + val tempvar = codeGen.getReusableTempvar(origAssign.definingSub()!!, origAssign.target.type) val assign = PtAssignment(origAssign.position) val target = PtAssignTarget(origAssign.position) target.add(tempvar) @@ -291,22 +290,26 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express private fun loadIndexReg(array: PtArrayIndexer, itemsize: Int): Pair { // returns the code to load the Index into the register, which is also return\ed. - if(codeGen.options.useNewExprCode) { - TODO("use aug assigns instead of BinExpr to calc proper array index") - // return blah + val result = mutableListOf() + if(itemsize==1) { + val tr = expressionEval.translateExpression(array.index) + addToResult(result, tr, tr.resultReg, -1) + return Pair(result, tr.resultReg) } - val result = mutableListOf() - val tr = if(itemsize==1) { - expressionEval.translateExpression(array.index) + if(codeGen.options.useNewExprCode) { + val tr = expressionEval.translateExpression(array.index) + result += tr.chunks + addInstr(result, IRInstruction(Opcode.MUL, tr.dt, reg1=tr.resultReg, value = itemsize), null) + return Pair(result, tr.resultReg) } else { - val mult : PtExpression + val mult: PtExpression mult = PtBinaryExpression("*", DataType.UBYTE, array.position) mult.children += array.index mult.children += PtNumber(DataType.UBYTE, itemsize.toDouble(), array.position) - expressionEval.translateExpression(mult) + val tr = expressionEval.translateExpression(mult) + addToResult(result, tr, tr.resultReg, -1) + return Pair(result, tr.resultReg) } - addToResult(result, tr, tr.resultReg, -1) - return Pair(result, tr.resultReg) } } \ No newline at end of file diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 3a43bcee2..9b2677caa 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -21,6 +21,7 @@ class IRCodeGen( private val expressionEval = ExpressionGen(this) private val builtinFuncGen = BuiltinFuncGen(this, expressionEval) private val assignmentGen = AssignmentGen(this, expressionEval) + private var irSymbolTable: IRSymbolTable = IRSymbolTable(null) internal val registers = RegisterPool() fun generate(): IRProgram { @@ -28,7 +29,8 @@ class IRCodeGen( moveAllNestedSubroutinesToBlockScope() verifyNameScoping(program, symbolTable) - val irProg = IRProgram(program.name, IRSymbolTable(symbolTable), options, program.encoding) + irSymbolTable = IRSymbolTable(symbolTable) + val irProg = IRProgram(program.name, irSymbolTable, options, program.encoding) if(options.evalStackBaseAddress!=null) throw AssemblyError("IR doesn't use eval-stack") @@ -1531,4 +1533,22 @@ class IRCodeGen( internal fun isZero(expression: PtExpression): Boolean = expression is PtNumber && expression.number==0.0 internal fun isOne(expression: PtExpression): Boolean = expression is PtNumber && expression.number==1.0 + + fun getReusableTempvar(scope: PtNamedNode, type: DataType): PtIdentifier { + val uniqueId = Pair(scope, type).hashCode().toUInt() + val tempvarname = "${scope.scopedName}.tempvar_${uniqueId}" + val tempvar = PtIdentifier(tempvarname, type, Position.DUMMY) + val staticVar = StStaticVariable( + tempvarname, + type, + null, + null, + null, + null, + ZeropageWish.DONTCARE, + tempvar + ) + irSymbolTable.add(staticVar) + return tempvar + } } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 0fcdb0a9f..fde94746d 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -4,8 +4,7 @@ TODO For next minor release ^^^^^^^^^^^^^^^^^^^^^^ - -newexpr: fix fallbackAssign() and loadIndexReg() +newexpr in VM: fix float printing from arrays ... diff --git a/examples/test.p8 b/examples/test.p8 index b45598e56..0aea0348c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,32 +5,27 @@ main { sub start() { - ubyte xx=100 - ubyte yy=21 + ubyte[] ba = [11,22,33] + uword[] wa = [1111,2222,3333] + float[] fa = [1.1, 2.2, 3.3] - xx %= yy - txt.print_ub(xx) + txt.print_ub(ba[1]) + txt.nl() + txt.print_uw(wa[1]) + txt.nl() + floats.print_f(fa[1]) ; TODO FIX FLOAT PRINT IN VM txt.nl() - ubyte ub1 = 100 - ubyte ub2 = 13 - ubyte ubd - ubyte ubr - divmod(ub1, ub2, ubd, ubr) - txt.print_ub(ubd) - txt.spc() - txt.print_ub(ubr) + ubyte index=1 + ubyte calc=1 + ba[index] += 1 + wa[index] += 1 + fa[index] += 1 + txt.print_ub(ba[1]) txt.nl() - - uword uw1 = 10000 - uword uw2 = 900 - uword uwd - uword uwr - divmodw(uw1, uw2, uwd, uwr) - txt.print_uw(uwd) - txt.spc() - txt.print_uw(uwr) + txt.print_uw(wa[1]) + txt.nl() + floats.print_f(fa[1]) ; TODO FIX FLOAT PRINT IN VM txt.nl() - } }