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