checkpoint
[fleet.git] / src / edu / berkeley / fleet / CodeBag.java
1 package edu.berkeley.fleet;
2
3 import java.util.*;
4 import java.io.*;
5
6 /** a codebag */
7 public class CodeBag {
8
9     private static int max_allocated_descriptor = 1;
10     private static HashMap<Integer,CodeBag> codeBagsByDescriptor = new HashMap<Integer,CodeBag>();
11
12     public static CodeBag getCodeBagByDescriptor(int descriptor) {
13         return codeBagsByDescriptor.get(descriptor);
14     }
15
16     private final int descriptor;
17     private final CodeBag parent;
18     private final String name;
19     private ArrayList<Dispatchable> dispatchables = new ArrayList<Dispatchable>();
20     private HashMap<String,CodeBag> codeBags = new HashMap<String,CodeBag>();
21
22     public CodeBag(CodeBag parent, String name) {
23         this.parent = parent;
24         this.name = name;
25         this.descriptor = max_allocated_descriptor++;
26         codeBagsByDescriptor.put(descriptor, this);
27         if (parent != null && name != null)
28             parent.add(name, this);
29     }
30
31     public void dispatch(Fleet fleet) {
32         for(Dispatchable d : dispatchables) {
33             Log.dispatch(d);
34             d.dispatch(fleet);
35         }
36     }
37
38     public void add(Dispatchable instr) {
39         dispatchables.add(instr);
40     }
41
42     public void add(String name, CodeBag instr) {
43         codeBags.put(name, instr);
44     }
45
46     public int getDescriptor() {
47         return descriptor;
48     }
49
50     public CodeBag getCodeBag(String name) {
51         CodeBag ret = codeBags.get(name);
52         if (ret != null) return ret;
53         if (parent==null) return null;
54         return parent.getCodeBag(name);
55     }
56
57     public void dump(OutputStream os, int data) throws Exception {
58         os.write((byte)data);
59         System.out.print(data+" ");
60     }
61     public void dump(Fleet fleet) throws Exception {
62         OutputStream os = new FileOutputStream("fleet.bin");
63         for(Dispatchable d : dispatchables) {
64             if (d instanceof Instruction) {
65                 Instruction inst = (Instruction)d;
66                 dump(os, inst.source.resolve(fleet).instr_bits);
67                 dump(os, inst.source.resolve(fleet).instr_addr);
68                 dump(os, 5);
69                 dump(os, 10); // input+output
70                 dump(os, inst.destination.resolve(fleet).bits);
71                 dump(os, inst.destination.resolve(fleet).addr);
72                 dump(os, 0);
73                 System.out.println();
74             } else if (d instanceof Literal.LiteralDatum) {
75                 Literal.LiteralDatum ld = (Literal.LiteralDatum)d;
76                 dump(os, ld.destination.resolve(fleet).instr_bits);
77                 dump(os, ld.destination.resolve(fleet).instr_addr);
78                 dump(os, 5);
79                 dump(os, 2);
80                 dump(os, 32);
81                 dump(os, ld.data);
82                 dump(os, 0);
83                 System.out.println();
84             }
85         }
86         os.flush();
87         os.close();
88     }
89
90     public String toString() {
91         if (name != null) return name;
92         StringBuffer ret = new StringBuffer();
93         for(Dispatchable d : dispatchables) {
94             ret.append(d);
95             ret.append("\n");
96         }
97         for(String n : codeBags.keySet()) {
98             ret.append(n + ": ");
99             ret.append(codeBags.get(n));
100             ret.append("\n");
101         }
102         return "{\n  "+Log.indent(ret.toString(), "  ")+"\n}";
103     }
104
105
106 }