1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-02-17 10:30:43 +00:00

Added test declaring a memory-variables. #328

This commit is contained in:
jespergravgaard 2019-09-24 08:55:06 +02:00
parent d312edf024
commit f696f8db18
8 changed files with 491 additions and 3 deletions

View File

@ -347,7 +347,9 @@ public abstract class Scope implements Symbol, Serializable {
if(asmName != null) {
res.append(" " + asmName);
}
if(symVar.getStorageStrategy().equals(SymbolVariable.StorageStrategy.MEMORY)) {
res.append(" memory");
}
Registers.Register declRegister = symVar.getDeclaredRegister();
if(declRegister != null) {
res.append(" !" + declRegister);

View File

@ -837,12 +837,12 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
if(ctx.NUMBER() != null) {
try {
ConstantInteger memoryAddress = NumberParser.parseIntegerLiteral(ctx.NUMBER().getText());
return new DirectiveRegister(memoryAddress.getInteger());
return new DirectiveMemory(memoryAddress.getInteger());
} catch(NumberFormatException e) {
throw new CompileError(e.getMessage(), new StatementSource(ctx));
}
} else {
return new DirectiveRegister(null);
return new DirectiveMemory();
}
}

View File

@ -35,6 +35,11 @@ public class TestPrograms {
public TestPrograms() {
}
@Test
public void testDelcaredMemoryVar0() throws IOException, URISyntaxException {
compileAndCompare("declared-memory-var-0"); //, log().verboseParse().verboseCreateSsa());
}
/*
@Test
public void testProcedureCallingConventionStack6() throws IOException, URISyntaxException {

View File

@ -0,0 +1,13 @@
// Test declaring a variable as "memory", meaning it will be stored in memory and accessed through an implicit pointer (using load/store)
memory char idx;
const char* SCREEN = 0x0400;
void main() {
idx = 0;
for( char i: 0..5 ) {
SCREEN[i] = idx;
idx +=i;
}
}

View File

@ -0,0 +1,18 @@
// Test declaring a variable as "memory", meaning it will be stored in memory and accessed through an implicit pointer (using load/store)
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label SCREEN = $400
main: {
ldx #0
txa
b1:
sta SCREEN,x
stx.z $ff
clc
adc.z $ff
inx
cpx #6
bne b1
rts
}

View File

@ -0,0 +1,25 @@
@begin: scope:[] from
[0] phi()
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::@1
[5] (byte) main::i#2 ← phi( main/(byte) 0 main::@1/(byte) main::i#1 )
[5] (byte) idx#5 ← phi( main/(byte) 0 main::@1/(byte) idx#2 )
[6] *((const byte*) SCREEN#0 + (byte) main::i#2) ← (byte) idx#5
[7] (byte) idx#2 ← (byte) idx#5 + (byte) main::i#2
[8] (byte) main::i#1 ← ++ (byte) main::i#2
[9] if((byte) main::i#1!=(byte) 6) goto main::@1
to:main::@return
main::@return: scope:[main] from main::@1
[10] return
to:@return

View File

@ -0,0 +1,408 @@
Culled Empty Block (label) main::@2
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
(byte) idx#0 ← (byte) 0
(byte*) SCREEN#0 ← ((byte*)) (number) $400
to:@1
(void()) main()
main: scope:[main] from @1
(byte) idx#1 ← (number) 0
(byte) main::i#0 ← (byte) 0
to:main::@1
main::@1: scope:[main] from main main::@1
(byte) main::i#2 ← phi( main/(byte) main::i#0 main::@1/(byte) main::i#1 )
(byte) idx#5 ← phi( main/(byte) idx#1 main::@1/(byte) idx#2 )
*((byte*) SCREEN#0 + (byte) main::i#2) ← (byte) idx#5
(byte) idx#2 ← (byte) idx#5 + (byte) main::i#2
(byte) main::i#1 ← (byte) main::i#2 + rangenext(0,5)
(bool~) main::$0 ← (byte) main::i#1 != rangelast(0,5)
if((bool~) main::$0) goto main::@1
to:main::@return
main::@return: scope:[main] from main::@1
(byte) idx#6 ← phi( main::@1/(byte) idx#2 )
(byte) idx#3 ← (byte) idx#6
return
to:@return
@1: scope:[] from @begin
(byte) idx#8 ← phi( @begin/(byte) idx#0 )
call main
to:@2
@2: scope:[] from @1
(byte) idx#7 ← phi( @1/(byte) idx#3 )
(byte) idx#4 ← (byte) idx#7
to:@end
@end: scope:[] from @2
SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @begin
(label) @end
(byte*) SCREEN
(byte*) SCREEN#0
(byte) idx memory
(byte) idx#0 memory
(byte) idx#1 memory
(byte) idx#2 memory
(byte) idx#3 memory
(byte) idx#4 memory
(byte) idx#5 memory
(byte) idx#6 memory
(byte) idx#7 memory
(byte) idx#8 memory
(void()) main()
(bool~) main::$0
(label) main::@1
(label) main::@return
(byte) main::i
(byte) main::i#0
(byte) main::i#1
(byte) main::i#2
Adding number conversion cast (unumber) 0 in (byte) idx#1 ← (number) 0
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (byte*) SCREEN#0 ← (byte*)(number) $400
Inlining cast (byte) idx#1 ← (unumber)(number) 0
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 0
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 0
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias (byte) idx#2 = (byte) idx#6 (byte) idx#3
Alias (byte) idx#0 = (byte) idx#8
Alias (byte) idx#4 = (byte) idx#7
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) idx#4 (byte) idx#2
Successful SSA optimization Pass2IdenticalPhiElimination
Simple Condition (bool~) main::$0 [9] if((byte) main::i#1!=rangelast(0,5)) goto main::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte) idx#0 = 0
Constant (const byte*) SCREEN#0 = (byte*) 1024
Constant (const byte) idx#1 = 0
Constant (const byte) main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value [7] main::i#1 ← ++ main::i#2 to ++
Resolved ranged comparison value [9] if(main::i#1!=rangelast(0,5)) goto main::@1 to (number) 6
Eliminating unused constant (const byte) idx#0
Successful SSA optimization PassNEliminateUnusedVars
Adding number conversion cast (unumber) 6 in if((byte) main::i#1!=(number) 6) goto main::@1
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast 6
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 6
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inlining constant with var siblings (const byte) main::i#0
Inlining constant with var siblings (const byte) idx#1
Constant inlined main::i#0 = (byte) 0
Constant inlined idx#1 = (byte) 0
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting main::@3(between main::@1 and main::@1)
Adding NOP phi() at start of @begin
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 2 initial phi equivalence classes
Coalesced [12] idx#9 ← idx#2
Coalesced [13] main::i#3 ← main::i#1
Coalesced down to 2 phi equivalence classes
Culled Empty Block (label) @2
Culled Empty Block (label) main::@3
Adding NOP phi() at start of @begin
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] phi()
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::@1
[5] (byte) main::i#2 ← phi( main/(byte) 0 main::@1/(byte) main::i#1 )
[5] (byte) idx#5 ← phi( main/(byte) 0 main::@1/(byte) idx#2 )
[6] *((const byte*) SCREEN#0 + (byte) main::i#2) ← (byte) idx#5
[7] (byte) idx#2 ← (byte) idx#5 + (byte) main::i#2
[8] (byte) main::i#1 ← ++ (byte) main::i#2
[9] if((byte) main::i#1!=(byte) 6) goto main::@1
to:main::@return
main::@return: scope:[main] from main::@1
[10] return
to:@return
VARIABLE REGISTER WEIGHTS
(byte*) SCREEN
(byte) idx memory
(byte) idx#2 memory 7.333333333333333
(byte) idx#5 memory 16.5
(void()) main()
(byte) main::i
(byte) main::i#1 16.5
(byte) main::i#2 14.666666666666666
Initial phi equivalence classes
[ idx#5 idx#2 ]
[ main::i#2 main::i#1 ]
Complete equivalence classes
[ idx#5 idx#2 ]
[ main::i#2 main::i#1 ]
Allocated zp ZP_BYTE:2 [ idx#5 idx#2 ]
Allocated zp ZP_BYTE:3 [ main::i#2 main::i#1 ]
INITIAL ASM
Target platform is c64basic / MOS6502X
// File Comments
// Test declaring a variable as "memory", meaning it will be stored in memory and accessed through an implicit pointer (using load/store)
// Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
.label idx = 2
// @begin
bbegin:
// [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 = 3
// [5] phi from main to main::@1 [phi:main->main::@1]
b1_from_main:
// [5] phi (byte) main::i#2 = (byte) 0 [phi:main->main::@1#0] -- vbuz1=vbuc1
lda #0
sta.z i
// [5] phi (byte) idx#5 = (byte) 0 [phi:main->main::@1#1] -- vbuz1=vbuc1
lda #0
sta.z idx
jmp b1
// [5] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
b1_from_b1:
// [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy
// [5] phi (byte) idx#5 = (byte) idx#2 [phi:main::@1->main::@1#1] -- register_copy
jmp b1
// main::@1
b1:
// [6] *((const byte*) SCREEN#0 + (byte) main::i#2) ← (byte) idx#5 -- pbuc1_derefidx_vbuz1=vbuz2
lda.z idx
ldy.z i
sta SCREEN,y
// [7] (byte) idx#2 ← (byte) idx#5 + (byte) main::i#2 -- vbuz1=vbuz1_plus_vbuz2
lda.z idx
clc
adc.z i
sta.z idx
// [8] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuz1=_inc_vbuz1
inc.z i
// [9] if((byte) main::i#1!=(byte) 6) goto main::@1 -- vbuz1_neq_vbuc1_then_la1
lda #6
cmp.z i
bne b1_from_b1
jmp breturn
// main::@return
breturn:
// [10] return
rts
}
// File Data
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [7] (byte) idx#2 ← (byte) idx#5 + (byte) main::i#2 [ main::i#2 idx#2 ] ( main:2 [ main::i#2 idx#2 ] ) always clobbers reg byte a
Removing always clobbered register reg byte a as potential for zp ZP_BYTE:3 [ main::i#2 main::i#1 ]
Statement [7] (byte) idx#2 ← (byte) idx#5 + (byte) main::i#2 [ main::i#2 idx#2 ] ( main:2 [ main::i#2 idx#2 ] ) always clobbers reg byte a
Potential registers zp ZP_BYTE:2 [ idx#5 idx#2 ] : zp ZP_BYTE:2 , reg byte a , reg byte x , reg byte y ,
Potential registers zp ZP_BYTE:3 [ main::i#2 main::i#1 ] : zp ZP_BYTE:3 , reg byte x , reg byte y ,
REGISTER UPLIFT SCOPES
Uplift Scope [main] 31.17: zp ZP_BYTE:3 [ main::i#2 main::i#1 ]
Uplift Scope [] 23.83: zp ZP_BYTE:2 [ idx#5 idx#2 ]
Uplifting [main] best 423 combination reg byte x [ main::i#2 main::i#1 ]
Uplifting [] best 343 combination reg byte a [ idx#5 idx#2 ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test declaring a variable as "memory", meaning it will be stored in memory and accessed through an implicit pointer (using load/store)
// Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @begin
bbegin:
// [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) 0 [phi:main->main::@1#0] -- vbuxx=vbuc1
ldx #0
// [5] phi (byte) idx#5 = (byte) 0 [phi:main->main::@1#1] -- vbuaa=vbuc1
lda #0
jmp b1
// [5] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
b1_from_b1:
// [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy
// [5] phi (byte) idx#5 = (byte) idx#2 [phi:main::@1->main::@1#1] -- register_copy
jmp b1
// main::@1
b1:
// [6] *((const byte*) SCREEN#0 + (byte) main::i#2) ← (byte) idx#5 -- pbuc1_derefidx_vbuxx=vbuaa
sta SCREEN,x
// [7] (byte) idx#2 ← (byte) idx#5 + (byte) main::i#2 -- vbuaa=vbuaa_plus_vbuxx
stx.z $ff
clc
adc.z $ff
// [8] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx
inx
// [9] if((byte) main::i#1!=(byte) 6) goto main::@1 -- vbuxx_neq_vbuc1_then_la1
cpx #6
bne b1_from_b1
jmp breturn
// main::@return
breturn:
// [10] return
rts
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing instruction lda #0 with TXA
Replacing label b1_from_b1 with b1
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction main_from_b1:
Removing instruction bend_from_b1:
Removing instruction b1_from_b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b1_from_main:
Removing instruction breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction jmp b1
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(byte) idx memory
(byte) idx#2 memory reg byte a 7.333333333333333
(byte) idx#5 memory reg byte a 16.5
(void()) main()
(label) main::@1
(label) main::@return
(byte) main::i
(byte) main::i#1 reg byte x 16.5
(byte) main::i#2 reg byte x 14.666666666666666
reg byte a [ idx#5 idx#2 ]
reg byte x [ main::i#2 main::i#1 ]
FINAL ASSEMBLER
Score: 241
// File Comments
// Test declaring a variable as "memory", meaning it will be stored in memory and accessed through an implicit pointer (using load/store)
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @begin
// [1] phi from @begin to @1 [phi:@begin->@1]
// @1
// [2] call main
// [4] phi from @1 to main [phi:@1->main]
// [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) 0 [phi:main->main::@1#0] -- vbuxx=vbuc1
ldx #0
// [5] phi (byte) idx#5 = (byte) 0 [phi:main->main::@1#1] -- vbuaa=vbuc1
txa
// [5] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
// [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy
// [5] phi (byte) idx#5 = (byte) idx#2 [phi:main::@1->main::@1#1] -- register_copy
// main::@1
b1:
// SCREEN[i] = idx
// [6] *((const byte*) SCREEN#0 + (byte) main::i#2) ← (byte) idx#5 -- pbuc1_derefidx_vbuxx=vbuaa
sta SCREEN,x
// idx +=i
// [7] (byte) idx#2 ← (byte) idx#5 + (byte) main::i#2 -- vbuaa=vbuaa_plus_vbuxx
stx.z $ff
clc
adc.z $ff
// for( char i: 0..5 )
// [8] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx
inx
// [9] if((byte) main::i#1!=(byte) 6) goto main::@1 -- vbuxx_neq_vbuc1_then_la1
cpx #6
bne b1
// main::@return
// }
// [10] return
rts
}
// File Data

View File

@ -0,0 +1,17 @@
(label) @1
(label) @begin
(label) @end
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(byte) idx memory
(byte) idx#2 memory reg byte a 7.333333333333333
(byte) idx#5 memory reg byte a 16.5
(void()) main()
(label) main::@1
(label) main::@return
(byte) main::i
(byte) main::i#1 reg byte x 16.5
(byte) main::i#2 reg byte x 14.666666666666666
reg byte a [ idx#5 idx#2 ]
reg byte x [ main::i#2 main::i#1 ]