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 
33 /**
34  * The terminating conditional multi-branch operation modeling {@code tableswitch} and {@code lookupswitch} instructions.
35  * <p>
36  * This operation accepts an int computational type operand (JVMS 2.11.1-B),
37  * variable number of distinct constant labels and the same number of successors.
38  * When the operand is matching one of the labels, the relevant successor is selected.
39  * If none of the labels is matching, the default successor is selected.
40  * Default is a successor with corresponds null label value.
41  * The selected successor refers to the next block to branch to.
42  */
43 public final class ConstantLabelSwitchOp extends AbstractOp implements Op.BlockTerminating {
44 
45     final List<Integer> labels;
46     final List<Block.Reference> targets;
47 
48     public ConstantLabelSwitchOp(Value intSelector, List<Integer> labels, List<Block.Reference> targets) {
49         super(List.of(intSelector));
50         assert targets.size() == labels.size();
51         this.labels = labels;
52         this.targets = targets;
53     }
54 
55     ConstantLabelSwitchOp(ConstantLabelSwitchOp that, CodeContext cc) {
56         super(that, cc);
57         this.labels = that.labels;
58         this.targets = that.targets.stream().map(cc::getReferenceOrCreate).toList();
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 List<Block.Reference> successors() {
77         return targets;
78     }
79 
80     @Override
81     public Map<String, Object> externalize() {
82         return Map.of("", labels);
83     }
84 
85 }