Merge branch 'master' into verilog-ams
[sverilog.git] / t-dll.h
blob247a5dae7017338999cba675ea0a527964d3656a
1 #ifndef __t_dll_H
2 #define __t_dll_H
3 /*
4 * Copyright (c) 2000-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
22 # include "target.h"
23 # include "ivl_target.h"
24 # include "StringHeap.h"
25 # include "netlist.h"
26 # include <vector>
28 #if defined(__MINGW32__)
29 #include <windows.h>
30 typedef void *ivl_dll_t;
31 #elif defined(HAVE_DLFCN_H)
32 # include <dlfcn.h>
33 typedef void* ivl_dll_t;
34 #elif defined(HAVE_DL_H)
35 # include <dl.h>
36 typedef shl_t ivl_dll_t;
37 #else
38 # error No DLL stub support for this target.
39 #endif
41 struct ivl_design_s {
43 int time_precision;
45 ivl_scope_t *roots_;
46 unsigned nroots_;
48 ivl_process_t threads_;
50 ivl_net_const_t*consts;
51 unsigned nconsts;
53 const Design*self;
57 * The DLL target type loads a named object file to handle the process
58 * of scanning the netlist. When it is time to start the design, I
59 * locate and link in the desired DLL, then start calling methods. The
60 * DLL will call me back to get information out of the netlist in
61 * particular.
63 struct dll_target : public target_t, public expr_scan_t {
65 bool start_design(const Design*);
66 int end_design(const Design*);
68 bool bufz(const NetBUFZ*);
69 void event(const NetEvent*);
70 void logic(const NetLogic*);
71 bool tran(const NetTran*);
72 bool ureduce(const NetUReduce*);
73 void net_case_cmp(const NetCaseCmp*);
74 void udp(const NetUDP*);
75 void lpm_abs(const NetAbs*);
76 void lpm_add_sub(const NetAddSub*);
77 bool lpm_array_dq(const NetArrayDq*);
78 void lpm_clshift(const NetCLShift*);
79 void lpm_compare(const NetCompare*);
80 void lpm_divide(const NetDivide*);
81 void lpm_ff(const NetFF*);
82 void lpm_modulo(const NetModulo*);
83 void lpm_mult(const NetMult*);
84 void lpm_mux(const NetMux*);
85 void lpm_pow(const NetPow*);
86 bool concat(const NetConcat*);
87 bool part_select(const NetPartSelect*);
88 bool replicate(const NetReplicate*);
89 void net_assign(const NetAssign_*);
90 bool net_sysfunction(const NetSysFunc*);
91 bool net_function(const NetUserFunc*);
92 bool net_const(const NetConst*);
93 bool net_literal(const NetLiteral*);
94 void net_probe(const NetEvProbe*);
95 bool sign_extend(const NetSignExtend*);
97 bool process(const NetProcTop*);
98 void scope(const NetScope*);
99 void signal(const NetNet*);
100 bool signal_paths(const NetNet*);
101 ivl_dll_t dll_;
103 ivl_design_s des_;
105 target_design_f target_;
108 /* These methods and members are used for forming the
109 statements of a thread. */
110 struct ivl_statement_s*stmt_cur_;
111 void proc_assign(const NetAssign*);
112 void proc_assign_nb(const NetAssignNB*);
113 bool proc_block(const NetBlock*);
114 void proc_case(const NetCase*);
115 bool proc_cassign(const NetCAssign*);
116 bool proc_condit(const NetCondit*);
117 bool proc_deassign(const NetDeassign*);
118 bool proc_delay(const NetPDelay*);
119 bool proc_disable(const NetDisable*);
120 bool proc_force(const NetForce*);
121 void proc_forever(const NetForever*);
122 bool proc_release(const NetRelease*);
123 void proc_repeat(const NetRepeat*);
124 void proc_stask(const NetSTask*);
125 bool proc_trigger(const NetEvTrig*);
126 void proc_utask(const NetUTask*);
127 bool proc_wait(const NetEvWait*);
128 void proc_while(const NetWhile*);
130 bool func_def(const NetScope*);
131 void task_def(const NetScope*);
133 struct ivl_expr_s*expr_;
134 void expr_binary(const NetEBinary*);
135 void expr_concat(const NetEConcat*);
136 void expr_const(const NetEConst*);
137 void expr_creal(const NetECReal*);
138 void expr_param(const NetEConstParam*);
139 void expr_event(const NetEEvent*);
140 void expr_scope(const NetEScope*);
141 void expr_select(const NetESelect*);
142 void expr_sfunc(const NetESFunc*);
143 void expr_ternary(const NetETernary*);
144 void expr_ufunc(const NetEUFunc*);
145 void expr_unary(const NetEUnary*);
146 void expr_signal(const NetESignal*);
148 ivl_scope_t lookup_scope_(const NetScope*scope);
150 ivl_attribute_s* fill_in_attributes(const Attrib*net);
151 void switch_attributes(struct ivl_switch_s *obj, const NetNode*net);
152 void logic_attributes(struct ivl_net_logic_s *obj, const NetNode*net);
154 private:
155 StringHeap strings_;
157 static ivl_scope_t find_scope(ivl_design_s &des, const NetScope*cur);
158 static ivl_signal_t find_signal(ivl_design_s &des, const NetNet*net);
159 static ivl_parameter_t scope_find_param(ivl_scope_t scope,
160 const char*name);
162 void add_root(ivl_design_s &des_, const NetScope *s);
164 void make_assign_lvals_(const NetAssignBase*net);
165 void sub_off_from_expr_(long);
166 void mul_expr_by_const_(long);
168 void make_logic_delays_(struct ivl_net_logic_s*obj, const NetObj*net);
169 void make_lpm_delays_(struct ivl_lpm_s*obj, const NetObj*net);
170 void make_const_delays_(struct ivl_net_const_s*obj, const NetObj*net);
171 void make_scope_parameters(ivl_scope_t scope, const NetScope*net);
172 void make_scope_param_expr(ivl_parameter_t cur_par, NetExpr*etmp);
174 static ivl_expr_t expr_from_value_(const verinum&that);
178 * These are various private declarations used by the t-dll target.
181 struct ivl_delaypath_s {
182 ivl_scope_t scope;
183 ivl_nexus_t src;
184 ivl_nexus_t condit;
185 bool conditional;
186 bool posedge;
187 bool negedge;
188 uint64_t delay[12];
191 struct ivl_event_s {
192 perm_string name;
193 ivl_scope_t scope;
194 unsigned nany, nneg, npos;
195 ivl_nexus_t*pins;
199 * The ivl_expr_t is an opaque reference to one of these
200 * structures. This structure holds all the information we need about
201 * an expression node, including its type, the expression width, and
202 * type specific properties.
204 struct ivl_expr_s {
205 ivl_expr_type_t type_;
206 ivl_variable_type_t value_;
207 perm_string file;
208 unsigned lineno;
210 unsigned width_;
211 unsigned signed_ : 1;
213 union {
214 struct {
215 char op_;
216 ivl_expr_t lef_;
217 ivl_expr_t rig_;
218 } binary_;
220 struct {
221 unsigned rept;
222 unsigned parms;
223 ivl_expr_t*parm;
224 } concat_;
226 struct {
227 char*bits_;
228 ivl_parameter_t parameter;
229 } number_;
231 struct {
232 ivl_event_t event;
233 } event_;
235 struct {
236 ivl_scope_t scope;
237 } scope_;
239 struct {
240 ivl_signal_t sig;
241 ivl_expr_t word;
242 } signal_;
244 struct {
245 const char *name_;
246 ivl_expr_t *parm;
247 unsigned parms;
248 } sfunc_;
250 struct {
251 char*value_;
252 ivl_parameter_t parameter;
253 } string_;
255 struct {
256 ivl_expr_t cond;
257 ivl_expr_t true_e;
258 ivl_expr_t false_e;
259 } ternary_;
261 struct {
262 ivl_memory_t mem_;
263 ivl_expr_t idx_;
264 } memory_;
266 struct {
267 ivl_scope_t def;
268 ivl_expr_t *parm;
269 unsigned parms;
270 } ufunc_;
272 struct {
273 unsigned long value;
274 } ulong_;
276 struct {
277 double value;
278 ivl_parameter_t parameter;
279 } real_;
281 struct {
282 char op_;
283 ivl_expr_t sub_;
284 } unary_;
286 } u_;
290 * LPM devices are handled by this suite of types. The ivl_lpm_s
291 * structure holds the core, including a type code, the object name
292 * and scope. The other properties of the device are held in the type
293 * specific member of the union.
296 struct ivl_lpm_s {
297 ivl_lpm_type_t type;
298 ivl_scope_t scope;
299 perm_string name;
300 perm_string file;
301 unsigned lineno;
302 // Value returned by ivl_lpm_width;
303 unsigned width;
304 ivl_expr_t delay[3];
306 union {
307 struct ivl_lpm_ff_s {
308 ivl_nexus_t clk;
309 ivl_nexus_t we;
310 ivl_nexus_t aclr;
311 ivl_nexus_t aset;
312 ivl_nexus_t sclr;
313 ivl_nexus_t sset;
314 union {
315 ivl_nexus_t*pins;
316 ivl_nexus_t pin;
317 } q;
318 union {
319 ivl_nexus_t*pins;
320 ivl_nexus_t pin;
321 } d;
322 ivl_expr_t aset_value;
323 ivl_expr_t sset_value;
324 } ff;
326 struct ivl_lpm_mux_s {
327 unsigned size;
328 unsigned swid;
329 ivl_nexus_t*d;
330 ivl_nexus_t q, s;
331 } mux;
333 struct ivl_lpm_shift_s {
334 unsigned select;
335 unsigned signed_flag :1;
336 ivl_nexus_t q, d, s;
337 } shift;
339 struct ivl_lpm_arith_s {
340 unsigned signed_flag :1;
341 ivl_nexus_t q, a, b;
342 } arith;
344 struct ivl_lpm_array_s {
345 ivl_signal_t sig;
346 unsigned swid;
347 ivl_nexus_t q, a;
348 } array;
350 struct ivl_concat_s {
351 unsigned inputs;
352 ivl_nexus_t*pins;
353 } concat;
355 struct ivl_part_s {
356 unsigned base;
357 unsigned signed_flag :1;
358 ivl_nexus_t q, a, s;
359 } part;
361 // IVL_LPM_RE_* and IVL_LPM_SIGN_EXT use this.
362 struct ivl_lpm_reduce_s {
363 ivl_nexus_t q, a;
364 } reduce;
366 struct ivl_lpm_repeat_s {
367 unsigned count;
368 ivl_nexus_t q, a;
369 } repeat;
371 struct ivl_lpm_sfunc_s {
372 const char* fun_name;
373 unsigned ports;
374 ivl_nexus_t*pins;
375 } sfunc;
377 struct ivl_lpm_ufunc_s {
378 ivl_scope_t def;
379 unsigned ports;
380 ivl_nexus_t*pins;
381 } ufunc;
382 } u_;
386 * This object represents l-values to assignments. The l-value can be
387 * a register bit or part select, or a memory word select with a part
388 * select.
391 enum ivl_lval_type_t {
392 IVL_LVAL_REG = 0,
393 IVL_LVAL_MUX = 1,
394 /* IVL_LVAL_MEM = 2, / Deprecated in favor of LVAL_ARR? */
395 IVL_LVAL_NET = 3, /* Only force can have NET l-values */
396 IVL_LVAL_ARR = 4
399 struct ivl_lval_s {
400 ivl_expr_t loff;
401 ivl_expr_t idx;
402 unsigned width_;
403 unsigned type_ : 8;
404 union {
405 ivl_signal_t sig;
406 ivl_memory_t mem;
407 } n;
411 * This object represents a literal constant, possibly signed, in a
412 * structural context.
414 struct ivl_net_const_s {
415 ivl_variable_type_t type;
416 unsigned width_;
417 unsigned signed_ : 1;
419 union {
420 double real_value;
421 char bit_[sizeof(char*)];
422 char *bits_;
423 } b;
425 ivl_nexus_t pin_;
427 ivl_expr_t delay[3];
431 * Logic gates (just about everything that has a single output) are
432 * represented structurally by instances of this object.
434 struct ivl_net_logic_s {
435 ivl_logic_t type_;
436 unsigned width_;
437 ivl_udp_t udp;
439 perm_string name_;
440 ivl_scope_t scope_;
442 unsigned npins_;
443 ivl_nexus_t*pins_;
445 struct ivl_attribute_s*attr;
446 unsigned nattr;
448 ivl_expr_t delay[3];
451 struct ivl_switch_s {
452 ivl_switch_type_t type;
454 perm_string name;
455 ivl_scope_t scope;
457 struct ivl_attribute_s*attr;
458 unsigned nattr;
460 ivl_nexus_t pins[3];
461 perm_string file;
462 unsigned lineno;
466 * UDP definition.
468 struct ivl_udp_s {
469 perm_string name;
470 unsigned nin;
471 int sequ; /* boolean */
472 char init;
473 unsigned nrows;
474 typedef const char*ccharp_t;
475 ccharp_t*table; // zero terminated array of pointers
479 * The ivl_nexus_t is a single-bit link of some number of pins of
480 * devices. the __nexus_ptr structure is a helper that actually does
481 * the pointing.
483 * The type_ member specifies which of the object pointers in the
484 * union are valid.
486 * The drive01 members gives the strength of the drive that the device
487 * is applying to the nexus, with 0 HiZ and 3 supply. If the pin is an
488 * input to the device, then the drives are both HiZ.
490 struct ivl_nexus_ptr_s {
491 unsigned pin_;
492 unsigned type_ : 8;
493 unsigned drive0 : 3;
494 unsigned drive1 : 3;
495 union {
496 ivl_signal_t sig; /* type 0 */
497 ivl_net_logic_t log; /* type 1 */
498 ivl_net_const_t con; /* type 2 */
499 ivl_lpm_t lpm; /* type 3 */
500 ivl_switch_t swi; /* type 4 */
501 } l;
503 # define __NEXUS_PTR_SIG 0
504 # define __NEXUS_PTR_LOG 1
505 # define __NEXUS_PTR_CON 2
506 # define __NEXUS_PTR_LPM 3
507 # define __NEXUS_PTR_SWI 4
510 * NOTE: ONLY allocate ivl_nexus_s objects with the included "new" operator.
512 struct ivl_nexus_s {
513 unsigned nptr_;
514 struct ivl_nexus_ptr_s*ptrs_;
515 const Nexus*nexus_;
516 const char*name_;
517 void*private_data;
519 void* operator new (size_t s);
520 void operator delete(void*obj, size_t s); // Not implemented
524 * This is the implementation of a parameter. Each scope has a list of
525 * these.
527 struct ivl_parameter_s {
528 perm_string basename;
529 ivl_scope_t scope;
530 ivl_expr_t value;
531 perm_string file;
532 unsigned lineno;
535 * All we know about a process it its type (initial or always) and the
536 * single statement that is it. A process also has a scope, although
537 * that generally only matters for VPI calls.
539 struct ivl_process_s {
540 ivl_process_type_t type_;
541 ivl_scope_t scope_;
542 ivl_statement_t stmt_;
544 struct ivl_attribute_s*attr;
545 unsigned nattr;
547 ivl_process_t next_;
551 * Scopes are kept in a tree. Each scope points to its first child,
552 * and also to any siblings. Thus a parent can scan all its children
553 * by following its child pointer then following sibling pointers from
554 * there.
556 struct ivl_scope_s {
557 ivl_scope_t child_, sibling_, parent;
559 perm_string name_;
560 perm_string tname_;
561 perm_string file;
562 perm_string def_file;
563 unsigned lineno;
564 unsigned def_lineno;
565 ivl_scope_type_t type_;
567 unsigned nsigs_;
568 ivl_signal_t*sigs_;
570 unsigned nlog_;
571 ivl_net_logic_t*log_;
573 unsigned nevent_;
574 ivl_event_t* event_;
576 unsigned nlpm_;
577 ivl_lpm_t* lpm_;
579 unsigned nparam_;
580 ivl_parameter_t param_;
582 /* Scopes that are tasks/functions have a definition. */
583 ivl_statement_t def;
585 unsigned ports;
586 ivl_signal_t*port;
588 std::vector<ivl_switch_t>switches;
590 signed int time_precision :8;
591 signed int time_units :8;
593 struct ivl_attribute_s*attr;
594 unsigned nattr;
598 * A signal is a thing like a wire, a reg, or whatever. It has a type,
599 * and if it is a port is also has a direction. Signals are collected
600 * into scopes (which also point back to me) and have pins that
601 * connect to the rest of the netlist.
603 struct ivl_signal_s {
604 ivl_signal_type_t type_;
605 ivl_signal_port_t port_;
606 ivl_variable_type_t data_type;
607 perm_string file;
608 unsigned lineno;
610 unsigned width_;
611 unsigned signed_ : 1;
612 unsigned isint_ : 1;
613 unsigned local_ : 1;
615 /* For now, support only 0 or 1 array dimensions. */
616 unsigned array_dimensions_ : 1;
618 /* These encode the run-time index for the least significant
619 bit, and the distance to the second bit. */
620 signed lsb_index;
621 signed lsb_dist;
623 perm_string name_;
624 ivl_scope_t scope_;
626 unsigned array_words;
627 int array_base;
628 union {
629 ivl_nexus_t pin;
630 ivl_nexus_t*pins;
633 ivl_delaypath_s*path;
634 unsigned npath;
636 struct ivl_attribute_s*attr;
637 unsigned nattr;
641 * The ivl_statement_t represents any statement. The type of statement
642 * is defined by the ivl_statement_type_t enumeration. Given the type,
643 * certain information about the statement may be available.
645 struct ivl_statement_s {
646 enum ivl_statement_type_e type_;
647 perm_string file;
648 unsigned lineno;
650 union {
651 struct { /* IVL_ST_ASSIGN IVL_ST_ASSIGN_NB
652 IVL_ST_CASSIGN, IVL_ST_DEASSIGN */
653 unsigned lvals_;
654 struct ivl_lval_s*lval_;
655 ivl_expr_t rval_;
656 ivl_expr_t delay;
657 } assign_;
659 struct { /* IVL_ST_BLOCK, IVL_ST_FORK */
660 struct ivl_statement_s*stmt_;
661 unsigned nstmt_;
662 ivl_scope_t scope;
663 } block_;
665 struct { /* IVL_ST_CASE, IVL_ST_CASEX, IVL_ST_CASEZ */
666 ivl_expr_t cond;
667 unsigned ncase;
668 ivl_expr_t*case_ex;
669 struct ivl_statement_s*case_st;
670 } case_;
672 struct { /* IVL_ST_CONDIT */
673 /* This is the condition expression */
674 ivl_expr_t cond_;
675 /* This is two statements, the true and false. */
676 struct ivl_statement_s*stmt_;
677 } condit_;
679 struct { /* IVL_ST_DELAY */
680 uint64_t delay_;
681 ivl_statement_t stmt_;
682 } delay_;
684 struct { /* IVL_ST_DELAYX */
685 ivl_expr_t expr; /* XXXX */
686 ivl_statement_t stmt_;
687 } delayx_;
689 struct { /* IVL_ST_DISABLE */
690 ivl_scope_t scope;
691 } disable_;
693 struct { /* IVL_ST_FOREVER */
694 ivl_statement_t stmt_;
695 } forever_;
697 struct { /* IVL_ST_STASK */
698 const char*name_;
699 unsigned nparm_;
700 ivl_expr_t*parms_;
701 } stask_;
703 struct { /* IVL_ST_UTASK */
704 ivl_scope_t def;
705 } utask_;
707 struct { /* IVL_ST_TRIGGER IVL_ST_WAIT */
708 unsigned nevent;
709 union {
710 ivl_event_t event;
711 ivl_event_t*events;
713 ivl_statement_t stmt_;
714 } wait_;
716 struct { /* IVL_ST_WHILE IVL_ST_REPEAT */
717 ivl_expr_t cond_;
718 ivl_statement_t stmt_;
719 } while_;
720 } u_;
724 * The FILE_NAME function is a shorthand for attaching file/line
725 * information to the statement object.
727 static inline void FILE_NAME(ivl_statement_t stmt, const LineInfo*info)
729 stmt->file = info->get_file();
730 stmt->lineno = info->get_lineno();
733 static inline void FILE_NAME(ivl_expr_t expr, const LineInfo*info)
735 expr->file = info->get_file();
736 expr->lineno = info->get_lineno();
739 static inline void FILE_NAME(ivl_lpm_t lpm, const LineInfo*info)
741 lpm->file = info->get_file();
742 lpm->lineno = info->get_lineno();
745 static inline void FILE_NAME(ivl_scope_t scope, const NetScope*info)
747 scope->file = info->get_file();
748 scope->def_file = info->get_def_file();
749 scope->lineno = info->get_lineno();
750 scope->def_lineno = info->get_def_lineno();
753 static inline void FILE_NAME(ivl_switch_t net, const LineInfo*info)
755 net->file = info->get_file();
756 net->lineno = info->get_lineno();
759 #endif