mirror of
https://github.com/irmen/prog8.git
synced 2024-11-04 04:05:00 +00:00
constValue(expr) convenience function added for new Ast expression nodes
This commit is contained in:
parent
3f6393f732
commit
e2e951efdf
@ -182,3 +182,7 @@ class PtTypeCast(type: DataType, position: Position) : PtExpression(type, positi
|
|||||||
val value: PtExpression
|
val value: PtExpression
|
||||||
get() = children.single() as 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
|
||||||
|
@ -399,7 +399,7 @@ class CodeGen(internal val program: PtProgram,
|
|||||||
var condition = ifElse.condition
|
var condition = ifElse.condition
|
||||||
|
|
||||||
val cond = ifElse.condition as? PtBinaryExpression
|
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(cond.operator == "==") {
|
||||||
// if X==0 ... so we branch on Not-zero instead.
|
// if X==0 ... so we branch on Not-zero instead.
|
||||||
branch = Opcode.BNZ
|
branch = Opcode.BNZ
|
||||||
@ -462,7 +462,7 @@ class CodeGen(internal val program: PtProgram,
|
|||||||
val variable = array.variable.targetName
|
val variable = array.variable.targetName
|
||||||
var variableAddr = allocations.get(variable)
|
var variableAddr = allocations.get(variable)
|
||||||
val itemsize = program.memsizer.memorySize(array.type)
|
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) {
|
val memOp = when(postIncrDecr.operator) {
|
||||||
"++" -> Opcode.INCM
|
"++" -> Opcode.INCM
|
||||||
"--" -> Opcode.DECM
|
"--" -> Opcode.DECM
|
||||||
@ -485,14 +485,14 @@ class CodeGen(internal val program: PtProgram,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun translate(repeat: PtRepeatLoop): VmCodeChunk {
|
private fun translate(repeat: PtRepeatLoop): VmCodeChunk {
|
||||||
if((repeat.count as? PtNumber)?.number==0.0)
|
when (constIntValue(repeat.count)) {
|
||||||
return VmCodeChunk()
|
0 -> return VmCodeChunk()
|
||||||
if((repeat.count as? PtNumber)?.number==1.0)
|
1 -> return translateGroup(repeat.children)
|
||||||
return translateGroup(repeat.children)
|
256 -> {
|
||||||
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.
|
// 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)
|
repeat.children[0] = PtNumber(DataType.UBYTE, 0.0, repeat.count.position)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val code = VmCodeChunk()
|
val code = VmCodeChunk()
|
||||||
val counterReg = vmRegisters.nextFree()
|
val counterReg = vmRegisters.nextFree()
|
||||||
@ -543,7 +543,7 @@ class CodeGen(internal val program: PtProgram,
|
|||||||
val variable = array.variable.targetName
|
val variable = array.variable.targetName
|
||||||
var variableAddr = allocations.get(variable)
|
var variableAddr = allocations.get(variable)
|
||||||
val itemsize = program.memsizer.memorySize(array.type)
|
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)
|
val vmDtArrayIdx = vmType(array.type)
|
||||||
if(fixedIndex!=null) {
|
if(fixedIndex!=null) {
|
||||||
variableAddr += fixedIndex*itemsize
|
variableAddr += fixedIndex*itemsize
|
||||||
|
Loading…
Reference in New Issue
Block a user