VariableInstructionForm.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. /**
  19.  * This abstract class implements the common code for instructions which have variable lengths. This is currently the *switch instructions and some wide (_w)
  20.  * instructions.
  21.  */
  22. public abstract class VariableInstructionForm extends ByteCodeForm {

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

  26.     /**
  27.      * This method writes operand directly into the rewrite array at index position specified.
  28.      *
  29.      * @param operand     value to write
  30.      * @param absPosition position in array to write. Note that this is absolute position in the array, so one can overwrite the bytecode if one isn't careful.
  31.      * @param rewrite     array to write into
  32.      */
  33.     public void setRewrite2Bytes(final int operand, final int absPosition, final int[] rewrite) {
  34.         if (absPosition < 0) {
  35.             throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
  36.         }

  37.         final int byteCodeRewriteLength = rewrite.length;

  38.         if (absPosition + 1 > byteCodeRewriteLength) {
  39.             throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
  40.         }

  41.         rewrite[absPosition] = (0xFF00 & operand) >> 8;
  42.         rewrite[absPosition + 1] = 0x00FF & operand;
  43.     }

  44.     /**
  45.      * This method writes operand directly into the rewrite array at index position specified.
  46.      *
  47.      * @param operand     value to write
  48.      * @param absPosition position in array to write. Note that this is absolute position in the array, so one can overwrite the bytecode if one isn't careful.
  49.      * @param rewrite     array to write into
  50.      */
  51.     public void setRewrite4Bytes(final int operand, final int absPosition, final int[] rewrite) {
  52.         if (absPosition < 0) {
  53.             throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
  54.         }

  55.         final int byteCodeRewriteLength = rewrite.length;

  56.         if (absPosition + 3 > byteCodeRewriteLength) {
  57.             throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
  58.         }

  59.         rewrite[absPosition] = (0xFF000000 & operand) >> 24;
  60.         rewrite[absPosition + 1] = (0x00FF0000 & operand) >> 16;
  61.         rewrite[absPosition + 2] = (0x0000FF00 & operand) >> 8;
  62.         rewrite[absPosition + 3] = 0x000000FF & operand;
  63.     }

  64.     /**
  65.      * Given an int operand, set the rewrite bytes for the next available operand position and the three immediately following it to a highest-byte, mid-high,
  66.      * mid-low, low-byte encoding of the operand.
  67.      *
  68.      * Note that unlike the ByteCode setOperand* operations, this starts with an actual bytecode rewrite array (rather than a ByteCodeForm prototype rewrite
  69.      * array). Also, this method overwrites -1 values in the rewrite array - so if you start with an array that looks like: {100, -1, -1, -1, -1, 200, -1, -1,
  70.      * -1, -1} then calling setRewrite4Bytes(0, rewrite) the first time will convert it to: {100, 0, 0, 0, 0, 200, -1, -1, -1, -1} Calling setRewrite4Bytes(0,
  71.      * rewrite) a second time will convert it to: {100, 0, 0, 0, 0, 200, 0, 0, 0, 0}
  72.      *
  73.      * @param operand int to set the rewrite bytes to
  74.      * @param rewrite int[] bytes to rewrite
  75.      */
  76.     public void setRewrite4Bytes(final int operand, final int[] rewrite) {
  77.         int firstOperandPosition = -1;

  78.         // Find the first -1 in the rewrite array
  79.         for (int index = 0; index < rewrite.length - 3; index++) {
  80.             if (rewrite[index] == -1 && rewrite[index + 1] == -1 && rewrite[index + 2] == -1 && rewrite[index + 3] == -1) {
  81.                 firstOperandPosition = index;
  82.                 break;
  83.             }
  84.         }
  85.         setRewrite4Bytes(operand, firstOperandPosition, rewrite);
  86.     }
  87. }