LookupSwitchForm.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 LookupSwitchForm extends SwitchForm {

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

  25.     @Override
  26.     public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, final int codeLength) {
  27.         final int caseCount = operandManager.nextCaseCount();
  28.         final int defaultPc = operandManager.nextLabel();
  29.         final int[] caseValues = new int[caseCount];
  30.         Arrays.setAll(caseValues, i -> operandManager.nextCaseValues());
  31.         final int[] casePcs = new int[caseCount];
  32.         Arrays.setAll(casePcs, i -> operandManager.nextLabel());

  33.         final int[] labelsArray = new int[caseCount + 1];
  34.         labelsArray[0] = defaultPc;
  35.         System.arraycopy(casePcs, 0, labelsArray, 1, caseCount + 1 - 1);
  36.         byteCode.setByteCodeTargets(labelsArray);

  37.         // All this gets dumped into the rewrite bytes of the
  38.         // poor bytecode.

  39.         // Unlike most byte codes, the LookupSwitch is a
  40.         // variable-sized bytecode. Because of this, the
  41.         // rewrite array has to be defined here individually
  42.         // for each bytecode, rather than in the ByteCodeForm
  43.         // class.

  44.         // First, there's the bytecode. Then there are 0-3
  45.         // bytes of padding so that the first (default)
  46.         // label is on a 4-byte offset.
  47.         final int padLength = 3 - codeLength % 4;
  48.         final int rewriteSize = 1 + padLength + 4 // defaultbytes
  49.                 + 4 // npairs
  50.                 + 4 * caseValues.length + 4 * casePcs.length;

  51.         final int[] newRewrite = new int[rewriteSize];
  52.         int rewriteIndex = 0;

  53.         // Fill in what we can now
  54.         // opcode
  55.         newRewrite[rewriteIndex++] = byteCode.getOpcode();

  56.         // padding
  57.         for (int index = 0; index < padLength; index++) {
  58.             newRewrite[rewriteIndex++] = 0;
  59.         }

  60.         // defaultbyte
  61.         // This gets overwritten by fixUpByteCodeTargets
  62.         newRewrite[rewriteIndex++] = -1;
  63.         newRewrite[rewriteIndex++] = -1;
  64.         newRewrite[rewriteIndex++] = -1;
  65.         newRewrite[rewriteIndex++] = -1;

  66.         // npairs
  67.         final int npairsIndex = rewriteIndex;
  68.         setRewrite4Bytes(caseValues.length, npairsIndex, newRewrite);
  69.         rewriteIndex += 4;

  70.         // match-offset pairs
  71.         // The caseValues aren't overwritten, but the
  72.         // casePcs will get overwritten by fixUpByteCodeTargets
  73.         for (final int caseValue : caseValues) {
  74.             // match
  75.             setRewrite4Bytes(caseValue, rewriteIndex, newRewrite);
  76.             rewriteIndex += 4;
  77.             // offset
  78.             newRewrite[rewriteIndex++] = -1;
  79.             newRewrite[rewriteIndex++] = -1;
  80.             newRewrite[rewriteIndex++] = -1;
  81.             newRewrite[rewriteIndex++] = -1;
  82.         }
  83.         byteCode.setRewrite(newRewrite);
  84.     }
  85. }