1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-01-11 04:29:53 +00:00

Rudimentary return value working. TODO: Passing using registers, Live ranges, Entry Points.

This commit is contained in:
jespergravgaard 2020-11-22 22:39:15 +01:00
parent 3be0b5d2d4
commit 381799bbea
11 changed files with 483 additions and 37 deletions

View File

@ -311,13 +311,17 @@ public class Compiler {
new Pass1CallVoidReturns(program).execute();
new Pass1CallStackVar(program).execute();
//getLog().append("PROCEDURE CALLS");
//getLog().append(program.getGraph().toString(program));
if(getLog().isVerbosePass1CreateSsa()) {
getLog().append("PROCEDURE CALLS");
getLog().append(program.getGraph().toString(program));
}
new Pass1CallStack(program).execute();
new Pass1CallVar(program).execute();
new Pass1CallPhiParameters(program).execute();
//getLog().append("PROCEDURE PARAMETERS");
//getLog().append(program.getGraph().toString(program));
if(getLog().isVerbosePass1CreateSsa()) {
getLog().append("PROCEDURE PARAMETERS");
getLog().append(program.getGraph().toString(program));
}
new PassNUnwindLValueLists(program).execute();
new Pass1GenerateSingleStaticAssignmentForm(program).execute();
new Pass1CallPhiReturn(program).execute();

View File

@ -2,23 +2,17 @@ package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.InternalError;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.iterator.ProgramValueIterator;
import dk.camelot64.kickc.model.statements.*;
import dk.camelot64.kickc.model.symbols.Procedure;
import dk.camelot64.kickc.model.symbols.Scope;
import dk.camelot64.kickc.model.symbols.Variable;
import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.types.SymbolTypeInference;
import dk.camelot64.kickc.model.values.LValue;
import dk.camelot64.kickc.model.values.ParamValue;
import dk.camelot64.kickc.model.values.RValue;
import dk.camelot64.kickc.model.values.VariableRef;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
/** Handle calling convention {@link Procedure.CallingConvention#VAR_CALL } by converting the making control flow graph and symbols calling convention specific. */
public class Pass1CallVar extends Pass2SsaOptimization {
@ -45,27 +39,6 @@ public class Pass1CallVar extends Pass2SsaOptimization {
}
}
// Convert param(xxx) to ??? = xxx
ProgramValueIterator.execute(getGraph(), (programValue, currentStmt, stmtIt, currentBlock) -> {
if(programValue.get() instanceof ParamValue) {
// Convert ParamValues to calling-convention specific param-value
ParamValue paramValue = (ParamValue) programValue.get();
VariableRef parameterRef = paramValue.getParameter();
SymbolType parameterType = SymbolTypeInference.inferType(getScope(), paramValue.getParameter());
final Variable paramVar = getScope().getVariable(parameterRef);
final Scope blockScope = paramVar.getScope();
if(blockScope instanceof Procedure) {
Procedure procedure = (Procedure) blockScope;
if(Procedure.CallingConvention.VAR_CALL.equals(procedure.getCallingConvention())) {
throw new InternalError(paramValue.toString());
//programValue.set(stackIdxValue);
//getLog().append("Calling convention " + Procedure.CallingConvention.STACK_CALL + " replacing " + paramValue.toString(getProgram()) + " with " + stackIdxValue.toString(getProgram()));
}
}
}
}
);
// Convert procedure return xxx to proc.return = xxx;
for(ControlFlowBlock block : getGraph().getAllBlocks()) {
ListIterator<Statement> stmtIt = block.getStatements().listIterator();
@ -78,11 +51,7 @@ public class Pass1CallVar extends Pass2SsaOptimization {
final SymbolType returnType = procedure.getReturnType();
if(!SymbolType.VOID.equals(returnType) && Procedure.CallingConvention.VAR_CALL.equals(procedure.getCallingConvention())) {
final RValue value = ((StatementReturn) statement).getValue();
//stmtIt.previous();
//generateStackReturnValues(value, returnType, returnOffsetConstant, statement.getSource(), statement.getComments(), stmtIt);
//stmtIt.next();
//((StatementReturn) statement).setValue(null);
throw new InternalError(statement.toString());
((StatementReturn) statement).setValue(null);
}
}
}

View File

@ -48,9 +48,14 @@ public class TestPrograms {
// compileAndCompare("unknown-var-problem.c", log().verboseParse());
//}
@Test
public void testVarCall2() throws IOException, URISyntaxException {
compileAndCompare("varcall-2.c");
}
@Test
public void testVarCall1() throws IOException, URISyntaxException {
compileAndCompare("varcall-1.c", log());
compileAndCompare("varcall-1.c");
}
@Test

View File

@ -1,4 +1,5 @@
// Test __varcall calling convention
// Parameter passing
void main() {
setbg(0);

17
src/test/kc/varcall-2.c Normal file
View File

@ -0,0 +1,17 @@
// Test __varcall calling convention
// Return value
char * const BGCOL = 0xd021;
void main() {
char a = 1;
*BGCOL = a;
a = plus(a, 1);
*BGCOL = a;
a = plus(a, 1);
*BGCOL = a;
}
__varcall char plus(char a, char b) {
return a+b;
}

View File

@ -1,4 +1,5 @@
// Test __varcall calling convention
// Parameter passing
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"

View File

@ -116,6 +116,7 @@ Uplifting [setbg] best 47 combination zp[1]:2 [ setbg::col ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test __varcall calling convention
// Parameter passing
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
@ -177,6 +178,7 @@ Score: 41
// File Comments
// Test __varcall calling convention
// Parameter passing
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)

View File

@ -0,0 +1,44 @@
// Test __varcall calling convention
// Return value
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label BGCOL = $d021
// plus(byte zp(3) a, byte zp(4) b)
plus: {
.label return = 2
.label a = 3
.label b = 4
// a+b
lda.z a
clc
adc.z b
// return a+b;
sta.z return
// }
rts
}
main: {
// *BGCOL = a
lda #1
sta BGCOL
// plus(a, 1)
sta.z plus.a
sta.z plus.b
jsr plus
// a = plus(a, 1)
lda.z plus.return
// *BGCOL = a
sta BGCOL
// plus(a, 1)
sta.z plus.a
lda #1
sta.z plus.b
jsr plus
// a = plus(a, 1)
lda.z plus.return
// *BGCOL = a
sta BGCOL
// }
rts
}

View File

@ -0,0 +1,27 @@
__varcall byte plus(byte plus::a , byte plus::b)
plus: scope:[plus] from
[0] plus::$0 = plus::a + plus::b
[1] plus::return = plus::$0
to:plus::@return
plus::@return: scope:[plus] from plus
[2] return
to:@return
void main()
main: scope:[main] from
[3] *BGCOL = 1
[4] plus::a = 1
[5] plus::b = 1
[6] callexecute plus
[7] main::a#1 = plus::return
[8] *BGCOL = main::a#1
[9] plus::a = main::a#1
[10] plus::b = 1
[11] callexecute plus
[12] main::a#2 = plus::return
[13] *BGCOL = main::a#2
to:main::@return
main::@return: scope:[main] from main
[14] return
to:@return

359
src/test/ref/varcall-2.log Normal file
View File

@ -0,0 +1,359 @@
Calling convention __varcall adding prepare/execute/finalize for main::$0 = call plus main::a 1
Calling convention __varcall adding prepare/execute/finalize for main::$1 = call plus main::a 1
Converting parameter in __varcall procedure to load/store plus::a
Converting parameter in __varcall procedure to load/store plus::b
Converting return in __varcall procedure to load/store plus::return
CONTROL FLOW GRAPH SSA
void main()
main: scope:[main] from __start
main::a#0 = 1
*BGCOL = main::a#0
plus::a = main::a#0
plus::b = 1
callexecute plus
main::$0 = plus::return
main::a#1 = main::$0
*BGCOL = main::a#1
plus::a = main::a#1
plus::b = 1
callexecute plus
main::$1 = plus::return
main::a#2 = main::$1
*BGCOL = main::a#2
to:main::@return
main::@return: scope:[main] from main
return
to:@return
__varcall byte plus(byte plus::a , byte plus::b)
plus: scope:[plus] from
plus::$0 = plus::a + plus::b
plus::return = plus::$0
to:plus::@return
plus::@return: scope:[plus] from plus
return
to:@return
void __start()
__start: scope:[__start] from
call main
to:__start::@1
__start::@1: scope:[__start] from __start
to:__start::@return
__start::@return: scope:[__start] from __start::@1
return
to:@return
SYMBOL TABLE SSA
const nomodify byte* BGCOL = (byte*)$d021
void __start()
void main()
byte~ main::$0
byte~ main::$1
byte main::a
byte main::a#0
byte main::a#1
byte main::a#2
__varcall byte plus(byte plus::a , byte plus::b)
byte~ plus::$0
byte plus::a loadstore
byte plus::b loadstore
byte plus::return loadstore
Adding number conversion cast (unumber) 1 in plus::b = 1
Adding number conversion cast (unumber) 1 in plus::b = 1
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast plus::b = (unumber)1
Inlining cast plus::b = (unumber)1
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 53281
Simplifying constant integer cast 1
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type 1
Finalized unsigned number type 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias candidate removed (volatile)plus::return = plus::$0
Alias main::a#1 = main::$0
Alias main::a#2 = main::$1
Successful SSA optimization Pass2AliasElimination
Alias candidate removed (volatile)plus::return = plus::$0
Constant main::a#0 = 1
Successful SSA optimization Pass2ConstantIdentification
Removing unused procedure __start
Removing unused procedure block __start
Removing unused procedure block __start::@1
Removing unused procedure block __start::@return
Successful SSA optimization PassNEliminateEmptyStart
Alias candidate removed (volatile)plus::return = plus::$0
Inlining constant with var siblings main::a#0
Constant inlined main::a#0 = 1
Successful SSA optimization Pass2ConstantInlining
Alias candidate removed (volatile)plus::return = plus::$0
Alias candidate removed (volatile)plus::return = plus::$0
CALL GRAPH
Calls in [main] to plus:6 plus:11
Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
FINAL CONTROL FLOW GRAPH
__varcall byte plus(byte plus::a , byte plus::b)
plus: scope:[plus] from
[0] plus::$0 = plus::a + plus::b
[1] plus::return = plus::$0
to:plus::@return
plus::@return: scope:[plus] from plus
[2] return
to:@return
void main()
main: scope:[main] from
[3] *BGCOL = 1
[4] plus::a = 1
[5] plus::b = 1
[6] callexecute plus
[7] main::a#1 = plus::return
[8] *BGCOL = main::a#1
[9] plus::a = main::a#1
[10] plus::b = 1
[11] callexecute plus
[12] main::a#2 = plus::return
[13] *BGCOL = main::a#2
to:main::@return
main::@return: scope:[main] from main
[14] return
to:@return
VARIABLE REGISTER WEIGHTS
void main()
byte main::a
byte main::a#1 3.0
byte main::a#2 4.0
__varcall byte plus(byte plus::a , byte plus::b)
byte~ plus::$0 22.0
byte plus::a loadstore 3.75
byte plus::b loadstore 7.5
byte plus::return loadstore 3.75
Initial phi equivalence classes
Added variable plus::$0 to live range equivalence class [ plus::$0 ]
Added variable plus::return to live range equivalence class [ plus::return ]
Added variable plus::a to live range equivalence class [ plus::a ]
Added variable plus::b to live range equivalence class [ plus::b ]
Added variable main::a#1 to live range equivalence class [ main::a#1 ]
Added variable main::a#2 to live range equivalence class [ main::a#2 ]
Complete equivalence classes
[ plus::$0 ]
[ plus::return ]
[ plus::a ]
[ plus::b ]
[ main::a#1 ]
[ main::a#2 ]
Allocated zp[1]:2 [ plus::$0 ]
Allocated zp[1]:3 [ plus::return ]
Allocated zp[1]:4 [ plus::a ]
Allocated zp[1]:5 [ plus::b ]
Allocated zp[1]:6 [ main::a#1 ]
Allocated zp[1]:7 [ main::a#2 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [0] plus::$0 = plus::a + plus::b [ plus::$0 ] ( plus:6 [ plus::$0 ] { { main::a#1 = plus::a } } plus:11 [ plus::$0 ] { { main::a#1 = plus::a } } ) always clobbers reg byte a
Statement [3] *BGCOL = 1 [ ] ( [ ] { } ) always clobbers reg byte a
Statement [4] plus::a = 1 [ plus::a ] ( [ plus::a ] { } ) always clobbers reg byte a
Statement [5] plus::b = 1 [ plus::a plus::b ] ( [ plus::a plus::b ] { } ) always clobbers reg byte a
Statement [10] plus::b = 1 [ plus::a plus::b ] ( [ plus::a plus::b ] { } ) always clobbers reg byte a
Potential registers zp[1]:2 [ plus::$0 ] : zp[1]:2 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:3 [ plus::return ] : zp[1]:3 ,
Potential registers zp[1]:4 [ plus::a ] : zp[1]:4 ,
Potential registers zp[1]:5 [ plus::b ] : zp[1]:5 ,
Potential registers zp[1]:6 [ main::a#1 ] : zp[1]:6 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:7 [ main::a#2 ] : zp[1]:7 , reg byte a , reg byte x , reg byte y ,
REGISTER UPLIFT SCOPES
Uplift Scope [plus] 22: zp[1]:2 [ plus::$0 ] 7.5: zp[1]:5 [ plus::b ] 3.75: zp[1]:3 [ plus::return ] 3.75: zp[1]:4 [ plus::a ]
Uplift Scope [main] 4: zp[1]:7 [ main::a#2 ] 3: zp[1]:6 [ main::a#1 ]
Uplift Scope []
Uplifting [plus] best 94 combination reg byte a [ plus::$0 ] zp[1]:5 [ plus::b ] zp[1]:3 [ plus::return ] zp[1]:4 [ plus::a ]
Uplifting [main] best 79 combination reg byte a [ main::a#2 ] reg byte a [ main::a#1 ]
Uplifting [] best 79 combination
Attempting to uplift remaining variables inzp[1]:5 [ plus::b ]
Uplifting [plus] best 79 combination zp[1]:5 [ plus::b ]
Attempting to uplift remaining variables inzp[1]:3 [ plus::return ]
Uplifting [plus] best 79 combination zp[1]:3 [ plus::return ]
Attempting to uplift remaining variables inzp[1]:4 [ plus::a ]
Uplifting [plus] best 79 combination zp[1]:4 [ plus::a ]
Allocated (was zp[1]:3) zp[1]:2 [ plus::return ]
Allocated (was zp[1]:4) zp[1]:3 [ plus::a ]
Allocated (was zp[1]:5) zp[1]:4 [ plus::b ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test __varcall calling convention
// Return value
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.label BGCOL = $d021
// plus
// plus(byte zp(3) a, byte zp(4) b)
plus: {
.label return = 2
.label a = 3
.label b = 4
// [0] plus::$0 = plus::a + plus::b -- vbuaa=vbuz1_plus_vbuz2
lda.z a
clc
adc.z b
// [1] plus::return = plus::$0 -- vbuz1=vbuaa
sta.z return
jmp __breturn
// plus::@return
__breturn:
// [2] return
rts
}
// main
main: {
// [3] *BGCOL = 1 -- _deref_pbuc1=vbuc2
lda #1
sta BGCOL
// [4] plus::a = 1 -- vbuz1=vbuc1
lda #1
sta.z plus.a
// [5] plus::b = 1 -- vbuz1=vbuc1
lda #1
sta.z plus.b
// [6] callexecute plus -- jsr
jsr plus
// [7] main::a#1 = plus::return -- vbuaa=vbuz1
lda.z plus.return
// [8] *BGCOL = main::a#1 -- _deref_pbuc1=vbuaa
sta BGCOL
// [9] plus::a = main::a#1 -- vbuz1=vbuaa
sta.z plus.a
// [10] plus::b = 1 -- vbuz1=vbuc1
lda #1
sta.z plus.b
// [11] callexecute plus -- jsr
jsr plus
// [12] main::a#2 = plus::return -- vbuaa=vbuz1
lda.z plus.return
// [13] *BGCOL = main::a#2 -- _deref_pbuc1=vbuaa
sta BGCOL
jmp __breturn
// main::@return
__breturn:
// [14] return
rts
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda #1
Removing instruction lda #1
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Removing instruction __breturn:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
const nomodify byte* BGCOL = (byte*) 53281
void main()
byte main::a
byte main::a#1 reg byte a 3.0
byte main::a#2 reg byte a 4.0
__varcall byte plus(byte plus::a , byte plus::b)
byte~ plus::$0 reg byte a 22.0
byte plus::a loadstore zp[1]:3 3.75
byte plus::b loadstore zp[1]:4 7.5
byte plus::return loadstore zp[1]:2 3.75
reg byte a [ plus::$0 ]
zp[1]:2 [ plus::return ]
zp[1]:3 [ plus::a ]
zp[1]:4 [ plus::b ]
reg byte a [ main::a#1 ]
reg byte a [ main::a#2 ]
FINAL ASSEMBLER
Score: 69
// File Comments
// Test __varcall calling convention
// Return value
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.label BGCOL = $d021
// plus
// plus(byte zp(3) a, byte zp(4) b)
plus: {
.label return = 2
.label a = 3
.label b = 4
// a+b
// [0] plus::$0 = plus::a + plus::b -- vbuaa=vbuz1_plus_vbuz2
lda.z a
clc
adc.z b
// return a+b;
// [1] plus::return = plus::$0 -- vbuz1=vbuaa
sta.z return
// plus::@return
// }
// [2] return
rts
}
// main
main: {
// *BGCOL = a
// [3] *BGCOL = 1 -- _deref_pbuc1=vbuc2
lda #1
sta BGCOL
// plus(a, 1)
// [4] plus::a = 1 -- vbuz1=vbuc1
sta.z plus.a
// [5] plus::b = 1 -- vbuz1=vbuc1
sta.z plus.b
// [6] callexecute plus -- jsr
jsr plus
// a = plus(a, 1)
// [7] main::a#1 = plus::return -- vbuaa=vbuz1
lda.z plus.return
// *BGCOL = a
// [8] *BGCOL = main::a#1 -- _deref_pbuc1=vbuaa
sta BGCOL
// plus(a, 1)
// [9] plus::a = main::a#1 -- vbuz1=vbuaa
sta.z plus.a
// [10] plus::b = 1 -- vbuz1=vbuc1
lda #1
sta.z plus.b
// [11] callexecute plus -- jsr
jsr plus
// a = plus(a, 1)
// [12] main::a#2 = plus::return -- vbuaa=vbuz1
lda.z plus.return
// *BGCOL = a
// [13] *BGCOL = main::a#2 -- _deref_pbuc1=vbuaa
sta BGCOL
// main::@return
// }
// [14] return
rts
}
// File Data

View File

@ -0,0 +1,17 @@
const nomodify byte* BGCOL = (byte*) 53281
void main()
byte main::a
byte main::a#1 reg byte a 3.0
byte main::a#2 reg byte a 4.0
__varcall byte plus(byte plus::a , byte plus::b)
byte~ plus::$0 reg byte a 22.0
byte plus::a loadstore zp[1]:3 3.75
byte plus::b loadstore zp[1]:4 7.5
byte plus::return loadstore zp[1]:2 3.75
reg byte a [ plus::$0 ]
zp[1]:2 [ plus::return ]
zp[1]:3 [ plus::a ]
zp[1]:4 [ plus::b ]
reg byte a [ main::a#1 ]
reg byte a [ main::a#2 ]