From 600a80dbcc5049c4a4fc553c495df11e57c726e0 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Wed, 12 Feb 2020 23:26:20 +0100 Subject: [PATCH] Fixed last errors for ssa_mem var-model. --- .../dk/camelot64/kickc/asm/AsmFormat.java | 74 +++++++++++-------- .../camelot64/kickc/model/Initializers.java | 2 + src/test/kc/long-pointer-1.kc | 2 +- src/test/kc/zeropage-exhausted.kc | 2 +- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/asm/AsmFormat.java b/src/main/java/dk/camelot64/kickc/asm/AsmFormat.java index 8e7e3f6d2..6268d0665 100644 --- a/src/main/java/dk/camelot64/kickc/asm/AsmFormat.java +++ b/src/main/java/dk/camelot64/kickc/asm/AsmFormat.java @@ -1,9 +1,12 @@ package dk.camelot64.kickc.asm; import dk.camelot64.kickc.model.CompileError; +import dk.camelot64.kickc.model.ConstantNotLiteral; import dk.camelot64.kickc.model.Program; import dk.camelot64.kickc.model.operators.*; -import dk.camelot64.kickc.model.symbols.*; +import dk.camelot64.kickc.model.symbols.Procedure; +import dk.camelot64.kickc.model.symbols.Symbol; +import dk.camelot64.kickc.model.symbols.Variable; import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.types.SymbolTypeInference; import dk.camelot64.kickc.model.types.SymbolTypePointer; @@ -116,14 +119,18 @@ public class AsmFormat { // 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); + try { + 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); + } + } catch(ConstantNotLiteral e) { + // Not literal - do the cast } // Cast is needed return getAsmConstant(program, new ConstantBinary(new ConstantInteger((long) 0xff), Operators.BOOL_AND, operand), outerPrecedence, codeScope); @@ -133,18 +140,22 @@ public class AsmFormat { // 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); + try { + 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); + } + } catch(ConstantNotLiteral e) { + // Not literal - do the cast } // Cast is needed return getAsmConstant(program, new ConstantBinary(new ConstantInteger((long) 0xffff), Operators.BOOL_AND, operand), outerPrecedence, codeScope); @@ -154,14 +165,18 @@ public class AsmFormat { // 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); + try { + 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); + } + } catch(ConstantNotLiteral e) { + // Not literal - do the cast } // Cast is needed return getAsmConstant(program, new ConstantBinary(new ConstantInteger((long) 0xffffffffL), Operators.BOOL_AND, operand), outerPrecedence, codeScope); @@ -278,6 +293,7 @@ public class AsmFormat { /** * Fix characters in an ASM parameter/label name. Handles '@:$#' + * * @param source The source string * @return The fixed string */ diff --git a/src/main/java/dk/camelot64/kickc/model/Initializers.java b/src/main/java/dk/camelot64/kickc/model/Initializers.java index 73566ae76..216979219 100644 --- a/src/main/java/dk/camelot64/kickc/model/Initializers.java +++ b/src/main/java/dk/camelot64/kickc/model/Initializers.java @@ -24,6 +24,8 @@ public class Initializers { if(typeSpec.getType() instanceof SymbolTypeIntegerFixed) { // Add an zero value initializer return new ConstantInteger(0L, typeSpec.getType()); + } else if(typeSpec.getType().equals(SymbolType.BOOLEAN)) { + return new ConstantBool(false); } else if(typeSpec.getType() instanceof SymbolTypeStruct) { // Add an zero-struct initializer SymbolTypeStruct typeStruct = (SymbolTypeStruct) typeSpec.getType(); diff --git a/src/test/kc/long-pointer-1.kc b/src/test/kc/long-pointer-1.kc index 97d3c0ece..5035c7e58 100644 --- a/src/test/kc/long-pointer-1.kc +++ b/src/test/kc/long-pointer-1.kc @@ -1,7 +1,7 @@ // Tests creating a long (32bit) pointer on zeropage for 45GS02 flat memory access void main() { unsigned long long_ptr = 0x12345678; - char long_ptr_zp = (char)&long_ptr; + __zp char long_ptr_zp = (char)&long_ptr; asm { nop lda (long_ptr_zp),y diff --git a/src/test/kc/zeropage-exhausted.kc b/src/test/kc/zeropage-exhausted.kc index 2656d33d0..d590f616e 100644 --- a/src/test/kc/zeropage-exhausted.kc +++ b/src/test/kc/zeropage-exhausted.kc @@ -6,6 +6,6 @@ // And then allocate a 2-byte-variable void main() { const int* SCREEN = 0x0400; - for(int i=0;i<10;i++) + for(__zp int i=0;i<10;i++) SCREEN[(char)i] = i; }