From 2f5bed36b3372c09e2c528d5f06986ceb74128b2 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 31 Jan 2023 01:14:13 +0100 Subject: [PATCH] remove bool to ubyte typecasts --- .../codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt | 2 ++ .../src/prog8/compiler/astprocessing/IntermediateAstMaker.kt | 1 + compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt | 2 +- compiler/test/codegeneration/TestVarious.kt | 3 ++- compilerAst/src/prog8/ast/expressions/InferredTypes.kt | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt index b95c3b776..fbb3444f9 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt @@ -149,6 +149,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, val value = if(program.memsizer.memorySize(origValue.type) > program.memsizer.memorySize(target.datatype)) { val typecast = PtTypeCast(target.datatype, origValue.position) typecast.add(origValue) + require(typecast.type!=origValue.type) typecast } else { @@ -1783,6 +1784,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, val castDt = if (outerCastDt largerThan innerCastDt) innerCastDt else outerCastDt val resultingCast = PtTypeCast(castDt, position) resultingCast.add((cast.value as PtTypeCast).value) + require(castDt!=resultingCast.value.type) inplaceCast(target, resultingCast, position) } } diff --git a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt index 4d1fd3924..197e8b378 100644 --- a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt @@ -464,6 +464,7 @@ class IntermediateAstMaker(private val program: Program, private val symbolTable private fun transform(srcCast: TypecastExpression): PtTypeCast { val cast = PtTypeCast(srcCast.type, srcCast.position) cast.add(transformExpression(srcCast.expression)) + require(cast.type!=cast.value.type) return cast } diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 48d52d8b2..c6728b183 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -40,7 +40,7 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter, } val sourceDt = typecast.expression.inferType(program) - if(sourceDt istype typecast.type) + if(sourceDt istype typecast.type || (sourceDt istype DataType.BOOL && typecast.type==DataType.UBYTE)) return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent)) if(parent is Assignment) { diff --git a/compiler/test/codegeneration/TestVarious.kt b/compiler/test/codegeneration/TestVarious.kt index cb3adc2dc..0da463989 100644 --- a/compiler/test/codegeneration/TestVarious.kt +++ b/compiler/test/codegeneration/TestVarious.kt @@ -11,7 +11,8 @@ class TestVarious: FunSpec({ main { sub start() { ubyte[3] values - func(33 + (22 in values)) + func(22 in values) + ubyte @shared qq = 22 in values } sub func(ubyte arg) { arg++ diff --git a/compilerAst/src/prog8/ast/expressions/InferredTypes.kt b/compilerAst/src/prog8/ast/expressions/InferredTypes.kt index 79891a140..3762a6624 100644 --- a/compilerAst/src/prog8/ast/expressions/InferredTypes.kt +++ b/compilerAst/src/prog8/ast/expressions/InferredTypes.kt @@ -14,7 +14,7 @@ object InferredTypes { fun getOr(default: DataType) = if(isUnknown || isVoid) default else datatype!! fun getOrElse(transform: (InferredType) -> DataType): DataType = if(isUnknown || isVoid) transform(this) else datatype!! - infix fun istype(type: DataType): Boolean = if(isUnknown || isVoid) false else this.datatype==type + infix fun istype(type: DataType): Boolean = if(isUnknown || isVoid) false else this.datatype==type // strict equality if known infix fun isnot(type: DataType): Boolean = if(isUnknown || isVoid) true else this.datatype!=type fun oneOf(vararg types: DataType) = if(isUnknown || isVoid) false else this.datatype in types