001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.compress.harmony.unpack200.bytecode.forms; 018 019/** 020 * This abstract class implements the common code for instructions which have variable lengths. This is currently the 021 * *switch instructions and some wide (_w) instructions. 022 */ 023public abstract class VariableInstructionForm extends ByteCodeForm { 024 025 public VariableInstructionForm(final int opcode, final String name) { 026 super(opcode, name); 027 } 028 029 /** 030 * Given an int operand, set the rewrite bytes for the next available operand position and the three immediately 031 * following it to a highest-byte, mid-high, mid-low, low-byte encoding of the operand. 032 * 033 * Note that unlike the ByteCode setOperand* operations, this starts with an actual bytecode rewrite array (rather 034 * than a ByteCodeForm prototype rewrite array). Also, this method overwrites -1 values in the rewrite array - so if 035 * you start with an array that looks like: {100, -1, -1, -1, -1, 200, -1, -1, -1, -1} then calling 036 * setRewrite4Bytes(0, rewrite) the first time will convert it to: {100, 0, 0, 0, 0, 200, -1, -1, -1, -1} Calling 037 * setRewrite4Bytes(0, rewrite) a second time will convert it to: {100, 0, 0, 0, 0, 200, 0, 0, 0, 0} 038 * 039 * @param operand int to set the rewrite bytes to 040 * @param rewrite int[] bytes to rewrite 041 */ 042 public void setRewrite4Bytes(final int operand, final int[] rewrite) { 043 int firstOperandPosition = -1; 044 045 // Find the first -1 in the rewrite array 046 for (int index = 0; index < rewrite.length - 3; index++) { 047 if ((rewrite[index] == -1) && (rewrite[index + 1] == -1) && (rewrite[index + 2] == -1) 048 && (rewrite[index + 3] == -1)) { 049 firstOperandPosition = index; 050 break; 051 } 052 } 053 setRewrite4Bytes(operand, firstOperandPosition, rewrite); 054 } 055 056 /** 057 * This method writes operand directly into the rewrite array at index position specified. 058 * 059 * @param operand value to write 060 * @param absPosition position in array to write. Note that this is absolute position in the array, so one can 061 * overwrite the bytecode if one isn't careful. 062 * @param rewrite array to write into 063 */ 064 public void setRewrite4Bytes(final int operand, final int absPosition, final int[] rewrite) { 065 if (absPosition < 0) { 066 throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes"); 067 } 068 069 final int byteCodeRewriteLength = rewrite.length; 070 071 if (absPosition + 3 > byteCodeRewriteLength) { 072 throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition 073 + " but this won't fit in the rewrite array"); 074 } 075 076 rewrite[absPosition] = ((0xFF000000) & operand) >> 24; 077 rewrite[absPosition + 1] = ((0x00FF0000) & operand) >> 16; 078 rewrite[absPosition + 2] = ((0x0000FF00) & operand) >> 8; 079 rewrite[absPosition + 3] = ((0x000000FF) & operand); 080 } 081 082 /** 083 * This method writes operand directly into the rewrite array at index position specified. 084 * 085 * @param operand value to write 086 * @param absPosition position in array to write. Note that this is absolute position in the array, so one can 087 * overwrite the bytecode if one isn't careful. 088 * @param rewrite array to write into 089 */ 090 public void setRewrite2Bytes(final int operand, final int absPosition, final int[] rewrite) { 091 if (absPosition < 0) { 092 throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes"); 093 } 094 095 final int byteCodeRewriteLength = rewrite.length; 096 097 if (absPosition + 1 > byteCodeRewriteLength) { 098 throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition 099 + " but this won't fit in the rewrite array"); 100 } 101 102 rewrite[absPosition] = ((0xFF00) & operand) >> 8; 103 rewrite[absPosition + 1] = ((0x00FF) & operand); 104 } 105}