fix IR array indexing for newexpr

This commit is contained in:
Irmen de Jong 2023-04-03 03:13:35 +02:00
parent 64d1f09ce0
commit d8214d4f12
4 changed files with 54 additions and 37 deletions

View File

@ -106,8 +106,7 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
require(origAssign.operator.endsWith('=')) require(origAssign.operator.endsWith('='))
if(codeGen.options.useNewExprCode) { if(codeGen.options.useNewExprCode) {
// X += Y -> temp = X, temp += Y, X = temp // X += Y -> temp = X, temp += Y, X = temp
val tempvarname = "some_random_tempvar" // TODO create proper tempvar val tempvar = codeGen.getReusableTempvar(origAssign.definingSub()!!, origAssign.target.type)
val tempvar = PtIdentifier(tempvarname, origAssign.target.type, origAssign.position)
val assign = PtAssignment(origAssign.position) val assign = PtAssignment(origAssign.position)
val target = PtAssignTarget(origAssign.position) val target = PtAssignTarget(origAssign.position)
target.add(tempvar) 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<IRCodeChunks, Int> { private fun loadIndexReg(array: PtArrayIndexer, itemsize: Int): Pair<IRCodeChunks, Int> {
// returns the code to load the Index into the register, which is also return\ed. // returns the code to load the Index into the register, which is also return\ed.
if(codeGen.options.useNewExprCode) { val result = mutableListOf<IRCodeChunkBase>()
TODO("use aug assigns instead of BinExpr to calc proper array index") if(itemsize==1) {
// return blah val tr = expressionEval.translateExpression(array.index)
addToResult(result, tr, tr.resultReg, -1)
return Pair(result, tr.resultReg)
} }
val result = mutableListOf<IRCodeChunkBase>() if(codeGen.options.useNewExprCode) {
val tr = if(itemsize==1) { val tr = expressionEval.translateExpression(array.index)
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 { } else {
val mult: PtExpression val mult: PtExpression
mult = PtBinaryExpression("*", DataType.UBYTE, array.position) mult = PtBinaryExpression("*", DataType.UBYTE, array.position)
mult.children += array.index mult.children += array.index
mult.children += PtNumber(DataType.UBYTE, itemsize.toDouble(), array.position) mult.children += PtNumber(DataType.UBYTE, itemsize.toDouble(), array.position)
expressionEval.translateExpression(mult) val tr = expressionEval.translateExpression(mult)
}
addToResult(result, tr, tr.resultReg, -1) addToResult(result, tr, tr.resultReg, -1)
return Pair(result, tr.resultReg) return Pair(result, tr.resultReg)
} }
} }
}

View File

@ -21,6 +21,7 @@ class IRCodeGen(
private val expressionEval = ExpressionGen(this) private val expressionEval = ExpressionGen(this)
private val builtinFuncGen = BuiltinFuncGen(this, expressionEval) private val builtinFuncGen = BuiltinFuncGen(this, expressionEval)
private val assignmentGen = AssignmentGen(this, expressionEval) private val assignmentGen = AssignmentGen(this, expressionEval)
private var irSymbolTable: IRSymbolTable = IRSymbolTable(null)
internal val registers = RegisterPool() internal val registers = RegisterPool()
fun generate(): IRProgram { fun generate(): IRProgram {
@ -28,7 +29,8 @@ class IRCodeGen(
moveAllNestedSubroutinesToBlockScope() moveAllNestedSubroutinesToBlockScope()
verifyNameScoping(program, symbolTable) 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) if(options.evalStackBaseAddress!=null)
throw AssemblyError("IR doesn't use eval-stack") 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 isZero(expression: PtExpression): Boolean = expression is PtNumber && expression.number==0.0
internal fun isOne(expression: PtExpression): Boolean = expression is PtNumber && expression.number==1.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
}
} }

View File

@ -4,8 +4,7 @@ TODO
For next minor release For next minor release
^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
newexpr in VM: fix float printing from arrays
newexpr: fix fallbackAssign() and loadIndexReg()
... ...

View File

@ -5,32 +5,27 @@
main { main {
sub start() { sub start() {
ubyte xx=100 ubyte[] ba = [11,22,33]
ubyte yy=21 uword[] wa = [1111,2222,3333]
float[] fa = [1.1, 2.2, 3.3]
xx %= yy txt.print_ub(ba[1])
txt.print_ub(xx) txt.nl()
txt.print_uw(wa[1])
txt.nl()
floats.print_f(fa[1]) ; TODO FIX FLOAT PRINT IN VM
txt.nl() txt.nl()
ubyte ub1 = 100 ubyte index=1
ubyte ub2 = 13 ubyte calc=1
ubyte ubd ba[index] += 1
ubyte ubr wa[index] += 1
divmod(ub1, ub2, ubd, ubr) fa[index] += 1
txt.print_ub(ubd) txt.print_ub(ba[1])
txt.spc()
txt.print_ub(ubr)
txt.nl() txt.nl()
txt.print_uw(wa[1])
uword uw1 = 10000 txt.nl()
uword uw2 = 900 floats.print_f(fa[1]) ; TODO FIX FLOAT PRINT IN VM
uword uwd
uword uwr
divmodw(uw1, uw2, uwd, uwr)
txt.print_uw(uwd)
txt.spc()
txt.print_uw(uwr)
txt.nl() txt.nl()
} }
} }