TableSwitchForm.java

  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. import java.util.Arrays;

  19. import org.apache.commons.compress.harmony.unpack200.bytecode.ByteCode;
  20. import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager;

  21. public class TableSwitchForm extends SwitchForm {

  22.     public TableSwitchForm(final int opcode, final String name) {
  23.         super(opcode, name);
  24.     }

  25.     /*
  26.      * (non-Javadoc)
  27.      *
  28.      * @see org.apache.commons.compress.harmony.unpack200.bytecode.forms.SwitchForm#setByteCodeOperands(org.apache.commons.
  29.      * compress.harmony.unpack200.bytecode.ByteCode, org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager, int)
  30.      */
  31.     @Override
  32.     public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, final int codeLength) {
  33.         final int caseCount = operandManager.nextCaseCount();
  34.         final int defaultPc = operandManager.nextLabel();
  35.         int caseValue = -1;
  36.         caseValue = operandManager.nextCaseValues();

  37.         final int[] casePcs = new int[caseCount];
  38.         Arrays.setAll(casePcs, i -> operandManager.nextLabel());

  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.         final int lowValue = caseValue;
  44.         final int highValue = lowValue + caseCount - 1;
  45.         // All this gets dumped into the rewrite bytes of the
  46.         // poor bytecode.

  47.         // Unlike most byte codes, the TableSwitch 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.         // First, there's the bytecode. Then there are 0-3
  53.         // bytes of padding so that the first (default)
  54.         // label is on a 4-byte offset.
  55.         final int padLength = 3 - codeLength % 4;
  56.         final int rewriteSize = 1 + padLength + 4 // defaultbytes
  57.                 + 4 // lowbyte
  58.                 + 4 // highbyte
  59.                 + 4 * casePcs.length;

  60.         final int[] newRewrite = new int[rewriteSize];
  61.         int rewriteIndex = 0;

  62.         // Fill in what we can now
  63.         // opcode
  64.         newRewrite[rewriteIndex++] = byteCode.getOpcode();

  65.         // padding
  66.         for (int index = 0; index < padLength; index++) {
  67.             newRewrite[rewriteIndex++] = 0;
  68.         }

  69.         // defaultbyte
  70.         // This gets overwritten by fixUpByteCodeTargets
  71.         newRewrite[rewriteIndex++] = -1;
  72.         newRewrite[rewriteIndex++] = -1;
  73.         newRewrite[rewriteIndex++] = -1;
  74.         newRewrite[rewriteIndex++] = -1;

  75.         // lowbyte
  76.         final int lowbyteIndex = rewriteIndex;
  77.         setRewrite4Bytes(lowValue, lowbyteIndex, newRewrite);
  78.         rewriteIndex += 4;

  79.         // highbyte
  80.         final int highbyteIndex = rewriteIndex;
  81.         setRewrite4Bytes(highValue, highbyteIndex, newRewrite);
  82.         rewriteIndex += 4;

  83.         // jump offsets
  84.         // The case_pcs will get overwritten by fixUpByteCodeTargets
  85.         for (int index = 0; index < caseCount; index++) {
  86.             // offset
  87.             newRewrite[rewriteIndex++] = -1;
  88.             newRewrite[rewriteIndex++] = -1;
  89.             newRewrite[rewriteIndex++] = -1;
  90.             newRewrite[rewriteIndex++] = -1;
  91.         }
  92.         byteCode.setRewrite(newRewrite);
  93.     }
  94. }