1 /*
 2  * Copyright (c) 2025, 2026, 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 package jdk.incubator.code.bytecode.impl;
26 
27 import java.util.List;
28 import java.util.Map;
29 
30 import jdk.incubator.code.*;
31 import jdk.incubator.code.dialect.java.JavaType;
32 import jdk.incubator.code.extern.ExternalizedOp;
33 
34 /**
35  * A block terminating conditional multi-branch operation modeling {@code tableswitch} and {@code lookupswitch}
36  * instructions.
37  * <p>
38  * This operation accepts an int computational type operand (JVMS 2.11.1-B),
39  * variable number of distinct constant labels and the same number of successors.
40  * When the operand is matching one of the labels, the relevant successor is selected.
41  * If none of the labels is matching, the default successor is selected.
42  * Default is a successor with corresponds null label value.
43  * The selected successor refers to the next block to branch to.
44  */
45 public final class ConstantLabelSwitchOp extends AbstractOp.Terminating
46         implements ExternalizedOp.Externalizable {
47 
48     final List<Integer> labels;
49 
50     public ConstantLabelSwitchOp(Value intSelector, List<Integer> labels, List<Block.Reference> targets) {
51         assert targets.size() == labels.size();
52         super(List.of(intSelector), targets);
53         this.labels = labels;
54     }
55 
56     ConstantLabelSwitchOp(ConstantLabelSwitchOp that, CodeContext cc) {
57         super(that, cc);
58         this.labels = that.labels;
59     }
60 
61     @Override
62     public ConstantLabelSwitchOp transform(CodeContext cc, CodeTransformer ct) {
63         return new ConstantLabelSwitchOp(this, cc);
64     }
65 
66     @Override
67     public CodeType resultType() {
68         return JavaType.VOID;
69     }
70 
71     public List<Integer> labels() {
72         return labels;
73     }
74 
75     @Override
76     public Map<String, Object> externalize() {
77         return Map.of("", labels);
78     }
79 }