binaryquad.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.lOr(c2));

                case LXOR:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.lXor(c2));

                case LSHL:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.lShl(c2));

                case LSHR:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.lShr(c2));

                case LUSHR:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.lUshr(c2));

                case FADD:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.fAdd(c2));

                case FSUB:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.fSub(c2));

                case FMUL:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.fMul(c2));

                case FDIV:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.fDiv(c2));

                case FREM:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.fRem(c2));

                case DADD:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.dAdd(c2));

                case DSUB:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.dSub(c2));

                case DMUL:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.dMul(c2));

                case DDIV:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.dDiv(c2));

                case DREM:
                    return new ConstantRefAssignQuad(this.getAddress(), this.getBasicBlock(),
                        this.getLHS().getIndex(), c1.dRem(c2));

				default:
					throw new IllegalArgumentException("Don't know how to fold those yet...");
			}
		}
		return this;
	}

	/**
	 * @see org.jnode.vm.compiler.ir.quad.AssignQuad#propagate(org.jnode.vm.compiler.ir.Variable)
	 */
	public Operand propagate(Variable operand) {
		Quad quad = foldConstants();
		if (quad instanceof ConstantRefAssignQuad) {
			setDeadCode(true);
			ConstantRefAssignQuad cop = (ConstantRefAssignQuad) quad;
			return cop.getRHS();
		}
		return operand;
	}

	/**
	 * Simplifies operands by calling operand.simplify().
	 * simplify will combine phi references and propagate copies
	 * This method will also update liveness of operands by setting last use addr
	 *
	 * @see org.jnode.vm.compiler.ir.quad.Quad#doPass2(org.jnode.util.BootableHashMap)
	 */
	public void doPass2(BootableHashMap liveVariables) {
		operand1 = operand1.simplify();
		operand2 = operand2.simplify();
		if (operand1 instanceof Variable) {
			Variable v = (Variable) operand1;
			v.setLastUseAddress(this.getAddress());
			liveVariables.put(v, v);
		}
		if (operand2 instanceof Variable) {
			Variable v = (Variable) operand2;
			v.setLastUseAddress(this.getAddress());
			liveVariables.put(v, v);
		}
	}

	/**
	 * Code generation is complicated by the permutations of addressing modes.
	 * This is not as nice as it could be, but it could be worse!
	 *
	 * @see org.jnode.vm.compiler.ir.quad.Quad#generateCode(org.jnode.vm.compiler.ir.CodeGenerator)
	 */
	public void generateCode(CodeGenerator cg) {
		Variable lhs = getLHS();
		int lhsMode = lhs.getAddressingMode();
		int op1Mode = operand1.getAddressingMode();
		int op2Mode = operand2.getAddressingMode();

		Object reg1 = null;
		if (lhsMode == Operand.MODE_REGISTER) {
			RegisterLocation regLoc = (RegisterLocation) lhs.getLocation();
			reg1 = regLoc.getRegister();
		}
		Object reg2 = null;
		if (op1Mode == Operand.MODE_REGISTER) {
			Variable var = (Variable) operand1;
			RegisterLocation regLoc = (RegisterLocation) var.getLocation();
			reg2 = regLoc.getRegister();
		}
		Object reg3 = null;
		if (op2Mode == Operand.MODE_REGISTER) {
			Variable var = (Variable) operand2;
			RegisterLocation regLoc = (RegisterLocation) var.getLocation();
			reg3 = regLoc.getRegister();
		}

		int disp1 = 0;
		if (lhsMode == Operand.MODE_STACK) {
			StackLocation stackLoc = (StackLocation) lhs.getLocation();
			disp1 = stackLoc.getDisplacement();
		}
		int disp2 = 0;
		if (op1Mode == Operand.MODE_STACK) {
			Variable var = (Variable) operand1;
			StackLocation stackLoc = (StackLocation) var.getLocation();
			disp2 = stackLoc.getDisplacement();
		}
		int disp3 = 0;
		if (op2Mode == Operand.MODE_STACK) {
			Variable var = (Variable) operand2;
			StackLocation stackLoc = (StackLocation) var.getLocation();
			disp3 = stackLoc.getDisplacement();
		}

		Constant c2 = null;
		if (op1Mode == Operand.MODE_CONSTANT) {
			c2 = (Constant) operand1;
		}
		Constant c3 = null;
		if (op2Mode == Operand.MODE_CONSTANT) {
			c3 = (Constant) operand2;
		}

		int aMode = (lhsMode << 16) | (op1Mode << 8) | op2Mode;
		switch (aMode) {
			case MODE_RCC:
				cg.generateBinaryOP(reg1, c2, operation, c3);
				break;
			case MODE_RCR:
				if (reg1 == reg3 && commutative && !cg.supports3AddrOps()) {
					cg.generateBinaryOP(reg1, reg3, operation, c2);
				} else {
					cg.generateBinaryOP(reg1, c2, operation, reg3);
				}
				break;
			case MODE_RCS:
				cg.generateBinaryOP(reg1, c2, operation, disp3);
				break;
			case MODE_RRC:
				cg.generateBinaryOP(reg1, reg2, operation, c3);
				break;
			case MODE_RRR:
				if (reg1 == reg3 && commutative && !cg.supports3AddrOps()) {
					cg.generateBinaryOP(reg1, reg3, operation, reg2);
				} else {
					cg.generateBinaryOP(reg1, reg2, operation, reg3);
				}
				break;
			case MODE_RRS:
				cg.generateBinaryOP(reg1, reg2, operation, disp3);
				break;
			case MODE_RSC:
				cg.generateBinaryOP(reg1, disp2, operation, c3);
				break;
			case MODE_RSR:
				if (reg1 == reg3 && commutative && !cg.supports3AddrOps()) {
					cg.generateBinaryOP(reg1, reg3, operation, disp2);
				} else {
					cg.generateBinaryOP(reg1, disp2, operation, reg3);
				}
				break;
			case MODE_RSS:
				cg.generateBinaryOP(reg1, disp2, operation, disp3);
				break;
			case MODE_SCC:
				cg.generateBinaryOP(disp1, c2, operation, c3);
				break;
			case MODE_SCR:
				cg.generateBinaryOP(disp1, c2, operation, reg3);
				break;
			case MODE_SCS:
				if (disp1 == disp3 && commutative && !cg.supports3AddrOps()) {
					cg.generateBinaryOP(disp1, disp3, operation, c2);
				} else {
					cg.generateBinaryOP(disp1, c2, operation, disp3);
				}
				break;
			case MODE_SRC:
				cg.generateBinaryOP(disp1, reg2, operation, c3);
				break;
			case MODE_SRR:
				cg.generateBinaryOP(disp1, reg2, operation, reg3);
				break;
			case MODE_SRS:
				if (disp1 == disp3 && commutative && !cg.supports3AddrOps()) {
					cg.generateBinaryOP(disp1, disp3, operation, reg2);
				} else {
					cg.generateBinaryOP(disp1, reg2, operation, disp3);
				}
				break;
			case MODE_SSC:
				cg.generateBinaryOP(disp1, disp2, operation, c3);
				break;
			case MODE_SSR:
				cg.generateBinaryOP(disp1, disp2, operation, reg3);
				break;
			case MODE_SSS:
				if (disp1 == disp3 && commutative && !cg.supports3AddrOps()) {
					cg.generateBinaryOP(disp1, disp3, operation, disp2);
				} else {
					cg.generateBinaryOP(disp1, disp2, operation, disp3);
				}
				break;
			default:
				throw new IllegalArgumentException("Undefined addressing mode: " + aMode);
		}
	}

	/**
	 * @see org.jnode.vm.compiler.ir.quad.AssignQuad#getLHSLiveAddress()
	 */
	public int getLHSLiveAddress() {
		CodeGenerator cg = CodeGenerator.getInstance();
		int addr = this.getAddress();
		if (cg.supports3AddrOps() || commutative) {
			return addr + 1;
		}
		return addr;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?