make getDestAddr() return a BitVector
[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 bv = path.getSignal();
80         if (bv != null) {
81             if (bv.length() > 1) throw new RuntimeException("signal was " + bv.length() + " bits long!");
82             if (bv.length() > 0 && bv.get(0)) ret |= 1;
83         }
84         throw new RuntimeException("fixme");
85     }
86
87
88     // ShipDescription //////////////////////////////////////////////////////////////////////////////
89
90     public void expand(ShipDescription sd) {
91         try {
92             String filename = sd.getName();
93             //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
94             File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
95             new File(outf.getParent()).mkdirs();
96             System.err.println("writing to " + outf);
97             FileOutputStream out = new FileOutputStream(outf);
98             PrintWriter pw = new PrintWriter(out);
99
100             pw.println("package edu.berkeley.fleet.interpreter;");
101             pw.println("import edu.berkeley.sbp.util.ANSI;");
102             pw.println("import edu.berkeley.fleet.api.*;");
103             pw.println("import edu.berkeley.fleet.two.*;");
104             pw.println("import edu.berkeley.fleet.*;");
105             pw.println("import java.util.*;");
106             pw.println("import java.io.*;");
107             pw.println("");
108             pw.println("public class "+filename+" extends InterpreterShip {");
109             pw.println("");
110             for(DockDescription b : sd) {
111                 String name = b.getName();
112                 pw.print("    InterpreterDock box_");
113                 pw.print(name);
114                 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
115             }
116             pw.println("");
117             pw.println("    public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
118             pw.println("       super(fleet, sd);");
119             for(DockDescription b : sd)
120                 pw.println("       addDock(\""+b.getName()+"\", box_"+b.getName()+");");
121             pw.println("    }");
122             pw.println("");
123             pw.println(sd.getSection("fleeterpreter"));
124             pw.println("}");
125             pw.flush();
126             pw.close();
127         } catch (Exception e) { throw new RuntimeException(e); }
128     }
129
130     // Run //////////////////////////////////////////////////////////////////////////////
131
132     public FleetProcess run(final Instruction[] instructions) {
133         InterpreterProcess ip = new InterpreterProcess(instructions);
134         new Thread(ip).start();
135         return ip;
136     }
137
138     private class InterpreterProcess extends FleetProcess implements Runnable {
139         private Instruction[] instructions;
140         public void dispatchWord(Destination d, BitVector word) {
141             throw new RuntimeException("not implemented");
142         }
143         public InterpreterProcess(Instruction[] instructions) {
144             this.instructions = instructions;
145             for(Instruction i : instructions)
146                 dispatchInstruction(i);
147         }
148         public Fleet getFleet() { return Interpreter.this; }
149         public void dispatchInstruction(Instruction i) { dispatch(i); }
150         public Dock getDebugInputDock() { return debugShip.getDock("in"); }
151         public BitVector readWord() {
152             try {
153                 return debugStream.take();
154             } catch (Exception e) { throw new RuntimeException(e); }
155         }
156         protected void _terminate() { }
157         public void run() {
158             try {
159                 while(!isTerminated())
160                     for(InterpreterShip ship : ships.values())
161                         for(int j=0; j<10; j++)
162                             ship._service();
163                 for(InterpreterShip ship : ships.values())
164                     ship.reset();
165                 debugStream.clear();
166             } catch (Exception e) {
167                 if (isTerminated()) return;
168                 throw new RuntimeException(e);
169             }
170         }
171     }
172 }