From 2006604530fb8fd214fe4e8e182bedacd71b49b3 Mon Sep 17 00:00:00 2001 From: azidar Date: Sat, 2 May 2015 15:25:04 -0700 Subject: Added a infrastructure for check passes, and wrote a few --- src/main/stanza/errors.stanza | 139 ++++++++++++++++++++++++++++++++ src/main/stanza/firrtl-ir.stanza | 5 ++ src/main/stanza/firrtl-main.stanza | 1 + src/main/stanza/firrtl-test-main.stanza | 1 + src/main/stanza/passes.stanza | 6 +- src/main/stanza/primop.stanza | 1 + 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/main/stanza/errors.stanza (limited to 'src') diff --git a/src/main/stanza/errors.stanza b/src/main/stanza/errors.stanza new file mode 100644 index 00000000..43575854 --- /dev/null +++ b/src/main/stanza/errors.stanza @@ -0,0 +1,139 @@ +defpackage firrtl/errors : + import core + import verse + import firrtl/ir2 + import firrtl/ir-utils + import firrtl/primops + import firrtl/passes + import firrtl-main + +;========== ALL CHECKS ================= +;CAUGHT IN PARSER +; No nested modules <- parser +; Only modules in circuit (no statements or expressions) <- parser + +;CAUGHT in HIGH FORM CHECK +; Unique names per module +; No name can be a prefix of any other name. +; Can only connect to a Ref or Subfield or Index +; UInt only has positive ints +; all references are declared +; mems cannot be a bundle with flips +; cannot connect to Register or ReadPort + +;AFTER KIND RESOLUTION +; Cannot connect directly to a mem ever +; onreset can only handle a register + +;AFTER TYPE INFERENCE +; expression in pad must be a ground type +; Subfields are only on bundles, before type inference <- need to not error, just do unknown-type +; node's value cannot be a bundle with a flip in it +; 2nd arg in dshr/l must be UInt +; pred in conditionally must be of type UInt +; Type checking + +;AFTER WIDTH INFERENCE +; No names +; No Unknowns +; All widths are positive +; Pad's width is greater than value's width +; pad's width is greater than value's width + +;AFTER LOWERING +; All things connect to once + +; ?? +; No combinational loops +; cannot connect to a pad, or a register. only connct to a reference + +definterface HighFormException <: Exception +defn HighFormException (s:String) : + new HighFormException : + defmethod print (o:OutputStream, this) : + print(o, s) + +defn HighFormExceptions (xs:Streamable) : + HighFormException(string-join(xs, "\n")) + +defn NotUnique (info:FileInfo|False, name:Symbol) : + HighFormException $ string-join $ + [info ": Reference " name " does not have a unique name."] + +defn IsPrefix (info:FileInfo|False, name:Symbol, prefix:Symbol, dec:FileInfo|False) : + HighFormException $ string-join $ + [info ": Reference " name " is an invalid name because the prefix " prefix " is declared at " dec "."] + +defn InvalidLOC (info:FileInfo|False) : + HighFormException $ string-join $ + [info ": Invalid connect to an expression that is not a reference or a WritePort."] + +defn NegUInt (info:FileInfo|False) : + HighFormException $ string-join $ + [info ": UInt has a negative value."] + +defn UndeclaredReference (info:FileInfo|False, name:Symbol) : + HighFormException $ string-join $ + [info ": Reference " name " is not declared."] + +defn MemWithFlip (info:FileInfo|False, name:Symbol) : + HighFormException $ string-join $ + [info ": Memory " name " cannot be a bundle type with flips."] + +defn InvalidSubfield (info:FileInfo|False, name:Symbol) : + HighFormException $ string-join $ + [info ": Invalid subfield access to non-reference."] + +defn InvalidIndex (info:FileInfo|False, name:Symbol) : + HighFormException $ string-join $ + [info ": Invalid index access to non-reference."] + +defn NoTopModule (info:FileInfo|False, name:Symbol) : + HighFormException $ string-join $ + [info ": A single module must be named " name "."] + +;================ Check Helper Functions ============== +defn has-flip? (t:Type) -> True|False : + var has? = false + defn find-flip (t:Type) -> Type : + match(t) : + (t:BundleType) : + for f in fields(t) do : + if flip(f) == REVERSE : has? = true + t + (t) : t + find-flip(t) + map(find-flip,t) + has? +;================= High Form Check ========================== +;CAUGHT in HIGH FORM CHECK +; o Unique names per module +; o No name can be a prefix of any other name. +; o Can only connect to a Ref or Subfield or Index +; o UInt only has positive ints +; o all references are declared +; o cannot connect to Register or ReadPort +; * A module has the same name as main of circuit +; * mems cannot be a bundle with flips + +public defn check-high-form (c:Circuit) -> Circuit : + val errors = Vector() + + defn check-high-form-s (s:Stmt) -> Stmt : + map{check-high-form-s,_} $ match(s) : + (s:DefMemory) : + if has-flip?(type(s)) : add(errors, MemWithFlip(info!(s), name(s))) + s + (s) : s + defn check-high-form-m (ms:List) -> False : + var number-top-m = 0 + for m in ms do : + if name(m) == main(c) : number-top-m = number-top-m + 1 + check-high-form-s(body(m)) + if number-top-m != 1 : add(errors,NoTopModule(info!(c),main(c))) + + check-high-form-m(modules(c)) + throw(HighFormExceptions(errors)) when not empty?(errors) + c + + diff --git a/src/main/stanza/firrtl-ir.stanza b/src/main/stanza/firrtl-ir.stanza index 63a28211..67d2c2d9 100644 --- a/src/main/stanza/firrtl-ir.stanza +++ b/src/main/stanza/firrtl-ir.stanza @@ -2,6 +2,9 @@ defpackage firrtl/ir2 : import core import verse +public defmulti info! (x:?) -> False +public defmethod info! (x:?) : false + public definterface Direction public val INPUT = new Direction public val OUTPUT = new Direction @@ -236,3 +239,5 @@ public defstruct Module : public defstruct Circuit : modules: List main: Symbol + + diff --git a/src/main/stanza/firrtl-main.stanza b/src/main/stanza/firrtl-main.stanza index 92bb066f..efc6a7d0 100644 --- a/src/main/stanza/firrtl-main.stanza +++ b/src/main/stanza/firrtl-main.stanza @@ -8,6 +8,7 @@ #include("ir-parser.stanza") #include("passes.stanza") #include("primop.stanza") +#include("errors.stanza") defpackage firrtl-main : import core diff --git a/src/main/stanza/firrtl-test-main.stanza b/src/main/stanza/firrtl-test-main.stanza index f929455d..ca9bfa33 100644 --- a/src/main/stanza/firrtl-test-main.stanza +++ b/src/main/stanza/firrtl-test-main.stanza @@ -8,6 +8,7 @@ #include("ir-parser.stanza") #include("passes.stanza") #include("primop.stanza") +#include("errors.stanza") defpackage firrtl-main : import core diff --git a/src/main/stanza/passes.stanza b/src/main/stanza/passes.stanza index 1b752c27..7d5cd481 100644 --- a/src/main/stanza/passes.stanza +++ b/src/main/stanza/passes.stanza @@ -5,6 +5,7 @@ defpackage firrtl/passes : import firrtl/ir-utils import firrtl/primops import firrtl-main + import firrtl/errors ;============== EXCEPTIONS ================================= defclass PassException <: Exception @@ -838,7 +839,9 @@ defn lower (body:Stmt) -> Stmt : switch fn ([x,y]) : lgender == x and rgender == y : [FEMALE,MALE] : ConnectToIndexed(index(s),locs,r*) [MALE,FEMALE] : ConnectFromIndexed(index(s),r*,locs) - (s:Begin|Conditionally|EmptyStmt) : map(lower-stmt,s) + (s:Conditionally) : + Conditionally(exp(head $ expand-expr(pred(s))),lower-stmt(conseq(s)),lower-stmt(alt(s))) + (s:Begin|EmptyStmt) : map(lower-stmt,s) lower-stmt(body) @@ -2008,6 +2011,7 @@ public defn run-passes (c: Circuit, p: List,file:String) : ; Early passes: ; If modules have a reset defined, must be an INPUT and UInt(1) + if contains(p,'X') or contains(p,'A') : do-stage("High Form Check", check-high-form) if contains(p,'X') or contains(p,'a') : do-stage("Temp Elimination", temp-elimination) if contains(p,'X') or contains(p,'b') : do-stage("Working IR", to-working-ir) if contains(p,'X') or contains(p,'c') : do-stage("Make Explicit Reset", make-explicit-reset) diff --git a/src/main/stanza/primop.stanza b/src/main/stanza/primop.stanza index b293da52..34dd0392 100644 --- a/src/main/stanza/primop.stanza +++ b/src/main/stanza/primop.stanza @@ -185,6 +185,7 @@ public defn lower-and-type-primop (e:DoPrim) -> DoPrim : match(type(args(e)[1]),type(args(e)[2])) : (t1:UIntType, t2:UIntType) : MUX-UU-OP (t1:SIntType, t2:SIntType) : MUX-SS-OP + (t1,t2) : error(to-string(args(e))) MUX-UU-OP : DoPrim(op(e),args(e),consts(e),u()) MUX-SS-OP : DoPrim(op(e),args(e),consts(e),s()) PAD-OP : -- cgit v1.2.3