1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-02-03 09:31:40 +00:00

Working on fixing test errors - 196/350 working.

This commit is contained in:
jespergravgaard 2019-05-07 21:03:26 +02:00
parent 47aa623f25
commit 82729cb4d9
6 changed files with 58 additions and 5 deletions

View File

@ -90,10 +90,17 @@ public class SymbolTypeConversion {
// Right is the number type - left is the fixed type
numberVal = right;
fixedType = (SymbolTypeIntegerFixed) leftType;
} else if(SymbolType.NUMBER.equals(leftType) && rightType instanceof SymbolTypePointer) {
// Left is the number type - right is a pointer (effectively unsigned word)
numberVal = left;
fixedType = SymbolType.WORD;
} else if(SymbolType.NUMBER.equals(rightType) && leftType instanceof SymbolTypePointer) {
// Right is the number type - left is a pointer (effectively unsigned word)
numberVal = right;
fixedType = SymbolType.WORD;
} else {
// Binary operator combining number and non-integer
return null;
//throw new CompileError("Error! Incompatible operands " + left.toString() + " and " + right.toString(), currentStmt);
}
if(numberVal instanceof ConstantValue) {

View File

@ -3,10 +3,12 @@ package dk.camelot64.kickc.passes;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.iterator.ProgramExpressionIterator;
import dk.camelot64.kickc.model.iterator.ProgramExpressionUnary;
import dk.camelot64.kickc.model.iterator.ProgramValueIterator;
import dk.camelot64.kickc.model.operators.OperatorCast;
import dk.camelot64.kickc.model.types.SymbolType;
import dk.camelot64.kickc.model.types.SymbolTypeIntegerFixed;
import dk.camelot64.kickc.model.types.SymbolTypePointer;
import dk.camelot64.kickc.model.values.ConstantCastValue;
import dk.camelot64.kickc.model.values.ConstantInteger;
import dk.camelot64.kickc.model.values.ConstantPointer;
@ -28,21 +30,46 @@ public class Pass2ConstantCastSimplification extends Pass2SsaOptimization {
ProgramExpressionUnary unary = (ProgramExpressionUnary) programExpression;
if(unary.getOperand() instanceof ConstantInteger) {
ConstantInteger constantInteger = (ConstantInteger) unary.getOperand();
if(constantInteger.getType().equals(SymbolType.NUMBER)) {
if(SymbolType.NUMBER.equals(constantInteger.getType())) {
SymbolType castType = operatorCast.getToType();
if(castType instanceof SymbolTypeIntegerFixed ) {
ConstantInteger newConstInt = new ConstantInteger(constantInteger.getInteger(), castType);
programExpression.set(newConstInt);
getLog().append("Simplifying constant integer cast " + newConstInt.toString());
optimized.set(true);
} else if(castType instanceof SymbolTypePointer) {
ConstantPointer newConstPointer = new ConstantPointer(constantInteger.getInteger(), ((SymbolTypePointer) castType).getElementType());
programExpression.set(newConstPointer);
getLog().append("Simplifying constant pointer cast " + newConstPointer.toString());
optimized.set(true);
}
}
}
}
});
ProgramValueIterator.execute(getProgram(), (programValue, currentStmt, stmtIt, currentBlock) -> {
if(programValue.get() instanceof ConstantCastValue) {
ConstantCastValue constantCastValue = (ConstantCastValue) programValue.get();
if(constantCastValue.getValue() instanceof ConstantInteger) {
ConstantInteger constantInteger = (ConstantInteger) constantCastValue.getValue();
if(SymbolType.NUMBER.equals(constantInteger.getType())) {
SymbolType castType = constantCastValue.getToType();
if(castType instanceof SymbolTypeIntegerFixed ) {
ConstantInteger newConstInt = new ConstantInteger(constantInteger.getInteger(), castType);
programValue.set(newConstInt);
getLog().append("Simplifying constant integer cast " + newConstInt.toString());
optimized.set(true);
} else if(castType instanceof SymbolTypePointer) {
ConstantPointer newConstPointer = new ConstantPointer(constantInteger.getInteger(), ((SymbolTypePointer) castType).getElementType());
programValue.set(newConstPointer);
getLog().append("Simplifying constant pointer cast " + newConstPointer.toString());
optimized.set(true);
}
}
}
}
});
return optimized.get();
}

View File

@ -73,6 +73,12 @@ public class Pass2ConstantIdentification extends Pass2SsaOptimization {
SymbolType valueType = SymbolTypeInference.inferType(getScope(), constVal);
SymbolType variableType = variable.getType();
if(!variableType.equals(SymbolType.NUMBER) && valueType.equals(SymbolType.NUMBER)) {
// Value is number - wait til it is cast to a proper type
constants.remove(constRef);
continue;
}
if(!SymbolTypeMatch.assignmentTypeMatch(variableType, valueType)) {
throw new CompileError(
"Constant variable has a non-matching type \n variable: " + variable.toString(getProgram()) +

View File

@ -6,6 +6,7 @@ import dk.camelot64.kickc.model.iterator.ProgramExpressionIterator;
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.RValue;
/**
@ -28,12 +29,12 @@ public class PassNAddNumberTypeConversions extends Pass2SsaOptimization {
if(conversionType != null) {
// Convert both left and right to the found type
SymbolType leftType = SymbolTypeInference.inferType(getProgram().getScope(), left);
if(!leftType.equals(conversionType)) {
if(!leftType.equals(conversionType) && !(leftType instanceof SymbolTypePointer)) {
getLog().append("Adding number conversion cast (" + conversionType + ") " + binary.getLeft().toString() + " in " + currentStmt.toString(getProgram(), false));
binary.addLeftCast(conversionType, stmtIt, currentBlock.getScope(), getScope());
}
SymbolType rightType = SymbolTypeInference.inferType(getProgram().getScope(), right);
if(!rightType.equals(conversionType)) {
if(!rightType.equals(conversionType) && !(rightType instanceof SymbolTypePointer)) {
getLog().append("Adding number conversion cast (" + conversionType + ") " + binary.getRight().toString() + " in " + currentStmt.toString(getProgram(), false));
binary.addRightCast(conversionType, stmtIt, currentBlock.getScope(), getScope());
}
@ -43,5 +44,4 @@ public class PassNAddNumberTypeConversions extends Pass2SsaOptimization {
return false;
}
}

View File

@ -103,6 +103,11 @@ public class TestPrograms {
compileAndCompare("type-signed");
}
@Test
public void testConstIntCastProblem() throws IOException, URISyntaxException {
compileAndCompare("const-int-cast-problem");
}
@Test
public void testPointerPlus0() throws IOException, URISyntaxException {
compileAndCompare("pointer-plus-0");

View File

@ -0,0 +1,8 @@
// Test a problem with converting casted constant numbers to fixed type constant integers
const byte* SCREEN = $0400;
void main() {
for( byte i: 121..122) {
SCREEN[i] = i>>4;
}
}