From e2e951efdf36dca558fbead8cf2dc94a56554b07 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 10 Apr 2022 18:45:33 +0200 Subject: [PATCH] constValue(expr) convenience function added for new Ast expression nodes --- codeAst/src/prog8/code/ast/AstExpressions.kt | 4 ++++ .../src/prog8/codegen/virtual/CodeGen.kt | 20 +++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/codeAst/src/prog8/code/ast/AstExpressions.kt b/codeAst/src/prog8/code/ast/AstExpressions.kt index 5a599bb29..7174f7363 100644 --- a/codeAst/src/prog8/code/ast/AstExpressions.kt +++ b/codeAst/src/prog8/code/ast/AstExpressions.kt @@ -182,3 +182,7 @@ class PtTypeCast(type: DataType, position: Position) : PtExpression(type, positi val value: PtExpression get() = children.single() as PtExpression } + + +fun constValue(expr: PtExpression): Double? = if(expr is PtNumber) expr.number else null +fun constIntValue(expr: PtExpression): Int? = if(expr is PtNumber) expr.number.toInt() else null diff --git a/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt b/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt index 9003409d1..fea47c2c5 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/CodeGen.kt @@ -399,7 +399,7 @@ class CodeGen(internal val program: PtProgram, var condition = ifElse.condition val cond = ifElse.condition as? PtBinaryExpression - if((cond?.right as? PtNumber)?.number==0.0) { + if(cond!=null && constValue(cond.right)==0.0) { if(cond.operator == "==") { // if X==0 ... so we branch on Not-zero instead. branch = Opcode.BNZ @@ -462,7 +462,7 @@ class CodeGen(internal val program: PtProgram, val variable = array.variable.targetName var variableAddr = allocations.get(variable) val itemsize = program.memsizer.memorySize(array.type) - val fixedIndex = (array.index as? PtNumber)?.number?.toInt() + val fixedIndex = constIntValue(array.index) val memOp = when(postIncrDecr.operator) { "++" -> Opcode.INCM "--" -> Opcode.DECM @@ -485,13 +485,13 @@ class CodeGen(internal val program: PtProgram, } private fun translate(repeat: PtRepeatLoop): VmCodeChunk { - if((repeat.count as? PtNumber)?.number==0.0) - return VmCodeChunk() - if((repeat.count as? PtNumber)?.number==1.0) - return translateGroup(repeat.children) - if((repeat.count as? PtNumber)?.number==256.0) { - // 256 iterations can still be done with just a byte counter if you set it to zero as starting value. - repeat.children[0] = PtNumber(DataType.UBYTE, 0.0, repeat.count.position) + when (constIntValue(repeat.count)) { + 0 -> return VmCodeChunk() + 1 -> return translateGroup(repeat.children) + 256 -> { + // 256 iterations can still be done with just a byte counter if you set it to zero as starting value. + repeat.children[0] = PtNumber(DataType.UBYTE, 0.0, repeat.count.position) + } } val code = VmCodeChunk() @@ -543,7 +543,7 @@ class CodeGen(internal val program: PtProgram, val variable = array.variable.targetName var variableAddr = allocations.get(variable) val itemsize = program.memsizer.memorySize(array.type) - val fixedIndex = (array.index as? PtNumber)?.number?.toInt() + val fixedIndex = constIntValue(array.index) val vmDtArrayIdx = vmType(array.type) if(fixedIndex!=null) { variableAddr += fixedIndex*itemsize