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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
package firrtl
import com.typesafe.scalalogging.LazyLogging
import Utils._
import DebugUtils._
object PrimOps extends LazyLogging {
private val mapPrimOp2String = Map[PrimOp, String](
ADD_OP -> "add",
SUB_OP -> "sub",
MUL_OP -> "mul",
DIV_OP -> "div",
REM_OP -> "rem",
LESS_OP -> "lt",
LESS_EQ_OP -> "leq",
GREATER_OP -> "gt",
GREATER_EQ_OP -> "geq",
EQUAL_OP -> "eq",
NEQUAL_OP -> "neq",
PAD_OP -> "pad",
AS_UINT_OP -> "asUInt",
AS_SINT_OP -> "asSInt",
AS_CLOCK_OP -> "asClock",
SHIFT_LEFT_OP -> "shl",
SHIFT_RIGHT_OP -> "shr",
DYN_SHIFT_LEFT_OP -> "dshl",
DYN_SHIFT_RIGHT_OP -> "dshr",
NEG_OP -> "neg",
CONVERT_OP -> "cvt",
NOT_OP -> "not",
AND_OP -> "and",
OR_OP -> "or",
XOR_OP -> "xor",
AND_REDUCE_OP -> "andr",
OR_REDUCE_OP -> "orr",
XOR_REDUCE_OP -> "xorr",
CONCAT_OP -> "cat",
BITS_SELECT_OP -> "bits",
HEAD_OP -> "head",
TAIL_OP -> "tail"
)
private val mapString2PrimOp = mapPrimOp2String.map(_.swap)
def fromString(op: String): PrimOp = mapString2PrimOp(op)
implicit class PrimOpImplicits(op: PrimOp){
def getString(): String = mapPrimOp2String(op)
}
// Borrowed from Stanza implementation
def set_primop_type (e:DoPrim) : DoPrim = {
//println-all(["Inferencing primop type: " e])
def PLUS (w1:Width,w2:Width) : Width = PlusWidth(w1,w2)
def MAX (w1:Width,w2:Width) : Width = MaxWidth(Seq(w1,w2))
def MINUS (w1:Width,w2:Width) : Width = MinusWidth(w1,w2)
def POW (w1:Width) : Width = ExpWidth(w1)
def MIN (w1:Width,w2:Width) : Width = MinWidth(Seq(w1,w2))
val o = e.op
val a = e.args
val c = e.consts
def t1 () = tpe(a(0))
def t2 () = tpe(a(1))
def t3 () = tpe(a(2))
def w1 () = widthBANG(tpe(a(0)))
def w2 () = widthBANG(tpe(a(1)))
def w3 () = widthBANG(tpe(a(2)))
def c1 () = IntWidth(c(0))
def c2 () = IntWidth(c(1))
o match {
case ADD_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => UIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1:UIntType, t2:SIntType) => SIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1:SIntType, t2:UIntType) => SIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1:SIntType, t2:SIntType) => SIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case SUB_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => SIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1:UIntType, t2:SIntType) => SIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1:SIntType, t2:UIntType) => SIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1:SIntType, t2:SIntType) => SIntType(PLUS(MAX(w1(),w2()),ONE))
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case MUL_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => UIntType(PLUS(w1(),w2()))
case (t1:UIntType, t2:SIntType) => SIntType(PLUS(w1(),w2()))
case (t1:SIntType, t2:UIntType) => SIntType(PLUS(w1(),w2()))
case (t1:SIntType, t2:SIntType) => SIntType(PLUS(w1(),w2()))
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case DIV_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => UIntType(w1())
case (t1:UIntType, t2:SIntType) => SIntType(PLUS(w1(),ONE))
case (t1:SIntType, t2:UIntType) => SIntType(w1())
case (t1:SIntType, t2:SIntType) => SIntType(PLUS(w1(),ONE))
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case REM_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => UIntType(MIN(w1(),w2()))
case (t1:UIntType, t2:SIntType) => UIntType(MIN(w1(),w2()))
case (t1:SIntType, t2:UIntType) => SIntType(MIN(w1(),PLUS(w2(),ONE)))
case (t1:SIntType, t2:SIntType) => SIntType(MIN(w1(),w2()))
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case LESS_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => BoolType()
case (t1:SIntType, t2:UIntType) => BoolType()
case (t1:UIntType, t2:SIntType) => BoolType()
case (t1:SIntType, t2:SIntType) => BoolType()
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case LESS_EQ_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => BoolType()
case (t1:SIntType, t2:UIntType) => BoolType()
case (t1:UIntType, t2:SIntType) => BoolType()
case (t1:SIntType, t2:SIntType) => BoolType()
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case GREATER_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => BoolType()
case (t1:SIntType, t2:UIntType) => BoolType()
case (t1:UIntType, t2:SIntType) => BoolType()
case (t1:SIntType, t2:SIntType) => BoolType()
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case GREATER_EQ_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => BoolType()
case (t1:SIntType, t2:UIntType) => BoolType()
case (t1:UIntType, t2:SIntType) => BoolType()
case (t1:SIntType, t2:SIntType) => BoolType()
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case EQUAL_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => BoolType()
case (t1:SIntType, t2:UIntType) => BoolType()
case (t1:UIntType, t2:SIntType) => BoolType()
case (t1:SIntType, t2:SIntType) => BoolType()
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case NEQUAL_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => BoolType()
case (t1:SIntType, t2:UIntType) => BoolType()
case (t1:UIntType, t2:SIntType) => BoolType()
case (t1:SIntType, t2:SIntType) => BoolType()
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case PAD_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(MAX(w1(),c1()))
case (t1:SIntType) => SIntType(MAX(w1(),c1()))
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case AS_UINT_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(w1())
case (t1:SIntType) => UIntType(w1())
case (t1:ClockType) => UIntType(ONE)
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case AS_SINT_OP => {
val t = (t1()) match {
case (t1:UIntType) => SIntType(w1())
case (t1:SIntType) => SIntType(w1())
case (t1:ClockType) => SIntType(ONE)
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case AS_CLOCK_OP => {
val t = (t1()) match {
case (t1:UIntType) => ClockType()
case (t1:SIntType) => ClockType()
case (t1:ClockType) => ClockType()
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case SHIFT_LEFT_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(PLUS(w1(),c1()))
case (t1:SIntType) => SIntType(PLUS(w1(),c1()))
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case SHIFT_RIGHT_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(MINUS(w1(),c1()))
case (t1:SIntType) => SIntType(MINUS(w1(),c1()))
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case DYN_SHIFT_LEFT_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(PLUS(w1(),POW(w2())))
case (t1:SIntType) => SIntType(PLUS(w1(),POW(w2())))
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case DYN_SHIFT_RIGHT_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(w1())
case (t1:SIntType) => SIntType(w1())
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case CONVERT_OP => {
val t = (t1()) match {
case (t1:UIntType) => SIntType(PLUS(w1(),ONE))
case (t1:SIntType) => SIntType(w1())
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case NEG_OP => {
val t = (t1()) match {
case (t1:UIntType) => SIntType(PLUS(w1(),ONE))
case (t1:SIntType) => SIntType(PLUS(w1(),ONE))
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case NOT_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(w1())
case (t1:SIntType) => UIntType(w1())
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case AND_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:SIntType) => UIntType(MAX(w1(),w2()))
case (t1,t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case OR_OP => {
val t = (t1(),t2()) match {
case (t1:SIntType, t2:SIntType) => UIntType(MAX(w1(),w2()))
case (t1,t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case XOR_OP => {
val t = (t1(),t2()) match {
case (t1:UIntType, t2:UIntType) => UIntType(MAX(w1(),w2()))
case (t1:SIntType, t2:UIntType) => UIntType(MAX(w1(),w2()))
case (t1:UIntType, t2:SIntType) => UIntType(MAX(w1(),w2()))
case (t1:SIntType, t2:SIntType) => UIntType(MAX(w1(),w2()))
case (t1,t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case AND_REDUCE_OP => {
val t = (t1()) match {
case (t1:UIntType) => BoolType()
case (t1:SIntType) => BoolType()
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case OR_REDUCE_OP => {
val t = (t1()) match {
case (t1:UIntType) => BoolType()
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case XOR_REDUCE_OP => {
val t = (t1()) match {
case (t1:SIntType) => BoolType()
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case CONCAT_OP => {
val t = (t1(),t2()) match {
case (t1:SIntType, t2:SIntType) => UIntType(PLUS(w1(),w2()))
case (t1, t2) => UnknownType()
}
DoPrim(o,a,c,t)
}
case BITS_SELECT_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(PLUS(MINUS(c1(),c2()),ONE))
case (t1:SIntType) => UIntType(PLUS(MINUS(c1(),c2()),ONE))
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case HEAD_OP => {
val t = (t1()) match {
case (t1:UIntType) => UIntType(c1())
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
case TAIL_OP => {
val t = (t1()) match {
case (t1:SIntType) => UIntType(MINUS(w1(),c1()))
case (t1) => UnknownType()
}
DoPrim(o,a,c,t)
}
}
}
}
|