use of com.oracle.truffle.dsl.processor.model.CacheExpression in project graal by oracle.
the class FlatNodeGenFactory method generateReflectionInfo.
private void generateReflectionInfo(CodeTypeElement clazz) {
clazz.getImplements().add(context.getType(Introspection.Provider.class));
CodeExecutableElement reflection = new CodeExecutableElement(modifiers(PUBLIC), context.getType(Introspection.class), "getIntrospectionData");
CodeTreeBuilder builder = reflection.createBuilder();
List<SpecializationData> filteredSpecializations = new ArrayList<>();
for (SpecializationData s : node.getSpecializations()) {
if (s.getMethod() == null) {
continue;
}
filteredSpecializations.add(s);
}
ArrayCodeTypeMirror objectArray = new ArrayCodeTypeMirror(context.getType(Object.class));
builder.declaration(objectArray, "data", builder.create().startNewArray(objectArray, CodeTreeBuilder.singleString(String.valueOf(filteredSpecializations.size() + 1))).end().build());
builder.declaration(objectArray, "s", (CodeTree) null);
// declare version 0
builder.statement("data[0] = 0");
FrameState frameState = FrameState.load(this);
builder.tree(state.createLoad(frameState));
if (requiresExclude()) {
builder.tree(exclude.createLoad(frameState));
}
int index = 1;
for (SpecializationData specialization : filteredSpecializations) {
builder.startStatement().string("s = ").startNewArray(objectArray, CodeTreeBuilder.singleString("3")).end().end();
builder.startStatement().string("s[0] = ").doubleQuote(specialization.getMethodName()).end();
builder.startIf().tree(state.createContains(frameState, new Object[] { specialization })).end().startBlock();
builder.startStatement().string("s[1] = (byte)0b01 /* active */").end();
TypeMirror listType = new DeclaredCodeTypeMirror((TypeElement) context.getDeclaredType(ArrayList.class).asElement(), Arrays.asList(context.getType(Object.class)));
if (!specialization.getCaches().isEmpty()) {
builder.declaration(listType, "cached", "new ArrayList<>()");
boolean useSpecializationClass = useSpecializationClass(specialization);
String name = createSpecializationLocalName(specialization);
if (useSpecializationClass) {
builder.tree(loadSpecializationClass(frameState, specialization));
if (specialization.hasMultipleInstances()) {
builder.startWhile();
} else {
builder.startIf();
}
builder.string(name, " != null");
builder.end();
builder.startBlock();
}
builder.startStatement().startCall("cached", "add");
builder.startStaticCall(context.getType(Arrays.class), "asList");
for (CacheExpression cache : specialization.getCaches()) {
builder.startGroup();
builder.tree(createCacheReference(frameState, specialization, cache.getParameter()));
builder.end();
}
builder.end();
builder.end().end();
if (useSpecializationClass) {
if (specialization.getMaximumNumberOfInstances() > 1) {
builder.startStatement().string(name, " = ", name, ".next_").end();
}
// cache while or if
builder.end();
}
builder.statement("s[2] = cached");
}
builder.end();
if (mayBeExcluded(specialization)) {
builder.startElseIf().tree(exclude.createContains(frameState, new Object[] { specialization })).end().startBlock();
builder.startStatement().string("s[1] = (byte)0b10 /* excluded */").end();
builder.end();
}
builder.startElseBlock();
builder.startStatement().string("s[1] = (byte)0b00 /* inactive */").end();
builder.end();
builder.startStatement().string("data[", String.valueOf(index), "] = s").end();
index++;
}
builder.startReturn().startStaticCall(context.getType(Introspection.Provider.class), "create").string("data").end().end();
clazz.add(reflection);
}
use of com.oracle.truffle.dsl.processor.model.CacheExpression in project graal by oracle.
the class FlatNodeGenFactory method createFields.
private void createFields(CodeTypeElement clazz) {
state.declareFields(clazz);
if (requiresExclude()) {
exclude.declareFields(clazz);
}
for (SpecializationData specialization : reachableSpecializations) {
List<CodeVariableElement> fields = new ArrayList<>();
boolean useSpecializationClass = useSpecializationClass(specialization);
for (CacheExpression cache : specialization.getCaches()) {
Parameter parameter = cache.getParameter();
String fieldName = createFieldName(specialization, parameter);
TypeMirror type = parameter.getType();
Modifier visibility = useSpecializationClass ? null : Modifier.PRIVATE;
CodeVariableElement cachedField;
if (ElementUtils.isAssignable(type, context.getType(NodeInterface.class))) {
cachedField = createNodeField(visibility, type, fieldName, Child.class);
} else if (isNodeInterfaceArray(type)) {
cachedField = createNodeField(visibility, type, fieldName, Children.class);
} else {
cachedField = createNodeField(visibility, type, fieldName, null);
setFieldCompilationFinal(cachedField, parameter.getVariableElement().getAnnotation(Cached.class).dimensions());
}
fields.add(cachedField);
}
for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
String fieldName = createAssumptionFieldName(specialization, assumption);
TypeMirror type;
int compilationFinalDimensions;
if (assumption.getExpression().getResolvedType().getKind() == TypeKind.ARRAY) {
type = context.getType(Assumption[].class);
compilationFinalDimensions = 1;
} else {
type = context.getType(Assumption.class);
compilationFinalDimensions = -1;
}
CodeVariableElement assumptionField;
if (useSpecializationClass) {
assumptionField = createNodeField(null, type, fieldName, null);
} else {
assumptionField = createNodeField(PRIVATE, type, fieldName, null);
}
setFieldCompilationFinal(assumptionField, compilationFinalDimensions);
fields.add(assumptionField);
}
if (useSpecializationClass) {
TypeMirror baseType;
boolean useNode = specializationClassIsNode(specialization);
if (useNode) {
baseType = context.getType(Node.class);
} else {
baseType = context.getType(Object.class);
}
CodeTypeElement cacheType = GeneratorUtils.createClass(node, null, modifiers(PRIVATE, FINAL, STATIC), createSpecializationTypeName(specialization), baseType);
Class<?> annotationType;
if (useNode) {
annotationType = Child.class;
if (specialization.getMaximumNumberOfInstances() > 1) {
cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class));
}
CodeExecutableElement getNodeCost = new CodeExecutableElement(modifiers(PUBLIC), context.getType(NodeCost.class), "getCost");
getNodeCost.createBuilder().startReturn().staticReference(context.getType(NodeCost.class), "NONE").end();
cacheType.add(getNodeCost);
} else {
annotationType = CompilationFinal.class;
if (specialization.getMaximumNumberOfInstances() > 1) {
cacheType.add(createNodeField(null, cacheType.asType(), "next_", annotationType));
}
}
cacheType.add(GeneratorUtils.createConstructorUsingFields(modifiers(), cacheType));
cacheType.getEnclosedElements().addAll(fields);
clazz.add(createNodeField(PRIVATE, cacheType.asType(), createSpecializationFieldName(specialization), annotationType));
clazz.add(cacheType);
specializationClasses.put(specialization, cacheType);
} else {
clazz.getEnclosedElements().addAll(fields);
}
}
}
use of com.oracle.truffle.dsl.processor.model.CacheExpression in project graal by oracle.
the class NodeParser method initializeCaches.
private void initializeCaches(SpecializationData specialization, DSLExpressionResolver resolver) {
TypeMirror cacheMirror = context.getType(Cached.class);
List<CacheExpression> expressions = new ArrayList<>();
for (Parameter parameter : specialization.getParameters()) {
AnnotationMirror annotationMirror = ElementUtils.findAnnotationMirror(parameter.getVariableElement().getAnnotationMirrors(), cacheMirror);
if (annotationMirror != null) {
String initializer = ElementUtils.getAnnotationValue(String.class, annotationMirror, "value");
TypeMirror parameterType = parameter.getType();
DSLExpressionResolver localResolver = resolver;
if (parameterType.getKind() == TypeKind.DECLARED) {
localResolver = localResolver.copy(importPublicStaticMembers(ElementUtils.fromTypeMirror(parameterType), true));
}
CacheExpression cacheExpression;
DSLExpression expression = null;
try {
expression = DSLExpression.parse(initializer);
expression.accept(localResolver);
cacheExpression = new CacheExpression(parameter, annotationMirror, expression);
if (!ElementUtils.typeEquals(expression.getResolvedType(), parameter.getType())) {
cacheExpression.addError("Incompatible return type %s. The expression type must be equal to the parameter type %s.", ElementUtils.getSimpleName(expression.getResolvedType()), ElementUtils.getSimpleName(parameter.getType()));
}
} catch (InvalidExpressionException e) {
cacheExpression = new CacheExpression(parameter, annotationMirror, null);
cacheExpression.addError("Error parsing expression '%s': %s", initializer, e.getMessage());
}
if (!cacheExpression.hasErrors()) {
Cached cached = cacheExpression.getParameter().getVariableElement().getAnnotation(Cached.class);
cacheExpression.setDimensions(cached.dimensions());
if (parameterType.getKind() == TypeKind.ARRAY && !ElementUtils.isSubtype(((ArrayType) parameterType).getComponentType(), context.getType(NodeInterface.class))) {
if (cacheExpression.getDimensions() == -1) {
cacheExpression.addWarning("The cached dimensions attribute must be specified for array types.");
}
} else {
if (cacheExpression.getDimensions() != -1) {
cacheExpression.addError("The dimensions attribute has no affect for the type %s.", ElementUtils.getSimpleName(parameterType));
}
}
}
expressions.add(cacheExpression);
}
}
specialization.setCaches(expressions);
if (specialization.hasErrors()) {
return;
}
// verify that cache expressions are bound in the correct order.
for (int i = 0; i < expressions.size(); i++) {
CacheExpression currentExpression = expressions.get(i);
Set<VariableElement> boundVariables = currentExpression.getExpression().findBoundVariableElements();
for (int j = i + 1; j < expressions.size(); j++) {
CacheExpression boundExpression = expressions.get(j);
if (boundVariables.contains(boundExpression.getParameter().getVariableElement())) {
currentExpression.addError("The initializer expression of parameter '%s' binds unitialized parameter '%s. Reorder the parameters to resolve the problem.", currentExpression.getParameter().getLocalName(), boundExpression.getParameter().getLocalName());
break;
}
}
}
}
use of com.oracle.truffle.dsl.processor.model.CacheExpression in project graal by oracle.
the class FlatNodeGenFactory method initializeCaches.
private List<IfTriple> initializeCaches(FrameState frameState, SpecializationGroup group, Collection<CacheExpression> caches, NodeExecutionMode mode, boolean store, boolean forcePersist) {
if (mode != NodeExecutionMode.SLOW_PATH || group.getSpecialization() == null) {
return Collections.emptyList();
}
List<IfTriple> triples = new ArrayList<>();
if (!caches.isEmpty()) {
// preinitialize caches for guards in local variables
for (CacheExpression cache : caches) {
triples.addAll(initializeCasts(frameState, group, cache.getExpression(), mode));
triples.addAll(persistAndInitializeCache(frameState, group.getSpecialization(), cache, store, forcePersist));
}
}
return triples;
}
Aggregations