interpreter updates
[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             Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
50             Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
51             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
52             ShipDescription sd = new ShipDescription(shipType, br);
53             InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
54             ships.put(shipname, ret);
55             if (shipType.equals("Debug") && debugShip == null)
56                 debugShip = ret;
57             return ret;
58         } catch (Exception e) {
59             e.printStackTrace();
60             return null;
61         }
62     }
63
64     void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
65     void debug(BitVector data) {
66         try {
67             if (debugStream != null) debugStream.put(data);
68             else Log.println(ANSI.invert("   DEBUG: got a datum: " +  data+ANSI.clreol()));
69         } catch (Exception e) {
70             throw new RuntimeException(e);
71         }
72     }
73
74     // Instruction Encoding /////////////////////////////////////////////////////////////////////////
75
76     public long getDestAddr(Path path) {
77         long ret = ((InterpreterDestination)path.getDestination()).addr;
78         BitVector bv = path.getSignal();
79         if (bv != null) {
80             if (bv.length() > 1) throw new RuntimeException("signal was " + bv.length() + " bits long!");
81             if (bv.length() > 0 && bv.get(0)) ret |= 1;
82         }
83         return ret;
84     }
85
86
87     // ShipDescription //////////////////////////////////////////////////////////////////////////////
88
89     public void expand(ShipDescription sd) {
90         try {
91             String filename = sd.getName();
92             //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
93             File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
94             new File(outf.getParent()).mkdirs();
95             System.err.println("writing to " + outf);
96             FileOutputStream out = new FileOutputStream(outf);
97             PrintWriter pw = new PrintWriter(out);
98
99             pw.println("package edu.berkeley.fleet.interpreter;");
100             pw.println("import edu.berkeley.sbp.util.ANSI;");
101             pw.println("import edu.berkeley.fleet.api.*;");
102             pw.println("import edu.berkeley.fleet.two.*;");
103             pw.println("import edu.berkeley.fleet.*;");
104             pw.println("import java.util.*;");
105             pw.println("import java.io.*;");
106             pw.println("");
107             pw.println("public class "+filename+" extends InterpreterShip {");
108             pw.println("");
109             for(DockDescription b : sd) {
110                 String name = b.getName();
111                 pw.print("    InterpreterDock box_");
112                 pw.print(name);
113                 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
114             }
115             pw.println("");
116             pw.println("    public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
117             pw.println("       super(fleet, sd);");
118             for(DockDescription b : sd)
119                 pw.println("       addDock(\""+b.getName()+"\", box_"+b.getName()+");");
120             pw.println("    }");
121             pw.println("");
122             pw.println(sd.getSection("fleeterpreter"));
123             pw.println("}");
124             pw.flush();
125             pw.close();
126         } catch (Exception e) { throw new RuntimeException(e); }
127     }
128
129     // Run //////////////////////////////////////////////////////////////////////////////
130
131     public FleetProcess run(final Instruction[] instructions) {
132         InterpreterProcess ip = new InterpreterProcess(instructions);
133         new Thread(ip).start();
134         return ip;
135     }
136
137     private class InterpreterProcess extends FleetProcess implements Runnable {
138         private Instruction[] instructions;
139         public InterpreterProcess(Instruction[] instructions) {
140             this.instructions = instructions;
141             for(Instruction i : instructions)
142                 dispatchInstruction(i);
143         }
144         public void dispatchInstruction(Instruction i) { dispatch(i); }
145         public Dock getDebugInputDock() { return debugShip.getDock("in"); }
146         public BitVector readWord() {
147             try {
148                 return debugStream.take();
149             } catch (Exception e) { throw new RuntimeException(e); }
150         }
151         protected void _terminate() { }
152         public void run() {
153             try {
154                 while(!isTerminated())
155                     for(InterpreterShip ship : ships.values())
156                         for(int j=0; j<10; j++)
157                             ship._service();
158                 for(InterpreterShip ship : ships.values())
159                     ship.reset();
160                 debugStream.clear();
161             } catch (Exception e) {
162                 if (isTerminated()) return;
163                 throw new RuntimeException(e);
164             }
165         }
166     }
167 }