51 */ |
50 */ |
52 @NodeInfo |
51 @NodeInfo |
53 public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ArithmeticOperation, ConvertNode, ArithmeticLIRLowerable, StampInverter { |
52 public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ArithmeticOperation, ConvertNode, ArithmeticLIRLowerable, StampInverter { |
54 @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class); |
53 @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class); |
55 |
54 |
56 protected final SerializableIntegerConvertFunction<OP> getOp; |
|
57 protected final SerializableIntegerConvertFunction<REV> getReverseOp; |
|
58 |
|
59 protected final int inputBits; |
55 protected final int inputBits; |
60 protected final int resultBits; |
56 protected final int resultBits; |
61 |
57 |
62 protected interface SerializableIntegerConvertFunction<T> extends Function<ArithmeticOpTable, IntegerConvertOp<T>>, Serializable { |
58 protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, IntegerConvertOp<OP> opForStampComputation, int inputBits, int resultBits, ValueNode input) { |
63 } |
59 super(c, opForStampComputation.foldStamp(inputBits, resultBits, input.stamp(NodeView.DEFAULT)), input); |
64 |
|
65 protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, SerializableIntegerConvertFunction<OP> getOp, SerializableIntegerConvertFunction<REV> getReverseOp, int inputBits, |
|
66 int resultBits, ValueNode input) { |
|
67 super(c, getOp.apply(ArithmeticOpTable.forStamp(input.stamp(NodeView.DEFAULT))).foldStamp(inputBits, resultBits, input.stamp(NodeView.DEFAULT)), input); |
|
68 this.getOp = getOp; |
|
69 this.getReverseOp = getReverseOp; |
|
70 this.inputBits = inputBits; |
60 this.inputBits = inputBits; |
71 this.resultBits = resultBits; |
61 this.resultBits = resultBits; |
72 assert ((PrimitiveStamp) input.stamp(NodeView.DEFAULT)).getBits() == inputBits; |
62 assert PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) == 0 || PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) == inputBits; |
73 } |
63 } |
74 |
64 |
75 public int getInputBits() { |
65 public int getInputBits() { |
76 return inputBits; |
66 return inputBits; |
77 } |
67 } |
78 |
68 |
79 public int getResultBits() { |
69 public int getResultBits() { |
80 return resultBits; |
70 return resultBits; |
81 } |
71 } |
82 |
72 |
83 protected final IntegerConvertOp<OP> getOp(ValueNode forValue) { |
73 protected abstract IntegerConvertOp<OP> getOp(ArithmeticOpTable table); |
84 return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp(NodeView.DEFAULT))); |
74 |
85 } |
75 protected abstract IntegerConvertOp<REV> getReverseOp(ArithmeticOpTable table); |
86 |
76 |
87 @Override |
77 @Override |
88 public final IntegerConvertOp<OP> getArithmeticOp() { |
78 public final IntegerConvertOp<OP> getArithmeticOp() { |
89 return getOp(getValue()); |
79 return getOp(getArithmeticOpTable(getValue())); |
90 } |
80 } |
91 |
81 |
92 @Override |
82 @Override |
93 public Constant convert(Constant c, ConstantReflectionProvider constantReflection) { |
83 public Constant convert(Constant c, ConstantReflectionProvider constantReflection) { |
94 return getArithmeticOp().foldConstant(getInputBits(), getResultBits(), c); |
84 return getArithmeticOp().foldConstant(getInputBits(), getResultBits(), c); |
95 } |
85 } |
96 |
86 |
97 @Override |
87 @Override |
98 public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) { |
88 public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) { |
99 IntegerConvertOp<REV> reverse = getReverseOp.apply(ArithmeticOpTable.forStamp(stamp(NodeView.DEFAULT))); |
89 IntegerConvertOp<REV> reverse = getReverseOp(ArithmeticOpTable.forStamp(stamp(NodeView.DEFAULT))); |
100 return reverse.foldConstant(getResultBits(), getInputBits(), c); |
90 return reverse.foldConstant(getResultBits(), getInputBits(), c); |
101 } |
91 } |
102 |
92 |
103 @Override |
93 @Override |
104 public Stamp foldStamp(Stamp newStamp) { |
94 public Stamp foldStamp(Stamp newStamp) { |
106 return getArithmeticOp().foldStamp(inputBits, resultBits, newStamp); |
96 return getArithmeticOp().foldStamp(inputBits, resultBits, newStamp); |
107 } |
97 } |
108 |
98 |
109 @Override |
99 @Override |
110 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { |
100 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { |
111 ValueNode synonym = findSynonym(getOp(forValue), forValue, inputBits, resultBits, stamp(NodeView.DEFAULT)); |
101 ValueNode synonym = findSynonym(getOp(getArithmeticOpTable(forValue)), forValue, inputBits, resultBits, stamp(NodeView.DEFAULT)); |
112 if (synonym != null) { |
102 if (synonym != null) { |
113 return synonym; |
103 return synonym; |
114 } |
104 } |
115 return this; |
105 return this; |
116 } |
106 } |