Skip to content

Commit

Permalink
Updated tests, and included a check for the name of the pass, which
Browse files Browse the repository at this point in the history
allows the compiler to print after each pass to ease debugging
  • Loading branch information
azidar committed Feb 24, 2015
1 parent 641af82 commit 2353d64
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 27 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ src/lib/stanzam
src/*/__MACOSX
src/main/stanza/firrtl-main
utils/bin/firrtl
test/unit/Output
test/unit/*.out
test/passes/Output
test/passes/*/Output
test/passes/*.out
test/passes/*/*.out
spec/spec.aux
spec/spec.log
spec/spec.pdf
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ Secondary TODO:

Install lit:
pip install lit


Stanza Installation:
git clone https://github.com/ucb-bar/firrtl
cd src/lib
unzip stanzam.zip
cd stanzam
sudo ./stanzam -platform os-x -install /usr/local/bin/stanzam





4 changes: 4 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ Make stanza a git repo
Write installation instructions for stanza
Need 2 different prints, one with IR-internal information, and another that matches correct FIRRTL

Tests:
Error if declare anything other than module in circuit
Error if incorrectly assign stuff, like use = instead of :=

10 changes: 10 additions & 0 deletions notes/notes.02.23.15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TODO: rename ! to *.

when declare a var, give it a type because compiler assumes it is ?

if costantantly adding to end of a list, should use a vector type instead of a list, because add is much faster than append (depends on length of left operand)

TODO: test case
inst a of mywire


64 changes: 40 additions & 24 deletions src/main/stanza/passes.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ defmethod print (o:OutputStream, k:Kind) :
(k:StructuralMemKind) : "smem:"

defmethod print (o:OutputStream, e:WRef) :
print-all(o,[kind(e) name(e)])
match(type(e)) :
(t:UnknownType) :
print-all(o,[kind(e) name(e)])
(t) :
print-all(o,[kind(e) name(e) ":" type(e)])

defmethod print (o:OutputStream, e:WField) :
print-all(o,[exp(e) "." name(e)])
Expand Down Expand Up @@ -212,7 +216,8 @@ defn make-explicit-reset (c:Circuit) :
; This pass replaces the reg.init construct by creating a new
; wire that holds the value at initialization. This wire
; is then connected to the register conditionally on reset,
; at the end of that register's scope.
; at the end of the scope containing the register
; declaration
; If a register has no inital value, the wire is connected to
; a NULL node. Later passes will remove these with the base
; case Mux(reset,NULL,a) -> a, and Mux(reset,a,NULL) -> a.
Expand All @@ -221,15 +226,19 @@ defn make-explicit-reset (c:Circuit) :

defn initialize-registers (c:Circuit) :
defn add-when (s:Stmt,renames:HashTable<Symbol,Symbol>) -> Stmt :
Begin(list(s,when-reset)) where :
var inits = List<Stmt>()
for kv in renames do :
val refreg = WRef(key(kv),UnknownType(),RegKind(),UNKNOWN-DIR)
val refwire = WRef(value(kv),UnknownType(),NodeKind(),UNKNOWN-DIR)
val connect = Connect(refreg,refwire)
inits = append(inits,list(connect))
var inits = List<Stmt>()
for kv in renames do :
val refreg = WRef(key(kv),UnknownType(),RegKind(),UNKNOWN-DIR)
val refwire = WRef(value(kv),UnknownType(),NodeKind(),UNKNOWN-DIR)
val connect = Connect(refreg,refwire)
inits = append(inits,list(connect))
if empty?(inits) :
s
else :
val pred = WRef(`reset, UnknownType(), PortKind(), UNKNOWN-DIR)
val when-reset = Conditionally(pred,Begin(inits),Begin(List<Stmt>()))
Begin(list(s,when-reset))

defn rename (s:Stmt,l:HashTable<Symbol, Symbol>) -> Stmt :
defn rename-stmt (s:Stmt) -> Stmt :
map{rename-expr,_} $
Expand Down Expand Up @@ -268,38 +277,43 @@ defn initialize-registers (c:Circuit) :
[Begin(body!),renames] where :
val defreg = s
val defwire = DefWire(wire-name,type(s))
val conwire = Connect(WRef(wire-name,type(s),NodeKind(),UNKNOWN-DIR),Null())
val conwire = Connect(WRef(wire-name,UnknownType(),NodeKind(),UNKNOWN-DIR),Null())
val body! = list(defreg,defwire,conwire)
(s:Conditionally) :
[Conditionally(pred(s),initialize-scope(conseq(s)),initialize-scope(alt(s))),empty-hash]
(s:LetRec) :
[LetRec(entries(s),initialize-scope(body(s))),empty-hash]
;TODO Add Letrec
(s) : [s,empty-hash]


defn initialize-scope (s:Stmt) -> Stmt :
defn run-initialize (s:Stmt) -> Stmt :
val [s!,renames] = initialize-registers(s)
val s!! = rename(s!,renames)
val s!!! = add-when(s!!,renames)
s!!!
match(s) :
(s:Begin) : run-initialize(s)
(s:DefRegister) : run-initialize(s)
(s:Conditionally) : run-initialize(s)
;TODO Add Letrec
(s) : s
val [s!,renames] = initialize-registers(s)
val s!! = rename(s!,renames)
val s!!! = add-when(s!!,renames)
s!!!

defn initialize-scope (m:Module) -> Module :
defn initialize-module (m:Module) -> Module :
Module(name(m), ports(m), body!) where :
val body! = initialize-scope(body(m))

Circuit(modules*, main(c)) where :
val modules* =
for m in modules(c) map :
initialize-scope(m)
initialize-module(m)


;============== INFER TYPES ================================
; This pass infers the type field in all IR nodes by updating
; and passing an environment to all statements in pre-order
; traversal, and resolving types in expressions in post-
; order traversal.
; Type propagation for primary ops are defined here.
; Notable cases: LetRec requires updating environment before
; resolving the subexpressions in its elements.
; Type errors are not checked in this pass, as this is
; postponed for a later/earlier pass.

defmethod type (v:UIntValue) :
UIntType(width(v))

Expand Down Expand Up @@ -327,6 +341,7 @@ defn infer (op:PrimOp, arg-types: List<Type>) -> Type :

defn arg0 () : wipe-width(arg-types[0])
defn arg1 () : wipe-width(arg-types[1])
; TODO subtle, not entirely figured out
switch {op == _} :
ADD-OP : arg0()
ADD-MOD-OP : arg0()
Expand Down Expand Up @@ -380,6 +395,7 @@ defn infer (c:Stmt, e:List<KeyValue<Symbol, Type>>) -> [Stmt, List<KeyValue<Symb
put-type(e, infer(op(e), map(type, args(e))))
(e:ReadPort) :
put-type(e, vector-elem-type(type(mem(e))))
(e:Null) : e

defn element-type (e:Element, env:List<KeyValue<Symbol,Type>>) :
match(e) :
Expand Down Expand Up @@ -1883,6 +1899,7 @@ public defn run-passes (c: Circuit, p: List<Char>) :
defn do-stage (name:String, f: Circuit -> Circuit) :
println(name)
c* = f(c*)
println(c*)

; Early passes:
; If modules have a reset defined, must be an INPUT and UInt(1)
Expand All @@ -1902,7 +1919,6 @@ public defn run-passes (c: Circuit, p: List<Char>) :
if contains(p,'n') : do-stage("Pad Widths", pad-widths)
if contains(p,'o') : do-stage("Inline Instances", inline-instances)

println(c*)
println("\n\n\n\n")


Expand Down
46 changes: 46 additions & 0 deletions test/passes/infer-types/gcd.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
; RUN: firrtl %s abcde | tee %s.out | FileCheck %s

;CHECK: Infer Types
circuit top :
module subtracter :
input x : UInt
input y : UInt
output z : UInt
z := sub-mod(x, y)
module gcd :
input a : UInt(16)
input b : UInt(16)
input e : UInt(1)
output z : UInt(16)
output v : UInt(1)
reg x : UInt
reg y : UInt
; CHECK: reg x : UInt
x.init := UInt(0)
y.init := UInt(42)
when greater(x, y) :
inst s of subtracter
s.x := x
s.y := y
x := s.z
else :
inst s2 of subtracter
s2.x := x
s2.y := y
y := s2.z
when e :
x := a
y := b
v := equal(v, UInt(0))
z := x
module top :
input a : UInt(16)
input b : UInt(16)
output z : UInt
inst i of gcd
i.a := a
i.b := b
i.e := UInt(1)
z := i.z


8 changes: 8 additions & 0 deletions test/passes/infer-types/primops.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; RUN: firrtl %s abcde | tee %s.out | FileCheck %s

;CHECK: Infer Types
circuit top :
module top :
wire io : {input x : UInt, output y : UInt}
io.x := add(io.x,io.y)
;CHECK: n:io:{input x : UInt, output y : UInt}.x := add(n:io:{input x : UInt, output y : UInt}.x, n:io:{input x : UInt, output y : UInt}.y)
2 changes: 2 additions & 0 deletions test/passes/initialize-register/begin.fir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; RUN: firrtl %s abcd | tee %s.out | FileCheck %s

; CHECK: Initialize Registers
circuit top :
module top :
input a : UInt(16)
Expand All @@ -12,6 +13,7 @@

reg r2 : UInt
r2.init := UInt(0)
; CHECK-NOT: r2.init := UInt(0)
; CHECK: wire [[R2:gen[0-9]*]] : UInt
; CHECK-NOT: reg:r2 := n:[[R2]]
; CHECK: n:[[R2]] := Null
Expand Down
3 changes: 2 additions & 1 deletion test/passes/initialize-register/when.fir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; RUN: firrtl %s abcd | tee %s.out | FileCheck %s
; CHECK: circuit top :

; CHECK: Initialize Registers
circuit top :
module top :
input a : UInt(16)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; RUN: firrtl %s abc | tee %s.out | FileCheck %s

; CHECK: Make Explicit Reset
circuit top :
module A :
;CHECK: input reset : UInt(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; RUN: firrtl %s ab | tee %s.out | FileCheck %s

; CHECK: Resolve Kinds
circuit top :
module subtracter :
input x : UInt
Expand Down

0 comments on commit 2353d64

Please sign in to comment.