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

Implemented switch()-statement - first simple test working. #170

This commit is contained in:
jespergravgaard 2019-08-12 21:46:01 +02:00
parent 46b88d8cf0
commit c9a3f33773
8 changed files with 885 additions and 12 deletions

View File

@ -122,6 +122,18 @@ public class StatementSource implements Serializable {
return new StatementSource(nodeStart, nodeStop);
}
public static StatementSource switchCase(KickCParser.SwitchCaseContext ctx) {
ParseTree nodeStart = ctx;
ParseTree nodeStop = ctx.getChild(ctx.getChildCount() - 1);
return new StatementSource(nodeStart, nodeStop);
}
public static StatementSource switchDefault(KickCParser.SwitchCasesContext ctx) {
ParseTree nodeStart = ctx.getChild(ctx.getChildCount() - 2);
ParseTree nodeStop = ctx.getChild(ctx.getChildCount() - 1);
return new StatementSource(nodeStart, nodeStop);
}
public static StatementSource procedureEnd(KickCParser.DeclFunctionContext ctx) {
ParseTree nodeStart = ctx.getChild(ctx.getChildCount() - 1);
ParseTree nodeStop = ctx;

View File

@ -399,7 +399,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
String varName = ctx.NAME().getText();
SymbolRef variableRef;
Symbol symbol = getCurrentScope().getSymbol(varName);
if(symbol!=null) {
if(symbol != null) {
//Found an existing variable
variableRef = symbol.getRef();
} else {
@ -681,7 +681,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
}
} else if(directive instanceof DirectiveRegister) {
DirectiveRegister directiveRegister = (DirectiveRegister) directive;
if(directiveRegister.getName()!=null) {
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) {
@ -779,7 +779,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override
public Directive visitDirectiveRegister(KickCParser.DirectiveRegisterContext ctx) {
String name = null;
if(ctx.NAME()!=null) {
if(ctx.NAME() != null) {
name = ctx.NAME().getText();
}
return new DirectiveRegister(name);
@ -975,7 +975,6 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override
public Object visitStmtSwitch(KickCParser.StmtSwitchContext ctx) {
/*
// Create a block scope - to keep all statements of the loop inside it
BlockScope blockScope = getCurrentScope().addBlockScope();
scopeStack.push(blockScope);
@ -984,20 +983,61 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
// TODO: Add comments to next stmt
// Evaluate the switch-expression
PrePostModifierHandler.addPreModifiers(this, ctx.commaExpr(), StatementSource.switchExpr(ctx));
RValue rValue = (RValue) this.visit(ctx.commaExpr());
RValue eValue = (RValue) this.visit(ctx.commaExpr());
PrePostModifierHandler.addPostModifiers(this, ctx.commaExpr(), StatementSource.switchExpr(ctx));
// TODO: Iterate cases
// TODO: Handle default
// Add case conditional jumps
List<SwitchCaseBody> caseBodies = new ArrayList<>();
for(KickCParser.SwitchCaseContext caseContext : ctx.switchCases().switchCase()) {
List<Comment> caseComments = ensureUnusedComments(getCommentsSymbol(caseContext));
RValue cValue = (RValue) this.visit(caseContext.expr());
Label cJumpLabel = getCurrentScope().addLabelIntermediate();
caseBodies.add(new SwitchCaseBody(cJumpLabel, caseContext.stmtSeq(), StatementSource.switchCase(caseContext)));
StatementConditionalJump cJmpStmt = new StatementConditionalJump(eValue, Operators.EQ, cValue, cJumpLabel.getRef(), StatementSource.switchCase(caseContext), caseComments);
sequence.addStatement(cJmpStmt);
}
// Add default ending jump
Label dJumpLabel = getCurrentScope().addLabelIntermediate();
StatementJump dJmpStmt = new StatementJump(dJumpLabel.getRef(), StatementSource.switchDefault(ctx.switchCases()), Comment.NO_COMMENTS);
sequence.addStatement(dJmpStmt);
// Add case labels & bodies
for(SwitchCaseBody caseBody : caseBodies) {
StatementLabel cJumpTarget = new StatementLabel(caseBody.cJumpLabel.getRef(), caseBody.statementSource, Comment.NO_COMMENTS);
sequence.addStatement(cJumpTarget);
if(caseBody.stmtSequence != null) {
this.visit(caseBody.stmtSequence);
}
}
// Add default label
StatementLabel dJumpTarget = new StatementLabel(dJumpLabel.getRef(), StatementSource.switchDefault(ctx.switchCases()), Comment.NO_COMMENTS);
sequence.addStatement(dJumpTarget);
if(ctx.switchCases().stmtSeq() != null) {
this.visit(ctx.switchCases().stmtSeq());
}
// TODO: Do something to handle continue!
addLoopBreakLabel(loopStack.pop(), ctx);
scopeStack.pop();
return null;
*/
throw new InternalError("switch() is not supported in this version of the compiler.");
}
/** Holds the body of a switch() case. */
public static class SwitchCaseBody {
/** Label for the statments of the case. */
Label cJumpLabel;
/** Statments of the case. */
KickCParser.StmtSeqContext stmtSequence;
/** Source of the case. */
StatementSource statementSource;
public SwitchCaseBody(Label cJumpLabel, KickCParser.StmtSeqContext stmtSequence, StatementSource statementSource) {
this.cJumpLabel = cJumpLabel;
this.stmtSequence = stmtSequence;
this.statementSource = statementSource;
}
}
@Override
public Object visitStmtFor(KickCParser.StmtForContext ctx) {
this.visit(ctx.forLoop());
@ -1247,7 +1287,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
if(!definedLabels.contains(label)) {
// Look for the symbol
Symbol symbol = getCurrentScope().getSymbol(ctxLabel.NAME().getText());
if(symbol!=null) {
if(symbol != null) {
referenced.put(label, symbol.getRef());
} else {
// Either forward reference or a non-existing variable. Create a forward reference for later resolving.

View File

@ -36,6 +36,11 @@ public class TestPrograms {
public TestPrograms() {
}
@Test
public void testSwitch0() throws IOException, URISyntaxException {
compileAndCompare("switch-0");
}
@Test
public void testInlineAsmParam() throws IOException, URISyntaxException {
compileAndCompare("inline-asm-param");

27
src/test/kc/switch-0.kc Normal file
View File

@ -0,0 +1,27 @@
// Tests simple switch()-statement
// Expected output 'd1444d'
void main() {
const char* SCREEN = 0x0400;
for(char i:0..5) {
// Test switching on a simple char
switch(i) {
// A simple case with a break
case 1:
SCREEN[i] = '1';
break;
// A case with no body
case 2:
// A case with fall-through
case 3:
SCREEN[i] = '3';
case 4:
SCREEN[i] = '4';
break;
// No case for 0 & 5
default:
SCREEN[i] = 'd';
}
}
}

42
src/test/ref/switch-0.asm Normal file
View File

@ -0,0 +1,42 @@
// Tests simple switch()-statement
// Expected output 'd1444d'
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
main: {
.label SCREEN = $400
ldx #0
b5:
lda #'d'
sta SCREEN,x
b6:
inx
cpx #6
bne b1
rts
b1:
// A simple case with a break
cpx #1
beq b2
// A case with no body
cpx #2
beq b3
// A case with fall-through
cpx #3
beq b3
cpx #4
beq b4
jmp b5
b4:
lda #'4'
sta SCREEN,x
jmp b6
b3:
lda #'3'
sta SCREEN,x
jmp b4
b2:
lda #'1'
sta SCREEN,x
jmp b6
}

45
src/test/ref/switch-0.cfg Normal file
View File

@ -0,0 +1,45 @@
@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::@5
main::@5: scope:[main] from main main::@9
[5] (byte) main::i#29 ← phi( main::@9/(byte) main::i#1 main/(byte) 0 )
[6] *((const byte*) main::SCREEN#0 + (byte) main::i#29) ← (byte) 'd'
to:main::@6
main::@6: scope:[main] from main::@2 main::@4 main::@5
[7] (byte) main::i#18 ← phi( main::@2/(byte) main::i#1 main::@4/(byte) main::i#1 main::@5/(byte) main::i#29 )
[8] (byte) main::i#1 ← ++ (byte) main::i#18
[9] if((byte) main::i#1!=(byte) 6) goto main::@1
to:main::@return
main::@return: scope:[main] from main::@6
[10] return
to:@return
main::@1: scope:[main] from main::@6
[11] if((byte) main::i#1==(byte) 1) goto main::@2
to:main::@7
main::@7: scope:[main] from main::@1
[12] if((byte) main::i#1==(byte) 2) goto main::@3
to:main::@8
main::@8: scope:[main] from main::@7
[13] if((byte) main::i#1==(byte) 3) goto main::@3
to:main::@9
main::@9: scope:[main] from main::@8
[14] if((byte) main::i#1==(byte) 4) goto main::@4
to:main::@5
main::@4: scope:[main] from main::@3 main::@9
[15] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '4'
to:main::@6
main::@3: scope:[main] from main::@7 main::@8
[16] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '3'
to:main::@4
main::@2: scope:[main] from main::@1
[17] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '1'
to:main::@6

680
src/test/ref/switch-0.log Normal file
View File

@ -0,0 +1,680 @@
Culled Empty Block (label) main::@3
Culled Empty Block (label) main::@14
Culled Empty Block (label) main::@15
Culled Empty Block (label) main::@8
Culled Empty Block (label) main::@9
Culled Empty Block (label) main::@10
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
to:@1
main: scope:[main] from @1
(byte*) main::SCREEN#0 ← ((byte*)) (number) $400
(byte) main::i#0 ← (byte) 0
to:main::@1
main::@1: scope:[main] from main main::@7
(byte) main::i#2 ← phi( main/(byte) main::i#0 main::@7/(byte) main::i#1 )
if((byte) main::i#2==(number) 1) goto main::@2
to:main::@11
main::@2: scope:[main] from main::@1
(byte) main::i#3 ← phi( main::@1/(byte) main::i#2 )
*((byte*) main::SCREEN#0 + (byte) main::i#3) ← (byte) '1'
to:main::@7
main::@11: scope:[main] from main::@1
(byte) main::i#4 ← phi( main::@1/(byte) main::i#2 )
if((byte) main::i#4==(number) 2) goto main::@4
to:main::@12
main::@12: scope:[main] from main::@11
(byte) main::i#5 ← phi( main::@11/(byte) main::i#4 )
if((byte) main::i#5==(number) 3) goto main::@4
to:main::@13
main::@4: scope:[main] from main::@11 main::@12
(byte) main::i#6 ← phi( main::@11/(byte) main::i#4 main::@12/(byte) main::i#5 )
*((byte*) main::SCREEN#0 + (byte) main::i#6) ← (byte) '3'
to:main::@5
main::@13: scope:[main] from main::@12
(byte) main::i#7 ← phi( main::@12/(byte) main::i#5 )
if((byte) main::i#7==(number) 4) goto main::@5
to:main::@6
main::@5: scope:[main] from main::@13 main::@4
(byte) main::i#8 ← phi( main::@13/(byte) main::i#7 main::@4/(byte) main::i#6 )
*((byte*) main::SCREEN#0 + (byte) main::i#8) ← (byte) '4'
to:main::@7
main::@6: scope:[main] from main::@13
(byte) main::i#9 ← phi( main::@13/(byte) main::i#7 )
*((byte*) main::SCREEN#0 + (byte) main::i#9) ← (byte) 'd'
to:main::@7
main::@7: scope:[main] from main::@2 main::@5 main::@6
(byte) main::i#10 ← phi( main::@2/(byte) main::i#3 main::@5/(byte) main::i#8 main::@6/(byte) main::i#9 )
(byte) main::i#1 ← (byte) main::i#10 + 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::@7
return
to:@return
@1: scope:[] from @begin
call main
to:@2
@2: scope:[] from @1
to:@end
@end: scope:[] from @2
SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @begin
(label) @end
(void()) main()
(bool~) main::$0
(label) main::@1
(label) main::@11
(label) main::@12
(label) main::@13
(label) main::@2
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@7
(label) main::@return
(byte*) main::SCREEN
(byte*) main::SCREEN#0
(byte) main::i
(byte) main::i#0
(byte) main::i#1
(byte) main::i#10
(byte) main::i#2
(byte) main::i#3
(byte) main::i#4
(byte) main::i#5
(byte) main::i#6
(byte) main::i#7
(byte) main::i#8
(byte) main::i#9
Adding number conversion cast (unumber) 1 in if((byte) main::i#2==(number) 1) goto main::@2
Adding number conversion cast (unumber) 2 in if((byte) main::i#4==(number) 2) goto main::@4
Adding number conversion cast (unumber) 3 in if((byte) main::i#5==(number) 3) goto main::@4
Adding number conversion cast (unumber) 4 in if((byte) main::i#7==(number) 4) goto main::@5
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (byte*) main::SCREEN#0 ← (byte*)(number) $400
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 1
Simplifying constant integer cast 2
Simplifying constant integer cast 3
Simplifying constant integer cast 4
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 1
Finalized unsigned number type (byte) 2
Finalized unsigned number type (byte) 3
Finalized unsigned number type (byte) 4
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias (byte) main::i#2 = (byte) main::i#3 (byte) main::i#4 (byte) main::i#5 (byte) main::i#7 (byte) main::i#9
Successful SSA optimization Pass2AliasElimination
Alias (byte) main::i#2 = (byte) main::i#6
Successful SSA optimization Pass2AliasElimination
Alias (byte) main::i#2 = (byte) main::i#8
Successful SSA optimization Pass2AliasElimination
Alias (byte) main::i#10 = (byte) main::i#2
Successful SSA optimization Pass2AliasElimination
Simple Condition (bool~) main::$0 [21] if((byte) main::i#1!=rangelast(0,5)) goto main::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte*) main::SCREEN#0 = (byte*) 1024
Constant (const byte) main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value [19] main::i#1 ← ++ main::i#10 to ++
Resolved ranged comparison value [21] if(main::i#1!=rangelast(0,5)) goto main::@1 to (number) 6
Successful SSA optimization Pass2LoopHeadConstantIdentification
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
Alias (byte) main::i#1 = (byte) main::i#10
Alias (byte) main::i#12 = (byte) main::i#13 (byte) main::i#15 (byte) main::i#17
Successful SSA optimization Pass2AliasElimination
Alias (byte) main::i#12 = (byte) main::i#14
Successful SSA optimization Pass2AliasElimination
Alias (byte) main::i#12 = (byte) main::i#16
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) main::i#19 (const byte) main::i#0
Successful SSA optimization Pass2IdenticalPhiElimination
Removing PHI-reference to removed block (main::@1_1) in block main::@2
if() condition always false - eliminating [22] if((const byte) main::i#0==(byte) 1) goto main::@2
Successful SSA optimization Pass2ConstantIfs
Successful SSA optimization Pass2LoopHeadConstantIdentification
Alias (byte) main::i#1 = (byte) main::i#11 (byte) main::i#12
Alias (byte) main::i#20 = (byte) main::i#22 (byte) main::i#24
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) main::i#25 (const byte) main::i#0
Successful SSA optimization Pass2IdenticalPhiElimination
Removing PHI-reference to removed block (main::@11_1) in block main::@4
if() condition always false - eliminating [21] if((const byte) main::i#0==(byte) 2) goto main::@4
Successful SSA optimization Pass2ConstantIfs
Successful SSA optimization Pass2LoopHeadConstantIdentification
Alias (byte) main::i#1 = (byte) main::i#20
Alias (byte) main::i#26 = (byte) main::i#27
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) main::i#28 (const byte) main::i#0
Successful SSA optimization Pass2IdenticalPhiElimination
Removing PHI-reference to removed block (main::@12_1) in block main::@4
if() condition always false - eliminating [19] if((const byte) main::i#0==(byte) 3) goto main::@4
Successful SSA optimization Pass2ConstantIfs
Successful SSA optimization Pass2LoopHeadConstantIdentification
Alias (byte) main::i#1 = (byte) main::i#21 (byte) main::i#26
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) main::i#30 (const byte) main::i#0
Successful SSA optimization Pass2IdenticalPhiElimination
Removing PHI-reference to removed block (main::@13_1) in block main::@5
if() condition always false - eliminating [18] if((const byte) main::i#0==(byte) 4) goto main::@5
Successful SSA optimization Pass2ConstantIfs
Alias (byte) main::i#1 = (byte) main::i#23
Successful SSA optimization Pass2AliasElimination
Inlining constant with var siblings (const byte) main::i#0
Constant inlined main::i#0 = (byte) 0
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting main::@16(between main::@13 and main::@6)
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
Adding NOP phi() at start of main::@11_1
Adding NOP phi() at start of main::@12_1
Adding NOP phi() at start of main::@13_1
CALL GRAPH
Calls in [] to main:2
Created 2 initial phi equivalence classes
Coalesced [12] main::i#34 ← main::i#29
Coalesced [21] main::i#31 ← main::i#1
Coalesced (already) [23] main::i#33 ← main::i#1
Coalesced (already) [26] main::i#32 ← 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::@11_1
Culled Empty Block (label) main::@12_1
Culled Empty Block (label) main::@13_1
Culled Empty Block (label) main::@16
Renumbering block main::@4 to main::@3
Renumbering block main::@5 to main::@4
Renumbering block main::@6 to main::@5
Renumbering block main::@7 to main::@6
Renumbering block main::@11 to main::@7
Renumbering block main::@12 to main::@8
Renumbering block main::@13 to main::@9
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::@5
main::@5: scope:[main] from main main::@9
[5] (byte) main::i#29 ← phi( main::@9/(byte) main::i#1 main/(byte) 0 )
[6] *((const byte*) main::SCREEN#0 + (byte) main::i#29) ← (byte) 'd'
to:main::@6
main::@6: scope:[main] from main::@2 main::@4 main::@5
[7] (byte) main::i#18 ← phi( main::@2/(byte) main::i#1 main::@4/(byte) main::i#1 main::@5/(byte) main::i#29 )
[8] (byte) main::i#1 ← ++ (byte) main::i#18
[9] if((byte) main::i#1!=(byte) 6) goto main::@1
to:main::@return
main::@return: scope:[main] from main::@6
[10] return
to:@return
main::@1: scope:[main] from main::@6
[11] if((byte) main::i#1==(byte) 1) goto main::@2
to:main::@7
main::@7: scope:[main] from main::@1
[12] if((byte) main::i#1==(byte) 2) goto main::@3
to:main::@8
main::@8: scope:[main] from main::@7
[13] if((byte) main::i#1==(byte) 3) goto main::@3
to:main::@9
main::@9: scope:[main] from main::@8
[14] if((byte) main::i#1==(byte) 4) goto main::@4
to:main::@5
main::@4: scope:[main] from main::@3 main::@9
[15] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '4'
to:main::@6
main::@3: scope:[main] from main::@7 main::@8
[16] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '3'
to:main::@4
main::@2: scope:[main] from main::@1
[17] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '1'
to:main::@6
VARIABLE REGISTER WEIGHTS
(void()) main()
(byte*) main::SCREEN
(byte) main::i
(byte) main::i#1 134.66666666666669
(byte) main::i#18 314.0
(byte) main::i#29 61.5
Initial phi equivalence classes
[ main::i#18 main::i#29 main::i#1 ]
Complete equivalence classes
[ main::i#18 main::i#29 main::i#1 ]
Allocated zp ZP_BYTE:2 [ main::i#18 main::i#29 main::i#1 ]
INITIAL ASM
Target platform is c64basic
// File Comments
// Tests simple switch()-statement
// Expected output 'd1444d'
// Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
// Global Constants & labels
// @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 SCREEN = $400
.label i = 2
// [5] phi from main to main::@5 [phi:main->main::@5]
b5_from_main:
// [5] phi (byte) main::i#29 = (byte) 0 [phi:main->main::@5#0] -- vbuz1=vbuc1
lda #0
sta.z i
jmp b5
// main::@5
b5:
// [6] *((const byte*) main::SCREEN#0 + (byte) main::i#29) ← (byte) 'd' -- pbuc1_derefidx_vbuz1=vbuc2
lda #'d'
ldy.z i
sta SCREEN,y
// [7] phi from main::@2 main::@4 main::@5 to main::@6 [phi:main::@2/main::@4/main::@5->main::@6]
b6_from_b2:
b6_from_b4:
b6_from_b5:
// [7] phi (byte) main::i#18 = (byte) main::i#1 [phi:main::@2/main::@4/main::@5->main::@6#0] -- register_copy
jmp b6
// main::@6
b6:
// [8] (byte) main::i#1 ← ++ (byte) main::i#18 -- 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
jmp breturn
// main::@return
breturn:
// [10] return
rts
// main::@1
b1:
// [11] if((byte) main::i#1==(byte) 1) goto main::@2 -- vbuz1_eq_vbuc1_then_la1
// A simple case with a break
lda #1
cmp.z i
beq b2
jmp b7
// main::@7
b7:
// [12] if((byte) main::i#1==(byte) 2) goto main::@3 -- vbuz1_eq_vbuc1_then_la1
// A case with no body
lda #2
cmp.z i
beq b3
jmp b8
// main::@8
b8:
// [13] if((byte) main::i#1==(byte) 3) goto main::@3 -- vbuz1_eq_vbuc1_then_la1
// A case with fall-through
lda #3
cmp.z i
beq b3
jmp b9
// main::@9
b9:
// [14] if((byte) main::i#1==(byte) 4) goto main::@4 -- vbuz1_eq_vbuc1_then_la1
lda #4
cmp.z i
beq b4
// [5] phi from main::@9 to main::@5 [phi:main::@9->main::@5]
b5_from_b9:
// [5] phi (byte) main::i#29 = (byte) main::i#1 [phi:main::@9->main::@5#0] -- register_copy
jmp b5
// main::@4
b4:
// [15] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '4' -- pbuc1_derefidx_vbuz1=vbuc2
lda #'4'
ldy.z i
sta SCREEN,y
jmp b6_from_b4
// main::@3
b3:
// [16] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '3' -- pbuc1_derefidx_vbuz1=vbuc2
lda #'3'
ldy.z i
sta SCREEN,y
jmp b4
// main::@2
b2:
// [17] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '1' -- pbuc1_derefidx_vbuz1=vbuc2
lda #'1'
ldy.z i
sta SCREEN,y
jmp b6_from_b2
}
// File Data
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [6] *((const byte*) main::SCREEN#0 + (byte) main::i#29) ← (byte) 'd' [ main::i#29 ] ( main:2 [ main::i#29 ] ) always clobbers reg byte a
Removing always clobbered register reg byte a as potential for zp ZP_BYTE:2 [ main::i#18 main::i#29 main::i#1 ]
Statement [15] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '4' [ main::i#1 ] ( main:2 [ main::i#1 ] ) always clobbers reg byte a
Statement [16] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '3' [ main::i#1 ] ( main:2 [ main::i#1 ] ) always clobbers reg byte a
Statement [17] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '1' [ main::i#1 ] ( main:2 [ main::i#1 ] ) always clobbers reg byte a
Statement [6] *((const byte*) main::SCREEN#0 + (byte) main::i#29) ← (byte) 'd' [ main::i#29 ] ( main:2 [ main::i#29 ] ) always clobbers reg byte a
Statement [15] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '4' [ main::i#1 ] ( main:2 [ main::i#1 ] ) always clobbers reg byte a
Statement [16] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '3' [ main::i#1 ] ( main:2 [ main::i#1 ] ) always clobbers reg byte a
Statement [17] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '1' [ main::i#1 ] ( main:2 [ main::i#1 ] ) always clobbers reg byte a
Potential registers zp ZP_BYTE:2 [ main::i#18 main::i#29 main::i#1 ] : zp ZP_BYTE:2 , reg byte x , reg byte y ,
REGISTER UPLIFT SCOPES
Uplift Scope [main] 510.17: zp ZP_BYTE:2 [ main::i#18 main::i#29 main::i#1 ]
Uplift Scope []
Uplifting [main] best 7118 combination reg byte x [ main::i#18 main::i#29 main::i#1 ]
Uplifting [] best 7118 combination
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Tests simple switch()-statement
// Expected output 'd1444d'
// Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
// Global Constants & labels
// @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 SCREEN = $400
// [5] phi from main to main::@5 [phi:main->main::@5]
b5_from_main:
// [5] phi (byte) main::i#29 = (byte) 0 [phi:main->main::@5#0] -- vbuxx=vbuc1
ldx #0
jmp b5
// main::@5
b5:
// [6] *((const byte*) main::SCREEN#0 + (byte) main::i#29) ← (byte) 'd' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'d'
sta SCREEN,x
// [7] phi from main::@2 main::@4 main::@5 to main::@6 [phi:main::@2/main::@4/main::@5->main::@6]
b6_from_b2:
b6_from_b4:
b6_from_b5:
// [7] phi (byte) main::i#18 = (byte) main::i#1 [phi:main::@2/main::@4/main::@5->main::@6#0] -- register_copy
jmp b6
// main::@6
b6:
// [8] (byte) main::i#1 ← ++ (byte) main::i#18 -- vbuxx=_inc_vbuxx
inx
// [9] if((byte) main::i#1!=(byte) 6) goto main::@1 -- vbuxx_neq_vbuc1_then_la1
cpx #6
bne b1
jmp breturn
// main::@return
breturn:
// [10] return
rts
// main::@1
b1:
// [11] if((byte) main::i#1==(byte) 1) goto main::@2 -- vbuxx_eq_vbuc1_then_la1
// A simple case with a break
cpx #1
beq b2
jmp b7
// main::@7
b7:
// [12] if((byte) main::i#1==(byte) 2) goto main::@3 -- vbuxx_eq_vbuc1_then_la1
// A case with no body
cpx #2
beq b3
jmp b8
// main::@8
b8:
// [13] if((byte) main::i#1==(byte) 3) goto main::@3 -- vbuxx_eq_vbuc1_then_la1
// A case with fall-through
cpx #3
beq b3
jmp b9
// main::@9
b9:
// [14] if((byte) main::i#1==(byte) 4) goto main::@4 -- vbuxx_eq_vbuc1_then_la1
cpx #4
beq b4
// [5] phi from main::@9 to main::@5 [phi:main::@9->main::@5]
b5_from_b9:
// [5] phi (byte) main::i#29 = (byte) main::i#1 [phi:main::@9->main::@5#0] -- register_copy
jmp b5
// main::@4
b4:
// [15] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '4' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'4'
sta SCREEN,x
jmp b6_from_b4
// main::@3
b3:
// [16] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '3' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'3'
sta SCREEN,x
jmp b4
// main::@2
b2:
// [17] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '1' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'1'
sta SCREEN,x
jmp b6_from_b2
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b5
Removing instruction jmp b6
Removing instruction jmp breturn
Removing instruction jmp b7
Removing instruction jmp b8
Removing instruction jmp b9
Succesful ASM optimization Pass5NextJumpElimination
Replacing label b6_from_b4 with b6
Replacing label b6_from_b2 with b6
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction main_from_b1:
Removing instruction bend_from_b1:
Removing instruction b6_from_b2:
Removing instruction b6_from_b4:
Removing instruction b6_from_b5:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b5_from_main:
Removing instruction breturn:
Removing instruction b7:
Removing instruction b8:
Removing instruction b9:
Removing instruction b5_from_b9:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@7
(label) main::@8
(label) main::@9
(label) main::@return
(byte*) main::SCREEN
(const byte*) main::SCREEN#0 SCREEN = (byte*) 1024
(byte) main::i
(byte) main::i#1 reg byte x 134.66666666666669
(byte) main::i#18 reg byte x 314.0
(byte) main::i#29 reg byte x 61.5
reg byte x [ main::i#18 main::i#29 main::i#1 ]
FINAL ASSEMBLER
Score: 5576
// File Comments
// Tests simple switch()-statement
// Expected output 'd1444d'
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
// @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: {
.label SCREEN = $400
// [5] phi from main to main::@5 [phi:main->main::@5]
// [5] phi (byte) main::i#29 = (byte) 0 [phi:main->main::@5#0] -- vbuxx=vbuc1
ldx #0
// main::@5
b5:
// SCREEN[i] = 'd'
// [6] *((const byte*) main::SCREEN#0 + (byte) main::i#29) ← (byte) 'd' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'d'
sta SCREEN,x
// [7] phi from main::@2 main::@4 main::@5 to main::@6 [phi:main::@2/main::@4/main::@5->main::@6]
// [7] phi (byte) main::i#18 = (byte) main::i#1 [phi:main::@2/main::@4/main::@5->main::@6#0] -- register_copy
// main::@6
b6:
// for(char i:0..5)
// [8] (byte) main::i#1 ← ++ (byte) main::i#18 -- 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
// main::@1
b1:
// case 1:
// SCREEN[i] = '1';
// break;
// [11] if((byte) main::i#1==(byte) 1) goto main::@2 -- vbuxx_eq_vbuc1_then_la1
// A simple case with a break
cpx #1
beq b2
// main::@7
// case 2:
// [12] if((byte) main::i#1==(byte) 2) goto main::@3 -- vbuxx_eq_vbuc1_then_la1
// A case with no body
cpx #2
beq b3
// main::@8
// case 3:
// SCREEN[i] = '3';
// [13] if((byte) main::i#1==(byte) 3) goto main::@3 -- vbuxx_eq_vbuc1_then_la1
// A case with fall-through
cpx #3
beq b3
// main::@9
// case 4:
// SCREEN[i] = '4';
// break;
// [14] if((byte) main::i#1==(byte) 4) goto main::@4 -- vbuxx_eq_vbuc1_then_la1
cpx #4
beq b4
// [5] phi from main::@9 to main::@5 [phi:main::@9->main::@5]
// [5] phi (byte) main::i#29 = (byte) main::i#1 [phi:main::@9->main::@5#0] -- register_copy
jmp b5
// main::@4
b4:
// SCREEN[i] = '4'
// [15] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '4' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'4'
sta SCREEN,x
jmp b6
// main::@3
b3:
// SCREEN[i] = '3'
// [16] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '3' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'3'
sta SCREEN,x
jmp b4
// main::@2
b2:
// SCREEN[i] = '1'
// [17] *((const byte*) main::SCREEN#0 + (byte) main::i#1) ← (byte) '1' -- pbuc1_derefidx_vbuxx=vbuc2
lda #'1'
sta SCREEN,x
jmp b6
}
// File Data

22
src/test/ref/switch-0.sym Normal file
View File

@ -0,0 +1,22 @@
(label) @1
(label) @begin
(label) @end
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@7
(label) main::@8
(label) main::@9
(label) main::@return
(byte*) main::SCREEN
(const byte*) main::SCREEN#0 SCREEN = (byte*) 1024
(byte) main::i
(byte) main::i#1 reg byte x 134.66666666666669
(byte) main::i#18 reg byte x 314.0
(byte) main::i#29 reg byte x 61.5
reg byte x [ main::i#18 main::i#29 main::i#1 ]