    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.harmony.unpack200.bytecode.forms;
    020
    021import java.util.Arrays;
    022
    023import org.apache.commons.compress.harmony.unpack200.bytecode.ByteCode;
    024import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager;
    025
    026/**
    027 * Table switch instruction form.
    028 */
    029public class TableSwitchForm extends SwitchForm {
    030
    031    /**
    032     * Constructs a new instance with the specified opcode, name, operandType and rewrite.
    033     *
    034     * @param opcode  index corresponding to the opcode's value.
    035     * @param name    String printable name of the opcode.
    036     */
    037    public TableSwitchForm(final int opcode, final String name) {
    038        super(opcode, name);
    039    }
    040
    041    /*
    042     * (non-Javadoc)
    043     *
    044     * @see org.apache.commons.compress.harmony.unpack200.bytecode.forms.SwitchForm#setByteCodeOperands(org.apache.commons.
    045     * compress.harmony.unpack200.bytecode.ByteCode, org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager, int)
    046     */
    047    @Override
    048    public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, final int codeLength) {
    049        final int caseCount = operandManager.nextCaseCount();
    050        final int defaultPc = operandManager.nextLabel();
    051        int caseValue = -1;
    052        caseValue = operandManager.nextCaseValues();
    053
    054        final int[] casePcs = new int[caseCount];
    055        Arrays.setAll(casePcs, i -> operandManager.nextLabel());
    056
    057        final int[] labelsArray = new int[caseCount + 1];
    058        labelsArray[0] = defaultPc;
    059        System.arraycopy(casePcs, 0, labelsArray, 1, caseCount + 1 - 1);
    060        byteCode.setByteCodeTargets(labelsArray);
    061
    062        final int lowValue = caseValue;
    063        final int highValue = lowValue + caseCount - 1;
    064        // All this gets dumped into the rewrite bytes of the
    065        // poor bytecode.
    066
    067        // Unlike most byte codes, the TableSwitch is a
    068        // variable-sized bytecode. Because of this, the
    069        // rewrite array has to be defined here individually
    070        // for each bytecode, rather than in the ByteCodeForm
    071        // class.
    072
    073        // First, there's the bytecode. Then there are 0-3
    074        // bytes of padding so that the first (default)
    075        // label is on a 4-byte offset.
    076        final int padLength = 3 - codeLength % 4;
    077        final int rewriteSize = 1 + padLength + 4 // defaultbytes
    078                + 4 // lowbyte
    079                + 4 // highbyte
    080                + 4 * casePcs.length;
    081
    082        final int[] newRewrite = new int[rewriteSize];
    083        int rewriteIndex = 0;
    084
    085        // Fill in what we can now
    086        // opcode
    087        newRewrite[rewriteIndex++] = byteCode.getOpcode();
    088
    089        // padding
    090        for (int index = 0; index < padLength; index++) {
    091            newRewrite[rewriteIndex++] = 0;
    092        }
    093
    094        // defaultbyte
    095        // This gets overwritten by fixUpByteCodeTargets
    096        newRewrite[rewriteIndex++] = -1;
    097        newRewrite[rewriteIndex++] = -1;
    098        newRewrite[rewriteIndex++] = -1;
    099        newRewrite[rewriteIndex++] = -1;
    100
    101        // lowbyte
    102        final int lowbyteIndex = rewriteIndex;
    103        setRewrite4Bytes(lowValue, lowbyteIndex, newRewrite);
    104        rewriteIndex += 4;
    105
    106        // highbyte
    107        final int highbyteIndex = rewriteIndex;
    108        setRewrite4Bytes(highValue, highbyteIndex, newRewrite);
    109        rewriteIndex += 4;
    110
    111        // jump offsets
    112        // The case_pcs will get overwritten by fixUpByteCodeTargets
    113        for (int index = 0; index < caseCount; index++) {
    114            // offset
    115            newRewrite[rewriteIndex++] = -1;
    116            newRewrite[rewriteIndex++] = -1;
    117            newRewrite[rewriteIndex++] = -1;
    118            newRewrite[rewriteIndex++] = -1;
    119        }
    120        byteCode.setRewrite(newRewrite);
    121    }
    122}