mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-01-10 12:31:09 +00:00
Proper handling of post modifiers
This commit is contained in:
parent
1dfbcd1ca4
commit
1743e4f9d0
@ -2,6 +2,7 @@ package dk.camelot64.kickc.icl;
|
|||||||
|
|
||||||
import dk.camelot64.kickc.parser.KickCBaseVisitor;
|
import dk.camelot64.kickc.parser.KickCBaseVisitor;
|
||||||
import dk.camelot64.kickc.parser.KickCParser;
|
import dk.camelot64.kickc.parser.KickCParser;
|
||||||
|
import org.antlr.v4.runtime.ParserRuleContext;
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -61,7 +62,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitStmtBlock(KickCParser.StmtBlockContext ctx) {
|
public Void visitStmtBlock(KickCParser.StmtBlockContext ctx) {
|
||||||
if(ctx.stmtSeq()!=null) {
|
if (ctx.stmtSeq() != null) {
|
||||||
this.visit(ctx.stmtSeq());
|
this.visit(ctx.stmtSeq());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -70,12 +71,14 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
@Override
|
@Override
|
||||||
public Void visitStmtExpr(KickCParser.StmtExprContext ctx) {
|
public Void visitStmtExpr(KickCParser.StmtExprContext ctx) {
|
||||||
this.visit(ctx.expr());
|
this.visit(ctx.expr());
|
||||||
|
PostModifierHandler.addPostModifiers(this, ctx.expr());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitStmtIfElse(KickCParser.StmtIfElseContext ctx) {
|
public Void visitStmtIfElse(KickCParser.StmtIfElseContext ctx) {
|
||||||
RValue rValue = (RValue) this.visit(ctx.expr());
|
RValue rValue = (RValue) this.visit(ctx.expr());
|
||||||
|
PostModifierHandler.addPostModifiers(this, ctx.expr());
|
||||||
Label ifJumpLabel = getCurrentSymbols().addLabelIntermediate();
|
Label ifJumpLabel = getCurrentSymbols().addLabelIntermediate();
|
||||||
Label elseJumpLabel = getCurrentSymbols().addLabelIntermediate();
|
Label elseJumpLabel = getCurrentSymbols().addLabelIntermediate();
|
||||||
Statement ifJmpStmt = new StatementConditionalJump(rValue, ifJumpLabel);
|
Statement ifJmpStmt = new StatementConditionalJump(rValue, ifJumpLabel);
|
||||||
@ -110,6 +113,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
StatementLabel beginJumpTarget = new StatementLabel(beginJumpLabel);
|
StatementLabel beginJumpTarget = new StatementLabel(beginJumpLabel);
|
||||||
sequence.addStatement(beginJumpTarget);
|
sequence.addStatement(beginJumpTarget);
|
||||||
RValue rValue = (RValue) this.visit(ctx.expr());
|
RValue rValue = (RValue) this.visit(ctx.expr());
|
||||||
|
PostModifierHandler.addPostModifiers(this, ctx.expr());
|
||||||
Statement doJmpStmt = new StatementConditionalJump(rValue, doJumpLabel);
|
Statement doJmpStmt = new StatementConditionalJump(rValue, doJumpLabel);
|
||||||
sequence.addStatement(doJmpStmt);
|
sequence.addStatement(doJmpStmt);
|
||||||
Statement endJmpStmt = new StatementJump(endJumpLabel);
|
Statement endJmpStmt = new StatementJump(endJumpLabel);
|
||||||
@ -129,10 +133,11 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
Label beginJumpLabel = getCurrentSymbols().addLabelIntermediate();
|
Label beginJumpLabel = getCurrentSymbols().addLabelIntermediate();
|
||||||
StatementLabel beginJumpTarget = new StatementLabel(beginJumpLabel);
|
StatementLabel beginJumpTarget = new StatementLabel(beginJumpLabel);
|
||||||
sequence.addStatement(beginJumpTarget);
|
sequence.addStatement(beginJumpTarget);
|
||||||
if(ctx.stmt()!=null) {
|
if (ctx.stmt() != null) {
|
||||||
this.visit(ctx.stmt());
|
this.visit(ctx.stmt());
|
||||||
}
|
}
|
||||||
RValue rValue = (RValue) this.visit(ctx.expr());
|
RValue rValue = (RValue) this.visit(ctx.expr());
|
||||||
|
PostModifierHandler.addPostModifiers(this, ctx.expr());
|
||||||
Statement doJmpStmt = new StatementConditionalJump(rValue, beginJumpLabel);
|
Statement doJmpStmt = new StatementConditionalJump(rValue, beginJumpLabel);
|
||||||
sequence.addStatement(doJmpStmt);
|
sequence.addStatement(doJmpStmt);
|
||||||
return null;
|
return null;
|
||||||
@ -155,11 +160,11 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
}
|
}
|
||||||
procedure.setParameters(parameterList);
|
procedure.setParameters(parameterList);
|
||||||
sequence.addStatement(new StatementProcedureBegin(procedure));
|
sequence.addStatement(new StatementProcedureBegin(procedure));
|
||||||
if(ctx.stmtSeq()!=null) {
|
if (ctx.stmtSeq() != null) {
|
||||||
this.visit(ctx.stmtSeq());
|
this.visit(ctx.stmtSeq());
|
||||||
}
|
}
|
||||||
sequence.addStatement(new StatementLabel(procExit));
|
sequence.addStatement(new StatementLabel(procExit));
|
||||||
if(returnVar!=null) {
|
if (returnVar != null) {
|
||||||
sequence.addStatement(new StatementAssignment(returnVar, returnVar));
|
sequence.addStatement(new StatementAssignment(returnVar, returnVar));
|
||||||
}
|
}
|
||||||
sequence.addStatement(new StatementReturn(returnVar));
|
sequence.addStatement(new StatementReturn(returnVar));
|
||||||
@ -194,6 +199,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
rValue = (RValue) this.visit(exprCtx);
|
rValue = (RValue) this.visit(exprCtx);
|
||||||
Variable returnVar = procedure.getVariable("return");
|
Variable returnVar = procedure.getVariable("return");
|
||||||
sequence.addStatement(new StatementAssignment(returnVar, rValue));
|
sequence.addStatement(new StatementAssignment(returnVar, rValue));
|
||||||
|
PostModifierHandler.addPostModifiers(this, exprCtx);
|
||||||
}
|
}
|
||||||
Label returnLabel = procedure.getLabel("@return");
|
Label returnLabel = procedure.getLabel("@return");
|
||||||
sequence.addStatement(new StatementJump(returnLabel));
|
sequence.addStatement(new StatementJump(returnLabel));
|
||||||
@ -211,6 +217,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
RValue rValue = (RValue) visit(ctx.initializer());
|
RValue rValue = (RValue) visit(ctx.initializer());
|
||||||
Statement stmt = new StatementAssignment(lValue, rValue);
|
Statement stmt = new StatementAssignment(lValue, rValue);
|
||||||
sequence.addStatement(stmt);
|
sequence.addStatement(stmt);
|
||||||
|
PostModifierHandler.addPostModifiers(this, ctx.initializer());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -221,6 +228,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
RValue rValue = (RValue) this.visit(ctx.expr());
|
RValue rValue = (RValue) this.visit(ctx.expr());
|
||||||
Statement stmt = new StatementAssignment(lValue, rValue);
|
Statement stmt = new StatementAssignment(lValue, rValue);
|
||||||
sequence.addStatement(stmt);
|
sequence.addStatement(stmt);
|
||||||
|
PostModifierHandler.addPostModifiers(this, ctx);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,8 +301,8 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
public Object visitExprCall(KickCParser.ExprCallContext ctx) {
|
public Object visitExprCall(KickCParser.ExprCallContext ctx) {
|
||||||
List<RValue> parameters;
|
List<RValue> parameters;
|
||||||
KickCParser.ParameterListContext parameterList = ctx.parameterList();
|
KickCParser.ParameterListContext parameterList = ctx.parameterList();
|
||||||
if(parameterList!=null) {
|
if (parameterList != null) {
|
||||||
parameters = (List<RValue>) this.visit(parameterList);
|
parameters = (List<RValue>) this.visit(parameterList);
|
||||||
} else {
|
} else {
|
||||||
parameters = new ArrayList<>();
|
parameters = new ArrayList<>();
|
||||||
}
|
}
|
||||||
@ -371,14 +379,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
@Override
|
@Override
|
||||||
public Object visitExprPostMod(KickCParser.ExprPostModContext ctx) {
|
public Object visitExprPostMod(KickCParser.ExprPostModContext ctx) {
|
||||||
RValue child = (RValue) this.visit(ctx.expr());
|
RValue child = (RValue) this.visit(ctx.expr());
|
||||||
String op = ((TerminalNode) ctx.getChild(1)).getSymbol().getText();
|
return child;
|
||||||
Operator operator = new Operator(op);
|
|
||||||
VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate();
|
|
||||||
Statement stmt1 = new StatementAssignment(tmpVar, child);
|
|
||||||
sequence.addStatement(stmt1);
|
|
||||||
Statement stmt2 = new StatementAssignment((LValue) child, operator, child);
|
|
||||||
sequence.addStatement(stmt2);
|
|
||||||
return tmpVar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -395,4 +396,50 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
return sequence;
|
return sequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class PostModifierHandler extends KickCBaseVisitor<Void> {
|
||||||
|
|
||||||
|
private List<PostMod> postMods;
|
||||||
|
private Pass1GenerateStatementSequence mainParser;
|
||||||
|
|
||||||
|
public PostModifierHandler(Pass1GenerateStatementSequence mainParser) {
|
||||||
|
this.mainParser = mainParser;
|
||||||
|
postMods = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PostMod> getPostMods() {
|
||||||
|
return postMods;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addPostModifiers(Pass1GenerateStatementSequence parser, ParserRuleContext ctx) {
|
||||||
|
PostModifierHandler postModifierHandler = new PostModifierHandler(parser);
|
||||||
|
postModifierHandler.visit(ctx);
|
||||||
|
List<PostMod> postMods = postModifierHandler.getPostMods();
|
||||||
|
for (PostMod mod : postMods) {
|
||||||
|
Statement stmt = new StatementAssignment((LValue) mod.child, mod.operator, mod.child);
|
||||||
|
parser.sequence.addStatement(stmt);
|
||||||
|
System.out.println("Adding postmod "+stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitExprPostMod(KickCParser.ExprPostModContext ctx) {
|
||||||
|
RValue child = (RValue) mainParser.visit(ctx.expr());
|
||||||
|
String op = ((TerminalNode) ctx.getChild(1)).getSymbol().getText();
|
||||||
|
Operator operator = new Operator(op);
|
||||||
|
PostMod postMod = new PostMod(child, operator);
|
||||||
|
postMods.add(postMod);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class PostMod {
|
||||||
|
public RValue child;
|
||||||
|
public Operator operator;
|
||||||
|
|
||||||
|
public PostMod(RValue child, Operator operator) {
|
||||||
|
this.child = child;
|
||||||
|
this.operator = operator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,6 +131,7 @@ public class Pass3RegisterAllocation {
|
|||||||
allocation.allocate(symbols.getVariable("flip::i#2"), RegisterAllocation.getRegisterX());
|
allocation.allocate(symbols.getVariable("flip::i#2"), RegisterAllocation.getRegisterX());
|
||||||
allocation.allocate(symbols.getVariable("flip::$0"), RegisterAllocation.getRegisterA());
|
allocation.allocate(symbols.getVariable("flip::$0"), RegisterAllocation.getRegisterA());
|
||||||
allocation.allocate(symbols.getVariable("flip::$8"), RegisterAllocation.getRegisterA());
|
allocation.allocate(symbols.getVariable("flip::$8"), RegisterAllocation.getRegisterA());
|
||||||
|
allocation.allocate(symbols.getVariable("flip::$4"), RegisterAllocation.getRegisterA());
|
||||||
allocation.allocate(symbols.getVariable("$1"), RegisterAllocation.getRegisterA());
|
allocation.allocate(symbols.getVariable("$1"), RegisterAllocation.getRegisterA());
|
||||||
allocation.allocate(symbols.getVariable("$3"), RegisterAllocation.getRegisterA());
|
allocation.allocate(symbols.getVariable("$3"), RegisterAllocation.getRegisterA());
|
||||||
allocation.allocate(symbols.getVariable("c#0"), RegisterAllocation.getRegisterX());
|
allocation.allocate(symbols.getVariable("c#0"), RegisterAllocation.getRegisterX());
|
||||||
|
@ -33,8 +33,7 @@ void flip() {
|
|||||||
do {
|
do {
|
||||||
byte c = 16;
|
byte c = 16;
|
||||||
do {
|
do {
|
||||||
buffer2[dstIdx] = buffer[srcIdx];
|
buffer2[dstIdx] = buffer[srcIdx++];
|
||||||
srcIdx++;
|
|
||||||
dstIdx = dstIdx+16;
|
dstIdx = dstIdx+16;
|
||||||
c--;
|
c--;
|
||||||
} while(c!=0)
|
} while(c!=0)
|
||||||
@ -56,9 +55,8 @@ void plot() {
|
|||||||
do {
|
do {
|
||||||
byte x=0;
|
byte x=0;
|
||||||
do {
|
do {
|
||||||
line[x] = buffer[i];
|
line[x] = buffer[i++];
|
||||||
x++;
|
x++;
|
||||||
i++;
|
|
||||||
} while(x<16)
|
} while(x<16)
|
||||||
line = line+40;
|
line = line+40;
|
||||||
y--;
|
y--;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user