Mercurial > hg > truffle
annotate graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/demo/SLAddWithoutSpecializationNode.java @ 13883:ff3136ecb5a7
SL: small changes
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Wed, 05 Feb 2014 03:16:21 -0800 |
parents | |
children | f3e4f746e9c6 |
rev | line source |
---|---|
13883
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
1 package com.oracle.truffle.sl.nodes.expression.demo; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
2 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
3 import java.math.*; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
4 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
5 import com.oracle.truffle.api.*; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
6 import com.oracle.truffle.api.dsl.*; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
7 import com.oracle.truffle.api.frame.*; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
8 import com.oracle.truffle.api.nodes.*; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
9 import com.oracle.truffle.sl.nodes.*; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
10 import com.oracle.truffle.sl.nodes.expression.*; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
11 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
12 /** |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
13 * This is an example how the add operation would be implemented without specializations and without |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
14 * the Truffle DSL. Do not write such code in your language! See {@link SLAddNode} how the add |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
15 * operation is implemented correctly. |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
16 */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
17 public class SLAddWithoutSpecializationNode extends SLExpressionNode { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
18 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
19 @Child private SLExpressionNode leftNode; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
20 @Child private SLExpressionNode rightNode; |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
21 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
22 public SLAddWithoutSpecializationNode(SLExpressionNode leftNode, SLExpressionNode rightNode) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
23 this.leftNode = adoptChild(leftNode); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
24 this.rightNode = adoptChild(rightNode); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
25 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
26 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
27 @Override |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
28 public Object executeGeneric(VirtualFrame frame) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
29 /* Evaluate the child nodes. */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
30 Object left = leftNode.executeGeneric(frame); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
31 Object right = rightNode.executeGeneric(frame); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
32 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
33 if (left instanceof Long && right instanceof Long) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
34 /* Fast path of the arbitrary-precision arithmetic. We need to check for overflows */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
35 try { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
36 return ExactMath.addExact((Long) left, (Long) right); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
37 } catch (ArithmeticException ex) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
38 /* Fall through to BigInteger case. */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
39 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
40 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
41 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
42 /* Implicit type conversions. */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
43 if (left instanceof Long) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
44 left = BigInteger.valueOf((Long) left); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
45 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
46 if (right instanceof Long) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
47 right = BigInteger.valueOf((Long) right); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
48 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
49 if (left instanceof BigInteger && right instanceof BigInteger) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
50 /* Slow path of the arbitrary-precision arithmetic. */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
51 return ((BigInteger) left).add((BigInteger) right); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
52 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
53 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
54 /* String concatenation if either the left or the right operand is a String. */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
55 if (left instanceof String || right instanceof String) { |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
56 return left.toString() + right.toString(); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
57 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
58 |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
59 /* Type error. */ |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
60 throw new UnsupportedSpecializationException(this, new Node[]{leftNode, rightNode}, new Object[]{left, right}); |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
61 } |
ff3136ecb5a7
SL: small changes
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
62 } |