1 #ifndef IVL_vpi_priv_H
2 #define IVL_vpi_priv_H
3 /*
4  * Copyright (c) 2001-2018 Stephen Williams (steve@icarus.com)
5  * Copyright (c) 2016 CERN Michele Castellana (michele.castellana@cern.ch)
6  *
7  *    This source code is free software; you can redistribute it
8  *    and/or modify it in source code form under the terms of the GNU
9  *    General Public License as published by the Free Software
10  *    Foundation; either version 2 of the License, or (at your option)
11  *    any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License
19  *    along with this program; if not, write to the Free Software
20  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 # include  "sv_vpi_user.h"
24 # include  "vvp_net.h"
25 # include  "config.h"
26 
27 # include  <map>
28 # include  <set>
29 # include  <string>
30 # include  <vector>
31 
32 /*
33  * Added to use some "vvp_fun_modpath_src"
34  * and "vvp_fun_modpath" classes definitions
35  */
36 #include  "delay.h"
37 
38 
39 class class_type;
40 class vvp_darray;
41 class vvp_fun_arrayport;
42 
43 typedef struct __vpiArray* vvp_array_t;
44 
45 /*
46  * This header file contains the internal definitions that the vvp
47  * program uses to implement the public interface in the vpi_user.h
48  * header file elsewhere.
49  */
50 
51 #if defined(__MINGW32__) || defined (__CYGWIN__)
52 extern vpip_routines_s vpi_routines;
53 #endif
54 
55 /*
56  * Routines/definitions used to build the file/line number tracing object.
57  */
58 #define _vpiFileLine    0x1000003
59 #define _vpiDescription 0x1000004
60 
61 extern bool show_file_line;
62 extern bool code_is_instrumented;
63 
64 extern vpiHandle vpip_build_file_line(char*description,
65                                       long file_idx, long lineno);
66 
67 /*
68  * Private VPI properties that are only used in the cleanup code.
69  */
70 #if defined(CHECK_WITH_VALGRIND) && !defined(BR916_STOPGAP_FIX)
71 #define _vpiFromThr 0x1000001
72 #   define _vpiNoThr   0
73 #   define _vpiString  1
74 #   define _vpiVThr    2
75 #   define _vpiWord    3
76 #   define _vpi_at_PV  4
77 #   define _vpi_at_A   5
78 #   define _vpi_at_APV 6
79 #endif
80 
81 
82 /*
83  * The vpi_mode_flag contains the major mode for VPI use. This is used
84  * to generate error messages when vpi functions are called
85  * incorrectly.
86  */
87 enum vpi_mode_t {
88       VPI_MODE_NONE =0,
89 	/* The compiler is calling a register function. */
90       VPI_MODE_REGISTER,
91 	/* The compiler is calling a compiletf function. */
92       VPI_MODE_COMPILETF,
93 	/* The compiler is calling a calltf function. */
94       VPI_MODE_CALLTF,
95 	/* We are in the midst of a RWSync callback. */
96       VPI_MODE_RWSYNC,
97 	/* We are in a ROSync callback. */
98       VPI_MODE_ROSYNC
99 };
100 extern vpi_mode_t vpi_mode_flag;
101 
102 /*
103  * This structure is the very base of a vpiHandle. Every handle
104  * structure is derived from this class so that the library can
105  * internally pass the derived types as pointers to one of these.
106  */
107 class __vpiHandle {
108     public:
__vpiHandle()109       inline __vpiHandle() { }
110 	// The destructor is virtual so that dynamic types will work.
111       virtual ~__vpiHandle();
112 
113       virtual int get_type_code(void) const =0;
114       virtual int vpi_get(int code);
115       virtual char* vpi_get_str(int code);
116 
117       virtual void vpi_get_value(p_vpi_value val);
118       virtual vpiHandle vpi_put_value(p_vpi_value val, int flags);
119       virtual vpiHandle vpi_handle(int code);
120       virtual vpiHandle vpi_iterate(int code);
121       virtual vpiHandle vpi_index(int idx);
122       virtual void vpi_get_delays(p_vpi_delay del);
123       virtual void vpi_put_delays(p_vpi_delay del);
124 
125 	// Objects may have destroyer functions of their own. If so,
126 	// then this virtual method will return a POINTER to that
127 	// function. The pointer is used to "delete" the object, which
128 	// is why the function itself cannot be a method.
129       typedef int (*free_object_fun_t)(vpiHandle);
130       virtual free_object_fun_t free_object_fun(void);
131 };
132 
133 
134 /*
135  * The vpiHandle for an iterator has this structure. The definition of
136  * the methods lives in vpi_iter.c
137  *
138  * The args and nargs members point to the array of vpiHandle objects
139  * that are to be iterated over. The next member is the index of the
140  * next item to be returned by a vpi_scan.
141  *
142  * The free_args_flag member is true if when this iterator object is
143  * released it must also free the args array.
144  */
145 struct __vpiIterator : public __vpiHandle {
146       __vpiIterator();
147       int get_type_code(void) const;
148       free_object_fun_t free_object_fun(void);
149 
150       vpiHandle *args;
151       unsigned  nargs;
152       unsigned  next;
153       bool free_args_flag;
154 };
155 
156 extern vpiHandle vpip_make_iterator(unsigned nargs, vpiHandle*args,
157 				    bool free_args_flag);
158 
159 class __vpiDecConst : public __vpiHandle {
160     public:
161       explicit __vpiDecConst(int val =0);
162       __vpiDecConst(const __vpiDecConst&that);
163       int get_type_code(void) const;
164       int vpi_get(int code);
165       void vpi_get_value(p_vpi_value val);
166 
167     public:
get_value()168       inline int get_value() const { return value; }
set_value(int val)169       inline void set_value(int val) { value = val; }
170 
171     private:
172       int value;
173 };
174 
175 /*
176  * This represents callback handles. There are some private types that
177  * are defined and used in vpi_callback.cc. The __vpiCallback are
178  * always used in association with vvp_vpi_callback objects.
179  */
180 struct __vpiCallback : public __vpiHandle {
181       __vpiCallback();
182       ~__vpiCallback();
183       int get_type_code(void) const;
184 
185 	// Used for listing callbacks.
186       struct __vpiCallback*next;
187 
188 	// user supplied callback data
189       struct t_cb_data cb_data;
190 };
191 
192 class value_callback : public __vpiCallback {
193     public:
194       explicit value_callback(p_cb_data data);
195 	// Return true if the callback really is ready to be called
196       virtual bool test_value_callback_ready(void);
197 
198     public:
199 	// user supplied callback data
200       struct t_vpi_time cb_time;
201       struct t_vpi_value cb_value;
202 };
203 
204 extern void callback_execute(struct __vpiCallback*cur);
205 
206 struct __vpiSystemTime : public __vpiHandle {
207       __vpiSystemTime();
208       int get_type_code(void) const;
209       int vpi_get(int code);
210       char*vpi_get_str(int code);
211       void vpi_get_value(p_vpi_value val);
212       vpiHandle vpi_handle(int code);
213 
214       __vpiScope*scope;
215 };
216 
217 struct __vpiScopedTime : public __vpiSystemTime {
218       __vpiScopedTime();
219       char*vpi_get_str(int code);
220       void vpi_get_value(p_vpi_value val);
221 };
222 struct __vpiScopedSTime : public __vpiSystemTime {
223       __vpiScopedSTime();
224       int vpi_get(int code);
225       char*vpi_get_str(int code);
226       void vpi_get_value(p_vpi_value val);
227 };
228 struct __vpiScopedRealtime : public __vpiSystemTime {
229       __vpiScopedRealtime();
230       int vpi_get(int code);
231       char*vpi_get_str(int code);
232       void vpi_get_value(p_vpi_value val);
233 };
234 
235 /*
236  * Scopes are created by .scope statements in the source. These
237  * objects hold the items and properties that are knowingly bound to a
238  * scope.
239  */
240 class __vpiScope : public __vpiHandle {
241 
242     public:
243       int vpi_get(int code);
244       char* vpi_get_str(int code);
245       vpiHandle vpi_handle(int code);
246       vpiHandle vpi_iterate(int code);
247 
248     public:
249 	// Return the BASE name of the scope. This does not include
250 	// any of the parent hierarchy.
scope_name()251       inline const char*scope_name() const { return name_; }
252 
scope_def_name()253       inline const char*scope_def_name() const { return tname_; }
254 	// TRUE if this is an automatic func/task/block
is_automatic()255       inline bool is_automatic() const { return is_automatic_; }
256 
257     public:
258       __vpiScope *scope;
259       unsigned file_idx;
260       unsigned lineno;
261       unsigned def_file_idx;
262       unsigned def_lineno;
263       bool is_cell;
264 	/* The scope has a system time of its own. */
265       __vpiScopedTime scoped_time;
266       struct __vpiScopedSTime scoped_stime;
267       struct __vpiScopedRealtime scoped_realtime;
268 	/* Keep an array of internal scope items. */
269       std::vector<class __vpiHandle*> intern;
270 	/* Set of types */
271       std::map<std::string,class_type*> classes;
272         /* Keep an array of items to be automatically allocated */
273       struct automatic_hooks_s**item;
274       unsigned nitem;
275         /* Keep a list of live contexts. */
276       vvp_context_t live_contexts;
277         /* Keep a list of freed contexts. */
278       vvp_context_t free_contexts;
279 	/* Keep a list of threads in the scope. */
280       std::set<vthread_t> threads;
281       signed int time_units :8;
282       signed int time_precision :8;
283 
284     protected:
285       __vpiScope(const char*nam, const char*tnam, bool is_auto_flag =false);
286 
287     private:
288 	/* The scope has a name. */
289       const char*name_;
290       const char*tname_;
291 	/* the scope may be "automatic" */
292       bool is_automatic_;
293 };
294 
295 class vpiScopeFunction  : public __vpiScope {
296     public:
vpiScopeFunction(const char * nam,const char * tnam,bool auto_flag,int func_type,unsigned func_wid,vvp_bit4_t func_init_val)297       inline vpiScopeFunction(const char*nam, const char*tnam,
298 			      bool auto_flag, int func_type, unsigned func_wid, vvp_bit4_t func_init_val)
299       : __vpiScope(nam,tnam, auto_flag), func_type_(func_type), func_wid_(func_wid), func_init_val_(func_init_val)
300       { }
301 
get_type_code(void)302       int get_type_code(void) const { return vpiFunction; }
vpi_get(int code)303       int vpi_get(int code)
304       {
305 	    switch (code) {
306 		case vpiFuncType:
307 		  return func_type_;
308 		default:
309 		  return __vpiScope::vpi_get(code);
310 	    }
311       }
312 
313     public:
get_func_width(void)314       inline unsigned get_func_width(void) const { return func_wid_; }
get_func_init_val(void)315       inline vvp_bit4_t get_func_init_val(void) const { return func_init_val_; }
316 
317     private:
318       int func_type_;
319       unsigned func_wid_;
320       vvp_bit4_t func_init_val_;
321 };
322 
323 extern __vpiScope* vpip_peek_current_scope(void);
324 extern void vpip_attach_to_scope(__vpiScope*scope, vpiHandle obj);
325 extern void vpip_attach_to_current_scope(vpiHandle obj);
326 extern __vpiScope* vpip_peek_context_scope(void);
327 extern unsigned vpip_add_item_to_context(automatic_hooks_s*item,
328                                          __vpiScope*scope);
329 extern vpiHandle vpip_make_root_iterator(void);
330 extern void vpip_make_root_iterator(class __vpiHandle**&table,
331 				    unsigned&ntable);
332 
333 /*
334  * Signals include the variable types (reg, integer, time) and are
335  * distinguished by the vpiType code. They also have a parent scope,
336  * a declared name and declaration indices.
337  */
338 struct __vpiSignal : public __vpiHandle {
339       int vpi_get(int code);
340       char* vpi_get_str(int code);
341       void vpi_get_value(p_vpi_value val);
342       vpiHandle vpi_put_value(p_vpi_value val, int flags);
343       vpiHandle vpi_handle(int code);
344       vpiHandle vpi_iterate(int code);
345       vpiHandle vpi_index(int idx);
346 
347     public:
348       unsigned width() const;
349       vpiHandle get_index(int index);
350       void get_bit_value(struct __vpiBit*bit, p_vpi_value vp);
351       vpiHandle put_bit_value(struct __vpiBit*bit, p_vpi_value vp, int flags);
352       void make_bits();
353 
354       struct __vpiBit*bits;
355 
356     public:
357       union { // The scope or parent array that contains me.
358 	    vpiHandle parent;
359 	    __vpiScope* scope;
360       } within;
361       union { // The name of this reg/net, or the index for array words.
362             const char*name;
363             vpiHandle index;
364       } id;
365 	/* The indices that define the width and access offset. */
366       __vpiDecConst msb, lsb;
367 	/* Flags */
368       unsigned signed_flag  : 1;
369       unsigned is_netarray  : 1; // This is word of a net array
370 	/* The represented value is here. */
371       vvp_net_t*node;
372 
373     public:
374       static void*operator new(std::size_t size);
375       static void operator delete(void*); // not implemented
376     protected:
__vpiSignal__vpiSignal377       inline __vpiSignal() : bits(NULL) { }
378     private: // Not implemented
379       static void*operator new[] (std::size_t size);
380       static void operator delete[](void*);
381 };
382 extern unsigned vpip_size(__vpiSignal *sig);
383 extern __vpiScope* vpip_scope(__vpiSignal*sig);
384 
385 extern vpiHandle vpip_make_int2(const char*name, int msb, int lsb,
386 			       bool signed_flag, vvp_net_t*vec);
387 extern vpiHandle vpip_make_int4(const char*name, int msb, int lsb,
388 			       vvp_net_t*vec);
389 extern vpiHandle vpip_make_var4(const char*name, int msb, int lsb,
390 			       bool signed_flag, vvp_net_t*net);
391 extern vpiHandle vpip_make_net4(__vpiScope*scope,
392 				const char*name, int msb, int lsb,
393 				bool signed_flag, vvp_net_t*node);
394 
395 /*
396  * This is used to represent a bit in a net/reg.
397  */
398 struct __vpiBit {
399       struct as_bit_t : public __vpiHandle {
400 	    int get_type_code(void) const;
401 	    int vpi_get(int code);
402 	    char* vpi_get_str(int code);
403 	    void vpi_get_value(p_vpi_value val);
404 	    vpiHandle vpi_put_value(p_vpi_value val, int flags);
405 	    vpiHandle vpi_handle(int code);
406       } as_bit;
407 
408       vpiHandle index;
409 
410       union {
411 	    struct __vpiSignal*parent;
412 	    struct __vpiBit*bit0;
413       };
414 
get_norm_index__vpiBit415       inline unsigned get_norm_index() const { return this - bit0; }
get_parent__vpiBit416       inline struct __vpiSignal*get_parent() const {return (bit0 - 1)->parent; }
417       int get_index(void) const;
418 };
419 
420 /*
421  * This is used by system calls to represent a bit/part select of
422  * a simple variable or constant array word.
423  */
424 struct __vpiPV : public __vpiHandle {
425       __vpiPV();
426       int get_type_code(void) const;
427       int vpi_get(int code);
428       char* vpi_get_str(int code);
429       void vpi_get_value(p_vpi_value val);
430       vpiHandle vpi_put_value(p_vpi_value val, int flags);
431       vpiHandle vpi_handle(int code);
432 
433       vpiHandle parent;
434       vvp_net_t*net;
435       vpiHandle sbase;
436       int tbase;
437       unsigned width;
438 };
439 extern vpiHandle vpip_make_PV(char*name, int base, int width);
440 extern vpiHandle vpip_make_PV(char*name, char*symbol, int width);
441 extern vpiHandle vpip_make_PV(char*name, vpiHandle handle, int width);
442 
443 struct __vpiModPathTerm : public __vpiHandle {
444       __vpiModPathTerm();
445       int get_type_code(void) const;
446       int vpi_get(int code);
447       vpiHandle vpi_handle(int code);
448 
449       vpiHandle expr;
450 	/* The value returned by vpi_get(vpiEdge, ...); */
451       int edge;
452 };
453 
454 struct __vpiModPathSrc : public __vpiHandle {
455       __vpiModPathSrc();
456       int get_type_code(void) const;
457       int vpi_get(int code);
458       void vpi_get_value(p_vpi_value val);
459       vpiHandle vpi_put_value(p_vpi_value val, int flags);
460       vpiHandle vpi_handle(int code);
461       vpiHandle vpi_iterate(int code);
462       vpiHandle vpi_index(int idx);
463       void vpi_get_delays(p_vpi_delay del);
464       void vpi_put_delays(p_vpi_delay del);
465       free_object_fun_t free_object_fun(void);
466 
467       struct __vpiModPath *dest;
468       int   type;
469 
470 	/* This is the input expression for this modpath. */
471       struct __vpiModPathTerm path_term_in;
472 
473 	/* This is the input net for the modpath. signals on this net
474 	   are used to determine the modpath. They are *not* propagated
475 	   anywhere. */
476       vvp_net_t *net;
477 } ;
478 
479 
480 /*
481  *
482  * The vpiModPath vpiHandle will define
483  * a vpiModPath of record .modpath as defined
484  * in the IEEE 1364
485  *
486  */
487 
488 struct __vpiModPath {
489       __vpiScope   *scope ;
490 
491       class vvp_fun_modpath*modpath;
492 
493       struct __vpiModPathTerm path_term_out;
494       vvp_net_t *input_net  ;
495 };
496 
497 
498 /*
499  * The Function is used to create the vpiHandle
500  * for vpiModPath && vpiModPathIn objects
501  */
502 
503 extern struct __vpiModPathSrc* vpip_make_modpath_src(struct __vpiModPath*path,
504                                                      vvp_net_t *net) ;
505 
506 extern struct __vpiModPath* vpip_make_modpath(vvp_net_t *net) ;
507 
508 
509 /*
510  * These methods support the vpi creation of events. The name string
511  * passed in will be saved, so the caller must allocate it (or not
512  * free it) after it is handed to this function.
513  */
514 class __vpiNamedEvent : public __vpiHandle {
515 
516     public:
517       __vpiNamedEvent(__vpiScope*scope, const char*name);
518       ~__vpiNamedEvent();
519       int get_type_code(void) const;
get_scope(void)520       __vpiScope*get_scope(void) const { return scope_; }
521       int vpi_get(int code);
522       char* vpi_get_str(int code);
523       vpiHandle vpi_put_value(p_vpi_value val, int flags);
524       vpiHandle vpi_handle(int code);
525 
add_vpi_callback(__vpiCallback * cb)526       inline void add_vpi_callback(__vpiCallback*cb)
527       { cb->next = callbacks_;
528 	callbacks_ = cb;
529       }
530 
531       void run_vpi_callbacks(void);
532 
533 	/* The functor, used for %set operations. */
534       vvp_net_t*funct;
535 
536     private:
537 	/* base name of the event object */
538       const char*name_;
539 	/* Parent scope of this object. */
540       __vpiScope*scope_;
541 	/* List of callbacks interested in this event. */
542       __vpiCallback*callbacks_;
543 };
544 
545 extern vpiHandle vpip_make_named_event(const char*name, vvp_net_t*f);
546 
547 /*
548  * Memory is an array of bits that is accessible in N-bit chunks, with
549  * N being the width of a word. The memory word handle just points
550  * back to the memory and uses an index to identify its position in
551  * the memory.
552  */
553 
554 extern bool is_net_array(vpiHandle obj);
555 
556 /*
557  * These are the various variable types.
558  */
559 struct __vpiRealVar : public __vpiHandle {
560       __vpiRealVar();
561       int get_type_code(void) const;
562       int vpi_get(int code);
563       char* vpi_get_str(int code);
564       void vpi_get_value(p_vpi_value val);
565       vpiHandle vpi_put_value(p_vpi_value val, int flags);
566       vpiHandle vpi_handle(int code);
567       vpiHandle vpi_iterate(int code);
568 
569       union { // The scope or parent array that contains me.
570 	    vpiHandle parent;
571 	    __vpiScope* scope;
572       } within;
573 	/* The name of this variable, or the index for array words. */
574       union {
575             const char*name;
576             vpiHandle index;
577       } id;
578       unsigned is_netarray  : 1; // This is word of a net array
579       unsigned is_wire      : 1; // This is a wire, not a variable
580       vvp_net_t*net;
581 };
582 
583 extern __vpiScope* vpip_scope(__vpiRealVar*sig);
584 extern vpiHandle vpip_make_real_var(const char*name, vvp_net_t*net);
585 extern vpiHandle vpip_make_real_net(__vpiScope*scope,
586 				    const char*name, vvp_net_t*net);
587 
588 class __vpiBaseVar : public __vpiHandle {
589 
590     public:
591       __vpiBaseVar(__vpiScope*scope, const char*name, vvp_net_t*net);
592 #ifdef CHECK_WITH_VALGRIND
593       ~__vpiBaseVar();
594 #endif
595 
get_net()596       inline vvp_net_t* get_net() const { return net_; }
597 
598     protected:
599       __vpiScope* scope_;
600       const char*name_;
601 
602     private:
603       vvp_net_t*net_;
604 };
605 
606 class __vpiStringVar : public __vpiBaseVar {
607 
608     public:
609       __vpiStringVar(__vpiScope*scope, const char*name, vvp_net_t*net);
610 
611       int get_type_code(void) const;
612       int vpi_get(int code);
613       void vpi_get_value(p_vpi_value val);
614       vpiHandle vpi_put_value(p_vpi_value val, int flags);
615 };
616 
617 extern vpiHandle vpip_make_string_var(const char*name, vvp_net_t*net);
618 
619 struct __vpiArrayBase {
__vpiArrayBase__vpiArrayBase620       __vpiArrayBase() : vals_words(NULL) {}
~__vpiArrayBase__vpiArrayBase621       virtual ~__vpiArrayBase() {}
622 
623       virtual unsigned get_size(void) const = 0;
624       virtual vpiHandle get_left_range() = 0;
625       virtual vpiHandle get_right_range() = 0;
626       virtual __vpiScope*get_scope() const = 0;
627 
628       virtual int get_word_size() const = 0;
629       virtual char*get_word_str(struct __vpiArrayWord*word, int code) = 0;
630       virtual void get_word_value(struct __vpiArrayWord*word, p_vpi_value vp) = 0;
631       virtual void put_word_value(struct __vpiArrayWord*word, p_vpi_value vp,
632                                     int flags) = 0;
633       virtual vpiHandle get_iter_index(struct __vpiArrayIterator*iter, int idx) = 0;
634 
635     // vpi_iterate is already defined by vpiHandle, so to avoid problems with
636     // classes inheriting from vpiHandle and vpiArrayBase just share the common
637     // code in the following function
638       vpiHandle vpi_array_base_iterate(int code);
639 
640       virtual void make_vals_words();
641 
642       struct __vpiArrayWord*vals_words;
643 };
644 
645 /*
646 * The vpiArray object holds an array of vpi objects that themselves
647 * represent the words of the array. The vpi_array_t is a pointer to
648 * a struct __vpiArray.
649 *
650 * The details of the implementation depends on what this is an array
651 * of. The easiest case is if this is an array of nets.
652 *
653 * - Array of Nets:
654 * If this represents an array of nets, then the nets member points to
655 * an array of vpiHandle objects. Each vpiHandle is a word. This is
656 * done because typically each word of a net array is simultaneously
657 * driven and accessed by other means, so there is no advantage to
658 * compacting the array in any other way.
659 *
660 * - Array of vector4 words.
661 * In this case, the nets pointer is nil, and the vals4 member points
662 * to a vvl_vector4array_t object that is a compact representation of
663 * an array of vvp_vector4_t vectors.
664 *
665 * - Array of real variables
666 * The vals member points to a dynamic array objects that has an
667 * array of double variables. This is very much like the way the
668 * vector4 array works.
669 */
670 struct __vpiArray : public __vpiArrayBase, public __vpiHandle {
get_type_code__vpiArray671       int get_type_code(void) const { return vpiMemory; }
get_size__vpiArray672       unsigned get_size() const { return array_count; }
get_left_range__vpiArray673       vpiHandle get_left_range() { assert(nets == 0); return &msb; }
get_right_range__vpiArray674       vpiHandle get_right_range() { assert(nets == 0); return &lsb; }
get_scope__vpiArray675       __vpiScope*get_scope() const { return scope; }
676 
677       int get_word_size() const;
678       char*get_word_str(struct __vpiArrayWord*word, int code);
679       void get_word_value(struct __vpiArrayWord*word, p_vpi_value vp);
680       void put_word_value(struct __vpiArrayWord*word, p_vpi_value vp, int flags);
681 
682       vpiHandle get_iter_index(struct __vpiArrayIterator*iter, int idx);
683 
684       int vpi_get(int code);
685       char* vpi_get_str(int code);
686       vpiHandle vpi_handle(int code);
vpi_iterate__vpiArray687       inline vpiHandle vpi_iterate(int code) { return vpi_array_base_iterate(code); }
688       vpiHandle vpi_index(int idx);
689 
690       void set_word(unsigned idx, unsigned off, const vvp_vector4_t&val);
691       void set_word(unsigned idx, double val);
692       void set_word(unsigned idx, const std::string&val);
693       void set_word(unsigned idx, const vvp_object_t&val);
694 
695       vvp_vector4_t get_word(unsigned address);
696       double get_word_r(unsigned address);
697       void get_word_obj(unsigned address, vvp_object_t&val);
698       std::string get_word_str(unsigned address);
699 
700       void alias_word(unsigned long addr, vpiHandle word, int msb, int lsb);
701       void attach_word(unsigned addr, vpiHandle word);
702       void word_change(unsigned long addr);
703 
704       const char*name; /* Permanently allocated string */
705       __vpiDecConst first_addr;
706       __vpiDecConst last_addr;
707       __vpiDecConst msb;
708       __vpiDecConst lsb;
709       unsigned vals_width;
710 	// If this is a net array, nets lists the handles.
711       vpiHandle*nets;
712 	// If this is a var array, then these are used instead of nets.
713       vvp_vector4array_t*vals4;
714       vvp_darray        *vals;
715 
716       vvp_fun_arrayport*ports_;
717       struct __vpiCallback *vpi_callbacks;
718       bool signed_flag;
719       bool swap_addr;
720 
721 private:
722       unsigned array_count;
723       __vpiScope*scope;
724 
725 friend vpiHandle vpip_make_array(char*label, const char*name,
726                                         int first_addr, int last_addr,
727                                         bool signed_flag);
728 friend void compile_array_alias(char*label, char*name, char*src);
729 };
730 
731 class __vpiDarrayVar : public __vpiBaseVar, public __vpiArrayBase {
732     public:
733       __vpiDarrayVar(__vpiScope*scope, const char*name, vvp_net_t*net);
734 
get_type_code()735       int get_type_code() const { return vpiArrayVar; }
736       unsigned get_size() const;
737       vpiHandle get_left_range();
738       vpiHandle get_right_range();
get_scope()739       __vpiScope*get_scope() const { return scope_; }
740 
741       int get_word_size() const;
742       char*get_word_str(struct __vpiArrayWord*word, int code);
743       void get_word_value(struct __vpiArrayWord*word, p_vpi_value vp);
744       void put_word_value(struct __vpiArrayWord*word, p_vpi_value vp, int flags);
745 
746       vpiHandle get_iter_index(struct __vpiArrayIterator*iter, int idx);
vpi_iterate(int code)747       inline vpiHandle vpi_iterate(int code) { return vpi_array_base_iterate(code); }
748 
749       int vpi_get(int code);
750       char* vpi_get_str(int code);
751       vpiHandle vpi_handle(int code);
752       vpiHandle vpi_index(int index);
753 
754       void vpi_get_value(p_vpi_value val);
755 
756     protected:
757       vvp_darray*get_vvp_darray() const;
758       __vpiDecConst left_range_, right_range_;
759 };
760 
761 extern vpiHandle vpip_make_darray_var(const char*name, vvp_net_t*net);
762 
763 class __vpiQueueVar : public __vpiBaseVar {
764 
765     public:
766       __vpiQueueVar(__vpiScope*scope, const char*name, vvp_net_t*net);
767 
768       int get_type_code(void) const;
769       int vpi_get(int code);
770       void vpi_get_value(p_vpi_value val);
771 };
772 
773 extern vpiHandle vpip_make_queue_var(const char*name, vvp_net_t*net);
774 
775 class __vpiCobjectVar : public __vpiBaseVar {
776 
777     public:
778       __vpiCobjectVar(__vpiScope*scope, const char*name, vvp_net_t*net);
779 
780       int get_type_code(void) const;
781       int vpi_get(int code);
782       void vpi_get_value(p_vpi_value val);
783 };
784 
785 extern vpiHandle vpip_make_cobject_var(const char*name, vvp_net_t*net);
786 
787 /*
788  * When a loaded VPI module announces a system task/function, one
789  * __vpiUserSystf object is created to hold the definition of that
790  * task/function. The distinction between task and function is stored
791  * in the vpi_systf_data structure data that was supplied by the
792  * external module.
793  *
794  * When the compiler encounters a %vpi_call statement, it creates a
795  * __vpiSysTaskCall to represent that particular call. The call refers
796  * to the definition handle so that when the %vpi_call instruction is
797  * encountered at run-time, the definition can be located and used.
798  *
799  * The vpiSysTaskCall handles both functions and tasks, as the two are
800  * extremely similar. The different VPI type is reflected in a
801  * different vpi_type pointer in the base structure. The only
802  * additional part is the vbit/vwid that is used by the put of the
803  * system function call to place the values in the vthread bit space.
804  */
805 struct __vpiUserSystf : public __vpiHandle {
806       __vpiUserSystf();
807       int get_type_code(void) const;
808 
809       s_vpi_systf_data info;
810       bool is_user_defn;
811 };
812 
813 extern vpiHandle vpip_make_systf_iterator(void);
814 
815 extern struct __vpiUserSystf* vpip_find_systf(const char*name);
816 
817 
818 struct __vpiSysTaskCall : public __vpiHandle {
819 
820       __vpiScope* scope;
821       struct __vpiUserSystf*defn;
822       unsigned nargs;
823       vpiHandle*args;
824 	/* Stack consumed by this call */
825       unsigned vec4_stack;
826       unsigned real_stack;
827       unsigned string_stack;
828 	/* Support for vpi_get_userdata. */
829       void*userdata;
830 	// This is set if this is a structural call to a function
831       class    vvp_net_t*fnet;
832       unsigned file_idx;
833       unsigned lineno;
834       bool put_value;
835     protected:
__vpiSysTaskCall__vpiSysTaskCall836       inline __vpiSysTaskCall()
837       {
838 	    vec4_stack = 0;
839 	    real_stack = 0;
840 	    string_stack = 0;
841       }
842 };
843 
844 extern struct __vpiSysTaskCall*vpip_cur_task;
845 
846 /*
847  * The persistent flag to vpip_make_string_const causes the created
848  * handle to be persistent. This is necessary for cases where the
849  * string handle may be reused, which is the normal case.
850  *
851  * When constructing with a string, the class takes possession of the
852  * text value string, and will delete it in the constructor.
853  */
854 
855 vpiHandle vpip_make_string_const(char*text, bool persistent =true);
856 vpiHandle vpip_make_string_param(char*name, char*value, bool local_flag,
857                                  long file_idx, long lineno);
858 
859 struct __vpiBinaryConst : public __vpiHandle {
860       __vpiBinaryConst();
861       int get_type_code(void) const;
862       int vpi_get(int code);
863       void vpi_get_value(p_vpi_value val);
864 
865       vvp_vector4_t bits;
866 	/* TRUE if this constant is signed. */
867       int signed_flag :1;
868 	/* TRUE if this constant has an explicit size (i.e. 19'h0 vs. 'h0) */
869       int sized_flag   :1;
870 };
871 
872 vpiHandle vpip_make_binary_const(unsigned wid, const char*bits);
873 vpiHandle vpip_make_binary_param(char*name, const vvp_vector4_t&bits,
874 				 bool signed_flag, bool local_flag,
875 				 long file_idx, long lineno);
876 
877 class __vpiRealConst : public __vpiHandle {
878     public:
879       explicit __vpiRealConst(double);
880       int get_type_code(void) const;
881       int vpi_get(int code);
882       void vpi_get_value(p_vpi_value val);
883 
884       double value;
885 };
886 
887 vpiHandle vpip_make_real_const(double value);
888 vpiHandle vpip_make_real_param(char*name, double value, bool local_flag,
889                                long file_idx, long lineno);
890 
891 /*
892  *  This one looks like a constant, but really is a vector in the current
893  *  thread.
894  */
895 
896 vpiHandle vpip_make_vthr_word(unsigned base, const char*type);
897 vpiHandle vpip_make_vthr_str_stack(unsigned depth);
898 vpiHandle vpip_make_vthr_vec4_stack(unsigned depth, bool signed_flag, unsigned wid);
899 
900 vpiHandle vpip_make_vthr_A(char*label, unsigned index);
901 vpiHandle vpip_make_vthr_A(char*label, char*symbol);
902 vpiHandle vpip_make_vthr_A(char*label, vpiHandle handle);
903 vpiHandle vpip_make_vthr_APV(char*label, unsigned index, unsigned bit, unsigned wid);
904 
905 /*
906  * This function is called before any compilation to load VPI
907  * modules. This gives the modules a chance to announce their
908  * contained functions before compilation commences. It is called only
909  * once per module.
910  */
911 extern void vpip_load_module(const char*name);
912 
913 extern void vpip_clear_module_paths();
914 extern void vpip_add_module_path(const char *path);
915 extern void vpip_add_env_and_default_module_paths();
916 
917 /*
918  * The vpip_build_vpi_call function creates a __vpiSysTaskCall object
919  * and returns the handle. The compiler uses this function when it
920  * encounters a %vpi_call or %vpi_func statement.
921  *
922  * The %vpi_call instruction has as its only parameter the handle that
923  * is returned by the vpip_build_vpi_call. This includes all the
924  * information needed by vpip_execute_vpi_call to actually execute the
925  * call. However, the vpiSysTaskCall that is the returned handle,
926  * holds a parameter argument list that is passed in here.
927  *
928  * The val_type and return_width fields are used if this turns out to
929  * be a system function. In that case, the val_type encodes the return
930  * type (-vpiRealVal, -vpiVectorVal) and if a vector the return_width
931  * has the vector width.
932  *
933  * Note that the argv array is saved in the handle, and should should
934  * not be released by the caller.
935  */
936 extern vpiHandle vpip_build_vpi_call(const char*name,
937 				     int val_type, unsigned return_width,
938 				     class vvp_net_t*fnet,
939 				     bool func_as_task_err,
940 				     bool func_as_task_warn,
941 				     unsigned argc,
942 				     vpiHandle*argv,
943 				     unsigned vec4_stack,
944 				     unsigned real_stack,
945 				     unsigned string_stack,
946 				     long file_idx,
947 				     long lineno);
948 
949 extern vthread_t vpip_current_vthread;
950 
951 extern void vpip_execute_vpi_call(vthread_t thr, vpiHandle obj);
952 
953 
954 /*
955  * These are functions used by the compiler to prepare for compilation
956  * and to finish compilation in preparation for execution.
957  */
958 
959 vpiHandle vpip_sim_time(__vpiScope*scope, bool is_stime);
960 vpiHandle vpip_sim_realtime(__vpiScope*scope);
961 
962 extern int vpip_get_time_precision(void);
963 extern void vpip_set_time_precision(int pres);
964 
965 extern int vpip_time_units_from_handle(vpiHandle obj);
966 extern int vpip_time_precision_from_handle(vpiHandle obj);
967 
968 extern void vpip_time_to_timestruct(struct t_vpi_time*ts, vvp_time64_t ti);
969 extern vvp_time64_t vpip_timestruct_to_time(const struct t_vpi_time*ts);
970 
971 extern double vpip_time_to_scaled_real(vvp_time64_t ti, __vpiScope*sc);
972 extern vvp_time64_t vpip_scaled_real_to_time64(double val, __vpiScope*sc);
973 
974 /*
975  * These functions are used mostly as compile time to strings into
976  * permallocated memory. The vpip_string function is the most general,
977  * it allocates a fresh string no matter what. The vpip_name_string
978  * allocates a string and keeps a pointer in the hash, and tries to
979  * reuse it if it can. This us useful for handle names, which may be
980  * reused in different scopes.
981  */
982 extern const char* vpip_string(const char*str);
983 extern const char* vpip_name_string(const char*str);
984 
985 
986 /*
987  * This function is used to make decimal string versions of various
988  * vectors. The input format is a vvp_vector4_t, and the result is
989  * written into buf, without overflowing nbuf.
990  */
991 extern unsigned vpip_vec4_to_dec_str(const vvp_vector4_t&vec4,
992 				     char *buf, unsigned int nbuf,
993 				     int signed_flag);
994 
995 
996 extern void vpip_vec4_to_hex_str(const vvp_vector4_t&bits, char*buf,
997 				 unsigned nbuf);
998 
999 extern void vpip_vec4_to_oct_str(const vvp_vector4_t&bits, char*buf,
1000 				 unsigned nbuf);
1001 
1002 extern void vpip_bin_str_to_vec4(vvp_vector4_t&val, const char*buf);
1003 extern void vpip_oct_str_to_vec4(vvp_vector4_t&val, const char*str);
1004 extern void vpip_dec_str_to_vec4(vvp_vector4_t&val, const char*str);
1005 extern void vpip_hex_str_to_vec4(vvp_vector4_t&val, const char*str);
1006 
1007 extern vvp_vector4_t vec4_from_vpi_value(s_vpi_value*vp, unsigned wid);
1008 extern double real_from_vpi_value(s_vpi_value*vp);
1009 
1010 extern void vpip_vec4_get_value(const vvp_vector4_t&word_val, unsigned width,
1011 				bool signed_flag, s_vpi_value*vp);
1012 extern void vpip_vec2_get_value(const vvp_vector2_t&word_val, unsigned width,
1013 				bool signed_flag, s_vpi_value*vp);
1014 extern void vpip_real_get_value(double real, s_vpi_value*vp);
1015 extern void vpip_string_get_value(const std::string&val, s_vpi_value*vp);
1016 
1017 /*
1018  * Function defined in vpi_signal.cc to manage vpi_get_* persistent
1019  * storage.
1020  */
1021 enum vpi_rbuf_t {
1022       RBUF_VAL =0,
1023 	/* Storage for *_get_value() */
1024       RBUF_STR,
1025 	/* Storage for *_get_str() */
1026       RBUF_DEL
1027 	/* Delete the storage for both buffers. */
1028 };
1029 extern void *need_result_buf(unsigned cnt, vpi_rbuf_t type);
1030 /* following two routines use need_result_buf(, RBUF_STR) */
1031 extern char *simple_set_rbuf_str(const char *s1);
1032 extern char *generic_get_str(int code, vpiHandle ref, const char *name, const char *index);
1033 
1034 /* A routine to find the enclosing module. */
1035 extern vpiHandle vpip_module(__vpiScope*scope);
1036 
1037 extern int vpip_delay_selection;
1038 
1039 #endif /* IVL_vpi_priv_H */
1040