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