diff --git a/compiler/examples/test.p8 b/compiler/examples/test.p8 index b42069727..082936348 100644 --- a/compiler/examples/test.p8 +++ b/compiler/examples/test.p8 @@ -10,67 +10,5 @@ sub start() { - A //= 1 ; @todo wrong error about float - A = A//1 ; @todo wrong error about float - Y = A//1 ; @todo wrong error about float - - - A=A*2 - A=255 - A=A*4 - A=255 - A=A*8 - A=255 - A=A*16 - A=255 - A=A*32 - A=255 - A=A*64 - A=255 - A=A*128 - A=255 - A=A*3 - A=255 - -; A *= 0 -; A *= 1 -; A *= 2 -; A *= 3 -; A *= 4 -; A *= 5 -; A *= 6 -; A *= 7 -; A *= 8 -; A *= 9 -; A *= 10 -; A *= 11 -; A *= 16 -; A *= 32 -; A *= 64 -; A *= 128 -; A *= 255 - ;A *= 256 - ;A *= 257 - -; Y = A -; Y = A*0 -; Y = A*1 -; Y = A*2 -; Y = A*3 -; Y = A*4 -; Y = A*5 -; Y = A*6 -; Y = A*7 -; Y = A*8 -; Y = A*9 -; Y = A*10 -; Y = A*11 -; Y = A*16 -; Y = A*32 -; Y = A*64 -; Y = A*128 -; Y = A*255 - ;Y = A*256 - ;Y = A*257 } } diff --git a/compiler/src/prog8/optimizing/SimplifyExpressions.kt b/compiler/src/prog8/optimizing/SimplifyExpressions.kt index 83c6752bd..4f3ac45c0 100644 --- a/compiler/src/prog8/optimizing/SimplifyExpressions.kt +++ b/compiler/src/prog8/optimizing/SimplifyExpressions.kt @@ -374,27 +374,38 @@ class SimplifyExpressions(private val namespace: INameScope, private val heap: H // right value is a constant, see if we can optimize val rightConst: LiteralValue = rightVal val cv = rightConst.asNumericValue?.toDouble() + val leftDt = expr.left.resultingDatatype(namespace, heap) when(cv) { -1.0 -> { - // '/' -> -left, '//' -> -ceil(left) + // '/' -> -left, '//' -> -ceil(left) (if operand is float) else just -left optimizationsDone++ when(expr.operator) { "/" -> return PrefixExpression("-", expr.left, expr.position) - "//" -> return PrefixExpression("-", - FunctionCall(IdentifierReference(listOf("ceil"), expr.position), mutableListOf(expr.left), expr.position), - expr.position) + "//" -> { + return if(leftDt in IntegerDatatypes) + PrefixExpression("-", expr.left, expr.left.position) + else + PrefixExpression("-", + FunctionCall(IdentifierReference(listOf("ceil"), expr.position), mutableListOf(expr.left), expr.position), + expr.position) + } } } 1.0 -> { - // '/' -> left, '//' -> floor(left) + // '/' -> left, '//' -> floor(left) (if operand is float) else just left optimizationsDone++ when(expr.operator) { "/" -> return expr.left - "//" -> return FunctionCall(IdentifierReference(listOf("floor"), expr.position), mutableListOf(expr.left), expr.position) + "//" -> { + return if(leftDt in IntegerDatatypes) + expr.left + else + FunctionCall(IdentifierReference(listOf("floor"), expr.position), mutableListOf(expr.left), expr.position) + } } } 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0, 1024.0, 2048.0, 4096.0, 8192.0, 16384.0, 32768.0, 65536.0 -> { - if(expr.left.resultingDatatype(namespace, heap) in IntegerDatatypes) { + if(leftDt in IntegerDatatypes) { // divided by a power of two => shift right optimizationsDone++ val numshifts = log2(cv) @@ -402,7 +413,7 @@ class SimplifyExpressions(private val namespace: INameScope, private val heap: H } } -2.0, -4.0, -8.0, -16.0, -32.0, -64.0, -128.0, -256.0, -512.0, -1024.0, -2048.0, -4096.0, -8192.0, -16384.0, -32768.0, -65536.0 -> { - if(expr.left.resultingDatatype(namespace, heap) in IntegerDatatypes) { + if(leftDt in IntegerDatatypes) { // divided by a negative power of two => negate, then shift right optimizationsDone++ val numshifts = log2(-cv) @@ -411,13 +422,13 @@ class SimplifyExpressions(private val namespace: INameScope, private val heap: H } } - if (expr.left.resultingDatatype(namespace, heap) == DataType.UBYTE) { + if (leftDt == DataType.UBYTE) { if(abs(rightConst.asNumericValue!!.toDouble()) >= 256.0) { optimizationsDone++ return LiteralValue(DataType.UBYTE, 0, position = expr.position) } } - else if (expr.left.resultingDatatype(namespace, heap) == DataType.UWORD) { + else if (leftDt == DataType.UWORD) { if(abs(rightConst.asNumericValue!!.toDouble()) >= 65536.0) { optimizationsDone++ return LiteralValue(DataType.UBYTE, 0, position = expr.position)