use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method storeCache.
private Collection<IfTriple> storeCache(FrameState frameState, SpecializationData specialization, CacheExpression cache, CodeTree value) {
if (value == null) {
return Collections.emptyList();
}
String name = createFieldName(specialization, cache.getParameter());
LocalVariable var = frameState.get(name);
if (var != null) {
// already initialized
return Collections.emptyList();
}
TypeMirror type = cache.getParameter().getType();
CodeTreeBuilder builder = new CodeTreeBuilder(null);
String refName = name + "_";
builder.declaration(type, refName, value);
frameState.set(name, new LocalVariable(type, name, CodeTreeBuilder.singleString(refName)));
return Arrays.asList(new IfTriple(builder.build(), null, null));
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createMethodGuardCheck.
private IfTriple createMethodGuardCheck(FrameState frameState, SpecializationData specialization, GuardExpression guard, NodeExecutionMode mode) {
DSLExpression expression = guard.getExpression();
Map<Variable, CodeTree> resolvedBindings = castBoundTypes(bindExpressionValues(frameState, expression, specialization));
CodeTree init = null;
CodeTree expressionCode = DSLExpressionGenerator.write(expression, null, resolvedBindings);
if (mode.isGuardFallback()) {
GuardExpression guardWithBit = getGuardThatNeedsStateBit(specialization, guard);
if (guardWithBit != null) {
CodeTreeBuilder builder = new CodeTreeBuilder(null);
builder.string("(");
builder.tree(state.createNotContains(frameState, new Object[] { guardWithBit }));
builder.string(" || ");
builder.tree(expressionCode);
builder.string(")");
expressionCode = builder.build();
fallbackNeedsState = true;
}
}
// overrule with assertion
CodeTree assertion = null;
if (mode.isFastPath() || mode.isGuardFallback()) {
if (!specialization.isDynamicParameterBound(expression)) {
assertion = CodeTreeBuilder.createBuilder().startAssert().tree(expressionCode).end().build();
expressionCode = null;
}
} else {
if (guard.isConstantTrueInSlowPath(context)) {
assertion = CodeTreeBuilder.createBuilder().startStatement().string("// assert ").tree(expressionCode).end().build();
expressionCode = null;
}
}
return new IfTriple(init, expressionCode, assertion);
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createAccessChildMethod.
private ExecutableElement createAccessChildMethod(NodeChildData child) {
if (child.getAccessElement() != null && child.getAccessElement().getModifiers().contains(Modifier.ABSTRACT)) {
ExecutableElement getter = (ExecutableElement) child.getAccessElement();
CodeExecutableElement method = CodeExecutableElement.clone(context.getEnvironment(), getter);
method.getModifiers().remove(Modifier.ABSTRACT);
List<NodeExecutionData> executions = new ArrayList<>();
for (NodeExecutionData execution : node.getChildExecutions()) {
if (execution.getChild() == child) {
executions.add(execution);
}
}
CodeTreeBuilder builder = method.createBuilder();
if (child.getCardinality().isMany()) {
builder.startReturn().startNewArray((ArrayType) child.getOriginalType(), null);
for (NodeExecutionData execution : executions) {
builder.string(accessNodeField(execution));
}
builder.end().end();
} else {
for (NodeExecutionData execution : executions) {
builder.startReturn().string(accessNodeField(execution)).end();
break;
}
}
return method;
}
return null;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder in project graal by oracle.
the class FlatNodeGenFactory method createCallExecute.
private CodeTree createCallExecute(ExecutableTypeData forType, ExecutableTypeData targetType, FrameState frameState) {
TypeMirror returnType = targetType.getReturnType();
List<CodeTree> bindings = new ArrayList<>();
List<TypeMirror> sourceTypes = forType.getSignatureParameters();
List<TypeMirror> targetTypes = targetType.getSignatureParameters();
if (sourceTypes.size() != targetTypes.size()) {
throw new IllegalArgumentException();
}
if (targetType.getFrameParameter() != null) {
LocalVariable parameterLocal = frameState.get(FRAME_VALUE);
TypeMirror parameterTargetType = targetType.getFrameParameter();
if (parameterLocal == null) {
bindings.add(CodeTreeBuilder.createBuilder().defaultValue(parameterTargetType).build());
} else {
bindings.add(parameterLocal.createReference());
}
}
for (int i = 0; i < sourceTypes.size(); i++) {
LocalVariable parameterLocal = frameState.getValue(i);
TypeMirror parameterTargetType = targetTypes.get(i);
if (parameterLocal == null) {
bindings.add(CodeTreeBuilder.createBuilder().defaultValue(parameterTargetType).build());
} else {
bindings.add(parameterLocal.createReference());
}
}
CodeTree call = callMethod(null, targetType.getMethod(), bindings.toArray(new CodeTree[0]));
CodeTreeBuilder builder = CodeTreeBuilder.createBuilder();
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 createNodeConstructor.
// old code
private CodeExecutableElement createNodeConstructor(CodeTypeElement clazz, ExecutableElement superConstructor) {
CodeExecutableElement constructor = GeneratorUtils.createConstructorUsingFields(modifiers(), clazz, superConstructor);
ElementUtils.setVisibility(constructor.getModifiers(), ElementUtils.getVisibility(superConstructor.getModifiers()));
constructor.setVarArgs(superConstructor.isVarArgs());
List<CodeVariableElement> childParameters = new ArrayList<>();
for (NodeChildData child : node.getChildren()) {
if (child.needsGeneratedField()) {
childParameters.add(new CodeVariableElement(child.getOriginalType(), child.getName()));
}
}
constructor.getParameters().addAll(superConstructor.getParameters().size(), childParameters);
CodeTreeBuilder builder = constructor.appendBuilder();
List<String> childValues = new ArrayList<>(node.getChildren().size());
if (!node.getChildExecutions().isEmpty()) {
for (NodeChildData child : node.getChildren()) {
if (child.needsGeneratedField()) {
String name = child.getName();
if (child.getCardinality().isMany()) {
CreateCastData createCast = node.findCast(child.getName());
if (createCast != null) {
CodeTree nameTree = CodeTreeBuilder.singleString(name);
CodeTreeBuilder callBuilder = builder.create();
callBuilder.string(name).string(" != null ? ");
callBuilder.tree(callMethod(null, createCast.getMethod(), nameTree));
callBuilder.string(" : null");
name += "_";
builder.declaration(child.getNodeType(), name, callBuilder.build());
}
}
childValues.add(name);
}
}
}
for (NodeExecutionData execution : node.getChildExecutions()) {
if (execution.getChild() == null || !execution.getChild().needsGeneratedField()) {
continue;
}
CreateCastData createCast = node.findCast(execution.getChild().getName());
builder.startStatement();
builder.string("this.").string(nodeFieldName(execution)).string(" = ");
String name = childValues.get(node.getChildren().indexOf(execution.getChild()));
CodeTreeBuilder accessorBuilder = builder.create();
accessorBuilder.string(name);
if (execution.hasChildArrayIndex()) {
accessorBuilder.string("[").string(String.valueOf(execution.getChildArrayIndex())).string("]");
}
CodeTree accessor = accessorBuilder.build();
if (createCast != null && execution.getChild().getCardinality().isOne()) {
accessor = callMethod(null, createCast.getMethod(), accessor);
}
if (execution.hasChildArrayIndex()) {
CodeTreeBuilder nullCheck = builder.create();
nullCheck.string(name).string(" != null && ").string(String.valueOf(execution.getChildArrayIndex())).string(" < ").string(name).string(".length").string(" ? ");
nullCheck.tree(accessor);
nullCheck.string(" : null");
accessor = nullCheck.build();
}
builder.tree(accessor);
builder.end();
}
return constructor;
}
Aggregations