Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/MaxMinObject.java @ 22046:e7c2d36daf72
TruffleLanguage.parse method to convert a source to CallTarget. Basic caching to make sure the code is shared among tenants in one JVM.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 30 Jul 2015 17:36:34 +0200 |
parents | 5bc7f7b867ab |
children | 78c3d3d8d86e |
comparison
equal
deleted
inserted
replaced
22045:ffbc7f472438 | 22046:e7c2d36daf72 |
---|---|
45 this.max = max; | 45 this.max = max; |
46 } | 46 } |
47 | 47 |
48 @Override | 48 @Override |
49 public ForeignAccess getForeignAccess() { | 49 public ForeignAccess getForeignAccess() { |
50 return ForeignAccess.create(MaxMinObject.class, new AF(max)); | 50 return ForeignAccess.create(MaxMinObject.class, new AF()); |
51 } | 51 } |
52 | 52 |
53 static final class AF implements ForeignAccess.Factory10 { | 53 static final class AF implements ForeignAccess.Factory10 { |
54 private final boolean max; | |
55 | |
56 public AF(boolean max) { | |
57 this.max = max; | |
58 } | |
59 | |
60 @Override | 54 @Override |
61 public CallTarget accessIsNull() { | 55 public CallTarget accessIsNull() { |
62 return null; | 56 return null; |
63 } | 57 } |
64 | 58 |
98 } | 92 } |
99 | 93 |
100 @Override | 94 @Override |
101 public CallTarget accessExecute(int argumentsLength) { | 95 public CallTarget accessExecute(int argumentsLength) { |
102 if (argumentsLength == 2) { | 96 if (argumentsLength == 2) { |
103 MaxMinNode maxNode = MaxMinObjectFactory.MaxMinNodeGen.create(max, MaxMinObjectFactory.UnboxNodeGen.create(new ReadArgNode(0)), | 97 MaxMinNode maxNode = MaxMinObjectFactory.MaxMinNodeGen.create(new ReadReceiverNode(), MaxMinObjectFactory.UnboxNodeGen.create(new ReadArgNode(0)), |
104 MaxMinObjectFactory.UnboxNodeGen.create(new ReadArgNode(1))); | 98 MaxMinObjectFactory.UnboxNodeGen.create(new ReadArgNode(1))); |
105 return Truffle.getRuntime().createCallTarget(maxNode); | 99 return Truffle.getRuntime().createCallTarget(maxNode); |
106 } | 100 } |
107 return null; | 101 return null; |
108 } | 102 } |
125 this.argIndex = argIndex; | 119 this.argIndex = argIndex; |
126 } | 120 } |
127 | 121 |
128 public Object execute(VirtualFrame frame) { | 122 public Object execute(VirtualFrame frame) { |
129 return ForeignAccess.getArguments(frame).get(argIndex); | 123 return ForeignAccess.getArguments(frame).get(argIndex); |
124 } | |
125 } | |
126 | |
127 static class ReadReceiverNode extends Node { | |
128 public Object execute(VirtualFrame frame) { | |
129 return ForeignAccess.getReceiver(frame); | |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 @NodeChildren({@NodeChild(value = "valueNode", type = ReadArgNode.class)}) | 133 @NodeChildren({@NodeChild(value = "valueNode", type = ReadArgNode.class)}) |
134 abstract static class UnboxNode extends Node { | 134 abstract static class UnboxNode extends Node { |
169 return (boolean) ForeignAccess.execute(isBoxed, frame, object); | 169 return (boolean) ForeignAccess.execute(isBoxed, frame, object); |
170 } | 170 } |
171 | 171 |
172 } | 172 } |
173 | 173 |
174 @NodeChildren({@NodeChild(value = "firstNode", type = UnboxNode.class), @NodeChild(value = "secondNode", type = UnboxNode.class)}) | 174 @NodeChildren({@NodeChild(value = "receiver", type = ReadReceiverNode.class), @NodeChild(value = "firstNode", type = UnboxNode.class), @NodeChild(value = "secondNode", type = UnboxNode.class)}) |
175 abstract static class MaxMinNode extends RootNode { | 175 abstract static class MaxMinNode extends RootNode { |
176 private final boolean max; | 176 |
177 | 177 MaxMinNode() { |
178 MaxMinNode(boolean max) { | |
179 super(MMLanguage.class, null, null); | 178 super(MMLanguage.class, null, null); |
180 this.max = max; | 179 } |
181 } | 180 |
182 | 181 @Specialization |
183 @Specialization | 182 public int execute(MaxMinObject receiver, int first, int second) { |
184 public int execute(int first, int second) { | 183 return receiver.max ? Math.max(first, second) : Math.min(first, second); |
185 return max ? Math.max(first, second) : Math.min(first, second); | 184 } |
186 } | 185 |
187 | 186 @Specialization |
188 @Specialization | 187 public long execute(MaxMinObject receiver, long first, long second) { |
189 public long execute(long first, long second) { | 188 return receiver.max ? Math.max(first, second) : Math.min(first, second); |
190 return max ? Math.max(first, second) : Math.min(first, second); | 189 } |
191 } | 190 |
192 | 191 @Specialization |
193 @Specialization | 192 public double execute(MaxMinObject receiver, double first, double second) { |
194 public double execute(double first, double second) { | 193 return receiver.max ? Math.max(first, second) : Math.min(first, second); |
195 return max ? Math.max(first, second) : Math.min(first, second); | |
196 } | 194 } |
197 } | 195 } |
198 | 196 |
199 private abstract class MMLanguage extends TruffleLanguage { | 197 private abstract class MMLanguage extends TruffleLanguage { |
200 public MMLanguage(Env env) { | 198 public MMLanguage(Env env) { |