|
28 | 28 | import static jdk.graal.compiler.replacements.processor.FoldHandler.INJECTED_PARAMETER_CLASS_NAME;
|
29 | 29 |
|
30 | 30 | import java.io.PrintWriter;
|
| 31 | +import java.io.StringWriter; |
31 | 32 | import java.util.List;
|
32 | 33 | import java.util.Set;
|
33 | 34 | import java.util.TreeSet;
|
@@ -66,138 +67,55 @@ public void extraImports(AbstractProcessor processor, Set<String> imports) {
|
66 | 67 | imports.add("jdk.vm.ci.meta.JavaConstant");
|
67 | 68 | imports.add("jdk.vm.ci.meta.JavaKind");
|
68 | 69 | imports.add("jdk.graal.compiler.nodes.ConstantNode");
|
69 |
| - imports.add("jdk.graal.compiler.core.common.type.Stamp"); |
70 | 70 | }
|
71 | 71 |
|
72 | 72 | @Override
|
73 | 73 | protected void createExecute(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
|
74 |
| - List<? extends VariableElement> params = intrinsicMethod.getParameters(); |
75 |
| - |
76 | 74 | out.printf(" if (b.shouldDeferPlugin(this)) {\n");
|
77 | 75 | out.printf(" b.replacePlugin%s(this, targetMethod, args, %s.FUNCTION);\n", getReplacementFunctionSuffix(processor), getReplacementName());
|
78 | 76 | out.printf(" return true;\n");
|
79 | 77 | out.printf(" }\n");
|
80 | 78 |
|
81 |
| - int argCount = 0; |
82 |
| - Object receiver; |
83 |
| - if (intrinsicMethod.getModifiers().contains(Modifier.STATIC)) { |
84 |
| - receiver = intrinsicMethod.getEnclosingElement(); |
85 |
| - } else { |
86 |
| - receiver = "arg0"; |
87 |
| - TypeElement type = (TypeElement) intrinsicMethod.getEnclosingElement(); |
88 |
| - constantArgument(processor, out, deps, argCount, type.asType(), argCount, false); |
89 |
| - argCount++; |
90 |
| - } |
91 |
| - |
92 |
| - int firstArg = argCount; |
93 |
| - for (VariableElement param : params) { |
94 |
| - if (processor.getAnnotation(param, processor.getType(INJECTED_PARAMETER_CLASS_NAME)) == null) { |
95 |
| - constantArgument(processor, out, deps, argCount, param.asType(), argCount, false); |
96 |
| - } else { |
| 79 | + int argCount = intrinsicMethod.getModifiers().contains(Modifier.STATIC) ? 0 : 1; |
| 80 | + for (VariableElement param : intrinsicMethod.getParameters()) { |
| 81 | + if (processor.getAnnotation(param, processor.getType(INJECTED_PARAMETER_CLASS_NAME)) != null) { |
97 | 82 | out.printf(" if (!checkInjectedArgument(b, args[%d], targetMethod)) {\n", argCount);
|
98 | 83 | out.printf(" return false;\n");
|
99 | 84 | out.printf(" }\n");
|
100 |
| - out.printf(" %s arg%d = %s;\n", param.asType(), argCount, deps.use(processor, (DeclaredType) param.asType())); |
101 | 85 | }
|
102 | 86 | argCount++;
|
103 | 87 | }
|
104 | 88 |
|
105 |
| - Set<String> suppressWarnings = new TreeSet<>(); |
106 |
| - if (intrinsicMethod.getAnnotation(Deprecated.class) != null) { |
107 |
| - suppressWarnings.add("deprecation"); |
108 |
| - } |
109 |
| - if (hasRawtypeWarning(intrinsicMethod.getReturnType())) { |
110 |
| - suppressWarnings.add("rawtypes"); |
111 |
| - } |
112 |
| - for (VariableElement param : params) { |
113 |
| - if (hasUncheckedWarning(param.asType())) { |
114 |
| - suppressWarnings.add("unchecked"); |
115 |
| - } |
116 |
| - } |
117 |
| - if (suppressWarnings.size() > 0) { |
118 |
| - out.printf(" @SuppressWarnings({"); |
119 |
| - String sep = ""; |
120 |
| - for (String suppressWarning : suppressWarnings) { |
121 |
| - out.printf("%s\"%s\"", sep, suppressWarning); |
122 |
| - sep = ", "; |
123 |
| - } |
124 |
| - out.printf("})\n"); |
125 |
| - } |
| 89 | + // Exercise the emission (but swallow generated output) to populate the deps |
| 90 | + emitReplace(processor, new PrintWriter(new StringWriter()), deps); |
126 | 91 |
|
127 |
| - out.printf(" %s result = %s.%s(", getErasedType(intrinsicMethod.getReturnType()), receiver, intrinsicMethod.getSimpleName()); |
128 |
| - if (argCount > firstArg) { |
129 |
| - out.printf("arg%d", firstArg); |
130 |
| - for (int i = firstArg + 1; i < argCount; i++) { |
131 |
| - out.printf(", arg%d", i); |
132 |
| - } |
| 92 | + // Build the list of extra arguments to be passed |
| 93 | + StringBuilder extraArguments = new StringBuilder(); |
| 94 | + for (InjectedDependencies.Dependency dep : deps) { |
| 95 | + extraArguments.append(", ").append(dep.getName(processor, intrinsicMethod)); |
133 | 96 | }
|
134 |
| - out.printf(");\n"); |
135 |
| - |
136 |
| - TypeMirror returnType = intrinsicMethod.getReturnType(); |
137 |
| - switch (returnType.getKind()) { |
138 |
| - case BOOLEAN: |
139 |
| - out.printf(" JavaConstant constant = JavaConstant.forInt(result ? 1 : 0);\n"); |
140 |
| - break; |
141 |
| - case BYTE: |
142 |
| - case SHORT: |
143 |
| - case CHAR: |
144 |
| - case INT: |
145 |
| - out.printf(" JavaConstant constant = JavaConstant.forInt(result);\n"); |
146 |
| - break; |
147 |
| - case LONG: |
148 |
| - out.printf(" JavaConstant constant = JavaConstant.forLong(result);\n"); |
149 |
| - break; |
150 |
| - case FLOAT: |
151 |
| - out.printf(" JavaConstant constant = JavaConstant.forFloat(result);\n"); |
152 |
| - break; |
153 |
| - case DOUBLE: |
154 |
| - out.printf(" JavaConstant constant = JavaConstant.forDouble(result);\n"); |
155 |
| - break; |
156 |
| - case ARRAY: |
157 |
| - case TYPEVAR: |
158 |
| - case DECLARED: |
159 |
| - if (returnType.equals(processor.getType("java.lang.String"))) { |
160 |
| - out.printf(" JavaConstant constant = %s.forString(result);\n", deps.use(processor, WellKnownDependency.CONSTANT_REFLECTION)); |
161 |
| - } else { |
162 |
| - out.printf(" JavaConstant constant = %s.forObject(result);\n", deps.use(processor, WellKnownDependency.SNIPPET_REFLECTION)); |
163 |
| - } |
164 |
| - break; |
165 |
| - default: |
166 |
| - throw new IllegalArgumentException(returnType.toString()); |
167 |
| - } |
168 |
| - |
169 |
| - out.printf(" ConstantNode node = ConstantNode.forConstant(constant, %s, %s);\n", deps.use(processor, WellKnownDependency.META_ACCESS), |
170 |
| - deps.use(processor, WellKnownDependency.STRUCTURED_GRAPH)); |
171 |
| - out.printf(" b.push(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod)); |
172 |
| - out.printf(" return true;\n"); |
| 97 | + out.printf(" return doExecute(b, args%s);\n", extraArguments); |
173 | 98 | }
|
174 | 99 |
|
175 |
| - @Override |
176 |
| - protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) { |
177 |
| - out.printf("\n"); |
178 |
| - out.printf(" @Override\n"); |
179 |
| - out.printf(" public boolean replace(GraphBuilderContext b, Replacements injection, Stamp stamp, NodeInputList<ValueNode> args) {\n"); |
180 |
| - |
| 100 | + private void emitReplace(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) { |
181 | 101 | List<? extends VariableElement> params = intrinsicMethod.getParameters();
|
182 |
| - |
183 |
| - int argCount = 0; |
| 102 | + final int firstArg = intrinsicMethod.getModifiers().contains(Modifier.STATIC) ? 0 : 1; |
184 | 103 | Object receiver;
|
185 |
| - if (intrinsicMethod.getModifiers().contains(Modifier.STATIC)) { |
| 104 | + if (firstArg == 0) { |
186 | 105 | receiver = intrinsicMethod.getEnclosingElement();
|
187 | 106 | } else {
|
188 | 107 | receiver = "arg0";
|
189 | 108 | TypeElement type = (TypeElement) intrinsicMethod.getEnclosingElement();
|
190 |
| - constantArgument(processor, out, deps, argCount, type.asType(), argCount, true); |
191 |
| - argCount++; |
| 109 | + constantArgument(processor, out, deps, 0, type.asType(), 0, false); |
192 | 110 | }
|
193 | 111 |
|
194 |
| - int firstArg = argCount; |
| 112 | + int argCount = firstArg; |
195 | 113 | for (VariableElement param : params) {
|
196 | 114 | if (processor.getAnnotation(param, processor.getType(INJECTED_PARAMETER_CLASS_NAME)) == null) {
|
197 |
| - constantArgument(processor, out, deps, argCount, param.asType(), argCount, true); |
| 115 | + constantArgument(processor, out, deps, argCount, param.asType(), argCount, false); |
198 | 116 | } else {
|
199 |
| - out.printf(" assert args.get(%d).isNullConstant() : \"Must be null constant \" + args.get(%d);\n", argCount, argCount); |
200 |
| - out.printf(" %s arg%d = %s;\n", param.asType(), argCount, deps.find(processor, (DeclaredType) param.asType()).getExpression(processor, intrinsicMethod)); |
| 117 | + out.printf(" assert args[%d].isNullConstant() : \"Must be null constant \" + args[%d];\n", argCount, argCount); |
| 118 | + out.printf(" %s arg%d = %s;\n", param.asType(), argCount, deps.use(processor, (DeclaredType) param.asType())); |
201 | 119 | }
|
202 | 120 | argCount++;
|
203 | 121 | }
|
@@ -270,6 +188,43 @@ protected void createHelpers(AbstractProcessor processor, PrintWriter out, Injec
|
270 | 188 | deps.use(processor, WellKnownDependency.STRUCTURED_GRAPH));
|
271 | 189 | out.printf(" b.push(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod));
|
272 | 190 | out.printf(" return true;\n");
|
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + protected void createPrivateMembersAndConstructor(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, String constructorName) { |
| 195 | + // Add declarations for the extra arguments |
| 196 | + StringBuilder extraArguments = new StringBuilder(); |
| 197 | + for (InjectedDependencies.Dependency dep : deps) { |
| 198 | + extraArguments.append(", ").append(dep.getType()).append(" ").append(dep.getName(processor, intrinsicMethod)); |
| 199 | + } |
| 200 | + out.printf("\n"); |
| 201 | + out.printf(" @SuppressWarnings(\"unused\")\n"); |
| 202 | + out.printf(" static boolean doExecute(GraphBuilderContext b, ValueNode[] args%s) {\n", extraArguments); |
| 203 | + emitReplace(processor, out, deps); |
| 204 | + out.printf(" }\n"); |
| 205 | + |
| 206 | + // This must be done after the code emission above to ensure that deps includes all required |
| 207 | + // dependencies. |
| 208 | + super.createPrivateMembersAndConstructor(processor, out, deps, constructorName); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) { |
| 213 | + out.printf("\n"); |
| 214 | + out.printf(" @Override\n"); |
| 215 | + out.printf(" public boolean replace(GraphBuilderContext b, GeneratedPluginInjectionProvider injection, ValueNode[] args) {\n"); |
| 216 | + |
| 217 | + // Create local declarations for all the injected arguments |
| 218 | + for (InjectedDependencies.Dependency dep : deps) { |
| 219 | + out.printf(" %s %s = %s;\n", dep.getType(), dep.getName(processor, intrinsicMethod), dep.getExpression(processor, intrinsicMethod)); |
| 220 | + } |
| 221 | + |
| 222 | + // Build the list of extra arguments to be passed |
| 223 | + StringBuilder extraArguments = new StringBuilder(); |
| 224 | + for (InjectedDependencies.Dependency dep : deps) { |
| 225 | + extraArguments.append(", ").append(dep.getName(processor, intrinsicMethod)); |
| 226 | + } |
| 227 | + out.printf(" return %s.doExecute(b, args%s);\n", getPluginName(), extraArguments); |
273 | 228 | out.printf(" }\n");
|
274 | 229 | }
|
275 | 230 | }
|
0 commit comments