diff --git a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java index 66857e723..e033a9106 100644 --- a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java +++ b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java @@ -42,6 +42,11 @@ public class TestPrograms { compileAndCompare("examples/kernalload/kernalload"); } + @Test + public void testVolatile2() throws IOException, URISyntaxException { + compileAndCompare("volatile-2"); + } + @Test public void testVolatile1() throws IOException, URISyntaxException { compileAndCompare("volatile-1"); diff --git a/src/test/kc/volatile-2.kc b/src/test/kc/volatile-2.kc new file mode 100644 index 000000000..d7e7902b6 --- /dev/null +++ b/src/test/kc/volatile-2.kc @@ -0,0 +1,11 @@ +// Test that volatile const vars are turned into load/store + +const char* SCREEN = 0x0400; + +const volatile char ch = 3; + +void main() { + char i=3; + while(i<7) + SCREEN[i++] = ch; +} \ No newline at end of file diff --git a/src/test/ref/volatile-2.asm b/src/test/ref/volatile-2.asm new file mode 100644 index 000000000..ea6227121 --- /dev/null +++ b/src/test/ref/volatile-2.asm @@ -0,0 +1,23 @@ +// Test that volatile const vars are turned into load/store +.pc = $801 "Basic" +:BasicUpstart(__bbegin) +.pc = $80d "Program" + .label SCREEN = $400 + .label ch = 2 +__bbegin: + lda #3 + sta.z ch + jsr main + rts +main: { + ldx #3 + __b1: + cpx #7 + bcc __b2 + rts + __b2: + lda.z ch + sta SCREEN,x + inx + jmp __b1 +} diff --git a/src/test/ref/volatile-2.cfg b/src/test/ref/volatile-2.cfg new file mode 100644 index 000000000..3795e6a85 --- /dev/null +++ b/src/test/ref/volatile-2.cfg @@ -0,0 +1,25 @@ +@begin: scope:[] from + [0] (byte) ch ← (byte) 3 + to:@1 +@1: scope:[] from @begin + [1] phi() + [2] call main + to:@end +@end: scope:[] from @1 + [3] phi() + +(void()) main() +main: scope:[main] from @1 + [4] phi() + to:main::@1 +main::@1: scope:[main] from main main::@2 + [5] (byte) main::i#2 ← phi( main/(byte) 3 main::@2/(byte) main::i#1 ) + [6] if((byte) main::i#2<(byte) 7) goto main::@2 + to:main::@return +main::@return: scope:[main] from main::@1 + [7] return + to:@return +main::@2: scope:[main] from main::@1 + [8] *((const byte*) SCREEN + (byte) main::i#2) ← (byte) ch + [9] (byte) main::i#1 ← ++ (byte) main::i#2 + to:main::@1 diff --git a/src/test/ref/volatile-2.log b/src/test/ref/volatile-2.log new file mode 100644 index 000000000..d8f2d8344 --- /dev/null +++ b/src/test/ref/volatile-2.log @@ -0,0 +1,373 @@ +Culled Empty Block (label) main::@4 +Culled Empty Block (label) main::@3 +Culled Empty Block (label) main::@5 +Culled Empty Block (label) main::@6 + +CONTROL FLOW GRAPH SSA +@begin: scope:[] from + (byte) ch ← (number) 3 + to:@1 + +(void()) main() +main: scope:[main] from @1 + (byte) main::i#0 ← (number) 3 + to:main::@1 +main::@1: scope:[main] from main main::@2 + (byte) main::i#2 ← phi( main/(byte) main::i#0 main::@2/(byte) main::i#1 ) + (bool~) main::$0 ← (byte) main::i#2 < (number) 7 + if((bool~) main::$0) goto main::@2 + to:main::@return +main::@2: scope:[main] from main::@1 + (byte) main::i#3 ← phi( main::@1/(byte) main::i#2 ) + *((const byte*) SCREEN + (byte) main::i#3) ← (byte) ch + (byte) main::i#1 ← ++ (byte) main::i#3 + to:main::@1 +main::@return: scope:[main] from main::@1 + return + to:@return +@1: scope:[] from @begin + call main + to:@2 +@2: scope:[] from @1 + to:@end +@end: scope:[] from @2 + +SYMBOL TABLE SSA +(label) @1 +(label) @2 +(label) @begin +(label) @end +(const byte*) SCREEN = (byte*)(number) $400 +(byte) ch loadstore +(void()) main() +(bool~) main::$0 +(label) main::@1 +(label) main::@2 +(label) main::@return +(byte) main::i +(byte) main::i#0 +(byte) main::i#1 +(byte) main::i#2 +(byte) main::i#3 + +Adding number conversion cast (unumber) 3 in (byte) ch ← (number) 3 +Adding number conversion cast (unumber) 3 in (byte) main::i#0 ← (number) 3 +Adding number conversion cast (unumber) 7 in (bool~) main::$0 ← (byte) main::i#2 < (number) 7 +Successful SSA optimization PassNAddNumberTypeConversions +Inlining cast (byte) ch ← (unumber)(number) 3 +Inlining cast (byte) main::i#0 ← (unumber)(number) 3 +Successful SSA optimization Pass2InlineCast +Simplifying constant pointer cast (byte*) 1024 +Simplifying constant integer cast 3 +Simplifying constant integer cast 3 +Simplifying constant integer cast 7 +Successful SSA optimization PassNCastSimplification +Finalized unsigned number type (byte) 3 +Finalized unsigned number type (byte) 3 +Finalized unsigned number type (byte) 7 +Successful SSA optimization PassNFinalizeNumberTypeConversions +Alias (byte) main::i#2 = (byte) main::i#3 +Successful SSA optimization Pass2AliasElimination +Simple Condition (bool~) main::$0 [4] if((byte) main::i#2<(byte) 7) goto main::@2 +Successful SSA optimization Pass2ConditionalJumpSimplification +Constant (const byte) main::i#0 = 3 +Successful SSA optimization Pass2ConstantIdentification +Inlining constant with var siblings (const byte) main::i#0 +Constant inlined main::i#0 = (byte) 3 +Successful SSA optimization Pass2ConstantInlining +Adding NOP phi() at start of @1 +Adding NOP phi() at start of @2 +Adding NOP phi() at start of @end +Adding NOP phi() at start of main +CALL GRAPH +Calls in [] to main:2 + +Created 1 initial phi equivalence classes +Coalesced [11] main::i#4 ← main::i#1 +Coalesced down to 1 phi equivalence classes +Culled Empty Block (label) @2 +Adding NOP phi() at start of @1 +Adding NOP phi() at start of @end +Adding NOP phi() at start of main + +FINAL CONTROL FLOW GRAPH +@begin: scope:[] from + [0] (byte) ch ← (byte) 3 + to:@1 +@1: scope:[] from @begin + [1] phi() + [2] call main + to:@end +@end: scope:[] from @1 + [3] phi() + +(void()) main() +main: scope:[main] from @1 + [4] phi() + to:main::@1 +main::@1: scope:[main] from main main::@2 + [5] (byte) main::i#2 ← phi( main/(byte) 3 main::@2/(byte) main::i#1 ) + [6] if((byte) main::i#2<(byte) 7) goto main::@2 + to:main::@return +main::@return: scope:[main] from main::@1 + [7] return + to:@return +main::@2: scope:[main] from main::@1 + [8] *((const byte*) SCREEN + (byte) main::i#2) ← (byte) ch + [9] (byte) main::i#1 ← ++ (byte) main::i#2 + to:main::@1 + + +VARIABLE REGISTER WEIGHTS +(byte) ch loadstore 1.8571428571428572 +(void()) main() +(byte) main::i +(byte) main::i#1 22.0 +(byte) main::i#2 14.666666666666666 + +Initial phi equivalence classes +[ main::i#2 main::i#1 ] +Added variable ch to live range equivalence class [ ch ] +Complete equivalence classes +[ main::i#2 main::i#1 ] +[ ch ] +Allocated zp[1]:2 [ main::i#2 main::i#1 ] +Allocated zp[1]:3 [ ch ] + +INITIAL ASM +Target platform is c64basic / MOS6502X + // File Comments +// Test that volatile const vars are turned into load/store + // Upstart +.pc = $801 "Basic" +:BasicUpstart(__bbegin) +.pc = $80d "Program" + // Global Constants & labels + .label SCREEN = $400 + .label ch = 3 + // @begin +__bbegin: + // [0] (byte) ch ← (byte) 3 -- vbuz1=vbuc1 + lda #3 + sta.z ch + // [1] phi from @begin to @1 [phi:@begin->@1] +__b1_from___bbegin: + jmp __b1 + // @1 +__b1: + // [2] call main + // [4] phi from @1 to main [phi:@1->main] +main_from___b1: + jsr main + // [3] phi from @1 to @end [phi:@1->@end] +__bend_from___b1: + jmp __bend + // @end +__bend: + // main +main: { + .label i = 2 + // [5] phi from main to main::@1 [phi:main->main::@1] + __b1_from_main: + // [5] phi (byte) main::i#2 = (byte) 3 [phi:main->main::@1#0] -- vbuz1=vbuc1 + lda #3 + sta.z i + jmp __b1 + // main::@1 + __b1: + // [6] if((byte) main::i#2<(byte) 7) goto main::@2 -- vbuz1_lt_vbuc1_then_la1 + lda.z i + cmp #7 + bcc __b2 + jmp __breturn + // main::@return + __breturn: + // [7] return + rts + // main::@2 + __b2: + // [8] *((const byte*) SCREEN + (byte) main::i#2) ← (byte) ch -- pbuc1_derefidx_vbuz1=vbuz2 + lda.z ch + ldy.z i + sta SCREEN,y + // [9] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuz1=_inc_vbuz1 + inc.z i + // [5] phi from main::@2 to main::@1 [phi:main::@2->main::@1] + __b1_from___b2: + // [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@2->main::@1#0] -- register_copy + jmp __b1 +} + // File Data + +REGISTER UPLIFT POTENTIAL REGISTERS +Statement [0] (byte) ch ← (byte) 3 [ ch ] ( [ ch ] ) always clobbers reg byte a +Statement [8] *((const byte*) SCREEN + (byte) main::i#2) ← (byte) ch [ ch main::i#2 ] ( main:2 [ ch main::i#2 ] ) always clobbers reg byte a +Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::i#2 main::i#1 ] +Statement [0] (byte) ch ← (byte) 3 [ ch ] ( [ ch ] ) always clobbers reg byte a +Statement [8] *((const byte*) SCREEN + (byte) main::i#2) ← (byte) ch [ ch main::i#2 ] ( main:2 [ ch main::i#2 ] ) always clobbers reg byte a +Potential registers zp[1]:2 [ main::i#2 main::i#1 ] : zp[1]:2 , reg byte x , reg byte y , +Potential registers zp[1]:3 [ ch ] : zp[1]:3 , + +REGISTER UPLIFT SCOPES +Uplift Scope [main] 36.67: zp[1]:2 [ main::i#2 main::i#1 ] +Uplift Scope [] 1.86: zp[1]:3 [ ch ] + +Uplifting [main] best 278 combination reg byte x [ main::i#2 main::i#1 ] +Uplifting [] best 278 combination zp[1]:3 [ ch ] +Attempting to uplift remaining variables inzp[1]:3 [ ch ] +Uplifting [] best 278 combination zp[1]:3 [ ch ] +Allocated (was zp[1]:3) zp[1]:2 [ ch ] + +ASSEMBLER BEFORE OPTIMIZATION + // File Comments +// Test that volatile const vars are turned into load/store + // Upstart +.pc = $801 "Basic" +:BasicUpstart(__bbegin) +.pc = $80d "Program" + // Global Constants & labels + .label SCREEN = $400 + .label ch = 2 + // @begin +__bbegin: + // [0] (byte) ch ← (byte) 3 -- vbuz1=vbuc1 + lda #3 + sta.z ch + // [1] phi from @begin to @1 [phi:@begin->@1] +__b1_from___bbegin: + jmp __b1 + // @1 +__b1: + // [2] call main + // [4] phi from @1 to main [phi:@1->main] +main_from___b1: + jsr main + // [3] phi from @1 to @end [phi:@1->@end] +__bend_from___b1: + jmp __bend + // @end +__bend: + // main +main: { + // [5] phi from main to main::@1 [phi:main->main::@1] + __b1_from_main: + // [5] phi (byte) main::i#2 = (byte) 3 [phi:main->main::@1#0] -- vbuxx=vbuc1 + ldx #3 + jmp __b1 + // main::@1 + __b1: + // [6] if((byte) main::i#2<(byte) 7) goto main::@2 -- vbuxx_lt_vbuc1_then_la1 + cpx #7 + bcc __b2 + jmp __breturn + // main::@return + __breturn: + // [7] return + rts + // main::@2 + __b2: + // [8] *((const byte*) SCREEN + (byte) main::i#2) ← (byte) ch -- pbuc1_derefidx_vbuxx=vbuz1 + lda.z ch + sta SCREEN,x + // [9] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx + inx + // [5] phi from main::@2 to main::@1 [phi:main::@2->main::@1] + __b1_from___b2: + // [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@2->main::@1#0] -- register_copy + jmp __b1 +} + // File Data + +ASSEMBLER OPTIMIZATIONS +Removing instruction jmp __b1 +Removing instruction jmp __bend +Removing instruction jmp __b1 +Removing instruction jmp __breturn +Succesful ASM optimization Pass5NextJumpElimination +Removing instruction __b1_from___bbegin: +Removing instruction main_from___b1: +Removing instruction __bend_from___b1: +Succesful ASM optimization Pass5RedundantLabelElimination +Removing instruction __b1: +Removing instruction __bend: +Removing instruction __b1_from_main: +Removing instruction __breturn: +Removing instruction __b1_from___b2: +Succesful ASM optimization Pass5UnusedLabelElimination +Adding RTS to root block +Succesful ASM optimization Pass5AddMainRts + +FINAL SYMBOL TABLE +(label) @1 +(label) @begin +(label) @end +(const byte*) SCREEN = (byte*) 1024 +(byte) ch loadstore zp[1]:2 1.8571428571428572 +(void()) main() +(label) main::@1 +(label) main::@2 +(label) main::@return +(byte) main::i +(byte) main::i#1 reg byte x 22.0 +(byte) main::i#2 reg byte x 14.666666666666666 + +reg byte x [ main::i#2 main::i#1 ] +zp[1]:2 [ ch ] + + +FINAL ASSEMBLER +Score: 218 + + // File Comments +// Test that volatile const vars are turned into load/store + // Upstart +.pc = $801 "Basic" +:BasicUpstart(__bbegin) +.pc = $80d "Program" + // Global Constants & labels + .label SCREEN = $400 + .label ch = 2 + // @begin +__bbegin: + // ch = 3 + // [0] (byte) ch ← (byte) 3 -- vbuz1=vbuc1 + lda #3 + sta.z ch + // [1] phi from @begin to @1 [phi:@begin->@1] + // @1 + // [2] call main + // [4] phi from @1 to main [phi:@1->main] + jsr main + rts + // [3] phi from @1 to @end [phi:@1->@end] + // @end + // main +main: { + // [5] phi from main to main::@1 [phi:main->main::@1] + // [5] phi (byte) main::i#2 = (byte) 3 [phi:main->main::@1#0] -- vbuxx=vbuc1 + ldx #3 + // main::@1 + __b1: + // while(i<7) + // [6] if((byte) main::i#2<(byte) 7) goto main::@2 -- vbuxx_lt_vbuc1_then_la1 + cpx #7 + bcc __b2 + // main::@return + // } + // [7] return + rts + // main::@2 + __b2: + // SCREEN[i++] = ch + // [8] *((const byte*) SCREEN + (byte) main::i#2) ← (byte) ch -- pbuc1_derefidx_vbuxx=vbuz1 + lda.z ch + sta SCREEN,x + // SCREEN[i++] = ch; + // [9] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx + inx + // [5] phi from main::@2 to main::@1 [phi:main::@2->main::@1] + // [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@2->main::@1#0] -- register_copy + jmp __b1 +} + // File Data + diff --git a/src/test/ref/volatile-2.sym b/src/test/ref/volatile-2.sym new file mode 100644 index 000000000..986aff8f5 --- /dev/null +++ b/src/test/ref/volatile-2.sym @@ -0,0 +1,15 @@ +(label) @1 +(label) @begin +(label) @end +(const byte*) SCREEN = (byte*) 1024 +(byte) ch loadstore zp[1]:2 1.8571428571428572 +(void()) main() +(label) main::@1 +(label) main::@2 +(label) main::@return +(byte) main::i +(byte) main::i#1 reg byte x 22.0 +(byte) main::i#2 reg byte x 14.666666666666666 + +reg byte x [ main::i#2 main::i#1 ] +zp[1]:2 [ ch ]