From 4b4c6f5364ab4beda1e4e95fe98816a68e09bbb7 Mon Sep 17 00:00:00 2001 From: Jesper Gravgaard Date: Sun, 22 Jul 2018 16:41:19 +0900 Subject: [PATCH] Using ProgramValueIterator instead of custom loop. --- .../kickc/model/VariableReferenceInfos.java | 7 +- .../kickc/model/iterator/ProgramValue.java | 10 +-- .../passes/PassNVariableReferenceInfos.java | 74 +++---------------- 3 files changed, 23 insertions(+), 68 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/model/VariableReferenceInfos.java b/src/main/java/dk/camelot64/kickc/model/VariableReferenceInfos.java index 8f7568515..88279d971 100644 --- a/src/main/java/dk/camelot64/kickc/model/VariableReferenceInfos.java +++ b/src/main/java/dk/camelot64/kickc/model/VariableReferenceInfos.java @@ -11,7 +11,12 @@ import java.util.Collection; import java.util.LinkedHashSet; import java.util.Map; -/** Cached information about which variables are defined/referenced/used in statements / blocks. */ +/** Cached information about which variables/constants are defined/referenced/used in which statements / blocks / symbols . + * */ public class VariableReferenceInfos { /** Variables referenced in each block. */ diff --git a/src/main/java/dk/camelot64/kickc/model/iterator/ProgramValue.java b/src/main/java/dk/camelot64/kickc/model/iterator/ProgramValue.java index b2c70f3e3..d176fb1a4 100644 --- a/src/main/java/dk/camelot64/kickc/model/iterator/ProgramValue.java +++ b/src/main/java/dk/camelot64/kickc/model/iterator/ProgramValue.java @@ -532,20 +532,20 @@ public abstract class ProgramValue { /** A generic Value. */ public static class GenericValue extends ProgramValue { - private ConstantValue constantValue; + private RValue rValue; - public GenericValue(ConstantValue constantValue) { - this.constantValue = constantValue; + public GenericValue(RValue rValue) { + this.rValue = rValue; } @Override public RValue get() { - return constantValue; + return rValue; } @Override public void set(RValue value) { - this.constantValue = (ConstantValue) value; + this.rValue = (ConstantValue) value; } } } diff --git a/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java b/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java index cde514876..3ba88e738 100644 --- a/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java +++ b/src/main/java/dk/camelot64/kickc/passes/PassNVariableReferenceInfos.java @@ -1,10 +1,13 @@ 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.ControlFlowBlock; +import dk.camelot64.kickc.model.Program; +import dk.camelot64.kickc.model.VariableReferenceInfos; +import dk.camelot64.kickc.model.iterator.ProgramValue; +import dk.camelot64.kickc.model.iterator.ProgramValueIterator; import dk.camelot64.kickc.model.statements.*; import dk.camelot64.kickc.model.symbols.ConstantVar; +import dk.camelot64.kickc.model.values.*; import java.util.*; @@ -56,66 +59,13 @@ public class PassNVariableReferenceInfos extends Pass2Base { * @return All referenced variables / constants */ private static Collection getReferenced(RValue rValue) { - if(rValue == null) { - return new ArrayList<>(); - } else if(rValue instanceof ConstantBinary) { - 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<>(); - for(ConstantValue elem : ((ConstantArrayList) rValue).getElements()) { - used.addAll(getReferenced(elem)); + Collection referenced = new LinkedHashSet<>(); + ProgramValueIterator.execute(new ProgramValue.GenericValue(rValue), (programValue, currentStmt, stmtIt, currentBlock) -> { + if(programValue.get() instanceof SymbolVariableRef) { + referenced.add((SymbolVariableRef) programValue.get()); } - return used; - } else if(rValue instanceof ArrayFilled) { - return getReferenced(((ArrayFilled) rValue).getSize()); - } else if(rValue instanceof ConstantArrayFilled) { - return getReferenced(((ConstantArrayFilled) rValue).getSize()); - } else if(rValue instanceof ConstantUnary) { - return getReferenced(((ConstantUnary) rValue).getOperand()); - } else if(rValue instanceof ConstantRef) { - return Arrays.asList((SymbolVariableRef) rValue); - } else if(rValue instanceof ConstantString) { - return new ArrayList<>(); - } else if(rValue instanceof ConstantInteger) { - return new ArrayList<>(); - } else if(rValue instanceof ConstantBool) { - return new ArrayList<>(); - } else if(rValue instanceof ConstantChar) { - return new ArrayList<>(); - } else if(rValue instanceof ConstantPointer) { - return new ArrayList<>(); - } else if(rValue instanceof PointerDereferenceSimple) { - return getReferenced(((PointerDereferenceSimple) rValue).getPointer()); - } else if(rValue instanceof PointerDereferenceIndexed) { - 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((SymbolVariableRef) rValue); - } else if(rValue instanceof ValueList) { - LinkedHashSet used = new LinkedHashSet<>(); - for(RValue value : ((ValueList) rValue).getList()) { - used.addAll(getReferenced(value)); - } - return used; - } else if(rValue instanceof CastValue) { - return getReferenced(((CastValue) rValue).getValue()); - } else if(rValue instanceof ConstantCastValue) { - return getReferenced(((ConstantCastValue) rValue).getValue()); - } else if(rValue instanceof ConstantVarPointer) { - return getReferenced(((ConstantVarPointer) rValue).getToVar()); - } else if(rValue instanceof RangeValue) { - Collection used = new LinkedHashSet<>(); - used.addAll(getReferenced(((RangeValue) rValue).getRangeFirst())); - used.addAll(getReferenced(((RangeValue) rValue).getRangeLast())); - return used; - } else { - throw new RuntimeException("Unhandled RValue type " + rValue); - } + }, null, null, null); + return referenced; } /** Create defined/referenced maps */