diff --git a/src/main/java/dk/camelot64/kickc/fragment/AsmFormat.java b/src/main/java/dk/camelot64/kickc/fragment/AsmFormat.java index 895e806a8..26db72078 100644 --- a/src/main/java/dk/camelot64/kickc/fragment/AsmFormat.java +++ b/src/main/java/dk/camelot64/kickc/fragment/AsmFormat.java @@ -114,12 +114,14 @@ public class AsmFormat { // No cast needed return getAsmConstant(program, operand, outerPrecedence, codeScope); } - if(SymbolType.NUMBER.equals(operandType)) { - List operandInferedTypes = SymbolTypeNumberInference.inferTypes(program.getScope(), operand); - if(operandInferedTypes.contains(SymbolType.BYTE) || operandInferedTypes.contains(SymbolType.SBYTE)) { - // No cast needed - return getAsmConstant(program, operand, outerPrecedence, codeScope); - } + ConstantLiteral constantLiteral = operand.calculateLiteral(program.getScope()); + if(constantLiteral instanceof ConstantInteger && Operators.CAST_BYTE.equals(operator) && SymbolType.BYTE.contains(((ConstantInteger) constantLiteral).getValue())) { + // No cast needed + return getAsmConstant(program, operand, outerPrecedence, codeScope); + } + if(constantLiteral instanceof ConstantInteger && Operators.CAST_SBYTE.equals(operator) && SymbolType.SBYTE.contains(((ConstantInteger) constantLiteral).getValue())) { + // No cast needed + return getAsmConstant(program, operand, outerPrecedence, codeScope); } // Cast is needed return getAsmConstant(program, new ConstantBinary(new ConstantInteger((long) 0xff), Operators.BOOL_AND, operand), outerPrecedence, codeScope); @@ -129,12 +131,18 @@ public class AsmFormat { // No cast needed return getAsmConstant(program, operand, outerPrecedence, codeScope); } - if(SymbolType.NUMBER.equals(operandType)) { - List operandInferedTypes = SymbolTypeNumberInference.inferTypes(program.getScope(), operand); - if(operandInferedTypes.contains(SymbolType.WORD) || operandInferedTypes.contains(SymbolType.SWORD)) { - // No cast needed - return getAsmConstant(program, operand, outerPrecedence, codeScope); - } + ConstantLiteral constantLiteral = operand.calculateLiteral(program.getScope()); + if(constantLiteral instanceof ConstantInteger && Operators.CAST_WORD.equals(operator)&& SymbolType.WORD.contains(((ConstantInteger) constantLiteral).getValue())) { + // No cast needed + return getAsmConstant(program, operand, outerPrecedence, codeScope); + } + if(constantLiteral instanceof ConstantInteger && Operators.CAST_SWORD.equals(operator)&& SymbolType.SWORD.contains(((ConstantInteger) constantLiteral).getValue())) { + // No cast needed + return getAsmConstant(program, operand, outerPrecedence, codeScope); + } + if(constantLiteral instanceof ConstantInteger && (operator instanceof OperatorCastPtr) && SymbolType.WORD.contains(((ConstantInteger) constantLiteral).getValue())) { + // No cast needed + return getAsmConstant(program, operand, outerPrecedence, codeScope); } // Cast is needed return getAsmConstant(program, new ConstantBinary(new ConstantInteger((long) 0xffff), Operators.BOOL_AND, operand), outerPrecedence, codeScope); @@ -144,12 +152,14 @@ public class AsmFormat { // No cast needed return getAsmConstant(program, operand, outerPrecedence, codeScope); } - if(SymbolType.NUMBER.equals(operandType)) { - List operandInferedTypes = SymbolTypeNumberInference.inferTypes(program.getScope(), operand); - if(operandInferedTypes.contains(SymbolType.DWORD) || operandInferedTypes.contains(SymbolType.SDWORD)) { - // No cast needed - return getAsmConstant(program, operand, outerPrecedence, codeScope); - } + ConstantLiteral constantLiteral = operand.calculateLiteral(program.getScope()); + if(constantLiteral instanceof ConstantInteger && Operators.CAST_DWORD.equals(operator)&& SymbolType.DWORD.contains(((ConstantInteger) constantLiteral).getValue())) { + // No cast needed + return getAsmConstant(program, operand, outerPrecedence, codeScope); + } + if(constantLiteral instanceof ConstantInteger && Operators.CAST_SDWORD.equals(operator)&& SymbolType.SDWORD.contains(((ConstantInteger) constantLiteral).getValue())) { + // No cast needed + return getAsmConstant(program, operand, outerPrecedence, codeScope); } // Cast is needed return getAsmConstant(program, new ConstantBinary(new ConstantInteger((long) 0xffffffffL), Operators.BOOL_AND, operand), outerPrecedence, codeScope); diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantCastSimplification.java b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantCastSimplification.java index b8daf00a8..0d516f99e 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantCastSimplification.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantCastSimplification.java @@ -8,9 +8,7 @@ import dk.camelot64.kickc.model.operators.OperatorCast; import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.types.SymbolTypeIntegerFixed; import dk.camelot64.kickc.model.types.SymbolTypePointer; -import dk.camelot64.kickc.model.values.ConstantCastValue; -import dk.camelot64.kickc.model.values.ConstantInteger; -import dk.camelot64.kickc.model.values.ConstantPointer; +import dk.camelot64.kickc.model.values.*; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java index 6a0063197..100a0e868 100644 --- a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java +++ b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java @@ -49,7 +49,7 @@ public class TestPrograms { @Test public void testNumberType() throws IOException, URISyntaxException { - compileAndCompare("number-type", log()); + compileAndCompare("number-type"); } @Test