1 package edu.berkeley.fleet;
9 private static int max_allocated_descriptor = 1;
10 private static HashMap<Integer,CodeBag> codeBagsByDescriptor = new HashMap<Integer,CodeBag>();
12 public static CodeBag getCodeBagByDescriptor(int descriptor) {
13 return codeBagsByDescriptor.get(descriptor);
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>();
22 public CodeBag(CodeBag parent, String name) {
25 this.descriptor = max_allocated_descriptor++;
26 codeBagsByDescriptor.put(descriptor, this);
27 if (parent != null && name != null)
28 parent.add(name, this);
31 public void dispatch(Fleet fleet) {
32 for(Dispatchable d : dispatchables) {
38 public void add(Dispatchable instr) {
39 dispatchables.add(instr);
42 public void add(String name, CodeBag instr) {
43 codeBags.put(name, instr);
46 public int getDescriptor() {
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);
57 public void dump(OutputStream os, int data) throws Exception {
59 System.out.print(data+" ");
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);
69 dump(os, 10); // input+output
70 dump(os, inst.destination.resolve(fleet).bits);
71 dump(os, inst.destination.resolve(fleet).addr);
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);
90 public String toString() {
91 if (name != null) return name;
92 StringBuffer ret = new StringBuffer();
93 for(Dispatchable d : dispatchables) {
97 for(String n : codeBags.keySet()) {
99 ret.append(codeBags.get(n));
102 return "{\n "+Log.indent(ret.toString(), " ")+"\n}";