implement am26 support for constants
authoradam <adam@megacz.com>
Thu, 23 Aug 2007 01:03:32 +0000 (02:03 +0100)
committeradam <adam@megacz.com>
Thu, 23 Aug 2007 01:03:32 +0000 (02:03 +0100)
14 files changed:
ships/Alu2.ship
src/edu/berkeley/fleet/api/BenkoBox.java
src/edu/berkeley/fleet/assembler/Parser.java
src/edu/berkeley/fleet/assembler/fleet.g
src/edu/berkeley/fleet/doc/BenkoBoxDescription.java
src/edu/berkeley/fleet/doc/Constant.java
src/edu/berkeley/fleet/doc/ShipDescription.java
src/edu/berkeley/fleet/fpga/FpgaBenkoBox.java
src/edu/berkeley/fleet/fpga/FpgaShip.java
src/edu/berkeley/fleet/interpreter/InstructionBenkoBox.java
src/edu/berkeley/fleet/interpreter/Interpreter.java
src/edu/berkeley/fleet/interpreter/InterpreterBenkoBox.java
src/edu/berkeley/fleet/interpreter/InterpreterShip.java
tests/recycle/test-recycle.fleet

index db76145..df756ab 100644 (file)
@@ -4,14 +4,13 @@ ship: Alu2
 data  in:   in1
 data  in:   in2
 data  in:   inOp
+  constant ADD: 0
+  constant SUB: 1
+  constant MAX: 2
+  constant MIN: 3
 
 data  out:  out
 
-== Constants ========================================================
-ADD: add the two arguments; treat link as carry
-SUB: subtract the two arguments; treat link as carry
-MAX:
-MIN:
 
 == TeX ==============================================================
 This ship is a two-input arithmetic unit.  It features several
@@ -120,10 +119,10 @@ debug.in:   [*] take, deliver;
 8:          sendto alu.in2;
 8:          sendto alu.in2;
 
-Alu2.ADD:   sendto alu.inOp;
-Alu2.SUB:   sendto alu.inOp;
-Alu2.MIN:   sendto alu.inOp;
-Alu2.MAX:   sendto alu.inOp;
+Alu2.inOp[ADD]:   sendto alu.inOp;
+Alu2.inOp[SUB]:   sendto alu.inOp;
+Alu2.inOp[MIN]:   sendto alu.inOp;
+Alu2.inOp[MAX]:   sendto alu.inOp;
 
 alu.in1:    [*] take, deliver;
 alu.in2:    [*] take, deliver;
index 693087b..bf892ad 100644 (file)
@@ -30,4 +30,5 @@ public abstract class BenkoBox {
     /** return the BenkoBox which is the destination of this Box's shortcut (if any) */
     public BenkoBox getShortcut() { return null; }
 
+    public abstract long resolveLiteral(String literal);
 }            
index af432a8..21f870b 100644 (file)
@@ -229,7 +229,13 @@ public class Parser {
                     break;
                 }
             }
-            long literal = chosenship.resolveLiteral(portName);
+            BenkoBox chosenport = chosenship.getBenkoBox(portName);
+            Tree specs = t.child(0).child(2);
+            long literal = 0;
+            for(int i=0; i<specs.size(); i++) {
+                Tree tt = specs.child(i);
+                literal |= chosenport.resolveLiteral(string(tt));
+            }
             cb.add(new Instruction.Literal.Absolute(portReference(t.child(1)), literal));
 
         } else if (t.head().equals("Literal")) {
index f73a0fe..4158d18 100644 (file)
@@ -40,21 +40,24 @@ Command         = Nop::      "nop"
 Source         = Port
                | ShipSpecific
 
-SSL            = ShipSpecificLiteral:: shiptype "." ShipSpecificLiteral
+SSL            = ShipSpecificLiteral:: shiptype "." portname "[" SSLSpec "]"
 Port           = Port::     shipname "." portname
                | SubPort::  shipname "." portname "." portname
                |           ^"()"
 
+SSLSpec::      = SSLElement +/ ","
+SSLElement     = "":: [a-zA-Z0-9=_]++
+
 CodeBagBody::  = Statement +/ ws
 CodeBag        = CodeBagRef:: CodeBagName
                | AnonymousCodeBag:: "{" CodeBagBody "}" /ws
 
 CodeBagName         = name
 shipname            = name
-shiptype            = Name:: [A-Z]    ("":: [A-Za-z0-9\[\]_]**)
-ShipSpecificLiteral = Name:: [A-Z]    ("":: [A-Z0-9\[\]_]**)
-portname            = Name:: [a-z]    ("":: [A-Za-z0-9\[\]_]**)
-name                = Name:: [A-Za-z] ("":: [A-Za-z0-9\[\]_]**)
+shiptype            = Name:: [A-Z]    ("":: [A-Za-z0-9_]**)
+ShipSpecificLiteral = Name:: [A-Z]    ("":: [A-Z0-9_]**)
+portname            = Name:: [a-z]    ("":: [A-Za-z0-9_]**)
+name                = Name:: [A-Za-z] ("":: [A-Za-z0-9_]**)
 index               = "[" ("":: [0-9]+) "]" | ("":: [0-9]+)
 int                 = "":: [\-0-9]++
 ShipSpecific        = ShipSpecific:: "\"" (~[\"])++ "\""
index 7b689ac..8cead4b 100644 (file)
@@ -38,10 +38,24 @@ public class BenkoBoxDescription implements Iterable<String> {
 
     void addDest(String dest) { destinations.add(dest); }
 
-
     public Constant getConstant(String name) {
         Constant ret = constants.get(name);
         if (ret == null) ret = ship.getConstant(name);
         return ret;
     }
+
+    public long resolveLiteral(String s) {
+        long val = 0;
+        long ret = 0;
+        if (s.indexOf('=') != -1) {
+            val = Long.parseLong(s.substring(s.indexOf('=')+1));
+            s = s.substring(0, s.indexOf('='));
+        }
+        Constant c = getConstant(s);
+        if (c==null) throw new RuntimeException("no constant " + s + " on benkobox " + this);
+        ret |= c.setbits;
+        ret &= ~c.clearbits;
+        // FIXME: val
+        return ret;
+    }
 }
index 959e88c..5756ea9 100644 (file)
@@ -32,27 +32,5 @@ public class Constant {
         }
     }
 
-    public static long resolveConstant(String exp, BenkoBoxDescription bbd) {
-        long ret = 0;
-        for(StringTokenizer st = new StringTokenizer(exp, ",");
-            st.hasMoreTokens();) {
-            String s = st.nextToken();
-            long val = 0;
-            if (s.indexOf('=') != -1) {
-                val = Long.parseLong(s.substring(s.indexOf('=')+1));
-                s = s.substring(0, s.indexOf('='));
-            }
-            Constant c = bbd.getConstant(s);
-            if (c==null) {
-                if (s.startsWith("0x")) ret |= Long.parseLong(s, 16);
-                else                    ret |= Long.parseLong(s);
-            } else {
-                ret |= c.setbits;
-                ret &= ~c.clearbits;
-                // FIXME: val
-            }
-        }
-        return ret;
-    }
 
 }
\ No newline at end of file
index bb203fb..6ea4241 100644 (file)
@@ -96,6 +96,7 @@ public class ShipDescription implements Iterable<BenkoBoxDescription> {
                     String constname = key.substring("constant".length()+1).trim();
                     String val       = s.substring(s.indexOf(':')+1).trim();
                     p.constants.put(constname, new Constant(val));
+                    continue;
                 } else if (key.startsWith("shortcut to")) {
                     continue;
                 }
index fe7074f..413a3c1 100644 (file)
@@ -1,4 +1,6 @@
 package edu.berkeley.fleet.fpga;
+import edu.berkeley.fleet.doc.*;
+import edu.berkeley.fleet.api.*;
 import edu.berkeley.fleet.api.*;
 import edu.berkeley.fleet.api.BenkoBox;
 import java.util.*;
@@ -22,6 +24,7 @@ public class FpgaBenkoBox extends BenkoBox {
     public int addr = (default_addr++);
     public int instr_bits;
     public int instr_addr = (default_instr_addr++);
+    private final BenkoBoxDescription bbd;
     protected boolean special = false;
     protected boolean ihorn = false;
     protected boolean dhorn = false;
@@ -32,13 +35,17 @@ public class FpgaBenkoBox extends BenkoBox {
     public boolean inbox;
     public boolean isInbox() { return inbox; }
     public boolean isOutbox() { return !inbox; }
-    public FpgaBenkoBox(boolean inbox, FpgaShip ship, String name) {
-        this(inbox, ship, name, false, false, false);
+    public long resolveLiteral(String literal) {
+        return bbd.resolveLiteral(literal);
+    }
+    public FpgaBenkoBox(BenkoBoxDescription bbd, boolean inbox, FpgaShip ship, String name) {
+        this(bbd, inbox, ship, name, false, false, false);
     }
-    public FpgaBenkoBox(boolean inbox, FpgaShip ship, String name, boolean special) {
-        this(inbox, ship, name, special, false, false);
+    public FpgaBenkoBox(BenkoBoxDescription bbd, boolean inbox, FpgaShip ship, String name, boolean special) {
+        this(bbd, inbox, ship, name, special, false, false);
     }
-    public FpgaBenkoBox(boolean inbox, FpgaShip ship, String name, boolean special, boolean ihorn, boolean dhorn) {
+    public FpgaBenkoBox(BenkoBoxDescription bbd, boolean inbox, FpgaShip ship, String name, boolean special, boolean ihorn, boolean dhorn) {
+        this.bbd = bbd;
         this.inbox = inbox;
         this.special = special;
         this.dhorn = dhorn;
index f285cda..68cf609 100644 (file)
@@ -11,23 +11,23 @@ public class FpgaShip extends Ship {
     public FpgaShip(Fpga fleet, String name, String type, ShipDescription sd) {
         this.fleet = fleet; this.type = type;
         for(BenkoBoxDescription sdbb : sd) {
-            FpgaBenkoBox sbb = new FpgaBenkoBox(sdbb.isInbox(), this, sdbb.getName());
+            FpgaBenkoBox sbb = new FpgaBenkoBox(sdbb, sdbb.isInbox(), this, sdbb.getName());
             for(String port : sdbb) {
                 if (port.equals("")) continue;
                 sbb.addDestination(port);
             }
         }
         if        (type.equals("Debug")) {
-            new FpgaBenkoBox(false, this, "out", true);
+            new FpgaBenkoBox(null, false, this, "out", true);
             
         } else if (type.equals("Execute")) {
-            new FpgaBenkoBox(false, this, "ihorn", true, true, false);
-            new FpgaBenkoBox(false, this, "dhorn", true, false, true);
+            new FpgaBenkoBox(null, false, this, "ihorn", true, true, false);
+            new FpgaBenkoBox(null, false, this, "dhorn", true, false, true);
             
         } else if (type.equals("Memory")) {
-            new FpgaBenkoBox(true,  this, "command", true);
-            new FpgaBenkoBox(false, this, "ihorn",   true, true, false);
-            new FpgaBenkoBox(false, this, "dhorn",   true, false, true);
+            new FpgaBenkoBox(null, true,  this, "command", true);
+            new FpgaBenkoBox(null, false, this, "ihorn",   true, true, false);
+            new FpgaBenkoBox(null, false, this, "dhorn",   true, false, true);
         }
     }
 
index 8b74ac1..261b873 100644 (file)
@@ -1,6 +1,7 @@
 package edu.berkeley.fleet.interpreter;
 import edu.berkeley.sbp.util.ANSI;
 import edu.berkeley.fleet.api.*;
+import edu.berkeley.fleet.doc.*;
 import edu.berkeley.fleet.api.Instruction;
 
 import java.util.*;
index 59b1575..5996334 100644 (file)
@@ -112,6 +112,9 @@ public class Interpreter extends Fleet implements Fleet.WithDynamicShips {
             Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
             Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class });
             InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname });
+            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
+            ShipDescription sd = new ShipDescription(shipname, br);
+            ret.setShipDescription(sd);
             ships.put(shipname, ret);
             return ret;
         } catch (Exception e) {
index 88a8815..4ee078b 100644 (file)
@@ -1,4 +1,5 @@
 package edu.berkeley.fleet.interpreter;
+import edu.berkeley.fleet.doc.*;
 import edu.berkeley.fleet.api.*;
 import edu.berkeley.fleet.api.BenkoBox;
 import java.util.*;
@@ -10,7 +11,15 @@ public abstract class InterpreterBenkoBox extends BenkoBox {
     private final InterpreterShip ship;
     private final Destination[] ports;
     private final int addr = max_addr++;
-        
+    private  BenkoBoxDescription bbd;
+
+    public void setDescription(BenkoBoxDescription bbd) {
+        this.bbd = bbd;
+    }
+    public long resolveLiteral(String literal) {
+        return bbd.resolveLiteral(literal);
+    }
+    
     public InterpreterBenkoBox(InterpreterShip ship, String name, String[] ports) {
         this.ship = ship;
         this.name = name;
index 70bbd80..6bfe2b2 100644 (file)
@@ -1,7 +1,6 @@
 package edu.berkeley.fleet.interpreter;
 import edu.berkeley.fleet.api.*;
-
-import edu.berkeley.fleet.api.*;
+import edu.berkeley.fleet.doc.*;
 import java.util.*;
 import java.io.*;
 
@@ -9,7 +8,10 @@ import java.io.*;
 abstract class InterpreterShip extends Ship {
         
     /** You should instantiate a bunch of Inboxes and Outboxes in your constructor */
-    public InterpreterShip(Interpreter fleet, String name, String type) { this.fleet = fleet; this.type = type; }
+    public InterpreterShip(Interpreter fleet, String name, String type) {
+        this.fleet = fleet;
+        this.type = type;
+    }
 
     private Interpreter  fleet;
     private String       type;
@@ -20,6 +22,15 @@ abstract class InterpreterShip extends Ship {
     public Fleet              getFleet()       { return fleet; }
     public Interpreter        getInterpreter() { return fleet; }
 
+    public void setShipDescription(ShipDescription sd) {
+        for(InterpreterBenkoBox ibb : ports.values()) {
+            for(BenkoBoxDescription bbd : sd) {
+                if (bbd.getName().equals(ibb.getName()))
+                    ibb.setDescription(bbd);
+            }
+        }
+    }
+
     /**
      *  Override this method, check inboxes for the data you need, and
      *  if you find it, deposit results in an outbox; we'll take care
index 3d83990..0da0052 100644 (file)
@@ -54,7 +54,7 @@
 
 2:         sendto alu.in1;
 4:         sendto alu.in2;
-Alu2.ADD:  sendto alu.inOp;
+Alu2.inOp[ADD]:  sendto alu.inOp;
 alu.in1:   [*] nop;
            (*) take, deliver;
            (*) deliver;