Fix signed ocmpare of negative numbers.
[iverilog.git] / t-dll.h
blobb1e0a1949d1dbdb5b955554fa89aa03031e776f8
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_nexus_t src;
179 ivl_nexus_t condit;
180 bool posedge;
181 bool negedge;
182 uint64_t delay[12];
185 struct ivl_event_s {
186 perm_string name;
187 ivl_scope_t scope;
188 unsigned nany, nneg, npos;
189 ivl_nexus_t*pins;
193 * The ivl_expr_t is an opaque reference to one of these
194 * structures. This structure holds all the information we need about
195 * an expression node, including its type, the expression width, and
196 * type specific properties.
198 struct ivl_expr_s {
199 ivl_expr_type_t type_;
200 ivl_variable_type_t value_;
202 unsigned width_;
203 unsigned signed_ : 1;
205 union {
206 struct {
207 char op_;
208 ivl_expr_t lef_;
209 ivl_expr_t rig_;
210 } binary_;
212 struct {
213 unsigned rept;
214 unsigned parms;
215 ivl_expr_t*parm;
216 } concat_;
218 struct {
219 char*bits_;
220 ivl_parameter_t parameter;
221 } number_;
223 struct {
224 ivl_event_t event;
225 } event_;
227 struct {
228 ivl_scope_t scope;
229 } scope_;
231 struct {
232 ivl_signal_t sig;
233 ivl_expr_t word;
234 } signal_;
236 struct {
237 const char *name_;
238 ivl_expr_t *parm;
239 unsigned parms;
240 } sfunc_;
242 struct {
243 char*value_;
244 ivl_parameter_t parameter;
245 } string_;
247 struct {
248 ivl_expr_t cond;
249 ivl_expr_t true_e;
250 ivl_expr_t false_e;
251 } ternary_;
253 struct {
254 ivl_memory_t mem_;
255 ivl_expr_t idx_;
256 } memory_;
258 struct {
259 ivl_scope_t def;
260 ivl_expr_t *parm;
261 unsigned parms;
262 } ufunc_;
264 struct {
265 unsigned long value;
266 } ulong_;
268 struct {
269 double value;
270 ivl_parameter_t parameter;
271 } real_;
273 struct {
274 char op_;
275 ivl_expr_t sub_;
276 } unary_;
278 } u_;
282 * LPM devices are handled by this suite of types. The ivl_lpm_s
283 * structure holds the core, including a type code, the object name
284 * and scope. The other properties of the device are held in the type
285 * specific member of the union.
288 struct ivl_lpm_s {
289 ivl_lpm_type_t type;
290 ivl_scope_t scope;
291 perm_string name;
292 // Value returned by ivl_lpm_width;
293 unsigned width;
295 union {
296 struct ivl_lpm_ff_s {
297 ivl_nexus_t clk;
298 ivl_nexus_t we;
299 ivl_nexus_t aclr;
300 ivl_nexus_t aset;
301 ivl_nexus_t sclr;
302 ivl_nexus_t sset;
303 union {
304 ivl_nexus_t*pins;
305 ivl_nexus_t pin;
306 } q;
307 union {
308 ivl_nexus_t*pins;
309 ivl_nexus_t pin;
310 } d;
311 ivl_expr_t aset_value;
312 ivl_expr_t sset_value;
313 } ff;
315 struct ivl_lpm_mux_s {
316 unsigned size;
317 unsigned swid;
318 ivl_nexus_t*d;
319 ivl_nexus_t q, s;
320 } mux;
322 struct ivl_lpm_shift_s {
323 unsigned select;
324 unsigned signed_flag :1;
325 ivl_nexus_t q, d, s;
326 } shift;
328 struct ivl_lpm_arith_s {
329 unsigned signed_flag :1;
330 ivl_nexus_t q, a, b;
331 } arith;
333 struct ivl_lpm_array_s {
334 ivl_signal_t sig;
335 unsigned swid;
336 ivl_nexus_t q, a;
337 } array;
339 struct ivl_concat_s {
340 unsigned inputs;
341 ivl_nexus_t*pins;
342 } concat;
344 struct ivl_part_s {
345 unsigned base;
346 unsigned signed_flag :1;
347 ivl_nexus_t q, a, s;
348 } part;
350 // IVL_LPM_RE_* and IVL_LPM_SIGN_EXT use this.
351 struct ivl_lpm_reduce_s {
352 ivl_nexus_t q, a;
353 } reduce;
355 struct ivl_lpm_repeat_s {
356 unsigned count;
357 ivl_nexus_t q, a;
358 } repeat;
360 struct ivl_lpm_sfunc_s {
361 const char* fun_name;
362 unsigned ports;
363 ivl_nexus_t*pins;
364 } sfunc;
366 struct ivl_lpm_ufunc_s {
367 ivl_scope_t def;
368 unsigned ports;
369 ivl_nexus_t*pins;
370 } ufunc;
371 } u_;
375 * This object represents l-values to assignments. The l-value can be
376 * a register bit or part select, or a memory word select with a part
377 * select.
380 enum ivl_lval_type_t {
381 IVL_LVAL_REG = 0,
382 IVL_LVAL_MUX = 1,
383 /* IVL_LVAL_MEM = 2, / Deprecated in favor of LVAL_ARR? */
384 IVL_LVAL_NET = 3, /* Only force can have NET l-values */
385 IVL_LVAL_ARR = 4
388 struct ivl_lval_s {
389 ivl_expr_t loff;
390 ivl_expr_t idx;
391 unsigned width_;
392 unsigned type_ : 8;
393 union {
394 ivl_signal_t sig;
395 ivl_memory_t mem;
396 } n;
400 * This object represents a literal constant, possibly signed, in a
401 * structural context.
403 struct ivl_net_const_s {
404 ivl_variable_type_t type;
405 unsigned width_;
406 unsigned signed_ : 1;
408 union {
409 double real_value;
410 char bit_[sizeof(char*)];
411 char *bits_;
412 } b;
414 ivl_nexus_t pin_;
418 * Logic gates (just about everything that has a single output) are
419 * represented structurally by instances of this object.
421 struct ivl_net_logic_s {
422 ivl_logic_t type_;
423 unsigned width_;
424 ivl_udp_t udp;
426 perm_string name_;
427 ivl_scope_t scope_;
429 unsigned npins_;
430 ivl_nexus_t*pins_;
432 struct ivl_attribute_s*attr;
433 unsigned nattr;
435 ivl_expr_t delay[3];
440 * UDP definition.
442 struct ivl_udp_s {
443 perm_string name;
444 unsigned nin;
445 int sequ; /* boolen */
446 char init;
447 unsigned nrows;
448 typedef const char*ccharp_t;
449 ccharp_t*table; // zero terminated array of pointers
453 * The ivl_nexus_t is a single-bit link of some number of pins of
454 * devices. the __nexus_ptr structure is a helper that actually does
455 * the pointing.
457 * The type_ member specifies which of the object pointers in the
458 * union are valid.
460 * The drive01 members gives the strength of the drive that the device
461 * is applying to the nexus, with 0 HiZ and 3 supply. If the pin is an
462 * input to the device, then the drives are both HiZ.
464 struct ivl_nexus_ptr_s {
465 unsigned pin_;
466 unsigned type_ : 8;
467 unsigned drive0 : 3;
468 unsigned drive1 : 3;
469 union {
470 ivl_signal_t sig; /* type 0 */
471 ivl_net_logic_t log; /* type 1 */
472 ivl_net_const_t con; /* type 2 */
473 ivl_lpm_t lpm; /* type 3 */
474 } l;
476 # define __NEXUS_PTR_SIG 0
477 # define __NEXUS_PTR_LOG 1
478 # define __NEXUS_PTR_CON 2
479 # define __NEXUS_PTR_LPM 3
482 * NOTE: ONLY allocat ivl_nexus_s objects with the included "new" operator.
484 struct ivl_nexus_s {
485 unsigned nptr_;
486 struct ivl_nexus_ptr_s*ptrs_;
487 const Nexus*nexus_;
488 const char*name_;
489 void*private_data;
491 void* operator new (size_t s);
492 void operator delete(void*obj, size_t s); // Not implemented
496 * This is the implementation of a parameter. Each scope has a list of
497 * these.
499 struct ivl_parameter_s {
500 perm_string basename;
501 ivl_scope_t scope;
502 ivl_expr_t value;
505 * All we know about a process it its type (initial or always) and the
506 * single statement that is it. A process also has a scope, although
507 * that generally only matters for VPI calls.
509 struct ivl_process_s {
510 ivl_process_type_t type_;
511 ivl_scope_t scope_;
512 ivl_statement_t stmt_;
514 struct ivl_attribute_s*attr;
515 unsigned nattr;
517 ivl_process_t next_;
521 * Scopes are kept in a tree. Each scope points to its first child,
522 * and also to any siblings. Thus a parent can scan all its children
523 * by following its child pointer then following sibling pointers from
524 * there.
526 struct ivl_scope_s {
527 ivl_scope_t child_, sibling_, parent;
529 perm_string name_;
530 perm_string tname_;
531 ivl_scope_type_t type_;
533 unsigned nsigs_;
534 ivl_signal_t*sigs_;
536 unsigned nlog_;
537 ivl_net_logic_t*log_;
539 unsigned nevent_;
540 ivl_event_t* event_;
542 unsigned nlpm_;
543 ivl_lpm_t* lpm_;
545 unsigned nparam_;
546 ivl_parameter_t param_;
548 /* Scopes that are tasks/functions have a definition. */
549 ivl_statement_t def;
551 unsigned ports;
552 ivl_signal_t*port;
554 signed int time_precision :8;
555 signed int time_units :8;
557 struct ivl_attribute_s*attr;
558 unsigned nattr;
562 * A signal is a thing like a wire, a reg, or whatever. It has a type,
563 * and if it is a port is also has a direction. Signals are collected
564 * into scopes (which also point back to me) and have pins that
565 * connect to the rest of the netlist.
567 struct ivl_signal_s {
568 ivl_signal_type_t type_;
569 ivl_signal_port_t port_;
570 ivl_variable_type_t data_type;
572 unsigned width_;
573 unsigned signed_ : 1;
574 unsigned isint_ : 1;
575 unsigned local_ : 1;
577 /* For now, support only 0 or 1 array dimensions. */
578 unsigned array_dimensions_ : 1;
580 /* These encode the run-time index for the least significant
581 bit, and the distance to the second bit. */
582 signed lsb_index;
583 signed lsb_dist;
585 perm_string name_;
586 ivl_scope_t scope_;
588 unsigned array_words;
589 int array_base;
590 union {
591 ivl_nexus_t pin;
592 ivl_nexus_t*pins;
595 ivl_delaypath_s*path;
596 unsigned npath;
598 struct ivl_attribute_s*attr;
599 unsigned nattr;
603 * The ivl_statement_t represents any statement. The type of statement
604 * is defined by the ivl_statement_type_t enumeration. Given the type,
605 * certain information about the statement may be available.
607 struct ivl_statement_s {
608 enum ivl_statement_type_e type_;
609 union {
610 struct { /* IVL_ST_ASSIGN IVL_ST_ASSIGN_NB
611 IVL_ST_CASSIGN, IVL_ST_DEASSIGN */
612 unsigned lvals_;
613 struct ivl_lval_s*lval_;
614 ivl_expr_t rval_;
615 ivl_expr_t delay;
616 } assign_;
618 struct { /* IVL_ST_BLOCK, IVL_ST_FORK */
619 struct ivl_statement_s*stmt_;
620 unsigned nstmt_;
621 ivl_scope_t scope;
622 } block_;
624 struct { /* IVL_ST_CASE, IVL_ST_CASEX, IVL_ST_CASEZ */
625 ivl_expr_t cond;
626 unsigned ncase;
627 ivl_expr_t*case_ex;
628 struct ivl_statement_s*case_st;
629 } case_;
631 struct { /* IVL_ST_CONDIT */
632 /* This is the condition expression */
633 ivl_expr_t cond_;
634 /* This is two statements, the true and false. */
635 struct ivl_statement_s*stmt_;
636 } condit_;
638 struct { /* IVL_ST_DELAY */
639 uint64_t delay_;
640 ivl_statement_t stmt_;
641 } delay_;
643 struct { /* IVL_ST_DELAYX */
644 ivl_expr_t expr; /* XXXX */
645 ivl_statement_t stmt_;
646 } delayx_;
648 struct { /* IVL_ST_DISABLE */
649 ivl_scope_t scope;
650 } disable_;
652 struct { /* IVL_ST_FOREVER */
653 ivl_statement_t stmt_;
654 } forever_;
656 struct { /* IVL_ST_STASK */
657 const char*name_;
658 unsigned nparm_;
659 ivl_expr_t*parms_;
660 } stask_;
662 struct { /* IVL_ST_UTASK */
663 ivl_scope_t def;
664 } utask_;
666 struct { /* IVL_ST_TRIGGER IVL_ST_WAIT */
667 unsigned nevent;
668 union {
669 ivl_event_t event;
670 ivl_event_t*events;
672 ivl_statement_t stmt_;
673 } wait_;
675 struct { /* IVL_ST_WHILE IVL_ST_REPEAT */
676 ivl_expr_t cond_;
677 ivl_statement_t stmt_;
678 } while_;
679 } u_;
682 #endif