    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 org.apache.commons.compress.harmony.unpack200.bytecode.forms.ByteCodeForm#fixUpByteCodeTarget(org.apache.commons.
    043     * compress.harmony.unpack200.bytecode.ByteCode, org.apache.commons.compress.harmony.unpack200.bytecode.CodeAttribute)
    044     */
    045    @Override
    046    public void fixUpByteCodeTargets(final ByteCode byteCode, final CodeAttribute codeAttribute) {
    047        // LabelForms need to fix up the target of label operations
    048        final int originalTarget = byteCode.getByteCodeTargets()[0];
    049        final int sourceIndex = byteCode.getByteCodeIndex();
    050        final int absoluteInstructionTargetIndex = sourceIndex + originalTarget;
    051        final int targetValue = codeAttribute.byteCodeOffsets.get(absoluteInstructionTargetIndex).intValue();
    052        final int sourceValue = codeAttribute.byteCodeOffsets.get(sourceIndex).intValue();
    053        // The operand is the difference between the source instruction
    054        // and the destination instruction.
    055        byteCode.setOperandSigned2Bytes(targetValue - sourceValue, 0);
    056        if (widened) {
    057            byteCode.setNestedPositions(new int[][] { { 0, 4 } });
    058        } else {
    059            byteCode.setNestedPositions(new int[][] { { 0, 2 } });
    060        }
    061    }
    062
    063    /*
    064     * (non-Javadoc)
    065     *
    066     * @see org.apache.commons.compress.harmony.unpack200.bytecode.forms.ByteCodeForm#setByteCodeOperands(org.apache.commons.
    067     * compress.harmony.unpack200.bytecode.ByteCode, org.apache.commons.compress.harmony.unpack200.bytecode.OperandTable,
    068     * org.apache.commons.compress.harmony.unpack200.SegmentConstantPool)
    069     */
    070    @Override
    071    public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, final int codeLength) {
    072        byteCode.setByteCodeTargets(new int[] { operandManager.nextLabel() });
    073        // The byte code operands actually get set later -
    074        // once we have all the bytecodes - in fixUpByteCodeTarget().
    075    }
    076}