Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/node/NodeCodeGenerator.java @ 8252:0905d796944a
Refactored codegen error model to make error redirection a lot easier.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Wed, 13 Mar 2013 19:58:28 +0100 |
parents | cb70ed101b5f |
children | 4dc7034317ec |
comparison
equal
deleted
inserted
replaced
8251:cb70ed101b5f | 8252:0905d796944a |
---|---|
180 } | 180 } |
181 body.string("."); | 181 body.string("."); |
182 body.startCall(method.getSimpleName().toString()); | 182 body.startCall(method.getSimpleName().toString()); |
183 } | 183 } |
184 | 184 |
185 private static String generatedGenericMethodName(SpecializationData specialization) { | 185 private String generatedGenericMethodName(SpecializationData specialization) { |
186 final String prefix = "generic"; | 186 final String prefix = "generic"; |
187 | 187 |
188 if (specialization == null) { | 188 if (specialization == null) { |
189 return prefix; | |
190 } | |
191 | |
192 if (!specialization.getNode().getGenericSpecialization().isUseSpecializationsForGeneric() || !specialization.getNode().needsRewrites(context)) { | |
189 return prefix; | 193 return prefix; |
190 } | 194 } |
191 | 195 |
192 SpecializationData prev = null; | 196 SpecializationData prev = null; |
193 for (SpecializationData current : specialization.getNode().getSpecializations()) { | 197 for (SpecializationData current : specialization.getNode().getSpecializations()) { |
248 builder.startBlock(); | 252 builder.startBlock(); |
249 ifCount++; | 253 ifCount++; |
250 } | 254 } |
251 | 255 |
252 if (implicitGuards == null && explicitGuards == null && conditionPrefix != null && !conditionPrefix.isEmpty()) { | 256 if (implicitGuards == null && explicitGuards == null && conditionPrefix != null && !conditionPrefix.isEmpty()) { |
253 builder.startIf().string(conditionPrefix).end().startBlock(); | 257 builder.startIf(); |
258 builder.string(conditionPrefix); | |
259 builder.end().startBlock(); | |
254 ifCount++; | 260 ifCount++; |
255 } | 261 } |
256 | 262 |
257 builder.tree(guardedStatements); | 263 builder.tree(guardedStatements); |
258 | 264 |
259 builder.end(ifCount); | 265 builder.end(ifCount); |
260 if (ifCount > 0 && elseStatements != null) { | 266 if (elseStatements != null && ifCount > 0) { |
261 builder.startElseBlock(); | |
262 builder.tree(elseStatements); | 267 builder.tree(elseStatements); |
263 builder.end(); | 268 } |
264 } | |
265 | |
266 return builder.getRoot(); | 269 return builder.getRoot(); |
267 } | 270 } |
268 | 271 |
269 private CodeTree createExplicitGuards(CodeTreeBuilder parent, String conditionPrefix, SpecializationData valueSpecialization, SpecializationData guardedSpecialization, boolean onSpecialization) { | 272 private CodeTree createExplicitGuards(CodeTreeBuilder parent, String conditionPrefix, SpecializationData valueSpecialization, SpecializationData guardedSpecialization, boolean onSpecialization) { |
270 CodeTreeBuilder builder = new CodeTreeBuilder(parent); | 273 CodeTreeBuilder builder = new CodeTreeBuilder(parent); |
271 String andOperator = conditionPrefix != null ? conditionPrefix + " && " : ""; | 274 String andOperator = conditionPrefix != null ? conditionPrefix + " && " : ""; |
272 if (guardedSpecialization.getGuards().length > 0) { | 275 if (guardedSpecialization.getGuards().size() > 0) { |
273 // Explicitly specified guards | 276 // Explicitly specified guards |
274 for (SpecializationGuardData guard : guardedSpecialization.getGuards()) { | 277 for (SpecializationGuardData guard : guardedSpecialization.getGuards()) { |
275 if ((guard.isOnSpecialization() && onSpecialization) || (guard.isOnExecution() && !onSpecialization)) { | 278 if ((guard.isOnSpecialization() && onSpecialization) || (guard.isOnExecution() && !onSpecialization)) { |
276 builder.string(andOperator); | 279 builder.string(andOperator); |
277 | 280 |
512 | 515 |
513 if (node.getSpecializations().size() > 1) { | 516 if (node.getSpecializations().size() > 1) { |
514 clazz.add(createCreateSpecializedMethod(node, createVisibility)); | 517 clazz.add(createCreateSpecializedMethod(node, createVisibility)); |
515 } | 518 } |
516 | 519 |
517 if (node.needsRewrites(getContext())) { | 520 if (node.needsRewrites(context)) { |
518 clazz.add(createSpecializeMethod(node)); | 521 clazz.add(createSpecializeMethod(node)); |
519 | 522 } |
523 | |
524 if (node.getGenericSpecialization() != null) { | |
520 List<CodeExecutableElement> genericMethods = createGeneratedGenericMethod(node); | 525 List<CodeExecutableElement> genericMethods = createGeneratedGenericMethod(node); |
521 for (CodeExecutableElement method : genericMethods) { | 526 for (CodeExecutableElement method : genericMethods) { |
522 clazz.add(method); | 527 clazz.add(method); |
523 } | 528 } |
524 } | 529 } |
598 TypeMirror nodeType = getContext().getTruffleTypes().getNode(); | 603 TypeMirror nodeType = getContext().getTruffleTypes().getNode(); |
599 TypeMirror wildcardNodeType = types.getWildcardType(nodeType, null); | 604 TypeMirror wildcardNodeType = types.getWildcardType(nodeType, null); |
600 classType = types.getDeclaredType(Utils.fromTypeMirror(classType), wildcardNodeType); | 605 classType = types.getDeclaredType(Utils.fromTypeMirror(classType), wildcardNodeType); |
601 TypeMirror returnType = types.getDeclaredType(listType, classType); | 606 TypeMirror returnType = types.getDeclaredType(listType, classType); |
602 | 607 |
603 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnType, "getChildrenSignature"); | 608 CodeExecutableElement method = new CodeExecutableElement(modifiers(PUBLIC), returnType, "getExecutionSignature"); |
604 CodeTreeBuilder builder = method.createBuilder(); | 609 CodeTreeBuilder builder = method.createBuilder(); |
605 | 610 |
606 List<TypeMirror> signatureTypes = new ArrayList<>(); | 611 List<TypeMirror> signatureTypes = new ArrayList<>(); |
607 assert !node.getSpecializations().isEmpty(); | 612 assert !node.getSpecializations().isEmpty(); |
608 SpecializationData data = node.getSpecializations().get(0); | 613 SpecializationData data = node.getSpecializations().get(0); |
932 return method; | 937 return method; |
933 } | 938 } |
934 | 939 |
935 private List<CodeExecutableElement> createGeneratedGenericMethod(NodeData node) { | 940 private List<CodeExecutableElement> createGeneratedGenericMethod(NodeData node) { |
936 TypeMirror genericReturnType = node.getGenericSpecialization().getReturnType().getActualType(); | 941 TypeMirror genericReturnType = node.getGenericSpecialization().getReturnType().getActualType(); |
937 if (node.getGenericSpecialization().isUseSpecializationsForGeneric()) { | 942 if (node.getGenericSpecialization().isUseSpecializationsForGeneric() && node.needsRewrites(context)) { |
938 List<CodeExecutableElement> methods = new ArrayList<>(); | 943 List<CodeExecutableElement> methods = new ArrayList<>(); |
939 | 944 |
940 List<SpecializationData> specializations = node.getSpecializations(); | 945 List<SpecializationData> specializations = node.getSpecializations(); |
941 SpecializationData prev = null; | 946 SpecializationData prev = null; |
942 for (int i = 0; i < specializations.size(); i++) { | 947 for (int i = 0; i < specializations.size(); i++) { |
972 CodeTreeBuilder invokeMethodBuilder = new CodeTreeBuilder(builder); | 977 CodeTreeBuilder invokeMethodBuilder = new CodeTreeBuilder(builder); |
973 emitInvokeDoMethod(invokeMethodBuilder, current, 0); | 978 emitInvokeDoMethod(invokeMethodBuilder, current, 0); |
974 CodeTree invokeMethod = invokeMethodBuilder.getRoot(); | 979 CodeTree invokeMethod = invokeMethodBuilder.getRoot(); |
975 | 980 |
976 if (next != null) { | 981 if (next != null) { |
977 invokeMethod = createGuardAndCast(builder, null, current.getNode().getGenericSpecialization(), current, false, invokeMethod, null); | 982 CodeTreeBuilder nextBuilder = builder.create(); |
983 | |
984 nextBuilder.startReturn().startCall(generatedGenericMethodName(next)); | |
985 nextBuilder.string(THIS_NODE_LOCAL_VAR_NAME); | |
986 addValueParameterNames(nextBuilder, next, null, true, true); | |
987 nextBuilder.end().end(); | |
988 | |
989 invokeMethod = createGuardAndCast(builder, null, current.getNode().getGenericSpecialization(), current, false, invokeMethod, nextBuilder.getRoot()); | |
978 } | 990 } |
979 | 991 |
980 builder.tree(invokeMethod); | 992 builder.tree(invokeMethod); |
981 | 993 |
982 if (next != null) { | 994 if (next != null) { |
983 builder.end(); | 995 builder.end(); |
984 | |
985 builder.startReturn().startCall(generatedGenericMethodName(next)); | |
986 builder.string(THIS_NODE_LOCAL_VAR_NAME); | |
987 addValueParameterNames(builder, next, null, true, true); | |
988 builder.end().end(); | |
989 } | 996 } |
990 } | 997 } |
991 | 998 |
992 private void emitInvokeDoMethod(CodeTreeBuilder builder, SpecializationData specialization, int level) { | 999 private void emitInvokeDoMethod(CodeTreeBuilder builder, SpecializationData specialization, int level) { |
993 if (!specialization.getExceptions().isEmpty()) { | 1000 if (!specialization.getExceptions().isEmpty()) { |
1207 CodeTreeBuilder builder = new CodeTreeBuilder(parent); | 1214 CodeTreeBuilder builder = new CodeTreeBuilder(parent); |
1208 if (!specialization.getExceptions().isEmpty()) { | 1215 if (!specialization.getExceptions().isEmpty()) { |
1209 builder.startTryBlock(); | 1216 builder.startTryBlock(); |
1210 } | 1217 } |
1211 | 1218 |
1212 if (specialization.getMethod() == null) { | 1219 if (specialization.getMethod() == null && !node.needsRewrites(context)) { |
1213 emitEncounteredSynthetic(builder); | 1220 emitEncounteredSynthetic(builder); |
1214 } else if (specialization.isUninitialized() || specialization.isGeneric()) { | 1221 } else if (specialization.isUninitialized() || specialization.isGeneric()) { |
1215 builder.startReturn().startCall(factoryClassName(node), generatedGenericMethodName(null)); | 1222 builder.startReturn().startCall(factoryClassName(node), generatedGenericMethodName(null)); |
1216 builder.string("this"); | 1223 builder.string("this"); |
1217 addValueParameterNames(builder, specialization, null, true, true); | 1224 addValueParameterNames(builder, specialization, null, true, true); |
1347 shortCircuitIndex++; | 1354 shortCircuitIndex++; |
1348 } | 1355 } |
1349 } | 1356 } |
1350 | 1357 |
1351 builder.startStatement().type(shortCircuitParam.getActualType()).string(" ").string(valueName(shortCircuitParam)).string(" = "); | 1358 builder.startStatement().type(shortCircuitParam.getActualType()).string(" ").string(valueName(shortCircuitParam)).string(" = "); |
1352 ShortCircuitData shortCircuitData = specialization.getShortCircuits()[shortCircuitIndex]; | 1359 ShortCircuitData shortCircuitData = specialization.getShortCircuits().get(shortCircuitIndex); |
1353 | 1360 |
1354 startCallOperationMethod(builder, shortCircuitData, false); | 1361 startCallOperationMethod(builder, shortCircuitData, false); |
1355 addValueParameterNames(builder, shortCircuitData, exceptionParam != null ? exceptionParam.getName() : null, false, false); | 1362 addValueParameterNames(builder, shortCircuitData, exceptionParam != null ? exceptionParam.getName() : null, false, false); |
1356 builder.end().end(); // call operation | 1363 builder.end().end(); // call operation |
1357 | 1364 |