From 9dc17caab20a069bf1ff3ed71bca9e503eecbd3f Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Mon, 10 Apr 2023 11:37:50 +0200 Subject: [PATCH] Made statement loops simpler. --- .../java/dk/camelot64/kickc/model/Graph.java | 1 - .../kickc/passes/Pass2NopCastInlining.java | 24 ++++++++----------- .../kickc/passes/PassNAssertTypeMatch.java | 9 +++---- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/model/Graph.java b/src/main/java/dk/camelot64/kickc/model/Graph.java index ff36d2353..b4071a4f7 100644 --- a/src/main/java/dk/camelot64/kickc/model/Graph.java +++ b/src/main/java/dk/camelot64/kickc/model/Graph.java @@ -155,7 +155,6 @@ public interface Graph { void addStatementBeforeCall(Statement newStatement); - String toString(Program program, Graph graph); } } diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2NopCastInlining.java b/src/main/java/dk/camelot64/kickc/passes/Pass2NopCastInlining.java index 183278174..49f25b3e3 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass2NopCastInlining.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2NopCastInlining.java @@ -1,7 +1,5 @@ package dk.camelot64.kickc.passes; -import dk.camelot64.kickc.model.ControlFlowBlock; -import dk.camelot64.kickc.model.Graph; import dk.camelot64.kickc.model.Program; import dk.camelot64.kickc.model.statements.Statement; import dk.camelot64.kickc.model.statements.StatementAssignment; @@ -13,8 +11,11 @@ import dk.camelot64.kickc.model.values.CastValue; import dk.camelot64.kickc.model.values.RValue; import dk.camelot64.kickc.model.values.SymbolRef; import dk.camelot64.kickc.model.values.VariableRef; - -import java.util.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.ListIterator; +import java.util.Set; /** * Compiler Pass inlining cast assignments that has no effect (byte to/from signed byte, word to/from signed word) @@ -45,11 +46,9 @@ public class Pass2NopCastInlining extends Pass2SsaOptimization { ListIterator stmtIt = block.getStatements().listIterator(); while(stmtIt.hasNext()) { Statement stmt = stmtIt.next(); - if(stmt instanceof StatementAssignment) { - StatementAssignment assignment = (StatementAssignment) stmt; + if(stmt instanceof StatementAssignment assignment) { // TODO: Handle constant values? - if(assignment.getOperator() == null && assignment.getrValue2() instanceof CastValue) { - CastValue castValue = (CastValue) assignment.getrValue2(); + if(assignment.getOperator() == null && assignment.getrValue2() instanceof CastValue castValue) { SymbolType subValType = SymbolTypeInference.inferType(getProgramScope(), castValue.getValue()); final SymbolType castToType = castValue.getToType(); boolean isNopCast = isNopCast(castToType, subValType); @@ -59,10 +58,8 @@ public class Pass2NopCastInlining extends Pass2SsaOptimization { if(assignmentVar.isKindLoadStore()) continue; - boolean isIntermediateVar = false; - if(castValue.getValue() instanceof VariableRef && ((VariableRef) castValue.getValue()).isIntermediate()) { - isIntermediateVar = true; - } + boolean isIntermediateVar = castValue.getValue() instanceof VariableRef + && ((VariableRef) castValue.getValue()).isIntermediate(); if(isIntermediateVar) { if(mode == null || mode == Mode.VARS) { mode = Mode.VARS; @@ -91,8 +88,7 @@ public class Pass2NopCastInlining extends Pass2SsaOptimization { while(castVal instanceof CastValue) { castVal = ((CastValue) castVal).getValue(); } - if(castVal instanceof VariableRef) { - VariableRef castVarRef = (VariableRef) castVal; + if(castVal instanceof VariableRef castVarRef) { VariableRef lValueRef = (VariableRef) assignment.getlValue(); if(castVarRef.getScopeNames().equals(lValueRef.getScopeNames())) { // Same scope - optimize away diff --git a/src/main/java/dk/camelot64/kickc/passes/PassNAssertTypeMatch.java b/src/main/java/dk/camelot64/kickc/passes/PassNAssertTypeMatch.java index d437bbc16..85ea9764f 100644 --- a/src/main/java/dk/camelot64/kickc/passes/PassNAssertTypeMatch.java +++ b/src/main/java/dk/camelot64/kickc/passes/PassNAssertTypeMatch.java @@ -1,17 +1,18 @@ package dk.camelot64.kickc.passes; import dk.camelot64.kickc.model.CompileError; -import dk.camelot64.kickc.model.ControlFlowBlock; -import dk.camelot64.kickc.model.Graph; import dk.camelot64.kickc.model.Program; -import dk.camelot64.kickc.model.statements.Statement; import dk.camelot64.kickc.model.statements.StatementAssignment; import dk.camelot64.kickc.model.symbols.Variable; import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.types.SymbolTypeConversion; import dk.camelot64.kickc.model.types.SymbolTypeInference; import dk.camelot64.kickc.model.types.SymbolTypePointer; -import dk.camelot64.kickc.model.values.*; +import dk.camelot64.kickc.model.values.AssignmentRValue; +import dk.camelot64.kickc.model.values.ConstantInteger; +import dk.camelot64.kickc.model.values.ConstantValue; +import dk.camelot64.kickc.model.values.LValue; +import dk.camelot64.kickc.model.values.RValue; /** * Asserts that types match in all assignments and calculations