Mercurial > hg > truffle
comparison graal/com.oracle.truffle.api.dsl/src/com/oracle/truffle/api/dsl/internal/SpecializationNode.java @ 19291:f4792a544170
Truffle-DSL: implement new assumptions semantics.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Wed, 11 Feb 2015 12:13:44 +0100 |
parents | 62c43fcf5be2 |
children | e8d2f3f95dcd |
comparison
equal
deleted
inserted
replaced
19290:bf166845c7d8 | 19291:f4792a544170 |
---|---|
23 * questions. | 23 * questions. |
24 */ | 24 */ |
25 package com.oracle.truffle.api.dsl.internal; | 25 package com.oracle.truffle.api.dsl.internal; |
26 | 26 |
27 import java.lang.reflect.*; | 27 import java.lang.reflect.*; |
28 import java.util.*; | |
28 import java.util.concurrent.*; | 29 import java.util.concurrent.*; |
29 | 30 |
30 import com.oracle.truffle.api.*; | 31 import com.oracle.truffle.api.*; |
31 import com.oracle.truffle.api.dsl.*; | 32 import com.oracle.truffle.api.dsl.*; |
32 import com.oracle.truffle.api.dsl.internal.RewriteEvent.RewriteEvent0; | 33 import com.oracle.truffle.api.dsl.internal.RewriteEvent.RewriteEvent0; |
163 node = node.next; | 164 node = node.next; |
164 } | 165 } |
165 return node; | 166 return node; |
166 } | 167 } |
167 | 168 |
169 protected final Object removeThis(final CharSequence reason, Frame frame) { | |
170 return removeThisImpl(reason).acceptAndExecute(frame); | |
171 } | |
172 | |
173 protected final Object removeThis(final CharSequence reason, Frame frame, Object o1) { | |
174 return removeThisImpl(reason).acceptAndExecute(frame, o1); | |
175 } | |
176 | |
177 protected final Object removeThis(final CharSequence reason, Frame frame, Object o1, Object o2) { | |
178 return removeThisImpl(reason).acceptAndExecute(frame, o1, o2); | |
179 } | |
180 | |
181 protected final Object removeThis(final CharSequence reason, Frame frame, Object o1, Object o2, Object o3) { | |
182 return removeThisImpl(reason).acceptAndExecute(frame, o1, o2, o3); | |
183 } | |
184 | |
185 protected final Object removeThis(final CharSequence reason, Frame frame, Object o1, Object o2, Object o3, Object o4) { | |
186 return removeThisImpl(reason).acceptAndExecute(frame, o1, o2, o3, o4); | |
187 } | |
188 | |
189 protected final Object removeThis(final CharSequence reason, Frame frame, Object o1, Object o2, Object o3, Object o4, Object o5) { | |
190 return removeThisImpl(reason).acceptAndExecute(frame, o1, o2, o3, o4, o5); | |
191 } | |
192 | |
193 protected final Object removeThis(final CharSequence reason, Frame frame, Object... args) { | |
194 return removeThisImpl(reason).acceptAndExecute(frame, args); | |
195 } | |
196 | |
197 private SpecializationNode removeThisImpl(final CharSequence reason) { | |
198 this.replace(this.next, reason); | |
199 return findEnd().findStart(); | |
200 } | |
201 | |
168 protected final SpecializationNode removeSame(final CharSequence reason) { | 202 protected final SpecializationNode removeSame(final CharSequence reason) { |
169 return atomic(new Callable<SpecializationNode>() { | 203 return atomic(new Callable<SpecializationNode>() { |
170 public SpecializationNode call() throws Exception { | 204 public SpecializationNode call() throws Exception { |
171 return removeImpl(SpecializationNode.this, reason); | 205 return removeSameImpl(SpecializationNode.this, reason); |
172 } | 206 } |
173 }); | 207 }); |
174 } | 208 } |
175 | 209 |
176 /** Find the topmost of the specialization chain. */ | 210 /** Find the topmost of the specialization chain. */ |
190 | 224 |
191 private Node findRoot() { | 225 private Node findRoot() { |
192 return findStart().getParent(); | 226 return findStart().getParent(); |
193 } | 227 } |
194 | 228 |
195 private SpecializationNode removeImpl(SpecializationNode toRemove, CharSequence reason) { | 229 private SpecializationNode removeSameImpl(SpecializationNode toRemove, CharSequence reason) { |
196 SpecializationNode start = findStart(); | 230 SpecializationNode start = findStart(); |
197 SpecializationNode current = start; | 231 SpecializationNode current = start; |
198 while (current != null) { | 232 while (current != null) { |
199 if (current.isSame(toRemove)) { | 233 if (current.isSame(toRemove)) { |
200 current.replace(current.next, reason); | 234 current.replace(current.next, reason); |
468 StringBuilder b = new StringBuilder(); | 502 StringBuilder b = new StringBuilder(); |
469 b.append(clazz.getSimpleName()); | 503 b.append(clazz.getSimpleName()); |
470 | 504 |
471 appendFields(b, clazz); | 505 appendFields(b, clazz); |
472 if (next != null) { | 506 if (next != null) { |
473 b.append(" -> ").append(next.toString()); | 507 b.append("\n -> ").append(next.toString()); |
474 } | 508 } |
475 return b.toString(); | 509 return b.toString(); |
476 } | 510 } |
477 | 511 |
478 private void appendFields(StringBuilder b, Class<?> clazz) { | 512 private void appendFields(StringBuilder b, Class<?> clazz) { |
479 Field[] fields = clazz.getDeclaredFields(); | 513 Field[] fields = clazz.getDeclaredFields(); |
480 if (fields.length == 0) { | 514 if (fields.length == 0) { |
481 return; | 515 return; |
482 } | 516 } |
483 b.append("("); | 517 b.append("("); |
518 String sep = ""; | |
484 for (Field field : fields) { | 519 for (Field field : fields) { |
485 if (Modifier.isStatic(field.getModifiers())) { | 520 if (Modifier.isStatic(field.getModifiers())) { |
486 continue; | 521 continue; |
487 } | 522 } |
523 b.append(sep); | |
488 String name = field.getName(); | 524 String name = field.getName(); |
489 if (name.equals("root")) { | 525 if (name.equals("root")) { |
490 continue; | 526 continue; |
491 } | 527 } |
492 b.append(field.getName()); | 528 b.append(field.getName()); |
529 b.append(" = "); | |
493 try { | 530 try { |
494 field.setAccessible(true); | 531 field.setAccessible(true); |
495 b.append(field.get(this)); | 532 Object value = field.get(this); |
533 if (value instanceof Object[]) { | |
534 b.append(Arrays.toString((Object[]) field.get(this))); | |
535 } else { | |
536 b.append(field.get(this)); | |
537 } | |
496 } catch (IllegalArgumentException e) { | 538 } catch (IllegalArgumentException e) { |
497 b.append(e.toString()); | 539 b.append(e.toString()); |
498 } catch (IllegalAccessException e) { | 540 } catch (IllegalAccessException e) { |
499 b.append(e.toString()); | 541 b.append(e.toString()); |
500 } | 542 } |
543 sep = ", "; | |
501 } | 544 } |
502 b.append(")"); | 545 b.append(")"); |
503 } | 546 } |
504 | 547 |
548 // utilities for generated code | |
549 protected static void check(Assumption assumption) throws InvalidAssumptionException { | |
550 if (assumption != null) { | |
551 assumption.check(); | |
552 } | |
553 } | |
554 | |
555 @ExplodeLoop | |
556 protected static void check(Assumption[] assumptions) throws InvalidAssumptionException { | |
557 if (assumptions != null) { | |
558 CompilerAsserts.compilationConstant(assumptions.length); | |
559 for (Assumption assumption : assumptions) { | |
560 check(assumption); | |
561 } | |
562 } | |
563 } | |
564 | |
565 protected static boolean isValid(Assumption assumption) { | |
566 if (assumption != null) { | |
567 return assumption.isValid(); | |
568 } | |
569 return true; | |
570 } | |
571 | |
572 protected static boolean isValid(Assumption[] assumptions) { | |
573 if (assumptions != null) { | |
574 for (Assumption assumption : assumptions) { | |
575 if (!isValid(assumption)) { | |
576 return false; | |
577 } | |
578 } | |
579 } | |
580 return true; | |
581 } | |
582 | |
505 } | 583 } |