1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-23 23:32:55 +00:00

Fixed clobber-problem with stack-parameter-passing return-values. Results are still not pushed. #316

This commit is contained in:
jespergravgaard 2019-09-22 23:12:19 +02:00
parent dc08f86302
commit 6540d60e60
11 changed files with 56 additions and 85 deletions

View File

@ -5,11 +5,9 @@ import dk.camelot64.kickc.asm.AsmProgram;
import dk.camelot64.kickc.fragment.AsmFragmentInstance;
import dk.camelot64.kickc.fragment.AsmFragmentTemplateSynthesizer;
import dk.camelot64.kickc.model.*;
import dk.camelot64.kickc.model.statements.*;
import dk.camelot64.kickc.model.values.LValue;
import dk.camelot64.kickc.model.values.VariableRef;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementAssignment;
import dk.camelot64.kickc.model.statements.StatementPhiBlock;
import java.util.*;
@ -39,8 +37,8 @@ public class Pass4RegisterUpliftPotentialRegisterAnalysis extends Pass2Base {
variableRefs.add(phiVariable.getVariable());
}
return variableRefs;
} else if(statement instanceof StatementAssignment) {
LValue lValue = ((StatementAssignment) statement).getlValue();
} else if(statement instanceof StatementLValue) {
LValue lValue = ((StatementLValue) statement).getlValue();
if(lValue instanceof VariableRef) {
variableRefs.add((VariableRef) lValue);
}

View File

@ -57,7 +57,7 @@ public class TestPrograms {
@Test
public void testProcedureCallingConventionStack0() throws IOException, URISyntaxException {
compileAndCompare("procedure-callingconvention-stack-0"); //, log().verboseCreateSsa().verboseParse().verboseStatementSequence().verboseUplift().verboseLiveRanges().verboseSSAOptimize());
compileAndCompare("procedure-callingconvention-stack-0");
}
@Test

View File

@ -5,7 +5,6 @@
.label SCREEN = $400
.const STACK_BASE = $103
main: {
.label _0 = 2
lda #'0'
pha
lda #7
@ -13,7 +12,6 @@ main: {
jsr plus
pla
pla
sta.z _0
sta SCREEN
rts
}

View File

@ -262,14 +262,11 @@ Uplift Scope [main] 2: zp ZP_BYTE:2 [ main::$0 ]
Uplift Scope []
Uplifting [plus] best 84 combination reg byte a [ plus::b#0 ] zp ZP_BYTE:3 [ plus::a#0 ] reg byte a [ plus::return#0 ]
Uplifting [main] best 84 combination zp ZP_BYTE:2 [ main::$0 ]
Uplifting [] best 84 combination
Attempting to uplift remaining variables inzp ZP_BYTE:2 [ main::$0 ]
Uplifting [main] best 84 combination zp ZP_BYTE:2 [ main::$0 ]
Uplifting [main] best 78 combination reg byte a [ main::$0 ]
Uplifting [] best 78 combination
Attempting to uplift remaining variables inzp ZP_BYTE:3 [ plus::a#0 ]
Uplifting [plus] best 84 combination zp ZP_BYTE:3 [ plus::a#0 ]
Coalescing zero page register [ zp ZP_BYTE:3 [ plus::a#0 ] ] with [ zp ZP_BYTE:2 [ main::$0 ] ]
Allocated (was zp ZP_BYTE:3) zp ZP_BYTE:2 [ plus::a#0 main::$0 ]
Uplifting [plus] best 78 combination zp ZP_BYTE:3 [ plus::a#0 ]
Allocated (was zp ZP_BYTE:3) zp ZP_BYTE:2 [ plus::a#0 ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
@ -299,7 +296,6 @@ bend_from_b1:
bend:
// main
main: {
.label _0 = 2
// [5] callprepare plus (byte) '0' (byte) 7
// [5] callprepare plus (byte) '0' (byte) 7 -- _stackpushbyte_=vbuc1
lda #'0'
@ -313,11 +309,9 @@ main: {
// [7] (byte~) main::$0 ← callfinalize plus
// [7] (byte~) main::$0 ← callfinalize plus -- _stackpullbyte_1
pla
// [7] (byte~) main::$0 ← callfinalize plus -- vbuz1=_stackpullbyte_
// [7] (byte~) main::$0 ← callfinalize plus -- vbuaa=_stackpullbyte_
pla
sta.z _0
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuz1
lda.z _0
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuaa
sta SCREEN
jmp breturn
// main::@return
@ -355,8 +349,6 @@ Removing instruction jmp bend
Removing instruction jmp breturn
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda.z _0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction main_from_b1:
@ -380,7 +372,7 @@ FINAL SYMBOL TABLE
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(const word) STACK_BASE STACK_BASE = (word) $103
(void()) main()
(byte~) main::$0 $0 zp ZP_BYTE:2 2.0
(byte~) main::$0 reg byte a 2.0
(label) main::@return
__stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(label) plus::@return
@ -393,13 +385,14 @@ __stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(byte) plus::return
(byte) plus::return#0 reg byte a 2.0
zp ZP_BYTE:2 [ plus::a#0 main::$0 ]
reg byte a [ main::$0 ]
zp ZP_BYTE:2 [ plus::a#0 ]
reg byte a [ plus::b#0 ]
reg byte a [ plus::return#0 ]
FINAL ASSEMBLER
Score: 63
Score: 60
// File Comments
// Test a procedure with calling convention stack
@ -419,7 +412,6 @@ Score: 63
// @end
// main
main: {
.label _0 = 2
// plus('0', 7)
// [5] callprepare plus (byte) '0' (byte) 7
// [5] callprepare plus (byte) '0' (byte) 7 -- _stackpushbyte_=vbuc1
@ -434,11 +426,10 @@ main: {
// [7] (byte~) main::$0 ← callfinalize plus
// [7] (byte~) main::$0 ← callfinalize plus -- _stackpullbyte_1
pla
// [7] (byte~) main::$0 ← callfinalize plus -- vbuz1=_stackpullbyte_
// [7] (byte~) main::$0 ← callfinalize plus -- vbuaa=_stackpullbyte_
pla
sta.z _0
// SCREEN[0] = plus('0', 7)
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuz1
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuaa
sta SCREEN
// main::@return
// }

View File

@ -5,7 +5,7 @@
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(const word) STACK_BASE STACK_BASE = (word) $103
(void()) main()
(byte~) main::$0 $0 zp ZP_BYTE:2 2.0
(byte~) main::$0 reg byte a 2.0
(label) main::@return
__stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(label) plus::@return
@ -18,6 +18,7 @@ __stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(byte) plus::return
(byte) plus::return#0 reg byte a 2.0
zp ZP_BYTE:2 [ plus::a#0 main::$0 ]
reg byte a [ main::$0 ]
zp ZP_BYTE:2 [ plus::a#0 ]
reg byte a [ plus::b#0 ]
reg byte a [ plus::return#0 ]

View File

@ -5,7 +5,6 @@
.label SCREEN = $400
.const STACK_BASE = $103
main: {
.label _0 = 2
lda #'0'
pha
lda #7
@ -13,7 +12,6 @@ main: {
jsr plus
pla
pla
sta.z _0
sta SCREEN
rts
}

View File

@ -262,14 +262,11 @@ Uplift Scope [main] 2: zp ZP_BYTE:2 [ main::$0 ]
Uplift Scope []
Uplifting [plus] best 84 combination reg byte a [ plus::b#0 ] zp ZP_BYTE:3 [ plus::a#0 ] reg byte a [ plus::return#0 ]
Uplifting [main] best 84 combination zp ZP_BYTE:2 [ main::$0 ]
Uplifting [] best 84 combination
Attempting to uplift remaining variables inzp ZP_BYTE:2 [ main::$0 ]
Uplifting [main] best 84 combination zp ZP_BYTE:2 [ main::$0 ]
Uplifting [main] best 78 combination reg byte a [ main::$0 ]
Uplifting [] best 78 combination
Attempting to uplift remaining variables inzp ZP_BYTE:3 [ plus::a#0 ]
Uplifting [plus] best 84 combination zp ZP_BYTE:3 [ plus::a#0 ]
Coalescing zero page register [ zp ZP_BYTE:3 [ plus::a#0 ] ] with [ zp ZP_BYTE:2 [ main::$0 ] ]
Allocated (was zp ZP_BYTE:3) zp ZP_BYTE:2 [ plus::a#0 main::$0 ]
Uplifting [plus] best 78 combination zp ZP_BYTE:3 [ plus::a#0 ]
Allocated (was zp ZP_BYTE:3) zp ZP_BYTE:2 [ plus::a#0 ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
@ -299,7 +296,6 @@ bend_from_b1:
bend:
// main
main: {
.label _0 = 2
// [5] callprepare plus (byte) '0' (byte) 7
// [5] callprepare plus (byte) '0' (byte) 7 -- _stackpushbyte_=vbuc1
lda #'0'
@ -313,11 +309,9 @@ main: {
// [7] (byte~) main::$0 ← callfinalize plus
// [7] (byte~) main::$0 ← callfinalize plus -- _stackpullbyte_1
pla
// [7] (byte~) main::$0 ← callfinalize plus -- vbuz1=_stackpullbyte_
// [7] (byte~) main::$0 ← callfinalize plus -- vbuaa=_stackpullbyte_
pla
sta.z _0
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuz1
lda.z _0
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuaa
sta SCREEN
jmp breturn
// main::@return
@ -355,8 +349,6 @@ Removing instruction jmp bend
Removing instruction jmp breturn
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda.z _0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction main_from_b1:
@ -380,7 +372,7 @@ FINAL SYMBOL TABLE
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(const word) STACK_BASE STACK_BASE = (word) $103
(void()) main()
(byte~) main::$0 $0 zp ZP_BYTE:2 2.0
(byte~) main::$0 reg byte a 2.0
(label) main::@return
__stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(label) plus::@return
@ -393,13 +385,14 @@ __stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(byte) plus::return
(byte) plus::return#0 reg byte a 2.0
zp ZP_BYTE:2 [ plus::a#0 main::$0 ]
reg byte a [ main::$0 ]
zp ZP_BYTE:2 [ plus::a#0 ]
reg byte a [ plus::b#0 ]
reg byte a [ plus::return#0 ]
FINAL ASSEMBLER
Score: 63
Score: 60
// File Comments
// Test a procedure with calling convention stack
@ -419,7 +412,6 @@ Score: 63
// @end
// main
main: {
.label _0 = 2
// plus('0', 7)
// [5] callprepare plus (byte) '0' (byte) 7
// [5] callprepare plus (byte) '0' (byte) 7 -- _stackpushbyte_=vbuc1
@ -434,11 +426,10 @@ main: {
// [7] (byte~) main::$0 ← callfinalize plus
// [7] (byte~) main::$0 ← callfinalize plus -- _stackpullbyte_1
pla
// [7] (byte~) main::$0 ← callfinalize plus -- vbuz1=_stackpullbyte_
// [7] (byte~) main::$0 ← callfinalize plus -- vbuaa=_stackpullbyte_
pla
sta.z _0
// SCREEN[0] = plus('0', 7)
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuz1
// [8] *((const byte*) SCREEN#0) ← (byte~) main::$0 -- _deref_pbuc1=vbuaa
sta SCREEN
// main::@return
// }

View File

@ -5,7 +5,7 @@
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(const word) STACK_BASE STACK_BASE = (word) $103
(void()) main()
(byte~) main::$0 $0 zp ZP_BYTE:2 2.0
(byte~) main::$0 reg byte a 2.0
(label) main::@return
__stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(label) plus::@return
@ -18,6 +18,7 @@ __stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(byte) plus::return
(byte) plus::return#0 reg byte a 2.0
zp ZP_BYTE:2 [ plus::a#0 main::$0 ]
reg byte a [ main::$0 ]
zp ZP_BYTE:2 [ plus::a#0 ]
reg byte a [ plus::b#0 ]
reg byte a [ plus::return#0 ]

View File

@ -6,7 +6,6 @@
.label SCREEN = $400
.const STACK_BASE = $103
main: {
.label w = 2
ldy #0
b1:
tya
@ -19,10 +18,9 @@ main: {
jsr plus
pla
pla
sta.z w
tya
sty.z $ff
clc
adc.z w
adc.z $ff
sta SCREEN
iny
cpy #2

View File

@ -403,16 +403,13 @@ Uplift Scope [main] 22.79: zp ZP_BYTE:2 [ main::a#2 main::a#1 ] 22: zp ZP_BYTE:5
Uplift Scope [plus] 4: zp ZP_BYTE:7 [ plus::b#0 ] 2: zp ZP_BYTE:6 [ plus::a#0 ] 2: zp ZP_BYTE:8 [ plus::return#0 ]
Uplift Scope []
Uplifting [main] best 671 combination reg byte y [ main::a#2 main::a#1 ] reg byte a [ main::$2 ] reg byte x [ main::v#0 ] zp ZP_BYTE:4 [ main::w#0 ]
Uplifting [main] best 651 combination reg byte y [ main::a#2 main::a#1 ] reg byte a [ main::$2 ] reg byte x [ main::v#0 ] reg byte a [ main::w#0 ]
Limited combination testing to 100 combinations of 128 possible.
Uplifting [plus] best 662 combination reg byte a [ plus::b#0 ] zp ZP_BYTE:6 [ plus::a#0 ] reg byte a [ plus::return#0 ]
Uplifting [] best 662 combination
Attempting to uplift remaining variables inzp ZP_BYTE:4 [ main::w#0 ]
Uplifting [main] best 662 combination zp ZP_BYTE:4 [ main::w#0 ]
Uplifting [plus] best 642 combination reg byte a [ plus::b#0 ] zp ZP_BYTE:6 [ plus::a#0 ] reg byte a [ plus::return#0 ]
Uplifting [] best 642 combination
Attempting to uplift remaining variables inzp ZP_BYTE:6 [ plus::a#0 ]
Uplifting [plus] best 662 combination zp ZP_BYTE:6 [ plus::a#0 ]
Coalescing zero page register [ zp ZP_BYTE:6 [ plus::a#0 ] ] with [ zp ZP_BYTE:4 [ main::w#0 ] ]
Allocated (was zp ZP_BYTE:6) zp ZP_BYTE:2 [ plus::a#0 main::w#0 ]
Uplifting [plus] best 642 combination zp ZP_BYTE:6 [ plus::a#0 ]
Allocated (was zp ZP_BYTE:6) zp ZP_BYTE:2 [ plus::a#0 ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
@ -443,7 +440,6 @@ bend_from_b1:
bend:
// main
main: {
.label w = 2
// [5] phi from main to main::@1 [phi:main->main::@1]
b1_from_main:
// [5] phi (byte) main::a#2 = (byte) 0 [phi:main->main::@1#0] -- vbuyy=vbuc1
@ -472,13 +468,12 @@ main: {
// [9] (byte) main::w#0 ← callfinalize plus
// [9] (byte) main::w#0 ← callfinalize plus -- _stackpullbyte_1
pla
// [9] (byte) main::w#0 ← callfinalize plus -- vbuz1=_stackpullbyte_
// [9] (byte) main::w#0 ← callfinalize plus -- vbuaa=_stackpullbyte_
pla
sta.z w
// [10] (byte~) main::$2 ← (byte) main::w#0 + (byte) main::a#2 -- vbuaa=vbuz1_plus_vbuyy
tya
// [10] (byte~) main::$2 ← (byte) main::w#0 + (byte) main::a#2 -- vbuaa=vbuaa_plus_vbuyy
sty.z $ff
clc
adc.z w
adc.z $ff
// [11] *((const byte*) SCREEN#0) ← (byte~) main::$2 -- _deref_pbuc1=vbuaa
sta SCREEN
// [12] (byte) main::a#1 ← ++ (byte) main::a#2 -- vbuyy=_inc_vbuyy
@ -561,7 +556,7 @@ FINAL SYMBOL TABLE
(byte) main::v
(byte) main::v#0 reg byte x 11.0
(byte) main::w
(byte) main::w#0 w zp ZP_BYTE:2 11.0
(byte) main::w#0 reg byte a 11.0
__stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(label) plus::@return
(const byte) plus::OFFSET_STACK_A OFFSET_STACK_A = (byte) 0
@ -575,14 +570,15 @@ __stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
reg byte y [ main::a#2 main::a#1 ]
reg byte x [ main::v#0 ]
reg byte a [ main::w#0 ]
reg byte a [ main::$2 ]
zp ZP_BYTE:2 [ plus::a#0 main::w#0 ]
zp ZP_BYTE:2 [ plus::a#0 ]
reg byte a [ plus::b#0 ]
reg byte a [ plus::return#0 ]
FINAL ASSEMBLER
Score: 557
Score: 537
// File Comments
// Test a procedure with calling convention stack
@ -603,7 +599,6 @@ Score: 557
// @end
// main
main: {
.label w = 2
// [5] phi from main to main::@1 [phi:main->main::@1]
// [5] phi (byte) main::a#2 = (byte) 0 [phi:main->main::@1#0] -- vbuyy=vbuc1
ldy #0
@ -630,14 +625,13 @@ main: {
// [9] (byte) main::w#0 ← callfinalize plus
// [9] (byte) main::w#0 ← callfinalize plus -- _stackpullbyte_1
pla
// [9] (byte) main::w#0 ← callfinalize plus -- vbuz1=_stackpullbyte_
// [9] (byte) main::w#0 ← callfinalize plus -- vbuaa=_stackpullbyte_
pla
sta.z w
// w+a
// [10] (byte~) main::$2 ← (byte) main::w#0 + (byte) main::a#2 -- vbuaa=vbuz1_plus_vbuyy
tya
// [10] (byte~) main::$2 ← (byte) main::w#0 + (byte) main::a#2 -- vbuaa=vbuaa_plus_vbuyy
sty.z $ff
clc
adc.z w
adc.z $ff
// SCREEN[i] = w+a
// [11] *((const byte*) SCREEN#0) ← (byte~) main::$2 -- _deref_pbuc1=vbuaa
sta SCREEN

View File

@ -15,7 +15,7 @@
(byte) main::v
(byte) main::v#0 reg byte x 11.0
(byte) main::w
(byte) main::w#0 w zp ZP_BYTE:2 11.0
(byte) main::w#0 reg byte a 11.0
__stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
(label) plus::@return
(const byte) plus::OFFSET_STACK_A OFFSET_STACK_A = (byte) 0
@ -29,7 +29,8 @@ __stackcall (byte()) plus((byte) plus::a , (byte) plus::b)
reg byte y [ main::a#2 main::a#1 ]
reg byte x [ main::v#0 ]
reg byte a [ main::w#0 ]
reg byte a [ main::$2 ]
zp ZP_BYTE:2 [ plus::a#0 main::w#0 ]
zp ZP_BYTE:2 [ plus::a#0 ]
reg byte a [ plus::b#0 ]
reg byte a [ plus::return#0 ]