use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class NodeFactoryFactory method createCreateGetNodeSignatures.
private CodeExecutableElement createCreateGetNodeSignatures() {
TypeMirror returnValue = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(List.class)));
CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnValue, "getNodeSignatures");
CodeTreeBuilder builder = method.createBuilder();
builder.startReturn();
builder.startGroup();
builder.startStaticCall(context.getType(Arrays.class), "asList");
List<ExecutableElement> constructors = GeneratorUtils.findUserConstructors(createdFactoryElement.asType());
for (ExecutableElement constructor : constructors) {
builder.startGroup();
builder.startStaticCall(context.getType(Arrays.class), "asList");
for (VariableElement var : constructor.getParameters()) {
builder.typeLiteral(var.asType());
}
builder.end();
builder.end();
}
builder.end();
builder.end();
builder.end();
return method;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class NodeFactoryFactory method createGetInstanceMethod.
private ExecutableElement createGetInstanceMethod(Modifier visibility) {
TypeElement nodeFactoryType = ElementUtils.fromTypeMirror(context.getType(NodeFactory.class));
TypeMirror returnType = ElementUtils.getDeclaredType(nodeFactoryType, node.getNodeType());
CodeExecutableElement method = new CodeExecutableElement(modifiers(), returnType, "getInstance");
if (visibility != null) {
method.getModifiers().add(visibility);
}
method.getModifiers().add(Modifier.STATIC);
String varName = instanceVarName(node);
CodeTreeBuilder builder = method.createBuilder();
builder.startIf();
builder.string(varName).string(" == null");
builder.end().startBlock();
builder.startStatement();
builder.string(varName);
builder.string(" = ");
builder.startNew(factoryClassName(node)).end();
builder.end();
builder.end();
builder.startReturn().string(varName).end();
return method;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class TypeSystemCodeGenerator method createExpectMethod.
static CodeExecutableElement createExpectMethod(Modifier visibility, TypeSystemData typeSystem, TypeMirror sourceTypeOriginal, TypeMirror expectedTypeOriginal) {
TypeMirror expectedType = ElementUtils.fillInGenericWildcards(expectedTypeOriginal);
TypeMirror sourceType = ElementUtils.fillInGenericWildcards(sourceTypeOriginal);
if (ElementUtils.isObject(expectedType) || ElementUtils.isVoid(expectedType)) {
return null;
}
CodeExecutableElement method = new CodeExecutableElement(modifiers(STATIC), expectedType, TypeSystemCodeGenerator.expectTypeMethodName(typeSystem, expectedType));
method.setVisibility(visibility);
method.addParameter(new CodeVariableElement(sourceType, LOCAL_VALUE));
method.addThrownType(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException());
CodeTreeBuilder body = method.createBuilder();
body.startIf().tree(check(typeSystem, expectedType, LOCAL_VALUE)).end().startBlock();
body.startReturn().tree(cast(typeSystem, expectedType, LOCAL_VALUE)).end();
body.end();
body.startThrow().startNew(typeSystem.getContext().getTruffleTypes().getUnexpectedValueException()).string(LOCAL_VALUE).end().end();
return method;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createExecuteDelegation.
private ExecuteDelegationResult createExecuteDelegation(CodeTreeBuilder parent, FrameState frameState, ExecutableTypeData type, List<ExecutableTypeData> delegateableTypes, final List<SpecializationData> compatibleSpecializations, List<SpecializationData> implementedSpecializations) {
CodeTreeBuilder builder = parent.create();
List<SpecializationData> notImplemented = new ArrayList<>(compatibleSpecializations);
for (SpecializationData specialization : implementedSpecializations) {
notImplemented.remove(specialization);
}
if (notImplemented.isEmpty()) {
throw new AssertionError();
}
List<ExecutableTypeData> compatibleDelegateTypes = filterCompatibleExecutableTypes(type, delegateableTypes);
List<ExecutableTypeData> delegatedDelegateTypes = new ArrayList<>();
CodeTreeBuilder delegateBuilder = builder.create();
boolean elseIf = false;
boolean coversAllSpecializations = false;
if (boxingEliminationEnabled) {
Set<TypeMirror> optimizeTypes = new HashSet<>();
for (SpecializationData specialization : reachableSpecializations) {
TypeMirror returnType = specialization.getReturnType().getType();
if (ElementUtils.isPrimitive(returnType)) {
optimizeTypes.add(returnType);
}
}
for (TypeMirror optimizedType : ElementUtils.uniqueSortedTypes(optimizeTypes, true)) {
ExecutableTypeData delegateType = null;
for (ExecutableTypeData compatibleType : compatibleDelegateTypes) {
if (ElementUtils.typeEquals(compatibleType.getReturnType(), optimizedType)) {
delegateType = compatibleType;
break;
}
}
if (delegateType != null) {
List<SpecializationData> delegateSpecializations = filterImplementedSpecializations(delegateType, filterCompatibleSpecializations(delegateType, reachableSpecializations));
coversAllSpecializations = delegateSpecializations.size() == reachableSpecializations.size();
if (!coversAllSpecializations) {
builder.tree(state.createLoad(frameState));
elseIf = delegateBuilder.startIf(elseIf);
delegateBuilder.startGroup();
delegateBuilder.tree(state.createContainsOnly(frameState, 0, -1, delegateSpecializations.toArray(), reachableSpecializationsArray)).end();
delegateBuilder.string(" && ");
delegateBuilder.tree(state.createIsNotAny(frameState, reachableSpecializationsArray));
delegateBuilder.end();
delegateBuilder.end();
delegateBuilder.startBlock();
}
delegatedDelegateTypes.add(delegateType);
delegateBuilder.tree(createCallExecute(type, delegateType, frameState));
if (!coversAllSpecializations) {
delegateBuilder.end();
}
if (coversAllSpecializations) {
break;
}
}
}
}
if (!compatibleDelegateTypes.isEmpty() && !coversAllSpecializations) {
ExecutableTypeData delegateType = compatibleDelegateTypes.get(0);
coversAllSpecializations = notImplemented.size() == reachableSpecializations.size();
if (!coversAllSpecializations) {
builder.tree(state.createLoad(frameState));
elseIf = delegateBuilder.startIf(elseIf);
delegateBuilder.tree(state.createContains(frameState, notImplemented.toArray())).end();
delegateBuilder.startBlock();
}
delegatedDelegateTypes.add(delegateType);
delegateBuilder.tree(createCallExecute(type, delegateType, frameState));
if (!coversAllSpecializations) {
delegateBuilder.end();
}
}
boolean hasUnexpected = false;
for (ExecutableTypeData delegateType : delegatedDelegateTypes) {
if (needsUnexpectedResultException(delegateType)) {
hasUnexpected = true;
break;
}
}
if (hasUnexpected) {
builder.startTryBlock();
builder.tree(delegateBuilder.build());
builder.end().startCatchBlock(context.getType(UnexpectedResultException.class), "ex");
if (isVoid(type.getReturnType())) {
builder.returnStatement();
} else {
builder.startReturn();
builder.tree(expectOrCast(getType(Object.class), type, CodeTreeBuilder.singleString("ex")));
builder.end();
}
builder.end();
} else {
builder.tree(delegateBuilder.build());
}
return new ExecuteDelegationResult(builder.build(), !coversAllSpecializations);
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createFastPathExecute.
private CodeTree createFastPathExecute(CodeTreeBuilder parent, final ExecutableTypeData forType, SpecializationData specialization, FrameState frameState) {
CodeTreeBuilder builder = parent.create();
int ifCount = 0;
if (specialization.isFallback()) {
builder.startIf().startCall(METHOD_FALLBACK_GUARD);
if (fallbackNeedsFrame) {
if (frameState.get(FRAME_VALUE) != null) {
builder.string(FRAME_VALUE);
} else {
builder.nullLiteral();
}
}
if (fallbackNeedsState) {
builder.string(STATE_VALUE);
}
frameState.addReferencesTo(builder);
builder.end();
builder.end();
builder.startBlock();
ifCount++;
}
builder.tree(createExecute(builder, frameState, forType, specialization, NodeExecutionMode.FAST_PATH));
builder.end(ifCount);
return builder.build();
}
Aggregations