use of com.oracle.truffle.dsl.processor.java.model.CodeTree in project graal by oracle.
the class FlatNodeGenFactory method createFastPathExecuteChild.
private CodeTree createFastPathExecuteChild(final CodeTreeBuilder parent, FrameState originalFrameState, FrameState frameState, final ExecutableTypeData currentType, SpecializationGroup group, NodeExecutionData execution) {
CodeTreeBuilder builder = parent.create();
LocalVariable var = frameState.getValue(execution);
if (var == null) {
TypeMirror targetType;
TypeGuard eliminatedGuard = null;
if (boxingEliminationEnabled) {
for (TypeGuard checkedGuard : group.getTypeGuards()) {
if (!ElementUtils.isPrimitive(checkedGuard.getType())) {
// no elimination for non primitive types
continue;
} else if (node.getChildExecutions().get(checkedGuard.getSignatureIndex()).getChild().findExecutableType(checkedGuard.getType()) == null) {
// type cannot be executed so it cannot be eliminated
continue;
}
if (checkedGuard.getSignatureIndex() == execution.getIndex()) {
eliminatedGuard = checkedGuard;
break;
}
}
}
if (eliminatedGuard != null) {
// we can optimize the type guard away by executing it
group.getTypeGuards().remove(eliminatedGuard);
targetType = eliminatedGuard.getType();
} else {
targetType = execution.getChild().findAnyGenericExecutableType(context).getReturnType();
}
var = frameState.createValue(execution, targetType).nextName();
LocalVariable fallbackVar;
List<TypeMirror> originalSourceTypes = typeSystem.lookupSourceTypes(targetType);
List<TypeMirror> sourceTypes = resolveOptimizedImplicitSourceTypes(execution, targetType);
if (sourceTypes.size() > 1) {
TypeGuard typeGuard = new TypeGuard(targetType, execution.getIndex());
TypeMirror generic = node.getPolymorphicSpecialization().findParameterOrDie(execution).getType();
fallbackVar = originalFrameState.createValue(execution, generic);
// we want to create the check tree in reverse order
Collections.reverse(sourceTypes);
CodeTree access = var.createReference();
boolean first = true;
for (TypeMirror sType : sourceTypes) {
if (ElementUtils.typeEquals(sType, targetType)) {
continue;
}
String localName = createSourceTypeLocalName(var, sType);
builder.declaration(sType, localName, CodeTreeBuilder.createBuilder().defaultValue(sType).build());
CodeTreeBuilder accessBuilder = builder.create();
accessBuilder.startParantheses();
accessBuilder.tree(state.createContainsOnly(frameState, originalSourceTypes.indexOf(sType), 1, new Object[] { typeGuard }, new Object[] { typeGuard }));
accessBuilder.string(" && ");
accessBuilder.tree(state.createIsNotAny(frameState, reachableSpecializationsArray));
accessBuilder.string(" ? ");
if (ElementUtils.isPrimitive(sType)) {
accessBuilder.string("(").type(generic).string(") ");
}
accessBuilder.string(localName);
accessBuilder.string(" : ");
if (first && ElementUtils.isPrimitive(targetType)) {
accessBuilder.string("(").type(generic).string(") ");
}
accessBuilder.tree(access);
accessBuilder.end();
access = accessBuilder.build();
first = false;
}
fallbackVar = fallbackVar.accessWith(access);
} else {
fallbackVar = var;
}
builder.tree(createAssignExecuteChild(originalFrameState, frameState, builder, execution, currentType, var));
frameState.setValue(execution, var);
originalFrameState.setValue(execution, fallbackVar);
}
return builder.build();
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTree in project graal by oracle.
the class FlatNodeGenFactory method persistAndInitializeCache.
private Collection<IfTriple> persistAndInitializeCache(FrameState frameState, SpecializationData specialization, CacheExpression cache, boolean store, boolean persist) {
List<IfTriple> triples = new ArrayList<>();
CodeTree init = initializeCache(frameState, specialization, cache);
if (store) {
triples.addAll(storeCache(frameState, specialization, cache, init));
}
if (persist) {
triples.addAll(persistCache(frameState, specialization, cache, init));
}
return triples;
}
use of com.oracle.truffle.dsl.processor.java.model.CodeTree 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.CodeTree 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.CodeTree in project graal by oracle.
the class FlatNodeGenFactory method castBoundTypes.
private static Map<Variable, CodeTree> castBoundTypes(Map<Variable, LocalVariable> bindings) {
Map<Variable, CodeTree> resolvedBindings = new HashMap<>();
for (Variable variable : bindings.keySet()) {
LocalVariable localVariable = bindings.get(variable);
CodeTree resolved = localVariable.createReference();
TypeMirror sourceType = localVariable.getTypeMirror();
TypeMirror targetType = variable.getResolvedTargetType();
if (targetType == null) {
targetType = variable.getResolvedType();
}
if (!ElementUtils.isAssignable(sourceType, targetType)) {
resolved = CodeTreeBuilder.createBuilder().startParantheses().cast(targetType, resolved).end().build();
}
resolvedBindings.put(variable, resolved);
}
return resolvedBindings;
}
Aggregations