1 package edu.berkeley.fleet.interpreter;
4 import java.util.concurrent.*;
5 import java.lang.reflect.*;
6 import edu.berkeley.fleet.*;
7 import edu.berkeley.sbp.util.ANSI;
8 import edu.berkeley.fleet.api.*;
9 import edu.berkeley.fleet.two.*;
10 import edu.berkeley.fleet.assembler.*;
11 import edu.berkeley.fleet.two.*;
12 import edu.berkeley.fleet.util.*;
14 public class Interpreter extends FleetTwoFleet implements Parser.FleetWithDynamicShips {
16 public Ship getShip(String type, int ordinal) {
18 if (s.getType().equals(type))
24 public int getWordWidth() { return 37; }
25 /** some "halt ship" can turn this on to stop the interpreter */
26 private HashMap<String,InterpreterShip> ships = new HashMap<String,InterpreterShip>();
27 private BlockingQueue<Long> debugStream = new LinkedBlockingQueue<Long>();
29 public int getWordSize() { return 37; }
31 public FleetProcess run(final byte[] instructions) {
33 final FleetProcess fp = new FleetProcess() {
34 public void dispatchInstruction(Instruction i) { throw new RuntimeException(); }
35 public void invokeInstruction(Instruction i) { throw new RuntimeException("not supported"); }
36 public Dock getDebugInputDock() { return null; }
37 public BitVector readWord() {
40 return debugStream.take();
41 } catch (Exception e) {
42 throw new RuntimeException(e);
44 throw new RuntimeException();
46 protected void _terminate() {
48 ships = new HashMap<String,InterpreterShip>();
49 debugStream = new LinkedBlockingQueue<Long>();
55 // find the first icache
56 InterpreterShip iscratch = null;
57 for(Ship ship : Interpreter.this)
58 if (ship.getClass().getSimpleName().equals("Memory")) {
59 iscratch = (InterpreterShip)ship;
64 new BufferedReader(new InputStreamReader(new FileInputStream("ships/Memory.ship")));
65 ShipDescription sd = new ShipDescription("Memory", br);
66 iscratch = (InterpreterShip)Class.forName("edu.berkeley.fleet.interpreter.Memory")
67 .getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class })
68 .newInstance(new Object[] { Interpreter.this, "memory", sd });
72 .getMethod("boot", new Class[] { byte[].class })
73 .invoke(iscratch, new Object[] { instructions });
75 while(!fp.isTerminated())
76 for(InterpreterShip ship : ships.values())
77 for(int j=0; j<10; j++)
80 // run the ships a bit longer for good measure
81 for(int i=0; i<100; i++)
82 for(InterpreterShip ship : ships.values())
83 for(int j=0; j<10; j++)
86 // check the state of the ships
87 for(InterpreterShip ship : ships.values())
90 Log.println(ANSI.yellow(" DONE: ====== FLEET is halted. Have a nice day. ======"));
92 } catch (Exception e) {
93 if (fp.isTerminated()) return;
94 throw new RuntimeException(e);
99 } catch (Exception e) {
100 throw new RuntimeException(e);
104 public void dispatch(Instruction i, long address) {
107 if (i instanceof Instruction.Tail) {
108 Instruction.Tail ic = (Instruction.Tail)i;
109 ((InstructionDock)(ic.dock)).tailged--;
112 InterpreterDock sourceDock = (InterpreterDock)(((Instruction)i).dock);
113 ((InstructionDock)sourceDock).addInstruction(((Instruction)i));
117 public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
119 public Ship createShip(String shipType, String shipname) {
121 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
122 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
123 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
124 ShipDescription sd = new ShipDescription(shipType, br);
125 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
126 ships.put(shipname, ret);
128 } catch (Exception e) {
134 public void debug(long data) {
136 if (debugStream != null) debugStream.put(data);
137 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
138 } catch (Exception e) {
139 throw new RuntimeException(e);
143 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
145 public Dock getUniversalSource() { return null; }
147 public long getDestAddr(Path path) { throw new RuntimeException(); }
148 public long getBoxInstAddr(Dock box) { return ((InterpreterDock)box).getDestAddr(); }
149 public Path getPathByAddr(Dock source, long dest) { throw new RuntimeException(); }
150 public Destination getDestByAddr(long dest) {
152 for(Ship ship : Interpreter.this)
154 if (getDestAddr(bb.getDataDestination())==dest)
155 return bb.getDataDestination();
159 public Dock getBoxByInstAddr(long dest) {
160 for(Ship ship : Interpreter.this)
162 if (getBoxInstAddr(bb) == dest)
168 // ShipDescription //////////////////////////////////////////////////////////////////////////////
170 public void expand(ShipDescription sd) {
172 String filename = sd.getName();
173 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
174 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
175 new File(outf.getParent()).mkdirs();
176 System.err.println("writing to " + outf);
177 FileOutputStream out = new FileOutputStream(outf);
178 PrintWriter pw = new PrintWriter(out);
180 pw.println("package edu.berkeley.fleet.interpreter;");
181 pw.println("import edu.berkeley.sbp.util.ANSI;");
182 pw.println("import edu.berkeley.fleet.api.*;");
183 pw.println("import edu.berkeley.fleet.two.*;");
184 pw.println("import edu.berkeley.fleet.*;");
185 pw.println("import java.util.*;");
186 pw.println("import java.io.*;");
188 pw.println("public class "+filename+" extends InterpreterShip {");
190 for(DockDescription b : sd) {
191 String name = b.getName();
193 if ( b.isInputDock()) pw.print("Inbox");
194 if (!b.isInputDock()) pw.print("Outbox");
198 if ( b.isInputDock()) pw.print("Inbox");
199 if (!b.isInputDock()) pw.print("Outbox");
200 pw.print("(this, new String[] { ");
201 boolean first = true;
203 pw.println("}, shipDescription.getDockDescription(\""+name+"\"));");
206 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
207 pw.println(" super(fleet, name, sd);");
208 for(DockDescription b : sd)
209 pw.println(" addDock(\""+b.getName()+"\", box_"+b.getName()+");");
212 pw.println(sd.getSection("fleeterpreter"));
216 } catch (Exception e) { throw new RuntimeException(e); }