diff --git a/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentSignature.java b/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentSignature.java index 147b2842d..911d71aae 100644 --- a/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentSignature.java +++ b/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentSignature.java @@ -218,8 +218,9 @@ public class AsmFragmentSignature { // Find value if it is already bound for (String name : bindings.keySet()) { Value bound = bindings.get(name); - if (bound.equals(value)) + if (bound.equals(value)) { return name; + } } if (value instanceof Variable) { @@ -231,19 +232,57 @@ public class AsmFragmentSignature { if (bound instanceof Variable) { Registers.Register boundRegister = ((Variable) bound).getAllocation(); if (boundRegister != null && boundRegister.equals(register)) { - return name; + if (SymbolTypeInference.typeMatch(((Variable) bound).getType(), variable.getType())) { + return name; + } } } } // Create a new suitable name if (Registers.RegisterType.ZP_BYTE.equals(register.getType())) { - String name = "zpby" + nextZpByteIdx++; - bindings.put(name, value); - return name; - } else if (Registers.RegisterType.ZP_SBYTE.equals(register.getType())) { - String name = "zpsby" + nextZpSByteIdx++; - bindings.put(name, value); - return name; + SymbolType varType = ((Variable) value).getType(); + if (SymbolType.isByte(varType)) { + String name = "zpby" + nextZpByteIdx++; + bindings.put(name, value); + return name; + } else if (SymbolType.isSByte(varType)) { + String name = "zpsby" + nextZpSByteIdx++; + bindings.put(name, value); + return name; + } + } else if (Registers.RegisterType.REG_X_BYTE.equals(register.getType())) { + SymbolType varType = ((Variable) value).getType(); + if (SymbolType.isByte(varType)) { + String name = "xby"; + bindings.put(name, value); + return name; + } else if (SymbolType.isSByte(varType)) { + String name = "xsby"; + bindings.put(name, value); + return name; + } + } else if (Registers.RegisterType.REG_Y_BYTE.equals(register.getType())) { + SymbolType varType = ((Variable) value).getType(); + if (SymbolType.isByte(varType)) { + String name = "yby"; + bindings.put(name, value); + return name; + } else if (SymbolType.isSByte(varType)) { + String name = "ysby"; + bindings.put(name, value); + return name; + } + } else if (Registers.RegisterType.REG_A_BYTE.equals(register.getType())) { + SymbolType varType = ((Variable) value).getType(); + if (SymbolType.isByte(varType)) { + String name = "aby"; + bindings.put(name, value); + return name; + } else if (SymbolType.isSByte(varType)) { + String name = "asby"; + bindings.put(name, value); + return name; + } } else if (Registers.RegisterType.ZP_WORD.equals(register.getType())) { String name = "zpwo" + nextZpWordIdx++; bindings.put(name, value); @@ -252,30 +291,6 @@ public class AsmFragmentSignature { String name = "zpbo" + nextZpBoolIdx++; bindings.put(name, value); return name; - } else if (Registers.RegisterType.REG_X_BYTE.equals(register.getType())) { - String name = "xby"; - bindings.put(name, value); - return name; - } else if (Registers.RegisterType.REG_Y_BYTE.equals(register.getType())) { - String name = "yby"; - bindings.put(name, value); - return name; - } else if (Registers.RegisterType.REG_A_BYTE.equals(register.getType())) { - String name = "aby"; - bindings.put(name, value); - return name; - } else if (Registers.RegisterType.REG_X_SBYTE.equals(register.getType())) { - String name = "xsby"; - bindings.put(name, value); - return name; - } else if (Registers.RegisterType.REG_Y_SBYTE.equals(register.getType())) { - String name = "ysby"; - bindings.put(name, value); - return name; - } else if (Registers.RegisterType.REG_A_SBYTE.equals(register.getType())) { - String name = "asby"; - bindings.put(name, value); - return name; } else if (Registers.RegisterType.ZP_PTR_BYTE.equals(register.getType())) { String name = "zpptrby" + nextZpPtrIdx++; bindings.put(name, value); @@ -287,7 +302,7 @@ public class AsmFragmentSignature { SymbolType constType; if (value instanceof ConstantVar) { constType = ((ConstantVar) value).getType(); - } else if(value instanceof ConstantValue) { + } else if (value instanceof ConstantValue) { constType = SymbolTypeInference.inferType(program.getScope(), (ConstantValue) value); } else { throw new RuntimeException("Unhandled constant type " + value); diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass4RegistersFinalize.java b/src/main/java/dk/camelot64/kickc/passes/Pass4RegistersFinalize.java index 8188af597..bbaa05713 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass4RegistersFinalize.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass4RegistersFinalize.java @@ -137,7 +137,7 @@ public class Pass4RegistersFinalize extends Pass2Base { if (SymbolType.isByte(varType)) { return new Registers.RegisterZpByte(currentZp++); } else if (SymbolType.isSByte(varType)) { - return new Registers.RegisterZpSignedByte(currentZp++); + return new Registers.RegisterZpByte(currentZp++); } else if (SymbolType.isWord(varType)) { Registers.RegisterZpWord registerZpWord = new Registers.RegisterZpWord(currentZp); diff --git a/src/main/java/dk/camelot64/kickc/test/ref/casting.log b/src/main/java/dk/camelot64/kickc/test/ref/casting.log index 3f8563f8c..af5ff5207 100644 --- a/src/main/java/dk/camelot64/kickc/test/ref/casting.log +++ b/src/main/java/dk/camelot64/kickc/test/ref/casting.log @@ -1483,8 +1483,8 @@ Complete equivalence classes Allocated zp ZP_BYTE:2 [ main::b#2 main::b#1 ] Allocated zp ZP_BYTE:3 [ w::i#2 w::i#1 ] Allocated zp ZP_BYTE:4 [ main::b2#0 ] -Allocated zp ZP_SBYTE:5 [ main::$1 ] -Allocated zp ZP_SBYTE:6 [ main::sb#0 ] +Allocated zp ZP_BYTE:5 [ main::$1 ] +Allocated zp ZP_BYTE:6 [ main::sb#0 ] Allocated zp ZP_BYTE:7 [ main::$3 ] Allocated zp ZP_BYTE:8 [ w::b2#0 ] INITIAL ASM @@ -1641,17 +1641,17 @@ Statement [21] *((const byte*) SCREEN4#0 + (byte) w::i#2) ← (byte) w::b2#0 [ w Potential registers zp ZP_BYTE:2 [ main::b#2 main::b#1 ] : zp ZP_BYTE:2 , reg byte x , reg byte y , Potential registers zp ZP_BYTE:3 [ w::i#2 w::i#1 ] : zp ZP_BYTE:3 , reg byte x , reg byte y , Potential registers zp ZP_BYTE:4 [ main::b2#0 ] : zp ZP_BYTE:4 , reg byte a , reg byte x , reg byte y , -Potential registers zp ZP_SBYTE:5 [ main::$1 ] : zp ZP_SBYTE:5 , reg sbyte a , reg sbyte x , reg sbyte y , -Potential registers zp ZP_SBYTE:6 [ main::sb#0 ] : zp ZP_SBYTE:6 , reg sbyte a , reg sbyte x , reg sbyte y , +Potential registers zp ZP_BYTE:5 [ main::$1 ] : zp ZP_BYTE:5 , reg byte a , reg byte x , reg byte y , +Potential registers zp ZP_BYTE:6 [ main::sb#0 ] : zp ZP_BYTE:6 , reg byte a , reg byte x , reg byte y , Potential registers zp ZP_BYTE:7 [ main::$3 ] : zp ZP_BYTE:7 , reg byte a , reg byte x , reg byte y , Potential registers zp ZP_BYTE:8 [ w::b2#0 ] : zp ZP_BYTE:8 , reg byte x , reg byte y , REGISTER UPLIFT SCOPES -Uplift Scope [main] 25.93: zp ZP_BYTE:2 [ main::b#2 main::b#1 ] 22: zp ZP_BYTE:4 [ main::b2#0 ] 22: zp ZP_SBYTE:5 [ main::$1 ] 22: zp ZP_SBYTE:6 [ main::sb#0 ] 22: zp ZP_BYTE:7 [ main::$3 ] +Uplift Scope [main] 25.93: zp ZP_BYTE:2 [ main::b#2 main::b#1 ] 22: zp ZP_BYTE:4 [ main::b2#0 ] 22: zp ZP_BYTE:5 [ main::$1 ] 22: zp ZP_BYTE:6 [ main::sb#0 ] 22: zp ZP_BYTE:7 [ main::$3 ] Uplift Scope [w] 30.25: zp ZP_BYTE:3 [ w::i#2 w::i#1 ] 11: zp ZP_BYTE:8 [ w::b2#0 ] Uplift Scope [] -Uplifting [main] best 1056 combination reg byte x [ main::b#2 main::b#1 ] reg byte a [ main::b2#0 ] reg sbyte a [ main::$1 ] reg sbyte a [ main::sb#0 ] reg byte a [ main::$3 ] +Uplifting [main] best 1056 combination reg byte x [ main::b#2 main::b#1 ] reg byte a [ main::b2#0 ] reg byte a [ main::$1 ] reg byte a [ main::sb#0 ] reg byte a [ main::$3 ] Uplifting [w] best 876 combination reg byte y [ w::i#2 w::i#1 ] reg byte x [ w::b2#0 ] Uplifting [] best 876 combination Removing instruction jmp b2 @@ -2085,7 +2085,7 @@ FINAL SYMBOL TABLE (byte*) SCREEN4 (const byte*) SCREEN4#0 SCREEN4 = (const byte*) SCREEN#0+(byte/signed byte/word/signed word) 40*(byte/signed byte/word/signed word) 9 (void()) main() -(signed byte~) main::$1 reg sbyte a 22.0 +(signed byte~) main::$1 reg byte a 22.0 (byte~) main::$3 reg byte a 22.0 (label) main::@1 (label) main::@2 @@ -2096,7 +2096,7 @@ FINAL SYMBOL TABLE (byte) main::b2 (byte) main::b2#0 reg byte a 22.0 (signed byte) main::sb -(signed byte) main::sb#0 reg sbyte a 22.0 +(signed byte) main::sb#0 reg byte a 22.0 (void()) w() (label) w::@1 (label) w::@return @@ -2115,8 +2115,8 @@ FINAL SYMBOL TABLE reg byte x [ main::b#2 main::b#1 ] reg byte y [ w::i#2 w::i#1 ] reg byte a [ main::b2#0 ] -reg sbyte a [ main::$1 ] -reg sbyte a [ main::sb#0 ] +reg byte a [ main::$1 ] +reg byte a [ main::sb#0 ] reg byte a [ main::$3 ] reg byte x [ w::b2#0 ] diff --git a/src/main/java/dk/camelot64/kickc/test/ref/casting.sym b/src/main/java/dk/camelot64/kickc/test/ref/casting.sym index 4844ec4c2..aa2806137 100644 --- a/src/main/java/dk/camelot64/kickc/test/ref/casting.sym +++ b/src/main/java/dk/camelot64/kickc/test/ref/casting.sym @@ -10,7 +10,7 @@ (byte*) SCREEN4 (const byte*) SCREEN4#0 SCREEN4 = (const byte*) SCREEN#0+(byte/signed byte/word/signed word) 40*(byte/signed byte/word/signed word) 9 (void()) main() -(signed byte~) main::$1 reg sbyte a 22.0 +(signed byte~) main::$1 reg byte a 22.0 (byte~) main::$3 reg byte a 22.0 (label) main::@1 (label) main::@2 @@ -21,7 +21,7 @@ (byte) main::b2 (byte) main::b2#0 reg byte a 22.0 (signed byte) main::sb -(signed byte) main::sb#0 reg sbyte a 22.0 +(signed byte) main::sb#0 reg byte a 22.0 (void()) w() (label) w::@1 (label) w::@return @@ -40,7 +40,7 @@ reg byte x [ main::b#2 main::b#1 ] reg byte y [ w::i#2 w::i#1 ] reg byte a [ main::b2#0 ] -reg sbyte a [ main::$1 ] -reg sbyte a [ main::sb#0 ] +reg byte a [ main::$1 ] +reg byte a [ main::sb#0 ] reg byte a [ main::$3 ] reg byte x [ w::b2#0 ] diff --git a/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.log b/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.log index 3d8c89886..7f12f585f 100644 --- a/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.log +++ b/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.log @@ -658,7 +658,7 @@ Complete equivalence classes [ main::i#2 main::i#1 ] [ main::j#2 main::j#1 ] [ main::$2 ] -Allocated zp ZP_SBYTE:2 [ main::i#2 main::i#1 ] +Allocated zp ZP_BYTE:2 [ main::i#2 main::i#1 ] Allocated zp ZP_BYTE:3 [ main::j#2 main::j#1 ] Allocated zp ZP_BYTE:4 [ main::$2 ] INITIAL ASM @@ -734,33 +734,37 @@ main: { } REGISTER UPLIFT POTENTIAL REGISTERS -Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment xsby_lt_coby1_then_la1 allocation: reg sbyte x [ main::i#2 main::i#1 ] -Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment ysby_lt_coby1_then_la1 allocation: reg sbyte y [ main::i#2 main::i#1 ] +Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment xsby_lt_coby1_then_la1 allocation: reg byte x [ main::i#2 main::i#1 ] +Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment ysby_lt_coby1_then_la1 allocation: reg byte y [ main::i#2 main::i#1 ] MISSING FRAGMENTS xsby_lt_coby1_then_la1 ysby_lt_coby1_then_la1 Statement [6] if((signed byte) main::i#2<(byte/signed byte/word/signed word) 127) goto main::@2 [ main::i#2 main::j#2 ] ( main:2 [ main::i#2 main::j#2 ] ) always clobbers reg byte a +Removing always clobbered register reg byte a as potential for zp ZP_BYTE:2 [ main::i#2 main::i#1 ] Removing always clobbered register reg byte a as potential for zp ZP_BYTE:3 [ main::j#2 main::j#1 ] -Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment xsby_lt_coby1_then_la1 allocation: reg sbyte x [ main::i#2 main::i#1 ] -Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment ysby_lt_coby1_then_la1 allocation: reg sbyte y [ main::i#2 main::i#1 ] +Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment xsby_lt_coby1_then_la1 allocation: reg byte x [ main::i#2 main::i#1 ] +Potential register analysis [6] if(main::i#2<127) goto main::@2 missing fragment ysby_lt_coby1_then_la1 allocation: reg byte y [ main::i#2 main::i#1 ] MISSING FRAGMENTS xsby_lt_coby1_then_la1 ysby_lt_coby1_then_la1 Statement [6] if((signed byte) main::i#2<(byte/signed byte/word/signed word) 127) goto main::@2 [ main::i#2 main::j#2 ] ( main:2 [ main::i#2 main::j#2 ] ) always clobbers reg byte a -Potential registers zp ZP_SBYTE:2 [ main::i#2 main::i#1 ] : zp ZP_SBYTE:2 , reg sbyte a , reg sbyte x , reg sbyte y , +Potential registers zp ZP_BYTE:2 [ main::i#2 main::i#1 ] : zp ZP_BYTE:2 , reg byte x , reg byte y , Potential registers zp ZP_BYTE:3 [ main::j#2 main::j#1 ] : zp ZP_BYTE:3 , reg byte x , reg byte y , Potential registers zp ZP_BYTE:4 [ main::$2 ] : zp ZP_BYTE:4 , reg byte a , reg byte x , reg byte y , REGISTER UPLIFT SCOPES -Uplift Scope [main] 28.6: zp ZP_BYTE:3 [ main::j#2 main::j#1 ] 22: zp ZP_SBYTE:2 [ main::i#2 main::i#1 ] 22: zp ZP_BYTE:4 [ main::$2 ] +Uplift Scope [main] 28.6: zp ZP_BYTE:3 [ main::j#2 main::j#1 ] 22: zp ZP_BYTE:2 [ main::i#2 main::i#1 ] 22: zp ZP_BYTE:4 [ main::$2 ] Uplift Scope [] -Uplifting [main] best 468 combination reg byte x [ main::j#2 main::j#1 ] zp ZP_SBYTE:2 [ main::i#2 main::i#1 ] reg byte a [ main::$2 ] +Uplifting [main] best 468 combination reg byte x [ main::j#2 main::j#1 ] zp ZP_BYTE:2 [ main::i#2 main::i#1 ] reg byte a [ main::$2 ] Uplifting [] best 468 combination MISSING FRAGMENTS - asby=_inc_asby xsby_lt_coby1_then_la1 ysby_lt_coby1_then_la1 +Attempting to uplift remaining variables inzp ZP_BYTE:2 [ main::i#2 main::i#1 ] +Uplifting [main] best 468 combination zp ZP_BYTE:2 [ main::i#2 main::i#1 ] +MISSING FRAGMENTS + ysby_lt_coby1_then_la1 Removing instruction jmp b1 Removing instruction jmp bend Removing instruction jmp b1 @@ -960,15 +964,15 @@ FINAL SYMBOL TABLE (label) main::@2 (label) main::@return (signed byte) main::i -(signed byte) main::i#1 i zp ZP_SBYTE:2 11.0 -(signed byte) main::i#2 i zp ZP_SBYTE:2 11.0 +(signed byte) main::i#1 i zp ZP_BYTE:2 11.0 +(signed byte) main::i#2 i zp ZP_BYTE:2 11.0 (byte) main::j (byte) main::j#1 reg byte x 22.0 (byte) main::j#2 reg byte x 6.6000000000000005 (byte[]) main::screen (const byte[]) main::screen#0 screen = ((byte*))(word/signed word) 1024 -zp ZP_SBYTE:2 [ main::i#2 main::i#1 ] +zp ZP_BYTE:2 [ main::i#2 main::i#1 ] reg byte x [ main::j#2 main::j#1 ] reg byte a [ main::$2 ] diff --git a/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.sym b/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.sym index 6aa720444..47f5d2745 100644 --- a/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.sym +++ b/src/main/java/dk/camelot64/kickc/test/ref/signed-bytes.sym @@ -7,14 +7,14 @@ (label) main::@2 (label) main::@return (signed byte) main::i -(signed byte) main::i#1 i zp ZP_SBYTE:2 11.0 -(signed byte) main::i#2 i zp ZP_SBYTE:2 11.0 +(signed byte) main::i#1 i zp ZP_BYTE:2 11.0 +(signed byte) main::i#2 i zp ZP_BYTE:2 11.0 (byte) main::j (byte) main::j#1 reg byte x 22.0 (byte) main::j#2 reg byte x 6.6000000000000005 (byte[]) main::screen (const byte[]) main::screen#0 screen = ((byte*))(word/signed word) 1024 -zp ZP_SBYTE:2 [ main::i#2 main::i#1 ] +zp ZP_BYTE:2 [ main::i#2 main::i#1 ] reg byte x [ main::j#2 main::j#1 ] reg byte a [ main::$2 ]