move Gadgets to MemoryUtils, discard a ton of obsolete junk
[fleet.git] / src / edu / berkeley / fleet / ir / Sheets.java
1 package edu.berkeley.fleet.ir;
2 import edu.berkeley.fleet.loops.*;
3
4 // ScanRows+Fix: HARD!
5 /**
6  *  How do we know when a stream is done?  In the case of Equalize?
7  */
8 public class Sheets {
9
10     public static final int NUM_LANES = 3;
11     public static final int FIFO_SHIP_CAPACITY = 8;
12
13     /**
14      *  A block is a set of statements which may be dispatched
15      *  concurrently; no memory may be both read from and written to
16      *  within a Block, each block may have at most one ReadBack, and
17      *  each Block will completely finish before starting the next
18      *  Block.
19      */
20     public static class Block {
21
22         private int var_idx = 0;
23
24         public Statement[] statements;
25         public String toString() {
26             StringBuffer sb = new StringBuffer();
27             sb.append("block {\n");
28             for(int i=0; i<statements.length; i++) {
29                 sb.append("  ");
30                 sb.append(statements[i].toString());
31                 sb.append('\n');
32             }
33             sb.append("}\n");
34             return sb.toString();
35         }
36         
37         /**
38          *  Each variable is single-assignment, single-use.  When
39          *  compiling, each Var maps to an outbox at which the values
40          *  in question will be made available.
41          */
42         public class Var {
43             private Statement assigner;
44             private Statement user;
45             public final int lane;
46             public final int idx;
47             public Var() { this(0); }
48             public Var(int lane) { this.lane = lane; this.idx = var_idx++; }
49             public String toString() { return "v"+idx; }
50             public Statement getAssigner() { return assigner; }
51             public void setAssigner(Statement assigner) {
52                 if (this.assigner!=null) throw new RuntimeException();
53                 this.assigner = assigner;
54             }
55             public Statement getUser() { return user; }
56             public void setUser(Statement user) {
57                 if (this.user!=null) throw new RuntimeException();
58                 this.user = user;
59             }
60         }
61
62         // Statements //////////////////////////////////////////////////////////////////////////////
63
64         public class Statement {
65         }
66
67         public class Literal extends Statement {
68             Var dest;
69             long[] vals;
70             public Literal(Var dest, long[] vals) {
71                 this.dest = dest;
72                 this.vals = vals;
73                 dest.setAssigner(this);
74             }
75             /*
76             public void emitSetupInstructions() {
77                 if (vals.length > FIFO_SHIP_CAPACITY) throw new RuntimeException();
78                 Ship fifo = allocateShip("Fifo");
79                 Dock fifo_in = fifo.getDock("in");
80                 for(int i=0; i<vals.length; i++) {
81
82                 }
83                 dest.setOutbox(fifo.getDock("out"));
84             }
85             public void emitTeardownInstructions() {
86
87             }
88             */
89         }
90
91         public class ReadBack extends Statement {
92             Var values;
93             public ReadBack(Var values) {
94                 this.values = values;
95                 values.setUser(this);
96             }
97         }
98
99         public class Fanout extends Statement {
100             public final Var[] dests;
101             public final Var   source;
102             public Fanout(Var[] dests, Var source) {
103                 this.dests = dests;
104                 this.source = source;
105             }
106         }
107
108         /** mainly used to relocate a value from one lane to another */
109         public class Move extends Statement {
110             public Move(Var dest, Var source) {
111             }
112         }
113
114         /**
115          *  Repeat the first item from val enough times to match the
116          *  length of mimic.  [val] MUST be a single-element stream.
117          */
118         public class Equalize extends Statement {
119             public Equalize(Var dest, Var val, Var mimic) { }
120         }
121
122         // technically we could "fan out" tokens instead of fanning out counts, but...
123         /** Like VRep, but for the single-column case */
124         public class Replicate extends Statement {
125             public Replicate(Var dest, Var counts, Var vals) { }
126         }
127
128         public class Alu extends Statement {
129             public Alu(Var dest, Var opcodes, Var in1, Var in2) { }
130         }
131
132         public class Lut extends Statement {
133             public Lut(Var dest, Var table, Var in1, Var in2) { }
134         }
135
136         public class ReadMem extends Statement {
137             public ReadMem(Var dest, long whichMem, Var addresses) { }
138         }
139
140         public class WriteMem extends Statement {
141             public WriteMem(long whichMem, Var addresses, Var values) { }
142         }
143
144     }
145
146
147 }