GeneratedNodeIntrinsicPlugin.java revision 12995:5e441a7ec5e3
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.replacements.verifier;
24
25import java.io.PrintWriter;
26import java.util.List;
27import java.util.Set;
28
29import javax.annotation.processing.ProcessingEnvironment;
30import javax.lang.model.element.ExecutableElement;
31import javax.lang.model.element.VariableElement;
32import javax.lang.model.type.DeclaredType;
33import javax.lang.model.type.TypeKind;
34import javax.lang.model.type.TypeMirror;
35
36import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
37import org.graalvm.compiler.graph.Node.InjectedNodeParameter;
38import org.graalvm.compiler.graph.Node.NodeIntrinsic;
39
40/**
41 * Create graph builder plugins for {@link NodeIntrinsic} methods.
42 */
43public abstract class GeneratedNodeIntrinsicPlugin extends GeneratedPlugin {
44
45    private final TypeMirror[] signature;
46
47    public GeneratedNodeIntrinsicPlugin(ExecutableElement intrinsicMethod, TypeMirror[] signature) {
48        super(intrinsicMethod);
49        this.signature = signature;
50    }
51
52    private static TypeMirror valueNodeType(ProcessingEnvironment env) {
53        return env.getElementUtils().getTypeElement("org.graalvm.compiler.nodes.ValueNode").asType();
54    }
55
56    protected abstract List<? extends VariableElement> getParameters();
57
58    protected abstract void factoryCall(ProcessingEnvironment env, PrintWriter out, InjectedDependencies deps, int argCount);
59
60    @Override
61    protected InjectedDependencies createExecute(ProcessingEnvironment env, PrintWriter out) {
62        InjectedDependencies deps = new InjectedDependencies();
63
64        List<? extends VariableElement> params = getParameters();
65
66        int idx = 0;
67        for (; idx < params.size(); idx++) {
68            VariableElement param = params.get(idx);
69            if (param.getAnnotation(InjectedNodeParameter.class) == null) {
70                break;
71            }
72
73            out.printf("            %s arg%d = %s;\n", param.asType(), idx, deps.use(env, (DeclaredType) param.asType()));
74        }
75
76        for (int i = 0; i < signature.length; i++, idx++) {
77            if (intrinsicMethod.getParameters().get(i).getAnnotation(ConstantNodeParameter.class) != null) {
78                constantArgument(env, out, deps, idx, signature[i], i);
79            } else {
80                if (signature[i].equals(valueNodeType(env))) {
81                    out.printf("            ValueNode arg%d = args[%d];\n", idx, i);
82                } else {
83                    out.printf("            %s arg%d = (%s) args[%d];\n", signature[i], idx, signature[i], i);
84                }
85            }
86        }
87
88        factoryCall(env, out, deps, idx);
89
90        return deps;
91    }
92
93    public static class ConstructorPlugin extends GeneratedNodeIntrinsicPlugin {
94
95        private final ExecutableElement constructor;
96
97        public ConstructorPlugin(ExecutableElement intrinsicMethod, ExecutableElement constructor, TypeMirror[] signature) {
98            super(intrinsicMethod, signature);
99            this.constructor = constructor;
100        }
101
102        @Override
103        public void extraImports(Set<String> imports) {
104            if (intrinsicMethod.getReturnType().getKind() != TypeKind.VOID) {
105                imports.add("jdk.vm.ci.meta.JavaKind");
106            }
107        }
108
109        @Override
110        protected List<? extends VariableElement> getParameters() {
111            return constructor.getParameters();
112        }
113
114        @Override
115        protected void factoryCall(ProcessingEnvironment env, PrintWriter out, InjectedDependencies deps, int argCount) {
116            out.printf("            %s node = new %s(", constructor.getEnclosingElement(), constructor.getEnclosingElement());
117            if (argCount > 0) {
118                out.printf("arg0");
119                for (int i = 1; i < argCount; i++) {
120                    out.printf(", arg%d", i);
121                }
122            }
123            out.printf(");\n");
124
125            if (intrinsicMethod.getReturnType().getKind() == TypeKind.VOID) {
126                out.printf("            b.add(node);\n");
127            } else {
128                out.printf("            b.addPush(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod));
129            }
130            out.printf("            return true;\n");
131        }
132    }
133
134    public static class CustomFactoryPlugin extends GeneratedNodeIntrinsicPlugin {
135
136        private final ExecutableElement customFactory;
137
138        public CustomFactoryPlugin(ExecutableElement intrinsicMethod, ExecutableElement customFactory, TypeMirror[] signature) {
139            super(intrinsicMethod, signature);
140            this.customFactory = customFactory;
141        }
142
143        @Override
144        public void extraImports(Set<String> imports) {
145        }
146
147        @Override
148        protected List<? extends VariableElement> getParameters() {
149            List<? extends VariableElement> ret = customFactory.getParameters();
150            // remove initial GraphBuilderContext and ResolvedJavaMethod parameters
151            return ret.subList(2, ret.size());
152        }
153
154        @Override
155        protected void factoryCall(ProcessingEnvironment env, PrintWriter out, InjectedDependencies deps, int argCount) {
156            out.printf("            return %s.%s(b, targetMethod", customFactory.getEnclosingElement(), customFactory.getSimpleName());
157            for (int i = 0; i < argCount; i++) {
158                out.printf(", arg%d", i);
159            }
160            out.printf(");\n");
161        }
162    }
163}
164