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