diff --git a/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java b/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java index c8adaad41..1a812d61f 100644 --- a/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java +++ b/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java @@ -43,7 +43,7 @@ public class CallingConventionStack { if(returnOffsetConstant == null) { // Constant not found - create it long returnByteOffset = getReturnByteOffset(procedure); - returnOffsetConstant = Variable.createConstant(OFFSET_STACK_RETURN, SymbolType.BYTE, procedure, null, new ConstantInteger(returnByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); + returnOffsetConstant = Variable.createConstant(OFFSET_STACK_RETURN, SymbolType.BYTE, procedure, new ConstantInteger(returnByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); procedure.add(returnOffsetConstant); } return returnOffsetConstant.getConstantRef(); @@ -62,7 +62,7 @@ public class CallingConventionStack { if(paramOffsetConstant == null) { // Constant not found - create it long paramByteOffset = getParameterByteOffset(procedure, parameter); - paramOffsetConstant = Variable.createConstant(paramOffsetConstantName, SymbolType.BYTE, procedure, null, new ConstantInteger(paramByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); + paramOffsetConstant = Variable.createConstant(paramOffsetConstantName, SymbolType.BYTE, procedure, new ConstantInteger(paramByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); procedure.add(paramOffsetConstant); } return paramOffsetConstant.getConstantRef(); @@ -137,7 +137,7 @@ public class CallingConventionStack { */ public static ConstantRef getStackBaseConstant(ProgramScope programScope) { long STACK_BASE_ADDRESS = 0x103L; - Variable stackBase = Variable.createConstant("STACK_BASE", SymbolType.WORD, programScope, null, new ConstantInteger(STACK_BASE_ADDRESS, SymbolType.WORD), Scope.SEGMENT_DATA_DEFAULT); + Variable stackBase = Variable.createConstant("STACK_BASE", SymbolType.WORD, programScope, new ConstantInteger(STACK_BASE_ADDRESS, SymbolType.WORD), Scope.SEGMENT_DATA_DEFAULT); stackBase.setExport(true); programScope.add(stackBase); return stackBase.getConstantRef(); diff --git a/src/main/java/dk/camelot64/kickc/model/Initializers.java b/src/main/java/dk/camelot64/kickc/model/Initializers.java index 45a3d029f..d20064c33 100644 --- a/src/main/java/dk/camelot64/kickc/model/Initializers.java +++ b/src/main/java/dk/camelot64/kickc/model/Initializers.java @@ -16,7 +16,7 @@ public class Initializers { /** * Create a statement that initializes a variable with the default (zero) value. The statement has to be added to the program by the caller. * - * @param type The type of the variable + * @param typeSpec The type of the variable * @param statementSource The source line * @return The new statement */ @@ -32,15 +32,15 @@ public class Initializers { return new StructZero(typeStruct); } else if(typeSpec.getType() instanceof SymbolTypePointer) { SymbolTypePointer typePointer = (SymbolTypePointer) typeSpec.getType(); - if(typeSpec.getArraySpec() == null) { + if(typePointer.getArraySpec() == null) { // Add an zero value initializer return new ConstantPointer(0L, typePointer.getElementType()); } else { // Add an zero-filled array initializer - if(typeSpec.getArraySpec().getArraySize() == null) { + if(typePointer.getArraySpec().getArraySize() == null) { throw new CompileError("Array has no declared size.", statementSource); } - return new ConstantArrayFilled(typePointer.getElementType(), typeSpec.getArraySpec().getArraySize()); + return new ConstantArrayFilled(typePointer.getElementType(), typePointer.getArraySpec().getArraySize()); } } else { throw new CompileError("Default initializer not implemented for type " + typeSpec.getType().getTypeName(), statementSource); @@ -52,20 +52,14 @@ public class Initializers { SymbolType type; - ArraySpec arraySpec; - - public ValueTypeSpec(SymbolType type, ArraySpec arraySpec) { + public ValueTypeSpec(SymbolType type) { this.type = type; - this.arraySpec = arraySpec; } public SymbolType getType() { return type; } - public ArraySpec getArraySpec() { - return arraySpec; - } } @@ -88,16 +82,16 @@ public class Initializers { final CastValue castValue = (CastValue) initValue; if(castValue.getValue() instanceof ValueList && castValue.getToType() instanceof SymbolTypeStruct) { final SymbolType toType = castValue.getToType(); - final RValue constantSub = constantify(castValue.getValue(), new ValueTypeSpec(toType, null), program, source); + final RValue constantSub = constantify(castValue.getValue(), new ValueTypeSpec(toType), program, source); if(constantSub instanceof ConstantValue) { return new ConstantCastValue(toType, (ConstantValue) constantSub); } } } else if(initValue instanceof ValueList) { ValueList initList = (ValueList) initValue; - if(typeSpec.getType() instanceof SymbolTypePointer && typeSpec.getArraySpec() != null) { + if(typeSpec.getType() instanceof SymbolTypePointer && ((SymbolTypePointer)typeSpec.getType()).getArraySpec() != null) { // Type is an array - initValue = constantifyArray(initList, (SymbolTypePointer) typeSpec.getType(), typeSpec.getArraySpec(), program, source); + initValue = constantifyArray(initList, (SymbolTypePointer) typeSpec.getType(), program, source); } else if(typeSpec.getType() instanceof SymbolTypeStruct) { // Type is a struct initValue = constantifyStruct(initList, (SymbolTypeStruct) typeSpec.getType(), program, source); @@ -145,7 +139,7 @@ public class Initializers { boolean allConst = true; List constantifiedList = new ArrayList<>(); for(RValue elementValue : valueList.getList()) { - RValue constantifiedElement = constantify(elementValue, new ValueTypeSpec(SymbolType.BYTE, null), program, source); + RValue constantifiedElement = constantify(elementValue, new ValueTypeSpec(SymbolType.BYTE), program, source); constantifiedList.add(constantifiedElement); if(!(constantifiedElement instanceof ConstantValue)) allConst = false; @@ -163,7 +157,7 @@ public class Initializers { boolean allConst = true; List constantifiedList = new ArrayList<>(); for(RValue elementValue : valueList.getList()) { - RValue constantifiedElement = constantify(elementValue, new ValueTypeSpec(SymbolType.WORD, null), program, source); + RValue constantifiedElement = constantify(elementValue, new ValueTypeSpec(SymbolType.WORD), program, source); constantifiedList.add(constantifiedElement); if(!(constantifiedElement instanceof ConstantValue)) allConst = false; @@ -209,7 +203,7 @@ public class Initializers { for(int i = 0; i < size; i++) { Variable memberDef = memberDefIt.next(); RValue memberValue = valueIt.next(); - RValue constantifiedMemberValue = constantify(memberValue, new ValueTypeSpec(memberDef.getType(), memberDef.getArraySpec()), program, source); + RValue constantifiedMemberValue = constantify(memberValue, new ValueTypeSpec(memberDef.getType()), program, source); constantifiedList.add(constantifiedMemberValue); if(constantifiedMemberValue instanceof ConstantValue) constMemberMap.put(memberDef.getRef(), (ConstantValue) constantifiedMemberValue); @@ -230,15 +224,15 @@ public class Initializers { * * @param valueList The list of values * @param arrayType The pointer type of the array - * @param arraySpec The array spec holding the array size. * @param program The program * @param source The source line * @return The constantified value */ - private static RValue constantifyArray(ValueList valueList, SymbolTypePointer arrayType, ArraySpec arraySpec, Program program, StatementSource source) { + private static RValue constantifyArray(ValueList valueList, SymbolTypePointer arrayType, Program program, StatementSource source) { + ArraySpec arraySpec = arrayType.getArraySpec(); SymbolType elementType = arrayType.getElementType(); // TODO: Handle array of array - ValueTypeSpec elementTypeSpec = new ValueTypeSpec(elementType, null); + ValueTypeSpec elementTypeSpec = new ValueTypeSpec(elementType); boolean allConst = true; List constantifiedList = new ArrayList<>(); for(RValue elementValue : valueList.getList()) { diff --git a/src/main/java/dk/camelot64/kickc/model/VariableBuilder.java b/src/main/java/dk/camelot64/kickc/model/VariableBuilder.java index 82200c7db..e1004e78d 100644 --- a/src/main/java/dk/camelot64/kickc/model/VariableBuilder.java +++ b/src/main/java/dk/camelot64/kickc/model/VariableBuilder.java @@ -29,9 +29,6 @@ public class VariableBuilder { /** The type of the variable. */ private SymbolType type; - /** Non-null if the variable is an array. */ - private ArraySpec arraySpec; - /** The directives of the variable declaration. */ private List directives; @@ -41,12 +38,11 @@ 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, ArraySpec arraySpec, List directives, String dataSegment, VariableBuilderConfig config) { + public VariableBuilder(String varName, Scope scope, boolean isParameter, SymbolType type, List directives, String dataSegment, VariableBuilderConfig config) { this.varName = varName; this.scope = scope; this.isParameter = isParameter; this.type = type; - this.arraySpec = arraySpec; this.directives = directives; this.dataSegment = dataSegment; this.config = config; @@ -58,7 +54,7 @@ public class VariableBuilder { * @return The variable */ public Variable build() { - Variable variable = new Variable(varName, getKind(), type, scope, getMemoryArea(), dataSegment, arraySpec, null); + Variable variable = new Variable(varName, getKind(), type, scope, getMemoryArea(), dataSegment, null); variable.setNoModify(this.isNoModify()); variable.setVolatile(this.isVolatile()); variable.setExport(this.isExport()); @@ -112,15 +108,6 @@ public class VariableBuilder { return SymbolType.isInteger(type) || SymbolType.BOOLEAN.equals(type); } - /** - * Is the type is a pointer type. - * - * @return True if the type is a pointer type. - */ - public boolean isTypePointer() { - return type instanceof SymbolTypePointer; - } - /** * Is the type is a struct type. * @@ -130,6 +117,15 @@ public class VariableBuilder { return type instanceof SymbolTypeStruct; } + /** + * Is the type is a pointer type. + * + * @return True if the type is a pointer type. + */ + public boolean isTypePointer() { + return type instanceof SymbolTypePointer; + } + /** * Is the variable a global variable * @@ -178,7 +174,7 @@ public class VariableBuilder { * @return true if the variable is an array declaration */ public boolean isArray() { - return arraySpec != null; + return isTypePointer() && ((SymbolTypePointer)type).getArraySpec()!=null; } /** diff --git a/src/main/java/dk/camelot64/kickc/model/operators/OperatorTypeId.java b/src/main/java/dk/camelot64/kickc/model/operators/OperatorTypeId.java index 19a6ab1e5..15f29bf36 100644 --- a/src/main/java/dk/camelot64/kickc/model/operators/OperatorTypeId.java +++ b/src/main/java/dk/camelot64/kickc/model/operators/OperatorTypeId.java @@ -47,7 +47,7 @@ public class OperatorTypeId extends OperatorUnary { if(typeIdConstant == null) { // Constant not found - create it long typeSize = getTypeId(type); - typeIdConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, null, new ConstantInteger(typeSize), Scope.SEGMENT_DATA_DEFAULT); + typeIdConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, new ConstantInteger(typeSize), Scope.SEGMENT_DATA_DEFAULT); programScope.add(typeIdConstant); } return typeIdConstant.getConstantRef(); diff --git a/src/main/java/dk/camelot64/kickc/model/symbols/Variable.java b/src/main/java/dk/camelot64/kickc/model/symbols/Variable.java index c6d18da06..51ebb2405 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/Variable.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/Variable.java @@ -103,9 +103,6 @@ public class Variable implements Symbol { /** Comments preceding the procedure in the source code. [ALL] */ private List comments; - /** Non-null if the variable is an array. [Only constants that are arrays] */ - private ArraySpec arraySpec; - /** The initial compiletime-value of the variable. Null if no initial value present. [Constants, Arrays, global/local-static loadstore-variables ] */ private ConstantValue initValue; @@ -127,17 +124,15 @@ public class Variable implements Symbol { * @param scope The scope * @param memoryArea The memory area (zeropage/main memory) * @param dataSegment The data segment (in main memory) - * @param arraySpec The array specification of the variable (if it is an array) * @param initValue The constant value of the variable (if it is constant) */ - public Variable(String name, Kind kind, SymbolType type, Scope scope, MemoryArea memoryArea, String dataSegment, ArraySpec arraySpec, ConstantValue initValue) { + public Variable(String name, Kind kind, SymbolType type, Scope scope, MemoryArea memoryArea, String dataSegment, ConstantValue initValue) { this.name = name; this.kind = kind; if(Kind.PHI_MASTER.equals(kind)) this.nextPhiVersionNumber = 0; this.type = type; this.scope = scope; - this.arraySpec = arraySpec; this.dataSegment = dataSegment; this.memoryArea = memoryArea; this.initValue = initValue; @@ -154,7 +149,7 @@ public class Variable implements Symbol { * @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, null); + return new Variable(name, Kind.INTERMEDIATE, SymbolType.VAR, scope, MemoryArea.ZEROPAGE_MEMORY, dataSegment, null); } /** @@ -168,7 +163,7 @@ public class Variable implements Symbol { * @return The new PHI-master variable */ public static Variable createLoadStore(String name, SymbolType type, Scope scope, Variable.MemoryArea memoryArea, String dataSegment) { - return new Variable(name, Kind.LOAD_STORE, type, scope, memoryArea, dataSegment, null, null); + return new Variable(name, Kind.LOAD_STORE, type, scope, memoryArea, dataSegment, null); } /** @@ -182,7 +177,7 @@ public class Variable implements Symbol { * @return The new PHI-master variable */ public static Variable createPhiMaster(String name, SymbolType type, Scope scope, Variable.MemoryArea memoryArea, String dataSegment) { - return new Variable(name, Kind.PHI_MASTER, type, scope, memoryArea, dataSegment, null, null); + return new Variable(name, Kind.PHI_MASTER, type, scope, memoryArea, dataSegment, null); } /** @@ -194,7 +189,7 @@ public class Variable implements Symbol { public static Variable createPhiVersion(Variable phiMaster, int versionNum) { if(!phiMaster.isKindPhiMaster()) throw new InternalError("Cannot version non-PHI variable " + phiMaster.toString()); - Variable version = new Variable(phiMaster.getName() + "#" + versionNum, Kind.PHI_VERSION, phiMaster.getType(), phiMaster.getScope(), phiMaster.getMemoryArea(), phiMaster.getDataSegment(), phiMaster.getArraySpec(), null); + Variable version = new Variable(phiMaster.getName() + "#" + versionNum, Kind.PHI_VERSION, phiMaster.getType(), phiMaster.getScope(), phiMaster.getMemoryArea(), phiMaster.getDataSegment(), null); version.setMemoryAlignment(phiMaster.getMemoryAlignment()); version.setMemoryAddress(phiMaster.getMemoryAddress()); version.setOptimize(phiMaster.isOptimize()); @@ -226,12 +221,11 @@ public class Variable implements Symbol { * @param name The name * @param type The type * @param scope The scope - * @param arraySpec The array specification (if an array) * @param constantValue The constant value * @param dataSegment The data segment (in main memory) */ - public static Variable createConstant(String name, SymbolType type, Scope scope, ArraySpec arraySpec, ConstantValue constantValue, String dataSegment) { - return new Variable(name, Kind.CONSTANT, type, scope, MemoryArea.MAIN_MEMORY, dataSegment, arraySpec, constantValue); + public static Variable createConstant(String name, SymbolType type, Scope scope, ConstantValue constantValue, String dataSegment) { + return new Variable(name, Kind.CONSTANT, type, scope, MemoryArea.MAIN_MEMORY, dataSegment, constantValue); } /** @@ -241,7 +235,7 @@ public class Variable implements Symbol { * @param constantValue The constant value */ public static Variable createConstant(Variable variable, ConstantValue constantValue) { - Variable constVar = new Variable(variable.getName(), Kind.CONSTANT, variable.getType(), variable.getScope(), MemoryArea.MAIN_MEMORY, variable.getDataSegment(), variable.getArraySpec(), constantValue); + Variable constVar = new Variable(variable.getName(), Kind.CONSTANT, variable.getType(), variable.getScope(), MemoryArea.MAIN_MEMORY, variable.getDataSegment(), constantValue); constVar.setMemoryAlignment(variable.getMemoryAlignment()); constVar.setMemoryAddress(variable.getMemoryAddress()); constVar.setOptimize(variable.isOptimize()); @@ -264,7 +258,7 @@ public class Variable implements Symbol { * @param original The original variable */ public static Variable createCopy(String name, Scope scope, Variable original) { - Variable copy = new Variable(name, original.getKind(), original.getType(), scope, original.getMemoryArea(), original.getDataSegment(), original.getArraySpec(), original.getInitValue()); + Variable copy = new Variable(name, original.getKind(), original.getType(), scope, original.getMemoryArea(), original.getDataSegment(), original.getInitValue()); copy.setMemoryAlignment(original.getMemoryAlignment()); copy.setMemoryAddress(original.getMemoryAddress()); copy.setOptimize(original.isOptimize()); @@ -293,13 +287,14 @@ public class Variable implements Symbol { Variable memberVariable; if(isParameter && memberDefinition.isArray()) { // Array struct members are converted to pointers when unwound (use same kind as the struct variable) - memberVariable = new Variable(name, structVar.getKind(), memberDefinition.getType(), structVar.getScope(), memoryArea, structVar.getDataSegment(), null, null); + SymbolTypePointer arrayType = (SymbolTypePointer) memberDefinition.getType(); + memberVariable = new Variable(name, structVar.getKind(), new SymbolTypePointer(arrayType.getElementType()), structVar.getScope(), memoryArea, structVar.getDataSegment(), null); } else if(memberDefinition.isKindConstant()) { // Constant members are unwound as constants - memberVariable = new Variable(name, Kind.CONSTANT, memberDefinition.getType(), structVar.getScope(), memoryArea, structVar.getDataSegment(), memberDefinition.getArraySpec(), memberDefinition.getInitValue()); + memberVariable = new Variable(name, Kind.CONSTANT, memberDefinition.getType(), structVar.getScope(), memoryArea, structVar.getDataSegment(), memberDefinition.getInitValue()); } else { // For others the kind is preserved from the member definition - memberVariable = new Variable(name, structVar.getKind(), memberDefinition.getType(), structVar.getScope(), memoryArea, structVar.getDataSegment(), memberDefinition.getArraySpec(), memberDefinition.getInitValue()); + memberVariable = new Variable(name, structVar.getKind(), memberDefinition.getType(), structVar.getScope(), memoryArea, structVar.getDataSegment(), memberDefinition.getInitValue()); } memberVariable.setVolatile(structVar.isVolatile()); memberVariable.setNoModify(structVar.isNoModify()); @@ -390,7 +385,7 @@ public class Variable implements Symbol { * @return True if the variable is an array. */ public boolean isArray() { - return arraySpec != null; + return (getType() instanceof SymbolTypePointer) && (((SymbolTypePointer) getType()).getArraySpec() != null); } /** @@ -399,18 +394,17 @@ public class Variable implements Symbol { * @return The size of the array if declared. Null if not an array or an array without a declared size. */ public ConstantValue getArraySize() { - if(arraySpec != null) - return arraySpec.getArraySize(); + if(isArray()) + return ((SymbolTypePointer) getType()).getArraySpec().getArraySize(); else return null; } public ArraySpec getArraySpec() { - return arraySpec; - } - - public void setArraySpec(ArraySpec arraySpec) { - this.arraySpec = arraySpec; + if(isArray()) + return ((SymbolTypePointer) getType()).getArraySpec(); + else + return null; } public boolean isStruct() { diff --git a/src/main/java/dk/camelot64/kickc/model/types/SymbolTypePointer.java b/src/main/java/dk/camelot64/kickc/model/types/SymbolTypePointer.java index 6c932b123..7c8bcadca 100644 --- a/src/main/java/dk/camelot64/kickc/model/types/SymbolTypePointer.java +++ b/src/main/java/dk/camelot64/kickc/model/types/SymbolTypePointer.java @@ -1,5 +1,9 @@ package dk.camelot64.kickc.model.types; +import dk.camelot64.kickc.model.symbols.ArraySpec; + +import java.util.Objects; + /** A pointer */ public class SymbolTypePointer implements SymbolType { @@ -8,8 +12,16 @@ public class SymbolTypePointer implements SymbolType { private SymbolType elementType; - public SymbolTypePointer(SymbolType elementType) { + /** If non-null the pointer is an array. */ + private ArraySpec arraySpec; + + public SymbolTypePointer(SymbolType elementType, ArraySpec arraySpec) { this.elementType = elementType; + this.arraySpec = arraySpec; + } + + public SymbolTypePointer(SymbolType elementType) { + this(elementType, null); } public SymbolType getElementType() { @@ -20,6 +32,10 @@ public class SymbolTypePointer implements SymbolType { this.elementType = elementType; } + public ArraySpec getArraySpec() { + return arraySpec; + } + @Override public String getTypeName() { return elementType.getTypeName() + "*"; @@ -32,19 +48,15 @@ public class SymbolTypePointer implements SymbolType { @Override public boolean equals(Object o) { - if(this == o) { - return true; - } - if(o == null || getClass() != o.getClass()) { - return false; - } + if(this == o) return true; + if(o == null || getClass() != o.getClass()) return false; SymbolTypePointer that = (SymbolTypePointer) o; - return elementType != null ? elementType.equals(that.elementType) : that.elementType == null; + return Objects.equals(elementType, that.elementType); } @Override public int hashCode() { - return elementType != null ? elementType.hashCode() : 0; + return Objects.hash(elementType); } @Override diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java index 1f4a97acf..c2d5dbde7 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java @@ -403,7 +403,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor typeDirectives; /** If the type is SymbolTypePointer this holds the declaration type of the elements. */ @@ -816,10 +814,6 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor getTypeDirectives() { return typeDirectives; } @@ -832,10 +826,6 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor effectiveDirectives = varDecl.getEffectiveDirectives(); final List declComments = varDecl.getDeclComments(); varDecl.exitVar(); - VariableBuilder varBuilder = new VariableBuilder(varName, getCurrentScope(), false, effectiveType, effectiveArraySpec, effectiveDirectives, currentDataSegment, program.getTargetPlatform().getVariableBuilderConfig()); + VariableBuilder varBuilder = new VariableBuilder(varName, getCurrentScope(), 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(), statementSource); @@ -1066,7 +1050,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor getMemberNames(ProgramScope scope) { if(getSymbolType() instanceof SymbolTypeStruct) { diff --git a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceConstant.java b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceConstant.java index c45dc1981..055b25c5c 100644 --- a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceConstant.java +++ b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceConstant.java @@ -15,12 +15,10 @@ import java.util.ListIterator; /** Unwinding a constant value. */ public class ValueSourceConstant extends ValueSourceBase { private final SymbolType symbolType; - private final ArraySpec arraySpec; private final ConstantValue value; - public ValueSourceConstant(SymbolType symbolType, ArraySpec arraySpec, ConstantValue value) { + public ValueSourceConstant(SymbolType symbolType, ConstantValue value) { this.symbolType = symbolType; - this.arraySpec = arraySpec; this.value = value; } @@ -29,10 +27,6 @@ public class ValueSourceConstant extends ValueSourceBase { return symbolType; } - @Override - public ArraySpec getArraySpec() { - return arraySpec; - } @Override protected boolean isStructClassic() { @@ -54,14 +48,12 @@ public class ValueSourceConstant extends ValueSourceBase { ConstantStructValue constantStructValue = (ConstantStructValue) val; final Variable member = structDefinition.getMember(memberName); final SymbolType type = member.getType(); - final ArraySpec arraySpec = member.getArraySpec(); final ConstantValue memberValue = constantStructValue.getValue(member.getRef()); - return new ValueSourceConstant(type, arraySpec, memberValue); + return new ValueSourceConstant(type, memberValue); } else if(val instanceof StructZero) { final SymbolType memberType = structDefinition.getMember(memberName).getType(); - final ArraySpec memberArraySpec = structDefinition.getMember(memberName).getArraySpec(); - final ConstantValue memberZeroValue = Initializers.createZeroValue(new Initializers.ValueTypeSpec(memberType, memberArraySpec), currentStmt.getSource()); - return new ValueSourceConstant(memberType, memberArraySpec, memberZeroValue); + final ConstantValue memberZeroValue = Initializers.createZeroValue(new Initializers.ValueTypeSpec(memberType), currentStmt.getSource()); + return new ValueSourceConstant(memberType, memberZeroValue); } throw new InternalError("Not supported "+ val); } @@ -74,7 +66,7 @@ public class ValueSourceConstant extends ValueSourceBase { @Override public RValue getBulkRValue(ProgramScope scope) { String constName = scope.allocateIntermediateVariableName(); - Variable constVar = Variable.createConstant(constName, symbolType, scope, getArraySpec(), value, Scope.SEGMENT_DATA_DEFAULT); + Variable constVar = Variable.createConstant(constName, symbolType, scope, value, Scope.SEGMENT_DATA_DEFAULT); scope.add(constVar); if(getArraySpec()!=null) { final SymbolType elementType = ((SymbolTypePointer) getSymbolType()).getElementType(); diff --git a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceFactory.java b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceFactory.java index 26eaba83d..c460bed50 100644 --- a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceFactory.java +++ b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceFactory.java @@ -55,7 +55,7 @@ public class ValueSourceFactory { return new ValueSourceZero(((StructZero) value).getTypeStruct(), null); } if(value instanceof ConstantValue) - return new ValueSourceConstant(((ConstantValue) value).getType(programScope), null, (ConstantValue) value); + return new ValueSourceConstant(((ConstantValue) value).getType(programScope), (ConstantValue) value); if(value instanceof PointerDereferenceSimple) return new ValueSourcePointerDereferenceSimple((PointerDereferenceSimple) value, valueType, null); if(value instanceof PointerDereferenceIndexed) diff --git a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceIndexed.java b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceIndexed.java index 35961dc4d..a0fb1207f 100644 --- a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceIndexed.java +++ b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceIndexed.java @@ -36,11 +36,6 @@ public class ValueSourcePointerDereferenceIndexed extends ValueSourceBase { return valueType; } - @Override - public ArraySpec getArraySpec() { - return valueArraySpec; - } - @Override protected boolean isStructClassic() { return getSymbolType() instanceof SymbolTypeStruct; @@ -87,7 +82,7 @@ public class ValueSourcePointerDereferenceIndexed extends ValueSourceBase { if(pointerDereferenceIndexed.getIndex() instanceof ConstantValue) { // Unwind to (elmType*)&struct + OFFSET_MEMBER + idx ConstantBinary elmPointer = new ConstantBinary(memberPointer, Operators.PLUS, (ConstantValue) pointerDereferenceIndexed.getIndex()); - return new ValueSourceConstant(new SymbolTypePointer(elementType), null, elmPointer); + return new ValueSourceConstant(new SymbolTypePointer(elementType), elmPointer); } else { // Unwind to (elmType*)&struct + OFFSET_MEMBER + idx Scope scope = programScope.getScope(currentBlock.getScope()); diff --git a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceSimple.java b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceSimple.java index 947b35f0a..e5811aeb4 100644 --- a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceSimple.java +++ b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourcePointerDereferenceSimple.java @@ -77,7 +77,7 @@ public class ValueSourcePointerDereferenceSimple extends ValueSourceBase { // Calculate member address (elmtype*)&struct + OFFSET_MEMBER ConstantBinary memberPointer = new ConstantBinary(structTypedPointer, Operators.PLUS, memberOffsetConstant); // Unwind to (type*)&struct + OFFSET_MEMBER - return new ValueSourceConstant(pointerToElementType, null, memberPointer); + return new ValueSourceConstant(pointerToElementType, memberPointer); } else { // Pointer to member type ConstantCastValue structTypedPointer = new ConstantCastValue(new SymbolTypePointer(memberType), (ConstantValue) structPointer); diff --git a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceVariable.java b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceVariable.java index 60247a122..4f422dc9a 100644 --- a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceVariable.java +++ b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceVariable.java @@ -83,7 +83,7 @@ public class ValueSourceVariable extends ValueSourceBase { // Calculate member address (elmtype*)&struct + OFFSET_MEMBER ConstantBinary memberArrayPointer = new ConstantBinary(structTypedPointer, Operators.PLUS, memberOffsetConstant); // Unwind to (elmtype*)&struct + OFFSET_MEMBER - return new ValueSourceConstant(pointerToElementType, null, memberArrayPointer); + return new ValueSourceConstant(pointerToElementType, memberArrayPointer); } else { // Simple member value - unwind to value of member *((type*)&struct + OFFSET_MEMBER) // Pointer to member type diff --git a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceZero.java b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceZero.java index e28ac0122..eb434465b 100644 --- a/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceZero.java +++ b/src/main/java/dk/camelot64/kickc/passes/unwinding/ValueSourceZero.java @@ -44,7 +44,7 @@ public class ValueSourceZero extends ValueSourceBase { @Override public RValue getSimpleValue(ProgramScope programScope) { - return Initializers.createZeroValue(new Initializers.ValueTypeSpec(getSymbolType(), getArraySpec()), null); + return Initializers.createZeroValue(new Initializers.ValueTypeSpec(getSymbolType()), null); } @Override diff --git a/src/main/java/dk/camelot64/kickc/passes/utils/SizeOfConstants.java b/src/main/java/dk/camelot64/kickc/passes/utils/SizeOfConstants.java index 14a99ad54..737a6e8cd 100644 --- a/src/main/java/dk/camelot64/kickc/passes/utils/SizeOfConstants.java +++ b/src/main/java/dk/camelot64/kickc/passes/utils/SizeOfConstants.java @@ -26,7 +26,7 @@ public class SizeOfConstants { if(typeSizeConstant == null) { // Constant not found - create it long typeSize = type.getSizeBytes(); - typeSizeConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, null, new ConstantInteger(typeSize&0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); + typeSizeConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, new ConstantInteger(typeSize&0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); programScope.add(typeSizeConstant); } return typeSizeConstant.getConstantRef(); @@ -77,7 +77,7 @@ public class SizeOfConstants { // Constant not found - create it Variable memberDef = structDefinition.getMember(memberName); long memberByteOffset = structDefinition.getMemberByteOffset(memberDef, programScope); - memberOffsetConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, null, new ConstantInteger(memberByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); + memberOffsetConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, new ConstantInteger(memberByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); programScope.add(memberOffsetConstant); } return memberOffsetConstant.getConstantRef(); diff --git a/src/test/ref/adventofcode/2020-01.log b/src/test/ref/adventofcode/2020-01.log index 3d3fbe6c4..600fd1433 100644 --- a/src/test/ref/adventofcode/2020-01.log +++ b/src/test/ref/adventofcode/2020-01.log @@ -6808,13 +6808,13 @@ ASSEMBLER BEFORE OPTIMIZATION // Find 2 entries that give 2020 when added together // And 3 entries that give 2020 when added together // Upstart - // Atari XL/XE executable XEX file with a single segment -// https://www.atarimax.com/jindroush.atari.org/afmtexe.html -.plugin "dk.camelot64.kickass.xexplugin.AtariXex" -.file [name="2020-01.xex", type="bin", segments="XexFile"] -.segmentdef XexFile [segments="Program", modify="XexFormat", _RunAddr=main] -.segmentdef Program [segments="Code, Data"] -.segmentdef Code [start=$2000] + // Atari XL/XE executable XEX file with a single segment +// https://www.atarimax.com/jindroush.atari.org/afmtexe.html +.plugin "dk.camelot64.kickass.xexplugin.AtariXex" +.file [name="2020-01.xex", type="bin", segments="XexFile"] +.segmentdef XexFile [segments="Program", modify="XexFormat", _RunAddr=main] +.segmentdef Program [segments="Code, Data"] +.segmentdef Code [start=$2000] .segmentdef Data [startAfter="Code"] // Global Constants & labels .const SIZEOF_WORD = 2 @@ -9782,13 +9782,13 @@ Score: 27696937 // Find 2 entries that give 2020 when added together // And 3 entries that give 2020 when added together // Upstart - // Atari XL/XE executable XEX file with a single segment -// https://www.atarimax.com/jindroush.atari.org/afmtexe.html -.plugin "dk.camelot64.kickass.xexplugin.AtariXex" -.file [name="2020-01.xex", type="bin", segments="XexFile"] -.segmentdef XexFile [segments="Program", modify="XexFormat", _RunAddr=main] -.segmentdef Program [segments="Code, Data"] -.segmentdef Code [start=$2000] + // Atari XL/XE executable XEX file with a single segment +// https://www.atarimax.com/jindroush.atari.org/afmtexe.html +.plugin "dk.camelot64.kickass.xexplugin.AtariXex" +.file [name="2020-01.xex", type="bin", segments="XexFile"] +.segmentdef XexFile [segments="Program", modify="XexFormat", _RunAddr=main] +.segmentdef Program [segments="Code, Data"] +.segmentdef Code [start=$2000] .segmentdef Data [startAfter="Code"] // Global Constants & labels .const SIZEOF_WORD = 2