constant.java

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

JAVA
395
字号
/*
 * $Id: Constant.java,v 1.6 2004/02/19 17:29:00 lsantha Exp $
 *
 * mailto:madhu@madhu.com
 */
package org.jnode.vm.compiler.ir;

/**
 * @author Madhu Siddalingaiah
 * @author Levente S醤tha
 */
public abstract class Constant extends Operand {
	public Constant(int type) {
		super(type);
	}
	
	public static Constant getInstance(int value) {
		return new IntConstant(value);
	}
	
	public static Constant getInstance(long value) {
		return new LongConstant(value);
	}
	
	public static Constant getInstance(float value) {
		return new FloatConstant(value);
	}
	
	public static Constant getInstance(double value) {
		return new DoubleConstant(value);
	}
	
	public static Constant getInstance(Object value) {
		return new ReferenceConstant(value);
	}
	
	/**
	 * @param c2
	 */
	public Constant iAdd(Constant c2) {
		int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 + i2);
	}

	/**
	 * @param c2
	 */
	public Constant iSub(Constant c2) {
		int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 - i2);
	}

	/**
	 * @param c2
	 */
	public Constant iMul(Constant c2) {
		int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 * i2);
	}

	/**
	 * @param c2
	 */
	public Constant iDiv(Constant c2) {
		int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 / i2);
	}

    /**
     *
     * @param c2
     * @return
     */
    public Constant iRem(Constant c2) {
        int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 % i2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant iAnd(Constant c2) {
        int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 & i2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant iOr(Constant c2) {
        int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 | i2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant iXor(Constant c2) {
        int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 ^ i2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant iShl(Constant c2) {
        int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 << i2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant iShr(Constant c2) {
        int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 >> i2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant iUshr(Constant c2) {
        int i1 = ((IntConstant) this).getValue();
		int i2 = ((IntConstant) c2).getValue();
		return new IntConstant(i1 >>> i2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lAdd(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 + l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lSub(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 - l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lMul(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 * l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lDiv(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 / l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lRem(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 % l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lAnd(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 & l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lOr(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 | l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lXor(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 ^ l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lShl(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 << l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lShr(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 >> l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant lUshr(Constant c2) {
        long l1 = ((LongConstant) this).getValue();
		long l2 = ((LongConstant) c2).getValue();
		return new LongConstant(l1 >>> l2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant fAdd(Constant c2) {
        float f1 = ((FloatConstant) this).getValue();
		float f2 = ((FloatConstant) c2).getValue();
		return new FloatConstant(f1 + f2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant fSub(Constant c2) {
        float f1 = ((FloatConstant) this).getValue();
		float f2 = ((FloatConstant) c2).getValue();
		return new FloatConstant(f1 - f2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant fMul(Constant c2) {
        float f1 = ((FloatConstant) this).getValue();
		float f2 = ((FloatConstant) c2).getValue();
		return new FloatConstant(f1 * f2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant fDiv(Constant c2) {
        float f1 = ((FloatConstant) this).getValue();
		float f2 = ((FloatConstant) c2).getValue();
		return new FloatConstant(f1 / f2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant fRem(Constant c2) {
        float f1 = ((FloatConstant) this).getValue();
		float f2 = ((FloatConstant) c2).getValue();
		return new FloatConstant(f1 / f2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant dAdd(Constant c2) {
        double d1 = ((DoubleConstant) this).getValue();
		double d2 = ((DoubleConstant) c2).getValue();
		return new DoubleConstant(d1 + d2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant dSub(Constant c2) {
        double d1 = ((DoubleConstant) this).getValue();
		double d2 = ((DoubleConstant) c2).getValue();
		return new DoubleConstant(d1 - d2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant dMul(Constant c2) {
        double d1 = ((DoubleConstant) this).getValue();
		double d2 = ((DoubleConstant) c2).getValue();
		return new DoubleConstant(d1 * d2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant dDiv(Constant c2) {
        double d1 = ((DoubleConstant) this).getValue();
		double d2 = ((DoubleConstant) c2).getValue();
		return new DoubleConstant(d1 / d2);
    }

    /**
     *
     * @param c2
     * @return
     */
    public Constant dRem(Constant c2) {
        double d1 = ((DoubleConstant) this).getValue();
		double d2 = ((DoubleConstant) c2).getValue();
		return new DoubleConstant(d1 % d2);
    }

	/* (non-Javadoc)
	 * @see org.jnode.vm.compiler.ir.Operand#simplify()
	 */
	public Operand simplify() {
		return this;
	}

	/* (non-Javadoc)
	 * @see org.jnode.vm.compiler.ir.Operand#getAddressingMode()
	 */
	public int getAddressingMode() {
		return Operand.MODE_CONSTANT;
	}
}

⌨️ 快捷键说明

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