aboutsummaryrefslogtreecommitdiff
path: root/src/main/stanza/flo.stanza
blob: fd000e20f99a70d0ab9641d0d13069f0df182c02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
defpackage firrtl/flo :
   import core
   import verse
   import firrtl/ir-utils
   import firrtl/ir2
   import firrtl/passes

;========== Pad Widths ==================

public defstruct Pad <: Pass
public defmethod pass (b:Pad) -> (Circuit -> Circuit) : pad-widths
public defmethod name (b:Pad) -> String : "Pad Widths"

defn int-width! (t:Type) -> Int :
   match(width!(t)) :
      (w:IntWidth) : width(w)
      (w) : error("Non-int width")

defn set-width (desired:Int,t:Type) -> Type :
   match(t) :
      (t:UIntType) : UIntType(IntWidth(desired))
      (t:SIntType) : SIntType(IntWidth(desired))
      (t) : error("Non-ground type")

defn pad-widths-e (desired:Int,e:Expression) -> Expression :
   defn trim (desired:Int, e:Expression) :
     ;; println-all(["TRIM " desired " e " e])
     DoPrim(BITS-SELECT-OP,list(e),list(desired - 1, 0),set-width(desired,type(e)))
   defn pad (desired:Int, e:Expression) :
     ;; println-all(["PAD " desired " e " e])
     DoPrim(PAD-OP,list(e),list(desired),set-width(desired,type(e)))
   defn trim-pad (desired:Int, e:Expression) :
     val i = int-width!(type(e))
     if i > desired : trim(desired, e)
     else if i == desired : e
     else : pad(desired, e)
   defn self-pad-widths-e (e:Expression) -> Expression :
     pad-widths-e(int-width!(type(e)), e)
   ;; println-all(["PAD-E " desired " " e])
   match(e) :
      (e:DoPrim) :
         val new-desired = reduce(max, 0, map(int-width!{type(_)}, args(e)))
         ;; println-all(["  NEW DESIRED " new-desired])
         val e* =
           if contains?([CONCAT-OP, DYN-SHIFT-RIGHT-OP, DYN-SHIFT-LEFT-OP], op(e)) :
             DoPrim(op(e), map(self-pad-widths-e, args(e)), consts(e), type(e))
           else if contains?([MUX-OP], op(e)) :
             DoPrim(op(e), list(pad-widths-e(1, args(e)[0]), pad-widths-e(new-desired, args(e)[1]), pad-widths-e(new-desired, args(e)[2])), consts(e), type(e))
           else :
             map(pad-widths-e{new-desired,_},e)
         trim-pad(desired, e*)
      (e:WRef|WSubfield|WIndex) : 
         trim-pad(desired, e)
      (e:UIntValue) : 
         val i = int-width!(type(e))
         if i > desired : trim(desired, e)
         else : UIntValue(value(e),IntWidth(desired))
      (e:SIntValue) : 
         val i = int-width!(type(e))
         if i > desired : trim(desired, e)
         else : SIntValue(value(e),IntWidth(desired))
      (e:Register) :
         val value* = pad-widths-e(desired, value(e))
         Register(type(value*), value*, pad-widths-e(1, enable(e)))
      (e:ReadPort) :
         if int-width!(type(e)) != desired : error("ReadPort has different width than desired")
         else : ReadPort(mem(e), self-pad-widths-e(index(e)), type(e), pad-widths-e(1, enable(e)))
      (e:WritePort) :
         if int-width!(type(e)) != desired : error("WritePort has different width than desired")
         else : WritePort(mem(e), self-pad-widths-e(index(e)), type(e), pad-widths-e(1, enable(e)))
      (e) : error(to-string $ e)

defn pad-widths-s (s:Stmt) -> Stmt :
   ;; println-all(["PAD-S " s])
   match(map(pad-widths-s,s)) :
      (s:Connect) : 
         val i = int-width!(type(loc(s)))
         val loc* = pad-widths-e(i,loc(s))
         val exp* = pad-widths-e(i,exp(s))
         Connect(info(s),loc*,exp*)
      (s:DefNode) : 
         val i = int-width!(type(value(s)))
         val exp* = pad-widths-e(i,value(s))
         DefNode(info(s),name(s),exp*)
      (s) :
         s

public defn pad-widths (c:Circuit) -> Circuit :
   Circuit{info(c),_,main(c)} $
      for m in modules(c) map :
         match(m) :
            (m:ExModule) : m
            (m:InModule) : InModule(info(m),name(m),ports(m),pad-widths-s(body(m)))

;============= Flo Backend ================

public defstruct Flo <: Pass :
   file : String
public defmethod pass (b:Flo) -> (Circuit -> Circuit) : emit-flo{file(b),_}
public defmethod name (b:Flo) -> String : "To Flo"
  
defn is-sint? (arg:Expression) -> True|False : type(arg) typeof SIntType

defn flo-op-name (op:PrimOp, args:List<Expression>) -> String :
   switch {op == _ } :
      ADD-OP :             "add"
      ADD-WRAP-OP :        "add"
      SUB-OP :             "sub"
      SUB-WRAP-OP :        "sub"
      MUL-OP :             "mul" ;; todo: signed version
      DIV-OP :             "div" ;; todo: signed version
      MOD-OP :             "mod" ;; todo: signed version
      QUO-OP :             "div" ;; todo: signed version
      REM-OP :             "mod" ;; todo: signed version
      LESS-OP :            "lt"  ;; todo: signed version
      LESS-EQ-OP :         "lte" ;; todo: swap args
      GREATER-OP :         "lt"  ;; todo: swap args
      GREATER-EQ-OP :      "lte" ;; todo: signed version
      NEQUAL-OP :          "neq"
      EQUAL-OP :           "eq"
      MUX-OP :             "mux"
      NEG-OP :             "neg"
      AS-UINT-OP :         "mov"
      AS-SINT-OP :         "mov"
      SHIFT-LEFT-OP :      "lsh" 
      SHIFT-RIGHT-OP :     if is-sint?(args[0]): "arsh" else: "rsh"
      DYN-SHIFT-LEFT-OP :  "lsh" 
      DYN-SHIFT-RIGHT-OP : if is-sint?(args[0]): "arsh" else: "rsh"
      PAD-OP :             if is-sint?(args[0]): "arsh" else: "rsh"
      CONVERT-OP :         if is-sint?(args[0]): "arsh" else: "rsh"
      BIT-AND-OP :         "and"
      BIT-NOT-OP :         "not"
      BIT-OR-OP :          "or"
      BIT-XOR-OP :         "xor"
      CONCAT-OP :          "cat"
      BIT-SELECT-OP :      "rsh"
      BITS-SELECT-OP :     "rsh"
      BIT-XOR-REDUCE-OP :  "xorr"
      else :
        error $ string-join $ ["Unable to print Primop: " op]

defn sane-width (wd:Width) -> Int :
  match(wd) :
    (w:IntWidth) : max(1, width(w))
    (w)          : error(string-join(["Unknown width: " w]))

defn prim-width (type:Type) -> Int :
   match(type) :
      (t:UIntType) : sane-width(width(t))
      (t:SIntType) : sane-width(width(t))
      (t) :          error("Bad prim width type")

defn sizeof (in: Int) -> Int :
  max(1, ceil-log2(in + 1))

defn emit-all (es:Streamable, top:Symbol) :
  for e in es do :
     match(e) :
       (ex:Expression) : emit!(ex,top)
       (ex:String) :     print(ex)
       (ex:Symbol) :     print(ex)
       ;; (ex:Int) :        print-all([ex "'" sizeof(ex)])
       (ex:Int) :        print(ex)
       (ex) :            print(ex)

defn emit! (e:Expression,top:Symbol) :
   defn greater-op? (op: PrimOp) -> True|False :
      contains?([GREATER-OP], op)
   defn greater-eq-op? (op: PrimOp) -> True|False :
      contains?([GREATER-EQ-OP], op)     
   defn less-eq-op? (op: PrimOp) -> True|False :
      contains?([LESS-EQ-OP], op)
   defn less-op? (op: PrimOp) -> True|False :
      contains?([LESS-OP], op)
   defn cmp-op? (op: PrimOp) -> True|False :
      greater-op?(op) or greater-eq-op?(op) or less-op?(op) or less-eq-op?(op) or 
      contains?([EQUAL-OP NEQUAL-OP] op)
   match(e) :
      (e:Ref) :       emit-all([top "::" name(e)], top)
      (e:UIntValue) : emit-all([value(e) "'" sane-width(width(e))], top)
      (e:SIntValue) : emit-all([value(e) "'" sane-width(width(e))], top)
      (e:Subfield) :  emit-all([exp(e) "/" name(e)], top)
      (e:Index) :     emit-all([exp(e) "/" value(e)], top)
      ;(e:Pad) : 
         ;emit-all(["rsh'" prim-width(type(e)) " " value(e) " 0"], top)
      (e:Register) : 
         emit-all(["reg'" prim-width(type(e)) " " enable(e) " " value(e)], top) 
      (e:ReadPort) : 
         emit-all(["rd'" prim-width(type(e)) " " "1" " " mem(e) " " index(e)], top) ;; enable(e)
      (e:DoPrim) : 
         if cmp-op?(op(e)) :
           emit-all([flo-op-name(op(e), args(e)) "'" prim-width(type(args(e)[0]))], top)
           if greater-op?(op(e)) or greater-eq-op?(op(e)) :
             emit-all([" " args(e)[1] " " args(e)[0]], top)
           else :
             emit-all([" " args(e)[0] " " args(e)[1]], top)
         else if op(e) == BIT-SELECT-OP :
           emit-all([flo-op-name(op(e), args(e)) "'1 " args(e)[0] " " consts(e)[0]], top)
         else if op(e) == BITS-SELECT-OP :
           val w = consts(e)[0] - consts(e)[1] + 1
           emit-all([flo-op-name(op(e), args(e)) "'" w " " args(e)[0] " " consts(e)[1]], top)
         else if op(e) == CONCAT-OP :
           val w = prim-width(type(args(e)[1]))
           emit-all([flo-op-name(op(e), args(e)) "'" w " " args(e)[0] " " args(e)[1]], top)
         else if op(e) == PAD-OP or op(e) == CONVERT-OP :
           emit-all([flo-op-name(op(e), args(e)) "'" prim-width(type(e)) " " args(e)[0] " 0"], top)
         else :
           emit-all([flo-op-name(op(e), args(e)) "'" prim-width(type(e))], top)
           for arg in args(e) do :
             print(" ")
             emit!(arg, top)
           for const in consts(e) do :
             print-all([" " const "'" sizeof(const)])
      (e) : error("SHOULDN'T EMIT THIS") ;; print-all(["EMIT(" e ")"])
      ;(e) : emit-all(["mov'" prim-width(type(e)) " " e], top) ;TODO, not sure which one is right

defn maybe-mov (e:Expression) -> String :
   val need-mov? = match(e) :
      (e:Ref) :       true
      (e:UIntValue) : true
      (e:SIntValue) : true
      (e:Subfield) :  true
      (e:Index) :     true
      (e) :           false
   if need-mov?: "mov " else: ""

defn emit-s (s:Stmt, v:List<Symbol>, top:Symbol,sh:HashTable<Symbol,Int>) :
   match(s) :
      (s:DefWire) : ""
      (s:DefInstance) : error("Shouldn't be here")
      (s:DefMemory) :
         val vtype = type(s) as VectorType
         emit-all([top "::" name(s) " = mem'" prim-width(type(vtype)) " " size(vtype) "\n"], top)
      (s:DefNode) :
         emit-all([top "::" name(s) " = " maybe-mov(value(s)) value(s) "\n"], top)
      (s:Begin) : do(emit-s{_, v, top,sh}, body(s))
      (s:Connect) : 
         match(loc(s)) :
           (r:Ref) :
             val n = name(r)
             if contains?(v,n) :
                emit-all([top "::" n " = out'" prim-width(type(r)) " " exp(s) "\n"], top)
             else :
                emit-all([top "::" n " = " maybe-mov(exp(s)) exp(s) "\n"], top)
           (w:WritePort) :
             val n = firrtl-gensym(`F,sh)
             emit-all([top "::" n " = wr'" prim-width(type(w)) " " enable(w) " " mem(w) " " index(w) " " exp(s) "\n"], top)
           (o) :
             println-all(["CONNEcT LOC " loc(s)])
             error("Unknown Connect")
      (s) : s 

defn emit-module (m:InModule,sh:HashTable<Symbol,Int>) :
   val v = Vector<Symbol>()
   for port in ports(m) do :
      if name(port) ==`reset :
         emit-all([name(m) "::" name(port) " = rst'1\n"], name(m))
      else : switch {_ == direction(port)} :
         INPUT : print-all([name(m) "::" name(port) " = " "in'" prim-width(type(port)) "\n"])
         OUTPUT : add(v,name(port))
   emit-s(body(m), to-list(v), name(m),sh)

public defn emit-flo (file:String, c:Circuit) :
   with-output-file{file, _} $ fn () :
      emit-module(modules(c)[0] as InModule,get-sym-hash(modules(c)[0] as InModule))
      false
   c