1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-26 12:49:21 +00:00

Now parenthesizing all #pragmas. Closes #512

This commit is contained in:
jespergravgaard 2020-08-27 23:40:10 +02:00
parent 86cd93c0d9
commit 1cac21d8e1
8 changed files with 463 additions and 6 deletions

View File

@ -37,12 +37,12 @@ public class StatementSource implements Serializable {
public static StatementSource NONE = new StatementSource(null, null, null, 0, 0); public static StatementSource NONE = new StatementSource(null, null, null, 0, 0);
public StatementSource(Token tokenStart, Token tokenStop) { public StatementSource(Token tokenStart, Token tokenStop) {
if(tokenStart != null) { if(tokenStart != null && tokenStart.getStartIndex() != 0) {
this.startIndex = tokenStart.getStartIndex(); this.startIndex = tokenStart.getStartIndex();
CharStream stream = tokenStart.getInputStream(); CharStream stream = tokenStart.getInputStream();
this.fileName = stream.getSourceName(); this.fileName = stream.getSourceName();
this.lineNumber = tokenStart.getLine(); this.lineNumber = tokenStart.getLine();
if(tokenStop != null) { if(tokenStop != null && tokenStop.getStopIndex() != 0) {
this.stopIndex = tokenStop.getStopIndex(); this.stopIndex = tokenStop.getStopIndex();
Interval interval = getInterval(); Interval interval = getInterval();
this.code = stream.getText(interval); this.code = stream.getText(interval);

View File

@ -195,17 +195,29 @@ public class CPreprocessor implements TokenSource {
} }
// Forward #pragma to parser // Forward #pragma to parser
// Convert space-based pragma to parenthesis-based for easier parsing
// #pragma NAME XXX YYY \n => #pragma NAME ( XXX , YYY ) \n
final ArrayList<Token> pragmaTokens = new ArrayList<>(); final ArrayList<Token> pragmaTokens = new ArrayList<>();
pragmaTokens.add(inputToken); pragmaTokens.add(inputToken);
pragmaTokens.addAll(ws); pragmaTokens.addAll(ws);
pragmaTokens.add(pragmaType); pragmaTokens.add(pragmaType);
pragmaTokens.addAll(skipWhitespace(cTokenSource)); pragmaTokens.addAll(skipWhitespace(cTokenSource));
ArrayList<Token> pragmaBody = readBody(cTokenSource); ArrayList<Token> pragmaBody = readBody(cTokenSource);
if(pragmaBody.get(0).getType() != KickCLexer.PAR_BEGIN) { final Token pragmaBodyStart = pragmaBody.get(0);
// Convert space-based pragma to parenthesis-based for easier parsing
// #pragma NAME XXX YYY \n => #pragma NAME ( XXX , YYY ) \n
if(pragmaBodyStart.getType() != KickCLexer.PAR_BEGIN) {
ArrayList<Token> parenthesizedBody = new ArrayList<>();
parenthesizedBody.add(new CommonToken(KickCLexer.PAR_BEGIN, "(" ));
// Parenthesize the parameter list // Parenthesize the parameter list
throw new InternalError("TODO: Parenthesize #pragmas!"); boolean first = true;
for(Token token : pragmaBody) {
if(token.getChannel() != CParser.CHANNEL_WHITESPACE && !first) {
parenthesizedBody.add(new CommonToken(KickCLexer.COMMA, "," ));
}
parenthesizedBody.add(token);
first = false;
}
parenthesizedBody.add(new CommonToken(KickCLexer.PAR_END, ")" ));
pragmaBody = parenthesizedBody;
} }
pragmaTokens.addAll(pragmaBody); pragmaTokens.addAll(pragmaBody);
// Pass on the #pragma to the parser - and mark it as already handled // Pass on the #pragma to the parser - and mark it as already handled

View File

@ -42,6 +42,11 @@ public class TestPrograms {
public TestPrograms() { public TestPrograms() {
} }
@Test
public void testPragmaNoParenthesis() throws IOException, URISyntaxException {
compileAndCompare("pragma-noparenthesis.c");
}
@Test @Test
public void testPragmaUnknown() throws IOException, URISyntaxException { public void testPragmaUnknown() throws IOException, URISyntaxException {
compileAndCompare("pragma-unknown.c"); compileAndCompare("pragma-unknown.c");

View File

@ -0,0 +1,14 @@
// Test #pragma without parenthesis
#pragma target vic20
#pragma constructor_for init main
char * const SCREEN = 0x0400;
void main() {
SCREEN[1] = 'b';
}
void init() {
SCREEN[0] = 'a';
}

View File

@ -0,0 +1,32 @@
// Test #pragma without parenthesis
.file [name="pragma-noparenthesis.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$1001]
.segmentdef Code [start=$100e]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(__start)
.segment Code
.label SCREEN = $400
.segment Code
__start: {
jsr init
jsr main
rts
}
init: {
// SCREEN[0] = 'a'
lda #'a'
sta SCREEN
// }
rts
}
main: {
// SCREEN[1] = 'b'
lda #'b'
sta SCREEN+1
// }
rts
}

View File

@ -0,0 +1,32 @@
(void()) __start()
__start: scope:[__start] from
[0] phi()
to:__start::__init1
__start::__init1: scope:[__start] from __start
[1] phi()
[2] call init
to:__start::@1
__start::@1: scope:[__start] from __start::__init1
[3] phi()
[4] call main
to:__start::@return
__start::@return: scope:[__start] from __start::@1
[5] return
to:@return
(void()) init()
init: scope:[init] from __start::__init1
[6] *((const nomodify byte*) SCREEN) ← (byte) 'a'
to:init::@return
init::@return: scope:[init] from init
[7] return
to:@return
(void()) main()
main: scope:[main] from __start::@1
[8] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte) 'b'
to:main::@return
main::@return: scope:[main] from main
[9] return
to:@return

View File

@ -0,0 +1,352 @@
Inlined call call __init
CONTROL FLOW GRAPH SSA
(void()) main()
main: scope:[main] from __start::@1
*((const nomodify byte*) SCREEN + (number) 1) ← (byte) 'b'
to:main::@return
main::@return: scope:[main] from main
return
to:@return
(void()) init()
init: scope:[init] from __start::__init1
*((const nomodify byte*) SCREEN + (number) 0) ← (byte) 'a'
to:init::@return
init::@return: scope:[init] from init
return
to:@return
(void()) __start()
__start: scope:[__start] from
to:__start::__init1
__start::__init1: scope:[__start] from __start
call init
to:__start::@2
__start::@2: scope:[__start] from __start::__init1
to:__start::@1
__start::@1: scope:[__start] from __start::@2
call main
to:__start::@3
__start::@3: scope:[__start] from __start::@1
to:__start::@return
__start::@return: scope:[__start] from __start::@3
return
to:@return
SYMBOL TABLE SSA
(const nomodify byte*) SCREEN = (byte*)(number) $400
(void()) __start()
(label) __start::@1
(label) __start::@2
(label) __start::@3
(label) __start::@return
(label) __start::__init1
(void()) init()
(label) init::@return
(void()) main()
(label) main::@return
Adding number conversion cast (unumber) 1 in *((const nomodify byte*) SCREEN + (number) 1) ← (byte) 'b'
Adding number conversion cast (unumber) 0 in *((const nomodify byte*) SCREEN + (number) 0) ← (byte) 'a'
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 1
Simplifying constant integer cast 0
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 1
Finalized unsigned number type (byte) 0
Successful SSA optimization PassNFinalizeNumberTypeConversions
Simplifying expression containing zero SCREEN in [2] *((const nomodify byte*) SCREEN + (byte) 0) ← (byte) 'a'
Successful SSA optimization PassNSimplifyExpressionWithZero
Consolidated array index constant in *(SCREEN+1)
Successful SSA optimization Pass2ConstantAdditionElimination
Adding NOP phi() at start of __start
Adding NOP phi() at start of __start::__init1
Adding NOP phi() at start of __start::@2
Adding NOP phi() at start of __start::@1
Adding NOP phi() at start of __start::@3
CALL GRAPH
Calls in [__start] to init:2 main:5
Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Culled Empty Block (label) __start::@2
Culled Empty Block (label) __start::@3
Adding NOP phi() at start of __start
Adding NOP phi() at start of __start::__init1
Adding NOP phi() at start of __start::@1
FINAL CONTROL FLOW GRAPH
(void()) __start()
__start: scope:[__start] from
[0] phi()
to:__start::__init1
__start::__init1: scope:[__start] from __start
[1] phi()
[2] call init
to:__start::@1
__start::@1: scope:[__start] from __start::__init1
[3] phi()
[4] call main
to:__start::@return
__start::@return: scope:[__start] from __start::@1
[5] return
to:@return
(void()) init()
init: scope:[init] from __start::__init1
[6] *((const nomodify byte*) SCREEN) ← (byte) 'a'
to:init::@return
init::@return: scope:[init] from init
[7] return
to:@return
(void()) main()
main: scope:[main] from __start::@1
[8] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte) 'b'
to:main::@return
main::@return: scope:[main] from main
[9] return
to:@return
VARIABLE REGISTER WEIGHTS
(void()) __start()
(void()) init()
(void()) main()
Initial phi equivalence classes
Complete equivalence classes
INITIAL ASM
Target platform is vic20 / MOS6502X
// File Comments
// Test #pragma without parenthesis
// Upstart
.file [name="pragma-noparenthesis.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$1001]
.segmentdef Code [start=$100e]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(__start)
.segment Code
// Global Constants & labels
.label SCREEN = $400
.segment Code
// __start
__start: {
// [1] phi from __start to __start::__init1 [phi:__start->__start::__init1]
__init1_from___start:
jmp __init1
// __start::__init1
__init1:
// [2] call init
jsr init
// [3] phi from __start::__init1 to __start::@1 [phi:__start::__init1->__start::@1]
__b1_from___init1:
jmp __b1
// __start::@1
__b1:
// [4] call main
jsr main
jmp __breturn
// __start::@return
__breturn:
// [5] return
rts
}
// init
init: {
// [6] *((const nomodify byte*) SCREEN) ← (byte) 'a' -- _deref_pbuc1=vbuc2
lda #'a'
sta SCREEN
jmp __breturn
// init::@return
__breturn:
// [7] return
rts
}
// main
main: {
// [8] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte) 'b' -- _deref_pbuc1=vbuc2
lda #'b'
sta SCREEN+1
jmp __breturn
// main::@return
__breturn:
// [9] return
rts
}
// File Data
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [6] *((const nomodify byte*) SCREEN) ← (byte) 'a' [ ] ( [ ] { } init:2 [ ] { } ) always clobbers reg byte a
Statement [8] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte) 'b' [ ] ( main:4 [ ] { } ) always clobbers reg byte a
REGISTER UPLIFT SCOPES
Uplift Scope [main]
Uplift Scope [init]
Uplift Scope [__start]
Uplift Scope []
Uplifting [main] best 57 combination
Uplifting [init] best 57 combination
Uplifting [__start] best 57 combination
Uplifting [] best 57 combination
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test #pragma without parenthesis
// Upstart
.file [name="pragma-noparenthesis.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$1001]
.segmentdef Code [start=$100e]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(__start)
.segment Code
// Global Constants & labels
.label SCREEN = $400
.segment Code
// __start
__start: {
// [1] phi from __start to __start::__init1 [phi:__start->__start::__init1]
__init1_from___start:
jmp __init1
// __start::__init1
__init1:
// [2] call init
jsr init
// [3] phi from __start::__init1 to __start::@1 [phi:__start::__init1->__start::@1]
__b1_from___init1:
jmp __b1
// __start::@1
__b1:
// [4] call main
jsr main
jmp __breturn
// __start::@return
__breturn:
// [5] return
rts
}
// init
init: {
// [6] *((const nomodify byte*) SCREEN) ← (byte) 'a' -- _deref_pbuc1=vbuc2
lda #'a'
sta SCREEN
jmp __breturn
// init::@return
__breturn:
// [7] return
rts
}
// main
main: {
// [8] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte) 'b' -- _deref_pbuc1=vbuc2
lda #'b'
sta SCREEN+1
jmp __breturn
// main::@return
__breturn:
// [9] return
rts
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __init1
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __init1_from___start:
Removing instruction __b1_from___init1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __init1:
Removing instruction __b1:
Removing instruction __breturn:
Removing instruction __breturn:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(const nomodify byte*) SCREEN = (byte*) 1024
(void()) __start()
(label) __start::@1
(label) __start::@return
(label) __start::__init1
(void()) init()
(label) init::@return
(void()) main()
(label) main::@return
FINAL ASSEMBLER
Score: 42
// File Comments
// Test #pragma without parenthesis
// Upstart
.file [name="pragma-noparenthesis.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$1001]
.segmentdef Code [start=$100e]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(__start)
.segment Code
// Global Constants & labels
.label SCREEN = $400
.segment Code
// __start
__start: {
// [1] phi from __start to __start::__init1 [phi:__start->__start::__init1]
// __start::__init1
// [2] call init
jsr init
// [3] phi from __start::__init1 to __start::@1 [phi:__start::__init1->__start::@1]
// __start::@1
// [4] call main
jsr main
// __start::@return
// [5] return
rts
}
// init
init: {
// SCREEN[0] = 'a'
// [6] *((const nomodify byte*) SCREEN) ← (byte) 'a' -- _deref_pbuc1=vbuc2
lda #'a'
sta SCREEN
// init::@return
// }
// [7] return
rts
}
// main
main: {
// SCREEN[1] = 'b'
// [8] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte) 'b' -- _deref_pbuc1=vbuc2
lda #'b'
sta SCREEN+1
// main::@return
// }
// [9] return
rts
}
// File Data

View File

@ -0,0 +1,10 @@
(const nomodify byte*) SCREEN = (byte*) 1024
(void()) __start()
(label) __start::@1
(label) __start::@return
(label) __start::__init1
(void()) init()
(label) init::@return
(void()) main()
(label) main::@return