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