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

Cleaned up lexer/parser. Added test of using ASM mnemonic as C symbol name.

This commit is contained in:
jespergravgaard 2019-08-25 13:53:54 +02:00
parent 26e96d085a
commit d3fa1f3743
9 changed files with 462 additions and 15 deletions

View File

@ -41,9 +41,6 @@ public class CParser {
/** Names of typedefs. Used by lexer to know the difference between normal value IDENTIFIERS and TYPEIDENTIFIERS */
private List<String> typedefs;
/** True whenever the lexer is expecting an import filename as the next token. */
private boolean modeImport;
/** A C-file that has been imported & parsed. */
public static class CFile {
/** The source file currently being parsed. */
@ -87,14 +84,6 @@ public class CParser {
return typedefs.contains(identifier);
}
public boolean isModeImport() {
return modeImport;
}
public void setModeImport(boolean modeImport) {
this.modeImport = modeImport;
}
/** Get the underlying token stream.
*
* @return The token stream

View File

@ -7,9 +7,15 @@ tokens { TYPEDEFNAME }
}
@lexer::members {
/** The C-Parser. Used for importing C-files and communicating with the Parser about typedefs. */
CParser cParser;
/** True of the next string is the name of a C-file to import*/
boolean importEnter = false;
/** True if the next CURLY starts ASM_MODE */
boolean asmEnter = false;
/** Counts the nested curlies inside ASM_MODE to determine when to exit ASM_MODE */
int asmCurlyCount = 0;
public KickCLexer(CharStream input, CParser cParser) {
@ -57,7 +63,7 @@ ASSIGN: '=' ;
ASSIGN_COMPOUND : '+=' | '-=' | '*=' | '/=' | '%=' | '<<=' | '>>=' | '&=' | '|=' | '^=' ;
// Keywords
IMPORT: 'import' { cParser.setModeImport(true); } ;
IMPORT: 'import' { importEnter=true; } ;
TYPEDEF: 'typedef' ;
PRAGMA: '#pragma' ;
RESERVE:'reserve' ;
@ -105,7 +111,7 @@ KICKASM_BODY: '{{' .*? '}}';
// Strings and chars - with special handling of imports
STRING : '"' ('\\"' | ~'"')* '"' [z]?([ps][mu]?)?[z]? { if(cParser.isModeImport()) { cParser.setModeImport(false); cParser.loadCFile(getText()); } } ;
STRING : '"' ('\\"' | ~'"')* '"' [z]?([ps][mu]?)?[z]? { if(importEnter) { importEnter=false; cParser.loadCFile(getText()); } } ;
CHAR : '\'' ('\\'['"rfn] | ~'\'' ) '\'';
// Numbers

View File

@ -151,9 +151,15 @@ public class KickCLexer extends Lexer {
}
/** The C-Parser. Used for importing C-files and communicating with the Parser about typedefs. */
CParser cParser;
/** True of the next string is the name of a C-file to import*/
boolean importEnter = false;
/** True if the next CURLY starts ASM_MODE */
boolean asmEnter = false;
/** Counts the nested curlies inside ASM_MODE to determine when to exit ASM_MODE */
int asmCurlyCount = 0;
public KickCLexer(CharStream input, CParser cParser) {
@ -221,7 +227,7 @@ public class KickCLexer extends Lexer {
private void IMPORT_action(RuleContext _localctx, int actionIndex) {
switch (actionIndex) {
case 1:
cParser.setModeImport(true);
importEnter=true;
break;
}
}
@ -235,7 +241,7 @@ public class KickCLexer extends Lexer {
private void STRING_action(RuleContext _localctx, int actionIndex) {
switch (actionIndex) {
case 3:
if(cParser.isModeImport()) { cParser.setModeImport(false); cParser.loadCFile(getText()); }
if(importEnter) { importEnter=false; cParser.loadCFile(getText()); }
break;
}
}

View File

@ -37,6 +37,11 @@ public class TestPrograms {
}
@Test
public void testAsmMnemonicNames() throws IOException, URISyntaxException {
compileAndCompare("asm-mnemonic-names");
}
@Test
public void testParseNegatedStructRef() throws IOException, URISyntaxException {
compileAndCompare("parse-negated-struct-ref");

View File

@ -0,0 +1,21 @@
// Test using an ASM mnemonic as a C symbol names
// Works if the C-lexer and the ASM-lexer are separated properly
const char* lda = 0x0400;
void main() {
char jmp = 1;
*lda = jmp;
bne(jmp);
// Inline asm using the mnemonics
asm {
lda a
jmp a
bne a
a:
}
}
void bne(char jsr) {
lda[1] = jsr;
}

View File

@ -0,0 +1,22 @@
// Test using an ASM mnemonic as a C symbol names
// Works if the C-lexer and the ASM-lexer are separated properly
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label lda = $400
main: {
.label jmp = 1
lda #jmp
sta lda
jsr bne
// Inline asm using the mnemonics
lda a
rts
a:
rts
}
bne: {
lda #main.jmp
sta lda+1
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()
main: scope:[main] from @1
[4] *((const byte*) lda#0) ← (const byte) main::jmp#0
[5] call bne
to:main::@1
main::@1: scope:[main] from main
asm { ldaa jmpa bnea a: }
to:main::@return
main::@return: scope:[main] from main::@1
[7] return
to:@return
bne: scope:[bne] from main
[8] *((const byte*) lda#0+(byte) 1) ← (const byte) main::jmp#0
to:bne::@return
bne::@return: scope:[bne] from bne
[9] return
to:@return

View File

@ -0,0 +1,359 @@
Identified constant variable (byte) main::jmp
Culled Empty Block (label) @1
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
(byte*) lda#0 ← ((byte*)) (number) $400
to:@2
main: scope:[main] from @2
(byte) main::jmp#0 ← (number) 1
*((byte*) lda#0) ← (byte) main::jmp#0
(byte) bne::jsr#0 ← (byte) main::jmp#0
call bne
to:main::@1
main::@1: scope:[main] from main
asm { ldaa jmpa bnea a: }
to:main::@return
main::@return: scope:[main] from main::@1
return
to:@return
bne: scope:[bne] from main
(byte) bne::jsr#1 ← phi( main/(byte) bne::jsr#0 )
*((byte*) lda#0 + (number) 1) ← (byte) bne::jsr#1
to:bne::@return
bne::@return: scope:[bne] from bne
return
to:@return
@2: scope:[] from @begin
call main
to:@3
@3: scope:[] from @2
to:@end
@end: scope:[] from @3
SYMBOL TABLE SSA
(label) @2
(label) @3
(label) @begin
(label) @end
(void()) bne((byte) bne::jsr)
(label) bne::@return
(byte) bne::jsr
(byte) bne::jsr#0
(byte) bne::jsr#1
(byte*) lda
(byte*) lda#0
(void()) main()
(label) main::@1
(label) main::@return
(byte) main::jmp
(byte) main::jmp#0
Adding number conversion cast (unumber) 1 in (byte) main::jmp#0 ← (number) 1
Adding number conversion cast (unumber) 1 in *((byte*) lda#0 + (number) 1) ← (byte) bne::jsr#1
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (byte*) lda#0 ← (byte*)(number) $400
Inlining cast (byte) main::jmp#0 ← (unumber)(number) 1
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 1
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 1
Finalized unsigned number type (byte) 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Identical Phi Values (byte) bne::jsr#1 (byte) bne::jsr#0
Successful SSA optimization Pass2IdenticalPhiElimination
Constant (const byte*) lda#0 = (byte*) 1024
Constant (const byte) main::jmp#0 = 1
Successful SSA optimization Pass2ConstantIdentification
Constant (const byte) bne::jsr#0 = main::jmp#0
Successful SSA optimization Pass2ConstantIdentification
Constant inlined bne::jsr#0 = (const byte) main::jmp#0
Successful SSA optimization Pass2ConstantInlining
Consolidated array index constant in *(lda#0+1)
Successful SSA optimization Pass2ConstantAdditionElimination
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @2
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
CALL GRAPH
Calls in [] to main:2
Calls in [main] to bne:6
Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Culled Empty Block (label) @3
Renumbering block @2 to @1
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end
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] *((const byte*) lda#0) ← (const byte) main::jmp#0
[5] call bne
to:main::@1
main::@1: scope:[main] from main
asm { ldaa jmpa bnea a: }
to:main::@return
main::@return: scope:[main] from main::@1
[7] return
to:@return
bne: scope:[bne] from main
[8] *((const byte*) lda#0+(byte) 1) ← (const byte) main::jmp#0
to:bne::@return
bne::@return: scope:[bne] from bne
[9] return
to:@return
VARIABLE REGISTER WEIGHTS
(void()) bne((byte) bne::jsr)
(byte) bne::jsr
(byte*) lda
(void()) main()
(byte) main::jmp
Initial phi equivalence classes
Complete equivalence classes
INITIAL ASM
Target platform is c64basic
// File Comments
// Test using an ASM mnemonic as a C symbol names
// Works if the C-lexer and the ASM-lexer are separated properly
// Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label lda = $400
// @begin
bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
jmp b1
// @1
b1:
// [2] call main
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
jmp bend
// @end
bend:
// main
main: {
.label jmp = 1
// [4] *((const byte*) lda#0) ← (const byte) main::jmp#0 -- _deref_pbuc1=vbuc2
lda #jmp
sta lda
// [5] call bne
jsr bne
jmp b1
// main::@1
b1:
// asm { ldaa jmpa bnea a: }
// Inline asm using the mnemonics
lda a
jmp a
bne a
a:
jmp breturn
// main::@return
breturn:
// [7] return
rts
}
// bne
bne: {
// [8] *((const byte*) lda#0+(byte) 1) ← (const byte) main::jmp#0 -- _deref_pbuc1=vbuc2
lda #main.jmp
sta lda+1
jmp breturn
// bne::@return
breturn:
// [9] return
rts
}
// File Data
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] *((const byte*) lda#0) ← (const byte) main::jmp#0 [ ] ( main:2 [ ] ) always clobbers reg byte a
Statement asm { ldaa jmpa bnea a: } always clobbers reg byte a
Statement [8] *((const byte*) lda#0+(byte) 1) ← (const byte) main::jmp#0 [ ] ( main:2::bne:5 [ ] ) always clobbers reg byte a
REGISTER UPLIFT SCOPES
Uplift Scope [main]
Uplift Scope [bne]
Uplift Scope []
Uplifting [main] best 60 combination
Uplifting [bne] best 60 combination
Uplifting [] best 60 combination
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test using an ASM mnemonic as a C symbol names
// Works if the C-lexer and the ASM-lexer are separated properly
// Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label lda = $400
// @begin
bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
jmp b1
// @1
b1:
// [2] call main
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
jmp bend
// @end
bend:
// main
main: {
.label jmp = 1
// [4] *((const byte*) lda#0) ← (const byte) main::jmp#0 -- _deref_pbuc1=vbuc2
lda #jmp
sta lda
// [5] call bne
jsr bne
jmp b1
// main::@1
b1:
// asm { ldaa jmpa bnea a: }
// Inline asm using the mnemonics
lda a
jmp a
bne a
a:
jmp breturn
// main::@return
breturn:
// [7] return
rts
}
// bne
bne: {
// [8] *((const byte*) lda#0+(byte) 1) ← (const byte) main::jmp#0 -- _deref_pbuc1=vbuc2
lda #main.jmp
sta lda+1
jmp breturn
// bne::@return
breturn:
// [9] return
rts
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction bne a
Removing instruction jmp breturn
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction bend_from_b1:
Removing instruction breturn:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b1:
Removing instruction breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Replacing jump to rts with rts in jmp a
Succesful ASM optimization Pass5DoubleJumpElimination
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(void()) bne((byte) bne::jsr)
(label) bne::@return
(byte) bne::jsr
(byte*) lda
(const byte*) lda#0 lda = (byte*) 1024
(void()) main()
(label) main::@1
(label) main::@return
(byte) main::jmp
(const byte) main::jmp#0 jmp = (byte) 1
FINAL ASSEMBLER
Score: 40
// File Comments
// Test using an ASM mnemonic as a C symbol names
// Works if the C-lexer and the ASM-lexer are separated properly
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.label lda = $400
// @begin
// [1] phi from @begin to @1 [phi:@begin->@1]
// @1
// [2] call main
// [3] phi from @1 to @end [phi:@1->@end]
// @end
// main
main: {
.label jmp = 1
// *lda = jmp
// [4] *((const byte*) lda#0) ← (const byte) main::jmp#0 -- _deref_pbuc1=vbuc2
lda #jmp
sta lda
// bne(jmp)
// [5] call bne
jsr bne
// main::@1
// asm
// asm { ldaa jmpa bnea a: }
// Inline asm using the mnemonics
lda a
rts
a:
// main::@return
// }
// [7] return
rts
}
// bne
bne: {
// lda[1] = jsr
// [8] *((const byte*) lda#0+(byte) 1) ← (const byte) main::jmp#0 -- _deref_pbuc1=vbuc2
lda #main.jmp
sta lda+1
// bne::@return
// }
// [9] return
rts
}
// File Data

View File

@ -0,0 +1,14 @@
(label) @1
(label) @begin
(label) @end
(void()) bne((byte) bne::jsr)
(label) bne::@return
(byte) bne::jsr
(byte*) lda
(const byte*) lda#0 lda = (byte*) 1024
(void()) main()
(label) main::@1
(label) main::@return
(byte) main::jmp
(const byte) main::jmp#0 jmp = (byte) 1