01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.pack200.bytecode.forms;
18:
19: import org.apache.harmony.pack200.bytecode.ByteCode;
20: import org.apache.harmony.pack200.bytecode.CodeAttribute;
21: import org.apache.harmony.pack200.bytecode.OperandManager;
22:
23: /**
24: * This class implements the byte code form for those
25: * bytecodes which have label references (and only
26: * label references).
27: */
28: public class LabelForm extends ByteCodeForm {
29:
30: protected boolean widened = false;
31:
32: public LabelForm(int opcode, String name, int[] rewrite) {
33: super (opcode, name, rewrite);
34: // TODO Auto-generated constructor stub
35: }
36:
37: public LabelForm(int opcode, String name, int[] rewrite,
38: boolean widened) {
39: this (opcode, name, rewrite);
40: this .widened = widened;
41: }
42:
43: public int getOperandType() {
44: return TYPE_LABEL;
45: }
46:
47: public boolean hasLabelOperand() {
48: return true;
49: }
50:
51: /* (non-Javadoc)
52: * @see org.apache.harmony.pack200.bytecode.forms.ByteCodeForm#fixUpByteCodeTarget(org.apache.harmony.pack200.bytecode.ByteCode, org.apache.harmony.pack200.bytecode.CodeAttribute)
53: */
54: public void fixUpByteCodeTargets(ByteCode byteCode,
55: CodeAttribute codeAttribute) {
56: // LabelForms need to fix up the target of label operations
57: int originalTarget = byteCode.getByteCodeTargets()[0];
58: int sourceIndex = byteCode.getByteCodeIndex();
59: int absoluteInstructionTargetIndex = sourceIndex
60: + originalTarget;
61: int targetValue = ((Integer) codeAttribute.byteCodeOffsets
62: .get(absoluteInstructionTargetIndex)).intValue();
63: int sourceValue = ((Integer) codeAttribute.byteCodeOffsets
64: .get(sourceIndex)).intValue();
65: // The operand is the difference between the source instruction
66: // and the destination instruction.
67: byteCode.setOperandSigned2Bytes(targetValue - sourceValue, 0);
68: if (widened) {
69: byteCode.setNestedPositions(new int[][] { { 0, 4 } });
70: } else {
71: byteCode.setNestedPositions(new int[][] { { 0, 2 } });
72: }
73: }
74:
75: /* (non-Javadoc)
76: * @see org.apache.harmony.pack200.bytecode.forms.ByteCodeForm#setByteCodeOperands(org.apache.harmony.pack200.bytecode.ByteCode, org.apache.harmony.pack200.bytecode.OperandTable, org.apache.harmony.pack200.SegmentConstantPool)
77: */
78: public void setByteCodeOperands(ByteCode byteCode,
79: OperandManager operandManager, int codeLength) {
80: byteCode.setByteCodeTargets(new int[] { operandManager
81: .nextLabel() });
82: // The byte code operands actually get set later -
83: // once we have all the bytecodes - in fixUpByteCodeTarget().
84: return;
85: }
86: }
|