use of com.oracle.truffle.dsl.processor.model.GuardExpression in project graal by oracle.
the class SpecializationGroup method combine.
private static SpecializationGroup combine(List<SpecializationGroup> groups) {
if (groups.isEmpty()) {
throw new IllegalArgumentException("empty combinations");
}
if (groups.size() == 1) {
return null;
}
List<TypeGuard> typeGuardsMatches = new ArrayList<>();
List<GuardExpression> guardMatches = new ArrayList<>();
SpecializationGroup first = groups.get(0);
List<SpecializationGroup> others = groups.subList(1, groups.size());
outer: for (TypeGuard typeGuard : first.typeGuards) {
for (SpecializationGroup other : others) {
if (!other.typeGuards.contains(typeGuard)) {
// type guards can be combined unordered
continue outer;
}
}
typeGuardsMatches.add(typeGuard);
}
outer: for (GuardExpression guard : first.guards) {
for (SpecializationGroup other : others) {
if (!other.guards.contains(guard)) {
// we must break here. One guard may depend on the other.
break outer;
}
}
guardMatches.add(guard);
}
// check for guards for required type casts
for (Iterator<GuardExpression> iterator = guardMatches.iterator(); iterator.hasNext(); ) {
GuardExpression guardMatch = iterator.next();
if (!guardMatch.getExpression().findBoundVariables().isEmpty()) {
iterator.remove();
}
// TODO we need to be smarter here with bound parameters.
}
if (typeGuardsMatches.isEmpty() && guardMatches.isEmpty()) {
return null;
}
for (SpecializationGroup group : groups) {
group.typeGuards.removeAll(typeGuardsMatches);
group.guards.removeAll(guardMatches);
}
List<SpecializationGroup> newChildren = new ArrayList<>(groups);
return new SpecializationGroup(newChildren, typeGuardsMatches, guardMatches);
}
use of com.oracle.truffle.dsl.processor.model.GuardExpression in project graal by oracle.
the class NodeParser method initializeGuards.
private void initializeGuards(SpecializationData specialization, DSLExpressionResolver resolver) {
final TypeMirror booleanType = context.getType(boolean.class);
List<String> guardDefinitions = ElementUtils.getAnnotationValueList(String.class, specialization.getMarkerAnnotation(), "guards");
List<GuardExpression> guardExpressions = new ArrayList<>();
for (String guard : guardDefinitions) {
GuardExpression guardExpression;
DSLExpression expression = null;
try {
expression = DSLExpression.parse(guard);
expression.accept(resolver);
guardExpression = new GuardExpression(specialization, expression);
if (!ElementUtils.typeEquals(expression.getResolvedType(), booleanType)) {
guardExpression.addError("Incompatible return type %s. Guards must return %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(booleanType));
}
} catch (InvalidExpressionException e) {
guardExpression = new GuardExpression(specialization, null);
guardExpression.addError("Error parsing expression '%s': %s", guard, e.getMessage());
}
guardExpressions.add(guardExpression);
}
specialization.setGuards(guardExpressions);
}
use of com.oracle.truffle.dsl.processor.model.GuardExpression in project graal by oracle.
the class SpecializationGroup method findNegatedGuardInPrevious.
private GuardExpression findNegatedGuardInPrevious(GuardExpression guard) {
SpecializationGroup previous = this.getPreviousGroup();
if (previous == null) {
return null;
}
List<GuardExpression> elseConnectedGuards = previous.findElseConnectableGuards();
if (previous == null || previous.getGuards().size() != elseConnectedGuards.size() + 1) {
return null;
}
/* Guard is else branch can be connected in previous specialization. */
if (elseConnectedGuards.contains(guard)) {
return guard;
}
GuardExpression previousGuard = previous.getGuards().get(elseConnectedGuards.size());
if (guard.equalsNegated(previousGuard)) {
return guard;
}
return null;
}
use of com.oracle.truffle.dsl.processor.model.GuardExpression 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);
}
Aggregations