eliminate ShipDescription.Constant class, use BitVector instead
authormegacz <adam@megacz.com>
Sat, 14 Mar 2009 23:38:21 +0000 (16:38 -0700)
committermegacz <adam@megacz.com>
Sat, 14 Mar 2009 23:38:21 +0000 (16:38 -0700)
src/edu/berkeley/fleet/Main.java
src/edu/berkeley/fleet/api/Ship.java
src/edu/berkeley/fleet/assembler/Parser.java
src/edu/berkeley/fleet/fpga/Fpga.java
src/edu/berkeley/fleet/interpreter/Interpreter.java
src/edu/berkeley/fleet/marina/MarinaDock.java
src/edu/berkeley/fleet/two/DockDescription.java
src/edu/berkeley/fleet/two/FleetTwoDock.java
src/edu/berkeley/fleet/two/FleetTwoFleet.java
src/edu/berkeley/fleet/two/ShipDescription.java

index ff07e9a..f8e5956 100644 (file)
@@ -61,9 +61,9 @@ public class Main {
             if (name.endsWith(".ship"))
                 name = name.substring(0, name.length() - ".ship".length());
             if (fleet instanceof edu.berkeley.fleet.fpga.Fpga) {
-                ((edu.berkeley.fleet.fpga.Fpga)fleet).expand(new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
+                ((edu.berkeley.fleet.fpga.Fpga)fleet).expand(new ShipDescription(fleet, name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
             } else {
-                ((Interpreter)fleet).expand(new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
+                ((Interpreter)fleet).expand(new ShipDescription(fleet, name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
             }
 
         } else if (command.equals("doc")) {
@@ -74,7 +74,7 @@ public class Main {
             for(String ss = br.readLine(); ss!=null; ss = br.readLine())
                 pw.println(ss);
             for(String f : new File("ships").list()) {
-                new ShipDescription(f, new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))).printTeX(pw);
+                new ShipDescription(fleet, f, new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))).printTeX(pw);
             }
             pw.println("\\end{document}");
             pw.close();
@@ -155,7 +155,7 @@ public class Main {
         } else if (f.getPath().endsWith(".fleet") || f.getPath().endsWith(".test")) {
             runTest(fleet, new InputStreamReader(new FileInputStream(f)), f.getPath());
         } else if (f.getPath().endsWith(".ship")) {
-            ShipDescription sd = new ShipDescription(f.getName(), new BufferedReader(new InputStreamReader(new FileInputStream(f))));
+            ShipDescription sd = new ShipDescription(fleet, f.getName(), new BufferedReader(new InputStreamReader(new FileInputStream(f))));
             String testsection = sd.getSection("test");
             if (testsection == null)
                 System.out.println("no test for " + sd.getName() + "!");
index f5eafa4..0b7945d 100644 (file)
@@ -26,7 +26,7 @@ public abstract class Ship implements Iterable<Dock> {
 
     /** get a constant associated with a ship; returns null if none found */
     public BitVector getConstant(String constantName) {
-        return null;
+        throw new RuntimeException("unknown constant \""+constantName+"\" on ship " + this);
     }
 
     public String toString() {
index e43b035..78049db 100644 (file)
@@ -291,7 +291,7 @@ public class Parser {
         }
     }
 
-    private long parseSSL(Tree t) {
+    private BitVector parseSSL(Tree t) {
         String shipType = name(t.child(0));
         String portName = name(t.child(1));
         Ship chosenship = null;
@@ -305,14 +305,12 @@ public class Parser {
         Dock chosenport = chosenship.getDock(portName);
         Tree specs = t.child(2);
         long literal = 0;
-        for(int i=0; i<specs.size(); i++) {
-            Tree tt = specs.child(i);
-            literal |= resolveLiteral(chosenport, stringBody(tt));
-        }
-        return literal;
+        if (specs.size() != 1) throw new RuntimeException("multiple constants not supported");
+        Tree tt = specs.child(0);
+        return resolveLiteral(chosenport, stringBody(tt));
     }
 
-    private static long resolveLiteral(Dock dd, String s) {
+    private static BitVector resolveLiteral(Dock dd, String s) {
         long val = 0;
         long ret = 0;
         boolean hasval = false;
@@ -321,13 +319,7 @@ public class Parser {
             s = s.substring(0, s.indexOf('='));
             hasval = true;
         }
-        ShipDescription.Constant c = ((FleetTwoDock)dd).getDockConstant(s);
-        if (c==null) throw new RuntimeException("no constant " + s + " on dock " + dd);
-        ret |= c.setbits;
-        ret &= ~c.clearbits;
-        if (hasval)
-            ret |= ((~(0xffffffffffffffffL << c.numberWidth)) & val) << c.numberOffset;
-        return ret;
+        return ((FleetTwoDock)dd).getDockConstant(s);
     }
 
     private static FlagFunction parseFlags(Tree<String> t) {
@@ -417,7 +409,7 @@ public class Parser {
                     cb.add(new Abort(dock, predicate));
                     continue;
                 } else if ("word".equals(tt.head())) {
-                    long literal = 0;
+                    BitVector literal = null;
                     if (tt.child(0).head().equals("CodeBagBody")) {
                         Tree<String> tq = tt;
                         CodeBag cb2 = getCodeBag("anon"+(anoncount++));
@@ -433,28 +425,22 @@ public class Parser {
                     } else if (tt.child(0).head().equals("[")) {
                         literal = parseSSL(tt.child(0));
                     } else {
-                        literal = number(tt.child(0));
+                        literal = new BitVector(dock.getShip().getFleet().getWordWidth()).set(number(tt.child(0)));
                     }
                     count = 1;
-                    /*
-                    if ("int".equals(tt.child(1).head())) {
-                        count = (int)number(tt.child(1));
-                    } else if ("forever".equals(tt.child(1).head())) {
-                        count = 0;
-                    }
-                    */
-
 
                     if (((FleetTwoFleet)fleet).isSmallEnoughToFit(literal)) {
-                        cb.add(new Set(dock, predicate, SetDest.DataLatch, (literal)));
+                        cb.add(new Set(dock, predicate, SetDest.DataLatch, literal.toLong()));
                     } else {
                         int counter = 0;
                         while(counter < dock.getShip().getFleet().getWordWidth()) counter += fleet.getShiftWidth();
-                        while(counter > 0) {
-                            cb.add(new Shift(dock, predicate,
-                                             new BitVector(dock.getShip().getFleet().getShiftWidth())
-                                             .set(getField(counter-1, counter-fleet.getShiftWidth(), literal))));
+                        while(true) {
                             counter -= fleet.getShiftWidth();
+                            if (counter < 0) break;
+                            BitVector bv = new BitVector(dock.getShip().getFleet().getShiftWidth());
+                            for(int i=0; i<fleet.getShiftWidth(); i++)
+                                bv.set(i, literal.get(counter+i));
+                            cb.add(new Shift(dock, predicate, bv));
                         }
                     }
                         
index f73e9b1..eaa559c 100644 (file)
@@ -64,7 +64,7 @@ public class Fpga extends FleetTwoFleet {
     // Setup //////////////////////////////////////////////////////////////////////////////
 
     Ship createShip(String type) throws IOException {
-        ShipDescription sd = new ShipDescription(type, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
+        ShipDescription sd = new ShipDescription(this, type, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
         int count = 0;
         for(Ship ship : ships.values()) if (ship.getType().equals(type)) count++;
         String name = type+count;
@@ -78,8 +78,8 @@ public class Fpga extends FleetTwoFleet {
         this.top = top;
         debugShip = createShip("Debug");
 
-        boolean small = true;
-        //boolean small = false;
+        //boolean small = true;
+        boolean small = false;
 
         if (small) {
             for(int i=0; i<2; i++) createShip("Alu");
@@ -118,7 +118,7 @@ public class Fpga extends FleetTwoFleet {
             createShip("Timer");
             createShip("DDR2");
             createShip("Dvi");
-            createShip("ZBT");
+            //createShip("ZBT");
         }
 
         // for FifoShip
index 063e15b..e58eb3d 100644 (file)
@@ -80,7 +80,7 @@ public class Interpreter extends FleetTwoFleet {
             String src = "/ships/" + shipType + ".ship";
             InputStream is = getClass().getResourceAsStream(src);
             BufferedReader br = new BufferedReader(new InputStreamReader(is));
-            ShipDescription sd = new ShipDescription(shipType, br);
+            ShipDescription sd = new ShipDescription(this, shipType, br);
             InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
             ships.put(shipname, ret);
             if (shipType.equals("Debug") && debugShip == null)
index 05a301a..059a420 100644 (file)
@@ -20,7 +20,9 @@ public class MarinaDock extends FleetTwoDock {
     }
 
     public String getName() { return "fakedock"; }
-    public ShipDescription.Constant getDockConstant(String s) { return null; }
+    public BitVector getDockConstant(String s) {
+        throw new RuntimeException("not supported");
+    }
 
 }
 
index eccbe6e..d3646b8 100644 (file)
@@ -11,7 +11,7 @@ public class DockDescription {
     private final boolean inbox;
     private final boolean left;
     private final boolean isDockless;
-    private final HashMap<String,ShipDescription.Constant> constants = new HashMap<String,ShipDescription.Constant>();
+    private final HashMap<String,BitVector> constants = new HashMap<String,BitVector>();
 
     DockDescription(ShipDescription ship, String name, boolean left, boolean inbox) {
         this(ship, name, left, inbox, false);
@@ -33,11 +33,11 @@ public class DockDescription {
     boolean isLeft() { return left; }
 
     /** Searches the dock-specific constants first, then ship-wide constants */
-    public ShipDescription.Constant getConstant(String name) {
-        ShipDescription.Constant ret = constants.get(name);
+    public BitVector getConstant(String name) {
+        BitVector ret = constants.get(name);
         if (ret == null) ret = ship.getConstant(name);
         return ret;
     }
 
-    void addConstant(String s, ShipDescription.Constant c) { constants.put(s, c); }
+    void addConstant(String s, BitVector c) { constants.put(s, c); }
 }
index 3990c1d..2868768 100644 (file)
@@ -14,7 +14,7 @@ public abstract class FleetTwoDock extends Dock {
 
     public String getName() { return dockDescription.getName(); }
 
-    public ShipDescription.Constant getDockConstant(String s) { return dockDescription.getConstant(s); }
+    public BitVector getDockConstant(String s) { return dockDescription.getConstant(s); }
 
     public boolean isInputDock() { return dockDescription.isInputDock(); }
 
index 87dcc15..19d5e9b 100644 (file)
@@ -76,7 +76,7 @@ public abstract class FleetTwoFleet extends Fleet {
     public final int  WIDTH_WORD;
     public final int  WIDTH_PACKET;
 
-    public final long DataLatch_WIDTH;
+    public final int DataLatch_WIDTH;
     private final long mask;
 
     public FleetTwoFleet() { this(false); }
@@ -218,6 +218,13 @@ public abstract class FleetTwoFleet extends Fleet {
         return false;
     }
 
+    public boolean isSmallEnoughToFit(BitVector immediate) {
+        boolean b = immediate.get((int)DataLatch_WIDTH);
+        for(int i=DataLatch_WIDTH+1; i<immediate.length(); i++)
+            if (immediate.get(i) != b) return false;
+        return true;
+    }
+
     public int getWordWidth() { return 37; }
 
     //////////////////////////////////////////////////////////////////////////////
index 1aa801b..182eed7 100644 (file)
@@ -6,11 +6,12 @@ import java.util.*;
 /** NOT YET FINALIZED: A description (specification) of a ship */
 public class ShipDescription implements Iterable<DockDescription> {
 
+    private Fleet fleet;
     private String name;
     private LinkedHashMap<String,DockDescription> docks         = new LinkedHashMap<String,DockDescription>();
     private LinkedHashMap<String,DockDescription> ports = new LinkedHashMap<String,DockDescription>();
     private HashMap<String,String>                sections      = new HashMap<String,String>();
-    private HashMap<String,Constant>              constants     = new HashMap<String,Constant>();
+    private HashMap<String,BitVector>              constants     = new HashMap<String,BitVector>();
 
     public String getName() { return name; }
     public String getSection(String sectionName) { return sections.get(sectionName); }
@@ -22,9 +23,10 @@ public class ShipDescription implements Iterable<DockDescription> {
 
     public final LinkedList<PercolatedPort> percolatedPorts = new LinkedList<PercolatedPort>();
 
-    public ShipDescription(String name, BufferedReader r) throws IOException {
+    public ShipDescription(Fleet fleet, String name, BufferedReader r) throws IOException {
         if (name.endsWith(".ship")) name = name.substring(0, name.length()-".ship".length());
         this.name = name;
+        this.fleet = fleet;
         String sectionName = null;
         StringBuffer sb = new StringBuffer();
         while(true) {
@@ -47,8 +49,11 @@ public class ShipDescription implements Iterable<DockDescription> {
             processSection(s);
     }
 
-    public Constant getConstant(String name) {
-        return constants.get(name);
+    public BitVector getConstant(String name) {
+        BitVector c = constants.get(name);
+        if (c==null)
+            throw new RuntimeException("unknown constant " + name);
+        return c;
     }
 
     private void processSection(String section) throws IOException {
@@ -69,7 +74,7 @@ public class ShipDescription implements Iterable<DockDescription> {
                 if (key.startsWith("constant")) {
                     String constname = key.substring("constant".length()+1).trim();
                     String val       = s.substring(s.indexOf(':')+1).trim();
-                    constants.put(constname, new Constant(val));
+                    constants.put(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
                 }
             }
         } else if (section.equals("ports")) {
@@ -104,7 +109,7 @@ public class ShipDescription implements Iterable<DockDescription> {
                 else if (key.startsWith("constant")) {
                     String constname = key.substring("constant".length()+1).trim();
                     String val       = s.substring(s.indexOf(':')+1).trim();
-                    p.addConstant(constname, new Constant(val));
+                    p.addConstant(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
                     continue;
                 } else if (key.startsWith("shortcut to")) {
                     continue;
@@ -203,33 +208,4 @@ public class ShipDescription implements Iterable<DockDescription> {
             pw.println(tex);
     }
 
-    // FIXME: merge with BitMask
-    public class Constant {
-        public long    setbits   = 0;
-        public long    clearbits = 0;
-        public boolean signExtend = false;
-        public int     numberOffset = 0;
-        public int     numberWidth = 0;
-        public Constant(String s) {
-            if (s.startsWith("0x")) {
-                setbits = Long.parseLong(s.substring(2), 16);
-                clearbits = ~setbits;
-            } else if (s.length() == 37) {
-                for(int i=36; i>=0; i--) {
-                    char c = s.charAt(36-i);
-                    switch(c) {
-                        case '0': clearbits |= (1<<i); break;
-                        case '1': setbits   |= (1<<i); break;
-                        case '.': break;
-                        case 's': signExtend = true;  numberOffset = i; numberWidth++; break;
-                        case 'u': signExtend = false; numberOffset = i; numberWidth++; break;
-                    }
-                }
-            } else {
-                setbits = Long.parseLong(s);
-                clearbits = ~setbits;
-            }
-        }
-    }
-
 }