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

Added main method to flipper. Fixed a few bugs in compiler.

This commit is contained in:
Jesper Gravgaard 2017-07-16 11:09:32 +02:00
parent f6eeb5ff83
commit a7e0e2b1d0
11 changed files with 2425 additions and 1949 deletions

View File

@ -12,6 +12,7 @@ public class CompileLog {
public void append(String msg) { public void append(String msg) {
log.append(msg); log.append(msg);
log.append("\n"); log.append("\n");
//System.out.printf(msg+"\n");
} }
public StringBuilder getLog() { public StringBuilder getLog() {

View File

@ -43,15 +43,22 @@ public class Compiler {
} }
} }
public CompilationResult compile(final CharStream input) throws IOException { public CompilationResult compile(final CharStream input) {
CompileLog log = new CompileLog(); CompileLog log = new CompileLog();
try {
log.append(input.toString()); log.append(input.toString());
KickCLexer lexer = new KickCLexer(input); KickCLexer lexer = new KickCLexer(input);
KickCParser parser = new KickCParser(new CommonTokenStream(lexer)); KickCParser parser = new KickCParser(new CommonTokenStream(lexer));
parser.setBuildParseTree(true); parser.setBuildParseTree(true);
parser.addErrorListener(new BaseErrorListener() { parser.addErrorListener(new BaseErrorListener() {
@Override @Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { public void syntaxError(
Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {
throw new RuntimeException("Error parsing file " + input.getSourceName() + "\n - Line: " + line + "\n - Message: " + msg); throw new RuntimeException("Error parsing file " + input.getSourceName() + "\n - Line: " + line + "\n - Message: " + msg);
} }
}); });
@ -122,7 +129,9 @@ public class Compiler {
} }
} }
Pass3BlockSequencePlanner pass3BlockSequencePlanner = new Pass3BlockSequencePlanner(controlFlowGraph, programScope); Pass3BlockSequencePlanner pass3BlockSequencePlanner = new Pass3BlockSequencePlanner(
controlFlowGraph,
programScope);
pass3BlockSequencePlanner.plan(); pass3BlockSequencePlanner.plan();
Pass3RegisterAllocation pass3RegisterAllocation = new Pass3RegisterAllocation(controlFlowGraph, programScope); Pass3RegisterAllocation pass3RegisterAllocation = new Pass3RegisterAllocation(controlFlowGraph, programScope);
pass3RegisterAllocation.allocate(); pass3RegisterAllocation.allocate();
@ -149,7 +158,16 @@ public class Compiler {
} }
} }
log.append("FINAL SYMBOL TABLE");
log.append(programScope.getSymbolTableContents());
log.append("FINAL CODE");
log.append(asmProgram.toString());
return new CompilationResult(asmProgram, controlFlowGraph, programScope, log); return new CompilationResult(asmProgram, controlFlowGraph, programScope, log);
} catch (Exception e) {
System.out.println(log.getLog());
throw e;
}
} }
} }

View File

@ -1,8 +1,8 @@
Features Features
- Move the main code into a main() function, and disallow code outside functions. The main function per default has no parameters and exits with RTS. - Move the main code into a main() function, and disallow code outside functions. The main function per default has no parameters and exits with RTS.
- Add a for loop for(init;condition;increment) {stmt} -> { init; do { stmt; increment } while (condition) }
- Implement Register Allocation (that utilize real registers - and non-zeropage memory) - Implement Register Allocation (that utilize real registers - and non-zeropage memory)
- Add Fixed Point number types - Add Fixed Point number types
- Add a for loop for(init;condition;increment) {stmt} -> { init; do { stmt; increment } while (condition) }
- Add imports - Add imports
- Add structs - Add structs
- Let { stmt } introduce a new anonymous scope. - Let { stmt } introduce a new anonymous scope.

View File

@ -44,7 +44,8 @@ public class Pass1ProcedureCallParameters extends ControlFlowGraphCopyVisitor {
// No return type. Remove variable receiving the result. // No return type. Remove variable receiving the result.
LValue lValue = origCall.getLValue(); LValue lValue = origCall.getLValue();
if(lValue instanceof Variable) { if(lValue instanceof Variable) {
scope.remove((Variable) lValue); Variable lVar = (Variable) lValue;
lVar.getScope().remove(lVar);
} }
} }

View File

@ -62,7 +62,7 @@ public class Pass2CullEmptyBlocks extends Pass2SsaOptimization {
}; };
phiFixVisitor.visitBlock(successor); phiFixVisitor.visitBlock(successor);
getGraph().getAllBlocks().remove(removeBlock); getGraph().getAllBlocks().remove(removeBlock);
getSymbols().remove(removeBlock.getLabel()); removeBlock.getLabel().getScope().remove(removeBlock.getLabel());
log.append("Culled Empty Block " + removeBlock.getLabel()); log.append("Culled Empty Block " + removeBlock.getLabel());
} }
return remove.size()>0; return remove.size()>0;

View File

@ -139,13 +139,13 @@ public class Pass3RegisterAllocation {
allocation.allocate(symbols.getVariable("flip::$0"), RegisterAllocation.getRegisterA()); allocation.allocate(symbols.getVariable("flip::$0"), RegisterAllocation.getRegisterA());
allocation.allocate(symbols.getVariable("flip::$8"), RegisterAllocation.getRegisterA()); allocation.allocate(symbols.getVariable("flip::$8"), RegisterAllocation.getRegisterA());
allocation.allocate(symbols.getVariable("flip::$4"), RegisterAllocation.getRegisterA()); allocation.allocate(symbols.getVariable("flip::$4"), RegisterAllocation.getRegisterA());
allocation.allocate(symbols.getVariable("$1"), RegisterAllocation.getRegisterA()); allocation.allocate(symbols.getVariable("main::$1"), RegisterAllocation.getRegisterA());
allocation.allocate(symbols.getVariable("$3"), RegisterAllocation.getRegisterA()); allocation.allocate(symbols.getVariable("main::$3"), RegisterAllocation.getRegisterA());
allocation.allocate(symbols.getVariable("c#0"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("main::c#0"), RegisterAllocation.getRegisterX());
allocation.allocate(symbols.getVariable("c#1"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("main::c#1"), RegisterAllocation.getRegisterX());
allocation.allocate(symbols.getVariable("c#2"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("main::c#2"), RegisterAllocation.getRegisterX());
allocation.allocate(symbols.getVariable("c#3"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("main::c#3"), RegisterAllocation.getRegisterX());
allocation.allocate(symbols.getVariable("c#4"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("main::c#4"), RegisterAllocation.getRegisterX());
symbols.setAllocation(allocation); symbols.setAllocation(allocation);
} }

View File

@ -1,9 +1,11 @@
byte[1000] SCREEN = $0400; byte[1000] SCREEN = $0400;
byte[16*16] buffer1 = $1000; byte[16*16] buffer1 = $1000;
byte[16*16] buffer2 = $1100; byte[16*16] buffer2 = $1100;
byte *RASTER = $d012; byte *RASTER = $d012;
main();
void main() {
prepare(); prepare();
do { do {
byte c = 25; byte c = 25;
@ -15,6 +17,7 @@ do {
flip(); flip();
plot(); plot();
} while(true) } while(true)
}
// Prepare buffer // Prepare buffer
void prepare() { void prepare() {

View File

@ -1,31 +1,35 @@
BBEGIN: BBEGIN:
jsr main
BEND:
main:
jsr prepare jsr prepare
B2_from_BBEGIN: main__B2_from_main:
ldx #25 ldx #25
jmp B2 jmp main__B2
B2_from_B20: main__B2_from_B18:
ldx #25 ldx #25
B2_from_B6: main__B2_from_B2:
B2: main__B2:
B3: main__B3:
lda 53266 lda 53266
cmp #254 cmp #254
bne B3 bne main__main__B3
B4: main__B4:
lda 53266 lda 53266
cmp #255 cmp #255
bne B4 bne main__main__B4
B6: B2:
dex dex
cpx #0 cpx #0
bne B2_from_B6 bne main__B2_from_B2
B7: B3:
jsr flip jsr flip
B19: B17:
jsr plot jsr plot
B20: B18:
jmp B2_from_B20 jmp main__B2_from_B18
BEND: main__Breturn:
rts
plot: plot:
plot__B1_from_plot: plot__B1_from_plot:
lda #16 lda #16
@ -35,7 +39,7 @@ plot__B1_from_plot:
sta 101 sta 101
lda #>1236 lda #>1236
sta 101+1 sta 101+1
plot__B1_from_B15: plot__B1_from_B12:
plot__B1: plot__B1:
plot__B2_from_B1: plot__B2_from_B1:
ldy #0 ldy #0
@ -47,7 +51,7 @@ plot__B2:
iny iny
cpy #16 cpy #16
bcc plot__B2_from_B2 bcc plot__B2_from_B2
B15: B12:
lda 101 lda 101
clc clc
adc #40 adc #40
@ -57,7 +61,7 @@ B15:
!: !:
dec 100 dec 100
lda 100 lda 100
bne plot__B1_from_B15 bne plot__B1_from_B12
plot__Breturn: plot__Breturn:
rts rts
flip: flip:
@ -66,7 +70,7 @@ flip__B1_from_flip:
sta 104 sta 104
ldx #0 ldx #0
ldy #15 ldy #15
flip__B1_from_B11: flip__B1_from_B8:
flip__B1: flip__B1:
flip__B2_from_B1: flip__B2_from_B1:
lda #16 lda #16
@ -83,12 +87,12 @@ flip__B2:
dec 103 dec 103
lda 103 lda 103
bne flip__B2_from_B2 bne flip__B2_from_B2
B11: B8:
dey dey
dec 104 dec 104
lda 104 lda 104
bne flip__B1_from_B11 bne flip__B1_from_B8
flip__B3_from_B11: flip__B3_from_B8:
ldx #0 ldx #0
flip__B3_from_B3: flip__B3_from_B3:
flip__B3: flip__B3:

View File

@ -1,31 +1,37 @@
@BEGIN: from @BEGIN: from
call prepare param-assignment call main param-assignment
to:@2
@2: from @20 @6 @BEGIN
(byte) c#2 ← phi( @20/(byte) 25 @6/(byte) c#1 @BEGIN/(byte) 25 )
to:@3
@3: from @2 @3
(byte~) $1 ← * (word) 53266
if((byte~) $1!=(byte) 254) goto @3
to:@4
@4: from @3 @4
(byte~) $3 ← * (word) 53266
if((byte~) $3!=(byte) 255) goto @4
to:@6
@6: from @4
(byte) c#1 ← -- (byte) c#2
if((byte) c#1!=(byte) 0) goto @2
to:@7
@7: from @6
call flip param-assignment
to:@19
@19: from @7
call plot param-assignment
to:@20
@20: from @19
if(true) goto @2
to:@END to:@END
prepare: from @BEGIN main: from @BEGIN
call prepare param-assignment
to:main::@2
main::@2: from @18 @2 main
(byte) main::c#2 ← phi( @2/(byte) main::c#1 @18/(byte) 25 main/(byte) 25 )
to:main::@3
main::@3: from main::@2 main::@3
(byte~) main::$1 ← * (word) 53266
if((byte~) main::$1!=(byte) 254) goto main::@3
to:main::@4
main::@4: from main::@3 main::@4
(byte~) main::$3 ← * (word) 53266
if((byte~) main::$3!=(byte) 255) goto main::@4
to:@2
@2: from main::@4
(byte) main::c#1 ← -- (byte) main::c#2
if((byte) main::c#1!=(byte) 0) goto main::@2
to:@3
@3: from @2
call flip param-assignment
to:@17
@17: from @3
call plot param-assignment
to:@18
@18: from @17
if(true) goto main::@2
to:main::@return
main::@return: from @18
return
to:@RETURN
prepare: from main
to:prepare::@1 to:prepare::@1
prepare::@1: from prepare prepare::@1 prepare::@1: from prepare prepare::@1
(byte) prepare::i#2 ← phi( prepare/(byte) 0 prepare::@1/(byte) prepare::i#1 ) (byte) prepare::i#2 ← phi( prepare/(byte) 0 prepare::@1/(byte) prepare::i#1 )
@ -36,12 +42,12 @@ prepare::@1: from prepare prepare::@1
prepare::@return: from prepare::@1 prepare::@return: from prepare::@1
return return
to:@RETURN to:@RETURN
flip: from @7 flip: from @3
to:flip::@1 to:flip::@1
flip::@1: from @11 flip flip::@1: from @8 flip
(byte) flip::r#2 ← phi( @11/(byte) flip::r#1 flip/(byte) 16 ) (byte) flip::r#2 ← phi( @8/(byte) flip::r#1 flip/(byte) 16 )
(byte) flip::srcIdx#3 ← phi( @11/(byte) flip::srcIdx#1 flip/(byte) 0 ) (byte) flip::srcIdx#3 ← phi( @8/(byte) flip::srcIdx#1 flip/(byte) 0 )
(byte) flip::dstIdx#5 ← phi( @11/(byte) flip::dstIdx#2 flip/(byte) 15 ) (byte) flip::dstIdx#5 ← phi( @8/(byte) flip::dstIdx#2 flip/(byte) 15 )
to:flip::@2 to:flip::@2
flip::@2: from flip::@1 flip::@2 flip::@2: from flip::@1 flip::@2
(byte) flip::c#2 ← phi( flip::@1/(byte) 16 flip::@2/(byte) flip::c#1 ) (byte) flip::c#2 ← phi( flip::@1/(byte) 16 flip::@2/(byte) flip::c#1 )
@ -53,14 +59,14 @@ flip::@2: from flip::@1 flip::@2
(byte) flip::dstIdx#1 ← (byte) flip::dstIdx#3 + (byte) 16 (byte) flip::dstIdx#1 ← (byte) flip::dstIdx#3 + (byte) 16
(byte) flip::c#1 ← -- (byte) flip::c#2 (byte) flip::c#1 ← -- (byte) flip::c#2
if((byte) flip::c#1!=(byte) 0) goto flip::@2 if((byte) flip::c#1!=(byte) 0) goto flip::@2
to:@11 to:@8
@11: from flip::@2 @8: from flip::@2
(byte) flip::dstIdx#2 ← -- (byte) flip::dstIdx#1 (byte) flip::dstIdx#2 ← -- (byte) flip::dstIdx#1
(byte) flip::r#1 ← -- (byte) flip::r#2 (byte) flip::r#1 ← -- (byte) flip::r#2
if((byte) flip::r#1!=(byte) 0) goto flip::@1 if((byte) flip::r#1!=(byte) 0) goto flip::@1
to:flip::@3 to:flip::@3
flip::@3: from @11 flip::@3 flip::@3: from @8 flip::@3
(byte) flip::i#2 ← phi( @11/(byte) 0 flip::@3/(byte) flip::i#1 ) (byte) flip::i#2 ← phi( @8/(byte) 0 flip::@3/(byte) flip::i#1 )
(byte~) flip::$4 ← (word) 4352 *idx (byte) flip::i#2 (byte~) flip::$4 ← (word) 4352 *idx (byte) flip::i#2
*((word) 4096 + (byte) flip::i#2) ← (byte~) flip::$4 *((word) 4096 + (byte) flip::i#2) ← (byte~) flip::$4
(byte) flip::i#1 ← ++ (byte) flip::i#2 (byte) flip::i#1 ← ++ (byte) flip::i#2
@ -69,12 +75,12 @@ flip::@3: from @11 flip::@3
flip::@return: from flip::@3 flip::@return: from flip::@3
return return
to:@RETURN to:@RETURN
plot: from @19 plot: from @17
to:plot::@1 to:plot::@1
plot::@1: from @15 plot plot::@1: from @12 plot
(byte) plot::y#2 ← phi( @15/(byte) plot::y#1 plot/(byte) 16 ) (byte) plot::y#2 ← phi( @12/(byte) plot::y#1 plot/(byte) 16 )
(byte) plot::i#3 ← phi( @15/(byte) plot::i#1 plot/(byte) 0 ) (byte) plot::i#3 ← phi( @12/(byte) plot::i#1 plot/(byte) 0 )
(byte*) plot::line#2 ← phi( @15/(byte*) plot::line#1 plot/(word) 1236 ) (byte*) plot::line#2 ← phi( @12/(byte*) plot::line#1 plot/(word) 1236 )
to:plot::@2 to:plot::@2
plot::@2: from plot::@1 plot::@2 plot::@2: from plot::@1 plot::@2
(byte) plot::x#2 ← phi( plot::@1/(byte) 0 plot::@2/(byte) plot::x#1 ) (byte) plot::x#2 ← phi( plot::@1/(byte) 0 plot::@2/(byte) plot::x#1 )
@ -84,13 +90,13 @@ plot::@2: from plot::@1 plot::@2
(byte) plot::i#1 ← ++ (byte) plot::i#2 (byte) plot::i#1 ← ++ (byte) plot::i#2
(byte) plot::x#1 ← ++ (byte) plot::x#2 (byte) plot::x#1 ← ++ (byte) plot::x#2
if((byte) plot::x#1<(byte) 16) goto plot::@2 if((byte) plot::x#1<(byte) 16) goto plot::@2
to:@15 to:@12
@15: from plot::@2 @12: from plot::@2
(byte*) plot::line#1 ← (byte*) plot::line#2 + (byte) 40 (byte*) plot::line#1 ← (byte*) plot::line#2 + (byte) 40
(byte) plot::y#1 ← -- (byte) plot::y#2 (byte) plot::y#1 ← -- (byte) plot::y#2
if((byte) plot::y#1!=(byte) 0) goto plot::@1 if((byte) plot::y#1!=(byte) 0) goto plot::@1
to:plot::@return to:plot::@return
plot::@return: from @15 plot::@return: from @12
return return
to:@RETURN to:@RETURN
@END: from @20 @END: from @BEGIN

File diff suppressed because it is too large Load Diff

View File

@ -1,23 +1,15 @@
(byte~) $1 reg byte a (label) @12
(byte~) $3 reg byte a (label) @17
(label) @11 (label) @18
(label) @15
(label) @19
(label) @2 (label) @2
(label) @20
(label) @3 (label) @3
(label) @4 (label) @8
(label) @6
(label) @7
(label) @BEGIN (label) @BEGIN
(label) @END (label) @END
(byte*) RASTER (byte*) RASTER
(byte[1000]) SCREEN (byte[1000]) SCREEN
(byte[256]) buffer1 (byte[256]) buffer1
(byte[256]) buffer2 (byte[256]) buffer2
(byte) c
(byte) c#1 reg byte x
(byte) c#2 reg byte x
(void()) flip() (void()) flip()
(byte~) flip::$0 reg byte a (byte~) flip::$0 reg byte a
(byte~) flip::$4 reg byte a (byte~) flip::$4 reg byte a
@ -44,6 +36,17 @@
(byte) flip::srcIdx#2 reg byte x (byte) flip::srcIdx#2 reg byte x
(byte) flip::srcIdx#3 reg byte x (byte) flip::srcIdx#3 reg byte x
(void()) main()
(byte~) main::$1 reg byte a
(byte~) main::$3 reg byte a
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@return
(byte) main::c
(byte) main::c#1 reg byte x
(byte) main::c#2 reg byte x
(void()) plot() (void()) plot()
(byte~) plot::$3 reg byte a (byte~) plot::$3 reg byte a
(label) plot::@1 (label) plot::@1