1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-10-04 20:55:00 +00:00

#666 working on memory model for intermediate vars

This commit is contained in:
Jesper Gravgaard 2021-06-06 12:19:05 +02:00
parent fcb23cbf59
commit 843baa6fb2
23 changed files with 128 additions and 19982 deletions

View File

@ -7,18 +7,9 @@
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
<package name="java.util" withSubpackages="false" static="false" />
<package name="kotlinx.android.synthetic" withSubpackages="true" static="false" />
<package name="io.ktor" withSubpackages="true" static="false" />
</value>
</option>
</JetCodeStyleSettings>

View File

@ -24,7 +24,7 @@
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="kickc" target="11" />
<module name="kickc" target="8" />
</bytecodeTargetLevel>
</component>
</project>

View File

@ -43,7 +43,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="14" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,9 @@ public class VariableBuilder {
/** The scope of the variable. */
private Scope scope;
/** The variable is an intermediate variable. */
private boolean isIntermediate;
/** The variable is a function parameter declaration. */
private boolean isParameter;
@ -38,9 +41,10 @@ public class VariableBuilder {
/** Configuration of how to setup optimization/memory area for variables. */
private VariableBuilderConfig config;
public VariableBuilder(String varName, Scope scope, boolean isParameter, SymbolType type, List<Directive> directives, String dataSegment, VariableBuilderConfig config) {
public VariableBuilder(String varName, Scope scope, boolean isParameter, boolean isIntermediate, SymbolType type, List<Directive> directives, String dataSegment, VariableBuilderConfig config) {
this.varName = varName;
this.scope = scope;
this.isIntermediate = isIntermediate;
this.isParameter = isParameter;
this.type = type;
this.directives = directives;
@ -48,6 +52,18 @@ public class VariableBuilder {
this.config = config;
}
/**
* Create a variable builder for an intermediate variable
* @param scope The scope to create the intermediate variable in
* @param type The variable type
* @param config The variable builder config
* @return The new intermediate variable
*/
public static Variable createIntermediate(Scope scope, SymbolType type, Program program) {
VariableBuilder builder = new VariableBuilder(scope.allocateIntermediateVariableName(), scope, false, true, type, null, scope.getSegmentData(), program.getTargetPlatform().getVariableBuilderConfig());
return builder.build();
}
/**
* Build the variable with the properties derived from type, scope, directives and configuration.
*
@ -166,7 +182,16 @@ public class VariableBuilder {
}
/**
* Is the variable an array declaration
* Is the variable an intermediate variable
*
* @return true if the variable is intermediate
*/
public boolean isIntermediate() {
return isIntermediate;
}
/**
* Is the variable an array declaration
*
* @return true if the variable is an array declaration
*/
@ -280,7 +305,9 @@ public class VariableBuilder {
* @return The variable kind
*/
public Variable.Kind getKind() {
if(isConstant())
if(isIntermediate()) {
return Variable.Kind.INTERMEDIATE;
} else if(isConstant())
return Variable.Kind.CONSTANT;
else if(isSingleStaticAssignment())
return Variable.Kind.PHI_MASTER;

View File

@ -2,6 +2,8 @@ package dk.camelot64.kickc.model.iterator;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.InternalError;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.operators.OperatorBinary;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.Statement;
@ -48,14 +50,14 @@ public interface ProgramExpressionBinary extends ProgramExpression {
*
* @param toType The toType to cast to
*/
void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols);
void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program);
/**
* Adds a cast to the right operand
*
* @param toType The toType to cast to
*/
void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols);
void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program);
/**
* Get the left operand as a replaceable program value
@ -124,13 +126,12 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(assignment.getrValue1() instanceof ConstantValue) {
assignment.setrValue1(new ConstantCastValue(toType, (ConstantValue) assignment.getrValue1()));
} else {
Scope blockScope = symbols.getScope(currentScope);
Variable tmpVar = blockScope.addVariableIntermediate();
tmpVar.setType(toType);
Scope blockScope = program.getScope().getScope(currentScope);
Variable tmpVar = VariableBuilder.createIntermediate(blockScope, toType, program);
StatementAssignment newAssignment = new StatementAssignment((LValue) tmpVar.getRef(), Operators.getCastUnary(toType), assignment.getrValue1(), true, assignment.getSource(), Comment.NO_COMMENTS);
assignment.setrValue1(tmpVar.getRef());
stmtIt.previous();
@ -140,13 +141,12 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(assignment.getrValue2() instanceof ConstantValue) {
assignment.setrValue2(new ConstantCastValue(toType, (ConstantValue) assignment.getrValue2()));
} else {
Scope blockScope = symbols.getScope(currentScope);
Variable tmpVar = blockScope.addVariableIntermediate();
tmpVar.setType(toType);
Scope blockScope = program.getScope().getScope(currentScope);
Variable tmpVar = VariableBuilder.createIntermediate(blockScope, toType, program);
StatementAssignment newAssignment = new StatementAssignment((LValue) tmpVar.getRef(), Operators.getCastUnary(toType), assignment.getrValue2(), true, assignment.getSource(), Comment.NO_COMMENTS);
assignment.setrValue2(tmpVar.getRef());
stmtIt.previous();
@ -202,12 +202,12 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
throw new InternalError("Casting parameter variable not allowed. " + parameterDef.toString());
}
@Override
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
Value value = parameterValue.get();
if(value instanceof ConstantValue) {
parameterValue.set(new ConstantCastValue(toType, (ConstantValue) value));
@ -270,18 +270,17 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(assignment.getlValue() instanceof VariableRef) {
Variable variable = symbols.getVariable((VariableRef) assignment.getlValue());
Variable variable = program.getScope().getVariable((VariableRef) assignment.getlValue());
if(variable.isKindIntermediate())
variable.setType(toType);
else
throw new InternalError("Cannot cast declared type!" + variable.toString());
} else {
Scope blockScope = symbols.getScope(currentScope);
Variable tmpVar = blockScope.addVariableIntermediate();
SymbolType rightType = SymbolTypeInference.inferType(symbols, getRight());
tmpVar.setType(rightType);
Scope blockScope = program.getScope().getScope(currentScope);
SymbolType rightType = SymbolTypeInference.inferType(program.getScope(), getRight());
Variable tmpVar = VariableBuilder.createIntermediate(blockScope, rightType, program);
StatementAssignment newAssignment = new StatementAssignment(assignment.getlValue(), Operators.getCastUnary(toType), tmpVar.getRef(), assignment.isInitialAssignment(), assignment.getSource(), Comment.NO_COMMENTS);
assignment.setlValue((LValue) tmpVar.getRef());
stmtIt.add(newAssignment);
@ -289,14 +288,13 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(assignment.getrValue1() == null && assignment.getOperator() == null) {
assignment.setOperator(Operators.getCastUnary(toType));
} else {
Scope blockScope = symbols.getScope(currentScope);
Variable tmpVar = blockScope.addVariableIntermediate();
SymbolType rightType = SymbolTypeInference.inferType(symbols, getRight());
tmpVar.setType(rightType);
Scope blockScope = program.getScope().getScope(currentScope);
SymbolType rightType = SymbolTypeInference.inferType(program.getScope(), getRight());
Variable tmpVar = VariableBuilder.createIntermediate(blockScope, rightType, program);
StatementAssignment newAssignment = new StatementAssignment(assignment.getlValue(), Operators.getCastUnary(toType), tmpVar.getRef(), assignment.isInitialAssignment(), assignment.getSource(), Comment.NO_COMMENTS);
assignment.setlValue((LValue) tmpVar.getRef());
stmtIt.add(newAssignment);
@ -351,7 +349,7 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(conditionalJump.getrValue1() instanceof ConstantValue) {
conditionalJump.setrValue1(new ConstantCastValue(toType, (ConstantValue) conditionalJump.getrValue1()));
} else {
@ -360,7 +358,7 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(conditionalJump.getrValue2() instanceof ConstantValue) {
conditionalJump.setrValue2(new ConstantCastValue(toType, (ConstantValue) conditionalJump.getrValue2()));
} else {
@ -420,12 +418,12 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
getConstantBinary().setLeft(new ConstantCastValue(toType, getConstantBinary().getLeft()));
}
@Override
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
getConstantBinary().setRight(new ConstantCastValue(toType, getConstantBinary().getRight()));
}
}
@ -479,7 +477,7 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(getPointerDereferenceIndexed().getPointer() instanceof ConstantValue) {
getPointerDereferenceIndexed().setPointer(new ConstantCastValue(toType, (ConstantValue) getPointerDereferenceIndexed().getPointer()));
} else {
@ -489,11 +487,11 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(getPointerDereferenceIndexed().getIndex() instanceof ConstantValue) {
getPointerDereferenceIndexed().setIndex(new ConstantCastValue(toType, (ConstantValue) getPointerDereferenceIndexed().getIndex()));
} else if(getPointerDereferenceIndexed().getIndex() instanceof VariableRef) {
Variable variable = symbols.getVariable((VariableRef) getPointerDereferenceIndexed().getIndex());
Variable variable = program.getScope().getVariable((VariableRef) getPointerDereferenceIndexed().getIndex());
if(variable.isKindIntermediate())
variable.setType(toType);
else
@ -554,8 +552,8 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
Variable variable = symbols.getVariable(phiVariable.getVariable());
public void addLeftCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
Variable variable = program.getScope().getVariable(phiVariable.getVariable());
if(variable.isKindIntermediate())
variable.setType(toType);
else
@ -563,9 +561,9 @@ public interface ProgramExpressionBinary extends ProgramExpression {
}
@Override
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, ProgramScope symbols) {
public void addRightCast(SymbolType toType, ListIterator<Statement> stmtIt, ScopeRef currentScope, Program program) {
if(getRight() instanceof VariableRef) {
Variable variable = symbols.getVariable((VariableRef) getRight());
Variable variable = program.getScope().getVariable((VariableRef) getRight());
if(variable.isKindIntermediate())
variable.setType(toType);
} else if(getRight() instanceof ConstantValue) {

View File

@ -111,11 +111,6 @@ public abstract class Scope implements Symbol, Serializable {
return symbol;
}
public Variable addVariableIntermediate() {
String name = allocateIntermediateVariableName();
return add(Variable.createIntermediate(name, this, getSegmentData()));
}
public Label addLabel(String name) {
return add(new Label(name, this, false));
}

View File

@ -128,18 +128,6 @@ public class Variable implements Symbol {
setFullName();
}
/**
* Create an intermediate variable. The type will initially be set to {@link SymbolType#VAR}.
*
* @param name The name
* @param scope The scope
* @param dataSegment The data segment (in main memory)
* @return The new intermediate variable
*/
public static Variable createIntermediate(String name, Scope scope, String dataSegment) {
return new Variable(name, Kind.INTERMEDIATE, SymbolType.VAR, scope, MemoryArea.ZEROPAGE_MEMORY, dataSegment, null);
}
/**
* Create a version of a PHI master variable
*

View File

@ -95,7 +95,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
if(currentScope == null || ScopeRef.ROOT.equals(currentScope.getRef())) {
currentScope = getInitProc();
}
return currentScope.addVariableIntermediate();
return VariableBuilder.createIntermediate(currentScope, SymbolType.VAR, program);
}
/**
@ -492,14 +492,14 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
if(parameter.name==null)
throw new CompileError("Illegal unnamed parameter.", statementSource);
VariableBuilder varBuilder = new VariableBuilder(parameter.name, getCurrentScope(), true, parameter.type, null, currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
VariableBuilder varBuilder = new VariableBuilder(parameter.name, getCurrentScope(), true, false, parameter.type, null, currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
final Variable paramVar = varBuilder.build();
parameterList.add(paramVar);
}
procedure.setParameters(parameterList);
// Add return variable
if(!SymbolType.VOID.equals(procedure.getReturnType())) {
final VariableBuilder builder = new VariableBuilder("return", procedure, false, procedure.getReturnType(), varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
final VariableBuilder builder = new VariableBuilder("return", procedure, false, false, procedure.getReturnType(), varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
builder.build();
}
// exit the procedure
@ -966,7 +966,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
final List<Directive> effectiveDirectives = varDecl.getDeclDirectives();
final List<Comment> declComments = varDecl.getDeclComments();
varDecl.exitVar();
VariableBuilder varBuilder = new VariableBuilder(varName, getCurrentScope(), false, effectiveType, effectiveDirectives, currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
VariableBuilder varBuilder = new VariableBuilder(varName, getCurrentScope(), false, false, effectiveType, effectiveDirectives, currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
Variable variable = varBuilder.build();
if(isStructMember && (initializer != null))
throw new CompileError("Initializer not supported inside structs " + effectiveType.getTypeName(), declSource);
@ -1052,7 +1052,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
ConstantArrayKickAsm constantArrayKickAsm = new ConstantArrayKickAsm(((SymbolTypePointer) varDecl.getEffectiveType()).getElementType(), kasm.kickAsmCode, kasm.uses, ((SymbolTypePointer) effectiveType).getArraySpec().getArraySize());
// Add a constant variable
Scope scope = getCurrentScope();
VariableBuilder varBuilder = new VariableBuilder(varName, scope, false, varDecl.getEffectiveType(), varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
VariableBuilder varBuilder = new VariableBuilder(varName, scope, false, false, varDecl.getEffectiveType(), varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
Variable variable = varBuilder.build();
// Set constant value
variable.setInitValue(getConstInitValue(constantArrayKickAsm, null, statementSource));
@ -1591,7 +1591,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
String varName = varDecl.getVarName();
Variable lValue;
if(varType != null) {
VariableBuilder varBuilder = new VariableBuilder(varName, blockScope, false, varType, varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
VariableBuilder varBuilder = new VariableBuilder(varName, blockScope, false, false, varType, varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
lValue = varBuilder.build();
} else {
lValue = getCurrentScope().findVariable(varName);
@ -2123,7 +2123,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
this.visit(ctx.declType());
this.visit(ctx.declarator());
String typedefName = varDecl.getVarName();
VariableBuilder varBuilder = new VariableBuilder(typedefName, getCurrentScope(), false, varDecl.getEffectiveType(), varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
VariableBuilder varBuilder = new VariableBuilder(typedefName, getCurrentScope(), false, false, varDecl.getEffectiveType(), varDecl.getDeclDirectives(), currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig());
varBuilder.build();
scopeStack.pop();
varDecl.exitType();

View File

@ -2,6 +2,7 @@ package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.operators.Operator;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.Statement;
@ -10,10 +11,9 @@ import dk.camelot64.kickc.model.statements.StatementLValue;
import dk.camelot64.kickc.model.symbols.ProgramScope;
import dk.camelot64.kickc.model.symbols.Scope;
import dk.camelot64.kickc.model.symbols.Variable;
import dk.camelot64.kickc.model.values.LValue;
import dk.camelot64.kickc.model.values.LvalueIntermediate;
import dk.camelot64.kickc.model.values.SymbolVariableRef;
import dk.camelot64.kickc.model.values.VariableRef;
import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.types.SymbolTypeInference;
import dk.camelot64.kickc.model.values.*;
import dk.camelot64.kickc.passes.utils.VarAssignments;
import java.util.ArrayList;
@ -82,10 +82,10 @@ public class Pass1FixLValuesLoHi extends Pass1Base {
Variable intermediateVar = programScope.getVariable(intermediate.getVariable());
Scope currentScope = intermediateVar.getScope();
// Let assignment put value into a tmp Var
Variable tmpVar = currentScope.addVariableIntermediate();
SymbolType type = SymbolTypeInference.inferType(programScope, new AssignmentRValue(intermediateAssignment));
Variable tmpVar = VariableBuilder.createIntermediate(currentScope, type, getProgram());
SymbolVariableRef tmpVarRef = tmpVar.getRef();
statementLValue.setlValue((LValue) tmpVarRef);
PassNTypeInference.updateInferedTypeLValue(getProgram(), statementLValue);
// Insert an extra "set low" assignment statement
Statement setLoHiAssignment = new StatementAssignment(loHiVar, loHiVar, loHiOperator, tmpVarRef, true, statementLValue.getSource(), new ArrayList<>());
statementsIt.add(setLoHiAssignment);

View File

@ -1,13 +1,11 @@
package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.CompileError;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.*;
import dk.camelot64.kickc.model.iterator.ProgramValueIterator;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementAssignment;
import dk.camelot64.kickc.model.symbols.Scope;
import dk.camelot64.kickc.model.symbols.StructDefinition;
import dk.camelot64.kickc.model.symbols.Symbol;
import dk.camelot64.kickc.model.symbols.Variable;
@ -71,8 +69,9 @@ public class Pass1PointerSizeofFix extends Pass1Base {
getLog().append("Fixing pointer array-indexing " + deref.toString(getProgram()));
SymbolVariableRef idx2VarRef = handled.getOrDefault(currentStmt, new LinkedHashMap<>()).get(deref.getIndex());
if(idx2VarRef == null) {
Variable idx2Var = getScope().getScope(currentBlock.getScope()).addVariableIntermediate();
idx2Var.setType(SymbolTypeInference.inferType(getScope(), deref.getIndex()));
SymbolType type = SymbolTypeInference.inferType(getScope(), deref.getIndex());
Scope scope = getScope().getScope(currentBlock.getScope());
Variable idx2Var = VariableBuilder.createIntermediate(scope, type, getProgram());
ConstantRef sizeOfTargetType = SizeOfConstants.getSizeOfConstantVar(getProgram().getScope(), pointerType.getElementType());
StatementAssignment idx2 = new StatementAssignment((LValue) idx2Var.getRef(), deref.getIndex(), Operators.MULTIPLY, sizeOfTargetType, true, currentStmt.getSource(), Comment.NO_COMMENTS);
stmtIt.previous();
@ -115,8 +114,9 @@ public class Pass1PointerSizeofFix extends Pass1Base {
if(getLog().isVerboseParse())
getLog().append("Fixing pointer addition " + assignment.toString(getProgram(), false));
LValue lValue = assignment.getlValue();
Variable tmpVar = getScope().getScope(block.getScope()).addVariableIntermediate();
tmpVar.setType(SymbolTypeInference.inferType(getScope(), assignment.getlValue()));
Scope scope = getScope().getScope(block.getScope());
SymbolType type = SymbolTypeInference.inferType(getScope(), assignment.getlValue());
Variable tmpVar = VariableBuilder.createIntermediate(scope, type, getProgram());
assignment.setlValue((LValue) tmpVar.getRef());
ConstantRef sizeOfTargetType = SizeOfConstants.getSizeOfConstantVar(getProgram().getScope(), pointerType.getElementType());
stmtIt.add(new StatementAssignment(lValue, tmpVar.getRef(), Operators.DIVIDE, sizeOfTargetType, assignment.isInitialAssignment(), assignment.getSource(), Comment.NO_COMMENTS));
@ -128,8 +128,9 @@ public class Pass1PointerSizeofFix extends Pass1Base {
// Adding to a pointer - multiply by sizeof()
if(getLog().isVerboseParse())
getLog().append("Fixing pointer addition " + assignment.toString(getProgram(), false));
Variable tmpVar = getScope().getScope(block.getScope()).addVariableIntermediate();
tmpVar.setType(SymbolTypeInference.inferType(getScope(), assignment.getrValue2()));
Scope scope = getScope().getScope(block.getScope());
SymbolType type = SymbolTypeInference.inferType(getScope(), assignment.getrValue2());
Variable tmpVar = VariableBuilder.createIntermediate(scope, type, getProgram());
stmtIt.remove();
ConstantRef sizeOfTargetType = SizeOfConstants.getSizeOfConstantVar(getProgram().getScope(), pointerType.getElementType());
stmtIt.add(new StatementAssignment((LValue) tmpVar.getRef(), assignment.getrValue2(), Operators.MULTIPLY, sizeOfTargetType, true, assignment.getSource(), Comment.NO_COMMENTS));

View File

@ -1,16 +1,12 @@
package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableReferenceInfos;
import dk.camelot64.kickc.model.*;
import dk.camelot64.kickc.model.iterator.ProgramValue;
import dk.camelot64.kickc.model.iterator.ProgramValueIterator;
import dk.camelot64.kickc.model.operators.Operator;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementAssignment;
import dk.camelot64.kickc.model.statements.StatementConditionalJump;
import dk.camelot64.kickc.model.StatementInfos;
import dk.camelot64.kickc.model.symbols.Scope;
import dk.camelot64.kickc.model.symbols.Variable;
import dk.camelot64.kickc.model.types.SymbolType;
@ -134,9 +130,8 @@ public class Pass2ConditionalJumpSimplification extends Pass2SsaOptimization {
final ControlFlowBlock conditionDefineBlock = statementInfos.getBlock(simpleCondition.conditionAssignment);
final ScopeRef conditionDefineScopeRef = conditionDefineBlock.getScope();
final Scope conditionDefineScope = getScope().getScope(conditionDefineScopeRef);
final Variable intermediateLoadStoreVar = conditionDefineScope.addVariableIntermediate();
SymbolType typeQualified = referencedLoadStoreVariable.getType().getQualified(false, referencedLoadStoreVariable.getType().isNomodify());
intermediateLoadStoreVar.setType(typeQualified);
final Variable intermediateLoadStoreVar = VariableBuilder.createIntermediate(conditionDefineScope, typeQualified, getProgram());
final StatementAssignment intermediateLoadStoreAssignment = new StatementAssignment(intermediateLoadStoreVar.getVariableRef(), referencedLoadStoreVariable.getRef(), true, simpleCondition.conditionAssignment.getSource(), Comment.NO_COMMENTS);
conditionDefineBlock.addStatementAfter(intermediateLoadStoreAssignment, simpleCondition.conditionAssignment);
// Replace all references to the load/store variable in the expressions with the new intermediate

View File

@ -2,6 +2,7 @@ package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.iterator.ProgramValueIterator;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.StatementAssignment;
@ -33,14 +34,13 @@ public class Pass2DeInlineWordDerefIdx extends Pass2SsaOptimization {
// Index is multiple bytes - de-inline it
getLog().append("De-inlining pointer[w] to *(pointer+w) "+currentStmt.toString(getProgram(), false));
Scope currentScope = getScope().getScope(currentBlock.getScope());
Variable tmpVar = currentScope.addVariableIntermediate();
SymbolType pointerType = SymbolTypeInference.inferType(getScope(), dereferenceIndexed.getPointer());
Variable tmpVar = VariableBuilder.createIntermediate(currentScope, pointerType, getProgram());
stmtIt.previous();
StatementAssignment tmpVarAssignment = new StatementAssignment((LValue) tmpVar.getRef(), dereferenceIndexed.getPointer(), Operators.PLUS, indexValue, true, currentStmt.getSource(), Comment.NO_COMMENTS);
stmtIt.add(tmpVarAssignment);
stmtIt.next();
programValue.set(new PointerDereferenceSimple(tmpVar.getRef()));
SymbolType pointerType = SymbolTypeInference.inferType(getScope(), new AssignmentRValue(tmpVarAssignment));
tmpVar.setType(pointerType);
optimized.set(true);
}
}

View File

@ -3,6 +3,7 @@ package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.iterator.ProgramExpression;
import dk.camelot64.kickc.model.iterator.ProgramExpressionIterator;
import dk.camelot64.kickc.model.iterator.ProgramExpressionUnary;
@ -82,8 +83,7 @@ public class Pass2FixInlineConstructors extends Pass2SsaOptimization {
public void addLiteralWordConstructor(OperatorBinary constructOperator, SymbolType castType, SymbolType constructType, SymbolType subType, ProgramExpression programExpression, List<RValue> listValues, Statement currentStmt, ListIterator<Statement> stmtIt, ControlFlowBlock currentBlock) {
// Convert list to a word constructor in a new tmp variable
Scope currentScope = Pass2FixInlineConstructors.this.getScope().getScope(currentBlock.getScope());
Variable tmpVar = currentScope.addVariableIntermediate();
tmpVar.setType(constructType);
Variable tmpVar = VariableBuilder.createIntermediate(currentScope, constructType, getProgram());
// Move backward - to insert before the current statement
stmtIt.previous();
// Add assignment of the new tmpVar

View File

@ -1,9 +1,6 @@
package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.ConstantNotLiteral;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.*;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementAssignment;
@ -71,13 +68,11 @@ public class Pass2MultiplyToShiftRewriting extends Pass2SsaOptimization {
long powVal = 1L <<pow2;
if(remains>=powVal) {
// First add shifts
Variable varShift = scope.addVariableIntermediate();
varShift.setType(resultType);
Variable varShift = VariableBuilder.createIntermediate(scope, resultType, getProgram());
stmtIt.add(new StatementAssignment((LValue) varShift.getRef(), building, Operators.SHIFT_LEFT, new ConstantInteger(shiftCount, SymbolType.BYTE), true, assignment.getSource(), Comment.NO_COMMENTS));
shiftCount = 0;
// Then add rvalue1
Variable varAdd = scope.addVariableIntermediate();
varAdd.setType(resultType);
Variable varAdd = VariableBuilder.createIntermediate(scope, resultType, getProgram());
stmtIt.add(new StatementAssignment((LValue) varAdd.getRef(), varShift.getRef(), Operators.PLUS, assignment.getrValue1(), true, assignment.getSource(), Comment.NO_COMMENTS));
building = varAdd.getRef();
remains -= powVal;
@ -90,8 +85,7 @@ public class Pass2MultiplyToShiftRewriting extends Pass2SsaOptimization {
}
// add remaining shifts
if(shiftCount>0) {
Variable varShift = scope.addVariableIntermediate();
varShift.setType(resultType);
Variable varShift = VariableBuilder.createIntermediate(scope, resultType, getProgram());
stmtIt.add(new StatementAssignment((LValue) varShift.getRef(), building, Operators.SHIFT_LEFT, new ConstantInteger(shiftCount, SymbolType.BYTE), true, assignment.getSource(), Comment.NO_COMMENTS));
building = varShift.getRef();
}

View File

@ -49,14 +49,15 @@ public class Pass3PhiLifting {
//VariableRef rValVarRef = (VariableRef) phiRValue.getrValue();
Variable newVar;
if(phiVariable.getVariable().isVersion()) {
Symbol phiLValue = programScope.getSymbol(phiVariable.getVariable());
Variable lValVar = program.getScope().getVariable(phiVariable.getVariable());
newVar = lValVar.getPhiMaster().createVersion();
newVar.setType(phiLValue.getType());
} else {
Symbol phiLValue = programScope.getSymbol(phiVariable.getVariable());
Variable lValVar = program.getScope().getVariable(phiVariable.getVariable());
newVar = lValVar.getScope().addVariableIntermediate();
newVar = VariableBuilder.createIntermediate(lValVar.getScope(), lValVar.getType(), program);
}
Symbol phiLValue = programScope.getSymbol(phiVariable.getVariable());
newVar.setType(phiLValue.getType());
List<Statement> predecessorStatements = predecessorBlock.getStatements();
Statement lastPredecessorStatement = null;
if(predecessorStatements.size() > 0) {

View File

@ -3,6 +3,7 @@ package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.iterator.ProgramExpressionIterator;
import dk.camelot64.kickc.model.iterator.ProgramExpressionUnary;
import dk.camelot64.kickc.model.operators.Operators;
@ -76,8 +77,7 @@ public class PassNAddBooleanCasts extends Pass2SsaOptimization {
private Variable addBooleanCast(RValue rValue, SymbolType rValueType, Statement currentStmt, ListIterator<Statement> stmtIt, ControlFlowBlock currentBlock) {
Scope currentScope = getScope().getScope(currentBlock.getScope());
stmtIt.previous();
Variable tmpVar = currentScope.addVariableIntermediate();
tmpVar.setType(SymbolType.BOOLEAN);
Variable tmpVar = VariableBuilder.createIntermediate(currentScope, SymbolType.BOOLEAN, getProgram());
// Go straight to xxx!=0 instead of casting to bool
ConstantValue nullValue;
if(rValueType instanceof SymbolTypePointer) {

View File

@ -33,13 +33,13 @@ public class PassNAddNumberTypeConversions extends Pass2SsaOptimization {
SymbolType leftType = SymbolTypeInference.inferType(getProgram().getScope(), left);
if(SymbolType.NUMBER.equals(leftType)) {
getLog().append("Adding number conversion cast (" + castType + ") " + binary.getLeft().toString() + " in " + (currentStmt==null?"":currentStmt.toString(getProgram(), false)));
binary.addLeftCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getScope());
binary.addLeftCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getProgram());
modified.set(true);
}
SymbolType rightType = SymbolTypeInference.inferType(getProgram().getScope(), right);
if(SymbolType.NUMBER.equals(rightType)) {
getLog().append("Adding number conversion cast (" + castType + ") " + binary.getRight().toString() + " in " + ((currentStmt==null)?"":currentStmt.toString(getProgram(), false)));
binary.addRightCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getScope());
binary.addRightCast(castType, stmtIt, currentBlock==null?null:currentBlock.getScope(), getProgram());
modified.set(true);
}

View File

@ -49,33 +49,33 @@ public class PassNAddTypeConversionAssignment extends Pass2SsaOptimization {
if((leftType instanceof SymbolTypePointer) && rightType instanceof SymbolTypePointer && SymbolType.VOID.equals(((SymbolTypePointer) leftType).getElementType())) {
if(!pass1 || getLog().isVerbosePass1CreateSsa())
getLog().append("Adding void pointer type conversion cast (" + leftType + ") " + binary.getRight().toString() + " in " + currentStmt.toString(getProgram(), false));
binary.addRightCast(leftType, stmtIt, currentBlock.getScope(), getScope());
binary.addRightCast(leftType, stmtIt, currentBlock.getScope(), getProgram());
modified.set(true);
} else if((leftType instanceof SymbolTypePointer) && rightType instanceof SymbolTypePointer && SymbolType.VOID.equals(((SymbolTypePointer) rightType).getElementType())) {
if(!pass1 || getLog().isVerbosePass1CreateSsa())
getLog().append("Adding pointer type conversion cast to void pointer (" + leftType + ") " + binary.getRight().toString() + " in " + currentStmt.toString(getProgram(), false));
binary.addRightCast(leftType, stmtIt, currentBlock.getScope(), getScope());
binary.addRightCast(leftType, stmtIt, currentBlock.getScope(), getProgram());
modified.set(true);
} else if(SymbolType.WORD.equals(leftType) && isLiteralWordCandidate(right)) {
// Detect word literal constructor
SymbolType conversionType = SymbolType.WORD;
if(!pass1 || getLog().isVerbosePass1CreateSsa())
getLog().append("Identified literal word (" + conversionType + ") " + binary.getRight().toString() + " in " + (currentStmt == null ? "" : currentStmt.toString(getProgram(), false)));
binary.addRightCast(conversionType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getScope());
binary.addRightCast(conversionType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getProgram());
modified.set(true);
} else if(leftType instanceof SymbolTypePointer && isLiteralWordCandidate(right)) {
// Detect word literal constructor
SymbolType conversionType = SymbolType.WORD;
if(!pass1 || getLog().isVerbosePass1CreateSsa())
getLog().append("Identified literal word (" + conversionType + ") " + binary.getRight().toString() + " in " + (currentStmt == null ? "" : currentStmt.toString(getProgram(), false)));
binary.addRightCast(conversionType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getScope());
binary.addRightCast(conversionType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getProgram());
modified.set(true);
} else if(SymbolType.DWORD.equals(leftType) && isLiteralWordCandidate(right)) {
// Detect dword literal constructor
SymbolType conversionType = SymbolType.DWORD;
if(!pass1 || getLog().isVerbosePass1CreateSsa())
getLog().append("Identified literal word (" + conversionType + ") " + binary.getRight().toString() + " in " + (currentStmt == null ? "" : currentStmt.toString(getProgram(), false)));
binary.addRightCast(conversionType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getScope());
binary.addRightCast(conversionType, stmtIt, currentBlock == null ? null : currentBlock.getScope(), getProgram());
modified.set(true);
}
}

View File

@ -3,6 +3,7 @@ package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Comment;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.iterator.ProgramExpressionBinary;
import dk.camelot64.kickc.model.iterator.ProgramExpressionIterator;
import dk.camelot64.kickc.model.iterator.ProgramValue;
@ -11,6 +12,7 @@ import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementAssignment;
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.values.CastValue;
import java.util.ListIterator;
@ -50,8 +52,8 @@ public class PassNDeInlineCastValues extends Pass2SsaOptimization {
if(!pass1)
getLog().append("De-inlining cast " + castValue.toString());
final Scope scope = getScope().getScope(currentBlock.getScope());
final Variable tmpVar = scope.addVariableIntermediate();
tmpVar.setType(castValue.getToType());
SymbolType toType = castValue.getToType();
final Variable tmpVar = VariableBuilder.createIntermediate(scope, toType, getProgram());
castProgramValue.set(tmpVar.getRef());
stmtIt.previous();
stmtIt.add(new StatementAssignment(tmpVar.getVariableRef(), castValue, true, currentStmt.getSource(), Comment.NO_COMMENTS));

View File

@ -3,6 +3,7 @@ package dk.camelot64.kickc.passes.unwinding;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.InternalError;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementAssignment;
@ -85,9 +86,7 @@ public class ValueSourcePointerDereferenceIndexed extends ValueSourceBase {
return new ValueSourceConstant(new SymbolTypePointer(elementType), elmPointer);
} else {
// Unwind to (elmType*)&struct + OFFSET_MEMBER + idx
Scope scope = programScope.getScope(currentBlock.getScope());
Variable elementAddress = scope.addVariableIntermediate();
elementAddress.setType(new SymbolTypePointer(elementType));
Variable elementAddress = VariableBuilder.createIntermediate(programScope.getScope(currentBlock.getScope()), new SymbolTypePointer(elementType), program);
stmtIt.previous();
stmtIt.add(new StatementAssignment((LValue) elementAddress.getRef(), memberPointer, Operators.PLUS, pointerDereferenceIndexed.getIndex(), true, currentStmt.getSource(), currentStmt.getComments()));
stmtIt.next();
@ -111,9 +110,7 @@ public class ValueSourcePointerDereferenceIndexed extends ValueSourceBase {
return new ValueSourcePointerDereferenceIndexed(memberDeref, memberType, null);
} else {
// Unwind to ((type*)&struct + idx)[OFFSET_MEMBER]
Scope scope = programScope.getScope(currentBlock.getScope());
Variable idxAddress = scope.addVariableIntermediate();
idxAddress.setType(new SymbolTypePointer(memberType));
Variable idxAddress = VariableBuilder.createIntermediate(programScope.getScope(currentBlock.getScope()), new SymbolTypePointer(memberType), program);
stmtIt.previous();
stmtIt.add(new StatementAssignment((LValue) idxAddress.getRef(), structTypedPointer, Operators.PLUS, pointerDereferenceIndexed.getIndex(), true, currentStmt.getSource(), currentStmt.getComments()));
stmtIt.next();
@ -125,8 +122,8 @@ public class ValueSourcePointerDereferenceIndexed extends ValueSourceBase {
if(memberArraySpec != null)
throw new InternalError("Not implemented!");
Scope scope = programScope.getScope(currentBlock.getScope());
Variable memberAddress = scope.addVariableIntermediate();
memberAddress.setType(new SymbolTypePointer(memberType));
SymbolTypePointer type = new SymbolTypePointer(memberType);
Variable memberAddress = VariableBuilder.createIntermediate(scope, type, program);
CastValue structTypedPointer = new CastValue(new SymbolTypePointer(memberType), structPointer);
// Add statement $1 = (memberType*)ptr_struct + OFFSET_MEMBER
stmtIt.previous();

View File

@ -2,6 +2,7 @@ package dk.camelot64.kickc.passes.unwinding;
import dk.camelot64.kickc.model.ControlFlowBlock;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.VariableBuilder;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementAssignment;
@ -92,8 +93,7 @@ public class ValueSourcePointerDereferenceSimple extends ValueSourceBase {
SymbolType elementType = ((SymbolTypePointer) memberType).getElementType();
SymbolTypePointer pointerToElementType = new SymbolTypePointer(elementType);
Scope scope = programScope.getScope(currentBlock.getScope());
Variable memberAddress = scope.addVariableIntermediate();
memberAddress.setType(pointerToElementType);
Variable memberAddress = VariableBuilder.createIntermediate(scope, pointerToElementType, program);
CastValue elementTypedPointer = new CastValue(pointerToElementType, structPointer);
// Add statement $1 = (elmType*)ptr_struct + OFFSET_MEMBER
stmtIt.previous();
@ -103,8 +103,7 @@ public class ValueSourcePointerDereferenceSimple extends ValueSourceBase {
return new ValueSourceVariable(memberAddress);
} else {
Scope scope = programScope.getScope(currentBlock.getScope());
Variable memberAddress = scope.addVariableIntermediate();
memberAddress.setType(new SymbolTypePointer(memberType));
Variable memberAddress = VariableBuilder.createIntermediate(scope, new SymbolTypePointer(memberType), program);
CastValue structTypedPointer = new CastValue(new SymbolTypePointer(memberType), structPointer);
// Add statement $1 = (memberType*)ptr_struct + OFFSET_MEMBER
stmtIt.previous();

View File

@ -174,7 +174,7 @@ public class Unroller {
Scope scope = origVar.getScope();
SymbolVariableRef newVarRef;
if(origVarRef.isIntermediate()) {
newVarRef = scope.addVariableIntermediate().getRef();
newVarRef = VariableBuilder.createIntermediate(scope, origVar.getType(), program).getRef();
} else {
newVarRef = (origVar).getPhiMaster().createVersion().getRef();
}