From 01008ccb26b04313042121b66a8214edf5821015 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Fri, 6 Sep 2019 07:44:09 +0200 Subject: [PATCH] Optimized Pass2AssertSymbols and PassNCalcLiveRangeVariables. --- .../kickc/passes/Pass2AssertSymbols.java | 22 ++++------ .../calcs/PassNCalcLiveRangeVariables.java | 44 ++++++++++++++----- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2AssertSymbols.java b/src/main/java/dk/camelot64/kickc/passes/Pass2AssertSymbols.java index 561fc2e48..c1b4a02fc 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass2AssertSymbols.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2AssertSymbols.java @@ -19,12 +19,15 @@ public class Pass2AssertSymbols extends Pass2SsaAssertion { @Override public void check() throws AssertionFailed { + HashSet codeSymbolFullNames = new HashSet<>(); HashSet codeSymbols = new HashSet<>(); ProgramValueIterator.execute(getGraph(), (programValue, currentStmt, stmtIt, currentBlock) -> { if(programValue.get() instanceof SymbolRef) { Symbol symbol = getScope().getSymbol((SymbolRef) programValue.get()); - if(symbol != null) + if(symbol != null) { codeSymbols.add(symbol); + codeSymbolFullNames.add(symbol.getFullName()); + } } }); @@ -38,6 +41,7 @@ public class Pass2AssertSymbols extends Pass2SsaAssertion { } // Check that all symbols in the symbol table is also in the code Collection tableSymbols = getScope().getAllSymbols(true); + for(Symbol tableSymbol : tableSymbols) { if(tableSymbol instanceof VariableUnversioned) continue; if(tableSymbol instanceof ConstantVar) continue; @@ -45,20 +49,10 @@ public class Pass2AssertSymbols extends Pass2SsaAssertion { if(tableSymbol instanceof EnumDefinition) continue; if(tableSymbol instanceof TypeDefsScope) continue; if(tableSymbol.getType() instanceof SymbolTypeStruct) continue; - Symbol codeSymbol = null; String codeSymbolFullName = tableSymbol.getFullName(); - for(Symbol symbol : codeSymbols) { - if(codeSymbolFullName.equals(symbol.getFullName())) { - codeSymbol = symbol; - break; - } - } - if(codeSymbol == null) { - if(tableSymbol.getType() instanceof SymbolTypeStruct) { - getLog().append("Struct no longer used in code "+codeSymbolFullName); - } else { - throw new AssertionFailed("Compile process error. Symbol found in symbol table, but not in code. " + codeSymbolFullName); - } + if(!codeSymbolFullNames.contains(codeSymbolFullName)) { + throw new AssertionFailed("Compile process error. Symbol found in symbol table, but not in code. " + codeSymbolFullName); + } } } diff --git a/src/main/java/dk/camelot64/kickc/passes/calcs/PassNCalcLiveRangeVariables.java b/src/main/java/dk/camelot64/kickc/passes/calcs/PassNCalcLiveRangeVariables.java index 8fbda44d0..3a987986c 100644 --- a/src/main/java/dk/camelot64/kickc/passes/calcs/PassNCalcLiveRangeVariables.java +++ b/src/main/java/dk/camelot64/kickc/passes/calcs/PassNCalcLiveRangeVariables.java @@ -1,9 +1,5 @@ package dk.camelot64.kickc.passes.calcs; -/** - * Identify the alive intervals for all variables. Add the intervals to the ProgramScope. - */ - import dk.camelot64.kickc.model.*; import dk.camelot64.kickc.model.statements.Statement; import dk.camelot64.kickc.model.statements.StatementCall; @@ -13,11 +9,11 @@ import dk.camelot64.kickc.model.values.LabelRef; import dk.camelot64.kickc.model.values.ProcedureRef; import dk.camelot64.kickc.model.values.VariableRef; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; +import java.util.*; +/** + * Identify the alive intervals for all variables. Add the intervals to the ProgramScope. + */ public class PassNCalcLiveRangeVariables extends PassNCalcBase { public PassNCalcLiveRangeVariables(Program program) { @@ -26,6 +22,7 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase> procedureReferencedVars; + + static int callCount = 0; + + /** + * Calculate the variables referenced inside each procedure and all it's sub-calls. + */ + private void calculateProcedureReferencedVars() { + //getLog().append("calculateLiveRanges starting "+callCount); + VariableReferenceInfos referenceInfo = getProgram().getVariableReferenceInfos(); + Collection allProcedures = getScope().getAllProcedures(true); + Map> procReferencedVars = new LinkedHashMap<>(); + for(Procedure procedure : allProcedures) { + Collection referencedVars = referenceInfo.getReferencedVars(procedure.getRef().getLabelRef()); + procReferencedVars.put(procedure.getRef(), referencedVars); + } + //getLog().append("calculateLiveRanges done "+callCount); + callCount++; + this.procedureReferencedVars = procReferencedVars; + } + /** * Runs through all statements propagating variable live ranges. * Variable live ranges of a statement is defined as all variables that are defined before or in the statement and used after the statement. @@ -63,7 +83,9 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase procUsed = referenceInfo.getReferencedVars(procedure.getLabelRef()); + Collection procUsed = procedureReferencedVars.get(procedure); // The call statement has no used or defined by itself so only work with the alive vars for(VariableRef aliveVar : aliveNextStmt) { // Add all variables to previous that are not used inside the method @@ -105,7 +127,7 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase procUsed = referenceInfo.getReferencedVars(procedure.getLabelRef()); + Collection procUsed = procedureReferencedVars.get(procedure); // The call statement has no used or defined by itself so only work with the alive vars for(VariableRef aliveVar : aliveNextStmt) { // Add all variables to previous that are not used inside the method @@ -123,7 +145,7 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase procUsed = referenceInfo.getReferencedVars(procedure.getRef().getLabelRef()); + Collection procUsed = procedureReferencedVars.get(procedure.getRef()); // The call statement has no used or defined by itself so only work with the alive vars for(VariableRef aliveVar : aliveNextStmt) { // Add all variables to previous that are used inside the method