From 62cfda64683ba9e5d5afa76c7f99065702fc8536 Mon Sep 17 00:00:00 2001 From: Jesper Gravgaard Date: Sat, 21 Jul 2018 22:58:42 +0900 Subject: [PATCH] Added SymbolVariableRef (preparing for refacing ValueReplacer and ReferenceInfos) --- .../kickc/model/values/ConstantRef.java | 2 +- .../kickc/model/values/SymbolVariableRef.java | 10 +++++++++ .../kickc/model/values/VariableRef.java | 2 +- .../passes/Pass2ConstantIdentification.java | 1 - .../passes/PassNVariableReferenceInfos.java | 21 ++++++++++--------- 5 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 src/main/java/dk/camelot64/kickc/model/values/SymbolVariableRef.java diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java index caabee9f9..ebf311fb9 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantRef.java @@ -5,7 +5,7 @@ import dk.camelot64.kickc.model.symbols.ProgramScope; import dk.camelot64.kickc.model.types.SymbolType; /** A reference to a named Constant (in the symbol table) */ -public class ConstantRef extends SymbolRef implements ConstantValue { +public class ConstantRef extends SymbolVariableRef implements ConstantValue { public ConstantRef(ConstantVar constantVar) { super(constantVar.getFullName()); diff --git a/src/main/java/dk/camelot64/kickc/model/values/SymbolVariableRef.java b/src/main/java/dk/camelot64/kickc/model/values/SymbolVariableRef.java new file mode 100644 index 000000000..be26678a6 --- /dev/null +++ b/src/main/java/dk/camelot64/kickc/model/values/SymbolVariableRef.java @@ -0,0 +1,10 @@ +package dk.camelot64.kickc.model.values; + +/** A reference to a Variable or Constant (in the symbol table) */ +public abstract class SymbolVariableRef extends SymbolRef implements RValue { + + public SymbolVariableRef(String fullName) { + super(fullName); + } + +} diff --git a/src/main/java/dk/camelot64/kickc/model/values/VariableRef.java b/src/main/java/dk/camelot64/kickc/model/values/VariableRef.java index 8dbcd7160..86a69725b 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/VariableRef.java +++ b/src/main/java/dk/camelot64/kickc/model/values/VariableRef.java @@ -3,7 +3,7 @@ package dk.camelot64.kickc.model.values; import dk.camelot64.kickc.model.symbols.Variable; /** A reference to a variable from the symbol table */ -public class VariableRef extends SymbolRef implements RValue, LValue { +public class VariableRef extends SymbolVariableRef implements RValue, LValue { public VariableRef(String fullName) { super(fullName); diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java index e5520444d..bb0be6aa6 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIdentification.java @@ -313,7 +313,6 @@ public class Pass2ConstantIdentification extends Pass2SsaOptimization { * @return true if the address-of operator is used on the variable */ public static boolean isAddressOfUsed(VariableRef var, Program program) { - for(ControlFlowBlock block : program.getGraph().getAllBlocks()) { for(Statement statement : block.getStatements()) { if(statement instanceof StatementAssignment) { diff --git a/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java b/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java index e53804891..cde514876 100644 --- a/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java +++ b/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java @@ -1,6 +1,7 @@ package dk.camelot64.kickc.passes; import dk.camelot64.kickc.model.*; +import dk.camelot64.kickc.model.symbols.SymbolVariable; import dk.camelot64.kickc.model.values.*; import dk.camelot64.kickc.model.statements.*; import dk.camelot64.kickc.model.symbols.ConstantVar; @@ -54,16 +55,16 @@ public class PassNVariableReferenceInfos extends Pass2Base { * @param rValue The rValue * @return All referenced variables / constants */ - private static Collection getReferenced(RValue rValue) { + private static Collection getReferenced(RValue rValue) { if(rValue == null) { return new ArrayList<>(); } else if(rValue instanceof ConstantBinary) { - Collection used = new LinkedHashSet<>(); + Collection used = new LinkedHashSet<>(); used.addAll(getReferenced(((ConstantBinary) rValue).getLeft())); used.addAll(getReferenced(((ConstantBinary) rValue).getRight())); return used; } else if(rValue instanceof ConstantArrayList) { - Collection used = new LinkedHashSet<>(); + Collection used = new LinkedHashSet<>(); for(ConstantValue elem : ((ConstantArrayList) rValue).getElements()) { used.addAll(getReferenced(elem)); } @@ -75,7 +76,7 @@ public class PassNVariableReferenceInfos extends Pass2Base { } else if(rValue instanceof ConstantUnary) { return getReferenced(((ConstantUnary) rValue).getOperand()); } else if(rValue instanceof ConstantRef) { - return Arrays.asList((SymbolRef) rValue); + return Arrays.asList((SymbolVariableRef) rValue); } else if(rValue instanceof ConstantString) { return new ArrayList<>(); } else if(rValue instanceof ConstantInteger) { @@ -89,14 +90,14 @@ public class PassNVariableReferenceInfos extends Pass2Base { } else if(rValue instanceof PointerDereferenceSimple) { return getReferenced(((PointerDereferenceSimple) rValue).getPointer()); } else if(rValue instanceof PointerDereferenceIndexed) { - Collection used = new LinkedHashSet<>(); + Collection used = new LinkedHashSet<>(); used.addAll(getReferenced(((PointerDereferenceIndexed) rValue).getPointer())); used.addAll(getReferenced(((PointerDereferenceIndexed) rValue).getIndex())); return used; } else if(rValue instanceof VariableRef) { - return Arrays.asList((SymbolRef) rValue); + return Arrays.asList((SymbolVariableRef) rValue); } else if(rValue instanceof ValueList) { - LinkedHashSet used = new LinkedHashSet<>(); + LinkedHashSet used = new LinkedHashSet<>(); for(RValue value : ((ValueList) rValue).getList()) { used.addAll(getReferenced(value)); } @@ -108,7 +109,7 @@ public class PassNVariableReferenceInfos extends Pass2Base { } else if(rValue instanceof ConstantVarPointer) { return getReferenced(((ConstantVarPointer) rValue).getToVar()); } else if(rValue instanceof RangeValue) { - Collection used = new LinkedHashSet<>(); + Collection used = new LinkedHashSet<>(); used.addAll(getReferenced(((RangeValue) rValue).getRangeFirst())); used.addAll(getReferenced(((RangeValue) rValue).getRangeLast())); return used; @@ -323,8 +324,8 @@ public class PassNVariableReferenceInfos extends Pass2Base { * @param statement The statement to examine * @return The referenced variables / constants (VariableRef / ConstantRef) */ - private Collection getReferenced(Statement statement) { - LinkedHashSet referenced = new LinkedHashSet<>(); + private Collection getReferenced(Statement statement) { + LinkedHashSet referenced = new LinkedHashSet<>(); if(statement instanceof StatementPhiBlock) { StatementPhiBlock phiBlock = (StatementPhiBlock) statement; for(StatementPhiBlock.PhiVariable phiVariable : phiBlock.getPhiVariables()) {