1 /*
 2  * Copyright (c) 2024, 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  */
23 
24 import org.testng.Assert;
25 import org.testng.annotations.Test;
26 
27 import java.lang.classfile.ClassFile;
28 import java.lang.classfile.Label;
29 import java.lang.constant.ClassDesc;
30 import java.lang.constant.ConstantDescs;
31 import java.lang.constant.MethodTypeDesc;
32 import java.lang.reflect.code.op.CoreOp;
33 import java.lang.reflect.code.bytecode.BytecodeLift;
34 import java.lang.reflect.code.interpreter.Interpreter;
35 
36 /*
37  * @test
38  * @enablePreview
39  * @run testng TestLiftCustomBytecode
40  */
41 
42 public class TestLiftCustomBytecode {
43 
44     @Test
45     public void testBackJumps() throws Throwable {
46         CoreOp.FuncOp f = getFuncOp(ClassFile.of().build(ClassDesc.of("BackJumps"), clb ->
47                 clb.withMethodBody("backJumps", MethodTypeDesc.of(ConstantDescs.CD_int, ConstantDescs.CD_int), ClassFile.ACC_STATIC, cob -> {
48                     Label l1 = cob.newLabel();
49                     Label l2 = cob.newLabel();
50                     Label l3 = cob.newLabel();
51                     Label l4 = cob.newLabel();
52                     // Code wrapped in back jumps requires multiple passes and block skipping
53                     cob.goto_(l1)
54                        .labelBinding(l2)
55                        .goto_(l3)
56                        .labelBinding(l4)
57                        .iload(0)
58                        .ireturn()
59                        .labelBinding(l1)
60                        .goto_(l2)
61                        .labelBinding(l3)
62                        .goto_(l4);
63                 })), "backJumps");
64 
65         Assert.assertEquals((int) Interpreter.invoke(f, 42), 42);
66     }
67 
68     static CoreOp.FuncOp getFuncOp(byte[] classdata, String method) {
69         CoreOp.FuncOp flift = BytecodeLift.lift(classdata, method);
70         flift.writeTo(System.out);
71         return flift;
72     }
73 }