fix bug in Interpreter.getDestAddr()
[fleet.git] / src / edu / berkeley / fleet / interpreter / Interpreter.java
1 package edu.berkeley.fleet.interpreter;
2 import java.io.*;
3 import java.util.*;
4 import java.util.concurrent.*;
5 import java.lang.reflect.*;
6 import edu.berkeley.sbp.util.ANSI;
7 import edu.berkeley.fleet.api.*;
8 import edu.berkeley.fleet.two.*;
9 import edu.berkeley.fleet.assembler.*;
10 import edu.berkeley.fleet.util.*;
11
12 public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynamicShips {
13
14     private InterpreterShip debugShip = null;
15     private BlockingQueue<BitVector> debugStream = new LinkedBlockingQueue<BitVector>();
16     private HashMap<String,InterpreterShip> ships = new HashMap<String,InterpreterShip>();
17     public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
18     public Ship getShip(String type, int ordinal) {
19         for(Ship s : this)
20             if (s.getType().equals(type))
21                 if (ordinal-- <= 0)
22                     return s;
23         return null;
24     }
25
26     /** do not use this; it is going to go away */
27     public Interpreter() {
28     }
29
30     public Interpreter(String[] ships, boolean logging) {
31         int i=0;
32         Log.quiet = !logging;
33         for(String s : ships) {
34             createShip(ships[i], ships[i]+"_"+i);
35             i++;
36         }
37     }
38
39     void dispatch(Instruction i) {
40         Log.dispatch(i);
41         long il = writeInstruction(i, debugShip.getDock("in"));
42         Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
43         new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
44     }
45
46     /** do not use this; it is going to go away */
47     public Ship createShip(String shipType, String shipname) {
48         try {
49             if (ships.get(shipname)!=null) return ships.get(shipname);
50             Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
51             Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
52             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
53             ShipDescription sd = new ShipDescription(shipType, br);
54             InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
55             ships.put(shipname, ret);
56             if (shipType.equals("Debug") && debugShip == null)
57                 debugShip = ret;
58             return ret;
59         } catch (Exception e) {
60             e.printStackTrace();
61             return null;
62         }
63     }
64
65     void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
66     void debug(BitVector data) {
67         try {
68             if (debugStream != null) debugStream.put(data);
69             else Log.println(ANSI.invert("   DEBUG: got a datum: " +  data+ANSI.clreol()));
70         } catch (Exception e) {
71             throw new RuntimeException(e);
72         }
73     }
74
75     // Instruction Encoding /////////////////////////////////////////////////////////////////////////
76
77     public BitVector getDestAddr(Path path) {
78         long ret = ((InterpreterDestination)path.getDestination()).addr;
79         BitVector sig = path.getSignal();
80         BitVector bv = new BitVector(DISPATCH_PATH.valmaskwidth+1);
81         bv.set(ret);
82         if (sig != null) {
83             if (sig.length() > 1) throw new RuntimeException("signal was " + sig.length() + " bits long!");
84             if (sig.length() > 0 && sig.get(0)) bv.set(bv.length()-1,true);
85         }
86         return bv;
87     }
88
89
90     // ShipDescription //////////////////////////////////////////////////////////////////////////////
91
92     public void expand(ShipDescription sd) {
93         try {
94             String filename = sd.getName();
95             //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
96             File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
97             new File(outf.getParent()).mkdirs();
98             System.err.println("writing to " + outf);
99             FileOutputStream out = new FileOutputStream(outf);
100             PrintWriter pw = new PrintWriter(out);
101
102             pw.println("package edu.berkeley.fleet.interpreter;");
103             pw.println("import edu.berkeley.sbp.util.ANSI;");
104             pw.println("import edu.berkeley.fleet.api.*;");
105             pw.println("import edu.berkeley.fleet.two.*;");
106             pw.println("import edu.berkeley.fleet.*;");
107             pw.println("import java.util.*;");
108             pw.println("import java.io.*;");
109             pw.println("");
110             pw.println("public class "+filename+" extends InterpreterShip {");
111             pw.println("");
112             for(DockDescription b : sd) {
113                 String name = b.getName();
114                 pw.print("    InterpreterDock box_");
115                 pw.print(name);
116                 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
117             }
118             pw.println("");
119             pw.println("    public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
120             pw.println("       super(fleet, sd);");
121             for(DockDescription b : sd)
122                 pw.println("       addDock(\""+b.getName()+"\", box_"+b.getName()+");");
123             pw.println("    }");
124             pw.println("");
125             pw.println(sd.getSection("fleeterpreter"));
126             pw.println("}");
127             pw.flush();
128             pw.close();
129         } catch (Exception e) { throw new RuntimeException(e); }
130     }
131
132     // Run //////////////////////////////////////////////////////////////////////////////
133
134     public FleetProcess run(final Instruction[] instructions) {
135         InterpreterProcess ip = new InterpreterProcess(instructions);
136         new Thread(ip).start();
137         return ip;
138     }
139
140     public class InterpreterProcess extends FleetProcess implements Runnable {
141         private Instruction[] instructions;
142         public void flush() { }
143         public void sendWord(Destination d, BitVector word) {
144             throw new RuntimeException("not implemented");
145         }
146         public void sendToken(Destination d) { throw new RuntimeException("not implemented"); }
147         public InterpreterProcess(Instruction[] instructions) {
148             this.instructions = instructions;
149             for(Instruction i : instructions)
150                 sendInstruction(i);
151         }
152         public Fleet getFleet() { return Interpreter.this; }
153         public void sendInstruction(Instruction i) { dispatch(i); }
154         public Dock getDebugInputDock() { return debugShip.getDock("in"); }
155         public BitVector recvWord() {
156             try {
157                 return debugStream.take();
158             } catch (Exception e) { throw new RuntimeException(e); }
159         }
160         protected void _terminate() { }
161         public void run() {
162             try {
163                 while(!isTerminated())
164                     for(InterpreterShip ship : ships.values())
165                         for(int j=0; j<10; j++)
166                             ship._service();
167                 for(InterpreterShip ship : ships.values())
168                     ship.reset();
169                 debugStream.clear();
170             } catch (Exception e) {
171                 if (isTerminated()) return;
172                 throw new RuntimeException(e);
173             }
174         }
175
176         public void step(Dock d) {
177             ((InterpreterDock)d).service();
178         }
179         
180         public void step(Ship s) {
181             ((InterpreterShip)s).service();
182         }
183
184     }
185 }