    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.CodeAttribute;
    021import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager;
    022
    023/**
    024 * This class implements the byte code form for those bytecodes which have label references (and only label references).
    025 */
    026public class LabelForm extends ByteCodeForm {
    027
    028    protected boolean widened;
    029
    030    public LabelForm(final int opcode, final String name, final int[] rewrite) {
    031        super(opcode, name, rewrite);
    032    }
    033
    034    public LabelForm(final int opcode, final String name, final int[] rewrite, final boolean widened) {
    035        this(opcode, name, rewrite);
    036        this.widened = widened;
    037    }
    038
    039    /*
    040     * (non-Javadoc)
    041     *
    042     * @see
    043     * org.apache.commons.compress.harmony.unpack200.bytecode.forms.ByteCodeForm#fixUpByteCodeTarget(org.apache.commons.
    044     * compress.harmony.unpack200.bytecode.ByteCode,
    045     * org.apache.commons.compress.harmony.unpack200.bytecode.CodeAttribute)
    046     */
    047    @Override
    048    public void fixUpByteCodeTargets(final ByteCode byteCode, final CodeAttribute codeAttribute) {
    049        // LabelForms need to fix up the target of label operations
    050        final int originalTarget = byteCode.getByteCodeTargets()[0];
    051        final int sourceIndex = byteCode.getByteCodeIndex();
    052        final int absoluteInstructionTargetIndex = sourceIndex + originalTarget;
    053        final int targetValue = codeAttribute.byteCodeOffsets.get(absoluteInstructionTargetIndex)
    054            .intValue();
    055        final int sourceValue = codeAttribute.byteCodeOffsets.get(sourceIndex).intValue();
    056        // The operand is the difference between the source instruction
    057        // and the destination instruction.
    058        byteCode.setOperandSigned2Bytes(targetValue - sourceValue, 0);
    059        if (widened) {
    060            byteCode.setNestedPositions(new int[][] {{0, 4}});
    061        } else {
    062            byteCode.setNestedPositions(new int[][] {{0, 2}});
    063        }
    064    }
    065
    066    /*
    067     * (non-Javadoc)
    068     *
    069     * @see
    070     * org.apache.commons.compress.harmony.unpack200.bytecode.forms.ByteCodeForm#setByteCodeOperands(org.apache.commons.
    071     * compress.harmony.unpack200.bytecode.ByteCode,
    072     * org.apache.commons.compress.harmony.unpack200.bytecode.OperandTable,
    073     * org.apache.commons.compress.harmony.unpack200.SegmentConstantPool)
    074     */
    075    @Override
    076    public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager,
    077        final int codeLength) {
    078        byteCode.setByteCodeTargets(new int[] {operandManager.nextLabel()});
    079        // The byte code operands actually get set later -
    080        // once we have all the bytecodes - in fixUpByteCodeTarget().
    081        return;
    082    }
    083}