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 LookupSwitchForm extends SwitchForm {
25  
26      public LookupSwitchForm(final int opcode, final String name) {
27          super(opcode, name);
28      }
29  
30      @Override
31      public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, final int codeLength) {
32          final int caseCount = operandManager.nextCaseCount();
33          final int defaultPc = operandManager.nextLabel();
34          final int[] caseValues = new int[caseCount];
35          Arrays.setAll(caseValues, i -> operandManager.nextCaseValues());
36          final int[] casePcs = new int[caseCount];
37          Arrays.setAll(casePcs, i -> operandManager.nextLabel());
38  
39          final int[] labelsArray = new int[caseCount + 1];
40          labelsArray[0] = defaultPc;
41          System.arraycopy(casePcs, 0, labelsArray, 1, caseCount + 1 - 1);
42          byteCode.setByteCodeTargets(labelsArray);
43  
44          // All this gets dumped into the rewrite bytes of the
45          // poor bytecode.
46  
47          // Unlike most byte codes, the LookupSwitch is a
48          // variable-sized bytecode. Because of this, the
49          // rewrite array has to be defined here individually
50          // for each bytecode, rather than in the ByteCodeForm
51          // class.
52  
53          // First, there's the bytecode. Then there are 0-3
54          // bytes of padding so that the first (default)
55          // label is on a 4-byte offset.
56          final int padLength = 3 - codeLength % 4;
57          final int rewriteSize = 1 + padLength + 4 // defaultbytes
58                  + 4 // npairs
59                  + 4 * caseValues.length + 4 * casePcs.length;
60  
61          final int[] newRewrite = new int[rewriteSize];
62          int rewriteIndex = 0;
63  
64          // Fill in what we can now
65          // opcode
66          newRewrite[rewriteIndex++] = byteCode.getOpcode();
67  
68          // padding
69          for (int index = 0; index < padLength; index++) {
70              newRewrite[rewriteIndex++] = 0;
71          }
72  
73          // defaultbyte
74          // This gets overwritten by fixUpByteCodeTargets
75          newRewrite[rewriteIndex++] = -1;
76          newRewrite[rewriteIndex++] = -1;
77          newRewrite[rewriteIndex++] = -1;
78          newRewrite[rewriteIndex++] = -1;
79  
80          // npairs
81          final int npairsIndex = rewriteIndex;
82          setRewrite4Bytes(caseValues.length, npairsIndex, newRewrite);
83          rewriteIndex += 4;
84  
85          // match-offset pairs
86          // The caseValues aren't overwritten, but the
87          // casePcs will get overwritten by fixUpByteCodeTargets
88          for (final int caseValue : caseValues) {
89              // match
90              setRewrite4Bytes(caseValue, rewriteIndex, newRewrite);
91              rewriteIndex += 4;
92              // offset
93              newRewrite[rewriteIndex++] = -1;
94              newRewrite[rewriteIndex++] = -1;
95              newRewrite[rewriteIndex++] = -1;
96              newRewrite[rewriteIndex++] = -1;
97          }
98          byteCode.setRewrite(newRewrite);
99      }
100 }