use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class DSLExpressionGenerator method visitCall.
public void visitCall(Call call) {
ExecutableElement method = call.getResolvedMethod();
CodeTree[] parameters = new CodeTree[method.getParameters().size()];
for (int i = 0; i < parameters.length; i++) {
parameters[parameters.length - i - 1] = pop();
}
CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
if (call.getResolvedMethod().getKind() == ElementKind.CONSTRUCTOR) {
builder.startNew(call.getResolvedType());
} else if (call.getReceiver() == null) {
if (isStatic(method)) {
builder.startStaticCall(method);
} else {
if (root != null) {
builder.tree(root).string(".");
}
builder.startCall(method.getSimpleName().toString());
}
} else {
if (isStatic(method)) {
throw new AssertionError("Static calls must not have receivers.");
}
builder.startCall(pop(), method.getSimpleName().toString());
}
for (CodeTree parameter : parameters) {
builder.tree(parameter);
}
builder.end();
push(builder.build());
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method generateReflectionInfo.
private void generateReflectionInfo(CodeTypeElement clazz) {
clazz.getImplements().add(context.getType(Introspection.Provider.class));
CodeExecutableElement reflection = new CodeExecutableElement(modifiers(PUBLIC), context.getType(Introspection.class), "getIntrospectionData");
CodeTreeBuilder builder = reflection.createBuilder();
List<SpecializationData> filteredSpecializations = new ArrayList<>();
for (SpecializationData s : node.getSpecializations()) {
if (s.getMethod() == null) {
continue;
}
filteredSpecializations.add(s);
}
ArrayCodeTypeMirror objectArray = new ArrayCodeTypeMirror(context.getType(Object.class));
builder.declaration(objectArray, "data", builder.create().startNewArray(objectArray, CodeTreeBuilder.singleString(String.valueOf(filteredSpecializations.size() + 1))).end().build());
builder.declaration(objectArray, "s", (CodeTree) null);
// declare version 0
builder.statement("data[0] = 0");
FrameState frameState = FrameState.load(this);
builder.tree(state.createLoad(frameState));
if (requiresExclude()) {
builder.tree(exclude.createLoad(frameState));
}
int index = 1;
for (SpecializationData specialization : filteredSpecializations) {
builder.startStatement().string("s = ").startNewArray(objectArray, CodeTreeBuilder.singleString("3")).end().end();
builder.startStatement().string("s[0] = ").doubleQuote(specialization.getMethodName()).end();
builder.startIf().tree(state.createContains(frameState, new Object[] { specialization })).end().startBlock();
builder.startStatement().string("s[1] = (byte)0b01 /* active */").end();
TypeMirror listType = new DeclaredCodeTypeMirror((TypeElement) context.getDeclaredType(ArrayList.class).asElement(), Arrays.asList(context.getType(Object.class)));
if (!specialization.getCaches().isEmpty()) {
builder.declaration(listType, "cached", "new ArrayList<>()");
boolean useSpecializationClass = useSpecializationClass(specialization);
String name = createSpecializationLocalName(specialization);
if (useSpecializationClass) {
builder.tree(loadSpecializationClass(frameState, specialization));
if (specialization.hasMultipleInstances()) {
builder.startWhile();
} else {
builder.startIf();
}
builder.string(name, " != null");
builder.end();
builder.startBlock();
}
builder.startStatement().startCall("cached", "add");
builder.startStaticCall(context.getType(Arrays.class), "asList");
for (CacheExpression cache : specialization.getCaches()) {
builder.startGroup();
builder.tree(createCacheReference(frameState, specialization, cache.getParameter()));
builder.end();
}
builder.end();
builder.end().end();
if (useSpecializationClass) {
if (specialization.getMaximumNumberOfInstances() > 1) {
builder.startStatement().string(name, " = ", name, ".next_").end();
}
// cache while or if
builder.end();
}
builder.statement("s[2] = cached");
}
builder.end();
if (mayBeExcluded(specialization)) {
builder.startElseIf().tree(exclude.createContains(frameState, new Object[] { specialization })).end().startBlock();
builder.startStatement().string("s[1] = (byte)0b10 /* excluded */").end();
builder.end();
}
builder.startElseBlock();
builder.startStatement().string("s[1] = (byte)0b00 /* inactive */").end();
builder.end();
builder.startStatement().string("data[", String.valueOf(index), "] = s").end();
index++;
}
builder.startReturn().startStaticCall(context.getType(Introspection.Provider.class), "create").string("data").end().end();
clazz.add(reflection);
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method persistAssumptions.
private List<IfTriple> persistAssumptions(FrameState frameState, SpecializationData specialization) {
List<IfTriple> triples = new ArrayList<>();
for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
LocalVariable var = frameState.get(assumption.getId());
String name = createAssumptionFieldName(specialization, assumption);
CodeTreeBuilder builder = new CodeTreeBuilder(null);
builder.startStatement();
builder.tree(createSpecializationFieldReference(frameState, specialization, name)).string(" = ").tree(var.createReference());
builder.end();
triples.add(new IfTriple(builder.build(), null, null));
}
return triples;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method wrapInAMethod.
private CodeTree wrapInAMethod(CodeTreeBuilder parent, SpecializationGroup group, FrameState frameState, String suffix, CodeTree codeTree) {
CodeExecutableElement parentMethod = (CodeExecutableElement) parent.findMethod();
CodeTypeElement parentClass = (CodeTypeElement) parentMethod.getEnclosingElement();
String name = parentMethod.getSimpleName().toString() + "_" + suffix + (boxingSplitIndex++);
CodeExecutableElement method = parentClass.add(frameState.createMethod(modifiers(Modifier.PRIVATE), parentMethod.getReturnType(), name, FRAME_VALUE, STATE_VALUE));
CodeTreeBuilder builder = method.createBuilder();
builder.tree(codeTree);
method.getThrownTypes().addAll(parentMethod.getThrownTypes());
addExplodeLoop(builder, group);
CodeTreeBuilder parentBuilder = parent.create();
parentBuilder.startReturn();
parentBuilder.startCall(method.getSimpleName().toString());
frameState.addReferencesTo(parentBuilder, FRAME_VALUE, STATE_VALUE);
parentBuilder.end();
parentBuilder.end();
return parentBuilder.build();
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createThrowUnsupported.
private CodeTree createThrowUnsupported(final CodeTreeBuilder parent, final FrameState frameState) {
CodeTreeBuilder builder = parent.create();
builder.startThrow().startNew(context.getType(UnsupportedSpecializationException.class));
builder.string("this");
builder.startNewArray(new ArrayCodeTypeMirror(context.getType(Node.class)), null);
List<CodeTree> values = new ArrayList<>();
for (NodeExecutionData execution : node.getChildExecutions()) {
NodeChildData child = execution.getChild();
LocalVariable var = frameState.getValue(execution);
if (child != null) {
builder.string(accessNodeField(execution));
} else {
builder.string("null");
}
if (var != null) {
values.add(var.createReference());
}
}
builder.end();
builder.trees(values.toArray(new CodeTree[0]));
builder.end().end();
return builder.build();
}
Aggregations