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 TableSwitchForm extends SwitchForm {
023
024    public TableSwitchForm(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        int case_value = -1;
042        case_value = operandManager.nextCaseValues();
043
044        final int case_pcs[] = new int[case_count];
045        for (int index = 0; index < case_count; index++) {
046            case_pcs[index] = operandManager.nextLabel();
047        }
048
049        final int[] labelsArray = new int[case_count + 1];
050        labelsArray[0] = default_pc;
051        for (int index = 1; index < case_count + 1; index++) {
052            labelsArray[index] = case_pcs[index - 1];
053        }
054        byteCode.setByteCodeTargets(labelsArray);
055
056        final int lowValue = case_value;
057        final int highValue = lowValue + case_count - 1;
058        // All this gets dumped into the rewrite bytes of the
059        // poor bytecode.
060
061        // Unlike most byte codes, the TableSwitch is a
062        // variable-sized bytecode. Because of this, the
063        // rewrite array has to be defined here individually
064        // for each bytecode, rather than in the ByteCodeForm
065        // class.
066
067        // First, there's the bytecode. Then there are 0-3
068        // bytes of padding so that the first (default)
069        // label is on a 4-byte offset.
070        final int padLength = 3 - (codeLength % 4);
071        final int rewriteSize = 1 + padLength + 4 // defaultbytes
072            + 4 // lowbyte
073            + 4 // highbyte
074            + (4 * case_pcs.length);
075
076        final int[] newRewrite = new int[rewriteSize];
077        int rewriteIndex = 0;
078
079        // Fill in what we can now
080        // opcode
081        newRewrite[rewriteIndex++] = byteCode.getOpcode();
082
083        // padding
084        for (int index = 0; index < padLength; index++) {
085            newRewrite[rewriteIndex++] = 0;
086        }
087
088        // defaultbyte
089        // This gets overwritten by fixUpByteCodeTargets
090        newRewrite[rewriteIndex++] = -1;
091        newRewrite[rewriteIndex++] = -1;
092        newRewrite[rewriteIndex++] = -1;
093        newRewrite[rewriteIndex++] = -1;
094
095        // lowbyte
096        final int lowbyteIndex = rewriteIndex;
097        setRewrite4Bytes(lowValue, lowbyteIndex, newRewrite);
098        rewriteIndex += 4;
099
100        // highbyte
101        final int highbyteIndex = rewriteIndex;
102        setRewrite4Bytes(highValue, highbyteIndex, newRewrite);
103        rewriteIndex += 4;
104
105        // jump offsets
106        // The case_pcs will get overwritten by fixUpByteCodeTargets
107        for (int index = 0; index < case_count; index++) {
108            // offset
109            newRewrite[rewriteIndex++] = -1;
110            newRewrite[rewriteIndex++] = -1;
111            newRewrite[rewriteIndex++] = -1;
112            newRewrite[rewriteIndex++] = -1;
113        }
114        byteCode.setRewrite(newRewrite);
115    }
116}