Commit a934407e authored by Tom Zhao's avatar Tom Zhao
Browse files

xz1919: all test pass except function for intel isa

parent 5de2fbcd
......@@ -262,7 +262,7 @@ public class IntelInstructionGenerator extends InstructionGenerator<IntelInstruc
for (int i = paramNum - 1; i >= 0; i--) {
ExprNode expr = params.get(i);
visit(expr);
Register reg = intelRegAllocator.curr();
IntelConcreteRegister reg = intelRegAllocator.curr();
int size = expr.getType().getSize();
instructions.add(new Sub(size, IntelInstructionSize.Q, rbp));
instructions.add(new Mov(reg.withSize(intToIntelSize.get(size)), new IntelAddress(rbp)));
......
......@@ -628,7 +628,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
semanticError |= typeCheck(ctx.expr(0), BOOL_BASIC_TYPE, expr1Type);
semanticError |= typeCheck(ctx.expr(1), BOOL_BASIC_TYPE, expr2Type);
return new BinopNode(expr1, expr2, Binop.AND);
return new BinopNode(expr1, expr2, Binop.AND, this.arch);
}
@Override
......@@ -641,7 +641,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
semanticError |= typeCheck(ctx.expr(0), BOOL_BASIC_TYPE, expr1Type);
semanticError |= typeCheck(ctx.expr(1), BOOL_BASIC_TYPE, expr2Type);
return new BinopNode(expr1, expr2, Binop.OR);
return new BinopNode(expr1, expr2, Binop.OR, this.arch);
}
@Override
......@@ -794,7 +794,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
@Override
public Node visitBoolExpr(BoolExprContext ctx) {
return new BoolNode(ctx.BOOL_LITER().getText().equals("true"));
return new BoolNode(ctx.BOOL_LITER().getText().equals("true"), this.arch);
}
/* pairExpr is basically 'null', extend 'null' to represent uninitialised struct */
......@@ -810,9 +810,9 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
* text for escChar like '\0' is \'\\0\' length is 4 */
assert text.length() == 3 || text.length() == 4;
if (text.length() == 3) {
return new CharNode(text.charAt(1));
return new CharNode(text.charAt(1), this.arch);
} else {
return new CharNode(escCharMap.get(text.charAt(2)));
return new CharNode(escCharMap.get(text.charAt(2)), this.arch);
}
}
......@@ -830,7 +830,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
semanticError |= typeCheck(ctx.expr(1), cmpStatAllowedTypes, expr2Type);
semanticError |= typeCheck(ctx.expr(0), expr1Type, expr2Type);
return new BinopNode(expr1, expr2, binop);
return new BinopNode(expr1, expr2, binop, this.arch);
}
@Override
......@@ -845,7 +845,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
semanticError |= typeCheck(ctx.expr(0), exrp1Type, expr2Type);
return new BinopNode(expr1, expr2, binop);
return new BinopNode(expr1, expr2, binop, this.arch);
}
@Override
......@@ -860,7 +860,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
@Override
public Node visitIntExpr(IntExprContext ctx) {
return new IntegerNode(intParse(ctx, ctx.INT_LITER().getText()));
return new IntegerNode(intParse(ctx, ctx.INT_LITER().getText()), this.arch);
}
@Override
......@@ -912,7 +912,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
semanticError |= typeCheck(ctx.expr(0), INT_BASIC_TYPE, expr1Type);
semanticError |= typeCheck(ctx.expr(1), INT_BASIC_TYPE, expr2Type);
return new BinopNode(expr1, expr2, binop);
return new BinopNode(expr1, expr2, binop, this.arch);
}
@Override
......@@ -922,7 +922,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
@Override
public Node visitStrExpr(StrExprContext ctx) {
return new StringNode(ctx.STR_LITER().getText());
return new StringNode(ctx.STR_LITER().getText(), this.arch);
}
/* visit UnopExpr with a special case on the MINUS sign being the negative sign of an integer */
......@@ -936,7 +936,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
String exprText = ctx.expr().getText();
if (unop.equals(Unop.MINUS) && isInteger(exprText)) {
Integer intVal = intParse(ctx.expr(), "-" + exprText);
return new IntegerNode(intVal);
return new IntegerNode(intVal, this.arch);
}
/* Check the range of integer in the chr unary operator */
......@@ -951,7 +951,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
Type exprType = expr.getType();
semanticError |= typeCheck(ctx.expr(), targetType, exprType);
return new UnopNode(expr, unop);
return new UnopNode(expr, unop, this.arch);
}
/* =======================================================
......
......@@ -3,6 +3,7 @@ package frontend.node.expr;
import frontend.type.BasicType;
import frontend.type.BasicTypeEnum;
import utils.NodeVisitor;
import utils.Utils.AssemblyArchitecture;
public class BinopNode extends ExprNode {
......@@ -23,7 +24,7 @@ public class BinopNode extends ExprNode {
private ExprNode expr2;
private Binop operator;
public BinopNode(ExprNode expr1, ExprNode expr2, Binop operator) {
public BinopNode(ExprNode expr1, ExprNode expr2, Binop operator, AssemblyArchitecture arch) {
this.expr1 = expr1;
this.expr2 = expr2;
this.operator = operator;
......@@ -33,10 +34,10 @@ public class BinopNode extends ExprNode {
case MUL:
case DIV:
case MOD:
type = new BasicType(BasicTypeEnum.INTEGER);
type = new BasicType(BasicTypeEnum.INTEGER, arch);
break;
default:
type = new BasicType(BasicTypeEnum.BOOLEAN);
type = new BasicType(BasicTypeEnum.BOOLEAN, arch);
}
weight = expr1.getWeight() + expr2.getWeight() + 2;
}
......
......@@ -3,6 +3,7 @@ package frontend.node.expr;
import frontend.type.BasicType;
import frontend.type.BasicTypeEnum;
import utils.NodeVisitor;
import utils.Utils.AssemblyArchitecture;
public class BoolNode extends ExprNode {
......@@ -13,9 +14,9 @@ public class BoolNode extends ExprNode {
private final boolean val;
public BoolNode(boolean val) {
public BoolNode(boolean val, AssemblyArchitecture arch) {
this.val = val;
this.type = new BasicType(BasicTypeEnum.BOOLEAN);
this.type = new BasicType(BasicTypeEnum.BOOLEAN, arch);
this.weight = 1;
}
......
......@@ -3,6 +3,7 @@ package frontend.node.expr;
import frontend.type.BasicType;
import frontend.type.BasicTypeEnum;
import utils.NodeVisitor;
import utils.Utils.AssemblyArchitecture;
public class CharNode extends ExprNode {
......@@ -13,9 +14,9 @@ public class CharNode extends ExprNode {
private final char val;
public CharNode(char c) {
public CharNode(char c, AssemblyArchitecture arch) {
this.val = c;
this.type = new BasicType(BasicTypeEnum.CHAR);
this.type = new BasicType(BasicTypeEnum.CHAR, arch);
this.weight = 1;
}
......
......@@ -3,6 +3,7 @@ package frontend.node.expr;
import frontend.type.BasicType;
import frontend.type.BasicTypeEnum;
import utils.NodeVisitor;
import utils.Utils.AssemblyArchitecture;
public class IntegerNode extends ExprNode {
......@@ -13,9 +14,9 @@ public class IntegerNode extends ExprNode {
private final int val;
public IntegerNode(int val) {
public IntegerNode(int val, AssemblyArchitecture arch) {
this.val = val;
this.type = new BasicType(BasicTypeEnum.INTEGER);
this.type = new BasicType(BasicTypeEnum.INTEGER, arch);
this.weight = 1;
}
......
......@@ -3,6 +3,7 @@ package frontend.node.expr;
import frontend.type.BasicType;
import frontend.type.BasicTypeEnum;
import utils.NodeVisitor;
import utils.Utils.AssemblyArchitecture;
public class StringNode extends ExprNode {
......@@ -13,9 +14,9 @@ public class StringNode extends ExprNode {
private final String string;
public StringNode(String string) {
public StringNode(String string, AssemblyArchitecture arch) {
this.string = string;
this.type = new BasicType(BasicTypeEnum.STRING);
this.type = new BasicType(BasicTypeEnum.STRING, arch);
this.weight = 1;
}
......
......@@ -3,6 +3,7 @@ package frontend.node.expr;
import frontend.type.BasicType;
import frontend.type.BasicTypeEnum;
import utils.NodeVisitor;
import utils.Utils.AssemblyArchitecture;
public class UnopNode extends ExprNode {
......@@ -18,20 +19,20 @@ public class UnopNode extends ExprNode {
ExprNode expr;
Unop operator;
public UnopNode(ExprNode expr, Unop operator) {
public UnopNode(ExprNode expr, Unop operator, AssemblyArchitecture arch) {
this.expr = expr;
this.operator = operator;
switch (operator) {
case NOT:
type = new BasicType(BasicTypeEnum.BOOLEAN);
type = new BasicType(BasicTypeEnum.BOOLEAN, arch);
break;
case LEN:
case MINUS:
case ORD:
type = new BasicType(BasicTypeEnum.INTEGER);
type = new BasicType(BasicTypeEnum.INTEGER, arch);
break;
case CHR:
type = new BasicType(BasicTypeEnum.CHAR);
type = new BasicType(BasicTypeEnum.CHAR, arch);
break;
}
}
......
......@@ -81,7 +81,7 @@ public class ArrayType implements Type {
public int hashCode() {
int hash = ARRAY_HASH_CODE;
if (contentType != null &&
contentType.equalToType(new BasicType(BasicTypeEnum.CHAR))) {
contentType.equalToType(new BasicType(BasicTypeEnum.CHAR, arch))) {
hash += BYTE_SIZE;
} else {
hash += arch.equals(AssemblyArchitecture.ARMv6) ? ARM_POINTER_SIZE : INTEL_POINTER_SIZE;
......
......@@ -2,12 +2,16 @@ package frontend.type;
import static utils.Utils.*;
import utils.Utils;
public class BasicType implements Type {
private final BasicTypeEnum basicTypeEnum;
private final AssemblyArchitecture arch;
public BasicType(BasicTypeEnum basicTypeEnum) {
public BasicType(BasicTypeEnum basicTypeEnum, AssemblyArchitecture arch) {
this.basicTypeEnum = basicTypeEnum;
this.arch = arch;
}
@Override
......@@ -22,8 +26,6 @@ public class BasicType implements Type {
return basicTypeEnum.equals(((BasicType) other).getTypeEnum());
}
public BasicTypeEnum getTypeEnum() {
return basicTypeEnum;
}
......@@ -47,8 +49,7 @@ public class BasicType implements Type {
case INTEGER:
return WORD_SIZE;
case STRING:
// todo: change returned value to arch.equals(AssemblyArchitecture.ARMv6) ? Utils.ARM_POINTER_SIZE : Utils.INTEL_POINTER_SIZE
return QUAD_SIZE;
return arch.equals(AssemblyArchitecture.ARMv6) ? Utils.ARM_POINTER_SIZE : Utils.INTEL_POINTER_SIZE;
default:
throw new IllegalArgumentException("unsupported base type enum: " + basicTypeEnum);
}
......
......@@ -46,7 +46,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
/* if either of the nodes is not immediate, stop constant propagation
* return a node with so far the simplified form */
if (!expr1.isImmediate() || !expr2.isImmediate()) {
return new BinopNode(expr1, expr2, node.getOperator());
return new BinopNode(expr1, expr2, node.getOperator(), AssemblyArchitecture.ARMv6);
}
/* apply arithmetic evaluation */
......@@ -54,14 +54,14 @@ public class ConstantPropagation implements NodeVisitor<Node> {
ExprNode simpChild = arithmeticApplyMap.get(node.getOperator()).apply(
expr1.getCastedVal(),
expr2.getCastedVal());
return simpChild == null ? new BinopNode(expr1, expr2, node.getOperator()) : simpChild;
return simpChild == null ? new BinopNode(expr1, expr2, node.getOperator(), AssemblyArchitecture.ARMv6) : simpChild;
}
/* otherwise, have to be binop covered by cmpMap key */
assert cmpMap.containsKey(node.getOperator());
boolean val = cmpMap.get(node.getOperator()).apply(expr1.getCastedVal(), expr2.getCastedVal());
return new BoolNode(val);
return new BoolNode(val, AssemblyArchitecture.ARMv6);
}
@Override
......@@ -128,7 +128,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
ExprNode expr = visit(node.getExpr()).asExprNode();
ExprNode simpChild = unopApplyMap.get(node.getOperator()).apply(expr);
return simpChild == null ?
new UnopNode(expr, node.getOperator()) :
new UnopNode(expr, node.getOperator(), AssemblyArchitecture.ARMv6) :
simpChild;
}
......
......@@ -30,10 +30,10 @@ public class Utils {
*/
/* Type classes to represent BasicType, ArrayType, and PairType, used in type comparisons throughout the SemanticChecker */
public static final Type INT_BASIC_TYPE = new BasicType(BasicTypeEnum.INTEGER);
public static final Type BOOL_BASIC_TYPE = new BasicType(BasicTypeEnum.BOOLEAN);
public static final Type CHAR_BASIC_TYPE = new BasicType(BasicTypeEnum.CHAR);
public static final Type STRING_BASIC_TYPE = new BasicType(BasicTypeEnum.STRING);
public static final Type INT_BASIC_TYPE = new BasicType(BasicTypeEnum.INTEGER, AssemblyArchitecture.ARMv6);
public static final Type BOOL_BASIC_TYPE = new BasicType(BasicTypeEnum.BOOLEAN, AssemblyArchitecture.ARMv6);
public static final Type CHAR_BASIC_TYPE = new BasicType(BasicTypeEnum.CHAR, AssemblyArchitecture.ARMv6);
public static final Type STRING_BASIC_TYPE = new BasicType(BasicTypeEnum.STRING, AssemblyArchitecture.ARMv6);
public static final Type ARRAY_TYPE = new ArrayType(AssemblyArchitecture.ARMv6);
public static final Type PAIR_TYPE = new PairType(AssemblyArchitecture.ARMv6);
public static final Type STRUCT_TYPE = new StructType("", AssemblyArchitecture.ARMv6);
......@@ -97,8 +97,8 @@ public class Utils {
Binop.PLUS, ((x, y) -> arithmeticWithCheck(x, y, Math::addExact)),
Binop.MINUS, ((x, y) -> arithmeticWithCheck(x, y, Math::subtractExact)),
Binop.MUL, ((x, y) -> arithmeticWithCheck(x, y, Math::multiplyExact)),
Binop.DIV, ((x, y) -> new IntegerNode(x / y)),
Binop.MOD, ((x, y) -> new IntegerNode(x % y))
Binop.DIV, ((x, y) -> new IntegerNode(x / y, AssemblyArchitecture.ARMv6)),
Binop.MOD, ((x, y) -> new IntegerNode(x % y, AssemblyArchitecture.ARMv6))
);
public static final Map<Binop, BiFunction<Integer, Integer, Boolean>> cmpMap = Map.of(
......@@ -114,15 +114,15 @@ public class Utils {
public static final Map<Unop, Function<ExprNode, ExprNode>> unopApplyMap = Map.of(
Unop.MINUS, (x -> arithmeticWithCheck(0, x.getCastedVal(), Math::subtractExact)),
Unop.NOT, (x -> new BoolNode(x.getCastedVal() != 1)),
Unop.LEN, (x -> new IntegerNode(x.getCastedVal())),
Unop.ORD, (x -> new IntegerNode(x.getCastedVal())),
Unop.CHR, (x -> new CharNode((char) x.getCastedVal()))
Unop.NOT, (x -> new BoolNode(x.getCastedVal() != 1, AssemblyArchitecture.ARMv6)),
Unop.LEN, (x -> new IntegerNode(x.getCastedVal(), AssemblyArchitecture.ARMv6)),
Unop.ORD, (x -> new IntegerNode(x.getCastedVal(), AssemblyArchitecture.ARMv6)),
Unop.CHR, (x -> new CharNode((char) x.getCastedVal(), AssemblyArchitecture.ARMv6))
);
private static ExprNode arithmeticWithCheck(int a, int b, BinaryOperator<Integer> exactOperator) {
try {
return new IntegerNode(exactOperator.apply(a, b));
return new IntegerNode(exactOperator.apply(a, b), AssemblyArchitecture.ARMv6);
} catch (ArithmeticException e) {
System.out.println("WARNING: arithmetic " + " on " + a + " and " + b + " will cause overflow");
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment