1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-10-11 12:23:45 +00:00

Removed ConstantValueCalculator.

This commit is contained in:
jespergravgaard 2018-03-10 21:11:24 +01:00
parent 18335aa29a
commit ec791672ce
11 changed files with 20 additions and 62 deletions

View File

@ -1,38 +0,0 @@
package dk.camelot64.kickc.model;
import dk.camelot64.kickc.model.values.*;
import dk.camelot64.kickc.model.symbols.ConstantVar;
import dk.camelot64.kickc.model.symbols.ProgramScope;
/** Can calculate the exact value for constants (used for type inference). */
public class ConstantValueCalculator {
public static ConstantLiteral calcValue(ProgramScope programScope, ConstantValue value) {
if(value instanceof ConstantInteger) {
return (ConstantLiteral) value;
} else if(value instanceof ConstantString) {
return (ConstantLiteral) value;
} else if(value instanceof ConstantChar) {
return (ConstantLiteral) value;
} else if(value instanceof ConstantRef) {
ConstantVar constantVar = programScope.getConstant((ConstantRef) value);
ConstantValue constantVarValue = constantVar.getValue();
return calcValue(programScope, constantVarValue);
} else if(value instanceof ConstantUnary) {
ConstantUnary unary = (ConstantUnary) value;
return unary.getOperator().calculate(calcValue(programScope, unary.getOperand()));
} else if(value instanceof ConstantBinary) {
ConstantBinary binary = (ConstantBinary) value;
return binary.getOperator().calculate(calcValue(programScope, binary.getLeft()), calcValue(programScope, binary.getRight()));
} else if(value instanceof ConstantArrayList) {
// Cannot calculate value of inline array
throw new ConstantNotLiteral("Not literal "+value.toString());
} else if(value instanceof ConstantArrayFilled) {
// Cannot calculate value of inline array
throw new ConstantNotLiteral("Not literal "+value.toString());
} else {
throw new RuntimeException("Unknown constant value " + value);
}
}
}

View File

@ -2,16 +2,15 @@ package dk.camelot64.kickc.model.types;
import dk.camelot64.kickc.model.CompileError;
import dk.camelot64.kickc.model.ConstantNotLiteral;
import dk.camelot64.kickc.model.ConstantValueCalculator;
import dk.camelot64.kickc.model.operators.Operator;
import dk.camelot64.kickc.model.operators.OperatorBinary;
import dk.camelot64.kickc.model.operators.OperatorUnary;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.statements.StatementAssignment;
import dk.camelot64.kickc.model.statements.StatementCall;
import dk.camelot64.kickc.model.statements.StatementLValue;
import dk.camelot64.kickc.model.operators.Operator;
import dk.camelot64.kickc.model.operators.Operators;
import dk.camelot64.kickc.model.values.*;
import dk.camelot64.kickc.model.symbols.*;
import dk.camelot64.kickc.model.values.*;
import java.util.ArrayList;
import java.util.Arrays;
@ -35,7 +34,7 @@ public class SymbolTypeInference {
if(rValue instanceof ConstantValue) {
ConstantValue value = null;
try {
value = operator.calculate(ConstantValueCalculator.calcValue(programScope, (ConstantValue) rValue));
value = operator.calculate(((ConstantValue) rValue).calculateLiteral(programScope));
} catch(ConstantNotLiteral e) {
value = null;
}
@ -71,8 +70,8 @@ public class SymbolTypeInference {
ConstantValue value = null;
try {
value = operator.calculate(
ConstantValueCalculator.calcValue(programScope, (ConstantValue) rValue1),
ConstantValueCalculator.calcValue(programScope, (ConstantValue) rValue2)
((ConstantValue) rValue1).calculateLiteral(programScope),
((ConstantValue) rValue2).calculateLiteral(programScope)
);
} catch(ConstantNotLiteral e) {
value = null;

View File

@ -33,8 +33,8 @@ public class ConstantArrayFilled implements ConstantValue {
}
@Override
public ConstantLiteral calculate(ProgramScope scope) {
throw new CompileError("Cannot calculate literal array");
public ConstantLiteral calculateLiteral(ProgramScope scope) {
throw new ConstantNotLiteral("Cannot calculate literal array");
}
@Override

View File

@ -35,8 +35,8 @@ public class ConstantArrayList implements ConstantValue {
}
@Override
public ConstantLiteral calculate(ProgramScope scope) {
throw new CompileError("Cannot calculate literal array");
public ConstantLiteral calculateLiteral(ProgramScope scope) {
throw new ConstantNotLiteral("Cannot calculate literal array");
}
@Override

View File

@ -1,7 +1,6 @@
package dk.camelot64.kickc.model.values;
import dk.camelot64.kickc.model.*;
import dk.camelot64.kickc.model.operators.Operator;
import dk.camelot64.kickc.model.operators.OperatorBinary;
import dk.camelot64.kickc.model.symbols.ProgramScope;
import dk.camelot64.kickc.model.types.SymbolType;
@ -38,8 +37,8 @@ public class ConstantBinary implements ConstantValue {
}
@Override
public ConstantLiteral calculate(ProgramScope scope) {
return operator.calculate(left.calculate(scope), right.calculate(scope));
public ConstantLiteral calculateLiteral(ProgramScope scope) {
return operator.calculate(left.calculateLiteral(scope), right.calculateLiteral(scope));
}
@Override

View File

@ -8,7 +8,7 @@ public interface ConstantLiteral<T> extends ConstantValue {
T getValue();
@Override
default ConstantLiteral calculate(ProgramScope scope) {
default ConstantLiteral calculateLiteral(ProgramScope scope) {
return this;
}
}

View File

@ -18,9 +18,9 @@ public class ConstantRef extends SymbolRef implements ConstantValue {
}
@Override
public ConstantLiteral calculate(ProgramScope scope) {
public ConstantLiteral calculateLiteral(ProgramScope scope) {
ConstantVar constantVar = scope.getConstant(this);
ConstantValue constantVarValue = constantVar.getValue();
return constantVarValue.calculate(scope);
return constantVarValue.calculateLiteral(scope);
}
}

View File

@ -1,7 +1,6 @@
package dk.camelot64.kickc.model.values;
import dk.camelot64.kickc.model.*;
import dk.camelot64.kickc.model.operators.Operator;
import dk.camelot64.kickc.model.operators.OperatorUnary;
import dk.camelot64.kickc.model.symbols.ProgramScope;
import dk.camelot64.kickc.model.types.SymbolType;
@ -30,8 +29,8 @@ public class ConstantUnary implements ConstantValue {
}
@Override
public ConstantLiteral calculate(ProgramScope scope) {
return operator.calculate(operand.calculate(scope));
public ConstantLiteral calculateLiteral(ProgramScope scope) {
return operator.calculate(operand.calculateLiteral(scope));
}
@Override

View File

@ -8,6 +8,6 @@ public interface ConstantValue extends RValue {
SymbolType getType(ProgramScope scope);
ConstantLiteral calculate(ProgramScope scope);
ConstantLiteral calculateLiteral(ProgramScope scope);
}

View File

@ -31,7 +31,7 @@ public class ConstantVarPointer implements ConstantValue {
}
@Override
public ConstantLiteral calculate(ProgramScope scope) {
public ConstantLiteral calculateLiteral(ProgramScope scope) {
throw new CompileError("Cannot calculate literal var pointer");
}

View File

@ -1,7 +1,6 @@
package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.*;
import dk.camelot64.kickc.model.operators.Operator;
import dk.camelot64.kickc.model.operators.OperatorBinary;
import dk.camelot64.kickc.model.operators.OperatorUnary;
import dk.camelot64.kickc.model.operators.Operators;
@ -62,7 +61,7 @@ public class Pass2ConstantIdentification extends Pass2SsaOptimization {
} else {
throw new CompileError(
"Constant variable has a non-matching type \n variable: " + variable.toString(getProgram()) +
"\n value: (" + valueType.toString() + ") " + ConstantValueCalculator.calcValue(getScope(), constVal) +
"\n value: (" + valueType.toString() + ") " + constVal.calculateLiteral(getScope()) +
"\n value definition: " + constVal.toString(getProgram()));
}
}