Fix botched processing of MUX with constant select.
[sverilog.git] / netlist.h
blob691311301e513de31f55c4c78c3f537fe123d51e
1 #ifndef __netlist_H
2 #define __netlist_H
3 /*
4 * Copyright (c) 1998-2008 Stephen Williams (steve@icarus.com)
6 * This source code is free software; you can redistribute it
7 * and/or modify it in source code form under the terms of the GNU
8 * General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 * The netlist types, as described in this header file, are intended
24 * to be the output from elaboration of the source design. The design
25 * can be passed around in this form to the various stages and design
26 * processors.
28 # include <string>
29 # include <map>
30 # include <list>
31 # include <vector>
32 # include "ivl_target.h"
33 # include "pform_types.h"
34 # include "config.h"
35 # include "verinum.h"
36 # include "verireal.h"
37 # include "StringHeap.h"
38 # include "HName.h"
39 # include "LineInfo.h"
40 # include "svector.h"
41 # include "Attrib.h"
43 #ifdef HAVE_IOSFWD
44 # include <iosfwd>
45 #else
46 class ostream;
47 #endif
49 class Design;
50 class Link;
51 class Nexus;
52 class NetEvent;
53 class NetNet;
54 class NetNode;
55 class NetProc;
56 class NetProcTop;
57 class NetRelease;
58 class NetScope;
59 class NetEvProbe;
60 class NetExpr;
61 class NetESignal;
62 class NetFuncDef;
63 class NetRamDq;
64 class NetTaskDef;
65 class NetEvTrig;
66 class NetEvWait;
69 struct target;
70 struct functor_t;
72 ostream& operator << (ostream&o, ivl_variable_type_t val);
74 /* =========
75 * A NetObj is anything that has any kind of behavior in the
76 * netlist. Nodes can be gates, registers, etc. and are linked
77 * together to form a design web.
79 * The web of nodes that makes up a circuit is held together by the
80 * Link class. There is a link for each pin. All mutually connected
81 * pins form a ring of links.
83 * A link can be INPUT, OUTPUT or PASSIVE. An input never drives the
84 * signal, and PASSIVE never receives the value of the signal. Wires
85 * are PASSIVE, for example.
87 * A NetObj also has delays specified as rise_time, fall_time and
88 * decay_time. The rise and fall time are the times to transition to 1
89 * or 0 values. The decay_time is the time needed to decay to a 'bz
90 * value, or to decay of the net is a trireg. The exact and precise
91 * interpretation of the rise/fall/decay times is typically left to
92 * the target to properly interpret.
94 class NetObj : public Attrib, public virtual LineInfo {
96 public:
97 public:
98 // The name of the object must be a permallocated string. A
99 // lex_strings string, for example.
100 explicit NetObj(NetScope*s, perm_string n, unsigned npins);
101 virtual ~NetObj();
103 NetScope* scope();
104 const NetScope* scope() const;
106 perm_string name() const { return name_; }
108 unsigned pin_count() const { return npins_; }
110 const NetExpr* rise_time() const { return delay1_; }
111 const NetExpr* fall_time() const { return delay2_; }
112 const NetExpr* decay_time() const { return delay3_; }
114 void rise_time(const NetExpr* d) { delay1_ = d; }
115 void fall_time(const NetExpr* d) { delay2_ = d; }
116 void decay_time(const NetExpr* d) { delay3_ = d; }
118 Link&pin(unsigned idx);
119 const Link&pin(unsigned idx) const;
121 void dump_node_pins(ostream&, unsigned) const;
122 void dump_obj_attr(ostream&, unsigned) const;
124 private:
125 NetScope*scope_;
126 perm_string name_;
127 Link*pins_;
128 const unsigned npins_;
129 const NetExpr* delay1_;
130 const NetExpr* delay2_;
131 const NetExpr* delay3_;
134 class Link {
136 friend void connect(Link&, Link&);
137 friend void connect(Nexus*, Link&);
138 friend class NetObj;
139 friend class Nexus;
141 public:
142 enum DIR { PASSIVE, INPUT, OUTPUT };
144 enum strength_t { HIGHZ, WEAK, PULL, STRONG, SUPPLY };
146 Link();
147 ~Link();
149 // Manipulate the link direction.
150 void set_dir(DIR d);
151 DIR get_dir() const;
153 // Set the delay for all the drivers to this nexus.
154 void drivers_delays(NetExpr*rise, NetExpr*fall, NetExpr*decay);
156 // A link has a drive strength for 0 and 1 values. The drive0
157 // strength is for when the link has the value 0, and drive1
158 // strength is for when the link has a value 1.
159 void drive0(strength_t);
160 void drive1(strength_t);
162 strength_t drive0() const;
163 strength_t drive1() const;
165 // A link has an initial value that is used by the nexus to
166 // figure out its initial value. Normally, only the object
167 // that contains the link sets the initial value, and only the
168 // attached Nexus gets it. The default link value is Vx.
169 void set_init(verinum::V val);
170 verinum::V get_init() const;
172 void cur_link(NetObj*&net, unsigned &pin);
173 void cur_link(const NetObj*&net, unsigned &pin) const;
175 // Get a pointer to the nexus that represents all the links
176 // connected to me.
177 Nexus* nexus();
178 const Nexus* nexus()const;
180 // Return a pointer to the next link in the nexus.
181 Link* next_nlink();
182 const Link* next_nlink() const;
184 // Remove this link from the set of connected pins. The
185 // destructor will automatically do this if needed.
186 void unlink();
188 // Return true if this link is connected to anything else.
189 bool is_linked() const;
191 // Return true if these pins are connected.
192 bool is_linked(const Link&that) const;
194 // Return true if this is the same pin of the same object of
195 // that link.
196 bool is_equal(const Link&that) const;
198 // Return information about the object that this link is
199 // a part of.
200 const NetObj*get_obj() const;
201 NetObj*get_obj();
202 unsigned get_pin() const;
204 // A link of an object (sometimes called a "pin") has a
205 // name. It is convenient for the name to have a string and an
206 // integer part.
207 void set_name(perm_string, unsigned inst =0);
208 perm_string get_name() const;
209 unsigned get_inst() const;
211 private:
212 // The NetNode manages these. They point back to the
213 // NetNode so that following the links can get me here.
214 NetObj *node_;
215 unsigned pin_;
217 DIR dir_;
218 strength_t drive0_, drive1_;
219 verinum::V init_;
221 // These members name the pin of the link. If the name
222 // has width, then the inst_ member is the index of the
223 // pin.
224 perm_string name_;
225 unsigned inst_;
227 private:
228 Link *next_;
229 Nexus*nexus_;
231 private: // not implemented
232 Link(const Link&);
233 Link& operator= (const Link&);
238 * The Nexus represents a collection of links that are joined
239 * together. Each link has its own properties, this class holds the
240 * properties of the group.
242 * The links in a nexus are grouped into a singly linked list, with
243 * the nexus pointing to the first Link. Each link in turn points to
244 * the next link in the nexus, with the last link pointing to 0.
246 * The t_cookie() is a void* that targets can use to store information
247 * in a Nexus. ivl guarantees that the t_cookie will be 0 when the
248 * target is invoked.
250 class Nexus {
252 friend void connect(Link&, Link&);
253 friend void connect(Nexus*, Link&);
254 friend class Link;
256 public:
257 explicit Nexus();
258 ~Nexus();
260 const char* name() const;
261 verinum::V get_init() const;
263 void drivers_delays(NetExpr*rise, NetExpr*fall, NetExpr*decay);
265 Link*first_nlink();
266 const Link* first_nlink()const;
268 /* Get the width of the Nexus, or 0 if there are no vectors
269 (in the form of NetNet objects) linked. */
270 unsigned vector_width() const;
272 NetNet* pick_any_net();
274 /* This method returns true if all the possible drivers of
275 this nexus are constant. It will also return true if there
276 are no drivers at all. */
277 bool drivers_constant() const;
279 /* Given the nexus has constant drivers, this method returns
280 the value that has been driven. */
281 verinum::V driven_value() const;
283 ivl_nexus_t t_cookie() const;
284 ivl_nexus_t t_cookie(ivl_nexus_t) const;
286 private:
287 Link*list_;
288 void unlink(Link*);
289 void relink(Link*);
291 mutable char* name_; /* Cache the calculated name for the Nexus. */
292 mutable ivl_nexus_t t_cookie_;
294 enum VALUE { NO_GUESS, V0, V1, Vx, Vz, VAR };
295 mutable VALUE driven_;
297 private: // not implemented
298 Nexus(const Nexus&);
299 Nexus& operator= (const Nexus&);
302 class NexusSet {
304 public:
305 ~NexusSet();
306 NexusSet();
308 unsigned count() const;
310 // Add the nexus to the set, if it is not already present.
311 void add(Nexus*that);
312 void add(const NexusSet&that);
314 // Remove the nexus from the set, if it is present.
315 void rem(Nexus*that);
316 void rem(const NexusSet&that);
318 Nexus* operator[] (unsigned idx) const;
320 // Return true if this set contains every nexus in that set.
321 bool contains(const NexusSet&that) const;
323 // Return true if this set contains any nexus in that set.
324 bool intersect(const NexusSet&that) const;
326 private:
327 Nexus**items_;
328 unsigned nitems_;
330 unsigned bsearch_(Nexus*that) const;
332 private: // not implemented
333 NexusSet(const NexusSet&);
334 NexusSet& operator= (const NexusSet&);
338 * A NetBus is a transparent device that is merely a bunch of pins
339 * used to tie some pins to. It is a convenient way to collect a
340 * bundle of pins and pass that bundle around.
342 class NetBus : public NetObj {
344 public:
345 NetBus(NetScope*scope, unsigned pin_count);
346 ~NetBus();
348 private: // not implemented
349 NetBus(const NetBus&);
350 NetBus& operator= (const NetBus&);
354 * A NetNode is a device of some sort, where each pin has a different
355 * meaning. (i.e., pin(0) is the output to an and gate.) NetNode
356 * objects are listed in the nodes_ of the Design object.
358 class NetNode : public NetObj {
360 public:
361 // The name parameter must be a permallocated string.
362 explicit NetNode(NetScope*s, perm_string n, unsigned npins);
364 virtual ~NetNode();
366 virtual bool emit_node(struct target_t*) const;
367 virtual void dump_node(ostream&, unsigned) const;
369 // This is used to scan a modifiable netlist, one node at a time.
370 virtual void functor_node(Design*, functor_t*);
372 private:
373 friend class Design;
374 NetNode*node_next_, *node_prev_;
375 Design*design_;
379 * A NetDelaySrc is an input-only device that calculates a path delay
380 * based on the time that the inputs change. This class is used by the
381 * NetNet class, and NetDelaySrc objects cannot exist outside of its
382 * association with NetNet objects.
384 class NetDelaySrc : public NetObj {
386 public:
387 explicit NetDelaySrc(NetScope*s, perm_string n, unsigned nsrc,
388 bool condit_src, bool conditional);
389 ~NetDelaySrc();
391 // These functions set the delays from the values in the
392 // source. These set_delays functions implement the various
393 // rules wrt collections of transitions.
395 // One transition specified.
396 void set_delays(uint64_t del);
397 // Two transitions: rise and fall
398 void set_delays(uint64_t rise, uint64_t fall);
399 // Three transitions
400 void set_delays(uint64_t rise, uint64_t fall, uint64_t tz);
401 void set_delays(uint64_t t01, uint64_t t10, uint64_t t0z,
402 uint64_t tz1, uint64_t t1z, uint64_t tz0);
403 void set_delays(uint64_t t01, uint64_t t10, uint64_t t0z,
404 uint64_t tz1, uint64_t t1z, uint64_t tz0,
405 uint64_t t0x, uint64_t tx1, uint64_t t1x,
406 uint64_t tx0, uint64_t txz, uint64_t tzx);
408 uint64_t get_delay(unsigned pe) const;
410 void set_posedge();
411 void set_negedge();
412 bool is_posedge() const;
413 bool is_negedge() const;
415 unsigned src_count() const;
416 Link&src_pin(unsigned);
417 const Link&src_pin(unsigned) const;
419 bool is_condit() const;
420 bool has_condit() const;
421 Link&condit_pin();
422 const Link&condit_pin() const;
424 void dump(ostream&, unsigned ind) const;
426 private:
427 uint64_t transition_delays_[12];
428 bool condit_flag_;
429 bool conditional_;
430 bool posedge_;
431 bool negedge_;
433 private: // Not implemented
434 NetDelaySrc(const NetDelaySrc&);
435 NetDelaySrc& operator= (const NetDelaySrc&);
439 * NetNet is a special kind of NetObj that doesn't really do anything,
440 * but carries the properties of the wire/reg/trireg, including its
441 * name. Scalars and vectors are all the same thing here, a NetNet
442 * with a single pin. The difference between a scalar and vector is
443 * the width of the atomic vector datum it carries.
445 * NetNet objects can also appear as side effects of synthesis or
446 * other abstractions.
448 * Note that INTEGER types are an alias for a ``reg signed [31:0]''.
450 * NetNet objects have a name and exist within a scope, so the
451 * constructor takes a pointer to the containing scope. The object
452 * automatically adds itself to the scope.
454 * NetNet objects are located by searching NetScope objects.
456 * The pin of a NetNet object are PASSIVE: they do not drive
457 * anything and they are not a data sink, per se. The pins follow the
458 * values on the nexus.
460 class NetNet : public NetObj {
462 public:
463 enum Type { NONE, IMPLICIT, IMPLICIT_REG, INTEGER, WIRE, TRI, TRI1,
464 SUPPLY0, SUPPLY1, WAND, TRIAND, TRI0, WOR, TRIOR, REG,
465 WONE };
467 enum PortType { NOT_A_PORT, PIMPLICIT, PINPUT, POUTPUT, PINOUT };
469 // The width in this case is a shorthand for ms=width-1 and
470 // ls=0. Only one pin is created, the width is of the vector
471 // that passed through.
472 explicit NetNet(NetScope*s, perm_string n, Type t, unsigned width =1);
474 // This form supports an array of vectors. The [ms:ls] define
475 // the base vector, and the [s0:e0] define the array
476 // dimensions. If s0==e0, then this is not an array after
477 // all.
478 explicit NetNet(NetScope*s, perm_string n, Type t,
479 long ms, long ls);
480 explicit NetNet(NetScope*s, perm_string n, Type t,
481 long ms, long ls, long s0, long e0);
483 virtual ~NetNet();
485 Type type() const;
486 void type(Type t);
488 PortType port_type() const;
489 void port_type(PortType t);
491 ivl_variable_type_t data_type() const;
492 void data_type(ivl_variable_type_t t);
494 /* If a NetNet is signed, then its value is to be treated as
495 signed. Otherwise, it is unsigned. */
496 bool get_signed() const;
497 void set_signed(bool);
499 /* Used to maintain original type of net since integers are
500 implemented as 'reg signed [31:0]' in Icarus */
501 bool get_isint() const;
502 void set_isint(bool);
504 /* These methods return the msb and lsb indices for the most
505 significant and least significant bits. These are signed
506 longs, and may be different from pin numbers. For example,
507 reg [1:8] has 8 bits, msb==1 and lsb==8. */
508 long msb() const;
509 long lsb() const;
510 unsigned long vector_width() const;
512 /* This method converts a signed index (the type that might be
513 found in the Verilog source) to a pin number. It accounts
514 for variation in the definition of the reg/wire/whatever. */
515 long sb_to_idx(long sb) const;
517 /* This method checks that the signed index is valid for this
518 signal. If it is, the above sb_to_idx can be used to get
519 the pin# from the index. */
520 bool sb_is_valid(long sb) const;
522 /* This method returns 0 for scalars and vectors, and greater
523 for arrays. The value is the number of array
524 indices. (Currently only one array index is supported.) */
525 unsigned array_dimensions() const;
526 long array_first() const;
528 // This is the number of array elements.
529 unsigned array_count() const;
531 // This method returns a 0 based address of an array entry as
532 // indexed by idx. The Verilog source may give index ranges
533 // that are not zero based.
534 bool array_index_is_valid(long idx) const;
535 unsigned array_index_to_address(long idx) const;
537 bool local_flag() const { return local_flag_; }
538 void local_flag(bool f) { local_flag_ = f; }
540 /* NetESignal objects may reference this object. Keep a
541 reference count so that I keep track of them. */
542 void incr_eref();
543 void decr_eref();
544 unsigned peek_eref() const;
546 /* Assignment statements count their lrefs here. */
547 void incr_lref();
548 void decr_lref();
549 unsigned peek_lref() const;
551 unsigned get_refs() const;
553 /* Manage path delays */
554 void add_delay_path(class NetDelaySrc*path);
555 unsigned delay_paths(void) const;
556 const class NetDelaySrc*delay_path(unsigned idx) const;
558 virtual void dump_net(ostream&, unsigned) const;
560 private:
561 // The NetScope class uses this for listing signals.
562 friend class NetScope;
563 NetNet*sig_next_, *sig_prev_;
565 private:
566 Type type_;
567 PortType port_type_;
568 ivl_variable_type_t data_type_;
569 bool signed_;
570 bool isint_; // original type of integer
572 long msb_, lsb_;
573 const unsigned dimensions_;
574 long s0_, e0_;
576 bool local_flag_;
577 unsigned eref_count_;
578 unsigned lref_count_;
580 vector<class NetDelaySrc*> delay_paths_;
584 * This object type is used to contain a logical scope within a
585 * design. The scope doesn't represent any executable hardware, but is
586 * just a handle that netlist processors can use to grab at the design.
588 class NetScope : public Attrib {
590 public:
591 enum TYPE { MODULE, TASK, FUNC, BEGIN_END, FORK_JOIN, GENBLOCK };
593 /* Create a new scope, and attach it to the given parent. The
594 name is expected to have been permallocated. */
595 NetScope(NetScope*up, const hname_t&name, TYPE t);
596 ~NetScope();
598 /* Rename the scope using the name generated by inserting as
599 many pad characters as required between prefix and suffix
600 to make the name unique in the parent scope. Return false
601 if a unique name couldn't be generated. */
602 bool auto_name(const char* prefix, char pad, const char* suffix);
604 /* Parameters exist within a scope, and these methods allow
605 one to manipulate the set. In these cases, the name is the
606 *simple* name of the parameter, the hierarchy is implicit in
607 the scope. The return value from set_parameter is the
608 previous expression, if there was one. */
610 struct range_t;
611 NetExpr* set_parameter(perm_string name, NetExpr*val,
612 ivl_variable_type_t type,
613 NetExpr*msb, NetExpr*lsb, bool signed_flag,
614 NetScope::range_t*range_list,
615 const LineInfo&file_line);
616 NetExpr* set_localparam(perm_string name, NetExpr*val,
617 const LineInfo&file_line);
619 const NetExpr*get_parameter(const char* name,
620 const NetExpr*&msb,
621 const NetExpr*&lsb) const;
623 /* These are used by defparam elaboration to replace the
624 expression with a new expression, without affecting the
625 range or signed_flag. Return false if the name does not
626 exist. */
627 bool replace_parameter(perm_string name, NetExpr*val);
629 /* These methods set or access events that live in this
630 scope. */
632 void add_event(NetEvent*);
633 void rem_event(NetEvent*);
634 NetEvent*find_event(perm_string name);
637 /* These methods manage signals. The add_ and rem_signal
638 methods are used by the NetNet objects to make themselves
639 available to the scope, and the find_signal method can be
640 used to locate signals within a scope. */
642 void add_signal(NetNet*);
643 void rem_signal(NetNet*);
644 NetNet* find_signal(perm_string name);
646 /* The parent and child() methods allow users of NetScope
647 objects to locate nearby scopes. */
648 NetScope* parent();
649 NetScope* child(const hname_t&name);
650 const NetScope* parent() const;
651 const NetScope* child(const hname_t&name) const;
653 TYPE type() const;
655 void set_task_def(NetTaskDef*);
656 void set_func_def(NetFuncDef*);
657 void set_module_name(perm_string);
659 NetTaskDef* task_def();
660 NetFuncDef* func_def();
662 void set_line(perm_string file, perm_string def_file,
663 unsigned lineno, unsigned def_lineno);
664 void set_line(perm_string file, unsigned lineno);
665 void set_line(const LineInfo *info);
666 perm_string get_file() const { return file_; };
667 perm_string get_def_file() const { return def_file_; };
668 unsigned get_lineno() const { return lineno_; };
669 unsigned get_def_lineno() const { return def_lineno_; };
671 bool in_func();
673 const NetTaskDef* task_def() const;
674 const NetFuncDef* func_def() const;
676 /* If the scope represents a module instance, the module_name
677 is the name of the module itself. */
678 perm_string module_name() const;
680 /* Scopes have their own time units and time precision. The
681 unit and precision are given as power of 10, i.e., -3 is
682 units of milliseconds.
684 If a NetScope is created with a parent scope, the new scope
685 will initially inherit the unit and precision of the
686 parent scope. */
688 void time_unit(int);
689 void time_precision(int);
691 int time_unit() const;
692 int time_precision() const;
694 void default_nettype(NetNet::Type);
695 NetNet::Type default_nettype() const;
697 /* The fullname of the scope is the hierarchical name
698 component (which includes the name and array index) whereas
699 the basename is just my name. */
700 perm_string basename() const;
701 const hname_t& fullname() const { return name_; }
703 void run_defparams(class Design*);
704 void evaluate_parameters(class Design*);
706 /* This method generates a non-hierarchical name that is
707 guaranteed to be unique within this scope. */
708 perm_string local_symbol();
710 void dump(ostream&) const;
711 void emit_scope(struct target_t*tgt) const;
712 bool emit_defs(struct target_t*tgt) const;
714 /* This method runs the functor on me. Recurse through the
715 children of this node as well. */
716 void run_functor(Design*des, functor_t*fun);
719 /* This member is used during elaboration to pass defparam
720 assignments from the scope pass to the parameter evaluation
721 step. After that, it is not used. */
723 map<pform_name_t,NetExpr*>defparams;
725 public:
726 struct range_t {
727 bool exclude_flag;
728 // Lower bound
729 bool low_open_flag;
730 NetExpr*low_expr;
731 // Upper bound
732 bool high_open_flag;
733 NetExpr*high_expr;
734 // Link to the next range specification
735 struct range_t*next;
738 /* After everything is all set up, the code generators like
739 access to these things to make up the parameter lists. */
740 struct param_expr_t : public LineInfo {
741 param_expr_t() : type(IVL_VT_NO_TYPE), range(0) { }
742 // Type information
743 ivl_variable_type_t type;
744 bool signed_flag;
745 NetExpr*msb;
746 NetExpr*lsb;
747 // range constraints
748 struct range_t*range;
749 // Expression value
750 NetExpr*expr;
752 map<perm_string,param_expr_t>parameters;
753 map<perm_string,param_expr_t>localparams;
755 typedef map<perm_string,param_expr_t>::iterator param_ref_t;
757 param_ref_t find_parameter(perm_string name);
760 struct spec_val_t {
761 ivl_variable_type_t type;
762 union {
763 double real_val; // type == IVL_VT_REAL
764 long integer; // type == IVL_VT_BOOL
767 map<perm_string,spec_val_t>specparams;
769 /* Module instance arrays are collected here for access during
770 the multiple elaboration passes. */
771 typedef svector<NetScope*> scope_vec_t;
772 map<perm_string, scope_vec_t>instance_arrays;
774 /* Loop generate uses this as scratch space during
775 elaboration. Expression evaluation can use this to match
776 names. */
777 perm_string genvar_tmp;
778 long genvar_tmp_val;
780 private:
781 void evaluate_parameter_logic_(Design*des, param_ref_t cur);
782 void evaluate_parameter_real_(Design*des, param_ref_t cur);
784 private:
785 TYPE type_;
786 hname_t name_;
788 perm_string file_;
789 perm_string def_file_;
790 unsigned lineno_;
791 unsigned def_lineno_;
793 signed char time_unit_, time_prec_;
794 NetNet::Type default_nettype_;
796 NetEvent *events_;
797 NetNet *signals_;
798 perm_string module_name_;
799 union {
800 NetTaskDef*task_;
801 NetFuncDef*func_;
804 NetScope*up_;
805 NetScope*sib_;
806 NetScope*sub_;
808 unsigned lcounter_;
812 * This class implements the LPM_ABS component. The node has a single
813 * input, a signe expression, that it converts to the absolute
814 * value. The gate is simple: pin(0) is the output and pin(1) is the input.
816 class NetAbs : public NetNode {
818 public:
819 NetAbs(NetScope*s, perm_string n, unsigned width);
820 ~NetAbs();
822 unsigned width() const;
824 virtual void dump_node(ostream&, unsigned ind) const;
825 virtual bool emit_node(struct target_t*) const;
826 virtual void functor_node(Design*des, functor_t*fun);
828 private:
829 unsigned width_;
833 * This class implements the LPM_ADD_SUB component as described in the
834 * EDIF LPM Version 2 1 0 standard. It is used as a structural
835 * implementation of the + and - operators.
837 class NetAddSub : public NetNode {
839 public:
840 NetAddSub(NetScope*s, perm_string n, unsigned width);
841 ~NetAddSub();
843 // Get the width of the device (that is, the width of the
844 // operands and results).
845 unsigned width() const;
847 Link& pin_Aclr();
848 Link& pin_Add_Sub();
849 Link& pin_Clock();
850 Link& pin_Cin();
851 Link& pin_Cout();
852 Link& pin_Overflow();
854 Link& pin_DataA();
855 Link& pin_DataB();
856 Link& pin_Result();
858 const Link& pin_Cout() const;
859 const Link& pin_DataA() const;
860 const Link& pin_DataB() const;
861 const Link& pin_Result() const;
863 virtual void dump_node(ostream&, unsigned ind) const;
864 virtual bool emit_node(struct target_t*) const;
865 virtual void functor_node(Design*des, functor_t*fun);
867 private:
868 unsigned width_;
872 * The NetArrayDq node represents an array dereference. The NetNet
873 * that this object refers to is an array, and the Address pin selects
874 * which word of the array to place on the Result.
876 class NetArrayDq : public NetNode {
878 public:
879 NetArrayDq(NetScope*s, perm_string name, NetNet*mem, unsigned awid);
880 ~NetArrayDq();
882 unsigned width() const;
883 unsigned awidth() const;
884 unsigned size() const;
885 const NetNet*mem() const;
887 Link& pin_Address();
888 Link& pin_Result();
890 const Link& pin_Address() const;
891 const Link& pin_Result() const;
893 virtual void dump_node(ostream&, unsigned ind) const;
894 virtual bool emit_node(struct target_t*) const;
896 private:
897 NetNet*mem_;
898 unsigned awidth_;
903 * This type represents the LPM_CLSHIFT device.
905 class NetCLShift : public NetNode {
907 public:
908 NetCLShift(NetScope*s, perm_string n, unsigned width,
909 unsigned width_dist, bool right_flag, bool signed_flag);
910 ~NetCLShift();
912 unsigned width() const;
913 unsigned width_dist() const;
915 bool right_flag() const;
916 bool signed_flag() const;
918 Link& pin_Data();
919 Link& pin_Result();
920 Link& pin_Distance();
922 const Link& pin_Data() const;
923 const Link& pin_Result() const;
924 const Link& pin_Distance() const;
926 virtual void dump_node(ostream&, unsigned ind) const;
927 virtual bool emit_node(struct target_t*) const;
929 private:
930 unsigned width_;
931 unsigned width_dist_;
932 bool right_flag_;
933 bool signed_flag_;
937 * This class supports the LPM_COMPARE device.
939 * The width of the device is the width of the inputs. If one of the
940 * inputs is narrower then the other, it is up to the generator to
941 * make sure all the data pins are properly driven.
943 * NOTE: This is not the same as the device used to support case
944 * compare. Case comparisons handle Vx and Vz values, whereas this
945 * device need not.
947 class NetCompare : public NetNode {
949 public:
950 NetCompare(NetScope*scope, perm_string n, unsigned width);
951 ~NetCompare();
953 unsigned width() const;
955 bool get_signed() const;
956 void set_signed(bool);
958 Link& pin_Aclr();
959 Link& pin_Clock();
960 Link& pin_AGB();
961 Link& pin_AGEB();
962 Link& pin_AEB();
963 Link& pin_ANEB();
964 Link& pin_ALB();
965 Link& pin_ALEB();
967 Link& pin_DataA();
968 Link& pin_DataB();
970 const Link& pin_Aclr() const;
971 const Link& pin_Clock() const;
972 const Link& pin_AGB() const;
973 const Link& pin_AGEB() const;
974 const Link& pin_AEB() const;
975 const Link& pin_ANEB() const;
976 const Link& pin_ALB() const;
977 const Link& pin_ALEB() const;
979 const Link& pin_DataA() const;
980 const Link& pin_DataB() const;
982 virtual void functor_node(Design*, functor_t*);
983 virtual void dump_node(ostream&, unsigned ind) const;
984 virtual bool emit_node(struct target_t*) const;
986 private:
987 unsigned width_;
988 bool signed_flag_;
993 * This node is a means to connect net inputs together to form a wider
994 * vector. The output (pin 0) is a concatenation of the input vectors,
995 * with pin-1 at the LSB, pin-2 next, and so on. This node is most
996 * like the NetLogic node, as it has one output at pin 0 and the
997 * remaining pins are the input that are combined to make the
998 * output. It is separated out because it it generally a special case
999 * for the code generators.
1001 * When constructing the node, the width is the vector_width of the
1002 * output, and the cnt is the number of pins. (the number of input
1003 * vectors.)
1005 class NetConcat : public NetNode {
1007 public:
1008 NetConcat(NetScope*scope, perm_string n, unsigned wid, unsigned cnt);
1009 ~NetConcat();
1011 unsigned width() const;
1013 void dump_node(ostream&, unsigned ind) const;
1014 bool emit_node(struct target_t*) const;
1016 private:
1017 unsigned width_;
1022 * This class represents a theoretical (though not necessarily
1023 * practical) integer divider gate. This is not to represent any real
1024 * hardware, but to support the / operator in Verilog, when it shows
1025 * up in structural contexts.
1027 * The operands of the operation are the DataA<i> and DataB<i> inputs,
1028 * and the Result<i> output reflects the value DataA/DataB.
1031 class NetDivide : public NetNode {
1033 public:
1034 NetDivide(NetScope*scope, perm_string n,
1035 unsigned width, unsigned wa, unsigned wb);
1036 ~NetDivide();
1038 unsigned width_r() const;
1039 unsigned width_a() const;
1040 unsigned width_b() const;
1042 void set_signed(bool);
1043 bool get_signed() const;
1045 Link& pin_DataA();
1046 Link& pin_DataB();
1047 Link& pin_Result();
1049 const Link& pin_DataA() const;
1050 const Link& pin_DataB() const;
1051 const Link& pin_Result() const;
1053 virtual void dump_node(ostream&, unsigned ind) const;
1054 virtual bool emit_node(struct target_t*) const;
1055 virtual void functor_node(Design*des, functor_t*fun);
1057 private:
1058 unsigned width_r_;
1059 unsigned width_a_;
1060 unsigned width_b_;
1062 bool signed_flag_;
1066 * This class represents a theoretical (though not necessarily
1067 * practical) integer modulo gate. This is not to represent any real
1068 * hardware, but to support the % operator in Verilog, when it shows
1069 * up in structural contexts.
1071 * The operands of the operation are the DataA<i> and DataB<i> inputs,
1072 * and the Result<i> output reflects the value DataA%DataB.
1075 class NetModulo : public NetNode {
1077 public:
1078 NetModulo(NetScope*s, perm_string n,
1079 unsigned width, unsigned wa, unsigned wb);
1080 ~NetModulo();
1082 unsigned width_r() const;
1083 unsigned width_a() const;
1084 unsigned width_b() const;
1086 Link& pin_DataA();
1087 Link& pin_DataB();
1088 Link& pin_Result();
1090 const Link& pin_DataA() const;
1091 const Link& pin_DataB() const;
1092 const Link& pin_Result() const;
1094 virtual void dump_node(ostream&, unsigned ind) const;
1095 virtual bool emit_node(struct target_t*) const;
1096 virtual void functor_node(Design*des, functor_t*fun);
1098 private:
1099 unsigned width_r_;
1100 unsigned width_a_;
1101 unsigned width_b_;
1105 * This class represents an LPM_FF device. There is no literal gate
1106 * type in Verilog that maps, but gates of this type can be inferred.
1108 class NetFF : public NetNode {
1110 public:
1111 NetFF(NetScope*s, perm_string n, unsigned vector_width);
1112 ~NetFF();
1114 unsigned width() const;
1116 Link& pin_Clock();
1117 Link& pin_Enable();
1118 Link& pin_Aset();
1119 Link& pin_Aclr();
1120 Link& pin_Sset();
1121 Link& pin_Sclr();
1122 Link& pin_Data();
1123 Link& pin_Q();
1125 const Link& pin_Clock() const;
1126 const Link& pin_Enable() const;
1127 const Link& pin_Aset() const;
1128 const Link& pin_Aclr() const;
1129 const Link& pin_Sset() const;
1130 const Link& pin_Sclr() const;
1131 const Link& pin_Data() const;
1132 const Link& pin_Q() const;
1134 void aset_value(const verinum&val);
1135 const verinum& aset_value() const;
1137 void sset_value(const verinum&val);
1138 const verinum& sset_value() const;
1140 virtual void dump_node(ostream&, unsigned ind) const;
1141 virtual bool emit_node(struct target_t*) const;
1142 virtual void functor_node(Design*des, functor_t*fun);
1144 private:
1145 unsigned width_;
1146 verinum aset_value_;
1147 verinum sset_value_;
1151 * This class implements a basic LPM_MULT combinational multiplier. It
1152 * is used as a structural representation of the * operator. The
1153 * device has inputs A and B and output Result all with independent
1154 * widths.
1156 * NOTE: Check this width thing. I think that the independence of the
1157 * widths is not necessary or even useful.
1159 class NetMult : public NetNode {
1161 public:
1162 NetMult(NetScope*sc, perm_string n, unsigned width,
1163 unsigned wa, unsigned wb);
1164 ~NetMult();
1166 bool get_signed() const;
1167 void set_signed(bool);
1169 // Get the width of the device bussed inputs. There are these
1170 // parameterized widths:
1171 unsigned width_r() const; // Result
1172 unsigned width_a() const; // DataA
1173 unsigned width_b() const; // DataB
1175 Link& pin_DataA();
1176 Link& pin_DataB();
1177 Link& pin_Result();
1179 const Link& pin_DataA() const;
1180 const Link& pin_DataB() const;
1181 const Link& pin_Result() const;
1183 virtual void dump_node(ostream&, unsigned ind) const;
1184 virtual bool emit_node(struct target_t*) const;
1185 virtual void functor_node(Design*des, functor_t*fun);
1187 private:
1188 bool signed_;
1189 unsigned width_r_;
1190 unsigned width_a_;
1191 unsigned width_b_;
1196 * This class represents an LPM_MUX device. This device has some
1197 * number of Result points (the width of the device) and some number
1198 * of input choices. There is also a selector of some width. The
1199 * parameters are:
1201 * width -- Width of the result and each possible Data input
1202 * size -- Number of Data input (each of width)
1203 * selw -- Width in bits of the select input
1205 * All the data inputs must have the same type, and are the type of
1206 * the result. The actual type does not matter, as the mux does not
1207 * process data, just selects alternatives.
1209 * The select input must be an integral type of some sort. Not real.
1211 class NetMux : public NetNode {
1213 public:
1214 NetMux(NetScope*scope, perm_string n,
1215 unsigned width, unsigned size, unsigned selw);
1216 ~NetMux();
1218 unsigned width() const;
1219 unsigned size() const;
1220 unsigned sel_width() const;
1222 Link& pin_Result();
1223 Link& pin_Data(unsigned si);
1224 Link& pin_Sel();
1226 const Link& pin_Result() const;
1227 const Link& pin_Data(unsigned) const;
1228 const Link& pin_Sel() const;
1230 virtual void dump_node(ostream&, unsigned ind) const;
1231 virtual bool emit_node(struct target_t*) const;
1232 virtual void functor_node(Design*des, functor_t*fun);
1234 private:
1235 unsigned width_;
1236 unsigned size_;
1237 unsigned swidth_;
1242 * This class implements a basic LPM_POW combinational power. It
1243 * is used as a structural representation of the ** operator. The
1244 * device has inputs A and B and output Result all with independent
1245 * widths.
1247 * NOTE: Check this width thing. I think that the independence of the
1248 * widths is not necessary or even useful.
1250 class NetPow : public NetNode {
1252 public:
1253 NetPow(NetScope*sc, perm_string n, unsigned width,
1254 unsigned wa, unsigned wb);
1255 ~NetPow();
1257 bool get_signed() const;
1258 void set_signed(bool);
1260 // Get the width of the device bussed inputs. There are these
1261 // parameterized widths:
1262 unsigned width_r() const; // Result
1263 unsigned width_a() const; // DataA
1264 unsigned width_b() const; // DataB
1266 Link& pin_DataA();
1267 Link& pin_DataB();
1268 Link& pin_Result();
1270 const Link& pin_DataA() const;
1271 const Link& pin_DataB() const;
1272 const Link& pin_Result() const;
1274 virtual void dump_node(ostream&, unsigned ind) const;
1275 virtual bool emit_node(struct target_t*) const;
1276 virtual void functor_node(Design*des, functor_t*fun);
1278 private:
1279 bool signed_;
1280 unsigned width_r_;
1281 unsigned width_a_;
1282 unsigned width_b_;
1287 * The NetReplicate node takes a vector input and makes it into a larger
1288 * vector by repeating the input vector some number of times. The
1289 * repeat count is a fixed value. This is just like the repeat
1290 * concatenation of Verilog: {<repeat>{<vector>}}.
1292 * When constructing this node, the wid is the vector width of the
1293 * output, and the rpt is the repeat count. The wid must be an even
1294 * multiple of the cnt, and wid/cnt is the expected input width.
1296 * The device has exactly 2 pins: pin(0) is the output and pin(1) the
1297 * input.
1299 class NetReplicate : public NetNode {
1301 public:
1302 NetReplicate(NetScope*scope, perm_string n, unsigned wid, unsigned rpt);
1303 ~NetReplicate();
1305 unsigned width() const;
1306 unsigned repeat() const;
1308 void dump_node(ostream&, unsigned ind) const;
1309 bool emit_node(struct target_t*) const;
1311 private:
1312 unsigned width_;
1313 unsigned repeat_;
1317 * This node represents the call of a user defined function in a
1318 * structural context. The pin count is the same as the port count,
1319 * with pin0 the return value.
1321 class NetUserFunc : public NetNode {
1323 public:
1324 NetUserFunc(NetScope*s, perm_string n, NetScope*def);
1325 ~NetUserFunc();
1327 ivl_variable_type_t data_type(unsigned port) const;
1328 unsigned port_width(unsigned port) const;
1330 const NetScope* def() const;
1332 virtual void dump_node(ostream&, unsigned ind) const;
1333 virtual bool emit_node(struct target_t*) const;
1335 private:
1336 NetScope*def_;
1340 * The number of ports includes the return value, so will always be at
1341 * least 1.
1343 class NetSysFunc : public NetNode {
1345 public:
1346 NetSysFunc(NetScope*s, perm_string n,
1347 const struct sfunc_return_type*def,
1348 unsigned ports);
1349 ~NetSysFunc();
1351 ivl_variable_type_t data_type() const;
1352 unsigned vector_width() const;
1353 const char* func_name() const;
1355 virtual void dump_node(ostream&, unsigned ind) const;
1356 virtual bool emit_node(struct target_t*) const;
1358 private:
1359 const struct sfunc_return_type*def_;
1362 /* =========
1363 * There are cases where expressions need to be represented. The
1364 * NetExpr class is the root of a hierarchy that serves that purpose.
1366 * The expr_width() is the width of the expression, that accounts
1367 * for the widths of the sub-expressions I might have. It is up to the
1368 * derived classes to properly set the expr width, if need be. The
1369 * set_width() method is used to compel an expression to have a
1370 * certain width, and is used particularly when the expression is an
1371 * rvalue in an assignment statement.
1373 class NetExpr : public LineInfo {
1374 public:
1375 explicit NetExpr(unsigned w =0);
1376 virtual ~NetExpr() =0;
1378 virtual void expr_scan(struct expr_scan_t*) const =0;
1379 virtual void dump(ostream&) const;
1381 // Expressions have type.
1382 virtual ivl_variable_type_t expr_type() const;
1384 // How wide am I?
1385 unsigned expr_width() const { return width_; }
1387 // Coerce the expression to have a specific width. If the
1388 // coercion works, then return true. Otherwise, return false.
1389 // A coercion will work or not depending on the implementation
1390 // in the derived class. Normally, the width will be set if
1391 // the expression is:
1392 // - already the requested size, OR
1393 // - otherwise unsized.
1394 // Normally, the resize will not allow a width size that loses
1395 // data. For example, it will not reduce a constant expression
1396 // to the point where significant bits are lost. But if the
1397 // last_chance flag is true, then the method assumes that high
1398 // bits will be lost anyhow, so try harder. Loss will be
1399 // allowed, but it still won't resize fixed size expressions
1400 // such as vector signals. This flag is meant to be used by
1401 // elaboration of procedural assignment to set the expression
1402 // width to the l-value width, if possible.
1403 virtual bool set_width(unsigned wid, bool last_chance =false);
1405 // This method returns true if the expression is
1406 // signed. Unsigned expressions return false.
1407 bool has_sign() const;
1408 virtual void cast_signed(bool flag);
1410 // This returns true if the expression has a definite
1411 // width. This is generally true, but in some cases the
1412 // expression is amorphous and desires a width from its
1413 // environment. For example, 'd5 has indefinite width, but
1414 // 5'd5 has a definite width.
1416 // This method is only really used within concatenation
1417 // expressions to check validity.
1418 virtual bool has_width() const;
1421 // This method evaluates the expression and returns an
1422 // equivalent expression that is reduced as far as compile
1423 // time knows how. Essentially, this is designed to fold
1424 // constants.
1426 // The prune_to_width is the maximum width that the result
1427 // should be. If it is 0 or -1, then do not prune the
1428 // result. If it is -1, go through special efforts to preserve
1429 // values that may expand. A width of 0 corresponds to a
1430 // self-determined context, and a width of -1 corresponds to
1431 // an infinitely wide context.
1432 virtual NetExpr*eval_tree(int prune_to_width = -1);
1434 // Make a duplicate of myself, and subexpressions if I have
1435 // any. This is a deep copy operation.
1436 virtual NetExpr*dup_expr() const =0;
1438 // Get the Nexus that are the input to this
1439 // expression. Normally this descends down to the reference to
1440 // a signal that reads from its input.
1441 virtual NexusSet* nex_input(bool rem_out = true) =0;
1443 // Return a version of myself that is structural. This is used
1444 // for converting expressions to gates.
1445 virtual NetNet*synthesize(Design*);
1448 protected:
1449 void expr_width(unsigned w) { width_ = w; }
1450 void cast_signed_base_(bool flag) {signed_flag_ = flag; }
1452 private:
1453 unsigned width_;
1454 bool signed_flag_;
1456 private: // not implemented
1457 NetExpr(const NetExpr&);
1458 NetExpr& operator=(const NetExpr&);
1462 * The expression constant is slightly special, and is sometimes
1463 * returned from other classes that can be evaluated at compile
1464 * time. This class represents constant values in expressions.
1466 class NetEConst : public NetExpr {
1468 public:
1469 explicit NetEConst(const verinum&val);
1470 ~NetEConst();
1472 const verinum&value() const;
1474 virtual bool set_width(unsigned w, bool last_chance =false);
1475 virtual void cast_signed(bool sign_flag);
1476 virtual bool has_width() const;
1477 virtual ivl_variable_type_t expr_type() const;
1479 virtual void expr_scan(struct expr_scan_t*) const;
1480 virtual void dump(ostream&) const;
1482 virtual NetEConst* dup_expr() const;
1483 virtual NetNet*synthesize(Design*);
1484 virtual NexusSet* nex_input(bool rem_out = true);
1486 private:
1487 verinum value_;
1490 class NetEConstParam : public NetEConst {
1492 public:
1493 explicit NetEConstParam(NetScope*scope, perm_string name,
1494 const verinum&val);
1495 ~NetEConstParam();
1497 perm_string name() const;
1498 const NetScope*scope() const;
1500 virtual bool set_width(unsigned w, bool last_chance);
1501 virtual void expr_scan(struct expr_scan_t*) const;
1502 virtual void dump(ostream&) const;
1504 virtual NetEConstParam* dup_expr() const;
1506 private:
1507 NetScope*scope_;
1508 perm_string name_;
1512 * This class represents a constant real value.
1514 class NetECReal : public NetExpr {
1516 public:
1517 explicit NetECReal(const verireal&val);
1518 ~NetECReal();
1520 const verireal&value() const;
1522 // Reals can be used in vector expressions. Conversions will
1523 // be done at the right time.
1524 virtual bool set_width(unsigned w, bool last_chance);
1526 // This type has no self-determined width. This is false.
1527 virtual bool has_width() const;
1529 // The type of this expression is ET_REAL
1530 ivl_variable_type_t expr_type() const;
1532 virtual void expr_scan(struct expr_scan_t*) const;
1533 virtual void dump(ostream&) const;
1535 virtual NetECReal* dup_expr() const;
1536 virtual NetNet*synthesize(Design*);
1537 virtual NexusSet* nex_input(bool rem_out = true);
1539 private:
1540 verireal value_;
1543 class NetECRealParam : public NetECReal {
1545 public:
1546 explicit NetECRealParam(NetScope*scope, perm_string name,
1547 const verireal&val);
1548 ~NetECRealParam();
1550 perm_string name() const;
1551 const NetScope*scope() const;
1553 virtual void expr_scan(struct expr_scan_t*) const;
1554 virtual void dump(ostream&) const;
1556 virtual NetECRealParam* dup_expr() const;
1558 private:
1559 NetScope*scope_;
1560 perm_string name_;
1564 * The NetPartSelect device represents a netlist part select of a
1565 * signal vector. Pin 0 is a vector that is a part select of pin 1,
1566 * which connected to the NetNet of the signal being selected from.
1568 * The part to be selected is the canonical (0-based) offset and the
1569 * specified number of bits (wid).
1571 * If the offset is non-constant, then pin(2) is the input vector for
1572 * the selector. If this pin is present, then use the non-constant
1573 * selector as the input.
1575 * The NetPartSelect can be output from the signal (i.e. reading a
1576 * part), input into the signal, or bi-directional. The DIR method
1577 * gives the type of the node.
1579 * VP (Vector-to-Part)
1580 * Output pin 0 is the part select, and input pin 1 is connected to
1581 * the NetNet object.
1583 * PV (Part-to-Vector)
1584 * Output pin 1 is connected to the NetNet, and input pin 0 is the
1585 * part select. In this case, the node is driving the NetNet.
1587 * BI (BI-directional)
1588 * Pin 0 is the part select and pin 1 is connected to the NetNet, but
1589 * the ports are intended to be bi-directional.
1591 * Note that whatever the direction that data is intended to flow,
1592 * pin-0 is the part select and pin-1 is connected to the NetNet.
1594 class NetPartSelect : public NetNode {
1596 public:
1597 // enum for the device direction
1598 enum dir_t { VP, PV, BI };
1600 explicit NetPartSelect(NetNet*sig,
1601 unsigned off, unsigned wid, dir_t dir);
1602 explicit NetPartSelect(NetNet*sig, NetNet*sel,
1603 unsigned wid);
1604 ~NetPartSelect();
1606 unsigned base() const;
1607 unsigned width() const;
1608 dir_t dir() const;
1610 virtual void dump_node(ostream&, unsigned ind) const;
1611 bool emit_node(struct target_t*tgt) const;
1613 private:
1614 unsigned off_;
1615 unsigned wid_;
1616 dir_t dir_;
1620 * The NetBUFZ is a magic device that represents the continuous
1621 * assign, with the output being the target register and the input
1622 * the logic that feeds it. The netlist preserves the directional
1623 * nature of that assignment with the BUFZ. The target may elide it if
1624 * that makes sense for the technology.
1626 class NetBUFZ : public NetNode {
1628 public:
1629 explicit NetBUFZ(NetScope*s, perm_string n, unsigned wid);
1630 ~NetBUFZ();
1632 unsigned width() const;
1634 virtual void dump_node(ostream&, unsigned ind) const;
1635 virtual bool emit_node(struct target_t*) const;
1637 private:
1638 unsigned width_;
1642 * This node is used to represent case equality in combinational
1643 * logic. Although this is not normally synthesizable, it makes sense
1644 * to support an abstract gate that can compare x and z. This node
1645 * always generates a single bit result, no matter the width of the
1646 * input. The elaboration, btw, needs to make sure the input widths
1647 * match.
1649 * This pins are assigned as:
1651 * 0 -- Output (always returns 0 or 1)
1652 * 1 -- Input
1653 * 2 -- Input
1655 class NetCaseCmp : public NetNode {
1657 public:
1658 explicit NetCaseCmp(NetScope*s, perm_string n, unsigned wid, bool eeq);
1659 ~NetCaseCmp();
1661 unsigned width() const;
1662 // true if this is ===, false if this is !==
1663 bool eeq() const;
1665 virtual void dump_node(ostream&, unsigned ind) const;
1666 virtual bool emit_node(struct target_t*) const;
1668 private:
1669 unsigned width_;
1670 bool eeq_;
1673 /* NOTE: This class should be replaced with the NetLiteral class
1674 * below, that is more general in that it supports different types of
1675 * values.
1677 * This class represents instances of the LPM_CONSTANT device. The
1678 * node has only outputs and a constant value. The width is available
1679 * by getting the pin_count(), and the value bits are available one at
1680 * a time. There is no meaning to the aggregation of bits to form a
1681 * wide NetConst object, although some targets may have an easier time
1682 * detecting interesting constructs if they are combined.
1684 class NetConst : public NetNode {
1686 public:
1687 explicit NetConst(NetScope*s, perm_string n, verinum::V v);
1688 explicit NetConst(NetScope*s, perm_string n, const verinum&val);
1689 ~NetConst();
1691 verinum::V value(unsigned idx) const;
1692 unsigned width() const;
1694 virtual bool emit_node(struct target_t*) const;
1695 virtual void functor_node(Design*, functor_t*);
1696 virtual void dump_node(ostream&, unsigned ind) const;
1698 private:
1699 unsigned width_;
1700 verinum::V*value_;
1704 * This class represents instances of the LPM_CONSTANT device. The
1705 * node has only outputs and a constant value. The width is available
1706 * by getting the pin_count(), and the value bits are available one at
1707 * a time. There is no meaning to the aggregation of bits to form a
1708 * wide NetConst object, although some targets may have an easier time
1709 * detecting interesting constructs if they are combined.
1711 class NetLiteral : public NetNode {
1713 public:
1714 // A read-valued literal.
1715 explicit NetLiteral(NetScope*s, perm_string n, const verireal&val);
1716 ~NetLiteral();
1718 ivl_variable_type_t data_type() const;
1720 const verireal& value_real() const;
1722 virtual bool emit_node(struct target_t*) const;
1723 virtual void functor_node(Design*, functor_t*);
1724 virtual void dump_node(ostream&, unsigned ind) const;
1726 private:
1727 verireal real_;
1731 * This class represents all manner of logic gates. Pin 0 is OUTPUT and
1732 * all the remaining pins are INPUT. The BUFIF[01] gates have the
1733 * more specific pinout as follows:
1735 * bufif<N>
1736 * 0 -- output
1737 * 1 -- input data
1738 * 2 -- enable
1740 * The pullup and pulldown gates have no inputs at all, and pin0 is
1741 * the output 1 or 0, depending on the gate type. It is the strength
1742 * of that value that is important.
1744 * All these devices process vectors bitwise, so each bit can be
1745 * logically separated. The exception is the CONCAT gate, which is
1746 * really an abstract gate that takes the inputs and turns it into a
1747 * vector of bits.
1749 class NetLogic : public NetNode {
1751 public:
1752 enum TYPE { AND, BUF, BUFIF0, BUFIF1, CMOS, NAND, NMOS, NOR, NOT,
1753 NOTIF0, NOTIF1, OR, PULLDOWN, PULLUP, RCMOS, RNMOS, RPMOS,
1754 PMOS, XNOR, XOR };
1756 explicit NetLogic(NetScope*s, perm_string n, unsigned pins,
1757 TYPE t, unsigned wid);
1759 TYPE type() const;
1760 unsigned width() const;
1762 virtual void dump_node(ostream&, unsigned ind) const;
1763 virtual bool emit_node(struct target_t*) const;
1764 virtual void functor_node(Design*, functor_t*);
1766 private:
1767 TYPE type_;
1768 unsigned width_;
1772 * This class represents a structural sign extension. The pin-0 is a
1773 * vector of the input pin-1 sign-extended. The input is taken to be
1774 * signed. This generally matches a hardware implementation of
1775 * replicating the top bit enough times to create the desired output
1776 * width.
1778 class NetSignExtend : public NetNode {
1780 public:
1781 explicit NetSignExtend(NetScope*s, perm_string n, unsigned wid);
1782 ~NetSignExtend();
1784 unsigned width() const;
1786 virtual void dump_node(ostream&, unsigned ind) const;
1787 virtual bool emit_node(struct target_t*) const;
1788 virtual void functor_node(Design*, functor_t*);
1790 private:
1791 unsigned width_;
1795 * This class represents *reduction* logic operators. Certain boolean
1796 * logic operators have reduction forms which take in a vector and
1797 * return a single bit that is calculated by applying the logic
1798 * operation through the width of the input vector. These correspond
1799 * to reduction unary operators in Verilog.
1801 class NetUReduce : public NetNode {
1803 public:
1804 enum TYPE {NONE, AND, OR, XOR, NAND, NOR, XNOR};
1806 NetUReduce(NetScope*s, perm_string n, TYPE t, unsigned wid);
1808 TYPE type() const;
1809 unsigned width() const;
1811 virtual void dump_node(ostream&, unsigned ind) const;
1812 virtual bool emit_node(struct target_t*) const;
1813 virtual void functor_node(Design*, functor_t*);
1815 private:
1816 TYPE type_;
1817 unsigned width_;
1821 * The UDP is a User Defined Primitive from the Verilog source. Do not
1822 * expand it out any further than this in the netlist, as this can be
1823 * used to represent target device primitives.
1825 * The UDP can be combinational or sequential. The sequential UDP
1826 * includes the current output in the truth table, and supports edges,
1827 * whereas the combinational does not and is entirely level sensitive.
1828 * In any case, pin 0 is an output, and all the remaining pins are
1829 * inputs.
1831 * Set_table takes as input a string with one letter per pin. The
1832 * parser translates the written sequences to one of these. The
1833 * valid characters are:
1835 * 0, 1, x -- The levels
1836 * r -- (01)
1837 * R -- (x1)
1838 * f -- (10)
1839 * F -- (x0)
1840 * P -- (0x)
1841 * N -- (1x)
1843 * It also takes one of the following glob letters to represent more
1844 * than one item.
1846 * p -- 01, 0x or x1 // check this with the lexer
1847 * n -- 10, 1x or x0 // check this with the lexer
1848 * ? -- 0, 1, or x
1849 * * -- any edge
1850 * + -- 01 or x1
1851 * _ -- 10 or x0 (Note that this is not the output '-'.)
1852 * % -- 0x or 1x
1854 * SEQUENTIAL
1855 * These objects have a single bit of memory. The logic table includes
1856 * an entry for the current value, and allows edges on the inputs. In
1857 * canonical form, only the entries that generate 0, 1 or - (no change)
1858 * are listed.
1860 * COMBINATIONAL
1861 * The logic table is a map between the input levels and the
1862 * output. Each input pin can have the value 0, 1 or x and the output
1863 * can have the values 0 or 1. If the input matches nothing, the
1864 * output is x. In canonical form, only the entries that generate 0 or
1865 * 1 are listed.
1868 #include "PUdp.h"
1870 class NetUDP : public NetNode {
1872 public:
1873 explicit NetUDP(NetScope*s, perm_string n, unsigned pins, PUdp*u);
1875 virtual bool emit_node(struct target_t*) const;
1876 virtual void dump_node(ostream&, unsigned ind) const;
1878 /* Use these methods to scan the truth table of the
1879 device. "first" returns the first item in the table, and
1880 "next" returns the next item in the table. The method will
1881 return false when the scan is done. */
1882 bool first(string&inp, char&out) const;
1883 bool next(string&inp, char&out) const;
1884 unsigned rows() const { return udp->tinput.count(); }
1886 unsigned nin() const { return pin_count()-1; }
1887 bool is_sequential() const { return udp->sequential; }
1888 perm_string udp_name() const { return udp->name_; }
1889 char get_initial() const;
1891 private:
1892 mutable unsigned table_idx;
1893 PUdp *udp;
1896 enum DelayType { NO_DELAY, ZERO_DELAY, POSSIBLE_DELAY, DEFINITE_DELAY };
1898 /* =========
1899 * A process is a behavioral-model description. A process is a
1900 * statement that may be compound. The various statement types may
1901 * refer to places in a netlist (by pointing to nodes) but is not
1902 * linked into the netlist. However, elaborating a process may cause
1903 * special nodes to be created to handle things like events.
1905 class NetProc : public virtual LineInfo {
1907 public:
1908 explicit NetProc();
1909 virtual ~NetProc();
1911 // Find the nexa that are input by the statement. This is used
1912 // for example by @* to find the inputs to the process for the
1913 // sensitivity list.
1914 virtual NexusSet* nex_input(bool rem_out = true);
1916 // Find the nexa that are set by the statement. Add the output
1917 // values to the set passed as a parameter.
1918 virtual void nex_output(NexusSet&);
1920 // This method is called to emit the statement to the
1921 // target. The target returns true if OK, false for errors.
1922 virtual bool emit_proc(struct target_t*) const;
1924 // This method is called by functors that want to scan a
1925 // process in search of matchable patterns.
1926 virtual int match_proc(struct proc_match_t*);
1928 // Return true if this represents the root of a combinational
1929 // process. Most process types are not.
1930 virtual bool is_asynchronous();
1932 // Return true if this represents the root of a synchronous
1933 // process. Most process types are not.
1934 virtual bool is_synchronous();
1936 // Synthesize as asynchronous logic, and return true.
1937 virtual bool synth_async(Design*des, NetScope*scope,
1938 const NetBus&nex_map, NetBus&nex_out);
1940 virtual bool synth_sync(Design*des, NetScope*scope, NetFF*ff,
1941 const NetBus&nex_map, NetBus&nex_out,
1942 const svector<NetEvProbe*>&events);
1944 virtual void dump(ostream&, unsigned ind) const;
1946 // Recursively checks to see if there is delay in this element.
1947 virtual DelayType delay_type() const;
1949 private:
1950 friend class NetBlock;
1951 NetProc*next_;
1953 private: // not implemented
1954 NetProc(const NetProc&);
1955 NetProc& operator= (const NetProc&);
1959 * Procedural assignment is broken into a suite of classes. These
1960 * classes represent the various aspects of the assignment statement
1961 * in behavioral code. (The continuous assignment is *not*
1962 * represented here.)
1964 * The NetAssignBase carries the common aspects of an assignment,
1965 * including the r-value. This class has no cares of blocking vs
1966 * non-blocking, however it carries nearly all the other properties
1967 * of the assignment statement. It is abstract because it does not
1968 * differentiate the virtual behaviors.
1970 * The NetAssign and NetAssignNB classes are the concrete classes that
1971 * give the assignment its final, precise meaning. These classes fill
1972 * in the NetProc behaviors.
1974 * The l-value of the assignment is a collection of NetAssign_
1975 * objects that are connected to the structural netlist where the
1976 * assignment has its effect. The NetAssign_ class is not to be
1977 * derived from.
1979 * The collection is arranged from lsb up to msb, and represents the
1980 * concatenation of l-values. The elaborator may collapse some
1981 * concatenations into a single NetAssign_. The "more" member of the
1982 * NetAssign_ object points to the next most significant bits of l-value.
1984 * NOTE: The elaborator will make an effort to match the width of the
1985 * r-value to the width of the l-value, but targets and functions
1986 * should know that this is not a guarantee.
1989 class NetAssign_ {
1991 public:
1992 NetAssign_(NetNet*sig);
1993 ~NetAssign_();
1995 // If this expression exists, then it is used to select a word
1996 // from an array/memory.
1997 NetExpr*word();
1998 const NetExpr*word() const;
2000 // Get the base index of the part select, or 0 if there is no
2001 // part select.
2002 const NetExpr* get_base() const;
2004 void set_word(NetExpr*);
2005 void set_part(NetExpr* loff, unsigned wid);
2007 // Get the width of the r-value that this node expects. This
2008 // method accounts for the presence of the mux, so it is not
2009 // necessarily the same as the pin_count().
2010 unsigned lwidth() const;
2012 // Get the name of the underlying object.
2013 perm_string name() const;
2015 NetNet* sig() const;
2017 // Mark that the synthesizer has worked with this l-value, so
2018 // when it is released, the l-value signal should be turned
2019 // into a wire.
2020 void turn_sig_to_wire_on_release();
2022 // It is possible that l-values can have *inputs*, as well as
2023 // being outputs. For example foo[idx] = ... is the l-value
2024 // (NetAssign_ object) with a foo l-value and the input
2025 // expression idx.
2026 NexusSet* nex_input(bool rem_out = true);
2028 // This pointer is for keeping simple lists.
2029 NetAssign_* more;
2031 void dump_lval(ostream&o) const;
2033 private:
2034 NetNet *sig_;
2035 // Memory word index
2036 NetExpr*word_;
2038 bool turn_sig_to_wire_on_release_;
2039 // indexed part select base
2040 NetExpr*base_;
2041 unsigned lwid_;
2044 class NetAssignBase : public NetProc {
2046 public:
2047 NetAssignBase(NetAssign_*lv, NetExpr*rv);
2048 virtual ~NetAssignBase() =0;
2050 // This is the (procedural) value that is to be assigned when
2051 // the assignment is executed.
2052 NetExpr*rval();
2053 const NetExpr*rval() const;
2055 void set_rval(NetExpr*);
2057 NetAssign_* l_val(unsigned);
2058 const NetAssign_* l_val(unsigned) const;
2059 unsigned l_val_count() const;
2061 void set_delay(NetExpr*);
2062 const NetExpr* get_delay() const;
2064 virtual NexusSet* nex_input(bool rem_out = true);
2065 virtual void nex_output(NexusSet&o);
2068 // This returns the total width of the accumulated l-value. It
2069 // accounts for any grouping of NetAssign_ objects that might happen.
2070 unsigned lwidth() const;
2072 bool synth_async(Design*des, NetScope*scope,
2073 const NetBus&nex_map, NetBus&nex_out);
2075 // This dumps all the lval structures.
2076 void dump_lval(ostream&) const;
2077 virtual void dump(ostream&, unsigned ind) const;
2079 private:
2080 NetAssign_*lval_;
2081 NetExpr *rval_;
2082 NetExpr *delay_;
2085 class NetAssign : public NetAssignBase {
2087 public:
2088 explicit NetAssign(NetAssign_*lv, NetExpr*rv);
2089 ~NetAssign();
2091 bool is_asynchronous();
2093 virtual bool emit_proc(struct target_t*) const;
2094 virtual int match_proc(struct proc_match_t*);
2095 virtual void dump(ostream&, unsigned ind) const;
2097 private:
2100 class NetAssignNB : public NetAssignBase {
2101 public:
2102 explicit NetAssignNB(NetAssign_*lv, NetExpr*rv);
2103 ~NetAssignNB();
2106 virtual bool emit_proc(struct target_t*) const;
2107 virtual int match_proc(struct proc_match_t*);
2108 virtual void dump(ostream&, unsigned ind) const;
2110 private:
2114 * A block is stuff like begin-end blocks, that contain an ordered
2115 * list of NetProc statements.
2117 * NOTE: The emit method calls the target->proc_block function but
2118 * does not recurse. It is up to the target-supplied proc_block
2119 * function to call emit_recurse.
2121 class NetBlock : public NetProc {
2123 public:
2124 enum Type { SEQU, PARA };
2126 NetBlock(Type t, NetScope*subscope);
2127 ~NetBlock();
2129 Type type() const { return type_; }
2130 NetScope* subscope() const { return subscope_; }
2132 void append(NetProc*);
2134 const NetProc*proc_first() const;
2135 const NetProc*proc_next(const NetProc*cur) const;
2138 // synthesize as asynchronous logic, and return true.
2139 bool synth_async(Design*des, NetScope*scope,
2140 const NetBus&nex_map, NetBus&nex_out);
2142 bool synth_sync(Design*des, NetScope*scope, NetFF*ff,
2143 const NetBus&nex_map, NetBus&nex_out,
2144 const svector<NetEvProbe*>&events);
2146 // This version of emit_recurse scans all the statements of
2147 // the begin-end block sequentially. It is typically of use
2148 // for sequential blocks.
2149 void emit_recurse(struct target_t*) const;
2151 virtual NexusSet* nex_input(bool rem_out = true);
2152 virtual void nex_output(NexusSet&);
2153 virtual bool emit_proc(struct target_t*) const;
2154 virtual int match_proc(struct proc_match_t*);
2155 virtual void dump(ostream&, unsigned ind) const;
2156 virtual DelayType delay_type() const;
2158 private:
2159 const Type type_;
2160 NetScope*subscope_;
2162 NetProc*last_;
2166 * A CASE statement in the Verilog source leads, eventually, to one of
2167 * these. This is different from a simple conditional because of the
2168 * way the comparisons are performed. Also, it is likely that the
2169 * target may be able to optimize differently.
2171 * Case can be one of three types:
2172 * EQ -- All bits must exactly match
2173 * EQZ -- z bits are don't care
2174 * EQX -- x and z bits are don't care.
2176 class NetCase : public NetProc {
2178 public:
2179 enum TYPE { EQ, EQX, EQZ };
2180 NetCase(TYPE c, NetExpr*ex, unsigned cnt);
2181 ~NetCase();
2183 void set_case(unsigned idx, NetExpr*ex, NetProc*st);
2185 TYPE type() const;
2186 const NetExpr*expr() const { return expr_; }
2187 unsigned nitems() const { return nitems_; }
2189 const NetExpr*expr(unsigned idx) const { return items_[idx].guard;}
2190 const NetProc*stat(unsigned idx) const { return items_[idx].statement; }
2192 virtual NexusSet* nex_input(bool rem_out = true);
2193 virtual void nex_output(NexusSet&out);
2195 bool synth_async(Design*des, NetScope*scope,
2196 const NetBus&nex_map, NetBus&nex_out);
2198 virtual bool emit_proc(struct target_t*) const;
2199 virtual void dump(ostream&, unsigned ind) const;
2200 virtual DelayType delay_type() const;
2202 private:
2204 TYPE type_;
2206 struct Item {
2207 NetExpr*guard;
2208 NetProc*statement;
2211 NetExpr* expr_;
2212 unsigned nitems_;
2213 Item*items_;
2217 * The cassign statement causes the r-val net to be forced onto the
2218 * l-val reg when it is executed. The code generator is expected to
2219 * know what that means.
2221 class NetCAssign : public NetAssignBase {
2223 public:
2224 explicit NetCAssign(NetAssign_*lv, NetExpr*rv);
2225 ~NetCAssign();
2227 virtual NexusSet* nex_input(bool rem_out = true);
2228 virtual void dump(ostream&, unsigned ind) const;
2229 virtual bool emit_proc(struct target_t*) const;
2231 private: // not implemented
2232 NetCAssign(const NetCAssign&);
2233 NetCAssign& operator= (const NetCAssign&);
2238 * A condit represents a conditional. It has an expression to test,
2239 * and a pair of statements to select from. If the original statement
2240 * has empty clauses, then the NetProc for it will be a null pointer.
2242 class NetCondit : public NetProc {
2244 public:
2245 explicit NetCondit(NetExpr*ex, NetProc*i, NetProc*e);
2246 ~NetCondit();
2248 const NetExpr*expr() const;
2249 NetExpr*expr();
2251 NetProc* if_clause();
2252 NetProc* else_clause();
2254 // Replace the condition expression.
2255 void set_expr(NetExpr*ex);
2257 bool emit_recurse_if(struct target_t*) const;
2258 bool emit_recurse_else(struct target_t*) const;
2260 virtual NexusSet* nex_input(bool rem_out = true);
2261 virtual void nex_output(NexusSet&o);
2263 bool is_asynchronous();
2264 bool synth_async(Design*des, NetScope*scope,
2265 const NetBus&nex_map, NetBus&nex_out);
2267 bool synth_sync(Design*des, NetScope*scope, NetFF*ff,
2268 const NetBus&nex_map, NetBus&nex_out,
2269 const svector<NetEvProbe*>&events);
2271 virtual bool emit_proc(struct target_t*) const;
2272 virtual int match_proc(struct proc_match_t*);
2273 virtual void dump(ostream&, unsigned ind) const;
2274 virtual DelayType delay_type() const;
2276 private:
2277 NetExpr* expr_;
2278 NetProc*if_;
2279 NetProc*else_;
2283 * The procedural deassign statement (the opposite of assign) releases
2284 * any assign expressions attached to the bits of the reg. The
2285 * lval is the expression of the "deassign <expr>;" statement with the
2286 * expr elaborated to a net.
2288 class NetDeassign : public NetAssignBase {
2290 public:
2291 explicit NetDeassign(NetAssign_*l);
2292 ~NetDeassign();
2294 virtual bool emit_proc(struct target_t*) const;
2295 virtual void dump(ostream&, unsigned ind) const;
2297 private: // not implemented
2298 NetDeassign(const NetDeassign&);
2299 NetDeassign& operator= (const NetDeassign&);
2303 * This node represents the behavioral disable statement. The Verilog
2304 * source that produces it looks like:
2306 * disable <scope>;
2308 * Where the scope is a named block or a task. It cannot be a module
2309 * instance scope because module instances cannot be disabled.
2311 class NetDisable : public NetProc {
2313 public:
2314 explicit NetDisable(NetScope*tgt);
2315 ~NetDisable();
2317 const NetScope*target() const;
2319 virtual bool emit_proc(struct target_t*) const;
2320 virtual void dump(ostream&, unsigned ind) const;
2322 private:
2323 NetScope*target_;
2325 private: // not implemented
2326 NetDisable(const NetDisable&);
2327 NetDisable& operator= (const NetDisable&);
2331 * A NetEvent is an object that represents an event object, that is
2332 * objects declared like so in Verilog:
2334 * event foo;
2336 * Once an object of this type exists, behavioral code can wait on the
2337 * event or trigger the event. Event waits refer to this object, as do
2338 * the event trigger statements. The NetEvent class may have a name and
2339 * a scope. The name is a simple name (no hierarchy) and the scope is
2340 * the NetScope that contains the object. The scope member is written
2341 * by the NetScope object when the NetEvent is stored.
2343 * The NetEvWait class represents a thread wait for an event. When
2344 * this statement is executed, it starts waiting on the
2345 * event. Conceptually, it puts itself on the event list for the
2346 * referenced event. When the event is triggered, the wait ends its
2347 * block and starts the associated statement.
2349 * The NetEvTrig class represents trigger statements. Executing this
2350 * statement causes the referenced event to be triggered, which in
2351 * turn awakens the waiting threads. Each NetEvTrig object references
2352 * exactly one event object.
2354 * The NetEvProbe class is the structural equivalent of the NetEvTrig,
2355 * in that it is a node and watches bit values that it receives. It
2356 * checks for edges then if appropriate triggers the associated
2357 * NetEvent. Each NetEvProbe references exactly one event object, and
2358 * the NetEvent objects have a list of NetEvProbe objects that
2359 * reference it.
2361 class NetEvent : public LineInfo {
2363 friend class NetScope;
2364 friend class NetEvProbe;
2365 friend class NetEvTrig;
2366 friend class NetEvWait;
2367 friend class NetEEvent;
2369 public:
2370 // The name of the event is the basename, and should not
2371 // include the scope. Also, the name passed here should be
2372 // perm-allocated.
2373 explicit NetEvent (perm_string n);
2374 ~NetEvent();
2376 perm_string name() const;
2378 // Get information about probes connected to me.
2379 unsigned nprobe() const;
2380 NetEvProbe* probe(unsigned);
2381 const NetEvProbe* probe(unsigned) const;
2383 // Return the number of NetEvWait nodes that reference me.
2384 unsigned nwait() const;
2385 unsigned ntrig() const;
2386 unsigned nexpr() const;
2388 NetScope* scope();
2389 const NetScope* scope() const;
2391 void nex_output(NexusSet&);
2393 // Locate the first event that matches my behavior and
2394 // monitors the same signals.
2395 void find_similar_event(list<NetEvent*>&);
2397 // This method replaces pointers to me with pointers to
2398 // that. It is typically used to replace similar events
2399 // located by the find_similar_event method.
2400 void replace_event(NetEvent*that);
2402 private:
2403 // This returns a nexus set if it represents possibly
2404 // asynchronous inputs, otherwise 0.
2405 NexusSet*nex_async_();
2407 private:
2408 perm_string name_;
2410 // The NetScope class uses these to list the events.
2411 NetScope*scope_;
2412 NetEvent*snext_;
2414 // Use these methods to list the probes attached to me.
2415 NetEvProbe*probes_;
2417 // Use these methods to list the triggers attached to me.
2418 NetEvTrig* trig_;
2420 // Use This member to count references by NetEvWait objects.
2421 unsigned waitref_;
2422 struct wcell_ {
2423 NetEvWait*obj;
2424 struct wcell_*next;
2426 struct wcell_ *wlist_;
2428 // expression references, ala. task/funcs
2429 unsigned exprref_;
2431 private: // not implemented
2432 NetEvent(const NetEvent&);
2433 NetEvent& operator= (const NetEvent&);
2436 class NetEvTrig : public NetProc {
2438 friend class NetEvent;
2440 public:
2441 explicit NetEvTrig(NetEvent*tgt);
2442 ~NetEvTrig();
2444 const NetEvent*event() const;
2446 virtual bool emit_proc(struct target_t*) const;
2447 virtual void dump(ostream&, unsigned ind) const;
2449 private:
2450 NetEvent*event_;
2451 // This is used to place me in the NetEvents lists of triggers.
2452 NetEvTrig*enext_;
2455 class NetEvWait : public NetProc {
2457 public:
2458 explicit NetEvWait(NetProc*st);
2459 ~NetEvWait();
2461 void add_event(NetEvent*tgt);
2462 void replace_event(NetEvent*orig, NetEvent*repl);
2464 unsigned nevents() const;
2465 const NetEvent*event(unsigned) const;
2466 NetEvent*event(unsigned);
2468 NetProc*statement();
2470 virtual bool emit_proc(struct target_t*) const;
2471 bool emit_recurse(struct target_t*) const;
2472 virtual int match_proc(struct proc_match_t*);
2474 // It is possible that this is the root of a combinational
2475 // process. This method checks.
2476 virtual bool is_asynchronous();
2478 // It is possible that this is the root of a synchronous
2479 // process? This method checks.
2480 virtual bool is_synchronous();
2482 virtual void nex_output(NexusSet&out);
2484 virtual bool synth_async(Design*des, NetScope*scope,
2485 const NetBus&nex_map, NetBus&nex_out);
2487 virtual bool synth_sync(Design*des, NetScope*scope, NetFF*ff,
2488 const NetBus&nex_map, NetBus&nex_out,
2489 const svector<NetEvProbe*>&events);
2491 virtual void dump(ostream&, unsigned ind) const;
2492 virtual DelayType delay_type() const;
2494 private:
2495 NetProc*statement_;
2497 unsigned nevents_;
2498 NetEvent**events_;
2501 class NetEvProbe : public NetNode {
2503 friend class NetEvent;
2505 public:
2506 enum edge_t { ANYEDGE, POSEDGE, NEGEDGE };
2508 explicit NetEvProbe(NetScope*s, perm_string n,
2509 NetEvent*tgt, edge_t t, unsigned p);
2510 ~NetEvProbe();
2512 edge_t edge() const;
2513 NetEvent* event();
2514 const NetEvent* event() const;
2516 void find_similar_probes(list<NetEvProbe*>&);
2518 virtual bool emit_node(struct target_t*) const;
2519 virtual void dump_node(ostream&, unsigned ind) const;
2521 private:
2522 NetEvent*event_;
2523 edge_t edge_;
2524 // The NetEvent class uses this to list me.
2525 NetEvProbe*enext_;
2529 * The force statement causes the r-val net to be forced onto the
2530 * l-val net when it is executed. The code generator is expected to
2531 * know what that means.
2533 class NetForce : public NetAssignBase {
2535 public:
2536 explicit NetForce(NetAssign_*l, NetExpr*r);
2537 ~NetForce();
2539 virtual NexusSet* nex_input(bool rem_out = true);
2541 virtual void dump(ostream&, unsigned ind) const;
2542 virtual bool emit_proc(struct target_t*) const;
2546 * A forever statement is executed over and over again forever. Or
2547 * until its block is disabled.
2549 class NetForever : public NetProc {
2551 public:
2552 explicit NetForever(NetProc*s);
2553 ~NetForever();
2555 void emit_recurse(struct target_t*) const;
2557 virtual NexusSet* nex_input(bool rem_out = true);
2558 virtual bool emit_proc(struct target_t*) const;
2559 virtual void dump(ostream&, unsigned ind) const;
2560 virtual DelayType delay_type() const;
2562 private:
2563 NetProc*statement_;
2567 * A function definition is elaborated just like a task, though by now
2568 * it is certain that the first parameter (a phantom parameter) is the
2569 * output and all the remaining parameters are the inputs. This makes
2570 * for easy code generation in targets that support behavioral
2571 * descriptions.
2573 * The NetNet array that is passed in as a parameter is the set of
2574 * signals that make up its parameter list. These are all internal to
2575 * the scope of the function.
2577 class NetFuncDef {
2579 public:
2580 NetFuncDef(NetScope*, NetNet*result, const svector<NetNet*>&po);
2581 ~NetFuncDef();
2583 void set_proc(NetProc*st);
2585 //const string name() const;
2586 const NetProc*proc() const;
2587 const NetScope*scope() const;
2588 NetScope*scope();
2590 unsigned port_count() const;
2591 const NetNet*port(unsigned idx) const;
2593 const NetNet*return_sig() const;
2595 void dump(ostream&, unsigned ind) const;
2597 private:
2598 NetScope*scope_;
2599 NetProc*statement_;
2600 NetNet*result_sig_;
2601 svector<NetNet*>ports_;
2605 * This class represents delay statements of the form:
2607 * #<expr> <statement>
2609 * Where the statement may be null. The delay is evaluated at
2610 * elaboration time to make a constant unsigned long that is the delay
2611 * in simulation ticks.
2613 * If the delay expression is non-constant, construct the NetPDelay
2614 * object with a NetExpr* instead of the d value, and use the expr()
2615 * method to get the expression. If expr() returns 0, use the delay()
2616 * method to get the constant delay.
2618 class NetPDelay : public NetProc {
2620 public:
2621 NetPDelay(uint64_t d, NetProc*st);
2622 NetPDelay(NetExpr* d, NetProc*st);
2623 ~NetPDelay();
2625 uint64_t delay() const;
2626 const NetExpr*expr() const;
2628 virtual NexusSet* nex_input(bool rem_out = true);
2629 virtual void nex_output(NexusSet&);
2631 virtual bool emit_proc(struct target_t*) const;
2632 virtual void dump(ostream&, unsigned ind) const;
2633 virtual DelayType delay_type() const;
2635 bool emit_proc_recurse(struct target_t*) const;
2637 private:
2638 uint64_t delay_;
2639 NetExpr*expr_;
2640 NetProc*statement_;
2644 * A repeat statement is executed some fixed number of times.
2646 class NetRepeat : public NetProc {
2648 public:
2649 explicit NetRepeat(NetExpr*e, NetProc*s);
2650 ~NetRepeat();
2652 const NetExpr*expr() const;
2653 void emit_recurse(struct target_t*) const;
2655 virtual NexusSet* nex_input(bool rem_out = true);
2656 virtual bool emit_proc(struct target_t*) const;
2657 virtual void dump(ostream&, unsigned ind) const;
2658 virtual DelayType delay_type() const;
2660 private:
2661 NetExpr*expr_;
2662 NetProc*statement_;
2666 * The procedural release statement (the opposite of force) releases
2667 * any force expressions attached to the bits of the wire or reg. The
2668 * lval is the expression of the "release <expr>;" statement with the
2669 * expr elaborated to a net.
2671 class NetRelease : public NetAssignBase {
2673 public:
2674 explicit NetRelease(NetAssign_*l);
2675 ~NetRelease();
2677 virtual bool emit_proc(struct target_t*) const;
2678 virtual void dump(ostream&, unsigned ind) const;
2680 private:
2685 * The NetSTask class is a call to a system task. These kinds of tasks
2686 * are generally handled very simply in the target. They certainly are
2687 * handled differently from user defined tasks because ivl knows all
2688 * about the user defined tasks.
2690 class NetSTask : public NetProc {
2692 public:
2693 NetSTask(const char*na, const svector<NetExpr*>&);
2694 ~NetSTask();
2696 const char* name() const;
2698 unsigned nparms() const;
2700 const NetExpr* parm(unsigned idx) const;
2702 virtual NexusSet* nex_input(bool rem_out = true);
2703 virtual void nex_output(NexusSet&);
2704 virtual bool emit_proc(struct target_t*) const;
2705 virtual void dump(ostream&, unsigned ind) const;
2707 private:
2708 const char* name_;
2709 svector<NetExpr*>parms_;
2713 * This class represents an elaborated class definition. NetUTask
2714 * classes may refer to objects of this type to get the meaning of the
2715 * defined task.
2717 * The task also introduces a scope, and the parameters are actually
2718 * reg objects in the new scope. The task is called by the calling
2719 * thread assigning (blocking assignment) to the in and inout
2720 * parameters, then invoking the thread, and finally assigning out the
2721 * output and inout variables. The variables accessible as ports are
2722 * also elaborated and accessible as ordinary reg objects.
2724 class NetTaskDef {
2726 public:
2727 NetTaskDef(NetScope*n, const svector<NetNet*>&po);
2728 ~NetTaskDef();
2730 void set_proc(NetProc*p);
2732 //const string& name() const;
2733 const NetScope* scope() const;
2734 const NetProc*proc() const;
2736 unsigned port_count() const;
2737 NetNet*port(unsigned idx);
2739 void dump(ostream&, unsigned) const;
2740 DelayType delay_type() const;
2742 private:
2743 NetScope*scope_;
2744 NetProc*proc_;
2745 svector<NetNet*>ports_;
2747 private: // not implemented
2748 NetTaskDef(const NetTaskDef&);
2749 NetTaskDef& operator= (const NetTaskDef&);
2753 * This node represents a function call in an expression. The object
2754 * contains a pointer to the function definition, which is used to
2755 * locate the value register and input expressions.
2757 class NetEUFunc : public NetExpr {
2759 public:
2760 NetEUFunc(NetScope*, NetScope*, NetESignal*, svector<NetExpr*>&);
2761 ~NetEUFunc();
2763 const NetESignal*result_sig() const;
2765 unsigned parm_count() const;
2766 const NetExpr* parm(unsigned idx) const;
2768 const NetScope* func() const;
2770 virtual bool set_width(unsigned, bool last_chance);
2771 virtual ivl_variable_type_t expr_type() const;
2772 virtual void dump(ostream&) const;
2774 virtual void expr_scan(struct expr_scan_t*) const;
2775 virtual NetEUFunc*dup_expr() const;
2776 virtual NexusSet* nex_input(bool rem_out = true);
2777 virtual NetNet* synthesize(Design*des);
2779 private:
2780 NetScope*scope_;
2781 NetScope*func_;
2782 NetESignal*result_sig_;
2783 svector<NetExpr*> parms_;
2785 private: // not implemented
2786 NetEUFunc(const NetEUFunc&);
2787 NetEUFunc& operator= (const NetEUFunc&);
2791 * A call to a user defined task is elaborated into this object. This
2792 * contains a pointer to the elaborated task definition, but is a
2793 * NetProc object so that it can be linked into statements.
2795 class NetUTask : public NetProc {
2797 public:
2798 NetUTask(NetScope*);
2799 ~NetUTask();
2801 const string name() const;
2803 const NetScope* task() const;
2805 virtual NexusSet* nex_input(bool rem_out = true);
2806 virtual void nex_output(NexusSet&);
2807 virtual bool emit_proc(struct target_t*) const;
2808 virtual void dump(ostream&, unsigned ind) const;
2809 virtual DelayType delay_type() const;
2811 private:
2812 NetScope*task_;
2816 * The while statement is a condition that is tested in the front of
2817 * each iteration, and a statement (a NetProc) that is executed as
2818 * long as the condition is true.
2820 class NetWhile : public NetProc {
2822 public:
2823 NetWhile(NetExpr*c, NetProc*p)
2824 : cond_(c), proc_(p) { }
2826 const NetExpr*expr() const { return cond_; }
2828 void emit_proc_recurse(struct target_t*) const;
2830 virtual NexusSet* nex_input(bool rem_out = true);
2831 virtual void nex_output(NexusSet&);
2832 virtual bool emit_proc(struct target_t*) const;
2833 virtual void dump(ostream&, unsigned ind) const;
2834 virtual DelayType delay_type() const;
2836 private:
2837 NetExpr* cond_;
2838 NetProc*proc_;
2843 * The is the top of any process. It carries the type (initial or
2844 * always) and a pointer to the statement, probably a block, that
2845 * makes up the process.
2847 class NetProcTop : public LineInfo, public Attrib {
2849 public:
2850 enum Type { KINITIAL, KALWAYS };
2852 NetProcTop(NetScope*s, Type t, class NetProc*st);
2853 ~NetProcTop();
2855 Type type() const { return type_; }
2856 NetProc*statement();
2857 const NetProc*statement() const;
2859 NetScope*scope();
2860 const NetScope*scope() const;
2862 /* Return true if this process represents combinational logic. */
2863 bool is_asynchronous();
2865 /* Create asynchronous logic from this thread and return true,
2866 or return false if that cannot be done. */
2867 bool synth_async(Design*des);
2869 /* Return true if this process represents synchronous logic. */
2870 bool is_synchronous();
2872 /* Create synchronous logic from this thread and return true,
2873 or return false if that cannot be done. */
2874 bool synth_sync(Design*des);
2876 void dump(ostream&, unsigned ind) const;
2877 bool emit(struct target_t*tgt) const;
2879 private:
2880 const Type type_;
2881 NetProc*const statement_;
2883 NetScope*scope_;
2884 friend class Design;
2885 NetProcTop*next_;
2889 * This class represents a binary operator, with the left and right
2890 * operands and a single character for the operator. The operator
2891 * values are:
2893 * ^ -- Bit-wise exclusive OR
2894 * + -- Arithmetic add
2895 * - -- Arithmetic minus
2896 * * -- Arithmetic multiply
2897 * / -- Arithmetic divide
2898 * % -- Arithmetic modulus
2899 * p -- Arithmetic power (**)
2900 * & -- Bit-wise AND
2901 * | -- Bit-wise OR
2902 * < -- Less than
2903 * > -- Greater than
2904 * e -- Logical equality (==)
2905 * E -- Case equality (===)
2906 * L -- Less or equal
2907 * G -- Greater or equal
2908 * n -- Logical inequality (!=)
2909 * N -- Case inequality (!==)
2910 * a -- Logical AND (&&)
2911 * A -- Bitwise NAND (~&)
2912 * o -- Logical OR (||)
2913 * O -- Bit-wise NOR (~|)
2914 * l -- Left shift (<<)
2915 * r -- Right shift (>>)
2916 * R -- signed right shift (>>>)
2917 * X -- Bitwise exclusive NOR (~^)
2918 * m -- min(a,b)
2919 * M -- max(a,b)
2921 class NetEBinary : public NetExpr {
2923 public:
2924 NetEBinary(char op, NetExpr*l, NetExpr*r);
2925 ~NetEBinary();
2927 const NetExpr*left() const { return left_; }
2928 const NetExpr*right() const { return right_; }
2930 char op() const { return op_; }
2932 virtual bool set_width(unsigned w, bool last_chance =false);
2934 // A binary expression node only has a definite
2935 // self-determinable width if the operands both have definite
2936 // widths.
2937 virtual bool has_width() const;
2939 virtual NetEBinary* dup_expr() const;
2940 virtual NexusSet* nex_input(bool rem_out = true);
2942 virtual void expr_scan(struct expr_scan_t*) const;
2943 virtual void dump(ostream&) const;
2945 protected:
2946 char op_;
2947 NetExpr* left_;
2948 NetExpr* right_;
2950 bool get_real_arguments_(verireal&lv, verireal&rv);
2954 * The addition operators have slightly more complex width
2955 * calculations because there is the optional carry bit that can be
2956 * used. The operators covered by this class are:
2957 * + -- Arithmetic add
2958 * - -- Arithmetic minus
2960 class NetEBAdd : public NetEBinary {
2962 public:
2963 NetEBAdd(char op, NetExpr*l, NetExpr*r, bool lossless_flag =false);
2964 ~NetEBAdd();
2966 virtual ivl_variable_type_t expr_type() const;
2968 virtual bool set_width(unsigned w, bool last_chance);
2969 virtual NetEBAdd* dup_expr() const;
2970 virtual NetExpr* eval_tree(int prune_to_width = -1);
2971 virtual NetNet* synthesize(Design*);
2973 private:
2974 NetECReal* eval_tree_real_();
2978 * This class represents the integer division operators.
2979 * / -- Divide
2980 * % -- Modulus
2982 class NetEBDiv : public NetEBinary {
2984 public:
2985 NetEBDiv(char op, NetExpr*l, NetExpr*r);
2986 ~NetEBDiv();
2988 virtual ivl_variable_type_t expr_type() const;
2990 virtual bool set_width(unsigned w, bool last_chance);
2991 virtual NetEBDiv* dup_expr() const;
2992 virtual NetExpr* eval_tree(int prune_to_width = -1);
2993 virtual NetNet* synthesize(Design*);
2997 * The bitwise binary operators are represented by this class. This is
2998 * a specialization of the binary operator, so is derived from
2999 * NetEBinary. The particular constraints on these operators are that
3000 * operand and result widths match exactly, and each bit slice of the
3001 * operation can be represented by a simple gate. The operators
3002 * covered by this class are:
3004 * ^ -- Bit-wise exclusive OR
3005 * & -- Bit-wise AND
3006 * | -- Bit-wise OR
3007 * O -- Bit-wise NOR
3008 * X -- Bit-wise XNOR (~^)
3010 class NetEBBits : public NetEBinary {
3012 public:
3013 NetEBBits(char op, NetExpr*l, NetExpr*r);
3014 ~NetEBBits();
3016 virtual bool set_width(unsigned w, bool last_chance);
3017 virtual NetEBBits* dup_expr() const;
3018 virtual NetEConst* eval_tree(int prune_to_width = -1);
3020 virtual NetNet* synthesize(Design*);
3024 * The binary comparison operators are handled by this class. This
3025 * this case the bit width of the expression is 1 bit, and the
3026 * operands take their natural widths. The supported operators are:
3028 * < -- Less than
3029 * > -- Greater than
3030 * e -- Logical equality (==)
3031 * E -- Case equality (===)
3032 * L -- Less or equal (<=)
3033 * G -- Greater or equal (>=)
3034 * n -- Logical inequality (!=)
3035 * N -- Case inequality (!==)
3037 class NetEBComp : public NetEBinary {
3039 public:
3040 NetEBComp(char op, NetExpr*l, NetExpr*r);
3041 ~NetEBComp();
3043 virtual bool set_width(unsigned w, bool last_chance);
3045 /* A compare expression has a definite width. */
3046 virtual bool has_width() const;
3047 virtual ivl_variable_type_t expr_type() const;
3048 virtual NetEBComp* dup_expr() const;
3049 virtual NetEConst* eval_tree(int prune_to_width = -1);
3051 virtual NetNet* synthesize(Design*);
3053 private:
3054 NetEConst* must_be_leeq_(NetExpr*le, const verinum&rv, bool eq_flag);
3056 NetEConst*eval_eqeq_(bool ne_flag);
3057 NetEConst*eval_less_();
3058 NetEConst*eval_leeq_();
3059 NetEConst*eval_leeq_real_(NetExpr*le, NetExpr*ri, bool eq_flag);
3060 NetEConst*eval_gt_();
3061 NetEConst*eval_gteq_();
3062 NetEConst*eval_eqeqeq_();
3063 NetEConst*eval_neeqeq_();
3067 * The binary logical operators are those that return boolean
3068 * results. The supported operators are:
3070 * a -- Logical AND (&&)
3071 * o -- Logical OR (||)
3073 class NetEBLogic : public NetEBinary {
3075 public:
3076 NetEBLogic(char op, NetExpr*l, NetExpr*r);
3077 ~NetEBLogic();
3079 virtual bool set_width(unsigned w, bool last_chance);
3080 virtual NetEBLogic* dup_expr() const;
3081 virtual NetEConst* eval_tree(int prune_to_width = -1);
3082 virtual NetNet* synthesize(Design*);
3084 private:
3088 * Support the binary min(l,r) and max(l,r) operators. The opcodes
3089 * supported are:
3091 * m -- min
3092 * M -- max
3094 class NetEBMinMax : public NetEBinary {
3096 public:
3097 NetEBMinMax(char op, NetExpr*l, NetExpr*r);
3098 ~NetEBMinMax();
3100 virtual ivl_variable_type_t expr_type() const;
3102 private:
3107 * Support the binary multiplication (*) operator.
3109 class NetEBMult : public NetEBinary {
3111 public:
3112 NetEBMult(char op, NetExpr*l, NetExpr*r);
3113 ~NetEBMult();
3115 virtual ivl_variable_type_t expr_type() const;
3117 virtual bool set_width(unsigned w, bool last_chance);
3118 virtual NetEBMult* dup_expr() const;
3119 virtual NetExpr* eval_tree(int prune_to_width = -1);
3120 virtual NetNet* synthesize(Design*);
3122 private:
3124 NetExpr* eval_tree_real_();
3129 * Support the binary power (**) operator.
3131 class NetEBPow : public NetEBinary {
3133 public:
3134 NetEBPow(char op, NetExpr*l, NetExpr*r);
3135 ~NetEBPow();
3137 virtual ivl_variable_type_t expr_type() const;
3139 virtual bool set_width(unsigned w, bool last_chance);
3140 virtual NetEBPow* dup_expr() const;
3141 virtual NetExpr* eval_tree(int prune_to_width = -1);
3142 virtual NetNet* synthesize(Design*);
3144 private:
3146 NetExpr* eval_tree_real_();
3152 * The binary logical operators are those that return boolean
3153 * results. The supported operators are:
3155 * l -- left shift (<<)
3156 * r -- right shift (>>)
3157 * R -- right shift arithmetic (>>>)
3159 class NetEBShift : public NetEBinary {
3161 public:
3162 NetEBShift(char op, NetExpr*l, NetExpr*r);
3163 ~NetEBShift();
3165 virtual bool set_width(unsigned w, bool last_chance);
3167 // A shift expression only needs the left expression to have a
3168 // definite width to give the expression a definite width.
3169 virtual bool has_width() const;
3171 virtual NetEBShift* dup_expr() const;
3172 virtual NetEConst* eval_tree(int prune_to_width = -1);
3174 virtual NetNet* synthesize(Design*);
3176 private:
3181 * This expression node supports the concat expression. This is an
3182 * operator that just glues the results of many expressions into a
3183 * single value.
3185 * Note that the class stores the parameter expressions in source code
3186 * order. That is, the parm(0) is placed in the most significant
3187 * position of the result.
3189 class NetEConcat : public NetExpr {
3191 public:
3192 NetEConcat(unsigned cnt, NetExpr* repeat =0);
3193 ~NetEConcat();
3195 // Manipulate the parameters.
3196 void set(unsigned idx, NetExpr*e);
3198 unsigned repeat();
3199 unsigned repeat() const;
3200 unsigned nparms() const { return parms_.count() ; }
3201 NetExpr* parm(unsigned idx) const { return parms_[idx]; }
3203 virtual NexusSet* nex_input(bool rem_out = true);
3204 virtual bool has_width() const;
3205 virtual bool set_width(unsigned w, bool last_chance =false);
3206 virtual NetEConcat* dup_expr() const;
3207 virtual NetEConst* eval_tree(int prune_to_width = -1);
3208 virtual NetNet*synthesize(Design*);
3209 virtual void expr_scan(struct expr_scan_t*) const;
3210 virtual void dump(ostream&) const;
3212 private:
3213 svector<NetExpr*>parms_;
3214 NetExpr* repeat_;
3215 unsigned repeat_value_;
3216 bool repeat_calculated_;
3221 * This class is a placeholder for a parameter expression. When
3222 * parameters are first created, an instance of this object is used to
3223 * hold the place where the parameter expression goes. Then, when the
3224 * parameters are resolved, these objects are removed.
3226 * If the parameter object is created with a path and name, then the
3227 * object represents a reference to a parameter that is known to exist.
3229 class NetEParam : public NetExpr {
3230 public:
3231 NetEParam();
3232 NetEParam(class Design*des, NetScope*scope, perm_string name);
3233 ~NetEParam();
3235 virtual NexusSet* nex_input(bool rem_out = true);
3236 virtual bool set_width(unsigned w, bool last_chance);
3237 virtual bool has_width() const;
3238 virtual void expr_scan(struct expr_scan_t*) const;
3239 virtual ivl_variable_type_t expr_type() const;
3240 virtual NetExpr* eval_tree(int prune_to_width = -1);
3241 virtual NetEParam* dup_expr() const;
3243 virtual void dump(ostream&) const;
3245 private:
3246 Design*des_;
3247 NetScope*scope_;
3248 typedef map<perm_string,NetScope::param_expr_t>::iterator ref_t;
3249 ref_t reference_;
3251 NetEParam(class Design*des, NetScope*scope, ref_t ref);
3256 * This expression node supports bit/part selects from general
3257 * expressions. The sub-expression is self-sized, and has bits
3258 * selected from it. The base is the expression that identifies the
3259 * lsb of the expression, and the wid is the width of the part select,
3260 * or 1 for a bit select. No matter what the subexpression is, the
3261 * base is translated in canonical bits. It is up to the elaborator
3262 * to figure this out and adjust the expression if the subexpression
3263 * has a non-canonical base or direction.
3265 * If the base expression is null, then this expression node can be
3266 * used to express width expansion, signed or unsigned depending on
3267 * the has_sign() flag.
3269 class NetESelect : public NetExpr {
3271 public:
3272 NetESelect(NetExpr*exp, NetExpr*base, unsigned wid);
3273 ~NetESelect();
3275 const NetExpr*sub_expr() const;
3276 const NetExpr*select() const;
3278 virtual NexusSet* nex_input(bool rem_out = true);
3279 virtual bool set_width(unsigned w, bool last_chance =false);
3280 virtual bool has_width() const;
3281 virtual void expr_scan(struct expr_scan_t*) const;
3282 virtual NetEConst* eval_tree(int prune_to_width = -1);
3283 virtual NetESelect* dup_expr() const;
3284 virtual NetNet*synthesize(Design*des);
3285 virtual void dump(ostream&) const;
3287 private:
3288 NetExpr*expr_;
3289 NetExpr*base_;
3293 * This node is for representation of named events.
3295 class NetEEvent : public NetExpr {
3297 public:
3298 NetEEvent(NetEvent*);
3299 ~NetEEvent();
3301 const NetEvent* event() const;
3303 virtual void expr_scan(struct expr_scan_t*) const;
3304 virtual NetEEvent* dup_expr() const;
3305 virtual NexusSet* nex_input(bool rem_out = true);
3307 virtual void dump(ostream&os) const;
3309 private:
3310 NetEvent*event_;
3314 * This class is a special (and magical) expression node type that
3315 * represents scope names. These can only be found as parameters to
3316 * NetSTask objects.
3318 class NetEScope : public NetExpr {
3320 public:
3321 NetEScope(NetScope*);
3322 ~NetEScope();
3324 const NetScope* scope() const;
3326 virtual void expr_scan(struct expr_scan_t*) const;
3327 virtual NetEScope* dup_expr() const;
3328 virtual NexusSet* nex_input(bool rem_out = true);
3330 virtual void dump(ostream&os) const;
3332 private:
3333 NetScope*scope_;
3337 * This node represents a system function call in an expression. The
3338 * object contains the name of the system function, which the backend
3339 * uses to do VPI matching.
3341 class NetESFunc : public NetExpr {
3343 public:
3344 NetESFunc(const char*name, ivl_variable_type_t t,
3345 unsigned width, unsigned nprms);
3346 ~NetESFunc();
3348 const char* name() const;
3350 unsigned nparms() const;
3351 void parm(unsigned idx, NetExpr*expr);
3352 NetExpr* parm(unsigned idx);
3353 const NetExpr* parm(unsigned idx) const;
3355 virtual ivl_variable_type_t expr_type() const;
3356 virtual NexusSet* nex_input(bool rem_out = true);
3357 virtual bool set_width(unsigned, bool last_chance);
3358 virtual void dump(ostream&) const;
3360 virtual void expr_scan(struct expr_scan_t*) const;
3361 virtual NetESFunc*dup_expr() const;
3362 virtual NetNet*synthesize(Design*);
3364 private:
3365 const char* name_;
3366 ivl_variable_type_t type_;
3367 unsigned nparms_;
3368 NetExpr**parms_;
3370 private: // not implemented
3371 NetESFunc(const NetESFunc&);
3372 NetESFunc& operator= (const NetESFunc&);
3376 * This class represents the ternary (?:) operator. It has 3
3377 * expressions, one of which is a condition used to select which of
3378 * the other two expressions is the result.
3380 class NetETernary : public NetExpr {
3382 public:
3383 NetETernary(NetExpr*c, NetExpr*t, NetExpr*f);
3384 ~NetETernary();
3386 virtual bool set_width(unsigned w, bool last_chance);
3388 const NetExpr*cond_expr() const;
3389 const NetExpr*true_expr() const;
3390 const NetExpr*false_expr() const;
3392 virtual NetETernary* dup_expr() const;
3393 virtual NetExpr* eval_tree(int prune_to_width = -1);
3395 virtual ivl_variable_type_t expr_type() const;
3396 virtual NexusSet* nex_input(bool rem_out = true);
3397 virtual void expr_scan(struct expr_scan_t*) const;
3398 virtual void dump(ostream&) const;
3399 virtual NetNet*synthesize(Design*);
3401 private:
3402 NetExpr*cond_;
3403 NetExpr*true_val_;
3404 NetExpr*false_val_;
3408 * This class represents a unary operator, with the single operand
3409 * and a single character for the operator. The operator values are:
3411 * ~ -- Bit-wise negation
3412 * ! -- Logical negation
3413 * & -- Reduction AND
3414 * | -- Reduction OR
3415 * ^ -- Reduction XOR
3416 * + --
3417 * - --
3418 * A -- Reduction NAND (~&)
3419 * N -- Reduction NOR (~|)
3420 * X -- Reduction NXOR (~^ or ^~)
3421 * m -- abs(x) (i.e. "magnitude")
3423 class NetEUnary : public NetExpr {
3425 public:
3426 NetEUnary(char op, NetExpr*ex);
3427 ~NetEUnary();
3429 char op() const { return op_; }
3430 const NetExpr* expr() const { return expr_; }
3432 virtual bool set_width(unsigned w, bool last_chance);
3434 virtual NetEUnary* dup_expr() const;
3435 virtual NetExpr* eval_tree(int prune_to_width = -1);
3437 virtual ivl_variable_type_t expr_type() const;
3438 virtual NexusSet* nex_input(bool rem_out = true);
3439 virtual void expr_scan(struct expr_scan_t*) const;
3440 virtual void dump(ostream&) const;
3442 protected:
3443 char op_;
3444 NetExpr* expr_;
3446 private:
3447 virtual NetExpr* eval_tree_real_();
3450 class NetEUBits : public NetEUnary {
3452 public:
3453 NetEUBits(char op, NetExpr*ex);
3454 ~NetEUBits();
3456 virtual NetNet* synthesize(Design*);
3458 virtual NetExpr* eval_tree(int prune_to_width = -1);
3459 virtual ivl_variable_type_t expr_type() const;
3462 class NetEUReduce : public NetEUnary {
3464 public:
3465 NetEUReduce(char op, NetExpr*ex);
3466 ~NetEUReduce();
3468 virtual bool set_width(unsigned w, bool last_chance);
3469 virtual NetNet* synthesize(Design*);
3470 virtual NetEUReduce* dup_expr() const;
3471 virtual NetEConst* eval_tree(int prune_to_width = -1);
3472 virtual ivl_variable_type_t expr_type() const;
3476 * When a signal shows up in an expression, this type represents
3477 * it. From this the expression can get any kind of access to the
3478 * structural signal, including arrays.
3480 * The NetESignal may refer to an array, if the word_index is
3481 * included. This expression calculates the index of the word in the
3482 * array. It may only be nil if the expression refers to the whole
3483 * array, and that is legal only in limited situation.
3485 class NetESignal : public NetExpr {
3487 public:
3488 NetESignal(NetNet*n);
3489 NetESignal(NetNet*n, NetExpr*word_index);
3490 ~NetESignal();
3492 perm_string name() const;
3493 virtual bool set_width(unsigned, bool last_chance);
3495 virtual NetESignal* dup_expr() const;
3496 NetNet* synthesize(Design*des);
3497 NexusSet* nex_input(bool rem_out = true);
3499 // This is the expression for selecting an array word, if this
3500 // signal refers to an array.
3501 const NetExpr* word_index() const;
3503 // This is the width of the vector that this signal refers to.
3504 unsigned vector_width() const;
3505 // Point back to the signal that this expression node references.
3506 const NetNet* sig() const;
3507 NetNet* sig();
3508 // Declared vector dimensions for the signal.
3509 unsigned msi() const;
3510 unsigned lsi() const;
3512 virtual ivl_variable_type_t expr_type() const;
3514 virtual void expr_scan(struct expr_scan_t*) const;
3515 virtual void dump(ostream&) const;
3517 private:
3518 NetNet*net_;
3519 // Expression to select a word from the net.
3520 NetExpr*word_;
3525 * This class contains an entire design. It includes processes and a
3526 * netlist, and can be passed around from function to function.
3528 class Design {
3530 public:
3531 Design();
3532 ~Design();
3535 /* The flags are a generic way of accepting command line
3536 parameters/flags and passing them to the processing steps
3537 that deal with the design. The compilation driver sets the
3538 entire flags map after elaboration is done. Subsequent
3539 steps can then use the get_flag() function to get the value
3540 of an interesting key. */
3542 void set_flags(const map<string,const char*>&f) { flags_ = f; }
3544 const char* get_flag(const string&key) const;
3546 NetScope* make_root_scope(perm_string name);
3547 NetScope* find_root_scope();
3548 list<NetScope*> find_root_scopes();
3550 const list<NetScope*> find_root_scopes() const;
3552 /* Attempt to set the precision to the specified value. If the
3553 precision is already more precise, the keep the precise
3554 setting. This is intended to hold the simulation precision
3555 for use throughout the entire design. */
3557 void set_precision(int val);
3558 int get_precision() const;
3560 /* This function takes a delay value and a scope, and returns
3561 the delay value scaled to the precision of the design. */
3562 uint64_t scale_to_precision(uint64_t, const NetScope*)const;
3564 /* Look up a scope. If no starting scope is passed, then the
3565 path is taken as an absolute scope name. Otherwise, the
3566 scope is located starting at the passed scope and working
3567 up if needed. */
3568 NetScope* find_scope(const std::list<hname_t>&path) const;
3569 NetScope* find_scope(NetScope*, const std::list<hname_t>&path,
3570 NetScope::TYPE type = NetScope::MODULE) const;
3572 // PARAMETERS
3574 void run_defparams();
3575 void evaluate_parameters();
3577 /* This method locates a signal, starting at a given
3578 scope. The name parameter may be partially hierarchical, so
3579 this method, unlike the NetScope::find_signal method,
3580 handles global name binding. */
3582 NetNet*find_signal(NetScope*scope, pform_name_t path);
3584 // Functions
3585 NetFuncDef* find_function(NetScope*scope, const pform_name_t&key);
3587 // Tasks
3588 NetScope* find_task(NetScope*scope, const pform_name_t&name);
3590 // NODES
3591 void add_node(NetNode*);
3592 void del_node(NetNode*);
3594 // PROCESSES
3595 void add_process(NetProcTop*);
3596 void delete_process(NetProcTop*);
3598 // Iterate over the design...
3599 void dump(ostream&) const;
3600 void functor(struct functor_t*);
3601 int emit(struct target_t*) const;
3603 // This is incremented by elaboration when an error is
3604 // detected. It prevents code being emitted.
3605 unsigned errors;
3607 public:
3608 string local_symbol(const string&path);
3610 private:
3611 // Keep a tree of scopes. The NetScope class handles the wide
3612 // tree and per-hop searches for me.
3613 list<NetScope*>root_scopes_;
3615 // List the nodes in the design.
3616 NetNode*nodes_;
3617 // These are in support of the node functor iterator.
3618 NetNode*nodes_functor_cur_;
3619 NetNode*nodes_functor_nxt_;
3621 // List the processes in the design.
3622 NetProcTop*procs_;
3623 NetProcTop*procs_idx_;
3625 map<string,const char*> flags_;
3627 int des_precision_;
3629 unsigned lcounter_;
3631 private: // not implemented
3632 Design(const Design&);
3633 Design& operator= (const Design&);
3637 /* =======
3640 inline bool operator == (const Link&l, const Link&r)
3641 { return l.is_equal(r); }
3643 inline bool operator != (const Link&l, const Link&r)
3644 { return ! l.is_equal(r); }
3646 /* Connect the pins of two nodes together. Either may already be
3647 connected to other things, connect is transitive. */
3648 extern void connect(Link&, Link&);
3650 /* Return true if l and r are connected. */
3651 inline bool connected(const Link&l, const Link&r)
3652 { return l.is_linked(r); }
3654 /* Return the number of links in the ring that are of the specified
3655 type. */
3656 extern unsigned count_inputs(const Link&pin);
3657 extern unsigned count_outputs(const Link&pin);
3658 extern unsigned count_signals(const Link&pin);
3660 /* Find the next link that is an output into the nexus. */
3661 extern Link* find_next_output(Link*lnk);
3663 /* Find the signal connected to the given node pin. There should
3664 always be exactly one signal. The bidx parameter gets filled with
3665 the signal index of the Net, in case it is a vector. */
3666 const NetNet* find_link_signal(const NetObj*net, unsigned pin,
3667 unsigned&bidx);
3669 inline ostream& operator << (ostream&o, const NetExpr&exp)
3670 { exp.dump(o); return o; }
3672 extern ostream& operator << (ostream&, NetNet::Type);
3675 * Manipulator to dump a scope complete path to the output. The
3676 * manipulator is "scope_path" and works like this:
3678 * out << .... << scope_path(sc) << ... ;
3680 struct __ScopePathManip { const NetScope*scope; };
3681 inline __ScopePathManip scope_path(const NetScope*scope)
3682 { __ScopePathManip tmp; tmp.scope = scope; return tmp; }
3684 extern ostream& operator << (ostream&o, __ScopePathManip);
3686 #endif