aboutsummaryrefslogtreecommitdiff
path: root/src/main/stanza/primop.stanza
diff options
context:
space:
mode:
authorazidar2015-11-06 09:51:59 -0800
committerazidar2016-01-16 14:28:16 -0800
commitffa090c10d6210395e3f304e56008e2183a85698 (patch)
tree25fce98795c897f655a4e6dd2f2ebf866e9c3049 /src/main/stanza/primop.stanza
parent407200e46de9a97f8a88c210e3b0e7d6d38f307b (diff)
WIP
Diffstat (limited to 'src/main/stanza/primop.stanza')
-rw-r--r--src/main/stanza/primop.stanza325
1 files changed, 200 insertions, 125 deletions
diff --git a/src/main/stanza/primop.stanza b/src/main/stanza/primop.stanza
index b285e0ee..a31b9c51 100644
--- a/src/main/stanza/primop.stanza
+++ b/src/main/stanza/primop.stanza
@@ -5,132 +5,207 @@ defpackage firrtl/primops :
import firrtl/ir-utils
import firrtl/passes
+defn PLUS (w1:Width,w2:Width) -> Width : PlusWidth(w1,w2)
+defn MAX (w1:Width,w2:Width) -> Width : MaxWidth(list(w1,w2))
+defn MINUS (w1:Width,w2:Width) -> Width : MinusWidth(w1,w2)
+defn POW (w1:Width) -> Width : ExpWidth(w1)
-public defn lower-and-type-primop (e:DoPrim) -> DoPrim :
- defn u () : UIntType(UnknownWidth())
- defn s () : SIntType(UnknownWidth())
- defn u-and (op1:Expression,op2:Expression) :
- match(type(op1), type(op2)) :
- (t1:UIntType, t2:UIntType) : u()
- (t1:SIntType, t2) : s()
- (t1, t2:SIntType) : s()
- (t1, t2) : UnknownType()
-
- defn of-type (op:Expression) :
- match(type(op)) :
- (t:UIntType) : u()
- (t:SIntType) : s()
- (t) : UnknownType()
-
+public defn set-primop-type (e:DoPrim) -> DoPrim :
;println-all(["Inferencing primop type: " e])
+ val o = op(e)
+ val a = args(e)
+ val c = consts(e)
+ defn t1 () : type(a[0])
+ defn t2 () : type(a[1])
+ defn t3 () : type(a[2])
+ defn w1 () : width!(type(a[0]))
+ defn w2 () : width!(type(a[1]))
+ defn w3 () : width!(type(a[2]))
+ defn c1 () : IntWidth(c[0])
+ defn c2 () : IntWidth(c[1])
switch {op(e) == _} :
- ADD-OP : DoPrim(ADD-OP,args(e),consts(e),u-and(args(e)[0],args(e)[1]))
- SUB-OP : DoPrim(SUB-OP,args(e),consts(e),s())
- MUL-OP : DoPrim(MUL-OP,args(e),consts(e),u-and(args(e)[0],args(e)[1]))
- DIV-OP : DoPrim(DIV-OP,args(e),consts(e),u-and(args(e)[0],args(e)[1]))
- MOD-OP : DoPrim(MOD-OP,args(e),consts(e),of-type(args(e)[0]))
- QUO-OP : DoPrim(QUO-OP,args(e),consts(e),u-and(args(e)[0],args(e)[1]))
- REM-OP : DoPrim(REM-OP,args(e),consts(e),of-type(args(e)[1]))
- ADD-WRAP-OP : DoPrim(ADD-WRAP-OP,args(e),consts(e),u-and(args(e)[0],args(e)[1]))
- SUB-WRAP-OP : DoPrim(SUB-WRAP-OP,args(e),consts(e),u-and(args(e)[0],args(e)[1]))
- LESS-OP : DoPrim(LESS-OP,args(e),consts(e),u())
- LESS-EQ-OP : DoPrim(LESS-EQ-OP,args(e),consts(e),u())
- GREATER-OP : DoPrim(GREATER-OP,args(e),consts(e),u())
- GREATER-EQ-OP : DoPrim(GREATER-EQ-OP,args(e),consts(e),u())
- EQUAL-OP : DoPrim(EQUAL-OP,args(e),consts(e),u())
- NEQUAL-OP : DoPrim(NEQUAL-OP,args(e),consts(e),u())
- EQUIV-OP : DoPrim(EQUIV-OP,args(e),consts(e),u())
- NEQUIV-OP : DoPrim(NEQUIV-OP,args(e),consts(e),u())
- MUX-OP : DoPrim(MUX-OP,args(e),consts(e),of-type(args(e)[1]))
- PAD-OP : DoPrim(PAD-OP,args(e),consts(e),of-type(args(e)[0]))
- AS-UINT-OP : DoPrim(AS-UINT-OP,args(e),consts(e),u())
- AS-SINT-OP : DoPrim(AS-SINT-OP,args(e),consts(e),s())
- DYN-SHIFT-LEFT-OP : DoPrim(DYN-SHIFT-LEFT-OP,args(e),consts(e),of-type(args(e)[0]))
- DYN-SHIFT-RIGHT-OP : DoPrim(DYN-SHIFT-RIGHT-OP,args(e),consts(e),of-type(args(e)[0]))
- SHIFT-LEFT-OP : DoPrim(SHIFT-LEFT-OP,args(e),consts(e),of-type(args(e)[0]))
- SHIFT-RIGHT-OP : DoPrim(SHIFT-RIGHT-OP,args(e),consts(e),of-type(args(e)[0]))
- CONVERT-OP : DoPrim(CONVERT-OP,args(e),consts(e),s())
- NEG-OP : DoPrim(NEG-OP,args(e),consts(e),s())
- BIT-NOT-OP : DoPrim(op(e),args(e),consts(e),of-type(args(e)[0]))
- BIT-AND-OP : DoPrim(op(e),args(e),consts(e),of-type(args(e)[0]))
- BIT-OR-OP : DoPrim(op(e),args(e),consts(e),of-type(args(e)[0]))
- BIT-XOR-OP : DoPrim(op(e),args(e),consts(e),of-type(args(e)[0]))
- BIT-SELECT-OP : DoPrim(op(e),args(e),consts(e),u())
- BITS-SELECT-OP : DoPrim(op(e),args(e),consts(e),u())
- CONCAT-OP : DoPrim(op(e),args(e),consts(e),u())
- BIT-AND-REDUCE-OP : DoPrim(op(e),args(e),consts(e),u())
- BIT-OR-REDUCE-OP : DoPrim(op(e),args(e),consts(e),u())
- BIT-XOR-REDUCE-OP : DoPrim(op(e),args(e),consts(e),u())
-
-public defn primop-gen-constraints (e:DoPrim,v:Vector<WGeq>) -> Type :
- defn get-max (i0:Int,i1:Int) -> Width : get-max(list(i0,i1))
- defn get-max (ls:List<Int>) -> Width :
- MaxWidth $ for i in ls map : width!(args(e)[i])
- defn all-max () -> Width :
- MaxWidth $ for x in args(e) map : width!(x)
-
- println-all-debug(["Looking at " op(e) " with inputs " args(e)])
- val w* = switch {op(e) == _} :
- ADD-OP : PlusWidth(get-max(0,1),LongWidth(1))
- SUB-OP : PlusWidth(get-max(0,1),LongWidth(1))
- MUL-OP : PlusWidth(width!(args(e)[0]),width!(args(e)[1]))
- DIV-OP :
- match(type(args(e)[0]),type(args(e)[1])) :
- (t0:UIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),LongWidth(1))
- (t0:SIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),LongWidth(1))
- (t0,t1) : width!(args(e)[0])
- MOD-OP :
- match(type(args(e)[0]),type(args(e)[1])) :
- (t0:SIntType,t1:UIntType) : PlusWidth(width!(args(e)[1]),LongWidth(1))
- (t0,t1) : width!(args(e)[1])
- QUO-OP :
- match(type(args(e)[0]),type(args(e)[1])) :
- (t0:UIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),LongWidth(1))
- (t0:SIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),LongWidth(1))
- (t0,t1) : width!(args(e)[0])
- REM-OP :
- match(type(args(e)[0]),type(args(e)[1])) :
- (t0:SIntType,t1:UIntType) : PlusWidth(width!(args(e)[1]),LongWidth(1))
- (t0,t1) : width!(args(e)[1])
- ADD-WRAP-OP : get-max(0,1)
- SUB-WRAP-OP : get-max(0,1)
- LESS-OP : LongWidth(1)
- LESS-EQ-OP : LongWidth(1)
- GREATER-OP : LongWidth(1)
- GREATER-EQ-OP : LongWidth(1)
- EQUAL-OP : LongWidth(1)
- NEQUAL-OP : LongWidth(1)
- EQUIV-OP : LongWidth(1)
- NEQUIV-OP : LongWidth(1)
- MUX-OP :
- add(v,WGeq(LongWidth(1),width!(args(e)[0])))
- add(v,WGeq(width!(args(e)[0]),LongWidth(1)))
- get-max(1,2)
- PAD-OP : LongWidth(consts(e)[0])
- AS-UINT-OP : width!(args(e)[0])
- AS-SINT-OP : width!(args(e)[0])
- SHIFT-LEFT-OP : PlusWidth(width!(args(e)[0]),LongWidth(consts(e)[0]))
- SHIFT-RIGHT-OP : MinusWidth(width!(args(e)[0]),LongWidth(consts(e)[0]))
- DYN-SHIFT-LEFT-OP : PlusWidth(width!(args(e)[0]),ExpWidth(width!(args(e)[1])))
- DYN-SHIFT-RIGHT-OP : width!(args(e)[0])
- CONVERT-OP :
- match(type(args(e)[0])) :
- (t0:UIntType) : PlusWidth(width!(args(e)[0]),LongWidth(1))
- (t0:SIntType) : width!(args(e)[0])
- NEG-OP : PlusWidth(width!(args(e)[0]),LongWidth(1))
- BIT-NOT-OP : width!(args(e)[0])
- BIT-AND-OP : get-max(0,1)
- BIT-OR-OP : get-max(0,1)
- BIT-XOR-OP : get-max(0,1)
- BIT-AND-REDUCE-OP : all-max()
- BIT-OR-REDUCE-OP : all-max()
- BIT-XOR-REDUCE-OP : all-max()
- CONCAT-OP : PlusWidth(width!(args(e)[0]),width!(args(e)[1]))
- BIT-SELECT-OP : LongWidth(1)
- BITS-SELECT-OP : LongWidth(consts(e)[0] - consts(e)[1] + 1)
-
- match(type(e)) :
- (t:UIntType) : UIntType(w*)
- (t:SIntType) : SIntType(w*)
- (t) : error("Shouldn't be here")
+ ADD-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(PLUS(MAX(w1(),w2()),ONE))
+ (t1:UIntType, t2:SIntType) : SIntType(PLUS(MAX(w1(),w2()),ONE))
+ (t1:SIntType, t2:UIntType) : SIntType(PLUS(MAX(w1(),w2()),ONE))
+ (t1:SIntType, t2:SIntType) : SIntType(PLUS(MAX(w1(),w2()),ONE))
+ SUB-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : SIntType(PLUS(MAX(w1(),w2()),ONE))
+ (t1:UIntType, t2:SIntType) : SIntType(PLUS(MAX(w1(),w2()),ONE))
+ (t1:SIntType, t2:UIntType) : SIntType(PLUS(MAX(w1(),w2()),ONE))
+ (t1:SIntType, t2:SIntType) : SIntType(PLUS(MAX(w1(),w2()),ONE))
+ ADD-WRAP-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(MAX(w1(),w2()))
+ (t1:UIntType, t2:SIntType) : SIntType(MAX(w1(),w2()))
+ (t1:SIntType, t2:UIntType) : SIntType(MAX(w1(),w2()))
+ (t1:SIntType, t2:SIntType) : SIntType(MAX(w1(),w2()))
+ SUB-WRAP-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(MAX(w1(),w2()))
+ (t1:UIntType, t2:SIntType) : SIntType(MAX(w1(),w2()))
+ (t1:SIntType, t2:UIntType) : SIntType(MAX(w1(),w2()))
+ (t1:SIntType, t2:SIntType) : SIntType(MAX(w1(),w2()))
+ MUL-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(PLUS(w1(),w2()))
+ (t1:UIntType, t2:SIntType) : SIntType(PLUS(w1(),w2()))
+ (t1:SIntType, t2:UIntType) : SIntType(PLUS(w1(),w2()))
+ (t1:SIntType, t2:SIntType) : SIntType(PLUS(w1(),w2()))
+ DIV-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(w1())
+ (t1:UIntType, t2:SIntType) : SIntType(PLUS(w1(),w2()))
+ (t1:SIntType, t2:UIntType) : SIntType(w1())
+ (t1:SIntType, t2:SIntType) : SIntType(PLUS(w1(),w2()))
+ MOD-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(w2())
+ (t1:UIntType, t2:SIntType) : UIntType(w2())
+ (t1:SIntType, t2:UIntType) : SIntType(PLUS(w2(),ONE))
+ (t1:SIntType, t2:SIntType) : SIntType(w2())
+ QUO-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(PLUS(w1(),ONE))
+ (t1:UIntType, t2:SIntType) : SIntType(w1())
+ (t1:SIntType, t2:UIntType) : SIntType(PLUS(w1(),ONE))
+ (t1:SIntType, t2:SIntType) : SIntType(w1())
+ REM-OP : DoPrim{o,a,c,_} $
+ match(t1(),t2()) :
+ (t1:UIntType, t2:UIntType) : UIntType(w2())
+ (t1:UIntType, t2:SIntType) : SIntType(w2())
+ (t1:SIntType, t2:UIntType) : UIntType(PLUS(w2(),ONE))
+ (t1:SIntType, t2:SIntType) : SIntType(w2())
+ LESS-OP : DoPrim(o,a,c,BoolType())
+ LESS-EQ-OP : DoPrim(o,a,c,BoolType())
+ GREATER-OP : DoPrim(o,a,c,BoolType())
+ GREATER-EQ-OP : DoPrim(o,a,c,BoolType())
+ EQUAL-OP : DoPrim(o,a,c,BoolType())
+ NEQUAL-OP : DoPrim(o,a,c,BoolType())
+ EQUIV-OP : DoPrim(o,a,c,BoolType())
+ NEQUIV-OP : DoPrim(o,a,c,BoolType())
+ MUX-OP : DoPrim{o,a,c,_} $
+ match(t2(),t3()) :
+ (t2:UIntType, t3:UIntType) : UIntType(MAX(w2(),w3()))
+ (t2:SIntType, t3:SIntType) : SIntType(MAX(w2(),w3()))
+ PAD-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : UIntType(c1())
+ (t1:SIntType) : SIntType(c1())
+ AS-UINT-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : UIntType(w1())
+ (t1:SIntType) : UIntType(w1())
+ AS-SINT-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : SIntType(w1())
+ (t1:SIntType) : SIntType(w1())
+ SHIFT-LEFT-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : UIntType(PLUS(w1(),c1()))
+ (t1:SIntType) : SIntType(PLUS(w1(),c1()))
+ SHIFT-RIGHT-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : UIntType(MINUS(w1(),c1()))
+ (t1:SIntType) : SIntType(MINUS(w1(),c1()))
+ DYN-SHIFT-LEFT-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : UIntType(PLUS(w1(),POW(w2())))
+ (t1:SIntType) : SIntType(PLUS(w1(),POW(w2())))
+ DYN-SHIFT-RIGHT-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : UIntType(w1())
+ (t1:SIntType) : SIntType(w1())
+ CONVERT-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : SIntType(PLUS(w1(),ONE))
+ (t1:SIntType) : SIntType(w1())
+ NEG-OP : DoPrim{o,a,c,_} $
+ match(t1()) :
+ (t1:UIntType) : SIntType(PLUS(w1(),ONE))
+ (t1:SIntType) : SIntType(w1())
+ BIT-NOT-OP : DoPrim(o,a,c,t1())
+ BIT-AND-OP : DoPrim(o,a,c,UIntType(MAX(w1(),w2())))
+ BIT-OR-OP : DoPrim(o,a,c,UIntType(MAX(w1(),w2())))
+ BIT-XOR-OP : DoPrim(o,a,c,UIntType(MAX(w1(),w2())))
+ BIT-AND-REDUCE-OP : DoPrim(o,a,c,BoolType())
+ BIT-OR-REDUCE-OP : DoPrim(o,a,c,BoolType())
+ BIT-XOR-REDUCE-OP : DoPrim(o,a,c,BoolType())
+ CONCAT-OP : DoPrim(o,a,c,UIntType(PLUS(w1(),w2())))
+ BIT-SELECT-OP : DoPrim(o,a,c,BoolType())
+ BITS-SELECT-OP : DoPrim(o,a,c,UIntType(PLUS(MINUS(c1(),c2()),ONE)))
+;public defn primop-gen-constraints (e:DoPrim,v:Vector<WGeq>) -> Type :
+; defn get-max (i0:Int,i1:Int) -> Width : get-max(list(i0,i1))
+; defn get-max (ls:List<Int>) -> Width :
+; MaxWidth $ for i in ls map : width!(args(e)[i])
+; defn all-max () -> Width :
+; MaxWidth $ for x in args(e) map : width!(x)
+;
+; println-all-debug(["Looking at " op(e) " with inputs " args(e)])
+; val w* = switch {op(e) == _} :
+; ADD-OP : PlusWidth(get-max(0,1),IntWidth(1))
+; SUB-OP : PlusWidth(get-max(0,1),IntWidth(1))
+; MUL-OP : PlusWidth(get-max(0,1),get-max(0,1))
+; DIV-OP :
+; match(type(args(e)[0]),type(args(e)[1])) :
+; (t0:UIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),IntWidth(1))
+; (t0:SIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),IntWidth(1))
+; (t0,t1) : width!(args(e)[0])
+; MOD-OP :
+; match(type(args(e)[0]),type(args(e)[1])) :
+; (t0:SIntType,t1:UIntType) : PlusWidth(width!(args(e)[1]),IntWidth(1))
+; (t0,t1) : width!(args(e)[1])
+; QUO-OP :
+; match(type(args(e)[0]),type(args(e)[1])) :
+; (t0:UIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),IntWidth(1))
+; (t0:SIntType,t1:SIntType) : PlusWidth(width!(args(e)[0]),IntWidth(1))
+; (t0,t1) : width!(args(e)[0])
+; REM-OP :
+; match(type(args(e)[0]),type(args(e)[1])) :
+; (t0:SIntType,t1:UIntType) : PlusWidth(width!(args(e)[1]),IntWidth(1))
+; (t0,t1) : width!(args(e)[1])
+; ADD-WRAP-OP : get-max(0,1)
+; SUB-WRAP-OP : get-max(0,1)
+; LESS-OP : IntWidth(1)
+; LESS-EQ-OP : IntWidth(1)
+; GREATER-OP : IntWidth(1)
+; GREATER-EQ-OP : IntWidth(1)
+; EQUAL-OP : IntWidth(1)
+; NEQUAL-OP : IntWidth(1)
+; EQUIV-OP : IntWidth(1)
+; NEQUIV-OP : IntWidth(1)
+; MUX-OP :
+; add(v,WGeq(IntWidth(1),width!(args(e)[0])))
+; add(v,WGeq(width!(args(e)[0]),IntWidth(1)))
+; get-max(1,2)
+; PAD-OP : IntWidth(consts(e)[0])
+; AS-UINT-OP : width!(args(e)[0])
+; AS-SINT-OP : width!(args(e)[0])
+; SHIFT-LEFT-OP : PlusWidth(width!(args(e)[0]),IntWidth(consts(e)[0]))
+; SHIFT-RIGHT-OP : MinusWidth(width!(args(e)[0]),IntWidth(consts(e)[0]))
+; DYN-SHIFT-LEFT-OP : PlusWidth(width!(args(e)[0]),ExpWidth(width!(args(e)[1])))
+; DYN-SHIFT-RIGHT-OP : width!(args(e)[0])
+; CONVERT-OP :
+; match(type(args(e)[0])) :
+; (t0:UIntType) : PlusWidth(width!(args(e)[0]),IntWidth(1))
+; (t0:SIntType) : width!(args(e)[0])
+; NEG-OP : PlusWidth(width!(args(e)[0]),IntWidth(1))
+; BIT-NOT-OP : width!(args(e)[0])
+; BIT-AND-OP : get-max(0,1)
+; BIT-OR-OP : get-max(0,1)
+; BIT-XOR-OP : get-max(0,1)
+; BIT-AND-REDUCE-OP : all-max()
+; BIT-OR-REDUCE-OP : all-max()
+; BIT-XOR-REDUCE-OP : all-max()
+; CONCAT-OP : PlusWidth(width!(args(e)[0]),width!(args(e)[1]))
+; BIT-SELECT-OP : IntWidth(1)
+; BITS-SELECT-OP : IntWidth(consts(e)[0] - consts(e)[1] + 1)
+;
+; match(type(e)) :
+; (t:UIntType) : UIntType(w*)
+; (t:SIntType) : SIntType(w*)
+; (t) : error("Shouldn't be here")
+;