mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-04-08 14:37:40 +00:00
Moved type inference into the model. Renamed ICL to model.
This commit is contained in:
parent
09ed57fb98
commit
efc750ce11
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
import dk.camelot64.kickc.parser.KickCLexer;
|
||||
import dk.camelot64.kickc.parser.KickCParser;
|
||||
import dk.camelot64.kickc.passes.*;
|
||||
|
@ -4,8 +4,7 @@ import dk.camelot64.kickc.NumberParser;
|
||||
import dk.camelot64.kickc.asm.*;
|
||||
import dk.camelot64.kickc.asm.parser.Asm6502BaseVisitor;
|
||||
import dk.camelot64.kickc.asm.parser.Asm6502Parser;
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.passes.Pass1TypeInference;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@ -376,7 +375,7 @@ public class AsmFragment {
|
||||
return name;
|
||||
}
|
||||
} else if(value instanceof ConstantValue) {
|
||||
SymbolType type = Pass1TypeInference.inferType(program.getScope(), (ConstantValue) value);
|
||||
SymbolType type = SymbolTypeInference.inferType(program.getScope(), (ConstantValue) value);
|
||||
if (SymbolTypeBasic.BYTE.equals(type)) {
|
||||
String name = "coby" + nextConstByteIdx++;
|
||||
bindings.put(name, value);
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** Signals some error in the code (or compilation) */
|
||||
public class CompileError extends RuntimeException {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** SSA form constant value */
|
||||
public interface Constant extends RValue {
|
@ -1,6 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
|
||||
import dk.camelot64.kickc.passes.Pass1TypeInference;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** A constant defined by a binary operator applied to two constants */
|
||||
public class ConstantBinary implements ConstantValue {
|
||||
@ -34,7 +32,7 @@ public class ConstantBinary implements ConstantValue {
|
||||
|
||||
@Override
|
||||
public SymbolType getType(ProgramScope scope) {
|
||||
return Pass1TypeInference.inferType(left.getType(scope), operator, right.getType(scope));
|
||||
return SymbolTypeInference.inferType(left.getType(scope), operator, right.getType(scope));
|
||||
}
|
||||
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/**
|
||||
* SSA form constant integer value
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/**
|
||||
* SSA form constant integer value
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/**
|
||||
* SSA form constant integer value
|
@ -1,6 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
|
||||
import dk.camelot64.kickc.passes.Pass1TypeInference;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** A constant defined by a unary operator appied to another constant */
|
||||
public class ConstantUnary implements ConstantValue {
|
||||
@ -26,7 +24,7 @@ public class ConstantUnary implements ConstantValue {
|
||||
|
||||
@Override
|
||||
public SymbolType getType(ProgramScope scope) {
|
||||
return Pass1TypeInference.inferType(operator, operand.getType(scope));
|
||||
return SymbolTypeInference.inferType(operator, operand.getType(scope));
|
||||
}
|
||||
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** A constant value. The value might be calculated through a constant expression. */
|
||||
public interface ConstantValue extends Constant {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.Collection;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** Assignable value (capable of being on the left part of an assignment)*/
|
||||
public interface LValue extends RValue {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** An Operator. The operation performed on the rvalues in a Statement. */
|
||||
public class Operator {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** A dereferenced pointer */
|
||||
public interface PointerDereference extends LValue {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** A dereferenced variable pointer plus an index (used for array writes)*/
|
||||
public class PointerDereferenceIndexed implements PointerDereference {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** A dereferenced pointer (based on a variable or a constant pointer)*/
|
||||
public class PointerDereferenceSimple implements PointerDereference {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,10 +1,9 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import dk.camelot64.kickc.CompileLog;
|
||||
import dk.camelot64.kickc.asm.AsmProgram;
|
||||
import dk.camelot64.kickc.passes.Pass1ModifiedVarsAnalysis;
|
||||
|
||||
/** A KickC Intermediate Compiler Language (ICL) Program */
|
||||
public class Program {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** A value usable as part of a calculation (ib the right side of an assignment)*/
|
||||
public interface RValue extends Value {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,7 +1,6 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.ibm.icu.text.NumberFormat;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** The different registers available for a program */
|
||||
public class Registers {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/**
|
||||
* Single Static Assignment Form Statement unconditional jump.
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/**
|
||||
* Single Static Assignment Form Statement with an LValuie - that is a statement assigning a value to a variable.
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/**
|
||||
* Single Static Assignment Form Statement Jump target.
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** Procedure declaration in SSA */
|
||||
public class StatementProcedureBegin extends StatementBase {
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/**
|
||||
* Procedure declaration in SSA
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,7 +1,6 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/** Basic Symbol Types */
|
124
src/main/java/dk/camelot64/kickc/model/SymbolTypeInference.java
Normal file
124
src/main/java/dk/camelot64/kickc/model/SymbolTypeInference.java
Normal file
@ -0,0 +1,124 @@
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
/** Type inference of expressions (rValues & unary/binary operators) */
|
||||
public class SymbolTypeInference {
|
||||
|
||||
public static SymbolType inferType(Operator operator, SymbolType subType) {
|
||||
if(operator==null) {
|
||||
return subType;
|
||||
}
|
||||
String op = operator.getOperator();
|
||||
switch (op) {
|
||||
case "*":
|
||||
if(subType instanceof SymbolTypePointer) {
|
||||
return ((SymbolTypePointer) subType).getElementType();
|
||||
} else {
|
||||
throw new RuntimeException("Type error: Dereferencing a non-pointer "+subType);
|
||||
}
|
||||
default:
|
||||
return subType;
|
||||
}
|
||||
}
|
||||
|
||||
public static SymbolType inferType(SymbolType type1, Operator operator, SymbolType type2) {
|
||||
String op = operator.getOperator();
|
||||
switch (op) {
|
||||
case "==":
|
||||
case "<":
|
||||
case "<=":
|
||||
case "=<":
|
||||
case ">":
|
||||
case ">=":
|
||||
case "=>":
|
||||
case "<>":
|
||||
case "!=":
|
||||
case "&&":
|
||||
case "||":
|
||||
case "and":
|
||||
case "or":
|
||||
return SymbolTypeBasic.BOOLEAN;
|
||||
case "+":
|
||||
case "-":
|
||||
if (type1 instanceof SymbolTypePointer && (type2.equals(SymbolTypeBasic.BYTE) || type2.equals(SymbolTypeBasic.WORD))) {
|
||||
return new SymbolTypePointer(((SymbolTypePointer) type1).getElementType());
|
||||
}
|
||||
if (type1 instanceof SymbolTypePointer && type2 instanceof SymbolTypePointer) {
|
||||
SymbolType elmType1 = ((SymbolTypePointer) type1).getElementType();
|
||||
SymbolType elmType2 = ((SymbolTypePointer) type2).getElementType();
|
||||
return inferType(elmType1, operator, elmType2);
|
||||
}
|
||||
if (SymbolTypeBasic.WORD.equals(type1) || SymbolTypeBasic.WORD.equals(type2)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1) && SymbolTypeBasic.BYTE.equals(type2)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "*":
|
||||
if(type1==null && type2 instanceof SymbolTypePointer) {
|
||||
return ((SymbolTypePointer) type2).getElementType();
|
||||
}
|
||||
if (SymbolTypeBasic.WORD.equals(type1) || SymbolTypeBasic.WORD.equals(type2)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1) && SymbolTypeBasic.BYTE.equals(type2)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "*idx":
|
||||
if(type1 instanceof SymbolTypePointer) {
|
||||
return ((SymbolTypePointer) type1).getElementType();
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "/":
|
||||
if (SymbolTypeBasic.WORD.equals(type1) || SymbolTypeBasic.WORD.equals(type2)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1) && SymbolTypeBasic.BYTE.equals(type2)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "<<":
|
||||
case ">>":
|
||||
if (SymbolTypeBasic.WORD.equals(type1)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
default:
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
}
|
||||
}
|
||||
|
||||
public static SymbolType inferType(ProgramScope programScope, RValue rValue) {
|
||||
SymbolType type = null;
|
||||
if (rValue instanceof VariableRef) {
|
||||
Variable variable = programScope.getVariable((VariableRef) rValue);
|
||||
type = variable.getType();
|
||||
} else if (rValue instanceof ConstantRef) {
|
||||
ConstantVar constVar = programScope.getConstant((ConstantRef) rValue);
|
||||
type = constVar.getType();
|
||||
} else if (rValue instanceof Symbol) {
|
||||
Symbol rSymbol = (Symbol) rValue;
|
||||
type = rSymbol.getType();
|
||||
} else if (rValue instanceof ConstantInteger) {
|
||||
ConstantInteger rInt = (ConstantInteger) rValue;
|
||||
return rInt.getType(programScope);
|
||||
} else if (rValue instanceof ConstantString) {
|
||||
type = SymbolTypeBasic.STRING;
|
||||
} else if (rValue instanceof ConstantBool) {
|
||||
type = SymbolTypeBasic.BOOLEAN;
|
||||
} else if (rValue instanceof ConstantUnary) {
|
||||
ConstantUnary constUnary = (ConstantUnary) rValue;
|
||||
SymbolType subType = inferType(programScope, constUnary.getOperand());
|
||||
return inferType(constUnary.getOperator(), subType);
|
||||
} else if (rValue instanceof ConstantBinary) {
|
||||
ConstantBinary constBin = (ConstantBinary) rValue;
|
||||
SymbolType leftType = inferType(programScope, constBin.getLeft());
|
||||
SymbolType rightType = inferType(programScope, constBin.getRight());
|
||||
return inferType(leftType, constBin.getOperator(), rightType);
|
||||
}
|
||||
if (type == null) {
|
||||
throw new RuntimeException("Cannot infer type for " + rValue);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
@ -1,7 +1,6 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/**
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl;
|
||||
package dk.camelot64.kickc.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@ -1,4 +1,4 @@
|
||||
package dk.camelot64.kickc.icl.jackson;
|
||||
package dk.camelot64.kickc.model.jackson;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
@ -1,7 +1,7 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.NumberParser;
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
import dk.camelot64.kickc.parser.KickCBaseVisitor;
|
||||
import dk.camelot64.kickc.parser.KickCParser;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
@ -1,7 +1,7 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.CompileLog;
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
@ -1,7 +1,7 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.CompileLog;
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
@ -2,7 +2,7 @@ package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.CompileLog;
|
||||
import dk.camelot64.kickc.NumberParser;
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
import dk.camelot64.kickc.parser.KickCBaseVisitor;
|
||||
import dk.camelot64.kickc.parser.KickCParser;
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -1,10 +1,7 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
@ -42,14 +42,14 @@ public class Pass1TypeInference {
|
||||
if (operator == null || assignment.getrValue1() == null) {
|
||||
// Copy operation or Unary operation
|
||||
RValue rValue = assignment.getrValue2();
|
||||
SymbolType subType = inferType(programScope, rValue);
|
||||
SymbolType type = inferType(operator, subType);
|
||||
SymbolType subType = SymbolTypeInference.inferType(programScope, rValue);
|
||||
SymbolType type = SymbolTypeInference.inferType(operator, subType);
|
||||
symbol.setTypeInferred(type);
|
||||
} else {
|
||||
// Binary operation
|
||||
SymbolType type1 = inferType(programScope, assignment.getrValue1());
|
||||
SymbolType type2 = inferType(programScope, assignment.getrValue2());
|
||||
SymbolType type = inferType(type1, operator, type2);
|
||||
SymbolType type1 = SymbolTypeInference.inferType(programScope, assignment.getrValue1());
|
||||
SymbolType type2 = SymbolTypeInference.inferType(programScope, assignment.getrValue2());
|
||||
SymbolType type = SymbolTypeInference.inferType(type1, operator, type2);
|
||||
symbol.setTypeInferred(type);
|
||||
}
|
||||
}
|
||||
@ -71,123 +71,4 @@ public class Pass1TypeInference {
|
||||
}
|
||||
}
|
||||
|
||||
public static SymbolType inferType(Operator operator, SymbolType subType) {
|
||||
if(operator==null) {
|
||||
return subType;
|
||||
}
|
||||
String op = operator.getOperator();
|
||||
switch (op) {
|
||||
case "*":
|
||||
if(subType instanceof SymbolTypePointer) {
|
||||
return ((SymbolTypePointer) subType).getElementType();
|
||||
} else {
|
||||
throw new RuntimeException("Type error: Dereferencing a non-pointer "+subType);
|
||||
}
|
||||
default:
|
||||
return subType;
|
||||
}
|
||||
}
|
||||
|
||||
public static SymbolType inferType(SymbolType type1, Operator operator, SymbolType type2) {
|
||||
String op = operator.getOperator();
|
||||
switch (op) {
|
||||
case "==":
|
||||
case "<":
|
||||
case "<=":
|
||||
case "=<":
|
||||
case ">":
|
||||
case ">=":
|
||||
case "=>":
|
||||
case "<>":
|
||||
case "!=":
|
||||
case "&&":
|
||||
case "||":
|
||||
case "and":
|
||||
case "or":
|
||||
return SymbolTypeBasic.BOOLEAN;
|
||||
case "+":
|
||||
case "-":
|
||||
if (type1 instanceof SymbolTypePointer && (type2.equals(SymbolTypeBasic.BYTE) || type2.equals(SymbolTypeBasic.WORD))) {
|
||||
return new SymbolTypePointer(((SymbolTypePointer) type1).getElementType());
|
||||
}
|
||||
if (type1 instanceof SymbolTypePointer && type2 instanceof SymbolTypePointer) {
|
||||
SymbolType elmType1 = ((SymbolTypePointer) type1).getElementType();
|
||||
SymbolType elmType2 = ((SymbolTypePointer) type2).getElementType();
|
||||
return inferType(elmType1, operator, elmType2);
|
||||
}
|
||||
if (SymbolTypeBasic.WORD.equals(type1) || SymbolTypeBasic.WORD.equals(type2)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1) && SymbolTypeBasic.BYTE.equals(type2)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "*":
|
||||
if(type1==null && type2 instanceof SymbolTypePointer) {
|
||||
return ((SymbolTypePointer) type2).getElementType();
|
||||
}
|
||||
if (SymbolTypeBasic.WORD.equals(type1) || SymbolTypeBasic.WORD.equals(type2)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1) && SymbolTypeBasic.BYTE.equals(type2)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "*idx":
|
||||
if(type1 instanceof SymbolTypePointer) {
|
||||
return ((SymbolTypePointer) type1).getElementType();
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "/":
|
||||
if (SymbolTypeBasic.WORD.equals(type1) || SymbolTypeBasic.WORD.equals(type2)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1) && SymbolTypeBasic.BYTE.equals(type2)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
case "<<":
|
||||
case ">>":
|
||||
if (SymbolTypeBasic.WORD.equals(type1)) {
|
||||
return SymbolTypeBasic.WORD;
|
||||
} else if (SymbolTypeBasic.BYTE.equals(type1)) {
|
||||
return SymbolTypeBasic.BYTE;
|
||||
}
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
default:
|
||||
throw new RuntimeException("Type inference case not handled " + type1 + " " + operator + " " + type2);
|
||||
}
|
||||
}
|
||||
|
||||
public static SymbolType inferType(ProgramScope programScope, RValue rValue) {
|
||||
SymbolType type = null;
|
||||
if (rValue instanceof VariableRef) {
|
||||
Variable variable = programScope.getVariable((VariableRef) rValue);
|
||||
type = variable.getType();
|
||||
} else if (rValue instanceof ConstantRef) {
|
||||
ConstantVar constVar = programScope.getConstant((ConstantRef) rValue);
|
||||
type = constVar.getType();
|
||||
} else if (rValue instanceof Symbol) {
|
||||
Symbol rSymbol = (Symbol) rValue;
|
||||
type = rSymbol.getType();
|
||||
} else if (rValue instanceof ConstantInteger) {
|
||||
ConstantInteger rInt = (ConstantInteger) rValue;
|
||||
return rInt.getType(programScope);
|
||||
} else if (rValue instanceof ConstantString) {
|
||||
type = SymbolTypeBasic.STRING;
|
||||
} else if (rValue instanceof ConstantBool) {
|
||||
type = SymbolTypeBasic.BOOLEAN;
|
||||
} else if (rValue instanceof ConstantUnary) {
|
||||
ConstantUnary constUnary = (ConstantUnary) rValue;
|
||||
SymbolType subType = inferType(programScope, constUnary.getOperand());
|
||||
return inferType(constUnary.getOperator(), subType);
|
||||
} else if (rValue instanceof ConstantBinary) {
|
||||
ConstantBinary constBin = (ConstantBinary) rValue;
|
||||
SymbolType leftType = inferType(programScope, constBin.getLeft());
|
||||
SymbolType rightType = inferType(programScope, constBin.getRight());
|
||||
return inferType(leftType, constBin.getOperator(), rightType);
|
||||
}
|
||||
if (type == null) {
|
||||
throw new RuntimeException("Cannot infer type for " + rValue);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -1,11 +1,8 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.ControlFlowGraphBaseVisitor;
|
||||
import dk.camelot64.kickc.icl.Program;
|
||||
import dk.camelot64.kickc.icl.RValue;
|
||||
import dk.camelot64.kickc.icl.StatementCall;
|
||||
|
||||
import java.util.List;
|
||||
import dk.camelot64.kickc.model.ControlFlowGraphBaseVisitor;
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.StatementCall;
|
||||
|
||||
/** Asserts that the program does not contain calls with lValues */
|
||||
public class Pass2AssertNoCallLvalues extends Pass2SsaAssertion {
|
||||
|
@ -1,8 +1,7 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/** Asserts that the program does not contain calls with parameters */
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
/** Asserts that the graph contains no label statements */
|
||||
public class Pass2AssertNoLabels extends Pass2SsaAssertion {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
/** Asserts that the graph contains no proc/endproc statements */
|
||||
public class Pass2AssertNoProcs extends Pass2SsaAssertion {
|
||||
|
@ -1,8 +1,8 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.ControlFlowGraphBaseVisitor;
|
||||
import dk.camelot64.kickc.icl.Program;
|
||||
import dk.camelot64.kickc.icl.StatementReturn;
|
||||
import dk.camelot64.kickc.model.ControlFlowGraphBaseVisitor;
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.StatementReturn;
|
||||
|
||||
/** Asserts that the program does not contain returns with values (as they have been replaced with assignments) */
|
||||
public class Pass2AssertNoReturnValues extends Pass2SsaAssertion {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.CompileLog;
|
||||
import dk.camelot64.kickc.icl.ControlFlowGraph;
|
||||
import dk.camelot64.kickc.icl.Program;
|
||||
import dk.camelot64.kickc.icl.ProgramScope;
|
||||
import dk.camelot64.kickc.model.ControlFlowGraph;
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.ProgramScope;
|
||||
|
||||
/** Base class for a compiler pass */
|
||||
public class Pass2Base {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -1,7 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.CompileLog;
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.passes;
|
||||
|
||||
import dk.camelot64.kickc.icl.*;
|
||||
import dk.camelot64.kickc.model.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user