logical operators now always return a boolean byte result, instead of sometimes word type as well

(preparing for codegen simplifications for these)
This commit is contained in:
Irmen de Jong 2022-06-13 01:37:16 +02:00
parent 775c85fc18
commit bf9d120081
5 changed files with 11 additions and 6 deletions

View File

@ -321,7 +321,8 @@ internal class AssignmentAsmGen(private val program: Program,
if(!expr.inferType(program).isInteger)
return false
/* TODO re-add these optimizations? after we improved the unneeded addition of !=0 expressions
/*
TODO re-add these optimizations? after we improved the unneeded addition of !=0 expressions
if(expr.operator=="and") {
val dt = expr.left.inferType(program).getOrElse { throw AssemblyError("weird dt") }
if (dt in ByteDatatypes) {

View File

@ -252,6 +252,7 @@ internal class StatementReorderer(val program: Program,
// is rewritten as "var!=0 and other-logical-expression", to avoid bitwise boolean and
// generating the wrong results later
// TODO use bool(expr) instead of expr!=0 rewriting
fun wrapped(expr: Expression): Expression =
BinaryExpression(expr, "!=", NumericLiteral(DataType.UBYTE, 0.0, expr.position), expr.position)

View File

@ -253,7 +253,7 @@ class TestOptimization: FunSpec({
(initY2.value as NumericLiteral).number shouldBe 11.0
}
test("not-typecasted assignment from ubyte logical expression to uword var") {
test("not-typecasted assignment from ubyte logical expression to uword var should be auto upcasted") {
val src = """
main {
sub start() {
@ -266,10 +266,11 @@ class TestOptimization: FunSpec({
val result = compileText(C64Target(), false, src, writeAssembly = false)!!
val wwAssign = result.program.entrypoint.statements.last() as Assignment
val expr = wwAssign.value as BinaryExpression
val expr = wwAssign.value as TypecastExpression
expr.type shouldBe DataType.UWORD
wwAssign.target.identifier?.nameInSource shouldBe listOf("ww")
expr.inferType(result.program) istype DataType.UWORD shouldBe true
expr.expression.inferType(result.program) istype DataType.UBYTE shouldBe true
}
test("intermediate assignment steps have correct types for codegen phase (BeforeAsmGenerationAstChanger)") {

View File

@ -215,7 +215,7 @@ class BinaryExpression(var left: Expression, var operator: String, var right: Ex
"&" -> leftDt
"|" -> leftDt
"^" -> leftDt
"and", "or", "xor",
"and", "or", "xor" -> InferredTypes.knownFor(DataType.UBYTE)
"<", ">",
"<=", ">=",
"==", "!=" -> dynamicBooleanType()

View File

@ -3,7 +3,9 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- get rid of unneeded !=0 added to logical expressions
- add a builtin function 'bool' that takes a numeric value and returns ubyte false if it was zero and true otherwise
- statementreorderer: after(expr: BinaryExpression): get rid of unneeded !=0 added to logical expressions
by using this bool() function instead for the operand if it is not a byte type
- optimize logical expressions in attemptAssignOptimizedBinexpr()
- add McCarthy evaluation to shortcircuit and/or expressions. First do ifs by splitting them up? Then do expressions that compute a value?
...