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 import org.testng.annotations.Test;
24 
25 import java.lang.reflect.code.Block;
26 import java.lang.reflect.code.Value;
27 import java.lang.reflect.code.bytecode.SlotOp;
28 import java.lang.reflect.code.bytecode.SlotSSA;
29 import java.lang.reflect.code.op.CoreOp;
30 import java.lang.reflect.code.type.FunctionType;
31 import java.lang.reflect.code.type.JavaType;
32 import java.lang.reflect.code.type.MethodRef;
33 
34 /*
35  * @test
36  * @run testng TestSlots
37  */
38 public class TestSlotOps {
39 
40     static void m(int i) {
41 
42     }
43 
44     @Test
45     public void test() {
46         CoreOp.FuncOp f = build();
47         System.out.println(f.toText());
48 
49         CoreOp.FuncOp fssa = SlotSSA.transform(f);
50         System.out.println(fssa.toText());
51     }
52 
53     static CoreOp.FuncOp build() {
54         return CoreOp.func("f", FunctionType.functionType(JavaType.J_L_STRING)).body(b -> {
55             Block.Builder trueBlock = b.block();
56             Block.Builder falseBlock = b.block();
57             Block.Builder exitBlock = b.block();
58 
59             // Entry block
60             {
61                 Value nullConstant = b.op(CoreOp.constant(JavaType.J_L_OBJECT, null));
62                 b.op(SlotOp.store(0, nullConstant));
63 
64                 b.op(CoreOp.conditionalBranch(b.op(CoreOp.constant(JavaType.BOOLEAN, true)),
65                         trueBlock.successor(), falseBlock.successor()));
66             }
67 
68             // True block
69             {
70                 Value oneConstant = trueBlock.op(CoreOp.constant(JavaType.INT, 1));
71                 trueBlock.op(SlotOp.store(0, oneConstant));
72 
73                 Value loadValue = trueBlock.op(SlotOp.load(0, JavaType.INT));
74                 trueBlock.op(CoreOp.invoke(MethodRef.method(TestSlots.class, "m", void.class, int.class), loadValue));
75 
76                 Value stringConstant = trueBlock.op(CoreOp.constant(JavaType.J_L_STRING, "TRUE"));
77                 trueBlock.op(SlotOp.store(0, stringConstant));
78 
79                 trueBlock.op(CoreOp.branch(exitBlock.successor()));
80             }
81 
82             // False block
83             {
84                 Value stringConstant = falseBlock.op(CoreOp.constant(JavaType.J_L_STRING, "FALSE"));
85                 falseBlock.op(SlotOp.store(0, stringConstant));
86 
87                 falseBlock.op(CoreOp.branch(exitBlock.successor()));
88             }
89 
90             // Exit block
91             {
92                 Value loadValue = exitBlock.op(SlotOp.load(0, JavaType.J_L_STRING));
93                 exitBlock.op(CoreOp._return(loadValue));
94             }
95         });
96     }
97 }