📄 arith.java
字号:
package inter;import lexer.*; import symbols.*;public class Arith extends Op { public Expr expr1, expr2; public Arith(Token tok, Expr x1, Expr x2) { super(tok, null); expr1 = x1; expr2 = x2; type = Type.max(expr1.type, expr2.type); if (type == null ) error("type error"); } public Expr gen() { return new Arith(op, expr1.reduce(), expr2.reduce()); } public String toString() { //return expr1.toString()+" "+op.toString()+" "+expr2.toString(); String result = ""; result += expr1.toString(); result += raiseType(expr1); result += expr2.toString(); result += raiseType(expr2); if(type == Type.Int) { switch(this.op.tag) { case '+': result += "iadd\n"; break; case '-': result += "isub\n"; break; case '*': result += "imul\n"; break; case '/': result += "idiv\n"; } } else if(type == Type.Float) { switch(this.op.tag) { case '+': result += "fadd\n"; break; case '-': result += "fsub\n"; break; case '*': result += "fmul\n"; break; case '/': result += "fdiv\n"; } } else if(type == Type.Complex) { switch(this.op.tag) { case '+': result += "fstore " + LocalVarArray.curlength + "\n"; result += "swap\n"; result += "fload " + LocalVarArray.curlength + "\n"; result += "fadd\n"; result += "fstore " + LocalVarArray.curlength + "\n"; result += "fadd\n"; result += "fload " + LocalVarArray.curlength + "\n"; break; case '-': result += "fstore " + LocalVarArray.curlength + "\n"; result += "swap\n"; result += "fload " + LocalVarArray.curlength + "\n"; result += "fsub\n"; result += "fstore " + LocalVarArray.curlength + "\n"; result += "fsub\n"; result += "fload " + LocalVarArray.curlength + "\n"; break; case '*': result += "fstore " + LocalVarArray.curlength + "\n"; result += "swap\n"; result += "fload " + LocalVarArray.curlength + "\n"; //Now it's (i2,i1,r2,r1) result += "fmul\n"; //Multiply the imaginary parts result += "fstore " + LocalVarArray.curlength + "\n"; //Store them result += "fmul\n"; result += "fload " + LocalVarArray.curlength + "\n"; result += "fsub\n"; //Now that's the real part done (R) //Load them again result += expr1.toString(); result += raiseType(expr1); result += expr2.toString(); result += raiseType(expr2); result += "fstore " + LocalVarArray.curlength + "\n"; //(r2, i1, r1, R) result += "fmul\n"; result += "fload " + LocalVarArray.curlength + "\n"; //(i2, r2i1, r1, R) result += "swap\n"; //(r2i1, i2, r1, R) result += "fstore " + LocalVarArray.curlength + "\n"; //(i2, r1, R) result += "fmul\n"; //(i2r1, R) result += "fload " + LocalVarArray.curlength + "\n"; //(r2i1, i2r1, R) result += "fadd\n"; break; case '/': result += "idiv\n"; } } return result; } private String raiseType(Expr e) { String s = ""; if(type != e.type) { if(type == Type.Float) { if(e.type == Type.Int) { s += "i2f\n"; } } else if(type == Type.Complex) { if(e.type == Type.Int) { s += "i2f\n"; s += "ldc 0.0\n"; } else if(e.type == Type.Float) { s += "ldc 0.0\n"; } } } return s;}private String push(Expr i){ String result = ""; /* if(i instanceof Id || i instanceof Temp) { if(i.type == Type.Int) { result += "iload " + i.lvaIdx + "\n"; } else if(type == Type.Float) { result += "fload " + i.lvaIdx + "\n"; } else if(type == Type.Complex) { } //result += i.toString(); } else if(i instanceof Constant) { result += ((Constant) i).toString(); } */ return result;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -