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 LookupSwitchForm extends SwitchForm {
025
026    public LookupSwitchForm(final int opcode, final String name) {
027        super(opcode, name);
028    }
029
030    /*
031     * (non-Javadoc)
032     *
033     * @see
034     * org.apache.commons.compress.harmony.unpack200.bytecode.forms.SwitchForm#setByteCodeOperands(org.apache.commons.
035     * compress.harmony.unpack200.bytecode.ByteCode,
036     * org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager, int)
037     */
038    @Override
039    public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager,
040        final int codeLength) {
041        final int caseCount = operandManager.nextCaseCount();
042        final int defaultPc = operandManager.nextLabel();
043        final int[] caseValues = new int[caseCount];
044        Arrays.setAll(caseValues, i -> operandManager.nextCaseValues());
045        final int[] casePcs = new int[caseCount];
046        Arrays.setAll(casePcs, i -> operandManager.nextLabel());
047
048        final int[] labelsArray = new int[caseCount + 1];
049        labelsArray[0] = defaultPc;
050        System.arraycopy(casePcs, 0, labelsArray, 1, caseCount + 1 - 1);
051        byteCode.setByteCodeTargets(labelsArray);
052
053        // All this gets dumped into the rewrite bytes of the
054        // poor bytecode.
055
056        // Unlike most byte codes, the LookupSwitch 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 // npairs
068            + (4 * caseValues.length) + (4 * casePcs.length);
069
070        final int[] newRewrite = new int[rewriteSize];
071        int rewriteIndex = 0;
072
073        // Fill in what we can now
074        // opcode
075        newRewrite[rewriteIndex++] = byteCode.getOpcode();
076
077        // padding
078        for (int index = 0; index < padLength; index++) {
079            newRewrite[rewriteIndex++] = 0;
080        }
081
082        // defaultbyte
083        // This gets overwritten by fixUpByteCodeTargets
084        newRewrite[rewriteIndex++] = -1;
085        newRewrite[rewriteIndex++] = -1;
086        newRewrite[rewriteIndex++] = -1;
087        newRewrite[rewriteIndex++] = -1;
088
089        // npairs
090        final int npairsIndex = rewriteIndex;
091        setRewrite4Bytes(caseValues.length, npairsIndex, newRewrite);
092        rewriteIndex += 4;
093
094        // match-offset pairs
095        // The caseValues aren't overwritten, but the
096        // casePcs will get overwritten by fixUpByteCodeTargets
097        for (final int caseValue : caseValues) {
098            // match
099            setRewrite4Bytes(caseValue, rewriteIndex, newRewrite);
100            rewriteIndex += 4;
101            // offset
102            newRewrite[rewriteIndex++] = -1;
103            newRewrite[rewriteIndex++] = -1;
104            newRewrite[rewriteIndex++] = -1;
105            newRewrite[rewriteIndex++] = -1;
106        }
107        byteCode.setRewrite(newRewrite);
108    }
109}