View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.harmony.unpack200.bytecode.forms;
18  
19  import java.util.Arrays;
20  
21  import org.apache.commons.compress.harmony.unpack200.bytecode.ByteCode;
22  import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager;
23  
24  public class TableSwitchForm extends SwitchForm {
25  
26      public TableSwitchForm(final int opcode, final String name) {
27          super(opcode, name);
28      }
29  
30      /*
31       * (non-Javadoc)
32       *
33       * @see org.apache.commons.compress.harmony.unpack200.bytecode.forms.SwitchForm#setByteCodeOperands(org.apache.commons.
34       * compress.harmony.unpack200.bytecode.ByteCode, org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager, int)
35       */
36      @Override
37      public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, final int codeLength) {
38          final int caseCount = operandManager.nextCaseCount();
39          final int defaultPc = operandManager.nextLabel();
40          int caseValue = -1;
41          caseValue = operandManager.nextCaseValues();
42  
43          final int[] casePcs = new int[caseCount];
44          Arrays.setAll(casePcs, i -> operandManager.nextLabel());
45  
46          final int[] labelsArray = new int[caseCount + 1];
47          labelsArray[0] = defaultPc;
48          System.arraycopy(casePcs, 0, labelsArray, 1, caseCount + 1 - 1);
49          byteCode.setByteCodeTargets(labelsArray);
50  
51          final int lowValue = caseValue;
52          final int highValue = lowValue + caseCount - 1;
53          // All this gets dumped into the rewrite bytes of the
54          // poor bytecode.
55  
56          // Unlike most byte codes, the TableSwitch is a
57          // variable-sized bytecode. Because of this, the
58          // rewrite array has to be defined here individually
59          // for each bytecode, rather than in the ByteCodeForm
60          // class.
61  
62          // First, there's the bytecode. Then there are 0-3
63          // bytes of padding so that the first (default)
64          // label is on a 4-byte offset.
65          final int padLength = 3 - codeLength % 4;
66          final int rewriteSize = 1 + padLength + 4 // defaultbytes
67                  + 4 // lowbyte
68                  + 4 // highbyte
69                  + 4 * casePcs.length;
70  
71          final int[] newRewrite = new int[rewriteSize];
72          int rewriteIndex = 0;
73  
74          // Fill in what we can now
75          // opcode
76          newRewrite[rewriteIndex++] = byteCode.getOpcode();
77  
78          // padding
79          for (int index = 0; index < padLength; index++) {
80              newRewrite[rewriteIndex++] = 0;
81          }
82  
83          // defaultbyte
84          // This gets overwritten by fixUpByteCodeTargets
85          newRewrite[rewriteIndex++] = -1;
86          newRewrite[rewriteIndex++] = -1;
87          newRewrite[rewriteIndex++] = -1;
88          newRewrite[rewriteIndex++] = -1;
89  
90          // lowbyte
91          final int lowbyteIndex = rewriteIndex;
92          setRewrite4Bytes(lowValue, lowbyteIndex, newRewrite);
93          rewriteIndex += 4;
94  
95          // highbyte
96          final int highbyteIndex = rewriteIndex;
97          setRewrite4Bytes(highValue, highbyteIndex, newRewrite);
98          rewriteIndex += 4;
99  
100         // jump offsets
101         // The case_pcs will get overwritten by fixUpByteCodeTargets
102         for (int index = 0; index < caseCount; index++) {
103             // offset
104             newRewrite[rewriteIndex++] = -1;
105             newRewrite[rewriteIndex++] = -1;
106             newRewrite[rewriteIndex++] = -1;
107             newRewrite[rewriteIndex++] = -1;
108         }
109         byteCode.setRewrite(newRewrite);
110     }
111 }