1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-02-17 10:30:43 +00:00

Fixed NPE when using booleans in if()/while(). Closes #152

This commit is contained in:
jespergravgaard 2019-03-22 07:25:55 +01:00
parent 4ad601ce72
commit c280a3b14a
8 changed files with 398 additions and 13 deletions

View File

@ -54,18 +54,20 @@ public class Pass2ConditionalAndOrRewriting extends Pass2SsaOptimization {
if(conditionRValue instanceof VariableRef && usages.get(conditionRValue).size() == 1) {
VariableRef conditionVar = (VariableRef) conditionRValue;
StatementAssignment conditionAssignment = assignments.get(conditionVar);
if(Operators.LOGIC_AND.equals(conditionAssignment.getOperator())) {
// Found if() with logical && condition - rewrite to if(c1) if(c2) { xx }
rewriteLogicAnd(block, conditional, conditionAssignment);
return conditionVar;
} else if(Operators.LOGIC_OR.equals(conditionAssignment.getOperator())) {
// Found if() with logical || condition - rewrite to if(c1) goto x else if(c2) goto x else goto end, x:{ xx } end:
rewriteLogicOr(block, conditional, conditionAssignment);
return conditionVar;
} else if(Operators.LOGIC_NOT.equals(conditionAssignment.getOperator())) {
// Found if() with logical ! condition - rewrite to if(!c1) goto x else goto end, x:{ xx } end:
rewriteLogicNot(block, conditional, conditionAssignment);
return conditionVar;
if(conditionAssignment!=null) {
if(Operators.LOGIC_AND.equals(conditionAssignment.getOperator())) {
// Found if() with logical && condition - rewrite to if(c1) if(c2) { xx }
rewriteLogicAnd(block, conditional, conditionAssignment);
return conditionVar;
} else if(Operators.LOGIC_OR.equals(conditionAssignment.getOperator())) {
// Found if() with logical || condition - rewrite to if(c1) goto x else if(c2) goto x else goto end, x:{ xx } end:
rewriteLogicOr(block, conditional, conditionAssignment);
return conditionVar;
} else if(Operators.LOGIC_NOT.equals(conditionAssignment.getOperator())) {
// Found if() with logical ! condition - rewrite to if(!c1) goto x else goto end, x:{ xx } end:
rewriteLogicNot(block, conditional, conditionAssignment);
return conditionVar;
}
}
}
}

View File

@ -43,7 +43,7 @@ public class Pass2ConditionalJumpSimplification extends Pass2SsaOptimization {
if(conditionRValue instanceof VariableRef && usages.get(conditionRValue).size() == 1) {
VariableRef conditionVar = (VariableRef) conditionRValue;
StatementAssignment conditionAssignment = assignments.get(conditionVar);
if(conditionAssignment.getOperator() != null) {
if(conditionAssignment!=null && conditionAssignment.getOperator() != null) {
switch(conditionAssignment.getOperator().getOperator()) {
case "==":
case "<>":

View File

@ -32,6 +32,11 @@ public class TestPrograms {
public TestPrograms() {
}
@Test
public void testBoolNullPointerException() throws IOException, URISyntaxException {
compileAndCompare("bool-nullpointer-exception");
}
@Test
public void testSignedWordMinusByte() throws IOException, URISyntaxException {
compileAndCompare("test-signed-word-minus-byte");

View File

@ -0,0 +1,11 @@
// Some bool code that causes a NullPointerException
bool framedone = true;
void main() {
while(true) {
while(!framedone) {}
framedone = false;
}
}

View File

@ -0,0 +1,14 @@
// Some bool code that causes a NullPointerException
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
main: {
lda #1
jmp b4
b1:
lda #0
b4:
cmp #0
bne b1
jmp b4
}

View File

@ -0,0 +1,18 @@
@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::@1
main::@1: scope:[main] from main main::@4
[5] (bool) framedone#2 ← phi( main/true main::@4/false )
to:main::@4
main::@4: scope:[main] from main::@1 main::@4
[6] if((bool) framedone#2) goto main::@1
to:main::@4

View File

@ -0,0 +1,325 @@
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
(bool) framedone#0 ← true
to:@1
main: scope:[main] from @1
(bool) framedone#11 ← phi( @1/(bool) framedone#10 )
to:main::@1
main::@1: scope:[main] from main main::@6
(bool) framedone#9 ← phi( main/(bool) framedone#11 main::@6/(bool) framedone#1 )
if(true) goto main::@2
to:main::@return
main::@2: scope:[main] from main::@1
(bool) framedone#7 ← phi( main::@1/(bool) framedone#9 )
to:main::@4
main::@4: scope:[main] from main::@2 main::@5
(bool) framedone#4 ← phi( main::@2/(bool) framedone#7 main::@5/(bool) framedone#8 )
(bool~) main::$0 ← ! (bool) framedone#4
if((bool~) main::$0) goto main::@5
to:main::@6
main::@5: scope:[main] from main::@4
(bool) framedone#8 ← phi( main::@4/(bool) framedone#4 )
to:main::@4
main::@6: scope:[main] from main::@4
(bool) framedone#1 ← false
to:main::@1
main::@return: scope:[main] from main::@1
(bool) framedone#5 ← phi( main::@1/(bool) framedone#9 )
(bool) framedone#2 ← (bool) framedone#5
return
to:@return
@1: scope:[] from @begin
(bool) framedone#10 ← phi( @begin/(bool) framedone#0 )
call main
to:@2
@2: scope:[] from @1
(bool) framedone#6 ← phi( @1/(bool) framedone#2 )
(bool) framedone#3 ← (bool) framedone#6
to:@end
@end: scope:[] from @2
SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @begin
(label) @end
(bool) framedone
(bool) framedone#0
(bool) framedone#1
(bool) framedone#10
(bool) framedone#11
(bool) framedone#2
(bool) framedone#3
(bool) framedone#4
(bool) framedone#5
(bool) framedone#6
(bool) framedone#7
(bool) framedone#8
(bool) framedone#9
(void()) main()
(bool~) main::$0
(label) main::@1
(label) main::@2
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@return
Alias (bool) framedone#2 = (bool) framedone#7 (bool) framedone#9 (bool) framedone#5
Alias (bool) framedone#4 = (bool) framedone#8
Alias (bool) framedone#0 = (bool) framedone#10
Alias (bool) framedone#3 = (bool) framedone#6
Successful SSA optimization Pass2AliasElimination
Self Phi Eliminated (bool) framedone#4
Successful SSA optimization Pass2SelfPhiElimination
Redundant Phi (bool) framedone#11 (bool) framedone#0
Redundant Phi (bool) framedone#4 (bool) framedone#2
Redundant Phi (bool) framedone#3 (bool) framedone#2
Successful SSA optimization Pass2RedundantPhiElimination
Rewriting ! if()-condition to reversed if() [6] (bool~) main::$0 ← ! (bool) framedone#2
Successful SSA optimization Pass2ConditionalAndOrRewriting
Constant (const bool) framedone#0 = true
Constant (const bool) framedone#1 = false
Successful SSA optimization Pass2ConstantIdentification
if() condition always true - replacing block destination [1] if(true) goto main::@2
Successful SSA optimization Pass2ConstantIfs
Removing unused block main::@return
Successful SSA optimization Pass2EliminateUnusedBlocks
Culled Empty Block (label) main::@2
Culled Empty Block (label) main::@5
Culled Empty Block (label) main::@6
Culled Empty Block (label) @2
Successful SSA optimization Pass2CullEmptyBlocks
Inlining constant with var siblings (const bool) framedone#0
Inlining constant with var siblings (const bool) framedone#1
Constant inlined framedone#1 = false
Constant inlined framedone#0 = true
Successful SSA optimization Pass2ConstantInlining
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
CALL GRAPH
Calls in [] to main:2
Created 1 initial phi equivalence classes
Coalesced down to 1 phi equivalence classes
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::@1
main::@1: scope:[main] from main main::@4
[5] (bool) framedone#2 ← phi( main/true main::@4/false )
to:main::@4
main::@4: scope:[main] from main::@1 main::@4
[6] if((bool) framedone#2) goto main::@1
to:main::@4
VARIABLE REGISTER WEIGHTS
(bool) framedone
(bool) framedone#2 50.5
(void()) main()
Initial phi equivalence classes
[ framedone#2 ]
Complete equivalence classes
[ framedone#2 ]
Allocated zp ZP_BOOL:2 [ framedone#2 ]
INITIAL ASM
//SEG0 File Comments
// Some bool code that causes a NullPointerException
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
.label framedone = 2
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
jmp b1
//SEG5 @1
b1:
//SEG6 [2] call main
//SEG7 [4] phi from @1 to main [phi:@1->main]
main_from_b1:
jsr main
//SEG8 [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
jmp bend
//SEG9 @end
bend:
//SEG10 main
main: {
//SEG11 [5] phi from main to main::@1 [phi:main->main::@1]
b1_from_main:
//SEG12 [5] phi (bool) framedone#2 = true [phi:main->main::@1#0] -- vboz1=vboc1
lda #1
sta framedone
jmp b1
//SEG13 [5] phi from main::@4 to main::@1 [phi:main::@4->main::@1]
b1_from_b4:
//SEG14 [5] phi (bool) framedone#2 = false [phi:main::@4->main::@1#0] -- vboz1=vboc1
lda #0
sta framedone
jmp b1
//SEG15 main::@1
b1:
jmp b4
//SEG16 main::@4
b4:
//SEG17 [6] if((bool) framedone#2) goto main::@1 -- vboz1_then_la1
lda framedone
cmp #0
bne b1_from_b4
jmp b4
}
REGISTER UPLIFT POTENTIAL REGISTERS
Potential registers zp ZP_BOOL:2 [ framedone#2 ] : zp ZP_BOOL:2 , reg byte a ,
REGISTER UPLIFT SCOPES
Uplift Scope [] 50.5: zp ZP_BOOL:2 [ framedone#2 ]
Uplift Scope [main]
Uplifting [] best 892 combination reg byte a [ framedone#2 ]
Uplifting [main] best 892 combination
ASSEMBLER BEFORE OPTIMIZATION
//SEG0 File Comments
// Some bool code that causes a NullPointerException
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
jmp b1
//SEG5 @1
b1:
//SEG6 [2] call main
//SEG7 [4] phi from @1 to main [phi:@1->main]
main_from_b1:
jsr main
//SEG8 [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
jmp bend
//SEG9 @end
bend:
//SEG10 main
main: {
//SEG11 [5] phi from main to main::@1 [phi:main->main::@1]
b1_from_main:
//SEG12 [5] phi (bool) framedone#2 = true [phi:main->main::@1#0] -- vboaa=vboc1
lda #1
jmp b1
//SEG13 [5] phi from main::@4 to main::@1 [phi:main::@4->main::@1]
b1_from_b4:
//SEG14 [5] phi (bool) framedone#2 = false [phi:main::@4->main::@1#0] -- vboaa=vboc1
lda #0
jmp b1
//SEG15 main::@1
b1:
jmp b4
//SEG16 main::@4
b4:
//SEG17 [6] if((bool) framedone#2) goto main::@1 -- vboaa_then_la1
cmp #0
bne b1_from_b4
jmp b4
}
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction jmp b4
Succesful ASM optimization Pass5NextJumpElimination
Replacing label b1 with b4
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction main_from_b1:
Removing instruction bend_from_b1:
Removing instruction b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b1_from_main:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Relabelling long label b1_from_b4 to b1
Succesful ASM optimization Pass5RelabelLongLabels
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(bool) framedone
(bool) framedone#2 reg byte a 50.5
(void()) main()
(label) main::@1
(label) main::@4
reg byte a [ framedone#2 ]
FINAL ASSEMBLER
Score: 820
//SEG0 File Comments
// Some bool code that causes a NullPointerException
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG2 Global Constants & labels
//SEG3 @begin
//SEG4 [1] phi from @begin to @1 [phi:@begin->@1]
//SEG5 @1
//SEG6 [2] call main
//SEG7 [4] phi from @1 to main [phi:@1->main]
//SEG8 [3] phi from @1 to @end [phi:@1->@end]
//SEG9 @end
//SEG10 main
main: {
//SEG11 [5] phi from main to main::@1 [phi:main->main::@1]
//SEG12 [5] phi (bool) framedone#2 = true [phi:main->main::@1#0] -- vboaa=vboc1
lda #1
jmp b4
//SEG13 [5] phi from main::@4 to main::@1 [phi:main::@4->main::@1]
b1:
//SEG14 [5] phi (bool) framedone#2 = false [phi:main::@4->main::@1#0] -- vboaa=vboc1
lda #0
//SEG15 main::@1
//SEG16 main::@4
b4:
//SEG17 [6] if((bool) framedone#2) goto main::@1 -- vboaa_then_la1
cmp #0
bne b1
jmp b4
}

View File

@ -0,0 +1,10 @@
(label) @1
(label) @begin
(label) @end
(bool) framedone
(bool) framedone#2 reg byte a 50.5
(void()) main()
(label) main::@1
(label) main::@4
reg byte a [ framedone#2 ]