Fix for assertion error when expanding macro.
[iverilog.git] / t-dll.h
blobfaea12b63a15d175e164f7082597e40778142acb
1 #ifndef __t_dll_H
2 #define __t_dll_H
3 /*
4 * Copyright (c) 2000-2004 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
21 #ifdef HAVE_CVS_IDENT
22 #ident "$Id: t-dll.h,v 1.143 2007/06/02 03:42:13 steve Exp $"
23 #endif
25 # include "target.h"
26 # include "ivl_target.h"
27 # include "StringHeap.h"
28 # include "netlist.h"
30 #if defined(__MINGW32__)
31 #include <windows.h>
32 typedef void *ivl_dll_t;
33 #elif defined(HAVE_DLFCN_H)
34 # include <dlfcn.h>
35 typedef void* ivl_dll_t;
36 #elif defined(HAVE_DL_H)
37 # include <dl.h>
38 typedef shl_t ivl_dll_t;
39 #else
40 # error No DLL stub support for this target.
41 #endif
43 struct ivl_design_s {
45 int time_precision;
47 ivl_scope_t *roots_;
48 unsigned nroots_;
50 ivl_process_t threads_;
52 ivl_net_const_t*consts;
53 unsigned nconsts;
55 const Design*self;
59 * The DLL target type loads a named object file to handle the process
60 * of scanning the netlist. When it is time to start the design, I
61 * locate and link in the desired DLL, then start calling methods. The
62 * DLL will call me back to get information out of the netlist in
63 * particular.
65 struct dll_target : public target_t, public expr_scan_t {
67 bool start_design(const Design*);
68 int end_design(const Design*);
70 bool bufz(const NetBUFZ*);
71 void event(const NetEvent*);
72 void logic(const NetLogic*);
73 bool ureduce(const NetUReduce*);
74 void net_case_cmp(const NetCaseCmp*);
75 void udp(const NetUDP*);
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 bool concat(const NetConcat*);
86 bool part_select(const NetPartSelect*);
87 bool replicate(const NetReplicate*);
88 void net_assign(const NetAssign_*);
89 bool net_sysfunction(const NetSysFunc*);
90 bool net_function(const NetUserFunc*);
91 bool net_const(const NetConst*);
92 bool net_literal(const NetLiteral*);
93 void net_probe(const NetEvProbe*);
94 bool sign_extend(const NetSignExtend*);
96 bool process(const NetProcTop*);
97 void scope(const NetScope*);
98 void signal(const NetNet*);
99 bool signal_paths(const NetNet*);
100 ivl_dll_t dll_;
102 ivl_design_s des_;
104 target_design_f target_;
107 /* These methods and members are used for forming the
108 statements of a thread. */
109 struct ivl_statement_s*stmt_cur_;
110 void proc_assign(const NetAssign*);
111 void proc_assign_nb(const NetAssignNB*);
112 bool proc_block(const NetBlock*);
113 void proc_case(const NetCase*);
114 bool proc_cassign(const NetCAssign*);
115 bool proc_condit(const NetCondit*);
116 bool proc_deassign(const NetDeassign*);
117 bool proc_delay(const NetPDelay*);
118 bool proc_disable(const NetDisable*);
119 bool proc_force(const NetForce*);
120 void proc_forever(const NetForever*);
121 bool proc_release(const NetRelease*);
122 void proc_repeat(const NetRepeat*);
123 void proc_stask(const NetSTask*);
124 bool proc_trigger(const NetEvTrig*);
125 void proc_utask(const NetUTask*);
126 bool proc_wait(const NetEvWait*);
127 void proc_while(const NetWhile*);
129 bool func_def(const NetScope*);
130 void task_def(const NetScope*);
132 struct ivl_expr_s*expr_;
133 void expr_binary(const NetEBinary*);
134 void expr_concat(const NetEConcat*);
135 void expr_const(const NetEConst*);
136 void expr_creal(const NetECReal*);
137 void expr_param(const NetEConstParam*);
138 void expr_event(const NetEEvent*);
139 void expr_scope(const NetEScope*);
140 void expr_select(const NetESelect*);
141 void expr_sfunc(const NetESFunc*);
142 void expr_ternary(const NetETernary*);
143 void expr_ufunc(const NetEUFunc*);
144 void expr_unary(const NetEUnary*);
145 void expr_signal(const NetESignal*);
147 ivl_scope_t lookup_scope_(const NetScope*scope);
149 ivl_attribute_s* fill_in_attributes(const Attrib*net);
150 void logic_attributes(struct ivl_net_logic_s *obj, const NetNode*net);
152 private:
153 StringHeap strings_;
155 static ivl_scope_t find_scope(ivl_design_s &des, const NetScope*cur);
156 static ivl_signal_t find_signal(ivl_design_s &des, const NetNet*net);
157 static ivl_parameter_t scope_find_param(ivl_scope_t scope,
158 const char*name);
160 void add_root(ivl_design_s &des_, const NetScope *s);
162 void make_assign_lvals_(const NetAssignBase*net);
163 void sub_off_from_expr_(long);
164 void mul_expr_by_const_(long);
166 void make_logic_delays_(struct ivl_net_logic_s*obj, const NetObj*net);
167 void make_scope_parameters(ivl_scope_t scope, const NetScope*net);
168 void make_scope_param_expr(ivl_parameter_t cur_par, NetExpr*etmp);
170 static ivl_expr_t expr_from_value_(const verinum&that);
174 * These are various private declarations used by the t-dll target.
177 struct ivl_delaypath_s {
178 ivl_scope_t scope;
179 ivl_nexus_t src;
180 ivl_nexus_t condit;
181 bool posedge;
182 bool negedge;
183 uint64_t delay[12];
186 struct ivl_event_s {
187 perm_string name;
188 ivl_scope_t scope;
189 unsigned nany, nneg, npos;
190 ivl_nexus_t*pins;
194 * The ivl_expr_t is an opaque reference to one of these
195 * structures. This structure holds all the information we need about
196 * an expression node, including its type, the expression width, and
197 * type specific properties.
199 struct ivl_expr_s {
200 ivl_expr_type_t type_;
201 ivl_variable_type_t value_;
203 unsigned width_;
204 unsigned signed_ : 1;
206 union {
207 struct {
208 char op_;
209 ivl_expr_t lef_;
210 ivl_expr_t rig_;
211 } binary_;
213 struct {
214 unsigned rept;
215 unsigned parms;
216 ivl_expr_t*parm;
217 } concat_;
219 struct {
220 char*bits_;
221 ivl_parameter_t parameter;
222 } number_;
224 struct {
225 ivl_event_t event;
226 } event_;
228 struct {
229 ivl_scope_t scope;
230 } scope_;
232 struct {
233 ivl_signal_t sig;
234 ivl_expr_t word;
235 } signal_;
237 struct {
238 const char *name_;
239 ivl_expr_t *parm;
240 unsigned parms;
241 } sfunc_;
243 struct {
244 char*value_;
245 ivl_parameter_t parameter;
246 } string_;
248 struct {
249 ivl_expr_t cond;
250 ivl_expr_t true_e;
251 ivl_expr_t false_e;
252 } ternary_;
254 struct {
255 ivl_memory_t mem_;
256 ivl_expr_t idx_;
257 } memory_;
259 struct {
260 ivl_scope_t def;
261 ivl_expr_t *parm;
262 unsigned parms;
263 } ufunc_;
265 struct {
266 unsigned long value;
267 } ulong_;
269 struct {
270 double value;
271 ivl_parameter_t parameter;
272 } real_;
274 struct {
275 char op_;
276 ivl_expr_t sub_;
277 } unary_;
279 } u_;
283 * LPM devices are handled by this suite of types. The ivl_lpm_s
284 * structure holds the core, including a type code, the object name
285 * and scope. The other properties of the device are held in the type
286 * specific member of the union.
289 struct ivl_lpm_s {
290 ivl_lpm_type_t type;
291 ivl_scope_t scope;
292 perm_string name;
293 // Value returned by ivl_lpm_width;
294 unsigned width;
296 union {
297 struct ivl_lpm_ff_s {
298 ivl_nexus_t clk;
299 ivl_nexus_t we;
300 ivl_nexus_t aclr;
301 ivl_nexus_t aset;
302 ivl_nexus_t sclr;
303 ivl_nexus_t sset;
304 union {
305 ivl_nexus_t*pins;
306 ivl_nexus_t pin;
307 } q;
308 union {
309 ivl_nexus_t*pins;
310 ivl_nexus_t pin;
311 } d;
312 ivl_expr_t aset_value;
313 ivl_expr_t sset_value;
314 } ff;
316 struct ivl_lpm_mux_s {
317 unsigned size;
318 unsigned swid;
319 ivl_nexus_t*d;
320 ivl_nexus_t q, s;
321 } mux;
323 struct ivl_lpm_shift_s {
324 unsigned select;
325 unsigned signed_flag :1;
326 ivl_nexus_t q, d, s;
327 } shift;
329 struct ivl_lpm_arith_s {
330 unsigned signed_flag :1;
331 ivl_nexus_t q, a, b;
332 } arith;
334 struct ivl_lpm_array_s {
335 ivl_signal_t sig;
336 unsigned swid;
337 ivl_nexus_t q, a;
338 } array;
340 struct ivl_concat_s {
341 unsigned inputs;
342 ivl_nexus_t*pins;
343 } concat;
345 struct ivl_part_s {
346 unsigned base;
347 unsigned signed_flag :1;
348 ivl_nexus_t q, a, s;
349 } part;
351 // IVL_LPM_RE_* and IVL_LPM_SIGN_EXT use this.
352 struct ivl_lpm_reduce_s {
353 ivl_nexus_t q, a;
354 } reduce;
356 struct ivl_lpm_repeat_s {
357 unsigned count;
358 ivl_nexus_t q, a;
359 } repeat;
361 struct ivl_lpm_sfunc_s {
362 const char* fun_name;
363 unsigned ports;
364 ivl_nexus_t*pins;
365 } sfunc;
367 struct ivl_lpm_ufunc_s {
368 ivl_scope_t def;
369 unsigned ports;
370 ivl_nexus_t*pins;
371 } ufunc;
372 } u_;
376 * This object represents l-values to assignments. The l-value can be
377 * a register bit or part select, or a memory word select with a part
378 * select.
381 enum ivl_lval_type_t {
382 IVL_LVAL_REG = 0,
383 IVL_LVAL_MUX = 1,
384 /* IVL_LVAL_MEM = 2, / Deprecated in favor of LVAL_ARR? */
385 IVL_LVAL_NET = 3, /* Only force can have NET l-values */
386 IVL_LVAL_ARR = 4
389 struct ivl_lval_s {
390 ivl_expr_t loff;
391 ivl_expr_t idx;
392 unsigned width_;
393 unsigned type_ : 8;
394 union {
395 ivl_signal_t sig;
396 ivl_memory_t mem;
397 } n;
401 * This object represents a literal constant, possibly signed, in a
402 * structural context.
404 struct ivl_net_const_s {
405 ivl_variable_type_t type;
406 unsigned width_;
407 unsigned signed_ : 1;
409 union {
410 double real_value;
411 char bit_[sizeof(char*)];
412 char *bits_;
413 } b;
415 ivl_nexus_t pin_;
419 * Logic gates (just about everything that has a single output) are
420 * represented structurally by instances of this object.
422 struct ivl_net_logic_s {
423 ivl_logic_t type_;
424 unsigned width_;
425 ivl_udp_t udp;
427 perm_string name_;
428 ivl_scope_t scope_;
430 unsigned npins_;
431 ivl_nexus_t*pins_;
433 struct ivl_attribute_s*attr;
434 unsigned nattr;
436 ivl_expr_t delay[3];
441 * UDP definition.
443 struct ivl_udp_s {
444 perm_string name;
445 unsigned nin;
446 int sequ; /* boolen */
447 char init;
448 unsigned nrows;
449 typedef const char*ccharp_t;
450 ccharp_t*table; // zero terminated array of pointers
454 * The ivl_nexus_t is a single-bit link of some number of pins of
455 * devices. the __nexus_ptr structure is a helper that actually does
456 * the pointing.
458 * The type_ member specifies which of the object pointers in the
459 * union are valid.
461 * The drive01 members gives the strength of the drive that the device
462 * is applying to the nexus, with 0 HiZ and 3 supply. If the pin is an
463 * input to the device, then the drives are both HiZ.
465 struct ivl_nexus_ptr_s {
466 unsigned pin_;
467 unsigned type_ : 8;
468 unsigned drive0 : 3;
469 unsigned drive1 : 3;
470 union {
471 ivl_signal_t sig; /* type 0 */
472 ivl_net_logic_t log; /* type 1 */
473 ivl_net_const_t con; /* type 2 */
474 ivl_lpm_t lpm; /* type 3 */
475 } l;
477 # define __NEXUS_PTR_SIG 0
478 # define __NEXUS_PTR_LOG 1
479 # define __NEXUS_PTR_CON 2
480 # define __NEXUS_PTR_LPM 3
483 * NOTE: ONLY allocat ivl_nexus_s objects with the included "new" operator.
485 struct ivl_nexus_s {
486 unsigned nptr_;
487 struct ivl_nexus_ptr_s*ptrs_;
488 const Nexus*nexus_;
489 const char*name_;
490 void*private_data;
492 void* operator new (size_t s);
493 void operator delete(void*obj, size_t s); // Not implemented
497 * This is the implementation of a parameter. Each scope has a list of
498 * these.
500 struct ivl_parameter_s {
501 perm_string basename;
502 ivl_scope_t scope;
503 ivl_expr_t value;
506 * All we know about a process it its type (initial or always) and the
507 * single statement that is it. A process also has a scope, although
508 * that generally only matters for VPI calls.
510 struct ivl_process_s {
511 ivl_process_type_t type_;
512 ivl_scope_t scope_;
513 ivl_statement_t stmt_;
515 struct ivl_attribute_s*attr;
516 unsigned nattr;
518 ivl_process_t next_;
522 * Scopes are kept in a tree. Each scope points to its first child,
523 * and also to any siblings. Thus a parent can scan all its children
524 * by following its child pointer then following sibling pointers from
525 * there.
527 struct ivl_scope_s {
528 ivl_scope_t child_, sibling_, parent;
530 perm_string name_;
531 perm_string tname_;
532 ivl_scope_type_t type_;
534 unsigned nsigs_;
535 ivl_signal_t*sigs_;
537 unsigned nlog_;
538 ivl_net_logic_t*log_;
540 unsigned nevent_;
541 ivl_event_t* event_;
543 unsigned nlpm_;
544 ivl_lpm_t* lpm_;
546 unsigned nparam_;
547 ivl_parameter_t param_;
549 /* Scopes that are tasks/functions have a definition. */
550 ivl_statement_t def;
552 unsigned ports;
553 ivl_signal_t*port;
555 signed int time_precision :8;
556 signed int time_units :8;
558 struct ivl_attribute_s*attr;
559 unsigned nattr;
563 * A signal is a thing like a wire, a reg, or whatever. It has a type,
564 * and if it is a port is also has a direction. Signals are collected
565 * into scopes (which also point back to me) and have pins that
566 * connect to the rest of the netlist.
568 struct ivl_signal_s {
569 ivl_signal_type_t type_;
570 ivl_signal_port_t port_;
571 ivl_variable_type_t data_type;
573 unsigned width_;
574 unsigned signed_ : 1;
575 unsigned isint_ : 1;
576 unsigned local_ : 1;
578 /* For now, support only 0 or 1 array dimensions. */
579 unsigned array_dimensions_ : 1;
581 /* These encode the run-time index for the least significant
582 bit, and the distance to the second bit. */
583 signed lsb_index;
584 signed lsb_dist;
586 perm_string name_;
587 ivl_scope_t scope_;
589 unsigned array_words;
590 int array_base;
591 union {
592 ivl_nexus_t pin;
593 ivl_nexus_t*pins;
596 ivl_delaypath_s*path;
597 unsigned npath;
599 struct ivl_attribute_s*attr;
600 unsigned nattr;
604 * The ivl_statement_t represents any statement. The type of statement
605 * is defined by the ivl_statement_type_t enumeration. Given the type,
606 * certain information about the statement may be available.
608 struct ivl_statement_s {
609 enum ivl_statement_type_e type_;
610 union {
611 struct { /* IVL_ST_ASSIGN IVL_ST_ASSIGN_NB
612 IVL_ST_CASSIGN, IVL_ST_DEASSIGN */
613 unsigned lvals_;
614 struct ivl_lval_s*lval_;
615 ivl_expr_t rval_;
616 ivl_expr_t delay;
617 } assign_;
619 struct { /* IVL_ST_BLOCK, IVL_ST_FORK */
620 struct ivl_statement_s*stmt_;
621 unsigned nstmt_;
622 ivl_scope_t scope;
623 } block_;
625 struct { /* IVL_ST_CASE, IVL_ST_CASEX, IVL_ST_CASEZ */
626 ivl_expr_t cond;
627 unsigned ncase;
628 ivl_expr_t*case_ex;
629 struct ivl_statement_s*case_st;
630 } case_;
632 struct { /* IVL_ST_CONDIT */
633 /* This is the condition expression */
634 ivl_expr_t cond_;
635 /* This is two statements, the true and false. */
636 struct ivl_statement_s*stmt_;
637 } condit_;
639 struct { /* IVL_ST_DELAY */
640 uint64_t delay_;
641 ivl_statement_t stmt_;
642 } delay_;
644 struct { /* IVL_ST_DELAYX */
645 ivl_expr_t expr; /* XXXX */
646 ivl_statement_t stmt_;
647 } delayx_;
649 struct { /* IVL_ST_DISABLE */
650 ivl_scope_t scope;
651 } disable_;
653 struct { /* IVL_ST_FOREVER */
654 ivl_statement_t stmt_;
655 } forever_;
657 struct { /* IVL_ST_STASK */
658 const char*name_;
659 unsigned nparm_;
660 ivl_expr_t*parms_;
661 } stask_;
663 struct { /* IVL_ST_UTASK */
664 ivl_scope_t def;
665 } utask_;
667 struct { /* IVL_ST_TRIGGER IVL_ST_WAIT */
668 unsigned nevent;
669 union {
670 ivl_event_t event;
671 ivl_event_t*events;
673 ivl_statement_t stmt_;
674 } wait_;
676 struct { /* IVL_ST_WHILE IVL_ST_REPEAT */
677 ivl_expr_t cond_;
678 ivl_statement_t stmt_;
679 } while_;
680 } u_;
683 #endif