1 /*
2  * Copyright (c) 2002-2019 Stephen Williams (steve@icarus.com)
3  *
4  *    This source code is free software; you can redistribute it
5  *    and/or modify it in source code form under the terms of the GNU
6  *    General Public License as published by the Free Software
7  *    Foundation; either version 2 of the License, or (at your option)
8  *    any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU General Public License for more details.
14  *
15  *    You should have received a copy of the GNU General Public License
16  *    along with this program; if not, write to the Free Software
17  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 # include  "config.h"
21 # include  "netlist.h"
22 # include  "netenum.h"
23 # include  "netclass.h"
24 # include  "netdarray.h"
25 # include  "compiler.h"
26 # include  "netmisc.h"
27 # include  <iostream>
28 # include  "ivl_assert.h"
29 
NetExpr(unsigned w)30 NetExpr::NetExpr(unsigned w)
31 : net_type_(0), width_(w), signed_flag_(false)
32 {
33 }
34 
NetExpr(ivl_type_t t)35 NetExpr::NetExpr(ivl_type_t t)
36 : net_type_(t), width_(0), signed_flag_(false)
37 {
38 }
39 
~NetExpr()40 NetExpr::~NetExpr()
41 {
42 }
43 
net_type() const44 ivl_type_t NetExpr::net_type() const
45 {
46       return net_type_;
47 }
48 
cast_signed(bool flag)49 void NetExpr::cast_signed(bool flag)
50 {
51       cast_signed_base_(flag);
52 }
53 
has_width() const54 bool NetExpr::has_width() const
55 {
56       return true;
57 }
58 
59 /*
60  * the grand default data type is a logic vector.
61  */
expr_type() const62 ivl_variable_type_t NetExpr::expr_type() const
63 {
64       if (net_type_)
65 	    return net_type_->base_type();
66       else
67 	    return IVL_VT_LOGIC;
68 }
69 
enumeration() const70 const netenum_t*NetExpr::enumeration() const
71 {
72       return 0;
73 }
74 
NetEArrayPattern(ivl_type_t lv_type,vector<NetExpr * > & items)75 NetEArrayPattern::NetEArrayPattern(ivl_type_t lv_type, vector<NetExpr*>&items)
76 : NetExpr(lv_type), items_(items)
77 {
78 }
79 
~NetEArrayPattern()80 NetEArrayPattern::~NetEArrayPattern()
81 {
82       for (size_t idx = 0 ; idx < items_.size() ; idx += 1)
83 	    delete items_[idx];
84 }
85 
86 /*
87  * Create an add/sub node from the two operands.
88  */
NetEBAdd(char op__,NetExpr * l,NetExpr * r,unsigned wid,bool signed_flag)89 NetEBAdd::NetEBAdd(char op__, NetExpr*l, NetExpr*r, unsigned wid, bool signed_flag)
90 : NetEBinary(op__, l, r, wid, signed_flag)
91 {
92 }
93 
~NetEBAdd()94 NetEBAdd::~NetEBAdd()
95 {
96 }
97 
expr_type() const98 ivl_variable_type_t NetEBAdd::expr_type() const
99 {
100       if (left_->expr_type() == IVL_VT_REAL)
101 	    return IVL_VT_REAL;
102 
103       if (right_->expr_type() == IVL_VT_REAL)
104 	    return IVL_VT_REAL;
105 
106       return IVL_VT_LOGIC;
107 }
108 
109 /*
110  * Create a comparison operator with two sub-expressions.
111  */
NetEBComp(char op__,NetExpr * l,NetExpr * r)112 NetEBComp::NetEBComp(char op__, NetExpr*l, NetExpr*r)
113 : NetEBinary(op__, l, r, 1, false)
114 {
115 }
116 
~NetEBComp()117 NetEBComp::~NetEBComp()
118 {
119 }
120 
has_width() const121 bool NetEBComp::has_width() const
122 {
123       return true;
124 }
125 
expr_type() const126 ivl_variable_type_t NetEBComp::expr_type() const
127 {
128 	// Case compare always returns BOOL
129       if (op() == 'E' || op() == 'N')
130 	    return IVL_VT_BOOL;
131 
132       if (left()->expr_type() == IVL_VT_LOGIC)
133 	    return IVL_VT_LOGIC;
134 
135       if (right()->expr_type() == IVL_VT_LOGIC)
136 	    return IVL_VT_LOGIC;
137 
138       return IVL_VT_BOOL;
139 }
140 
NetEBDiv(char op__,NetExpr * l,NetExpr * r,unsigned wid,bool signed_flag)141 NetEBDiv::NetEBDiv(char op__, NetExpr*l, NetExpr*r, unsigned wid, bool signed_flag)
142 : NetEBinary(op__, l, r, wid, signed_flag)
143 {
144 }
145 
~NetEBDiv()146 NetEBDiv::~NetEBDiv()
147 {
148 }
149 
expr_type() const150 ivl_variable_type_t NetEBDiv::expr_type() const
151 {
152       if (left_->expr_type() == IVL_VT_REAL)
153 	    return IVL_VT_REAL;
154 
155       if (right_->expr_type() == IVL_VT_REAL)
156 	    return IVL_VT_REAL;
157 
158       return IVL_VT_LOGIC;
159 }
160 
NetEBMinMax(char op__,NetExpr * l,NetExpr * r,unsigned wid,bool signed_flag)161 NetEBMinMax::NetEBMinMax(char op__, NetExpr*l, NetExpr*r, unsigned wid, bool signed_flag)
162 : NetEBinary(op__, l, r, wid, signed_flag)
163 {
164 }
165 
~NetEBMinMax()166 NetEBMinMax::~NetEBMinMax()
167 {
168 }
169 
expr_type() const170 ivl_variable_type_t NetEBMinMax::expr_type() const
171 {
172       if (left_->expr_type() == IVL_VT_REAL)
173 	    return IVL_VT_REAL;
174       if (right_->expr_type() == IVL_VT_REAL)
175 	    return IVL_VT_REAL;
176 
177       return IVL_VT_LOGIC;
178 }
179 
NetEBMult(char op__,NetExpr * l,NetExpr * r,unsigned wid,bool signed_flag)180 NetEBMult::NetEBMult(char op__, NetExpr*l, NetExpr*r, unsigned wid, bool signed_flag)
181 : NetEBinary(op__, l, r, wid, signed_flag)
182 {
183 }
184 
~NetEBMult()185 NetEBMult::~NetEBMult()
186 {
187 }
188 
expr_type() const189 ivl_variable_type_t NetEBMult::expr_type() const
190 {
191       if (left_->expr_type() == IVL_VT_REAL)
192 	    return IVL_VT_REAL;
193 
194       if (right_->expr_type() == IVL_VT_REAL)
195 	    return IVL_VT_REAL;
196 
197       return IVL_VT_LOGIC;
198 }
199 
NetEBPow(char op__,NetExpr * l,NetExpr * r,unsigned wid,bool signed_flag)200 NetEBPow::NetEBPow(char op__, NetExpr*l, NetExpr*r, unsigned wid, bool signed_flag)
201 : NetEBinary(op__, l, r, wid, signed_flag)
202 {
203 }
204 
~NetEBPow()205 NetEBPow::~NetEBPow()
206 {
207 }
208 
expr_type() const209 ivl_variable_type_t NetEBPow::expr_type() const
210 {
211       if (right_->expr_type() == IVL_VT_REAL)
212 	    return IVL_VT_REAL;
213       if (left_->expr_type() == IVL_VT_REAL)
214 	    return IVL_VT_REAL;
215 
216       return IVL_VT_LOGIC;
217 }
218 
NetEBShift(char op__,NetExpr * l,NetExpr * r,unsigned wid,bool signed_flag)219 NetEBShift::NetEBShift(char op__, NetExpr*l, NetExpr*r, unsigned wid, bool signed_flag)
220 : NetEBinary(op__, l, r, wid, signed_flag)
221 {
222 }
223 
~NetEBShift()224 NetEBShift::~NetEBShift()
225 {
226 }
227 
has_width() const228 bool NetEBShift::has_width() const
229 {
230       return left_->has_width();
231 }
232 
NetEConcat(unsigned cnt,unsigned r,ivl_variable_type_t vt)233 NetEConcat::NetEConcat(unsigned cnt, unsigned r, ivl_variable_type_t vt)
234 : parms_(cnt), repeat_(r), expr_type_(vt)
235 {
236       expr_width(0);
237 }
238 
~NetEConcat()239 NetEConcat::~NetEConcat()
240 {
241       for (unsigned idx = 0 ;  idx < parms_.size() ;  idx += 1)
242 	    delete parms_[idx];
243 }
244 
expr_type() const245 ivl_variable_type_t NetEConcat::expr_type() const
246 {
247       return expr_type_;
248 }
249 
has_width() const250 bool NetEConcat::has_width() const
251 {
252       return true;
253 }
254 
set(unsigned idx,NetExpr * e)255 void NetEConcat::set(unsigned idx, NetExpr*e)
256 {
257       assert(idx < parms_.size());
258       assert(parms_[idx] == 0);
259       parms_[idx] = e;
260       expr_width( expr_width() + repeat_ * e->expr_width() );
261 }
262 
NetEConstEnum(Definitions * s,perm_string n,const netenum_t * eset,const verinum & v)263 NetEConstEnum::NetEConstEnum(Definitions*s, perm_string n, const netenum_t*eset, const verinum&v)
264 : NetEConst(v), scope_(s), enum_set_(eset), name_(n)
265 {
266       assert(has_width());
267 }
268 
~NetEConstEnum()269 NetEConstEnum::~NetEConstEnum()
270 {
271 }
272 
enumeration() const273 const netenum_t*NetEConstEnum::enumeration() const
274 {
275       return enum_set_;
276 }
277 
NetECReal(const verireal & val)278 NetECReal::NetECReal(const verireal&val)
279 : value_(val)
280 {
281       expr_width(1);
282       cast_signed_base_(true);
283 }
284 
~NetECReal()285 NetECReal::~NetECReal()
286 {
287 }
288 
value() const289 const verireal& NetECReal::value() const
290 {
291       return value_;
292 }
293 
has_width() const294 bool NetECReal::has_width() const
295 {
296       return true;
297 }
298 
expr_type() const299 ivl_variable_type_t NetECReal::expr_type() const
300 {
301       return IVL_VT_REAL;
302 }
303 
NetECRealParam(NetScope * s,perm_string n,const verireal & v)304 NetECRealParam::NetECRealParam(NetScope*s, perm_string n, const verireal&v)
305 : NetECReal(v), scope_(s), name_(n)
306 {
307 }
308 
~NetECRealParam()309 NetECRealParam::~NetECRealParam()
310 {
311 }
312 
name() const313 perm_string NetECRealParam::name() const
314 {
315       return name_;
316 }
317 
scope() const318 const NetScope* NetECRealParam::scope() const
319 {
320       return scope_;
321 }
322 
323 
NetELast(NetNet * s)324 NetELast::NetELast(NetNet*s)
325 : sig_(s)
326 {
327 }
328 
~NetELast()329 NetELast::~NetELast()
330 {
331 }
332 
expr_type() const333 ivl_variable_type_t NetELast::expr_type() const
334 {
335       return IVL_VT_BOOL;
336 }
337 
NetENetenum(const netenum_t * s)338 NetENetenum::NetENetenum(const netenum_t*s)
339 : netenum_(s)
340 {
341 }
342 
~NetENetenum()343 NetENetenum::~NetENetenum()
344 {
345 }
346 
netenum() const347 const netenum_t* NetENetenum::netenum() const
348 {
349       return netenum_;
350 }
351 
NetENew(ivl_type_t t)352 NetENew::NetENew(ivl_type_t t)
353 : obj_type_(t), size_(0), init_val_(0)
354 {
355 }
356 
NetENew(ivl_type_t t,NetExpr * size,NetExpr * init_val)357 NetENew::NetENew(ivl_type_t t, NetExpr*size, NetExpr*init_val)
358 : obj_type_(t), size_(size), init_val_(init_val)
359 {
360 }
361 
~NetENew()362 NetENew::~NetENew()
363 {
364 }
365 
expr_type() const366 ivl_variable_type_t NetENew::expr_type() const
367 {
368       return size_ ? IVL_VT_DARRAY : IVL_VT_CLASS;
369 }
370 
NetENull()371 NetENull::NetENull()
372 {
373 }
374 
~NetENull()375 NetENull::~NetENull()
376 {
377 }
378 
NetEProperty(NetNet * net,perm_string pnam,NetExpr * idx)379 NetEProperty::NetEProperty(NetNet*net, perm_string pnam, NetExpr*idx)
380 : net_(net), index_(idx)
381 {
382       const netclass_t*use_type = dynamic_cast<const netclass_t*>(net->net_type());
383       assert(use_type);
384 
385       pidx_ = use_type->property_idx_from_name(pnam);
386       ivl_type_t prop_type = use_type->get_prop_type(pidx_);
387       expr_width(prop_type->packed_width());
388       cast_signed(prop_type->get_signed());
389 }
390 
~NetEProperty()391 NetEProperty::~NetEProperty()
392 {
393 }
394 
expr_type() const395 ivl_variable_type_t NetEProperty::expr_type() const
396 {
397       const netclass_t*use_type = dynamic_cast<const netclass_t*>(net_->net_type());
398       assert(use_type);
399 
400       ivl_type_t prop_type = use_type->get_prop_type(pidx_);
401       return prop_type->base_type();
402 }
403 
NetESelect(NetExpr * exp,NetExpr * base,unsigned wid,ivl_select_type_t sel_type)404 NetESelect::NetESelect(NetExpr*exp, NetExpr*base, unsigned wid,
405                        ivl_select_type_t sel_type)
406 : expr_(exp), base_(base), sel_type_(sel_type)
407 {
408       expr_width(wid);
409 }
410 
~NetESelect()411 NetESelect::~NetESelect()
412 {
413       delete expr_;
414       delete base_;
415 }
416 
sub_expr() const417 const NetExpr*NetESelect::sub_expr() const
418 {
419       return expr_;
420 }
421 
select() const422 const NetExpr*NetESelect::select() const
423 {
424       return base_;
425 }
426 
select_type() const427 ivl_select_type_t NetESelect::select_type() const
428 {
429       return sel_type_;
430 }
431 
expr_type() const432 ivl_variable_type_t NetESelect::expr_type() const
433 {
434       ivl_variable_type_t type = expr_->expr_type();
435 
436 	// Special case: If the sub-expression is an IVL_VT_STRING,
437 	// then this node is representing a character select. The
438 	// width is the width of a byte, and the data type is BOOL.
439       if (type == IVL_VT_STRING && expr_width()==8)
440 	    return IVL_VT_BOOL;
441 
442       if (type != IVL_VT_DARRAY)
443 	    return type;
444 
445       ivl_assert(*this, type == IVL_VT_DARRAY);
446 
447 	// Special case: If the expression is a DARRAY, then the
448 	// sub-expression must be a NetESignal and the type of the
449 	// NetESelect expression is the element type of the arrayed signal.
450       NetESignal*sig = dynamic_cast<NetESignal*>(expr_);
451       ivl_assert(*this, sig);
452       const netarray_t*array_type = dynamic_cast<const netarray_t*> (sig->sig()->net_type());
453       ivl_assert(*this, array_type);
454 
455       return array_type->element_type()->base_type();
456 }
457 
has_width() const458 bool NetESelect::has_width() const
459 {
460       return true;
461 }
462 
NetESFunc(const char * n,ivl_variable_type_t t,unsigned width,unsigned np,bool is_overridden)463 NetESFunc::NetESFunc(const char*n, ivl_variable_type_t t,
464 		     unsigned width, unsigned np, bool is_overridden)
465 : name_(0), type_(t), enum_type_(0), parms_(np), is_overridden_(is_overridden)
466 {
467       name_ = lex_strings.add(n);
468       expr_width(width);
469 }
470 
NetESFunc(const char * n,ivl_type_t rtype,unsigned np)471 NetESFunc::NetESFunc(const char*n, ivl_type_t rtype, unsigned np)
472 : NetExpr(rtype), name_(0), type_(IVL_VT_NO_TYPE), enum_type_(0), parms_(np), is_overridden_(false)
473 {
474       name_ = lex_strings.add(n);
475       expr_width(rtype->packed_width());
476 	// FIXME: For now, assume that all uses of this constructor
477 	// are for the IVL_VT_DARRAY type. Eventually, the type_
478 	// member will go away.
479       if (dynamic_cast<const netdarray_t*>(rtype))
480 	    type_ = IVL_VT_DARRAY;
481       else if (dynamic_cast<const netclass_t*>(rtype))
482 	    type_ = IVL_VT_CLASS;
483       else
484 	    ivl_assert(*this, 0);
485 }
486 
NetESFunc(const char * n,const netenum_t * enum_type,unsigned np)487 NetESFunc::NetESFunc(const char*n, const netenum_t*enum_type, unsigned np)
488 : name_(0), type_(enum_type->base_type()), enum_type_(enum_type), parms_(np), is_overridden_(false)
489 {
490       name_ = lex_strings.add(n);
491       expr_width(enum_type->packed_width());
492 }
493 
~NetESFunc()494 NetESFunc::~NetESFunc()
495 {
496       for (unsigned idx = 0 ;  idx < parms_.size() ;  idx += 1)
497 	    if (parms_[idx]) delete parms_[idx];
498 
499 	/* name_ string ls lex_strings allocated. */
500 }
501 
name() const502 const char* NetESFunc::name() const
503 {
504       return name_;
505 }
506 
nparms() const507 unsigned NetESFunc::nparms() const
508 {
509       return parms_.size();
510 }
511 
parm(unsigned idx,NetExpr * v)512 void NetESFunc::parm(unsigned idx, NetExpr*v)
513 {
514       assert(idx < parms_.size());
515       if (parms_[idx])
516 	    delete parms_[idx];
517       parms_[idx] = v;
518 }
519 
parm(unsigned idx) const520 const NetExpr* NetESFunc::parm(unsigned idx) const
521 {
522       assert(idx < parms_.size());
523       return parms_[idx];
524 }
525 
parm(unsigned idx)526 NetExpr* NetESFunc::parm(unsigned idx)
527 {
528       assert(idx < parms_.size());
529       return parms_[idx];
530 }
531 
expr_type() const532 ivl_variable_type_t NetESFunc::expr_type() const
533 {
534       return type_;
535 }
536 
enumeration() const537 const netenum_t* NetESFunc::enumeration() const
538 {
539       return enum_type_;
540 }
541 
NetEShallowCopy(NetExpr * arg1,NetExpr * arg2)542 NetEShallowCopy::NetEShallowCopy(NetExpr*arg1, NetExpr*arg2)
543 : arg1_(arg1), arg2_(arg2)
544 {
545 }
546 
~NetEShallowCopy()547 NetEShallowCopy::~NetEShallowCopy()
548 {
549 }
550 
expr_type() const551 ivl_variable_type_t NetEShallowCopy::expr_type() const
552 {
553       return arg1_->expr_type();
554 }
555 
NetEAccess(NetBranch * br,ivl_nature_t nat)556 NetEAccess::NetEAccess(NetBranch*br, ivl_nature_t nat)
557 : branch_(br), nature_(nat)
558 {
559 }
560 
~NetEAccess()561 NetEAccess::~NetEAccess()
562 {
563 }
564 
expr_type() const565 ivl_variable_type_t NetEAccess::expr_type() const
566 {
567       return IVL_VT_REAL;
568 }
569