1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.bcel.internal.generic;
23
24import java.io.*;
25import com.sun.org.apache.bcel.internal.util.ByteSequence;
26
27/**
28 * TABLESWITCH - Switch within given range of values, i.e., low..high
29 *
30 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
31 * @see SWITCH
32 */
33public class TABLESWITCH extends Select {
34  /**
35   * Empty constructor needed for the Class.newInstance() statement in
36   * Instruction.readInstruction(). Not to be used otherwise.
37   */
38  TABLESWITCH() {}
39
40  /**
41   * @param match sorted array of match values, match[0] must be low value,
42   * match[match_length - 1] high value
43   * @param targets where to branch for matched values
44   * @param target default branch
45   */
46  public TABLESWITCH(int[] match, InstructionHandle[] targets,
47                     InstructionHandle target) {
48    super(com.sun.org.apache.bcel.internal.Constants.TABLESWITCH, match, targets, target);
49
50    length = (short)(13 + match_length * 4); /* Alignment remainder assumed
51                                              * 0 here, until dump time */
52    fixed_length = length;
53  }
54
55  /**
56   * Dump instruction as byte code to stream out.
57   * @param out Output stream
58   */
59  public void dump(DataOutputStream out) throws IOException {
60    super.dump(out);
61
62    int low = (match_length > 0)? match[0] : 0;
63    out.writeInt(low);
64
65    int high = (match_length > 0)? match[match_length - 1] : 0;
66    out.writeInt(high);
67
68    for(int i=0; i < match_length; i++)     // jump offsets
69      out.writeInt(indices[i] = getTargetOffset(targets[i]));
70  }
71
72  /**
73   * Read needed data (e.g. index) from file.
74   */
75  protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
76  {
77    super.initFromFile(bytes, wide);
78
79    int low    = bytes.readInt();
80    int high   = bytes.readInt();
81
82    match_length = high - low + 1;
83    fixed_length = (short)(13 + match_length * 4);
84    length       = (short)(fixed_length + padding);
85
86    match   = new int[match_length];
87    indices = new int[match_length];
88    targets = new InstructionHandle[match_length];
89
90    for(int i=low; i <= high; i++)
91      match[i - low] = i;
92
93    for(int i=0; i < match_length; i++) {
94      indices[i] = bytes.readInt();
95    }
96  }
97
98
99  /**
100   * Call corresponding visitor method(s). The order is:
101   * Call visitor methods of implemented interfaces first, then
102   * call methods according to the class hierarchy in descending order,
103   * i.e., the most specific visitXXX() call comes last.
104   *
105   * @param v Visitor object
106   */
107  public void accept(Visitor v) {
108    v.visitVariableLengthInstruction(this);
109    v.visitStackProducer(this);
110    v.visitBranchInstruction(this);
111    v.visitSelect(this);
112    v.visitTABLESWITCH(this);
113  }
114}
115