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.doc.*;
9 import edu.berkeley.fleet.api.*;
10 import edu.berkeley.fleet.ies44.*;
12 public class Interpreter extends Fleet implements Fleet.WithDynamicShips {
14 /** some "halt ship" can turn this on to stop the interpreter */
15 private HashMap<String,InterpreterShip> ships = new HashMap<String,InterpreterShip>();
16 private BlockingQueue<Long> debugStream = new LinkedBlockingQueue<Long>();
18 public FleetProcess run(final byte[] instructions) {
20 final FleetProcess fp = new FleetProcess() {
21 public void invokeInstruction(Instruction i) { throw new RuntimeException("not supported"); }
22 public long readWord() {
24 return debugStream.take();
25 } catch (Exception e) {
26 throw new RuntimeException(e);
28 protected void _terminate() {
30 ships = new HashMap<String,InterpreterShip>();
31 debugStream = new LinkedBlockingQueue<Long>();
37 // find the first icache
38 InterpreterShip iscratch = null;
39 for(Ship ship : Interpreter.this)
40 if (ship.getClass().getSimpleName().equals("Memory")) {
41 iscratch = (InterpreterShip)ship;
45 iscratch = (InterpreterShip)Class.forName("edu.berkeley.fleet.interpreter.Memory")
46 .getConstructor(new Class[] { Interpreter.class, String.class })
47 .newInstance(new Object[] { Interpreter.this, "memory" });
50 .getMethod("boot", new Class[] { byte[].class })
51 .invoke(iscratch, new Object[] { instructions });
53 while(!fp.isTerminated())
54 for(InterpreterShip ship : ships.values())
55 for(int j=0; j<10; j++)
58 // run the ships a bit longer for good measure
59 for(int i=0; i<100; i++)
60 for(InterpreterShip ship : ships.values())
61 for(int j=0; j<10; j++)
64 // check the state of the ships
65 for(InterpreterShip ship : ships.values())
68 Log.println(ANSI.yellow(" DONE: ====== FLEET is halted. Have a nice day. ======"));
70 } catch (Exception e) {
71 if (fp.isTerminated()) return;
72 throw new RuntimeException(e);
77 } catch (Exception e) {
78 throw new RuntimeException(e);
82 public void dispatch(Instruction i, long address) {
84 if (i instanceof Instruction.Executable) {
85 InterpreterBenkoBox sourceBenkoBox = (InterpreterBenkoBox)(((Instruction.Executable)i).benkoBox);
86 ((InstructionBenkoBox)sourceBenkoBox).addInstruction(((Instruction.Executable)i));
88 } else if (i instanceof Instruction.Literal.CodeBagDescriptor) {
89 Instruction.Literal.CodeBagDescriptor cbd = (Instruction.Literal.CodeBagDescriptor)i;
90 long absolute_cbd = ((cbd.offset+address) << 6) | cbd.size;
91 new Packet(this, null, (int)absolute_cbd, (InterpreterDestination)cbd.dest).send();
93 } else if (i instanceof Instruction.Literal.Absolute) {
94 new Packet(this, null,
95 (int)((Instruction.Literal.Absolute)i).value,
96 (InterpreterDestination)(((Instruction.Literal.Absolute)i).dest)).send();
98 } else if (i instanceof Instruction.Kill) {
99 InterpreterBenkoBox benkoBox = (InterpreterBenkoBox)(((Instruction.Kill)i).benkoBox);
100 ((InstructionBenkoBox)benkoBox).kill(((Instruction.Kill)i).count,
101 ((Instruction.Kill)i).killOnlyStandingInstructions);
104 throw new Error("unsupported: " + i.getClass().getName());
108 public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
110 public Ship createShip(String shipType, String shipname) {
112 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
113 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class });
114 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname });
115 ships.put(shipname, ret);
117 } catch (Exception e) {
122 public void debug(long data) {
124 if (debugStream != null) debugStream.put(data);
125 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
126 } catch (Exception e) {
127 throw new RuntimeException(e);
131 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
133 public final InterpreterInstructionEncoder iie = new InterpreterInstructionEncoder();
134 public Instruction readInstruction(DataInputStream is) throws IOException { return iie.readInstruction(is); }
135 public Instruction readInstruction(long instr) { return iie.readInstruction(instr); }
136 public long writeInstruction(Instruction d) { return writeInstruction(d); }
137 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException { iie.writeInstruction(os, d); }
138 public class InterpreterInstructionEncoder extends InstructionEncoder {
139 public long getDestAddr(Destination box) { return ((InterpreterDestination)box).getDestAddr(); }
140 public long getBoxInstAddr(BenkoBox box) { return ((InterpreterBenkoBox)box).getDestAddr(); }
141 public Destination getDestByAddr(long dest) {
142 for(Ship ship : Interpreter.this)
143 for(BenkoBox bb : ship.getBenkoBoxes())
144 for(Destination d : bb.getDestinations())
145 if (getDestAddr(d)==dest)
149 public BenkoBox getBoxByInstAddr(long dest) {
150 for(Ship ship : Interpreter.this)
151 for(BenkoBox bb : ship.getBenkoBoxes())
152 if (getBoxInstAddr(bb) == dest)
158 // ShipDescription //////////////////////////////////////////////////////////////////////////////
160 public void expand(ShipDescription sd) {
162 String filename = sd.getName();
163 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
164 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
165 new File(outf.getParent()).mkdirs();
166 System.err.println("writing to " + outf);
167 FileOutputStream out = new FileOutputStream(outf);
168 PrintWriter pw = new PrintWriter(out);
170 pw.println("package edu.berkeley.fleet.interpreter;");
171 pw.println("import edu.berkeley.sbp.util.ANSI;");
172 pw.println("import edu.berkeley.fleet.api.*;");
173 pw.println("import edu.berkeley.fleet.*;");
174 pw.println("import java.util.*;");
175 pw.println("import java.io.*;");
177 pw.println("public class "+filename+" extends InterpreterShip {");
179 for(BenkoBoxDescription b : sd) {
180 String name = b.getName();
182 if ( b.isInbox()) pw.print("Inbox");
183 if (!b.isInbox()) pw.print("Outbox");
187 if ( b.isInbox()) pw.print("Inbox");
188 if (!b.isInbox()) pw.print("Outbox");
189 pw.print("(this, \""+name+"\", new String[] { ");
190 boolean first = true;
191 for(String destination : b) {
192 if (!first) pw.print(", ");
194 pw.print("\""+destination+"\"");
199 pw.println(" public "+filename+"(Interpreter fleet, String name) {");
200 pw.println(" super(fleet, name, \""+filename+"\");");
201 for(BenkoBoxDescription b : sd)
202 pw.println(" addBenkoBox(\""+b.getName()+"\", box_"+b.getName()+");");
205 pw.println(sd.getSection("fleeterpreter"));
209 } catch (Exception e) { throw new RuntimeException(e); }