1 /*
2 * Copyright (c) 2025, 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 import jdk.incubator.code.Block;
30 import jdk.incubator.code.CodeContext;
31 import jdk.incubator.code.CodeTransformer;
32 import jdk.incubator.code.Op;
33 import jdk.incubator.code.TypeElement;
34 import jdk.incubator.code.Value;
35 import jdk.incubator.code.dialect.java.JavaType;
36
37 /**
38 * The terminating conditional multi-branch operation modeling {@code tableswitch} and {@code lookupswitch} instructions.
39 * <p>
40 * This operation accepts an int operand, variable number of distinct constant labels
41 * and the same number of successors.
42 * When the operand is matching one of the labels, the relevant successor is selected.
43 * If none of the labels is matching, the default successor is selected.
44 * Default is a successor with corresponds null label value.
45 * The selected successor refers to the next block to branch to.
46 */
47 public final class ConstantLabelSwitchOp extends Op implements Op.BlockTerminating {
48
49 final List<Integer> labels;
50 final List<Block.Reference> targets;
51
52 public ConstantLabelSwitchOp(Value intSelector, List<Integer> labels, List<Block.Reference> targets) {
53 super(List.of(intSelector));
54 assert targets.size() == labels.size();
55 this.labels = labels;
56 this.targets = targets;
57 }
58
59 ConstantLabelSwitchOp(ConstantLabelSwitchOp that, CodeContext cc) {
60 super(that, cc);
61 this.labels = that.labels;
62 this.targets = that.targets.stream().map(cc::getSuccessorOrCreate).toList();
63 }
64
65 @Override
66 public ConstantLabelSwitchOp transform(CodeContext cc, CodeTransformer ot) {
67 return new ConstantLabelSwitchOp(this, cc);
68 }
69
70 @Override
71 public TypeElement resultType() {
72 return JavaType.VOID;
73 }
74
75 public List<Integer> labels() {
76 return labels;
77 }
78
79 @Override
80 public List<Block.Reference> successors() {
81 return targets;
82 }
83
84 @Override
85 public Map<String, Object> externalize() {
86 return Map.of("", labels);
87 }
88
89 }