use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createCallExecuteAndSpecialize.
private CodeTree createCallExecuteAndSpecialize(ExecutableTypeData forType, FrameState frameState) {
TypeMirror returnType = node.getPolymorphicSpecialization().getReturnType().getType();
String frame = null;
if (needsFrameToExecute(reachableSpecializations)) {
frame = FRAME_VALUE;
}
CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
builder.startCall("executeAndSpecialize");
frameState.addReferencesTo(builder, frame);
builder.end();
CodeTree call = builder.build();
builder = builder.create();
if (isVoid(forType.getReturnType())) {
builder.statement(call);
builder.returnStatement();
} else {
builder.startReturn();
builder.tree(expectOrCast(returnType, forType, call));
builder.end();
}
return builder.build();
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createFallbackGuard.
private Element createFallbackGuard() {
boolean frameUsed = false;
FrameState frameState = FrameState.load(this);
List<SpecializationData> specializations = new ArrayList<>(reachableSpecializations);
for (ListIterator<SpecializationData> iterator = specializations.listIterator(); iterator.hasNext(); ) {
SpecializationData specialization = iterator.next();
if (specialization.isFallback()) {
iterator.remove();
} else if (!specialization.isReachesFallback()) {
iterator.remove();
} else {
if (specialization.isFrameUsedByGuard()) {
frameUsed = true;
}
}
}
SpecializationGroup group = SpecializationGroup.create(specializations);
ExecutableTypeData executableType = node.findAnyGenericExecutableType(context, -1);
if (!frameUsed) {
frameState.removeValue(FRAME_VALUE);
}
fallbackNeedsState = false;
fallbackNeedsFrame = frameUsed;
state.createLoad(frameState);
CodeExecutableElement method = frameState.createMethod(modifiers(PRIVATE), getType(boolean.class), METHOD_FALLBACK_GUARD, FRAME_VALUE, STATE_VALUE);
CodeTree result = visitSpecializationGroup(CodeTreeBuilder.createBuilder(), group, executableType, frameState, null, NodeExecutionMode.FALLBACK_GUARD);
if (!fallbackNeedsState) {
VariableElement toRemove = null;
for (VariableElement v : method.getParameters()) {
if (v.getSimpleName().toString().equals(STATE_VALUE)) {
toRemove = v;
break;
}
}
if (toRemove != null) {
method.getParameters().remove(toRemove);
}
}
final CodeTreeBuilder builder = method.createBuilder();
for (SpecializationData implemented : specializations) {
if (implemented.getMaximumNumberOfInstances() > 1) {
method.getAnnotationMirrors().add(createExplodeLoop());
break;
}
}
builder.tree(result);
builder.returnTrue();
if (!accessesCachedState(specializations)) {
method.getModifiers().add(STATIC);
}
return method;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createExecute.
private CodeTree createExecute(CodeTreeBuilder parent, FrameState frameState, final ExecutableTypeData forType, SpecializationData specialization, NodeExecutionMode mode) {
CodeTreeBuilder builder = parent.create();
if (mode.isSlowPath()) {
builder.statement("lock.unlock()");
builder.statement("hasLock = false");
}
if (specialization.getMethod() == null) {
builder.tree(createThrowUnsupported(builder, frameState));
} else {
if (isVoid(specialization.getMethod().getReturnType())) {
builder.statement(callTemplateMethod(null, specialization, frameState));
if (isVoid(forType.getReturnType())) {
builder.returnStatement();
} else {
builder.startReturn().defaultValue(forType.getReturnType()).end();
}
} else {
builder.startReturn();
builder.tree(expectOrCast(specialization.getReturnType().getType(), forType, callTemplateMethod(null, specialization, frameState)));
builder.end();
}
}
return createCatchRewriteException(builder, specialization, forType, frameState, builder.build(), mode);
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createRemoveThis.
private CodeTree createRemoveThis(CodeTreeBuilder parent, FrameState frameState, ExecutableTypeData forType, SpecializationData specialization) {
CodeExecutableElement method = removeThisMethods.get(specialization);
String specializationLocalName = createSpecializationLocalName(specialization);
boolean useSpecializationClass = useSpecializationClass(specialization);
if (method == null) {
method = new CodeExecutableElement(context.getType(void.class), "remove" + specialization.getId() + "_");
if (useSpecializationClass) {
method.addParameter(new CodeVariableElement(context.getType(Object.class), specializationLocalName));
}
CodeTreeBuilder builder = method.createBuilder();
builder.declaration(context.getType(Lock.class), "lock", "getLock()");
builder.statement("lock.lock()");
builder.startTryBlock();
String fieldName = createSpecializationFieldName(specialization);
if (!useSpecializationClass || specialization.getMaximumNumberOfInstances() == 1) {
// single instance remove
builder.tree((state.createSet(null, new Object[] { specialization }, false, true)));
if (useSpecializationClass) {
builder.statement("this." + fieldName + " = null");
}
} else {
// multi instance remove
String typeName = createSpecializationTypeName(specialization);
boolean specializedIsNode = specializationClassIsNode(specialization);
builder.declaration(typeName, "prev", "null");
builder.declaration(typeName, "cur", "this." + fieldName);
builder.startWhile();
builder.string("cur != null");
builder.end().startBlock();
builder.startIf().string("cur == ").string(specializationLocalName).end().startBlock();
builder.startIf().string("prev == null").end().startBlock();
builder.statement("this." + fieldName + " = cur.next_");
if (specializedIsNode) {
builder.statement("this.adoptChildren()");
}
builder.end().startElseBlock();
builder.statement("prev.next_ = cur.next_");
if (specializedIsNode) {
builder.statement("prev.adoptChildren()");
}
builder.end();
builder.statement("break");
// if block
builder.end();
builder.statement("prev = cur");
builder.statement("cur = cur.next_");
// while block
builder.end();
builder.startIf().string("this." + fieldName).string(" == null").end().startBlock();
builder.tree((state.createSet(null, Arrays.asList(specialization).toArray(new SpecializationData[0]), false, true)));
builder.end();
}
builder.end().startFinallyBlock();
builder.statement("lock.unlock()");
builder.end();
removeThisMethods.put(specialization, method);
}
CodeTreeBuilder builder = parent.create();
builder.startStatement().startCall(method.getSimpleName().toString());
if (useSpecializationClass) {
builder.string(specializationLocalName);
}
builder.end().end();
builder.tree(createCallExecuteAndSpecialize(forType, frameState));
return builder.build();
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createTryExecuteChild.
private static CodeTree createTryExecuteChild(LocalVariable value, CodeTree executeChild, boolean needDeclaration, boolean hasTry) {
CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
boolean hasDeclaration = false;
if ((hasTry || !executeChild.isSingleLine()) && needDeclaration) {
builder.tree(value.createDeclaration(null));
hasDeclaration = true;
}
if (hasTry) {
builder.startTryBlock();
} else {
builder.startGroup();
}
if (executeChild.isSingleLine()) {
builder.startStatement();
if (hasDeclaration || !needDeclaration) {
builder.tree(executeChild);
} else {
builder.type(value.getTypeMirror()).string(" ").tree(executeChild);
}
builder.end();
} else {
builder.tree(executeChild);
}
builder.end();
return builder.build();
}
Aggregations