@DSLOptions public class TypeSystemParser extends AbstractParser<TypeSystemData> {
private Set<ExecutableTypeData> findSpecializedExecutableTypes(NodeExecutionData execution, TypeMirror type) { if (execution.getChild() == null) { return Collections.emptySet(); } ExecutableTypeData executableType = resolveExecutableType(execution.getChild(), type); Set<ExecutableTypeData> executedTypes = new HashSet<>(); executedTypes.add(executableType); if (typeSystem.hasImplicitSourceTypes(type)) { executedTypes.addAll(resolveSpecializedExecutables(execution, typeSystem.lookupSourceTypes(type), options.implicitTypeBoxingOptimization())); } return executedTypes; }
private boolean optimizeFallback(SpecializationData specialization) { switch (options.optimizeFallback()) { case NEVER: return false; case DECLARED: return specialization.getMethod() != null; case ALWAYS: return true; default: throw new AssertionError(); } }
private CodeTree createExecuteChild(NodeExecutionData execution, LocalVariable target, LocalContext currentValues, boolean shared) { final CodeTreeBuilder builder = CodeTreeBuilder.createBuilder(); CodeTree assignment = createAssignmentStart(target, shared); final Set<ExecutableTypeData> executableTypes = findSpecializedExecutableTypes(execution, target.getTypeMirror()); if (executableTypes.isEmpty()) { throw new AssertionError(); // cannot execute child } else if (executableTypes.size() == 1 && !typeSystem.hasImplicitSourceTypes(target.getTypeMirror())) { ExecutableTypeData executableType = executableTypes.iterator().next(); if (isObject(target.getTypeMirror()) && executableType.getEvaluatedCount() == 0) { return createPolymorphicExecuteChild(execution, target, currentValues, shared); } else { builder.tree(assignment); builder.tree(createSingleExecute(execution, target, currentValues, executableType)); } } else { if (options.implicitCastOptimization().isNone()) { throw new AssertionError("findSpecializedExecutableTypes is always 1 if implicit cast opt is disabled"); } else if (options.implicitCastOptimization().isDuplicateTail()) { builder.tree(createExecuteChildDuplicateTail(builder, execution, assignment, target, currentValues)); } else if (options.implicitCastOptimization().isMergeCasts()) { // TODO throw new UnsupportedOperationException(); } else { throw new AssertionError(); } } return builder.build(); }
private boolean isExecuteChildShared(NodeExecutionData execution, TypeMirror targetType) { if (isVoid(targetType)) { return false; } else if (isObject(targetType)) { return resolvePolymorphicExecutables(execution).size() >= 1; } else { if (!isTypeBoxingOptimized(options.monomorphicTypeBoxingOptimization(), targetType)) { return false; } if (!typeSystem.hasImplicitSourceTypes(targetType)) { return false; } int uses = 0; for (SpecializationData specialization : node.getSpecializations()) { List<Parameter> parameters = specialization.findByExecutionData(execution); for (Parameter parameter : parameters) { if (targetType.equals(parameter.getType())) { uses++; } } } if (uses > 1) { return resolveSpecializedExecutables(execution, typeSystem.lookupSourceTypes(targetType), options.implicitTypeBoxingOptimization()).size() > 1; } else { return false; } } }
if (isTypeBoxingOptimized(options.voidBoxingOptimization(), specialization.getReturnType().getType())) { return true; NodeExecutionData execution = node.getChildExecutions().get(i); TypeMirror specializedType = specialization.findParameterOrDie(execution).getType(); if (isTypeBoxingOptimized(options.monomorphicTypeBoxingOptimization(), specializedType)) { if (isTypeBoxingOptimized(options.monomorphicTypeBoxingOptimization(), executableType.getReturnType())) { return true;
private boolean useLazyClassLoading() { return options.useLazyClassLoading() && !singleSpecializable; }
private List<ExecutableTypeData> resolvePolymorphicExecutables(NodeExecutionData execution) { if (singleSpecializable) { return Collections.emptyList(); } Set<TypeMirror> specializedTypes = new HashSet<>(); for (TypeMirror type : node.findSpecializedTypes(execution)) { specializedTypes.addAll(typeSystem.lookupSourceTypes(type)); } return resolveSpecializedExecutables(execution, specializedTypes, options.polymorphicTypeBoxingElimination()); }
public CodeTypeElement create() { String typeName = typeName(forType); TypeMirror baseType = context.getType(Object.class); CodeTypeElement clazz = GeneratorUtils.createClass(typeSystem, null, modifiers(PUBLIC, FINAL, STATIC), typeName, baseType); for (TypeMirror sourceType : sourceTypes) { CodeVariableElement hasSeen = new CodeVariableElement(modifiers(PUBLIC), context.getType(boolean.class), seenFieldName(sourceType)); hasSeen.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(CompilationFinal.class))); clazz.add(hasSeen); } clazz.add(createConstructor(clazz)); if (isTypeBoxingOptimized(options.monomorphicTypeBoxingOptimization(), forType)) { clazz.add(createIsMonomorphic()); } clazz.add(createCast(false)); clazz.add(createCast(true)); clazz.add(createCheck()); clazz.add(createMerge(clazz)); clazz.add(createCreate(clazz)); return clazz; }
@Override public CodeTypeElement create(ProcessorContext context, TypeSystemData typeSystem) { CodeTypeElement clazz = new TypeClassFactory(context, typeSystem).create(); if (typeSystem.getOptions().implicitCastOptimization().isMergeCasts()) { for (TypeMirror type : typeSystem.lookupTargetTypes()) { clazz.add(new ImplicitCastNodeFactory(context, typeSystem, type).create()); } } return clazz; }
private CodeVariableElement createImplicitProfileParameter(NodeExecutionData execution, TypeMirror targetType) { if (typeSystem.hasImplicitSourceTypes(targetType)) { if (typeEquals(node.getGenericType(execution), targetType)) { return null; } switch (options.implicitCastOptimization()) { case NONE: return null; case DUPLICATE_TAIL: return new CodeVariableElement(getType(Class.class), implicitClassFieldName(execution)); case MERGE_CASTS: return new CodeVariableElement(ImplicitCastNodeFactory.type(typeSystem, targetType), implicitNodeFieldName(execution)); } } return null; }
List<TypeMirror> sourceTypes = typeSystem.lookupSourceTypes(target.getTypeMirror()); String implicitClassFieldName = implicitClassFieldName(execution); List<ExecutableTypeData> executableTypes = resolveSpecializedExecutables(execution, sourceTypes, options.implicitTypeBoxingOptimization());
@DSLOptions public class NodeParser extends AbstractParser<NodeData> {
private CodeExecutableElement createIsSameMethod(SpecializationData specialization) { if (!specialization.isSpecialized() || !options.implicitCastOptimization().isDuplicateTail()) { return null;
public CodeTypeElement create() { String name = typeName(typeSystem); CodeTypeElement clazz = GeneratorUtils.createClass(typeSystem, null, modifiers(PUBLIC, FINAL), name, typeSystem.getTemplateType().asType()); clazz.add(GeneratorUtils.createConstructorUsingFields(modifiers(PROTECTED), clazz)); CodeVariableElement singleton = createSingleton(clazz); clazz.add(singleton); for (TypeMirror type : typeSystem.getLegacyTypes()) { if (ElementUtils.isVoid(type) || ElementUtils.isObject(type)) { continue; } clazz.addOptional(createIsTypeMethod(type)); clazz.addOptional(createAsTypeMethod(type)); clazz.addOptional(createExpectTypeMethod(type, context.getType(Object.class))); } List<TypeMirror> lookupTargetTypes = typeSystem.lookupTargetTypes(); for (TypeMirror type : lookupTargetTypes) { clazz.add(createAsImplicitTypeMethod(type, false)); if (typeSystem.getOptions().implicitCastOptimization().isNone()) { clazz.add(createExpectImplicitTypeMethod(type, false)); } clazz.add(createIsImplicitTypeMethod(type, false)); if (typeSystem.getOptions().implicitCastOptimization().isDuplicateTail()) { clazz.add(createAsImplicitTypeMethod(type, true)); clazz.add(createExpectImplicitTypeMethod(type, true)); clazz.add(createIsImplicitTypeMethod(type, true)); clazz.add(createGetImplicitClass(type)); } } return clazz; }
castBuilder.tree(TypeSystemCodeGenerator.cast(typeSystem, targetType, valueReference)); } else { ImplicitCastOptimization opt = options.implicitCastOptimization(); if (specializationExecution.isFastPath() && !opt.isNone()) { if (opt.isDuplicateTail()) {
if (options.implicitCastOptimization().isDuplicateTail()) { constructor.addParameter(var.createParameter()); CodeTree implicitType = TypeSystemCodeGenerator.implicitType(typeSystem, p.getType(), var.createReference()); builder.startStatement().string("this.").string(implicitFieldName).string(" = ").tree(implicitType).end(); } else if (options.implicitCastOptimization().isMergeCasts()) {