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 019import org.apache.commons.compress.harmony.unpack200.bytecode.ByteCode; 020import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager; 021 022public class LookupSwitchForm extends SwitchForm { 023 024 public LookupSwitchForm(final int opcode, final String name) { 025 super(opcode, name); 026 } 027 028 /* 029 * (non-Javadoc) 030 * 031 * @see 032 * org.apache.commons.compress.harmony.unpack200.bytecode.forms.SwitchForm#setByteCodeOperands(org.apache.commons. 033 * compress.harmony.unpack200.bytecode.ByteCode, 034 * org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager, int) 035 */ 036 @Override 037 public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, 038 final int codeLength) { 039 final int case_count = operandManager.nextCaseCount(); 040 final int default_pc = operandManager.nextLabel(); 041 final int case_values[] = new int[case_count]; 042 for (int index = 0; index < case_count; index++) { 043 case_values[index] = operandManager.nextCaseValues(); 044 } 045 final int case_pcs[] = new int[case_count]; 046 for (int index = 0; index < case_count; index++) { 047 case_pcs[index] = operandManager.nextLabel(); 048 } 049 050 final int[] labelsArray = new int[case_count + 1]; 051 labelsArray[0] = default_pc; 052 for (int index = 1; index < case_count + 1; index++) { 053 labelsArray[index] = case_pcs[index - 1]; 054 } 055 byteCode.setByteCodeTargets(labelsArray); 056 057 // All this gets dumped into the rewrite bytes of the 058 // poor bytecode. 059 060 // Unlike most byte codes, the LookupSwitch is a 061 // variable-sized bytecode. Because of this, the 062 // rewrite array has to be defined here individually 063 // for each bytecode, rather than in the ByteCodeForm 064 // class. 065 066 // First, there's the bytecode. Then there are 0-3 067 // bytes of padding so that the first (default) 068 // label is on a 4-byte offset. 069 final int padLength = 3 - (codeLength % 4); 070 final int rewriteSize = 1 + padLength + 4 // defaultbytes 071 + 4 // npairs 072 + (4 * case_values.length) + (4 * case_pcs.length); 073 074 final int[] newRewrite = new int[rewriteSize]; 075 int rewriteIndex = 0; 076 077 // Fill in what we can now 078 // opcode 079 newRewrite[rewriteIndex++] = byteCode.getOpcode(); 080 081 // padding 082 for (int index = 0; index < padLength; index++) { 083 newRewrite[rewriteIndex++] = 0; 084 } 085 086 // defaultbyte 087 // This gets overwritten by fixUpByteCodeTargets 088 newRewrite[rewriteIndex++] = -1; 089 newRewrite[rewriteIndex++] = -1; 090 newRewrite[rewriteIndex++] = -1; 091 newRewrite[rewriteIndex++] = -1; 092 093 // npairs 094 final int npairsIndex = rewriteIndex; 095 setRewrite4Bytes(case_values.length, npairsIndex, newRewrite); 096 rewriteIndex += 4; 097 098 // match-offset pairs 099 // The case_values aren't overwritten, but the 100 // case_pcs will get overwritten by fixUpByteCodeTargets 101 for (int index = 0; index < case_values.length; index++) { 102 // match 103 setRewrite4Bytes(case_values[index], rewriteIndex, newRewrite); 104 rewriteIndex += 4; 105 // offset 106 newRewrite[rewriteIndex++] = -1; 107 newRewrite[rewriteIndex++] = -1; 108 newRewrite[rewriteIndex++] = -1; 109 newRewrite[rewriteIndex++] = -1; 110 } 111 byteCode.setRewrite(newRewrite); 112 } 113}