From cff815f5d783021d53cb8155aef6aaef10142b66 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Sun, 8 Dec 2019 17:49:14 +0100 Subject: [PATCH] Restructured Variable creation to make it more explicit what type is being created. --- .../kickc/fragment/AsmFragmentTemplate.java | 25 +-- .../kickc/model/CallingConventionStack.java | 6 +- .../kickc/model/operators/OperatorSizeOf.java | 2 +- .../kickc/model/operators/OperatorTypeId.java | 2 +- .../camelot64/kickc/model/symbols/Scope.java | 10 +- .../kickc/model/symbols/Variable.java | 180 ++++++++++++------ .../Pass0GenerateStatementSequence.java | 27 ++- .../Pass1EarlyConstantIdentification.java | 2 +- .../passes/Pass1ExtractInlineStrings.java | 2 +- .../kickc/passes/Pass1ProcedureInline.java | 2 +- .../kickc/passes/Pass1UnwindBlockScopes.java | 6 +- .../kickc/passes/Pass1UnwindStructValues.java | 10 +- .../passes/Pass2ConstantIdentification.java | 2 +- .../Pass2ConstantStringConsolidation.java | 2 +- .../passes/PassNStructPointerRewriting.java | 2 +- .../kickc/passes/utils/Unroller.java | 2 +- 16 files changed, 171 insertions(+), 111 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentTemplate.java b/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentTemplate.java index 6ae767f1d..8b05eb6c2 100644 --- a/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentTemplate.java +++ b/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentTemplate.java @@ -79,12 +79,13 @@ public class AsmFragmentTemplate { ProgramScope scope = new ProgramScope(); LinkedHashMap bindings = new LinkedHashMap<>(); { - Variable v1 = new Variable( "z1", Variable.Kind.PHI_VERSION, SymbolType.BYTE, scope, Variable.MemoryArea.ZEROPAGE_MEMORY, null); - Variable v2 = new Variable( "z2", Variable.Kind.PHI_VERSION, SymbolType.BYTE, scope, Variable.MemoryArea.ZEROPAGE_MEMORY, null); - Variable v3 = new Variable( "z3", Variable.Kind.PHI_VERSION, SymbolType.BYTE, scope, Variable.MemoryArea.ZEROPAGE_MEMORY, null); - Variable v4 = new Variable( "z4", Variable.Kind.PHI_VERSION, SymbolType.BYTE, scope, Variable.MemoryArea.ZEROPAGE_MEMORY, null); - Variable v5 = new Variable( "z5", Variable.Kind.PHI_VERSION, SymbolType.BYTE, scope, Variable.MemoryArea.ZEROPAGE_MEMORY, null); - Variable v6 = new Variable( "z6", Variable.Kind.PHI_VERSION, SymbolType.BYTE, scope, Variable.MemoryArea.ZEROPAGE_MEMORY, null); + Variable master = Variable.createPhiMaster("z", SymbolType.BYTE, scope, Variable.MemoryArea.ZEROPAGE_MEMORY, null); + Variable v1 = Variable.createPhiVersion(master, 1); v1.setName("z1"); + Variable v2 = Variable.createPhiVersion(master, 2); v2.setName("z2"); + Variable v3 = Variable.createPhiVersion(master, 3); v3.setName("z3"); + Variable v4 = Variable.createPhiVersion(master, 4); v4.setName("z4"); + Variable v5 = Variable.createPhiVersion(master, 5); v5.setName("z5"); + Variable v6 = Variable.createPhiVersion(master, 6); v6.setName("z6"); v1.setAllocation(new Registers.RegisterZpMem(2, 1)); v2.setAllocation(new Registers.RegisterZpMem(4, 1)); v3.setAllocation(new Registers.RegisterZpMem(6, 1)); @@ -99,12 +100,12 @@ public class AsmFragmentTemplate { if(signature.contains("z6")) bindings.put("z6", v6); } { - Variable v1 = new Variable( "m1", Variable.Kind.LOAD_STORE, SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); - Variable v2 = new Variable( "m2", Variable.Kind.LOAD_STORE, SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); - Variable v3 = new Variable( "m3", Variable.Kind.LOAD_STORE, SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); - Variable v4 = new Variable( "m4", Variable.Kind.LOAD_STORE, SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); - Variable v5 = new Variable( "m5", Variable.Kind.LOAD_STORE, SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); - Variable v6 = new Variable( "m6", Variable.Kind.LOAD_STORE, SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); + Variable v1 = Variable.createLoadStore("m1", SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); + Variable v2 = Variable.createLoadStore("m2", SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); + Variable v3 = Variable.createLoadStore("m3", SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); + Variable v4 = Variable.createLoadStore("m4", SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); + Variable v5 = Variable.createLoadStore("m5", SymbolType.BYTE, scope, Variable.MemoryArea.MAIN_MEMORY, null); + Variable v6 = Variable.createLoadStore("m6", SymbolType.BYTE, scope, 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)); diff --git a/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java b/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java index 4d1b57195..3e92f21a7 100644 --- a/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java +++ b/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java @@ -23,7 +23,7 @@ public class CallingConventionStack { if(returnOffsetConstant == null) { // Constant not found - create it long returnByteOffset = getReturnByteOffset(procedure); - returnOffsetConstant = new Variable(returnOffsetConstantName, SymbolType.BYTE, null, procedure, Scope.SEGMENT_DATA_DEFAULT, new ConstantInteger(returnByteOffset & 0xff, SymbolType.BYTE)); + returnOffsetConstant = Variable.createConstant(returnOffsetConstantName, SymbolType.BYTE, procedure, null, new ConstantInteger(returnByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); procedure.add(returnOffsetConstant); } return returnOffsetConstant.getConstantRef(); @@ -42,7 +42,7 @@ public class CallingConventionStack { if(paramOffsetConstant == null) { // Constant not found - create it long paramByteOffset = getParameterByteOffset(procedure, parameter); - paramOffsetConstant = new Variable(paramOffsetConstantName, SymbolType.BYTE, null, procedure, Scope.SEGMENT_DATA_DEFAULT, new ConstantInteger(paramByteOffset & 0xff, SymbolType.BYTE)); + paramOffsetConstant = Variable.createConstant(paramOffsetConstantName, SymbolType.BYTE, procedure, null, new ConstantInteger(paramByteOffset & 0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); procedure.add(paramOffsetConstant); } return paramOffsetConstant.getConstantRef(); @@ -123,7 +123,7 @@ public class CallingConventionStack { */ public static ConstantRef getStackBaseConstant(ProgramScope programScope) { long STACK_BASE_ADDRESS = 0x103L; - Variable stackBase = new Variable("STACK_BASE", SymbolType.WORD, null, programScope, Scope.SEGMENT_DATA_DEFAULT, new ConstantInteger(STACK_BASE_ADDRESS, SymbolType.WORD)); + Variable stackBase = Variable.createConstant("STACK_BASE", SymbolType.WORD, programScope, null, new ConstantInteger(STACK_BASE_ADDRESS, SymbolType.WORD), Scope.SEGMENT_DATA_DEFAULT); programScope.add(stackBase); return stackBase.getConstantRef(); } diff --git a/src/main/java/dk/camelot64/kickc/model/operators/OperatorSizeOf.java b/src/main/java/dk/camelot64/kickc/model/operators/OperatorSizeOf.java index d7167e98a..7e651c5eb 100644 --- a/src/main/java/dk/camelot64/kickc/model/operators/OperatorSizeOf.java +++ b/src/main/java/dk/camelot64/kickc/model/operators/OperatorSizeOf.java @@ -42,7 +42,7 @@ public class OperatorSizeOf extends OperatorUnary { if(typeSizeConstant == null) { // Constant not found - create it long typeSize = type.getSizeBytes(); - typeSizeConstant = new Variable(typeConstName, SymbolType.BYTE, null, programScope, Scope.SEGMENT_DATA_DEFAULT, new ConstantInteger(typeSize&0xff, SymbolType.BYTE)); + typeSizeConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, null, new ConstantInteger(typeSize&0xff, SymbolType.BYTE), Scope.SEGMENT_DATA_DEFAULT); programScope.add(typeSizeConstant); } return typeSizeConstant.getConstantRef(); 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 a72a3f230..7eea11d81 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 = new Variable(typeConstName, SymbolType.BYTE, null, programScope, Scope.SEGMENT_DATA_DEFAULT, new ConstantInteger(typeSize)); + typeIdConstant = Variable.createConstant(typeConstName, SymbolType.BYTE, programScope, null, new ConstantInteger(typeSize), Scope.SEGMENT_DATA_DEFAULT); programScope.add(typeIdConstant); } return typeIdConstant.getConstantRef(); diff --git a/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java b/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java index 4471affc4..f190b8e73 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java @@ -100,17 +100,9 @@ public abstract class Scope implements Symbol, Serializable { symbols.remove(symbol.getLocalName()); } - public Variable addVariable(Variable.Kind kind, String name, SymbolType type, Variable.MemoryArea memoryArea, String dataSegment) { - return add(new Variable( name, kind, type, this, memoryArea, dataSegment)); - } - - public Variable addVariablePhiMaster(String name, SymbolType type, Variable.MemoryArea memoryArea, String dataSegment) { - return add(new Variable( name, Variable.Kind.PHI_MASTER, type, this, memoryArea, dataSegment)); - } - public Variable addVariableIntermediate() { String name = allocateIntermediateVariableName(); - return add(new Variable( name, Variable.Kind.INTERMEDIATE, SymbolType.VAR, this, Variable.MemoryArea.ZEROPAGE_MEMORY, getSegmentData())); + return add(Variable.createIntermediate( name, this, getSegmentData())); } /** 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 6c8dbdf81..559930c99 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/Variable.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/Variable.java @@ -5,6 +5,7 @@ import dk.camelot64.kickc.model.InternalError; import dk.camelot64.kickc.model.Program; import dk.camelot64.kickc.model.Registers; import dk.camelot64.kickc.model.types.SymbolType; +import dk.camelot64.kickc.model.types.SymbolTypePointer; import dk.camelot64.kickc.model.values.ConstantRef; import dk.camelot64.kickc.model.values.ConstantValue; import dk.camelot64.kickc.model.values.SymbolVariableRef; @@ -14,12 +15,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -/** A Variable symbol (can either be a runtime variable or a compile-time constant)*/ +/** A Variable symbol (can either be a runtime variable or a compile-time constant) */ public class Variable implements Symbol { /** * The kind of the variable. The kind is the most significant property of the variable since it drives most of the behavior. - * + *

* The value depends on the directives memory/register/volatile/const - and on the compilers optimization decisions. *