1 /*
2 * Copyright (c) 2024, 2025, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 import jdk.incubator.code.dialect.core.CoreType;
27 import jdk.incubator.code.dialect.core.FunctionType;
28 import jdk.incubator.code.dialect.core.VarType;
29 import jdk.incubator.code.dialect.java.*;
30
31 import java.lang.classfile.Attributes;
32 import java.lang.classfile.ClassFile;
33 import java.lang.classfile.ClassModel;
34 import java.lang.classfile.CodeElement;
35 import java.lang.classfile.CodeModel;
36 import java.lang.classfile.Instruction;
37 import java.lang.classfile.Label;
38 import java.lang.classfile.MethodModel;
39 import java.lang.classfile.Opcode;
40 import java.lang.classfile.PseudoInstruction;
41 import java.lang.classfile.TypeKind;
42 import java.lang.classfile.attribute.StackMapFrameInfo;
43 import java.lang.classfile.instruction.*;
44 import java.lang.constant.ClassDesc;
45 import java.lang.constant.ConstantDesc;
46 import java.lang.constant.ConstantDescs;
47 import java.lang.constant.DirectMethodHandleDesc;
48 import java.lang.constant.DynamicConstantDesc;
49 import java.lang.constant.MethodTypeDesc;
50 import java.lang.invoke.CallSite;
51 import java.lang.invoke.MethodHandle;
52 import java.lang.reflect.AccessFlag;
53 import jdk.incubator.code.Block;
54 import jdk.incubator.code.TypeElement;
55 import jdk.incubator.code.dialect.core.CoreOp;
56 import jdk.incubator.code.Op;
57 import jdk.incubator.code.Value;
58 import jdk.incubator.code.analysis.NormalizeBlocksTransformer;
59
60 import java.util.ArrayDeque;
61 import java.util.ArrayList;
62 import java.util.Arrays;
63 import java.util.BitSet;
64 import java.util.Collections;
65 import java.util.Deque;
66 import java.util.HashMap;
67 import java.util.IdentityHashMap;
68 import java.util.List;
69 import java.util.Map;
70 import java.util.stream.Collectors;
71 import java.util.stream.IntStream;
72 import java.util.stream.Stream;
73
74 import static java.lang.classfile.attribute.StackMapFrameInfo.SimpleVerificationTypeInfo.*;
75
76 public final class BytecodeLift {
77
78 private static final ClassDesc CD_LambdaMetafactory = ClassDesc.ofDescriptor("Ljava/lang/invoke/LambdaMetafactory;");
79 private static final ClassDesc CD_StringConcatFactory = ClassDesc.ofDescriptor("Ljava/lang/invoke/StringConcatFactory;");
80 private static final JavaType MHS_LOOKUP = JavaType.type(ConstantDescs.CD_MethodHandles_Lookup);
81 private static final JavaType MH = JavaType.type(ConstantDescs.CD_MethodHandle);
82 private static final JavaType MT = JavaType.type(ConstantDescs.CD_MethodType);
83 private static final JavaType CLASS_ARRAY = JavaType.array(JavaType.J_L_CLASS);
84 private static final MethodRef LCMP = MethodRef.method(JavaType.J_L_LONG, "compare", JavaType.INT, JavaType.LONG, JavaType.LONG);
85 private static final MethodRef FCMP = MethodRef.method(JavaType.J_L_FLOAT, "compare", JavaType.INT, JavaType.FLOAT, JavaType.FLOAT);
86 private static final MethodRef DCMP = MethodRef.method(JavaType.J_L_DOUBLE, "compare", JavaType.INT, JavaType.DOUBLE, JavaType.DOUBLE);
87 private static final MethodRef LOOKUP = MethodRef.method(JavaType.type(ConstantDescs.CD_MethodHandles), "lookup", MHS_LOOKUP);
88 private static final MethodRef FIND_STATIC = MethodRef.method(MHS_LOOKUP, "findStatic", MH, JavaType.J_L_CLASS, JavaType.J_L_STRING, MT);
89 private static final MethodRef FIND_VIRTUAL = MethodRef.method(MHS_LOOKUP, "findVirtual", MH, JavaType.J_L_CLASS, JavaType.J_L_STRING, MT);
90 private static final MethodRef FIND_CONSTRUCTOR = MethodRef.method(MHS_LOOKUP, "findConstructor", MH, JavaType.J_L_CLASS, MT);
91 private static final MethodRef FIND_GETTER = MethodRef.method(MHS_LOOKUP, "findGetter", MH, JavaType.J_L_CLASS, JavaType.J_L_STRING, JavaType.J_L_CLASS);
92 private static final MethodRef FIND_STATIC_GETTER = MethodRef.method(MHS_LOOKUP, "findStaticGetter", MH, JavaType.J_L_CLASS, JavaType.J_L_STRING, JavaType.J_L_CLASS);
93 private static final MethodRef FIND_SETTER = MethodRef.method(MHS_LOOKUP, "findSetter", MH, JavaType.J_L_CLASS, JavaType.J_L_STRING, JavaType.J_L_CLASS);
94 private static final MethodRef FIND_STATIC_SETTER = MethodRef.method(MHS_LOOKUP, "findStaticSetter", MH, JavaType.J_L_CLASS, JavaType.J_L_STRING, JavaType.J_L_CLASS);
95 private static final MethodRef METHOD_TYPE_0 = MethodRef.method(MT, "methodType", MT, JavaType.J_L_CLASS);
96 private static final MethodRef METHOD_TYPE_1 = MethodRef.method(MT, "methodType", MT, JavaType.J_L_CLASS, JavaType.J_L_CLASS);
97 private static final MethodRef METHOD_TYPE_L = MethodRef.method(MT, "methodType", MT, JavaType.J_L_CLASS, CLASS_ARRAY);
98
99 private final Block.Builder entryBlock;
100 private final List<Value> initialValues;
101 private final ClassModel classModel;
102 private final List<Label> exceptionHandlers;
103 private final Map<Integer, Block.Builder> exceptionHandlerBlocks;
104 private final BitSet actualEreStack;
105 private final Map<Label, BitSet> exceptionHandlersMap;
106 private final Map<Label, Block.Builder> blockMap;
107 private final List<CodeElement> elements;
108 private final Deque<Value> stack;
109 private final Deque<ClassDesc> newStack;
110 private final List<ExceptionCatch> ecs;
111 private Block.Builder currentBlock;
112
113 private BytecodeLift(Block.Builder entryBlock, ClassModel classModel, CodeModel codeModel, Value... capturedValues) {
114 this.entryBlock = entryBlock;
115 this.initialValues = Stream.concat(Stream.of(capturedValues), entryBlock.parameters().stream()).toList();
116 this.currentBlock = entryBlock;
117 this.classModel = classModel;
118 this.exceptionHandlers = new ArrayList<>();
119 this.exceptionHandlerBlocks = new HashMap<>();
120 this.actualEreStack = new BitSet();
121 this.newStack = new ArrayDeque<>();
122 this.elements = codeModel.elementList();
123 this.stack = new ArrayDeque<>();
124 this.exceptionHandlersMap = new IdentityHashMap<>();
125 this.blockMap = codeModel.findAttribute(Attributes.stackMapTable()).map(sma ->
126 sma.entries().stream().collect(Collectors.toUnmodifiableMap(
127 StackMapFrameInfo::target,
128 smfi -> entryBlock.block(toBlockParams(smfi.stack()))))).orElseGet(Map::of);
129 this.ecs = codeModel.exceptionHandlers();
130 for (var ec : ecs.reversed()) {
131 if (exceptionHandlers.indexOf(ec.handler()) < 0) {
132 exceptionHandlers.add(ec.handler());
133 }
134 }
135 }
136
137 private List<TypeElement> toBlockParams(List<StackMapFrameInfo.VerificationTypeInfo> vtis) {
138 ArrayList<TypeElement> params = new ArrayList<>(vtis.size());
139 for (int i = vtis.size() - 1; i >= 0; i--) {
140 var vti = vtis.get(i);
141 switch (vti) {
142 case INTEGER -> params.add(UnresolvedType.unresolvedInt());
143 case FLOAT -> params.add(JavaType.FLOAT);
144 case DOUBLE -> params.add(JavaType.DOUBLE);
145 case LONG -> params.add(JavaType.LONG);
146 case NULL -> params.add(UnresolvedType.unresolvedRef());
147 case UNINITIALIZED_THIS ->
148 params.add(JavaType.type(classModel.thisClass().asSymbol()));
149 case StackMapFrameInfo.ObjectVerificationTypeInfo ovti ->
150 params.add(JavaType.type(ovti.classSymbol()));
151
152 // Unitialized entry (a new object before its constructor is called)
153 // must be skipped from block parameters because they do not exist in code reflection model
154 case StackMapFrameInfo.UninitializedVerificationTypeInfo _ -> {}
155 default ->
156 throw new IllegalArgumentException("Unexpected VTI: " + vti);
157 }
158 }
159 return params;
160 }
161
162 private Op.Result op(Op op) {
163 return currentBlock.op(op);
164 }
165
166 // Lift to core dialect
167 public static CoreOp.FuncOp lift(byte[] classdata, String methodName) {
168 return lift(classdata, methodName, null);
169 }
170
171 public static CoreOp.FuncOp lift(byte[] classdata, String methodName, MethodTypeDesc methodType) {
172 return lift(ClassFile.of(
173 ClassFile.DebugElementsOption.DROP_DEBUG,
174 ClassFile.LineNumbersOption.DROP_LINE_NUMBERS).parse(classdata).methods().stream()
175 .filter(mm -> mm.methodName().equalsString(methodName) && (methodType == null || mm.methodTypeSymbol().equals(methodType)))
176 .findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown method: " + methodName)));
177 }
178
179 public static CoreOp.FuncOp lift(MethodModel methodModel) {
180 ClassModel classModel = methodModel.parent().orElseThrow();
181 MethodTypeDesc mDesc = methodModel.methodTypeSymbol();
182 if (!methodModel.flags().has(AccessFlag.STATIC)) {
183 mDesc = mDesc.insertParameterTypes(0, classModel.thisClass().asSymbol());
184 }
185 return NormalizeBlocksTransformer.transform(
186 UnresolvedTypesTransformer.transform(
187 SlotToVarTransformer.transform(
188 CoreOp.func(methodModel.methodName().stringValue(),
189 MethodRef.ofNominalDescriptor(mDesc)).body(entryBlock ->
190 new BytecodeLift(entryBlock,
191 classModel,
192 methodModel.code().orElseThrow()).liftBody()))));
193 }
194
195 private void liftBody() {
196 // store entry block
197 int slot = 0;
198 for (var ep : initialValues) {
199 op(SlotOp.store(slot, ep));
200 slot += ep.type().equals(JavaType.LONG) || ep.type().equals(JavaType.DOUBLE) ? 2 : 1;
201 }
202
203 // fill exceptionHandlersMap
204 BitSet eStack = new BitSet();
205 for (var e : elements) {
206 if (e instanceof LabelTarget lt) {
207 BitSet newEreStack = null;
208 for (var er : ecs) {
209 if (lt.label() == er.tryStart() || lt.label() == er.tryEnd()) {
210 if (newEreStack == null) newEreStack = (BitSet)eStack.clone();
211
212 newEreStack.set(exceptionHandlers.indexOf(er.handler()), lt.label() == er.tryStart());
213 }
214 }
215 if (newEreStack != null || blockMap.containsKey(lt.label())) {
216 if (newEreStack != null) eStack = newEreStack;
217 exceptionHandlersMap.put(lt.label(), eStack);
218 }
219 }
220 }
221
222 for (int i = 0; i < elements.size(); i++) {
223 switch (elements.get(i)) {
224 case ExceptionCatch _ -> {
225 // Exception blocks are inserted by label target (below)
226 }
227 case LabelTarget lt -> {
228 BitSet newEreStack = exceptionHandlersMap.get(lt.label());
229 if (newEreStack != null) {
230 Block.Builder target = blockMap.get(lt.label());
231 if (target != null) {
232 if (currentBlock != null) {
233 // Transition to a branch target or a handler
234 ereTransit(actualEreStack, newEreStack, currentBlock, target, stackValues(target), exceptionHandlers.indexOf(lt.label()));
235 }
236 currentBlock = target;
237 stack.clear();
238 stack.addAll(target.parameters());
239 } else if (currentBlock != null && !actualEreStack.equals(newEreStack)) {
240 // Transition to a block with a different ERE stack
241 Block.Builder next = entryBlock.block();
242 ereTransit(actualEreStack, newEreStack, currentBlock, next, List.of(), -1);
243 currentBlock = next;
244 }
245 actualEreStack.clear();
246 actualEreStack.or(newEreStack);
247 }
248 }
249 case BranchInstruction inst when isUnconditionalBranch(inst.opcode()) -> {
250 Block.Builder target = blockMap.get(inst.target());
251 ereTransit(actualEreStack, exceptionHandlersMap.get(inst.target()), currentBlock, target, stackValues(target), exceptionHandlers.indexOf(inst.target()));
252 endOfFlow();
253 }
254 case BranchInstruction inst -> {
255 // Conditional branch
256 Value operand = stack.pop();
257 Op cop = switch (inst.opcode()) {
258 case IFNE -> JavaOp.eq(operand, liftConstant(0));
259 case IFEQ -> JavaOp.neq(operand, liftConstant(0));
260 case IFGE -> JavaOp.lt(operand, liftConstant(0));
261 case IFLE -> JavaOp.gt(operand, liftConstant(0));
262 case IFGT -> JavaOp.le(operand, liftConstant(0));
263 case IFLT -> JavaOp.ge(operand, liftConstant(0));
264 case IFNULL -> JavaOp.neq(operand, liftConstant(null));
265 case IFNONNULL -> JavaOp.eq(operand, liftConstant(null));
266 case IF_ICMPNE -> JavaOp.eq(stack.pop(), operand);
267 case IF_ICMPEQ -> JavaOp.neq(stack.pop(), operand);
268 case IF_ICMPGE -> JavaOp.lt(stack.pop(), operand);
269 case IF_ICMPLE -> JavaOp.gt(stack.pop(), operand);
270 case IF_ICMPGT -> JavaOp.le(stack.pop(), operand);
271 case IF_ICMPLT -> JavaOp.ge(stack.pop(), operand);
272 case IF_ACMPEQ -> JavaOp.neq(stack.pop(), operand);
273 case IF_ACMPNE -> JavaOp.eq(stack.pop(), operand);
274 default -> throw new UnsupportedOperationException("Unsupported branch instruction: " + inst);
275 };
276 Block.Builder branch = targetBlockForBranch(inst.target());
277 Block.Builder next = entryBlock.block();
278 op(CoreOp.conditionalBranch(op(cop),
279 next.successor(),
280 successorWithStack(branch)));
281 currentBlock = next;
282 }
283 case LookupSwitchInstruction si -> {
284 liftSwitch(si.defaultTarget(), si.cases());
285 }
286 case TableSwitchInstruction si -> {
287 liftSwitch(si.defaultTarget(), si.cases());
288 }
289 case ReturnInstruction inst when inst.typeKind() == TypeKind.VOID -> {
290 op(CoreOp.return_());
291 endOfFlow();
292 }
293 case ReturnInstruction _ -> {
294 op(CoreOp.return_(stack.pop()));
295 endOfFlow();
296 }
297 case ThrowInstruction _ -> {
298 op(JavaOp.throw_(stack.pop()));
299 endOfFlow();
300 }
301 case LoadInstruction inst -> {
302 stack.push(op(SlotOp.load(inst.slot(), inst.typeKind())));
303 }
304 case StoreInstruction inst -> {
305 op(SlotOp.store(inst.slot(), stack.pop()));
306 }
307 case IncrementInstruction inst -> {
308 op(SlotOp.store(inst.slot(), op(JavaOp.add(op(SlotOp.load(inst.slot(), TypeKind.INT)), liftConstant(inst.constant())))));
309 }
310 case ConstantInstruction inst -> {
311 stack.push(liftConstant(inst.constantValue()));
312 }
313 case ConvertInstruction inst -> {
314 stack.push(op(JavaOp.conv(switch (inst.toType()) {
315 case BYTE -> JavaType.BYTE;
316 case SHORT -> JavaType.SHORT;
317 case INT -> JavaType.INT;
318 case FLOAT -> JavaType.FLOAT;
319 case LONG -> JavaType.LONG;
320 case DOUBLE -> JavaType.DOUBLE;
321 case CHAR -> JavaType.CHAR;
322 case BOOLEAN -> JavaType.BOOLEAN;
323 default ->
324 throw new IllegalArgumentException("Unsupported conversion target: " + inst.toType());
325 }, stack.pop())));
326 }
327 case OperatorInstruction inst -> {
328 TypeKind tk = inst.typeKind();
329 Value operand = stack.pop();
330 stack.push(op(switch (inst.opcode()) {
331 case IADD, LADD, FADD, DADD ->
332 JavaOp.add(stack.pop(), operand);
333 case ISUB, LSUB, FSUB, DSUB ->
334 JavaOp.sub(stack.pop(), operand);
335 case IMUL, LMUL, FMUL, DMUL ->
336 JavaOp.mul(stack.pop(), operand);
337 case IDIV, LDIV, FDIV, DDIV ->
338 JavaOp.div(stack.pop(), operand);
339 case IREM, LREM, FREM, DREM ->
340 JavaOp.mod(stack.pop(), operand);
341 case INEG, LNEG, FNEG, DNEG ->
342 JavaOp.neg(operand);
343 case ARRAYLENGTH ->
344 JavaOp.arrayLength(operand);
345 case IAND, LAND ->
346 JavaOp.and(stack.pop(), operand);
347 case IOR, LOR ->
348 JavaOp.or(stack.pop(), operand);
349 case IXOR, LXOR ->
350 JavaOp.xor(stack.pop(), operand);
351 case ISHL, LSHL ->
352 JavaOp.lshl(stack.pop(), operand);
353 case ISHR, LSHR ->
354 JavaOp.ashr(stack.pop(), operand);
355 case IUSHR, LUSHR ->
356 JavaOp.lshr(stack.pop(), operand);
357 case LCMP ->
358 JavaOp.invoke(LCMP, stack.pop(), operand);
359 case FCMPL, FCMPG ->
360 JavaOp.invoke(FCMP, stack.pop(), operand);
361 case DCMPL, DCMPG ->
362 JavaOp.invoke(DCMP, stack.pop(), operand);
363 default ->
364 throw new IllegalArgumentException("Unsupported operator opcode: " + inst.opcode());
365 }));
366 }
367 case FieldInstruction inst -> {
368 FieldRef fd = FieldRef.field(
369 JavaType.type(inst.owner().asSymbol()),
370 inst.name().stringValue(),
371 JavaType.type(inst.typeSymbol()));
372 switch (inst.opcode()) {
373 case GETFIELD ->
374 stack.push(op(JavaOp.fieldLoad(fd, stack.pop())));
375 case GETSTATIC ->
376 stack.push(op(JavaOp.fieldLoad(fd)));
377 case PUTFIELD -> {
378 Value value = stack.pop();
379 op(JavaOp.fieldStore(fd, stack.pop(), value));
380 }
381 case PUTSTATIC ->
382 op(JavaOp.fieldStore(fd, stack.pop()));
383 default ->
384 throw new IllegalArgumentException("Unsupported field opcode: " + inst.opcode());
385 }
386 }
387 case ArrayStoreInstruction _ -> {
388 Value value = stack.pop();
389 Value index = stack.pop();
390 op(JavaOp.arrayStoreOp(stack.pop(), index, value));
391 }
392 case ArrayLoadInstruction ali -> {
393 Value index = stack.pop();
394 Value array = stack.pop();
395 if (array.type() instanceof UnresolvedType) {
396 stack.push(op(JavaOp.arrayLoadOp(array, index, switch (ali.typeKind()) {
397 case BYTE -> UnresolvedType.unresolvedInt(); // @@@ Create UnresolvedType.unresolvedByteOrBoolean();
398 case CHAR -> JavaType.CHAR;
399 case DOUBLE -> JavaType.DOUBLE;
400 case FLOAT -> JavaType.FLOAT;
401 case INT -> JavaType.INT;
402 case LONG -> JavaType.LONG;
403 case SHORT -> JavaType.SHORT;
404 case REFERENCE -> UnresolvedType.unresolvedRef();
405 case BOOLEAN, VOID -> throw new IllegalArgumentException("Unexpected array load instruction type");
406 })));
407 } else {
408 stack.push(op(JavaOp.arrayLoadOp(array, index)));
409 }
410 }
411 case InvokeInstruction inst -> {
412 FunctionType mType = MethodRef.ofNominalDescriptor(inst.typeSymbol());
413 List<Value> operands = new ArrayList<>();
414 for (var _ : mType.parameterTypes()) {
415 operands.add(stack.pop());
416 }
417 MethodRef mDesc = MethodRef.method(
418 JavaType.type(inst.owner().asSymbol()),
419 inst.name().stringValue(),
420 mType);
421 Op.Result result = switch (inst.opcode()) {
422 case INVOKEVIRTUAL, INVOKEINTERFACE -> {
423 operands.add(stack.pop());
424 yield op(JavaOp.invoke(JavaOp.InvokeOp.InvokeKind.INSTANCE, false,
425 mDesc.type().returnType(), mDesc, operands.reversed()));
426 }
427 case INVOKESTATIC ->
428 op(JavaOp.invoke(JavaOp.InvokeOp.InvokeKind.STATIC, false,
429 mDesc.type().returnType(), mDesc, operands.reversed()));
430 case INVOKESPECIAL -> {
431 if (inst.owner().asSymbol().equals(newStack.peek()) && inst.name().equalsString(ConstantDescs.INIT_NAME)) {
432 newStack.pop();
433 yield op(JavaOp.new_(
434 MethodRef.constructor(
435 mDesc.refType(),
436 mType.parameterTypes()),
437 operands.reversed()));
438 } else {
439 operands.add(stack.pop());
440 yield op(JavaOp.invoke(JavaOp.InvokeOp.InvokeKind.SUPER, false,
441 mDesc.type().returnType(), mDesc, operands.reversed()));
442 }
443 }
444 default ->
445 throw new IllegalArgumentException("Unsupported invocation opcode: " + inst.opcode());
446 };
447 if (!result.type().equals(JavaType.VOID)) {
448 stack.push(result);
449 }
450 }
451 case InvokeDynamicInstruction inst when inst.bootstrapMethod().kind() == DirectMethodHandleDesc.Kind.STATIC -> {
452 DirectMethodHandleDesc bsm = inst.bootstrapMethod();
453 ClassDesc bsmOwner = bsm.owner();
454 if (bsmOwner.equals(CD_LambdaMetafactory)
455 && inst.bootstrapArgs().get(0) instanceof MethodTypeDesc mtd
456 && inst.bootstrapArgs().get(1) instanceof DirectMethodHandleDesc dmhd) {
457
458 var capturedValues = new Value[dmhd.invocationType().parameterCount() - mtd.parameterCount()];
459 for (int ci = capturedValues.length - 1; ci >= 0; ci--) {
460 capturedValues[ci] = stack.pop();
461 }
462 for (int ci = capturedValues.length; ci < inst.typeSymbol().parameterCount(); ci++) {
463 stack.pop();
464 }
465 MethodTypeDesc mt = dmhd.invocationType();
466 if (capturedValues.length > 0) {
467 mt = mt.dropParameterTypes(0, capturedValues.length);
468 }
469 FunctionType lambdaFunc = CoreType.functionType(JavaType.type(mt.returnType()),
470 mt.parameterList().stream().map(JavaType::type).toList());
471 JavaOp.LambdaOp.Builder lambda = JavaOp.lambda(currentBlock.parentBody(),
472 lambdaFunc,
473 JavaType.type(inst.typeSymbol().returnType()));
474 // if ReflectableLambdaMetafactory is used, the lambda is reflectable
475 if (bsm.owner().displayName().equals("jdk.incubator.code.runtime.ReflectableLambdaMetafactory")) {
476 lambda = lambda.reflectable();
477 }
478
479 if (dmhd.methodName().startsWith("lambda$") && dmhd.owner().equals(classModel.thisClass().asSymbol())) {
480 // inline lambda impl method
481 MethodModel implMethod = classModel.methods().stream().filter(m -> m.methodName().equalsString(dmhd.methodName())).findFirst().orElseThrow();
482 stack.push(op(lambda.body(eb -> new BytecodeLift(eb,
483 classModel,
484 implMethod.code().orElseThrow(),
485 capturedValues).liftBody())));
486 } else {
487 // lambda call to a MH
488 stack.push(op(lambda.body(eb -> {
489 Op.Result ret = eb.op(JavaOp.invoke(
490 MethodRef.method(JavaType.type(dmhd.owner()),
491 dmhd.methodName(),
492 lambdaFunc.returnType(),
493 lambdaFunc.parameterTypes()),
494 Stream.concat(Arrays.stream(capturedValues), eb.parameters().stream()).toArray(Value[]::new)));
495 eb.op(ret.type().equals(JavaType.VOID) ? CoreOp.return_() : CoreOp.return_(ret));
496 })));
497 }
498 } else if (bsmOwner.equals(CD_StringConcatFactory)) {
499 int argsCount = inst.typeSymbol().parameterCount();
500 Deque<Value> args = new ArrayDeque<>(argsCount);
501 for (int ai = 0; ai < argsCount; ai++) {
502 args.push(stack.pop());
503 }
504 Value res = null;
505 if (bsm.methodName().equals("makeConcat")) {
506 for (Value argVal : args) {
507 res = res == null ? argVal : op(JavaOp.concat(res, argVal));
508 }
509 } else {
510 assert bsm.methodName().equals("makeConcatWithConstants");
511 var bsmArgs = inst.bootstrapArgs();
512 String recipe = (String)(bsmArgs.getFirst());
513 int bsmArg = 1;
514 for (int ri = 0; ri < recipe.length(); ri++) {
515 Value argVal = switch (recipe.charAt(ri)) {
516 case '\u0001' -> args.pop();
517 case '\u0002' -> liftConstant(bsmArgs.get(bsmArg++));
518 default -> {
519 char c;
520 int start = ri;
521 while (ri < recipe.length() && (c = recipe.charAt(ri)) != '\u0001' && c != '\u0002') ri++;
522 yield liftConstant(recipe.substring(start, ri--));
523 }
524 };
525 res = res == null ? argVal : op(JavaOp.concat(res, argVal));
526 }
527 }
528 if (res != null) stack.push(res);
529 } else {
530 MethodTypeDesc mtd = inst.typeSymbol();
531
532 //bootstrap
533 MethodTypeDesc bsmDesc = bsm.invocationType();
534 MethodRef bsmRef = MethodRef.method(JavaType.type(bsmOwner),
535 bsm.methodName(),
536 JavaType.type(bsmDesc.returnType()),
537 bsmDesc.parameterList().stream().map(JavaType::type).toArray(TypeElement[]::new));
538
539 Value[] bootstrapArgs = liftBootstrapArgs(bsmDesc, inst.name().toString(), mtd, inst.bootstrapArgs());
540 Value methodHandle = op(JavaOp.invoke(MethodRef.method(CallSite.class, "dynamicInvoker", MethodHandle.class),
541 op(JavaOp.invoke(JavaType.type(ConstantDescs.CD_CallSite), bsmRef, bootstrapArgs))));
542
543 //invocation
544 List<Value> operands = new ArrayList<>();
545 for (int c = 0; c < mtd.parameterCount(); c++) {
546 operands.add(stack.pop());
547 }
548 operands.add(methodHandle);
549 MethodRef mDesc = MethodRef.method(JavaType.type(ConstantDescs.CD_MethodHandle),
550 "invokeExact",
551 MethodRef.ofNominalDescriptor(mtd));
552 Op.Result result = op(JavaOp.invoke(mDesc, operands.reversed()));
553 if (!result.type().equals(JavaType.VOID)) {
554 stack.push(result);
555 }
556 }
557 }
558 case NewObjectInstruction inst -> {
559 // Skip over this and the dup to process the invoke special
560 if (i + 2 < elements.size() - 1
561 && elements.get(i + 1) instanceof StackInstruction dup
562 && dup.opcode() == Opcode.DUP) {
563 i++;
564 newStack.push(inst.className().asSymbol());
565 } else {
566 throw new UnsupportedOperationException("New must be followed by dup");
567 }
568 }
569 case NewPrimitiveArrayInstruction inst -> {
570 stack.push(op(JavaOp.newArray(
571 switch (inst.typeKind()) {
572 case BOOLEAN -> JavaType.BOOLEAN_ARRAY;
573 case BYTE -> JavaType.BYTE_ARRAY;
574 case CHAR -> JavaType.CHAR_ARRAY;
575 case DOUBLE -> JavaType.DOUBLE_ARRAY;
576 case FLOAT -> JavaType.FLOAT_ARRAY;
577 case INT -> JavaType.INT_ARRAY;
578 case LONG -> JavaType.LONG_ARRAY;
579 case SHORT -> JavaType.SHORT_ARRAY;
580 default ->
581 throw new UnsupportedOperationException("Unsupported new primitive array type: " + inst.typeKind());
582 },
583 stack.pop())));
584 }
585 case NewReferenceArrayInstruction inst -> {
586 stack.push(op(JavaOp.newArray(
587 JavaType.type(inst.componentType().asSymbol().arrayType()),
588 stack.pop())));
589 }
590 case NewMultiArrayInstruction inst -> {
591 stack.push(op(JavaOp.new_(
592 MethodRef.constructor(
593 JavaType.type(inst.arrayType().asSymbol()),
594 Collections.nCopies(inst.dimensions(), JavaType.INT)),
595 IntStream.range(0, inst.dimensions()).mapToObj(_ -> stack.pop()).toList().reversed())));
596 }
597 case TypeCheckInstruction inst when inst.opcode() == Opcode.CHECKCAST -> {
598 stack.push(op(JavaOp.cast(JavaType.type(inst.type().asSymbol()), stack.pop())));
599 }
600 case TypeCheckInstruction inst -> {
601 stack.push(op(JavaOp.instanceOf(JavaType.type(inst.type().asSymbol()), stack.pop())));
602 }
603 case StackInstruction inst -> {
604 switch (inst.opcode()) {
605 case POP -> {
606 stack.pop();
607 }
608 case POP2 -> {
609 if (isCategory1(stack.pop())) {
610 stack.pop();
611 }
612 }
613 case DUP -> {
614 stack.push(stack.peek());
615 }
616 case DUP_X1 -> {
617 var value1 = stack.pop();
618 var value2 = stack.pop();
619 stack.push(value1);
620 stack.push(value2);
621 stack.push(value1);
622 }
623 case DUP_X2 -> {
624 var value1 = stack.pop();
625 var value2 = stack.pop();
626 if (isCategory1(value2)) {
627 var value3 = stack.pop();
628 stack.push(value1);
629 stack.push(value3);
630 } else {
631 stack.push(value1);
632 }
633 stack.push(value2);
634 stack.push(value1);
635 }
636 case DUP2 -> {
637 var value1 = stack.peek();
638 if (isCategory1(value1)) {
639 stack.pop();
640 var value2 = stack.peek();
641 stack.push(value1);
642 stack.push(value2);
643 }
644 stack.push(value1);
645 }
646 case DUP2_X1 -> {
647 var value1 = stack.pop();
648 var value2 = stack.pop();
649 if (isCategory1(value1)) {
650 var value3 = stack.pop();
651 stack.push(value2);
652 stack.push(value1);
653 stack.push(value3);
654 } else {
655 stack.push(value1);
656 }
657 stack.push(value2);
658 stack.push(value1);
659 }
660 case DUP2_X2 -> {
661 var value1 = stack.pop();
662 var value2 = stack.pop();
663 if (isCategory1(value1)) {
664 var value3 = stack.pop();
665 if (isCategory1(value3)) {
666 var value4 = stack.pop();
667 stack.push(value2);
668 stack.push(value1);
669 stack.push(value4);
670 } else {
671 stack.push(value2);
672 stack.push(value1);
673 }
674 stack.push(value3);
675 } else {
676 if (isCategory1(value2)) {
677 var value3 = stack.pop();
678 stack.push(value1);
679 stack.push(value3);
680 } else {
681 stack.push(value1);
682 }
683 }
684 stack.push(value2);
685 stack.push(value1);
686 }
687 case SWAP -> {
688 var value1 = stack.pop();
689 var value2 = stack.pop();
690 stack.push(value1);
691 stack.push(value2);
692 }
693 default ->
694 throw new UnsupportedOperationException("Unsupported stack instruction: " + inst);
695 }
696 }
697 case MonitorInstruction inst -> {
698 var monitor = stack.pop();
699 switch (inst.opcode()) {
700 case MONITORENTER -> op(JavaOp.monitorEnter(monitor));
701 case MONITOREXIT -> op(JavaOp.monitorExit(monitor));
702 default ->
703 throw new UnsupportedOperationException("Unsupported stack instruction: " + inst);
704 }
705 }
706 case NopInstruction _ -> {}
707 case PseudoInstruction _ -> {}
708 case Instruction inst ->
709 throw new UnsupportedOperationException("Unsupported instruction: " + inst.opcode().name());
710 default ->
711 throw new UnsupportedOperationException("Unsupported code element: " + elements.get(i));
712 }
713 }
714 assert newStack.isEmpty();
715 }
716
717 private Op.Result liftConstantsIntoArray(TypeElement arrayType, Object... constants) {
718 Op.Result array = op(JavaOp.newArray(arrayType, liftConstant(constants.length)));
719 for (int i = 0; i < constants.length; i++) {
720 op(JavaOp.arrayStoreOp(array, liftConstant(i), liftConstant(constants[i])));
721 }
722 return array;
723 }
724
725 private Op.Result liftDefaultValue(ClassDesc type) {
726 return liftConstant(switch (TypeKind.from(type)) {
727 case BOOLEAN -> false;
728 case BYTE -> (byte)0;
729 case CHAR -> (char)0;
730 case DOUBLE -> 0d;
731 case FLOAT -> 0f;
732 case INT -> 0;
733 case LONG -> 0l;
734 case REFERENCE -> null;
735 case SHORT -> (short)0;
736 default -> throw new IllegalStateException("Invalid type " + type.displayName());
737 });
738 }
739
740 private Op.Result liftConstant(Object c) {
741 return switch (c) {
742 case null -> op(CoreOp.constant(UnresolvedType.unresolvedRef(), null));
743 case ClassDesc cd -> op(CoreOp.constant(JavaType.J_L_CLASS, JavaType.type(cd)));
744 case Double d -> op(CoreOp.constant(JavaType.DOUBLE, d));
745 case Float f -> op(CoreOp.constant(JavaType.FLOAT, f));
746 case Integer ii -> op(CoreOp.constant(UnresolvedType.unresolvedInt(), ii));
747 case Long l -> op(CoreOp.constant(JavaType.LONG, l));
748 case String s -> op(CoreOp.constant(JavaType.J_L_STRING, s));
749 case DirectMethodHandleDesc dmh -> {
750 Op.Result lookup = op(JavaOp.invoke(LOOKUP));
751 Op.Result owner = liftConstant(dmh.owner());
752 Op.Result name = liftConstant(dmh.methodName());
753 MethodTypeDesc invDesc = dmh.invocationType();
754 yield op(switch (dmh.kind()) {
755 case STATIC, INTERFACE_STATIC ->
756 JavaOp.invoke(FIND_STATIC, lookup, owner, name, liftConstant(invDesc));
757 case VIRTUAL, INTERFACE_VIRTUAL ->
758 JavaOp.invoke(FIND_VIRTUAL, lookup, owner, name, liftConstant(invDesc.dropParameterTypes(0, 1)));
759 case SPECIAL, INTERFACE_SPECIAL ->
760 //CoreOp.invoke(MethodRef.method(e), "findSpecial", owner, name, liftConstant(invDesc.dropParameterTypes(0, 1)), lookup.lookupClass());
761 throw new UnsupportedOperationException(dmh.toString());
762 case CONSTRUCTOR ->
763 JavaOp.invoke(FIND_CONSTRUCTOR, lookup, owner, liftConstant(invDesc.changeReturnType(ConstantDescs.CD_Void)));
764 case GETTER ->
765 JavaOp.invoke(FIND_GETTER, lookup, owner, name, liftConstant(invDesc.returnType()));
766 case STATIC_GETTER ->
767 JavaOp.invoke(FIND_STATIC_GETTER, lookup, owner, name, liftConstant(invDesc.returnType()));
768 case SETTER ->
769 JavaOp.invoke(FIND_SETTER, lookup, owner, name, liftConstant(invDesc.parameterType(1)));
770 case STATIC_SETTER ->
771 JavaOp.invoke(FIND_STATIC_SETTER, lookup, owner, name, liftConstant(invDesc.parameterType(0)));
772 });
773 }
774 case MethodTypeDesc mt -> op(switch (mt.parameterCount()) {
775 case 0 -> JavaOp.invoke(METHOD_TYPE_0, liftConstant(mt.returnType()));
776 case 1 -> JavaOp.invoke(METHOD_TYPE_1, liftConstant(mt.returnType()), liftConstant(mt.parameterType(0)));
777 default -> JavaOp.invoke(METHOD_TYPE_L, liftConstant(mt.returnType()), liftConstantsIntoArray(CLASS_ARRAY, (Object[])mt.parameterArray()));
778 });
779 case DynamicConstantDesc<?> v when v.bootstrapMethod().owner().equals(ConstantDescs.CD_ConstantBootstraps)
780 && v.bootstrapMethod().methodName().equals("nullConstant")
781 -> {
782 c = null;
783 yield liftConstant(null);
784 }
785 case DynamicConstantDesc<?> dcd -> {
786 DirectMethodHandleDesc bsm = dcd.bootstrapMethod();
787 MethodTypeDesc bsmDesc = bsm.invocationType();
788 Value[] bootstrapArgs = liftBootstrapArgs(bsmDesc, dcd.constantName(), dcd.constantType(), dcd.bootstrapArgsList());
789 MethodRef bsmRef = MethodRef.method(JavaType.type(bsm.owner()),
790 bsm.methodName(),
791 JavaType.type(bsmDesc.returnType()),
792 bsmDesc.parameterList().stream().map(JavaType::type).toArray(TypeElement[]::new));
793 yield op(JavaOp.invoke(bsmRef, bootstrapArgs));
794 }
795 case Boolean b -> op(CoreOp.constant(JavaType.BOOLEAN, b));
796 case Byte b -> op(CoreOp.constant(JavaType.BYTE, b));
797 case Short s -> op(CoreOp.constant(JavaType.SHORT, s));
798 case Character ch -> op(CoreOp.constant(JavaType.CHAR, ch));
799 default -> throw new UnsupportedOperationException(c.getClass().toString());
800 };
801 }
802
803 private Value[] liftBootstrapArgs(MethodTypeDesc bsmDesc, String name, ConstantDesc desc, List<ConstantDesc> bsmArgs) {
804 Value[] bootstrapArgs = new Value[bsmDesc.parameterCount()];
805 bootstrapArgs[0] = op(JavaOp.invoke(LOOKUP));
806 bootstrapArgs[1] = liftConstant(name);
807 bootstrapArgs[2] = liftConstant(desc);
808 ClassDesc lastArgType = bsmDesc.parameterType(bsmDesc.parameterCount() - 1);
809 if (lastArgType.isArray()) {
810 for (int ai = 0; ai < bootstrapArgs.length - 4; ai++) {
811 bootstrapArgs[ai + 3] = liftConstant(bsmArgs.get(ai));
812 }
813 // Vararg tail of the bootstrap method parameters
814 bootstrapArgs[bootstrapArgs.length - 1] =
815 liftConstantsIntoArray(JavaType.type(lastArgType),
816 bsmArgs.subList(bootstrapArgs.length - 4, bsmArgs.size()).toArray());
817 } else {
818 for (int ai = 0; ai < bootstrapArgs.length - 3; ai++) {
819 bootstrapArgs[ai + 3] = liftConstant(bsmArgs.get(ai));
820 }
821 }
822 return bootstrapArgs;
823 }
824
825 private void liftSwitch(Label defaultTarget, List<SwitchCase> cases) {
826 Value v = stack.pop();
827 if (!valueType(v).equals(PrimitiveType.INT)) {
828 v = op(JavaOp.conv(PrimitiveType.INT, v));
829 }
830 SwitchCase last = cases.getLast();
831 Block.Builder def = targetBlockForBranch(defaultTarget);
832 for (SwitchCase sc : cases) {
833 if (sc == last) {
834 op(CoreOp.conditionalBranch(
835 op(JavaOp.eq(v, liftConstant(sc.caseValue()))),
836 successorWithStack(targetBlockForBranch(sc.target())),
837 successorWithStack(def)));
838 } else {
839 Block.Builder next = entryBlock.block();
840 op(CoreOp.conditionalBranch(
841 op(JavaOp.eq(v, liftConstant(sc.caseValue()))),
842 successorWithStack(targetBlockForBranch(sc.target())),
843 next.successor()));
844 currentBlock = next;
845 }
846 }
847 endOfFlow();
848 }
849
850 private Block.Builder newBlock(List<Block.Parameter> otherBlockParams) {
851 return entryBlock.block(otherBlockParams.stream().map(Block.Parameter::type).toList());
852 }
853
854 private void endOfFlow() {
855 currentBlock = null;
856 // Flow discontinued, stack cleared to be ready for the next label target
857 stack.clear();
858 }
859
860 private Block.Builder targetBlockForExceptionHandler(BitSet initialEreStack, int exceptionHandlerIndex) {
861 Block.Builder target = exceptionHandlerBlocks.get(exceptionHandlerIndex);
862 if (target == null) { // Avoid ConcurrentModificationException
863 Label ehLabel = exceptionHandlers.get(exceptionHandlerIndex);
864 target = transitionBlockForTarget(initialEreStack, exceptionHandlersMap.get(ehLabel), blockMap.get(ehLabel), exceptionHandlerIndex);
865 exceptionHandlerBlocks.put(exceptionHandlerIndex, target);
866 }
867 return target;
868 }
869
870 private Block.Builder targetBlockForBranch(Label targetLabel) {
871 return transitionBlockForTarget(actualEreStack, exceptionHandlersMap.get(targetLabel), blockMap.get(targetLabel), -1);
872 }
873
874 private Block.Builder transitionBlockForTarget(BitSet initialEreStack, BitSet targetEreStack, Block.Builder targetBlock, int targetExceptionHandlerIndex) {
875 if (targetBlock == null) return null;
876 Block.Builder transitionBlock = newBlock(targetBlock.parameters());
877 ereTransit(initialEreStack, targetEreStack, transitionBlock, targetBlock, transitionBlock.parameters(), targetExceptionHandlerIndex);
878 return transitionBlock;
879 }
880
881 record EreT(boolean enter, int ehi) {}
882
883 private void ereTransit(BitSet initialEreStack, BitSet targetEreStack, Block.Builder initialBlock, Block.Builder targetBlock, List<? extends Value> values, int targetExceptionHandlerIndex) {
884 List<EreT> transits = new ArrayList<>();
885 BitSet ereStack = (BitSet)initialEreStack.clone();
886 ereStack.andNot(targetEreStack);
887 // Split region exits by handler stack
888 for (int ehi = ereStack.previousSetBit(Integer.MAX_VALUE); ehi >= 0; ehi = ereStack.previousSetBit(ehi - 1)) {
889 transits.add(new EreT(false, ehi));
890 }
891 ereStack = (BitSet)targetEreStack.clone();
892 ereStack.andNot(initialEreStack);
893 // Split region enters by handler stack
894 for (int ehi = ereStack.nextSetBit(0); ehi >= 0; ehi = ereStack.nextSetBit(ehi + 1)) {
895 transits.add(new EreT(true, ehi));
896 }
897
898 if (transits.isEmpty()) {
899 // Join with branch
900 initialBlock.op(CoreOp.branch(targetBlock.successor(values)));
901 } else {
902 // Insert ERE transitions
903 Block.Builder currentBlock = initialBlock;
904 ereStack = (BitSet)initialEreStack.clone();
905 for (int i = 0; i < transits.size() - 1; i++) {
906 EreT t = transits.get(i);
907 Block.Builder next = entryBlock.block();
908 ereTransit(initialBlock, currentBlock, t.enter(), next, List.of(), t.ehi(), targetExceptionHandlerIndex, ereStack);
909 currentBlock = next;
910 ereStack.set(t.ehi(), t.enter());
911 }
912 EreT t = transits.getLast();
913 ereTransit(initialBlock, currentBlock, t.enter(), targetBlock, values, t.ehi(), targetExceptionHandlerIndex, ereStack);
914 }
915 }
916
917 private void ereTransit(Block.Builder initialBlock, Block.Builder currentBlock, boolean enter, Block.Builder targetBlock, List<? extends Value> values, int ehi, int targetExceptionHandlerIndex, BitSet handlerEreStack) {
918 Block.Reference ref = targetBlock.successor(values);
919 Block.Reference catcher = (ehi == targetExceptionHandlerIndex
920 ? initialBlock
921 : targetBlockForExceptionHandler(handlerEreStack, ehi)).successor();
922 currentBlock.op(enter ? JavaOp.exceptionRegionEnter(ref, catcher) : JavaOp.exceptionRegionExit(ref, catcher));
923 }
924
925 Block.Reference successorWithStack(Block.Builder next) {
926 return next.successor(stackValues(next));
927 }
928
929 private List<Value> stackValues(Block.Builder limit) {
930 return stack.stream().limit(limit.parameters().size()).toList();
931 }
932
933 private static TypeElement valueType(Value v) {
934 var t = v.type();
935 while (t instanceof VarType vt) t = vt.valueType();
936 return t;
937 }
938
939 private static boolean isCategory1(Value v) {
940 TypeElement t = v.type();
941 return !t.equals(JavaType.LONG) && !t.equals(JavaType.DOUBLE);
942 }
943
944 private static boolean isUnconditionalBranch(Opcode opcode) {
945 return switch (opcode) {
946 case GOTO, ATHROW, GOTO_W, LOOKUPSWITCH, TABLESWITCH -> true;
947 default -> opcode.kind() == Opcode.Kind.RETURN;
948 };
949 }
950 }