01: // Copyright (c) 1999 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.bytecode;
05:
06: import java.io.*;
07:
08: /** A sequence of bytecode instructions belonging to a method's Code attribute.
09: * This provides a hook for re-arranging instruction sequences.
10: * It currently has limited functionality - no switch instructions,
11: * no non-relative jumps, etc.
12: * In the future this may be the basis for generating better code (e.g.
13: * avoiding needless jumps). Look at gcc/java/jcf-write.c for ideas.
14: *
15: * CodeFragment extends Label because it can be viewed as a label with
16: * some code following it.
17: */
18:
19: public class CodeFragment extends Label {
20: CodeFragment next;
21: byte[] insns;
22:
23: /** Length of code fragments (in instruction bytes), after genetaing code.
24: * (While generating code for this fragment, length is the start PC.) */
25: int length;
26:
27: /** If handlerIndex >= 0, it is the index in the exception_table
28: * for which this fragment is the handler. */
29: int handlerIndex;
30:
31: /** Used to save value of unreachable_here from the CodeAttr. */
32: boolean unreachable_save;
33:
34: /** Pairs of (relative PC, linenumber). */
35: short[] linenumbers;
36:
37: public CodeFragment(CodeAttr cattr) {
38: super (cattr);
39: handlerIndex = -1;
40: }
41:
42: public void emit(CodeAttr cattr) {
43: cattr.reserve(length);
44: System.arraycopy(insns, 0, cattr.code, cattr.PC, length);
45: define(cattr);
46: if (handlerIndex >= 0)
47: cattr.exception_table[4 * handlerIndex + 2] = (short) cattr.PC;
48: if (linenumbers != null) {
49: for (int i = 0; i < linenumbers.length; i += 2)
50: // The line numbers are already translated by the source map.
51: cattr.lines.put(linenumbers[i + 1], linenumbers[i]
52: + cattr.PC);
53: }
54: cattr.PC += length;
55: }
56: }
|