001    package AST;
002    
003    import java.util.HashSet;
004    import java.io.File;
005    import java.util.*;
006    import beaver.*;
007    import java.util.ArrayList;
008    import java.util.zip.*;
009    import java.io.*;
010    import java.io.FileNotFoundException;
011    import java.util.Collection;
012    /*************************************************************
013       * Auxiliary class
014       ************************************************************ * @ast class
015     * 
016     */
017    public class ByteArray extends java.lang.Object {
018    
019        private int stackDepth = 0;
020    
021    
022        private int maxStackDepth = 0;
023    
024    
025        private int size = 64;
026    
027    
028        private byte[] bytes = new byte[size];
029    
030    
031        private int pos = 0;
032    
033    
034        private int lastGotoPos = 0;
035    
036    
037        ByteArray add(int i) {return add((byte)i);}
038    
039    
040        ByteArray add(byte b) {
041          if(pos >= size) {
042            byte[] ba = new byte[size * 2];
043            System.arraycopy(bytes, 0, ba, 0, size);
044            size *= 2;
045            bytes = ba;
046          }
047          bytes[pos++] = b;
048          return this;
049        }
050    
051    
052        ByteArray add4(int i) { 
053          add(i >> 24 & 0xff);
054          add(i >> 16 & 0xff);
055          add(i >> 8 & 0xff);
056          add(i & 0xff);
057          return this;
058        }
059    
060    
061        ByteArray add2(int index) {
062          add(index >> 8 & 0xff);
063          add(index & 0xff);
064          return this;
065        }
066    
067    
068        ByteArray emit(byte b) {
069          changeStackDepth(BytecodeDebug.stackChange(b));
070          add(b);
071          return this;
072        }
073    
074    
075        ByteArray emitGoto(byte b) {
076          changeStackDepth(BytecodeDebug.stackChange(b));
077          lastGotoPos = pos;
078          add(b);
079          return this;
080        }
081    
082    
083        ByteArray emit(byte b, int stackChange) {
084          changeStackDepth(stackChange);
085          add(b);
086          return this;
087        }
088    
089    
090        
091        public int maxStackDepth() {
092          return maxStackDepth;
093        }
094    
095    
096        public int stackDepth() {
097          return stackDepth;
098        }
099    
100    
101        public void changeStackDepth(int i) {
102          stackDepth += i;
103          if(stackDepth > maxStackDepth)
104            maxStackDepth = stackDepth;
105        }
106    
107    
108        
109        public int pos() {return pos;}
110    
111    
112        public int lastGotoPos() {return lastGotoPos;}
113    
114    
115        public void setPos(int index) { pos = index; }
116    
117    
118        public int size() {return pos;}
119    
120    
121        public byte get(int index) {return bytes[index];}
122    
123    
124        public void set(int index, byte value) {bytes[index] = value;}
125    
126    
127        public String toString() {
128          StringBuffer b = new StringBuffer();
129          for(int i = 0; i < pos; i++) b.append(" " + bytes[i]);
130          return b.toString();
131        }
132    
133    
134        public byte[] toArray() {
135          byte[] b = new byte[pos];
136          System.arraycopy(bytes, 0, b, 0, pos);
137          return b;
138        }
139    
140    
141    }