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

Moved all ref creation to getRef()

This commit is contained in:
jespergravgaard 2017-07-20 10:02:25 +02:00
parent de88808c5f
commit 00463cd72f
6 changed files with 26 additions and 21 deletions

View File

@ -62,7 +62,7 @@ public class ControlFlowBlock {
} }
public void addPhiStatement(VariableVersion newVersion) { public void addPhiStatement(VariableVersion newVersion) {
statements.add(0, new StatementPhi(new VariableRef(newVersion))); statements.add(0, new StatementPhi(newVersion.getRef()));
} }
public String getAsTypedString(ControlFlowGraph graph, ProgramScope scope) { public String getAsTypedString(ControlFlowGraph graph, ProgramScope scope) {

View File

@ -29,11 +29,11 @@ public class StatementAssignment implements StatementLValue {
} }
public StatementAssignment(Variable lValue, Variable rValue2) { public StatementAssignment(Variable lValue, Variable rValue2) {
this(new VariableRef(lValue), new VariableRef(rValue2)); this(lValue.getRef(), rValue2.getRef());
} }
public StatementAssignment(Variable lValue, RValue rValue2) { public StatementAssignment(Variable lValue, RValue rValue2) {
this(new VariableRef(lValue), rValue2); this(lValue.getRef(), rValue2);
} }
@JsonCreator @JsonCreator

View File

@ -134,4 +134,8 @@ public abstract class Variable implements Symbol {
return getTypedName(); return getTypedName();
} }
@JsonIgnore
public VariableRef getRef() {
return new VariableRef(this);
}
} }

View File

@ -49,7 +49,7 @@ public class Pass1GenerateSingleStaticAssignmentForm {
// Assignment to a non-versioned non-intermediary variable // Assignment to a non-versioned non-intermediary variable
VariableUnversioned assignedSymbol = (VariableUnversioned) assignedVar; VariableUnversioned assignedSymbol = (VariableUnversioned) assignedVar;
VariableVersion version = assignedSymbol.createVersion(); VariableVersion version = assignedSymbol.createVersion();
statementLValue.setlValue(new VariableRef(version)); statementLValue.setlValue(version.getRef());
} }
} }
} }
@ -72,7 +72,7 @@ public class Pass1GenerateSingleStaticAssignmentForm {
{ {
VariableVersion version = findOrCreateVersion(statementReturn.getValue(), blockVersions, blockNewPhis); VariableVersion version = findOrCreateVersion(statementReturn.getValue(), blockVersions, blockNewPhis);
if (version != null) { if (version != null) {
statementReturn.setValue(new VariableRef(version)); statementReturn.setValue(version.getRef());
} }
} }
@ -82,13 +82,13 @@ public class Pass1GenerateSingleStaticAssignmentForm {
{ {
VariableVersion version = findOrCreateVersion(assignment.getrValue1(), blockVersions, blockNewPhis); VariableVersion version = findOrCreateVersion(assignment.getrValue1(), blockVersions, blockNewPhis);
if (version != null) { if (version != null) {
assignment.setrValue1(new VariableRef(version)); assignment.setrValue1(version.getRef());
} }
} }
{ {
VariableVersion version = findOrCreateVersion(assignment.getrValue2(), blockVersions, blockNewPhis); VariableVersion version = findOrCreateVersion(assignment.getrValue2(), blockVersions, blockNewPhis);
if (version != null) { if (version != null) {
assignment.setrValue2(new VariableRef(version)); assignment.setrValue2(version.getRef());
} }
} }
// Update map of versions encountered in the block // Update map of versions encountered in the block
@ -105,19 +105,19 @@ public class Pass1GenerateSingleStaticAssignmentForm {
RValue pointer = deref.getPointer(); RValue pointer = deref.getPointer();
VariableVersion version = findOrCreateVersion(pointer, blockVersions, blockNewPhis); VariableVersion version = findOrCreateVersion(pointer, blockVersions, blockNewPhis);
if (version != null) { if (version != null) {
deref.setPointer(new VariableRef(version)); deref.setPointer(version.getRef());
} }
} else if(lValue instanceof PointerDereferenceIndexed) { } else if(lValue instanceof PointerDereferenceIndexed) {
PointerDereferenceIndexed deref = (PointerDereferenceIndexed) lValue; PointerDereferenceIndexed deref = (PointerDereferenceIndexed) lValue;
RValue pointer = deref.getPointer(); RValue pointer = deref.getPointer();
VariableVersion version = findOrCreateVersion(pointer, blockVersions, blockNewPhis); VariableVersion version = findOrCreateVersion(pointer, blockVersions, blockNewPhis);
if (version != null) { if (version != null) {
deref.setPointer(new VariableRef(version)); deref.setPointer(version.getRef());
} }
RValue index = deref.getIndex(); RValue index = deref.getIndex();
VariableVersion iVersion = findOrCreateVersion(index, blockVersions, blockNewPhis); VariableVersion iVersion = findOrCreateVersion(index, blockVersions, blockNewPhis);
if (iVersion != null) { if (iVersion != null) {
deref.setIndex(new VariableRef(iVersion)); deref.setIndex(iVersion.getRef());
} }
} }
} }
@ -201,7 +201,7 @@ public class Pass1GenerateSingleStaticAssignmentForm {
predecessorNewPhis.put(unversioned, previousSymbol); predecessorNewPhis.put(unversioned, previousSymbol);
} }
} }
phi.addPreviousVersion(predecessorLabel, new VariableRef(previousSymbol)); phi.addPreviousVersion(predecessorLabel, previousSymbol.getRef());
} }
} }
} }

View File

@ -177,7 +177,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
sequence.addStatement(new StatementAssignment(returnVar, returnVar)); sequence.addStatement(new StatementAssignment(returnVar, returnVar));
} }
VariableRef returnVarRef = null; VariableRef returnVarRef = null;
if(returnVar!=null) {new VariableRef(returnVar); } if(returnVar!=null) {returnVarRef = returnVar.getRef();}
sequence.addStatement(new StatementReturn(returnVarRef)); sequence.addStatement(new StatementReturn(returnVarRef));
scopeStack.pop(); scopeStack.pop();
sequence.addStatement(new StatementProcedureEnd(procedure.getRef())); sequence.addStatement(new StatementProcedureEnd(procedure.getRef()));
@ -249,7 +249,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override @Override
public LValue visitLvalueName(KickCParser.LvalueNameContext ctx) { public LValue visitLvalueName(KickCParser.LvalueNameContext ctx) {
Variable variable = getCurrentSymbols().getVariable(ctx.NAME().getText()); Variable variable = getCurrentSymbols().getVariable(ctx.NAME().getText());
return new VariableRef(variable); return variable.getRef();
} }
@Override @Override
@ -322,8 +322,9 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
parameters = new ArrayList<>(); parameters = new ArrayList<>();
} }
VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate(); VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate();
sequence.addStatement(new StatementCall(new VariableRef(tmpVar), ctx.NAME().getText(), parameters)); VariableRef tmpVarRef = tmpVar.getRef();
return tmpVar; sequence.addStatement(new StatementCall(tmpVarRef, ctx.NAME().getText(), parameters));
return tmpVarRef;
} }
@Override @Override
@ -342,7 +343,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
RValue index = (RValue) visit(ctx.expr(1)); RValue index = (RValue) visit(ctx.expr(1));
Operator operator = new Operator("*idx"); Operator operator = new Operator("*idx");
VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate(); VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate();
VariableRef tmpVarRef = new VariableRef(tmpVar); VariableRef tmpVarRef = tmpVar.getRef();
Statement stmt = new StatementAssignment(tmpVarRef, array, operator, index); Statement stmt = new StatementAssignment(tmpVarRef, array, operator, index);
sequence.addStatement(stmt); sequence.addStatement(stmt);
return tmpVarRef; return tmpVarRef;
@ -376,7 +377,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
String op = ((TerminalNode) ctx.getChild(1)).getSymbol().getText(); String op = ((TerminalNode) ctx.getChild(1)).getSymbol().getText();
Operator operator = new Operator(op); Operator operator = new Operator(op);
VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate(); VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate();
VariableRef tmpVarRef = new VariableRef(tmpVar); VariableRef tmpVarRef = tmpVar.getRef();
Statement stmt = new StatementAssignment(tmpVarRef, left, operator, right); Statement stmt = new StatementAssignment(tmpVarRef, left, operator, right);
sequence.addStatement(stmt); sequence.addStatement(stmt);
return tmpVarRef; return tmpVarRef;
@ -388,7 +389,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
String op = ((TerminalNode) ctx.getChild(0)).getSymbol().getText(); String op = ((TerminalNode) ctx.getChild(0)).getSymbol().getText();
Operator operator = new Operator(op); Operator operator = new Operator(op);
VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate(); VariableIntermediate tmpVar = getCurrentSymbols().addVariableIntermediate();
VariableRef tmpVarRef = new VariableRef(tmpVar); VariableRef tmpVarRef = tmpVar.getRef();
Statement stmt = new StatementAssignment(tmpVarRef, operator, child); Statement stmt = new StatementAssignment(tmpVarRef, operator, child);
sequence.addStatement(stmt); sequence.addStatement(stmt);
return tmpVarRef; return tmpVarRef;
@ -414,7 +415,7 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override @Override
public RValue visitExprId(KickCParser.ExprIdContext ctx) { public RValue visitExprId(KickCParser.ExprIdContext ctx) {
Variable variable = getCurrentSymbols().getVariable(ctx.NAME().getText()); Variable variable = getCurrentSymbols().getVariable(ctx.NAME().getText());
return new VariableRef(variable); return variable.getRef();
} }
public StatementSequence getSequence() { public StatementSequence getSequence() {

View File

@ -31,13 +31,13 @@ public class Pass1ProcedureCallParameters extends ControlFlowGraphCopyVisitor {
for (int i = 0; i < parameterDecls.size(); i++) { for (int i = 0; i < parameterDecls.size(); i++) {
Variable parameterDecl = parameterDecls.get(i); Variable parameterDecl = parameterDecls.get(i);
RValue parameterValue = parameterValues.get(i); RValue parameterValue = parameterValues.get(i);
addStatementToCurrentBlock(new StatementAssignment(new VariableRef(parameterDecl), parameterValue)); addStatementToCurrentBlock(new StatementAssignment(parameterDecl.getRef(), parameterValue));
} }
String procedureName = origCall.getProcedureName(); String procedureName = origCall.getProcedureName();
Variable procReturnVar = procedure.getVariable("return"); Variable procReturnVar = procedure.getVariable("return");
VariableRef procReturnVarRef = null; VariableRef procReturnVarRef = null;
if (procReturnVar != null) { if (procReturnVar != null) {
procReturnVarRef = new VariableRef(procReturnVar); procReturnVarRef = procReturnVar.getRef();
} }
StatementCall copyCall = new StatementCall(procReturnVarRef, procedureName, null); StatementCall copyCall = new StatementCall(procReturnVarRef, procedureName, null);
copyCall.setParametersByAssignment(true); copyCall.setParametersByAssignment(true);