1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-20 02:32:36 +00:00

Removed isConstant from Variable (now using Kind).

This commit is contained in:
jespergravgaard 2019-11-23 18:44:43 +01:00
parent 363a6c4424
commit 4d9c23b6bc
13 changed files with 34 additions and 55 deletions

View File

@ -85,7 +85,7 @@ public class AsmFragmentInstance {
} else {
throw new RuntimeException("Register Type not implemented " + register);
}
} else if(boundValue instanceof Variable && ((Variable) boundValue).isConstant()) {
} else if(boundValue instanceof Variable && ((Variable) boundValue).isKindConstant()) {
Variable constantVar = (Variable) boundValue;
String constantValueAsm = AsmFormat.getAsmConstant(program, constantVar.getConstantRef(), 99, codeScopeRef);
boolean constantValueZp = SymbolType.BYTE.equals(constantVar.getType());

View File

@ -506,7 +506,7 @@ public class AsmFragmentInstanceSpecFactory {
// If the constant is already bound - reuse the index
for(String boundName : bindings.keySet()) {
Value boundValue = bindings.get(boundName);
if(boundValue instanceof ConstantValue || (boundValue instanceof Variable && ((Variable) boundValue).isConstant())) {
if(boundValue instanceof ConstantValue || (boundValue instanceof Variable && ((Variable) boundValue).isKindConstant())) {
if(boundValue.equals(constant)) {
return "c" + boundName.substring(boundName.length() - 1);
}

View File

@ -79,12 +79,12 @@ public class AsmFragmentTemplate {
ProgramScope scope = new ProgramScope();
LinkedHashMap<String, Value> bindings = new LinkedHashMap<>();
{
Variable v1 = new Variable(false, "z1", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v2 = new Variable(false, "z2", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v3 = new Variable(false, "z3", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v4 = new Variable(false, "z4", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v5 = new Variable(false, "z5", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v6 = new Variable(false, "z6", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v1 = new Variable( "z1", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v2 = new Variable( "z2", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v3 = new Variable( "z3", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v4 = new Variable( "z4", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v5 = new Variable( "z5", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
Variable v6 = new Variable( "z6", scope, SymbolType.BYTE, Variable.Kind.PHI_VERSION, Variable.MemoryArea.ZEROPAGE_MEMORY, null);
v1.setAllocation(new Registers.RegisterZpMem(2, 1));
v2.setAllocation(new Registers.RegisterZpMem(4, 1));
v3.setAllocation(new Registers.RegisterZpMem(6, 1));
@ -99,12 +99,12 @@ public class AsmFragmentTemplate {
if(signature.contains("z6")) bindings.put("z6", v6);
}
{
Variable v1 = new Variable(false, "m1", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v2 = new Variable(false, "m2", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v3 = new Variable(false, "m3", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v4 = new Variable(false, "m4", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v5 = new Variable(false, "m5", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v6 = new Variable(false, "m6", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v1 = new Variable( "m1", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v2 = new Variable( "m2", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v3 = new Variable( "m3", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v4 = new Variable( "m4", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v5 = new Variable( "m5", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
Variable v6 = new Variable( "m6", scope, SymbolType.BYTE, Variable.Kind.LOAD_STORE, Variable.MemoryArea.MAIN_MEMORY, null);
v1.setAllocation(new Registers.RegisterMainMem(v1.getVariableRef(), 1, null));
v2.setAllocation(new Registers.RegisterMainMem(v2.getVariableRef(), 1, null));
v3.setAllocation(new Registers.RegisterMainMem(v3.getVariableRef(), 1, null));

View File

@ -48,7 +48,7 @@ public class ProgramValueIterator {
* @param programValueHandler The programValueHandler to execute
*/
public static void execute(Variable variable, ProgramValueHandler programValueHandler) {
if(variable.isConstant()) {
if(variable.isKindConstant()) {
execute(new ProgramValue.ProgramValueConstantVar(variable), programValueHandler, null, null, null);
}
if(variable.isArray()) {

View File

@ -101,16 +101,16 @@ public abstract class Scope implements Symbol, Serializable {
}
public Variable addVariable(Variable.Kind kind, String name, SymbolType type, Variable.MemoryArea memoryArea, String dataSegment) {
return add(new Variable( false, name, this, type, kind, memoryArea, dataSegment));
return add(new Variable( name, this, type, kind, memoryArea, dataSegment));
}
public Variable addVariablePhiMaster(String name, SymbolType type, Variable.MemoryArea memoryArea, String dataSegment) {
return add(new Variable( false, name, this, type, Variable.Kind.PHI_MASTER, memoryArea, dataSegment));
return add(new Variable( name, this, type, Variable.Kind.PHI_MASTER, memoryArea, dataSegment));
}
public Variable addVariableIntermediate() {
String name = allocateIntermediateVariableName();
return add(new Variable( false, name, this, SymbolType.VAR, Variable.Kind.INTERMEDIATE, Variable.MemoryArea.ZEROPAGE_MEMORY, getSegmentData()));
return add(new Variable( name, this, SymbolType.VAR, Variable.Kind.INTERMEDIATE, Variable.MemoryArea.ZEROPAGE_MEMORY, getSegmentData()));
}
/**
@ -168,8 +168,7 @@ public abstract class Scope implements Symbol, Serializable {
}
public Variable getVar(String name) {
Variable symbol = (Variable) getSymbol(name);
return symbol;
return (Variable) getSymbol(name);
}
public Variable getVar(SymbolVariableRef variableRef) {
@ -188,7 +187,7 @@ public abstract class Scope implements Symbol, Serializable {
public Variable getConstant(String name) {
Variable symbol = (Variable) getSymbol(name);
if(symbol!=null && !symbol.isConstant()) throw new InternalError("Symbol is not a constant! "+symbol.toString());
if(symbol!=null && !symbol.isKindConstant()) throw new InternalError("Symbol is not a constant! "+symbol.toString());
return symbol;
}
@ -236,7 +235,7 @@ public abstract class Scope implements Symbol, Serializable {
public Collection<Variable> getAllConstants(boolean includeSubScopes) {
Collection<Variable> vars = new ArrayList<>();
getAllVars(includeSubScopes).stream().
filter(Variable::isConstant).
filter(variable -> variable.isKindConstant()).
forEach(vars::add);
return vars;
}
@ -396,7 +395,7 @@ public abstract class Scope implements Symbol, Serializable {
}
}
}
if(symVar.isConstant() && symVar.getConstantValue()!=null) {
if(symVar.isKindConstant() && symVar.getConstantValue()!=null) {
res.append(" = " + symVar.getConstantValue().toString(program));
}
res.append("\n");

View File

@ -36,9 +36,6 @@ public class Variable implements Symbol {
/** The kind of the variable. */
private Kind kind;
/** True of the variable is a compile-time constant (previously ConstantVar) [ALL] TODO: Eliminate (use Kind.CONSTANT) */
private boolean isConstant;
/** Non-null if teh variable is an array. */
private ArraySpec arraySpec;
@ -111,7 +108,6 @@ public class Variable implements Symbol {
*/
public Variable(String name, Scope scope, SymbolType type, ArraySpec arraySpec, String dataSegment, ConstantValue value) {
this.kind = Kind.CONSTANT;
this.isConstant = true;
this.name = name;
this.scope = scope;
this.type = type;
@ -132,8 +128,7 @@ public class Variable implements Symbol {
* @param memoryArea The memory area (zeropage/main memory)
* @param dataSegment The data segment (in main memory)
*/
public Variable(boolean isConstant, String name, Scope scope, SymbolType type, Kind kind, MemoryArea memoryArea, String dataSegment) {
this.isConstant = isConstant;
public Variable(String name, Scope scope, SymbolType type, Kind kind, MemoryArea memoryArea, String dataSegment) {
this.name = name;
this.scope = scope;
this.type = type;
@ -144,8 +139,6 @@ public class Variable implements Symbol {
this.nextPhiVersionNumber = 0;
this.comments = new ArrayList<>();
setFullName();
//if(isKindConstant()!=isConstant)
// throw new RuntimeException("CONST ISSUE!");
}
@ -156,7 +149,7 @@ public class Variable implements Symbol {
* @param version The version number
*/
public Variable(Variable phiMaster, int version) {
this(false, phiMaster.getName() + "#" + version, phiMaster.getScope(), phiMaster.getType(), Kind.PHI_VERSION, phiMaster.getMemoryArea(), phiMaster.getDataSegment());
this(phiMaster.getName() + "#" + version, phiMaster.getScope(), phiMaster.getType(), Kind.PHI_VERSION, phiMaster.getMemoryArea(), phiMaster.getDataSegment());
this.setArraySpec(phiMaster.getArraySpec());
this.setDeclaredAlignment(phiMaster.getDeclaredAlignment());
this.setDeclaredAsRegister(phiMaster.isDeclaredAsRegister());
@ -176,7 +169,7 @@ public class Variable implements Symbol {
* @param original The original variable
*/
public Variable(String name, Scope scope, Variable original) {
this(original.isConstant(), name, scope, original.getType(), original.getKind(), original.getMemoryArea(), original.getDataSegment());
this(name, scope, original.getType(), original.getKind(), original.getMemoryArea(), original.getDataSegment());
this.setArraySpec(original.getArraySpec());
this.setDeclaredAlignment(original.getDeclaredAlignment());
this.setDeclaredAsRegister(original.isDeclaredAsRegister());
@ -193,10 +186,6 @@ public class Variable implements Symbol {
return kind;
}
public void setKind(Kind kind) {
this.kind = kind;
}
public boolean isKindConstant() {
return Kind.CONSTANT.equals(getKind());
}
@ -217,26 +206,17 @@ public class Variable implements Symbol {
return Kind.INTERMEDIATE.equals(getKind());
}
/**
* True if the variable is a compile time constant. (Previously this was ConstantVar)
*
* @return True if the variable is a compile time constant.
*/
public boolean isConstant() {
return isConstant;
}
/**
* True if the variable is a not compile time constant. (Previously this was Variable)
*
* @return True if the variable is not a compile-time constant
*/
public boolean isVariable() {
return !isConstant();
return !isKindConstant();
}
public SymbolVariableRef getRef() {
if(isConstant())
if(isKindConstant())
return new ConstantRef(this);
else
return new VariableRef(this);

View File

@ -10,7 +10,7 @@ public class ConstantRef extends SymbolVariableRef implements ConstantValue {
public ConstantRef(Variable constantVar) {
super(constantVar.getFullName());
if(!constantVar.isConstant())
if(!constantVar.isKindConstant())
throw new InternalError("VariableRef not allowed for non-constant "+constantVar.toString());
}

View File

@ -267,7 +267,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
this.visitDeclTypes(ctx.declTypes());
SymbolType type = declVarType;
List<Directive> directives = declVarDirectives;
Variable param = new Variable(false, ctx.NAME().getText(), getCurrentScope(), type, Variable.Kind.PHI_MASTER, defaultMemoryArea, currentDataSegment);
Variable param = new Variable( ctx.NAME().getText(), getCurrentScope(), type, Variable.Kind.PHI_MASTER, defaultMemoryArea, currentDataSegment);
// Add directives
directiveContext.applyDirectives(param, true, false, directives, new StatementSource(ctx));
exitDeclTypes();

View File

@ -70,7 +70,7 @@ public class Pass1UnwindBlockScopes extends Pass1Base {
}
} else if(symbol instanceof Variable) {
Variable variable = (Variable) symbol;
if(variable.isConstant()) {
if(variable.isKindConstant()) {
String name = findLocalName(procedure, symbol);
Variable unwound = new Variable(name, procedure, (Variable) symbol);
procedure.add(unwound);

View File

@ -209,7 +209,7 @@ public class Pass1UnwindStructValues extends Pass1Base {
for(Variable member : structDefinition.getAllVars(false)) {
String name = variable.getLocalName() + "_" + member.getLocalName();
Variable.MemoryArea memoryArea = (member.getType() instanceof SymbolTypePointer)?Variable.MemoryArea.ZEROPAGE_MEMORY:variable.getMemoryArea();
Variable memberVariable = scope.add(new Variable( false, name, scope, member.getType(), variable.getKind(), memoryArea, variable.getDataSegment()));
Variable memberVariable = scope.add(new Variable( name, scope, member.getType(), variable.getKind(), memoryArea, variable.getDataSegment()));
memberVariable.setArraySpec(member.getArraySpec());
memberVariable.setDeclaredVolatile(variable.isDeclaredVolatile());
memberVariable.setInferredVolatile(variable.isInferredVolatile());

View File

@ -46,7 +46,7 @@ public class Pass2AssertSymbols extends Pass2SsaAssertion {
if(tableSymbol instanceof Variable && ((Variable) tableSymbol).isKindPhiMaster()) continue;
if(tableSymbol instanceof Variable && ((Variable) tableSymbol).isKindConstant()) continue;
if(tableSymbol instanceof Variable && ((Variable) tableSymbol).isKindLoadStore()) continue;
if(tableSymbol instanceof Variable && (((Variable) tableSymbol).isConstant()) ) continue;
if(tableSymbol instanceof Variable && (((Variable) tableSymbol).isKindConstant()) ) continue;
if(tableSymbol instanceof StructDefinition) continue;
if(tableSymbol instanceof EnumDefinition) continue;
if(tableSymbol instanceof TypeDefsScope) continue;

View File

@ -214,7 +214,7 @@ public class Pass2ConstantIdentification extends Pass2SsaOptimization {
public static ConstantValue getConstant(RValue rValue) {
if(rValue instanceof ConstantValue) {
return (ConstantValue) rValue;
} else if(rValue instanceof Variable && ((Variable) rValue).isConstant()) {
} else if(rValue instanceof Variable && ((Variable) rValue).isKindConstant()) {
Variable constantVar = (Variable) rValue;
return constantVar.getConstantRef();
} else if(rValue instanceof CastValue) {

View File

@ -147,7 +147,7 @@ public class Pass2ConstantInlining extends Pass2SsaOptimization {
aliases.put(constant.getConstantRef(), value);
getLog().append("Inlining constant with var siblings " + constant);
break;
} else if(symbol instanceof Variable && ((Variable) symbol).isConstant()) {
} else if(symbol instanceof Variable && ((Variable) symbol).isKindConstant()) {
ConstantValue otherValue = ((Variable) symbol).getConstantValue();
if(!otherValue.equals(value) && !(value instanceof ConstantString) && !(value instanceof ConstantArray) && !(otherValue instanceof ConstantRef)) {
aliases.put(constant.getConstantRef(), value);