comparison graal/com.oracle.truffle.api.dsl/src/com/oracle/truffle/api/dsl/internal/SpecializationNode.java @ 19289:62c43fcf5be2

Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
author Christian Humer <christian.humer@gmail.com>
date Tue, 03 Feb 2015 15:07:07 +0100
parents 92880b0f7fed
children f4792a544170
comparison
equal deleted inserted replaced
19288:3a37116ef37f 19289:62c43fcf5be2
92 } 92 }
93 93
94 protected final SpecializationNode polymorphicMerge(SpecializationNode newNode) { 94 protected final SpecializationNode polymorphicMerge(SpecializationNode newNode) {
95 SpecializationNode merged = next.merge(newNode); 95 SpecializationNode merged = next.merge(newNode);
96 if (merged == newNode && !isSame(newNode) && count() <= 2) { 96 if (merged == newNode && !isSame(newNode) && count() <= 2) {
97 return removeSame(new RewriteEvent0(findParentNode(), "merged polymorphic to monomorphic")); 97 return removeSame(new RewriteEvent0(findRoot(), "merged polymorphic to monomorphic"));
98 } 98 }
99 return merged; 99 return merged;
100 } 100 }
101 101
102 public final NodeCost getNodeCost() { 102 public final NodeCost getNodeCost() {
118 return this; 118 return this;
119 } 119 }
120 return next != null ? next.merge(newNode) : newNode; 120 return next != null ? next.merge(newNode) : newNode;
121 } 121 }
122 122
123 protected SpecializationNode mergeNoSame(SpecializationNode newNode) {
124 return next != null ? next.merge(newNode) : newNode;
125 }
126
127 protected final int countSame(SpecializationNode node) {
128 return findStart().countSameImpl(node);
129 }
130
131 private int countSameImpl(SpecializationNode node) {
132 if (next != null) {
133 return next.countSameImpl(node) + (isSame(node) ? 1 : 0);
134 } else {
135 return 0;
136 }
137 }
138
123 @Override 139 @Override
124 public final boolean equals(Object obj) { 140 public final boolean equals(Object obj) {
125 if (obj instanceof SpecializationNode) { 141 if (obj instanceof SpecializationNode) {
126 return ((SpecializationNode) obj).isSame(this); 142 return ((SpecializationNode) obj).isSame(this);
127 } 143 }
170 parent = node.getParent(); 186 parent = node.getParent();
171 } 187 }
172 return node; 188 return node;
173 } 189 }
174 190
175 private Node findParentNode() { 191 private Node findRoot() {
176 return findStart().getParent(); 192 return findStart().getParent();
177 } 193 }
178 194
179 private SpecializationNode removeImpl(SpecializationNode toRemove, CharSequence reason) { 195 private SpecializationNode removeImpl(SpecializationNode toRemove, CharSequence reason) {
180 SpecializationNode start = findStart(); 196 SpecializationNode start = findStart();
186 start = start.next; 202 start = start.next;
187 } 203 }
188 } 204 }
189 current = current.next; 205 current = current.next;
190 } 206 }
191 return start; 207 return findEnd().findStart();
192 } 208 }
193 209
194 public Object acceptAndExecute(Frame frame) { 210 public Object acceptAndExecute(Frame frame) {
195 throw new UnsupportedOperationException(); 211 throw new UnsupportedOperationException();
196 } 212 }
262 nextSpecialization = createFallback(); 278 nextSpecialization = createFallback();
263 } 279 }
264 if (nextSpecialization == null) { 280 if (nextSpecialization == null) {
265 return unsupported(frame); 281 return unsupported(frame);
266 } 282 }
267 return insertSpecialization(nextSpecialization, new RewriteEvent0(findParentNode(), "inserted new specialization")).acceptAndExecute(frame); 283 return insertSpecialization(nextSpecialization, new RewriteEvent0(findRoot(), "inserted new specialization")).acceptAndExecute(frame);
268 } 284 }
269 285
270 protected final Object uninitialized(Frame frame, Object o1) { 286 protected final Object uninitialized(Frame frame, Object o1) {
271 CompilerDirectives.transferToInterpreterAndInvalidate(); 287 CompilerDirectives.transferToInterpreterAndInvalidate();
272 SpecializationNode nextSpecialization = createNext(frame, o1); 288 SpecializationNode nextSpecialization = createNext(frame, o1);
274 nextSpecialization = createFallback(); 290 nextSpecialization = createFallback();
275 } 291 }
276 if (nextSpecialization == null) { 292 if (nextSpecialization == null) {
277 return unsupported(frame, o1); 293 return unsupported(frame, o1);
278 } 294 }
279 return insertSpecialization(nextSpecialization, new RewriteEvent1(findParentNode(), "inserted new specialization", o1)).acceptAndExecute(frame, o1); 295 return insertSpecialization(nextSpecialization, new RewriteEvent1(findRoot(), "inserted new specialization", o1)).acceptAndExecute(frame, o1);
280 } 296 }
281 297
282 protected final Object uninitialized(Frame frame, Object o1, Object o2) { 298 protected final Object uninitialized(Frame frame, Object o1, Object o2) {
283 CompilerDirectives.transferToInterpreterAndInvalidate(); 299 CompilerDirectives.transferToInterpreterAndInvalidate();
284 SpecializationNode nextSpecialization = createNext(frame, o1, o2); 300 SpecializationNode nextSpecialization = createNext(frame, o1, o2);
286 nextSpecialization = createFallback(); 302 nextSpecialization = createFallback();
287 } 303 }
288 if (nextSpecialization == null) { 304 if (nextSpecialization == null) {
289 return unsupported(frame, o1, o2); 305 return unsupported(frame, o1, o2);
290 } 306 }
291 return insertSpecialization(nextSpecialization, new RewriteEvent2(findParentNode(), "inserted new specialization", o1, o2)).acceptAndExecute(frame, o1, o2); 307 return insertSpecialization(nextSpecialization, new RewriteEvent2(findRoot(), "inserted new specialization", o1, o2)).acceptAndExecute(frame, o1, o2);
292 } 308 }
293 309
294 protected final Object uninitialized(Frame frame, Object o1, Object o2, Object o3) { 310 protected final Object uninitialized(Frame frame, Object o1, Object o2, Object o3) {
295 CompilerDirectives.transferToInterpreterAndInvalidate(); 311 CompilerDirectives.transferToInterpreterAndInvalidate();
296 SpecializationNode nextSpecialization = createNext(frame, o1, o2, o3); 312 SpecializationNode nextSpecialization = createNext(frame, o1, o2, o3);
298 nextSpecialization = createFallback(); 314 nextSpecialization = createFallback();
299 } 315 }
300 if (nextSpecialization == null) { 316 if (nextSpecialization == null) {
301 return unsupported(frame, o1, o2, o3); 317 return unsupported(frame, o1, o2, o3);
302 } 318 }
303 return insertSpecialization(nextSpecialization, new RewriteEvent3(findParentNode(), "inserted new specialization", o1, o2, o3)).acceptAndExecute(frame, o1, o2, o3); 319 return insertSpecialization(nextSpecialization, new RewriteEvent3(findRoot(), "inserted new specialization", o1, o2, o3)).acceptAndExecute(frame, o1, o2, o3);
304 } 320 }
305 321
306 protected final Object uninitialized(Frame frame, Object o1, Object o2, Object o3, Object o4) { 322 protected final Object uninitialized(Frame frame, Object o1, Object o2, Object o3, Object o4) {
307 CompilerDirectives.transferToInterpreterAndInvalidate(); 323 CompilerDirectives.transferToInterpreterAndInvalidate();
308 SpecializationNode nextSpecialization = createNext(frame, o1, o2, o3, o4); 324 SpecializationNode nextSpecialization = createNext(frame, o1, o2, o3, o4);
310 nextSpecialization = createFallback(); 326 nextSpecialization = createFallback();
311 } 327 }
312 if (nextSpecialization == null) { 328 if (nextSpecialization == null) {
313 return unsupported(frame, o1, o2, o3, o4); 329 return unsupported(frame, o1, o2, o3, o4);
314 } 330 }
315 return insertSpecialization(nextSpecialization, new RewriteEvent4(findParentNode(), "inserts new specialization", o1, o2, o3, o4)).acceptAndExecute(frame, o1, o2, o3, o4); 331 return insertSpecialization(nextSpecialization, new RewriteEvent4(findRoot(), "inserts new specialization", o1, o2, o3, o4)).acceptAndExecute(frame, o1, o2, o3, o4);
316 } 332 }
317 333
318 protected final Object uninitialized(Frame frame, Object o1, Object o2, Object o3, Object o4, Object o5) { 334 protected final Object uninitialized(Frame frame, Object o1, Object o2, Object o3, Object o4, Object o5) {
319 CompilerDirectives.transferToInterpreterAndInvalidate(); 335 CompilerDirectives.transferToInterpreterAndInvalidate();
320 SpecializationNode nextSpecialization = createNext(frame, o1, o2, o3, o4, o5); 336 SpecializationNode nextSpecialization = createNext(frame, o1, o2, o3, o4, o5);
322 nextSpecialization = createFallback(); 338 nextSpecialization = createFallback();
323 } 339 }
324 if (nextSpecialization == null) { 340 if (nextSpecialization == null) {
325 unsupported(frame, o1, o2, o3, o4, o5); 341 unsupported(frame, o1, o2, o3, o4, o5);
326 } 342 }
327 return insertSpecialization(nextSpecialization, new RewriteEventN(findParentNode(), "inserts new specialization", o1, o2, o3, o4, o5)).acceptAndExecute(frame, o1, o2, o3, o4, o5); 343 return insertSpecialization(nextSpecialization, new RewriteEventN(findRoot(), "inserts new specialization", o1, o2, o3, o4, o5)).acceptAndExecute(frame, o1, o2, o3, o4, o5);
328 } 344 }
329 345
330 protected final Object uninitialized(Frame frame, Object... args) { 346 protected final Object uninitialized(Frame frame, Object... args) {
331 CompilerDirectives.transferToInterpreterAndInvalidate(); 347 CompilerDirectives.transferToInterpreterAndInvalidate();
332 SpecializationNode nextSpecialization = createNext(frame, args); 348 SpecializationNode nextSpecialization = createNext(frame, args);
334 nextSpecialization = createFallback(); 350 nextSpecialization = createFallback();
335 } 351 }
336 if (nextSpecialization == null) { 352 if (nextSpecialization == null) {
337 unsupported(frame, args); 353 unsupported(frame, args);
338 } 354 }
339 return insertSpecialization(nextSpecialization, new RewriteEventN(findParentNode(), "inserts new specialization", args)).acceptAndExecute(frame, args); 355 return insertSpecialization(nextSpecialization, new RewriteEventN(findRoot(), "inserts new specialization", args)).acceptAndExecute(frame, args);
340 } 356 }
341 357
342 private boolean needsPolymorphic() { 358 private boolean needsPolymorphic() {
343 return findStart().count() == 2; 359 return findStart().count() == 2;
344 } 360 }
345 361
346 protected final Object remove(String reason, Frame frame) { 362 protected final Object remove(String reason, Frame frame) {
347 return removeSame(new RewriteEvent0(findParentNode(), reason)).acceptAndExecute(frame); 363 return removeSame(new RewriteEvent0(findRoot(), reason)).acceptAndExecute(frame);
348 } 364 }
349 365
350 protected final Object remove(String reason, Frame frame, Object o1) { 366 protected final Object remove(String reason, Frame frame, Object o1) {
351 return removeSame(new RewriteEvent1(findParentNode(), reason, o1)).acceptAndExecute(frame, o1); 367 return removeSame(new RewriteEvent1(findRoot(), reason, o1)).acceptAndExecute(frame, o1);
352 } 368 }
353 369
354 protected final Object remove(String reason, Frame frame, Object o1, Object o2) { 370 protected final Object remove(String reason, Frame frame, Object o1, Object o2) {
355 return removeSame(new RewriteEvent2(findParentNode(), reason, o1, o2)).acceptAndExecute(frame, o1, o2); 371 return removeSame(new RewriteEvent2(findRoot(), reason, o1, o2)).acceptAndExecute(frame, o1, o2);
356 } 372 }
357 373
358 protected final Object remove(String reason, Frame frame, Object o1, Object o2, Object o3) { 374 protected final Object remove(String reason, Frame frame, Object o1, Object o2, Object o3) {
359 return removeSame(new RewriteEvent3(findParentNode(), reason, o1, o2, o3)).acceptAndExecute(frame, o1, o2, o3); 375 return removeSame(new RewriteEvent3(findRoot(), reason, o1, o2, o3)).acceptAndExecute(frame, o1, o2, o3);
360 } 376 }
361 377
362 protected final Object remove(String reason, Frame frame, Object o1, Object o2, Object o3, Object o4) { 378 protected final Object remove(String reason, Frame frame, Object o1, Object o2, Object o3, Object o4) {
363 return removeSame(new RewriteEvent4(findParentNode(), reason, o1, o2, o3, o4)).acceptAndExecute(frame, o1, o2, o3, o4); 379 return removeSame(new RewriteEvent4(findRoot(), reason, o1, o2, o3, o4)).acceptAndExecute(frame, o1, o2, o3, o4);
364 } 380 }
365 381
366 protected final Object remove(String reason, Frame frame, Object o1, Object o2, Object o3, Object o4, Object o5) { 382 protected final Object remove(String reason, Frame frame, Object o1, Object o2, Object o3, Object o4, Object o5) {
367 return removeSame(new RewriteEventN(findParentNode(), reason, o1, o2, o3, o4, o5)).acceptAndExecute(frame, o1, o2, o3, o4, o5); 383 return removeSame(new RewriteEventN(findRoot(), reason, o1, o2, o3, o4, o5)).acceptAndExecute(frame, o1, o2, o3, o4, o5);
368 } 384 }
369 385
370 protected final Object remove(String reason, Frame frame, Object... args) { 386 protected final Object remove(String reason, Frame frame, Object... args) {
371 return removeSame(new RewriteEventN(findParentNode(), reason, args)).acceptAndExecute(frame, args); 387 return removeSame(new RewriteEventN(findRoot(), reason, args)).acceptAndExecute(frame, args);
372 } 388 }
373 389
374 protected Object unsupported(Frame frame) { 390 protected Object unsupported(Frame frame) {
375 throw new UnsupportedSpecializationException(findParentNode(), getSuppliedChildren()); 391 throw new UnsupportedSpecializationException(findRoot(), getSuppliedChildren());
376 } 392 }
377 393
378 protected Object unsupported(Frame frame, Object o1) { 394 protected Object unsupported(Frame frame, Object o1) {
379 throw new UnsupportedSpecializationException(findParentNode(), getSuppliedChildren(), o1); 395 throw new UnsupportedSpecializationException(findRoot(), getSuppliedChildren(), o1);
380 } 396 }
381 397
382 protected Object unsupported(Frame frame, Object o1, Object o2) { 398 protected Object unsupported(Frame frame, Object o1, Object o2) {
383 throw new UnsupportedSpecializationException(findParentNode(), getSuppliedChildren(), o1, o2); 399 throw new UnsupportedSpecializationException(findRoot(), getSuppliedChildren(), o1, o2);
384 } 400 }
385 401
386 protected Object unsupported(Frame frame, Object o1, Object o2, Object o3) { 402 protected Object unsupported(Frame frame, Object o1, Object o2, Object o3) {
387 throw new UnsupportedSpecializationException(findParentNode(), getSuppliedChildren(), o1, o2, o3); 403 throw new UnsupportedSpecializationException(findRoot(), getSuppliedChildren(), o1, o2, o3);
388 } 404 }
389 405
390 protected Object unsupported(Frame frame, Object o1, Object o2, Object o3, Object o4) { 406 protected Object unsupported(Frame frame, Object o1, Object o2, Object o3, Object o4) {
391 throw new UnsupportedSpecializationException(findParentNode(), getSuppliedChildren(), o1, o2, o3, o4); 407 throw new UnsupportedSpecializationException(findRoot(), getSuppliedChildren(), o1, o2, o3, o4);
392 } 408 }
393 409
394 protected Object unsupported(Frame frame, Object o1, Object o2, Object o3, Object o4, Object o5) { 410 protected Object unsupported(Frame frame, Object o1, Object o2, Object o3, Object o4, Object o5) {
395 throw new UnsupportedSpecializationException(findParentNode(), getSuppliedChildren(), o1, o2, o3, o4, o5); 411 throw new UnsupportedSpecializationException(findRoot(), getSuppliedChildren(), o1, o2, o3, o4, o5);
396 } 412 }
397 413
398 protected Object unsupported(Frame frame, Object... args) { 414 protected Object unsupported(Frame frame, Object... args) {
399 throw new UnsupportedSpecializationException(findParentNode(), getSuppliedChildren(), args); 415 throw new UnsupportedSpecializationException(findRoot(), getSuppliedChildren(), args);
400 } 416 }
401 417
402 private SpecializationNode insertSpecialization(final SpecializationNode generated, final CharSequence message) { 418 private SpecializationNode insertSpecialization(final SpecializationNode generated, final CharSequence message) {
403 return atomic(new Callable<SpecializationNode>() { 419 return atomic(new Callable<SpecializationNode>() {
404 public SpecializationNode call() { 420 public SpecializationNode call() {
426 } 442 }
427 SpecializationNode insertBefore = findInsertBeforeNode(generated.index, start); 443 SpecializationNode insertBefore = findInsertBeforeNode(generated.index, start);
428 return insertBefore(insertBefore, generated, message); 444 return insertBefore(insertBefore, generated, message);
429 } else { 445 } else {
430 // existing node 446 // existing node
431 merged.replace(merged, new RewriteEvent0(merged.findParentNode(), "merged specialization")); 447 merged.replace(merged, new RewriteEvent0(merged.findRoot(), "merged specialization"));
432 return merged; 448 return start;
433 } 449 }
434 } 450 }
435 451
436 private static SpecializationNode findInsertBeforeNode(int generatedIndex, SpecializationNode start) { 452 private static SpecializationNode findInsertBeforeNode(int generatedIndex, SpecializationNode start) {
437 SpecializationNode current = start; 453 SpecializationNode current = start;