From e5f1f7f387c49801d9af168754707867c4bd8c2e Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Wed, 14 Jun 2017 21:54:28 +0200 Subject: [PATCH] Added support for multiple procs --- .../kickc/icl/Pass2ConstantAdditionElimination.java | 9 ++++++++- src/dk/camelot64/kickc/icl/Scope.java | 12 +++++++++--- src/dk/camelot64/kickc/test/Main.java | 2 +- src/dk/camelot64/kickc/test/callsum.kc | 8 ++++++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/dk/camelot64/kickc/icl/Pass2ConstantAdditionElimination.java b/src/dk/camelot64/kickc/icl/Pass2ConstantAdditionElimination.java index 297ab159b..b8cf16d29 100644 --- a/src/dk/camelot64/kickc/icl/Pass2ConstantAdditionElimination.java +++ b/src/dk/camelot64/kickc/icl/Pass2ConstantAdditionElimination.java @@ -1,7 +1,14 @@ package dk.camelot64.kickc.icl; /** - * Compiler Pass eliminating several additions of constants by consolidating them to a single (compile time) constant c1+v+c2 => (c1+c2)+v + * Compiler Pass eliminating several additions of constants by consolidating them to a single (compile time) constant c1+v+c2 => (c1+c2)+v + * + * TODO: + * - If sub variable is used in other places consolidation is not allowed! + * Example: With only a & b the constants c1 & c2 could be consolidated to b=(c1+c2)+a, a=v. Since c also uses a this is not an option. + * a = c1 + v + * b = c2 + a + * c = a + c3 */ public class Pass2ConstantAdditionElimination extends Pass2SsaOptimization { diff --git a/src/dk/camelot64/kickc/icl/Scope.java b/src/dk/camelot64/kickc/icl/Scope.java index 4f877feb4..d3dbb9d81 100644 --- a/src/dk/camelot64/kickc/icl/Scope.java +++ b/src/dk/camelot64/kickc/icl/Scope.java @@ -104,14 +104,20 @@ public class Scope implements Symbol { if (pos >= 0) { String scopeName = name.substring(0, pos); String rest = name.substring(pos + 2); - Symbol scopeSym = symbols.get(scopeName); + Symbol scopeSym = getSymbol(scopeName); if (scopeSym instanceof Scope) { return ((Scope) scopeSym).getSymbol(rest); } else { throw new RuntimeException("Error looking up symbol " + name); } } else { - return symbols.get(name); + Symbol symbol = symbols.get(name); + if(symbol==null) { + if(parentScope!=null) { + symbol = parentScope.getSymbol(name); + } + } + return symbol; } } @@ -159,7 +165,7 @@ public class Scope implements Symbol { } public Procedure getProcedure(String name) { - Symbol symbol = symbols.get(name); + Symbol symbol = getSymbol(name); if (symbol != null && symbol instanceof Procedure) { return (Procedure) symbol; } else { diff --git a/src/dk/camelot64/kickc/test/Main.java b/src/dk/camelot64/kickc/test/Main.java index 5c9942634..5f7da3dbf 100644 --- a/src/dk/camelot64/kickc/test/Main.java +++ b/src/dk/camelot64/kickc/test/Main.java @@ -64,7 +64,7 @@ public class Main { List optimizations = new ArrayList<>(); optimizations.add(new Pass2CullEmptyBlocks(controlFlowGraph, programScope)); optimizations.add(new Pass2ConstantPropagation(controlFlowGraph, programScope)); - optimizations.add(new Pass2ConstantAdditionElimination(controlFlowGraph, programScope)); + //optimizations.add(new Pass2ConstantAdditionElimination(controlFlowGraph, programScope)); optimizations.add(new Pass2AliasElimination(controlFlowGraph, programScope)); optimizations.add(new Pass2RedundantPhiElimination(controlFlowGraph, programScope)); optimizations.add(new Pass2SelfPhiElimination(controlFlowGraph, programScope)); diff --git a/src/dk/camelot64/kickc/test/callsum.kc b/src/dk/camelot64/kickc/test/callsum.kc index a8f766cf8..5263ec3f9 100644 --- a/src/dk/camelot64/kickc/test/callsum.kc +++ b/src/dk/camelot64/kickc/test/callsum.kc @@ -1,10 +1,14 @@ byte a = 12; byte s = sum(5,a); -a = a+s; q +a = a+s; a = sum(s, a); byte sum(byte b1, byte b2) { byte b = b1+b2; - return b; + return inc(b); +} + +byte inc(byte b) { + return b+1; }