1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-08-01 17:29:45 +00:00

Added initial support for function declarations without a body. #196

This commit is contained in:
jespergravgaard 2020-04-08 23:40:27 +02:00
parent 433fcd3dd4
commit db92daf4bf
17 changed files with 1914 additions and 1066 deletions

View File

@ -137,15 +137,23 @@ public class StatementSource implements Serializable {
return new StatementSource(nodeStart, nodeStop);
}
public static StatementSource procedureEnd(KickCParser.DeclFunctionContext ctx) {
ParseTree nodeStart = ctx.getChild(ctx.getChildCount() - 1);
ParseTree nodeStop = ctx;
public static StatementSource procedureDecl(KickCParser.DeclFunctionContext ctx) {
ParseTree nodeStart = ctx;
ParseTree nodeStop = ctx.getChild(ctx.getChildCount() - 2);
return new StatementSource(nodeStart, nodeStop);
}
public static StatementSource procedureBegin(KickCParser.DeclFunctionContext ctx) {
ParseTree nodeStart = ctx;
ParseTree nodeStop = ctx.getChild(ctx.getChildCount() - 4);
final KickCParser.DeclFunctionBodyContext bodyCtx = ctx.declFunctionBody();
ParseTree nodeStart = bodyCtx;
ParseTree nodeStop = bodyCtx.getChild(bodyCtx.getChildCount() - 4);
return new StatementSource(nodeStart, nodeStop);
}
public static StatementSource procedureEnd(KickCParser.DeclFunctionContext ctx) {
final KickCParser.DeclFunctionBodyContext bodyCtx = ctx.declFunctionBody();
ParseTree nodeStart = bodyCtx.getChild(bodyCtx.getChildCount() - 1);
ParseTree nodeStop = bodyCtx;
return new StatementSource(nodeStart, nodeStop);
}

View File

@ -629,7 +629,7 @@ public class Variable implements Symbol {
Variable variable = (Variable) o;
return kind == variable.kind &&
Objects.equals(name, variable.name) &&
Objects.equals(scope, variable.scope) &&
Objects.equals(scope.getRef(), variable.scope.getRef()) &&
Objects.equals(type, variable.type);
}

View File

@ -3,6 +3,7 @@ package dk.camelot64.kickc.model.types;
import dk.camelot64.kickc.model.CompileError;
import dk.camelot64.kickc.model.ConstantNotLiteral;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.symbols.Procedure;
import dk.camelot64.kickc.model.symbols.ProgramScope;
import dk.camelot64.kickc.model.values.ConstantInteger;
import dk.camelot64.kickc.model.values.ConstantLiteral;
@ -247,5 +248,21 @@ public class SymbolTypeConversion {
return true;
}
/**
* Checks that two procedure declarations are a proper match
* @param first The first procedure declaration
* @param second The first procedure declaration
* @return true if they match types. False otherwise
*/
public static boolean procedureDeclarationMatch(Procedure first, Procedure second) {
if(!first.getFullName().equals(second.getFullName()))
return false;
if(!first.getReturnType().equals(second.getReturnType()))
return false;
if(!first.getParameters().equals(second.getParameters()))
return false;
return true;
}
}

View File

@ -128,7 +128,11 @@ enumMember
;
declFunction
: declType declPointer* NAME PAR_BEGIN parameterListDecl? PAR_END CURLY_BEGIN stmtSeq? CURLY_END
: declType declPointer* NAME PAR_BEGIN parameterListDecl? PAR_END (declFunctionBody | ';' )
;
declFunctionBody
: CURLY_BEGIN stmtSeq? CURLY_END
;
parameterListDecl

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -445,6 +445,18 @@ public class KickCParserBaseListener implements KickCParserListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitDeclFunction(KickCParser.DeclFunctionContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterDeclFunctionBody(KickCParser.DeclFunctionBodyContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitDeclFunctionBody(KickCParser.DeclFunctionBodyContext ctx) { }
/**
* {@inheritDoc}
*

View File

@ -265,6 +265,13 @@ public class KickCParserBaseVisitor<T> extends AbstractParseTreeVisitor<T> imple
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitDeclFunction(KickCParser.DeclFunctionContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitDeclFunctionBody(KickCParser.DeclFunctionBodyContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*

View File

@ -403,6 +403,16 @@ public interface KickCParserListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitDeclFunction(KickCParser.DeclFunctionContext ctx);
/**
* Enter a parse tree produced by {@link KickCParser#declFunctionBody}.
* @param ctx the parse tree
*/
void enterDeclFunctionBody(KickCParser.DeclFunctionBodyContext ctx);
/**
* Exit a parse tree produced by {@link KickCParser#declFunctionBody}.
* @param ctx the parse tree
*/
void exitDeclFunctionBody(KickCParser.DeclFunctionBodyContext ctx);
/**
* Enter a parse tree produced by {@link KickCParser#parameterListDecl}.
* @param ctx the parse tree

View File

@ -245,6 +245,12 @@ public interface KickCParserVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitDeclFunction(KickCParser.DeclFunctionContext ctx);
/**
* Visit a parse tree produced by {@link KickCParser#declFunctionBody}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitDeclFunctionBody(KickCParser.DeclFunctionBodyContext ctx);
/**
* Visit a parse tree produced by {@link KickCParser#parameterListDecl}.
* @param ctx the parse tree

View File

@ -9,6 +9,7 @@ import dk.camelot64.kickc.model.operators.*;
import dk.camelot64.kickc.model.statements.*;
import dk.camelot64.kickc.model.symbols.*;
import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.types.SymbolTypeConversion;
import dk.camelot64.kickc.model.types.SymbolTypePointer;
import dk.camelot64.kickc.model.types.SymbolTypeProcedure;
import dk.camelot64.kickc.model.values.*;
@ -17,10 +18,8 @@ import dk.camelot64.kickc.parser.KickCParser;
import dk.camelot64.kickc.parser.KickCParserBaseVisitor;
import dk.camelot64.kickc.passes.utils.SizeOfConstants;
import org.antlr.v4.runtime.BufferedTokenStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
@ -224,12 +223,12 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
SymbolType type = varDecl.getEffectiveType();
List<Directive> directives = varDecl.getEffectiveDirectives();
String name = ctx.NAME().getText();
Procedure procedure = getCurrentScope().addProcedure(name, type, currentCodeSegment, currentDataSegment, currentCallingConvention);
addDirectives(procedure, directives, StatementSource.procedureBegin(ctx));
Procedure procedure = new Procedure(name, type, program.getScope(), currentCodeSegment, currentDataSegment, currentCallingConvention);
addDirectives(procedure, directives, StatementSource.procedureDecl(ctx));
procedure.setComments(ensureUnusedComments(getCommentsSymbol(ctx)));
varDecl.exitType();
scopeStack.push(procedure);
Label procExit = procedure.addLabel(SymbolRef.PROCEXIT_BLOCK_NAME);
Variable returnVar = null;
if(!SymbolType.VOID.equals(type)) {
returnVar = procedure.add(Variable.createPhiMaster("return", type, procedure, defaultMemoryArea, procedure.getSegmentData()));
@ -239,27 +238,57 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
parameterList = (List<Variable>) this.visit(ctx.parameterListDecl());
}
procedure.setParameters(parameterList);
sequence.addStatement(new StatementProcedureBegin(procedure.getRef(), StatementSource.procedureBegin(ctx), Comment.NO_COMMENTS));
// Add parameter assignments
if(Procedure.CallingConvention.STACK_CALL.equals(procedure.getCallingConvention())) {
for(Variable param : parameterList) {
sequence.addStatement(new StatementAssignment((LValue) param.getRef(), new ParamValue((VariableRef) param.getRef()), true, StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
}
}
if(ctx.stmtSeq() != null) {
this.visit(ctx.stmtSeq());
}
sequence.addStatement(new StatementLabel(procExit.getRef(), StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
if(Procedure.CallingConvention.PHI_CALL.equals(procedure.getCallingConvention()) && returnVar != null) {
sequence.addStatement(new StatementAssignment(returnVar.getVariableRef(), returnVar.getRef(), false, StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
}
SymbolVariableRef returnVarRef = null;
if(returnVar != null) {
returnVarRef = returnVar.getRef();
}
sequence.addStatement(new StatementReturn(returnVarRef, StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
scopeStack.pop();
sequence.addStatement(new StatementProcedureEnd(procedure.getRef(), StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
// Check that the declaration matches any existing declaration!
final Symbol existingSymbol = program.getScope().getSymbol(procedure.getRef());
if(existingSymbol!=null) {
// Already declared - check equality
if(!SymbolTypeConversion.procedureDeclarationMatch((Procedure) existingSymbol, procedure))
throw new CompileError("Error! Conflicting declarations for "+procedure.getFullName(), StatementSource.procedureBegin(ctx));
} else {
// Not declared before - add it
program.getScope().add(procedure);
}
if(ctx.declFunctionBody() != null) {
// Make sure directives and more are taken from the procedure with the body!
if(existingSymbol!=null) {
program.getScope().remove(existingSymbol);
program.getScope().add(procedure);
}
// Check that the body has not already been added
for(Statement statement : sequence.getStatements())
if(statement instanceof StatementProcedureBegin && ((StatementProcedureBegin) statement).getProcedure().equals(procedure.getRef()))
throw new CompileError("Error! Redefinition of function "+procedure.getFullName(), StatementSource.procedureBegin(ctx));
// Add the body
scopeStack.push(procedure);
sequence.addStatement(new StatementProcedureBegin(procedure.getRef(), StatementSource.procedureBegin(ctx), Comment.NO_COMMENTS));
// Add parameter assignments
if(Procedure.CallingConvention.STACK_CALL.equals(procedure.getCallingConvention())) {
for(Variable param : parameterList) {
sequence.addStatement(new StatementAssignment((LValue) param.getRef(), new ParamValue((VariableRef) param.getRef()), true, StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
}
}
Label procExit = procedure.addLabel(SymbolRef.PROCEXIT_BLOCK_NAME);
if(ctx.declFunctionBody().stmtSeq() != null) {
this.visit(ctx.declFunctionBody().stmtSeq());
}
sequence.addStatement(new StatementLabel(procExit.getRef(), StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
if(Procedure.CallingConvention.PHI_CALL.equals(procedure.getCallingConvention()) && returnVar != null) {
sequence.addStatement(new StatementAssignment(returnVar.getVariableRef(), returnVar.getRef(), false, StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
}
SymbolVariableRef returnVarRef = null;
if(returnVar != null) {
returnVarRef = returnVar.getRef();
}
sequence.addStatement(new StatementReturn(returnVarRef, StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
scopeStack.pop();
sequence.addStatement(new StatementProcedureEnd(procedure.getRef(), StatementSource.procedureEnd(ctx), Comment.NO_COMMENTS));
}
return null;
}
@ -557,7 +586,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
public VariableDeclaration() {
this.declType = new VariableDeclType();
}
}
/**
* Exits the type layer (clears everything except struct information)
@ -2363,7 +2392,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
List<Comment> comments = new ArrayList<>();
BufferedTokenStream tokenStream = cParser.getTokenStream();
final int startTokenIndex = ctx.start.getTokenIndex();
if(startTokenIndex<0)
if(startTokenIndex < 0)
return commentBlocks;
List<Token> hiddenTokens = tokenStream.getHiddenTokensToLeft(startTokenIndex);
if(hiddenTokens != null) {

View File

@ -37,6 +37,11 @@ public class TestPrograms {
public TestPrograms() {
}
@Test
public void testCStyleDecl0() throws IOException, URISyntaxException {
compileAndCompare("cstyle-decl-0");
}
@Test
public void testPreprocessor8() throws IOException, URISyntaxException {
compileAndCompare("preprocessor-8");

View File

@ -0,0 +1,26 @@
// Test declarations without body
// Declaration of main-function
void main(void);
// Declaration of a sum-function
char sum(char a, char b);
char * const SCREEN = 0x0400;
// Definition of main()
void main() {
SCREEN[0] = sum('a', 2);
SCREEN[1] = sum('a', 12);
}
// Definition of sum()
char sum(char a, char b) {
return a+b;
}

View File

@ -0,0 +1,34 @@
// Test declarations without body
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label SCREEN = $400
// Definition of main()
main: {
// sum('a', 2)
lda #2
ldx #'a'
jsr sum
// sum('a', 2)
// SCREEN[0] = sum('a', 2)
sta SCREEN
// sum('a', 12)
lda #$c
ldx #'a'
jsr sum
// sum('a', 12)
// SCREEN[1] = sum('a', 12)
sta SCREEN+1
// }
rts
}
// Definition of sum()
// sum(byte register(X) a, byte register(A) b)
sum: {
// a+b
stx.z $ff
clc
adc.z $ff
// }
rts
}

View File

@ -0,0 +1,39 @@
@begin: scope:[] from
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
to:@end
@end: scope:[] from @1
[3] phi()
(void()) main()
main: scope:[main] from @1
[4] phi()
[5] call sum
[6] (byte) sum::return#0 ← (byte) sum::return#2
to:main::@1
main::@1: scope:[main] from main
[7] (byte~) main::$0 ← (byte) sum::return#0
[8] *((const nomodify byte*) SCREEN) ← (byte~) main::$0
[9] call sum
[10] (byte) sum::return#1 ← (byte) sum::return#2
to:main::@2
main::@2: scope:[main] from main::@1
[11] (byte~) main::$1 ← (byte) sum::return#1
[12] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte~) main::$1
to:main::@return
main::@return: scope:[main] from main::@2
[13] return
to:@return
(byte()) sum((byte) sum::a , (byte) sum::b)
sum: scope:[sum] from main main::@1
[14] (byte) sum::b#2 ← phi( main/(byte) 2 main::@1/(byte) $c )
[14] (byte) sum::a#2 ← phi( main/(byte) 'a' main::@1/(byte) 'a' )
[15] (byte) sum::return#2 ← (byte) sum::a#2 + (byte) sum::b#2
to:sum::@return
sum::@return: scope:[sum] from sum
[16] return
to:@return

View File

@ -0,0 +1,557 @@
Culled Empty Block (label) @1
Culled Empty Block (label) sum::@1
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
to:@2
(void()) main()
main: scope:[main] from @2
(byte) sum::a#0 ← (byte) 'a'
(byte) sum::b#0 ← (number) 2
call sum
(byte) sum::return#0 ← (byte) sum::return#3
to:main::@1
main::@1: scope:[main] from main
(byte) sum::return#4 ← phi( main/(byte) sum::return#0 )
(byte~) main::$0 ← (byte) sum::return#4
*((const nomodify byte*) SCREEN + (number) 0) ← (byte~) main::$0
(byte) sum::a#1 ← (byte) 'a'
(byte) sum::b#1 ← (number) $c
call sum
(byte) sum::return#1 ← (byte) sum::return#3
to:main::@2
main::@2: scope:[main] from main::@1
(byte) sum::return#5 ← phi( main::@1/(byte) sum::return#1 )
(byte~) main::$1 ← (byte) sum::return#5
*((const nomodify byte*) SCREEN + (number) 1) ← (byte~) main::$1
to:main::@return
main::@return: scope:[main] from main::@2
return
to:@return
(byte()) sum((byte) sum::a , (byte) sum::b)
sum: scope:[sum] from main main::@1
(byte) sum::b#2 ← phi( main/(byte) sum::b#0 main::@1/(byte) sum::b#1 )
(byte) sum::a#2 ← phi( main/(byte) sum::a#0 main::@1/(byte) sum::a#1 )
(byte~) sum::$0 ← (byte) sum::a#2 + (byte) sum::b#2
(byte) sum::return#2 ← (byte~) sum::$0
to:sum::@return
sum::@return: scope:[sum] from sum
(byte) sum::return#6 ← phi( sum/(byte) sum::return#2 )
(byte) sum::return#3 ← (byte) sum::return#6
return
to:@return
@2: scope:[] from @begin
call main
to:@3
@3: scope:[] from @2
to:@end
@end: scope:[] from @3
SYMBOL TABLE SSA
(label) @2
(label) @3
(label) @begin
(label) @end
(const nomodify byte*) SCREEN = (byte*)(number) $400
(void()) main()
(byte~) main::$0
(byte~) main::$1
(label) main::@1
(label) main::@2
(label) main::@return
(byte()) sum((byte) sum::a , (byte) sum::b)
(byte~) sum::$0
(label) sum::@return
(byte) sum::a
(byte) sum::a#0
(byte) sum::a#1
(byte) sum::a#2
(byte) sum::b
(byte) sum::b#0
(byte) sum::b#1
(byte) sum::b#2
(byte) sum::return
(byte) sum::return#0
(byte) sum::return#1
(byte) sum::return#2
(byte) sum::return#3
(byte) sum::return#4
(byte) sum::return#5
(byte) sum::return#6
Adding number conversion cast (unumber) 2 in (byte) sum::b#0 ← (number) 2
Adding number conversion cast (unumber) 0 in *((const nomodify byte*) SCREEN + (number) 0) ← (byte~) main::$0
Adding number conversion cast (unumber) $c in (byte) sum::b#1 ← (number) $c
Adding number conversion cast (unumber) 1 in *((const nomodify byte*) SCREEN + (number) 1) ← (byte~) main::$1
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (byte) sum::b#0 ← (unumber)(number) 2
Inlining cast (byte) sum::b#1 ← (unumber)(number) $c
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 1024
Simplifying constant integer cast 2
Simplifying constant integer cast 0
Simplifying constant integer cast $c
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 2
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) $c
Finalized unsigned number type (byte) 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Alias sum::return#0 = sum::return#4
Alias sum::return#1 = sum::return#5
Alias sum::return#2 = sum::$0 sum::return#6 sum::return#3
Successful SSA optimization Pass2AliasElimination
Constant (const byte) sum::a#0 = 'a'
Constant (const byte) sum::b#0 = 2
Constant (const byte) sum::a#1 = 'a'
Constant (const byte) sum::b#1 = $c
Successful SSA optimization Pass2ConstantIdentification
Simplifying expression containing zero SCREEN in [5] *((const nomodify byte*) SCREEN + (byte) 0) ← (byte~) main::$0
Successful SSA optimization PassNSimplifyExpressionWithZero
Inlining constant with var siblings (const byte) sum::a#0
Inlining constant with var siblings (const byte) sum::b#0
Inlining constant with var siblings (const byte) sum::a#1
Inlining constant with var siblings (const byte) sum::b#1
Constant inlined sum::b#1 = (byte) $c
Constant inlined sum::b#0 = (byte) 2
Constant inlined sum::a#1 = (byte) 'a'
Constant inlined sum::a#0 = (byte) 'a'
Successful SSA optimization Pass2ConstantInlining
Consolidated array index constant in *(SCREEN+1)
Successful SSA optimization Pass2ConstantAdditionElimination
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @2
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
CALL GRAPH
Calls in [] to main:2
Calls in [main] to sum:6 sum:10
Created 2 initial phi equivalence classes
Coalesced down to 2 phi equivalence classes
Culled Empty Block (label) @3
Renumbering block @2 to @1
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()
(void()) main()
main: scope:[main] from @1
[4] phi()
[5] call sum
[6] (byte) sum::return#0 ← (byte) sum::return#2
to:main::@1
main::@1: scope:[main] from main
[7] (byte~) main::$0 ← (byte) sum::return#0
[8] *((const nomodify byte*) SCREEN) ← (byte~) main::$0
[9] call sum
[10] (byte) sum::return#1 ← (byte) sum::return#2
to:main::@2
main::@2: scope:[main] from main::@1
[11] (byte~) main::$1 ← (byte) sum::return#1
[12] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte~) main::$1
to:main::@return
main::@return: scope:[main] from main::@2
[13] return
to:@return
(byte()) sum((byte) sum::a , (byte) sum::b)
sum: scope:[sum] from main main::@1
[14] (byte) sum::b#2 ← phi( main/(byte) 2 main::@1/(byte) $c )
[14] (byte) sum::a#2 ← phi( main/(byte) 'a' main::@1/(byte) 'a' )
[15] (byte) sum::return#2 ← (byte) sum::a#2 + (byte) sum::b#2
to:sum::@return
sum::@return: scope:[sum] from sum
[16] return
to:@return
VARIABLE REGISTER WEIGHTS
(void()) main()
(byte~) main::$0 22.0
(byte~) main::$1 22.0
(byte()) sum((byte) sum::a , (byte) sum::b)
(byte) sum::a
(byte) sum::a#2 101.0
(byte) sum::b
(byte) sum::b#2 101.0
(byte) sum::return
(byte) sum::return#0 22.0
(byte) sum::return#1 22.0
(byte) sum::return#2 30.75
Initial phi equivalence classes
[ sum::a#2 ]
[ sum::b#2 ]
Added variable sum::return#0 to live range equivalence class [ sum::return#0 ]
Added variable main::$0 to live range equivalence class [ main::$0 ]
Added variable sum::return#1 to live range equivalence class [ sum::return#1 ]
Added variable main::$1 to live range equivalence class [ main::$1 ]
Added variable sum::return#2 to live range equivalence class [ sum::return#2 ]
Complete equivalence classes
[ sum::a#2 ]
[ sum::b#2 ]
[ sum::return#0 ]
[ main::$0 ]
[ sum::return#1 ]
[ main::$1 ]
[ sum::return#2 ]
Allocated zp[1]:2 [ sum::a#2 ]
Allocated zp[1]:3 [ sum::b#2 ]
Allocated zp[1]:4 [ sum::return#0 ]
Allocated zp[1]:5 [ main::$0 ]
Allocated zp[1]:6 [ sum::return#1 ]
Allocated zp[1]:7 [ main::$1 ]
Allocated zp[1]:8 [ sum::return#2 ]
INITIAL ASM
Target platform is c64basic / MOS6502X
// File Comments
// Test declarations without body
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @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
// Definition of main()
main: {
.label __0 = 5
.label __1 = 7
// [5] call sum
// [14] phi from main to sum [phi:main->sum]
sum_from_main:
// [14] phi (byte) sum::b#2 = (byte) 2 [phi:main->sum#0] -- vbuz1=vbuc1
lda #2
sta.z sum.b
// [14] phi (byte) sum::a#2 = (byte) 'a' [phi:main->sum#1] -- vbuz1=vbuc1
lda #'a'
sta.z sum.a
jsr sum
// [6] (byte) sum::return#0 ← (byte) sum::return#2 -- vbuz1=vbuz2
lda.z sum.return_2
sta.z sum.return
jmp __b1
// main::@1
__b1:
// [7] (byte~) main::$0 ← (byte) sum::return#0 -- vbuz1=vbuz2
lda.z sum.return
sta.z __0
// [8] *((const nomodify byte*) SCREEN) ← (byte~) main::$0 -- _deref_pbuc1=vbuz1
lda.z __0
sta SCREEN
// [9] call sum
// [14] phi from main::@1 to sum [phi:main::@1->sum]
sum_from___b1:
// [14] phi (byte) sum::b#2 = (byte) $c [phi:main::@1->sum#0] -- vbuz1=vbuc1
lda #$c
sta.z sum.b
// [14] phi (byte) sum::a#2 = (byte) 'a' [phi:main::@1->sum#1] -- vbuz1=vbuc1
lda #'a'
sta.z sum.a
jsr sum
// [10] (byte) sum::return#1 ← (byte) sum::return#2 -- vbuz1=vbuz2
lda.z sum.return_2
sta.z sum.return_1
jmp __b2
// main::@2
__b2:
// [11] (byte~) main::$1 ← (byte) sum::return#1 -- vbuz1=vbuz2
lda.z sum.return_1
sta.z __1
// [12] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte~) main::$1 -- _deref_pbuc1=vbuz1
lda.z __1
sta SCREEN+1
jmp __breturn
// main::@return
__breturn:
// [13] return
rts
}
// sum
// Definition of sum()
// sum(byte zp(2) a, byte zp(3) b)
sum: {
.label return = 4
.label return_1 = 6
.label return_2 = 8
.label a = 2
.label b = 3
// [15] (byte) sum::return#2 ← (byte) sum::a#2 + (byte) sum::b#2 -- vbuz1=vbuz2_plus_vbuz3
lda.z a
clc
adc.z b
sta.z return_2
jmp __breturn
// sum::@return
__breturn:
// [16] return
rts
}
// File Data
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [15] (byte) sum::return#2 ← (byte) sum::a#2 + (byte) sum::b#2 [ sum::return#2 ] ( main:2::sum:5 [ sum::return#2 ] { { sum::return#0 = sum::return#2 } } main:2::sum:9 [ sum::return#2 ] { { sum::return#1 = sum::return#2 } } ) always clobbers reg byte a
Potential registers zp[1]:2 [ sum::a#2 ] : zp[1]:2 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:3 [ sum::b#2 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:4 [ sum::return#0 ] : zp[1]:4 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:5 [ main::$0 ] : zp[1]:5 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:6 [ sum::return#1 ] : zp[1]:6 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:7 [ main::$1 ] : zp[1]:7 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:8 [ sum::return#2 ] : zp[1]:8 , reg byte a , reg byte x , reg byte y ,
REGISTER UPLIFT SCOPES
Uplift Scope [sum] 101: zp[1]:2 [ sum::a#2 ] 101: zp[1]:3 [ sum::b#2 ] 30.75: zp[1]:8 [ sum::return#2 ] 22: zp[1]:4 [ sum::return#0 ] 22: zp[1]:6 [ sum::return#1 ]
Uplift Scope [main] 22: zp[1]:5 [ main::$0 ] 22: zp[1]:7 [ main::$1 ]
Uplift Scope []
Uplifting [sum] best 90 combination reg byte x [ sum::a#2 ] reg byte a [ sum::b#2 ] reg byte a [ sum::return#2 ] reg byte a [ sum::return#0 ] zp[1]:6 [ sum::return#1 ]
Limited combination testing to 100 combinations of 1024 possible.
Uplifting [main] best 78 combination reg byte a [ main::$0 ] reg byte a [ main::$1 ]
Uplifting [] best 78 combination
Attempting to uplift remaining variables inzp[1]:6 [ sum::return#1 ]
Uplifting [sum] best 72 combination reg byte a [ sum::return#1 ]
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test declarations without body
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @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
// Definition of main()
main: {
// [5] call sum
// [14] phi from main to sum [phi:main->sum]
sum_from_main:
// [14] phi (byte) sum::b#2 = (byte) 2 [phi:main->sum#0] -- vbuaa=vbuc1
lda #2
// [14] phi (byte) sum::a#2 = (byte) 'a' [phi:main->sum#1] -- vbuxx=vbuc1
ldx #'a'
jsr sum
// [6] (byte) sum::return#0 ← (byte) sum::return#2
jmp __b1
// main::@1
__b1:
// [7] (byte~) main::$0 ← (byte) sum::return#0
// [8] *((const nomodify byte*) SCREEN) ← (byte~) main::$0 -- _deref_pbuc1=vbuaa
sta SCREEN
// [9] call sum
// [14] phi from main::@1 to sum [phi:main::@1->sum]
sum_from___b1:
// [14] phi (byte) sum::b#2 = (byte) $c [phi:main::@1->sum#0] -- vbuaa=vbuc1
lda #$c
// [14] phi (byte) sum::a#2 = (byte) 'a' [phi:main::@1->sum#1] -- vbuxx=vbuc1
ldx #'a'
jsr sum
// [10] (byte) sum::return#1 ← (byte) sum::return#2
jmp __b2
// main::@2
__b2:
// [11] (byte~) main::$1 ← (byte) sum::return#1
// [12] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte~) main::$1 -- _deref_pbuc1=vbuaa
sta SCREEN+1
jmp __breturn
// main::@return
__breturn:
// [13] return
rts
}
// sum
// Definition of sum()
// sum(byte register(X) a, byte register(A) b)
sum: {
// [15] (byte) sum::return#2 ← (byte) sum::a#2 + (byte) sum::b#2 -- vbuaa=vbuxx_plus_vbuaa
stx.z $ff
clc
adc.z $ff
jmp __breturn
// sum::@return
__breturn:
// [16] return
rts
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __bend
Removing instruction jmp __b1
Removing instruction jmp __b2
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __b1_from___bbegin:
Removing instruction __b1:
Removing instruction main_from___b1:
Removing instruction __bend_from___b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __bend:
Removing instruction sum_from_main:
Removing instruction __b1:
Removing instruction sum_from___b1:
Removing instruction __b2:
Removing instruction __breturn:
Removing instruction __breturn:
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
(const nomodify byte*) SCREEN = (byte*) 1024
(void()) main()
(byte~) main::$0 reg byte a 22.0
(byte~) main::$1 reg byte a 22.0
(label) main::@1
(label) main::@2
(label) main::@return
(byte()) sum((byte) sum::a , (byte) sum::b)
(label) sum::@return
(byte) sum::a
(byte) sum::a#2 reg byte x 101.0
(byte) sum::b
(byte) sum::b#2 reg byte a 101.0
(byte) sum::return
(byte) sum::return#0 reg byte a 22.0
(byte) sum::return#1 reg byte a 22.0
(byte) sum::return#2 reg byte a 30.75
reg byte x [ sum::a#2 ]
reg byte a [ sum::b#2 ]
reg byte a [ sum::return#0 ]
reg byte a [ main::$0 ]
reg byte a [ sum::return#1 ]
reg byte a [ main::$1 ]
reg byte a [ sum::return#2 ]
FINAL ASSEMBLER
Score: 48
// File Comments
// Test declarations without body
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @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
// Definition of main()
main: {
// sum('a', 2)
// [5] call sum
// [14] phi from main to sum [phi:main->sum]
// [14] phi (byte) sum::b#2 = (byte) 2 [phi:main->sum#0] -- vbuaa=vbuc1
lda #2
// [14] phi (byte) sum::a#2 = (byte) 'a' [phi:main->sum#1] -- vbuxx=vbuc1
ldx #'a'
jsr sum
// sum('a', 2)
// [6] (byte) sum::return#0 ← (byte) sum::return#2
// main::@1
// [7] (byte~) main::$0 ← (byte) sum::return#0
// SCREEN[0] = sum('a', 2)
// [8] *((const nomodify byte*) SCREEN) ← (byte~) main::$0 -- _deref_pbuc1=vbuaa
sta SCREEN
// sum('a', 12)
// [9] call sum
// [14] phi from main::@1 to sum [phi:main::@1->sum]
// [14] phi (byte) sum::b#2 = (byte) $c [phi:main::@1->sum#0] -- vbuaa=vbuc1
lda #$c
// [14] phi (byte) sum::a#2 = (byte) 'a' [phi:main::@1->sum#1] -- vbuxx=vbuc1
ldx #'a'
jsr sum
// sum('a', 12)
// [10] (byte) sum::return#1 ← (byte) sum::return#2
// main::@2
// [11] (byte~) main::$1 ← (byte) sum::return#1
// SCREEN[1] = sum('a', 12)
// [12] *((const nomodify byte*) SCREEN+(byte) 1) ← (byte~) main::$1 -- _deref_pbuc1=vbuaa
sta SCREEN+1
// main::@return
// }
// [13] return
rts
}
// sum
// Definition of sum()
// sum(byte register(X) a, byte register(A) b)
sum: {
// a+b
// [15] (byte) sum::return#2 ← (byte) sum::a#2 + (byte) sum::b#2 -- vbuaa=vbuxx_plus_vbuaa
stx.z $ff
clc
adc.z $ff
// sum::@return
// }
// [16] return
rts
}
// File Data

View File

@ -0,0 +1,28 @@
(label) @1
(label) @begin
(label) @end
(const nomodify byte*) SCREEN = (byte*) 1024
(void()) main()
(byte~) main::$0 reg byte a 22.0
(byte~) main::$1 reg byte a 22.0
(label) main::@1
(label) main::@2
(label) main::@return
(byte()) sum((byte) sum::a , (byte) sum::b)
(label) sum::@return
(byte) sum::a
(byte) sum::a#2 reg byte x 101.0
(byte) sum::b
(byte) sum::b#2 reg byte a 101.0
(byte) sum::return
(byte) sum::return#0 reg byte a 22.0
(byte) sum::return#1 reg byte a 22.0
(byte) sum::return#2 reg byte a 30.75
reg byte x [ sum::a#2 ]
reg byte a [ sum::b#2 ]
reg byte a [ sum::return#0 ]
reg byte a [ main::$0 ]
reg byte a [ sum::return#1 ]
reg byte a [ main::$1 ]
reg byte a [ sum::return#2 ]