1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-09-08 17:54:40 +00:00

Added support for register keyword without parameter for compatibility. Closes #229

This commit is contained in:
jespergravgaard 2019-08-10 08:39:42 +02:00
parent 5755616588
commit 7b155ee4b0
10 changed files with 1202 additions and 694 deletions

View File

@ -79,7 +79,7 @@ directive
: 'const' #directiveConst
| 'extern' #directiveExtern
| 'align' '(' NUMBER ')' #directiveAlign
| 'register' '(' NAME ')' #directiveRegister
| 'register' ( '(' NAME ')')? #directiveRegister
| 'inline' #directiveInline
| 'volatile' #directiveVolatile
| 'interrupt' ( '(' NAME ')' )? #directiveInterrupt

File diff suppressed because it is too large Load Diff

View File

@ -678,11 +678,14 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
}
} else if(directive instanceof DirectiveRegister) {
DirectiveRegister directiveRegister = (DirectiveRegister) directive;
Registers.Register register = Registers.getRegister(directiveRegister.getName());
if(register == null) {
throw new CompileError("Error! Unknown register " + directiveRegister.getName(), source);
if(directiveRegister.getName()!=null) {
// Ignore register directive without parameter (all variables are placed on ZP and attempted register uplift anyways)
Registers.Register register = Registers.getRegister(directiveRegister.getName());
if(register == null) {
throw new CompileError("Error! Unknown register " + directiveRegister.getName(), source);
}
lValue.setDeclaredRegister(register);
}
lValue.setDeclaredRegister(register);
} else {
throw new CompileError("Unsupported variable directive " + directive, source);
}
@ -772,7 +775,10 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override
public Directive visitDirectiveRegister(KickCParser.DirectiveRegisterContext ctx) {
String name = ctx.NAME().getText();
String name = null;
if(ctx.NAME()!=null) {
name = ctx.NAME().getText();
}
return new DirectiveRegister(name);
}

View File

@ -2166,6 +2166,11 @@ public class TestPrograms {
compileAndCompare("var-register");
}
@Test
public void testVarRegisterNoarg() throws IOException, URISyntaxException {
compileAndCompare("var-register-noarg", log());
}
@Test
public void testDword() throws IOException, URISyntaxException {
compileAndCompare("dword");

View File

@ -29,9 +29,11 @@ void fn2() {
const char JMP = 0x4c;
const char NOP = 0xea;
char[] SYSCALLS = {
export char[] SYSCALLS = {
JMP, <&fn1, >&fn1, NOP,
JMP, <&fn2, >&fn2, NOP
};
const void()** FSYSCALLS = (void()**)(SYSCALLS+1);

View File

@ -0,0 +1,12 @@
// Test declaring a variable as register with no information about which register (for compatibility with standard C)
#pragma encoding(screencode_upper)
char* SCREEN = 0x0400;
char[] MSG = "CAMELOT!";
void main() {
register char i=0;
while(i<40*4)
SCREEN[i++] = MSG[i&7];
}

View File

@ -0,0 +1,21 @@
// Test declaring a variable as register with no information about which register (for compatibility with standard C)
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label SCREEN = $400
main: {
ldx #0
b2:
txa
and #7
tay
lda MSG,y
sta SCREEN,x
inx
cpx #$28*4
bcc b2
rts
}
.encoding "screencode_upper"
MSG: .text "CAMELOT!"
.byte 0

View File

@ -0,0 +1,24 @@
@begin: scope:[] from
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
to:@end
@end: scope:[] from @1
[3] phi()
main: scope:[main] from @1
[4] phi()
to:main::@2
main::@2: scope:[main] from main main::@1
[5] (byte) main::i#4 ← phi( main::@1/(byte) main::i#1 main/(byte) 0 )
[6] (byte~) main::$1 ← (byte) main::i#4 & (byte) 7
[7] *((const byte*) SCREEN#0 + (byte) main::i#4) ← *((const byte[]) MSG#0 + (byte~) main::$1)
[8] (byte) main::i#1 ← ++ (byte) main::i#4
to:main::@1
main::@1: scope:[main] from main::@2
[9] if((byte) main::i#1<(byte)(number) $28*(number) 4) goto main::@2
to:main::@return
main::@return: scope:[main] from main::@1
[10] return
to:@return

View File

@ -0,0 +1,410 @@
Identified constant variable (byte*) SCREEN
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*) SCREEN#0 ← ((byte*)) (number) $400
(byte[]) MSG#0 ← (const string) $0
to:@1
main: scope:[main] from @1
(byte) main::i#0 ← (number) 0
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) $28*(number) 4
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 )
(number~) main::$1 ← (byte) main::i#3 & (number) 7
*((byte*) SCREEN#0 + (byte) main::i#3) ← *((byte[]) MSG#0 + (number~) main::$1)
(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
(const string) $0 = (string) "CAMELOT!"su
(label) @1
(label) @2
(label) @begin
(label) @end
(byte[]) MSG
(byte[]) MSG#0
(byte*) SCREEN
(byte*) SCREEN#0
(void()) main()
(bool~) main::$0
(number~) main::$1
(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) 0 in (byte) main::i#0 ← (number) 0
Adding number conversion cast (unumber) $28*4 in (bool~) main::$0 ← (byte) main::i#2 < (number) $28*(number) 4
Adding number conversion cast (unumber) 7 in (number~) main::$1 ← (byte) main::i#3 & (number) 7
Adding number conversion cast (unumber) main::$1 in (number~) main::$1 ← (byte) main::i#3 & (unumber)(number) 7
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (byte*) SCREEN#0 ← (byte*)(number) $400
Inlining cast (byte) main::i#0 ← (unumber)(number) 0
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 0
Simplifying constant integer cast 7
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) 7
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to byte in (unumber~) main::$1 ← (byte) main::i#3 & (byte) 7
Alias (byte) main::i#2 = (byte) main::i#3
Successful SSA optimization Pass2AliasElimination
Simple Condition (bool~) main::$0 [5] if((byte) main::i#2<(byte)(number) $28*(number) 4) goto main::@2
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte*) SCREEN#0 = (byte*) 1024
Constant (const byte[]) MSG#0 = $0
Constant (const byte) main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Successful SSA optimization Pass2LoopHeadConstantIdentification
Alias (byte) main::i#1 = (byte) main::i#2
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) main::i#5 (const byte) main::i#0
Successful SSA optimization Pass2IdenticalPhiElimination
if() condition always true - replacing block destination [10] if((const byte) main::i#0<(byte)(number) $28*(number) 4) goto main::@2
Successful SSA optimization Pass2ConstantIfs
Inlining constant with var siblings (const byte) main::i#0
Constant inlined $0 = (const byte[]) MSG#0
Constant inlined main::i#0 = (byte) 0
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting main::@7(between main::@1 and main::@2)
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
Adding NOP phi() at start of main::@1_1
CALL GRAPH
Calls in [] to main:2
Created 1 initial phi equivalence classes
Coalesced [13] main::i#6 ← main::i#1
Coalesced down to 1 phi equivalence classes
Culled Empty Block (label) @2
Culled Empty Block (label) main::@1_1
Culled Empty Block (label) main::@7
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()
main: scope:[main] from @1
[4] phi()
to:main::@2
main::@2: scope:[main] from main main::@1
[5] (byte) main::i#4 ← phi( main::@1/(byte) main::i#1 main/(byte) 0 )
[6] (byte~) main::$1 ← (byte) main::i#4 & (byte) 7
[7] *((const byte*) SCREEN#0 + (byte) main::i#4) ← *((const byte[]) MSG#0 + (byte~) main::$1)
[8] (byte) main::i#1 ← ++ (byte) main::i#4
to:main::@1
main::@1: scope:[main] from main::@2
[9] if((byte) main::i#1<(byte)(number) $28*(number) 4) goto main::@2
to:main::@return
main::@return: scope:[main] from main::@1
[10] return
to:@return
VARIABLE REGISTER WEIGHTS
(byte[]) MSG
(byte*) SCREEN
(void()) main()
(byte~) main::$1 22.0
(byte) main::i
(byte) main::i#1 16.5
(byte) main::i#4 14.666666666666666
Initial phi equivalence classes
[ main::i#4 main::i#1 ]
Added variable main::$1 to zero page equivalence class [ main::$1 ]
Complete equivalence classes
[ main::i#4 main::i#1 ]
[ main::$1 ]
Allocated zp ZP_BYTE:2 [ main::i#4 main::i#1 ]
Allocated zp ZP_BYTE:3 [ main::$1 ]
INITIAL ASM
Target platform is c64basic
// File Comments
// Test declaring a variable as register with no information about which register (for compatibility with standard C)
// 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: {
.label _1 = 3
.label i = 2
// [5] phi from main to main::@2 [phi:main->main::@2]
b2_from_main:
// [5] phi (byte) main::i#4 = (byte) 0 [phi:main->main::@2#0] -- vbuz1=vbuc1
lda #0
sta.z i
jmp b2
// [5] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
b2_from_b1:
// [5] phi (byte) main::i#4 = (byte) main::i#1 [phi:main::@1->main::@2#0] -- register_copy
jmp b2
// main::@2
b2:
// [6] (byte~) main::$1 ← (byte) main::i#4 & (byte) 7 -- vbuz1=vbuz2_band_vbuc1
lda #7
and.z i
sta.z _1
// [7] *((const byte*) SCREEN#0 + (byte) main::i#4) ← *((const byte[]) MSG#0 + (byte~) main::$1) -- pbuc1_derefidx_vbuz1=pbuc2_derefidx_vbuz2
ldy.z _1
lda MSG,y
ldy.z i
sta SCREEN,y
// [8] (byte) main::i#1 ← ++ (byte) main::i#4 -- vbuz1=_inc_vbuz1
inc.z i
jmp b1
// main::@1
b1:
// [9] if((byte) main::i#1<(byte)(number) $28*(number) 4) goto main::@2 -- vbuz1_lt_vbuc1_then_la1
lda.z i
cmp #$28*4
bcc b2_from_b1
jmp breturn
// main::@return
breturn:
// [10] return
rts
}
// File Data
.encoding "screencode_upper"
MSG: .text "CAMELOT!"
.byte 0
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [7] *((const byte*) SCREEN#0 + (byte) main::i#4) ← *((const byte[]) MSG#0 + (byte~) main::$1) [ main::i#4 ] ( main:2 [ main::i#4 ] ) always clobbers reg byte a
Removing always clobbered register reg byte a as potential for zp ZP_BYTE:2 [ main::i#4 main::i#1 ]
Statement [6] (byte~) main::$1 ← (byte) main::i#4 & (byte) 7 [ main::i#4 main::$1 ] ( main:2 [ main::i#4 main::$1 ] ) always clobbers reg byte a
Statement [7] *((const byte*) SCREEN#0 + (byte) main::i#4) ← *((const byte[]) MSG#0 + (byte~) main::$1) [ main::i#4 ] ( main:2 [ main::i#4 ] ) always clobbers reg byte a
Potential registers zp ZP_BYTE:2 [ main::i#4 main::i#1 ] : zp ZP_BYTE:2 , reg byte x , reg byte y ,
Potential registers zp ZP_BYTE:3 [ main::$1 ] : zp ZP_BYTE:3 , reg byte a , reg byte x , reg byte y ,
REGISTER UPLIFT SCOPES
Uplift Scope [main] 31.17: zp ZP_BYTE:2 [ main::i#4 main::i#1 ] 22: zp ZP_BYTE:3 [ main::$1 ]
Uplift Scope []
Uplifting [main] best 378 combination reg byte x [ main::i#4 main::i#1 ] reg byte a [ main::$1 ]
Uplifting [] best 378 combination
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test declaring a variable as register with no information about which register (for compatibility with standard C)
// 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::@2 [phi:main->main::@2]
b2_from_main:
// [5] phi (byte) main::i#4 = (byte) 0 [phi:main->main::@2#0] -- vbuxx=vbuc1
ldx #0
jmp b2
// [5] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
b2_from_b1:
// [5] phi (byte) main::i#4 = (byte) main::i#1 [phi:main::@1->main::@2#0] -- register_copy
jmp b2
// main::@2
b2:
// [6] (byte~) main::$1 ← (byte) main::i#4 & (byte) 7 -- vbuaa=vbuxx_band_vbuc1
txa
and #7
// [7] *((const byte*) SCREEN#0 + (byte) main::i#4) ← *((const byte[]) MSG#0 + (byte~) main::$1) -- pbuc1_derefidx_vbuxx=pbuc2_derefidx_vbuaa
tay
lda MSG,y
sta SCREEN,x
// [8] (byte) main::i#1 ← ++ (byte) main::i#4 -- vbuxx=_inc_vbuxx
inx
jmp b1
// main::@1
b1:
// [9] if((byte) main::i#1<(byte)(number) $28*(number) 4) goto main::@2 -- vbuxx_lt_vbuc1_then_la1
cpx #$28*4
bcc b2_from_b1
jmp breturn
// main::@return
breturn:
// [10] return
rts
}
// File Data
.encoding "screencode_upper"
MSG: .text "CAMELOT!"
.byte 0
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b2
Removing instruction jmp b1
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing label b2_from_b1 with b2
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction main_from_b1:
Removing instruction bend_from_b1:
Removing instruction b2_from_b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b2_from_main:
Removing instruction b1:
Removing instruction breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction jmp b2
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(byte[]) MSG
(const byte[]) MSG#0 MSG = (string) "CAMELOT!"su
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(void()) main()
(byte~) main::$1 reg byte a 22.0
(label) main::@1
(label) main::@2
(label) main::@return
(byte) main::i
(byte) main::i#1 reg byte x 16.5
(byte) main::i#4 reg byte x 14.666666666666666
reg byte x [ main::i#4 main::i#1 ]
reg byte a [ main::$1 ]
FINAL ASSEMBLER
Score: 246
// File Comments
// Test declaring a variable as register with no information about which register (for compatibility with standard C)
// 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::@2 [phi:main->main::@2]
// [5] phi (byte) main::i#4 = (byte) 0 [phi:main->main::@2#0] -- vbuxx=vbuc1
ldx #0
// [5] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
// [5] phi (byte) main::i#4 = (byte) main::i#1 [phi:main::@1->main::@2#0] -- register_copy
// main::@2
b2:
// i&7
// [6] (byte~) main::$1 ← (byte) main::i#4 & (byte) 7 -- vbuaa=vbuxx_band_vbuc1
txa
and #7
// SCREEN[i++] = MSG[i&7]
// [7] *((const byte*) SCREEN#0 + (byte) main::i#4) ← *((const byte[]) MSG#0 + (byte~) main::$1) -- pbuc1_derefidx_vbuxx=pbuc2_derefidx_vbuaa
tay
lda MSG,y
sta SCREEN,x
// SCREEN[i++] = MSG[i&7];
// [8] (byte) main::i#1 ← ++ (byte) main::i#4 -- vbuxx=_inc_vbuxx
inx
// main::@1
// while(i<40*4)
// [9] if((byte) main::i#1<(byte)(number) $28*(number) 4) goto main::@2 -- vbuxx_lt_vbuc1_then_la1
cpx #$28*4
bcc b2
// main::@return
// }
// [10] return
rts
}
// File Data
.encoding "screencode_upper"
MSG: .text "CAMELOT!"
.byte 0

View File

@ -0,0 +1,18 @@
(label) @1
(label) @begin
(label) @end
(byte[]) MSG
(const byte[]) MSG#0 MSG = (string) "CAMELOT!"su
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(void()) main()
(byte~) main::$1 reg byte a 22.0
(label) main::@1
(label) main::@2
(label) main::@return
(byte) main::i
(byte) main::i#1 reg byte x 16.5
(byte) main::i#4 reg byte x 14.666666666666666
reg byte x [ main::i#4 main::i#1 ]
reg byte a [ main::$1 ]