From 8ee1cf8b7eea1870c5fa92def4666664425d44aa Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Thu, 29 Aug 2019 08:54:38 +0200 Subject: [PATCH] Prepared for constant condition interval analysis --- .../kickc/passes/Pass2ConstantIfs.java | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIfs.java b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIfs.java index c4c5150e1..651d7ed8b 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIfs.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass2ConstantIfs.java @@ -37,27 +37,8 @@ public class Pass2ConstantIfs extends Pass2SsaOptimization { while(stmtIt.hasNext()) { Statement statement = stmtIt.next(); if(statement instanceof StatementConditionalJump) { - ConstantLiteral literal = null; StatementConditionalJump conditional = (StatementConditionalJump) statement; - ConstantValue constValue1 = Pass2ConstantIdentification.getConstant(conditional.getrValue1()); - Operator operator = conditional.getOperator(); - ConstantValue constValue2 = Pass2ConstantIdentification.getConstant(conditional.getrValue2()); - try { - if(conditional.getrValue1() == null && operator == null && constValue2 != null) { - // Constant condition - literal = constValue2.calculateLiteral(getScope()); - } else if(conditional.getrValue1() == null && operator != null && constValue2 != null) { - // Constant unary condition - ConstantValue constVal = Pass2ConstantIdentification.createUnary((OperatorUnary) operator, constValue2); - literal = constVal.calculateLiteral(getScope()); - } else if(constValue1 != null && operator != null && constValue2 != null) { - // Constant binary condition - ConstantValue constVal = Pass2ConstantIdentification.createBinary(constValue1, (OperatorBinary) operator, constValue2, getScope()); - literal = constVal.calculateLiteral(getScope()); - } - } catch (ConstantNotLiteral e) { - // not literal - keep as null - } + ConstantLiteral literal = getConditionLiteralValue(conditional); if(literal!=null && literal instanceof ConstantBool) { // Condition is a constant boolean if(((ConstantBool) literal).getBool()) { @@ -83,4 +64,43 @@ public class Pass2ConstantIfs extends Pass2SsaOptimization { return modified; } + /** + * If the condition always evaluates to constant true or false this finds tha value. + * @param conditional The conditional + * @return The literal value of the condition + */ + private ConstantLiteral getConditionLiteralValue(StatementConditionalJump conditional) { + ConstantValue constValue1 = Pass2ConstantIdentification.getConstant(conditional.getrValue1()); + Operator operator = conditional.getOperator(); + ConstantValue constValue2 = Pass2ConstantIdentification.getConstant(conditional.getrValue2()); + try { + + // If all values are constant find the literal condition value + + if(conditional.getrValue1() == null && operator == null && constValue2 != null) { + // Constant condition + return constValue2.calculateLiteral(getScope()); + } else if(conditional.getrValue1() == null && operator != null && constValue2 != null) { + // Constant unary condition + ConstantValue constVal = Pass2ConstantIdentification.createUnary((OperatorUnary) operator, constValue2); + return constVal.calculateLiteral(getScope()); + } else if(constValue1 != null && operator != null && constValue2 != null) { + // Constant binary condition + ConstantValue constVal = Pass2ConstantIdentification.createBinary(constValue1, (OperatorBinary) operator, constValue2, getScope()); + return constVal.calculateLiteral(getScope()); + } + + // Examine the intervals of the comparison parameters + + // TODO: Compare intervals based on the operator + + + + + } catch (ConstantNotLiteral e) { + // not literal - keep as null + } + return null; + } + }