Add file and line information for parameters, etc.
[sverilog.git] / vvp / vpi_priv.h
blobd515a0b62e853d2c0d414146954339e86dcace8e
1 #ifndef __vpi_priv_H
2 #define __vpi_priv_H
3 /*
4 * Copyright (c) 2001-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 "vpi_user.h"
23 # include "pointers.h"
24 # include "vvp_net.h"
25 # include "memory.h"
29 * Added to use some "vvp_fun_modpath_src"
30 * and "vvp_fun_modpath" classes definitions
32 #include "delay.h"
36 * This header file contains the internal definitions that the vvp
37 * program uses to implement the public interface in the vpi_user.h
38 * header file elsewhere.
43 * The vpi_mode_flag contains the major mode for VPI use. This is used
44 * to generate error messages when vpi functions are called
45 * incorrectly.
47 enum vpi_mode_t {
48 VPI_MODE_NONE =0,
49 /* The compiler is calling a register function. */
50 VPI_MODE_REGISTER,
51 /* The compiler is calling a compiletf function. */
52 VPI_MODE_COMPILETF,
53 /* The compiler is calling a calltf function. */
54 VPI_MODE_CALLTF,
55 /* We are in the midst of a RWSync callback. */
56 VPI_MODE_RWSYNC,
57 /* We are in a ROSync callback. */
58 VPI_MODE_ROSYNC
60 extern vpi_mode_t vpi_mode_flag;
63 * This structure is the very base of a vpiHandle. Every handle
64 * structure starts with this structure, so that the library can
65 * internally pass the derived types as pointers to one of these.
67 struct __vpiHandle {
68 const struct __vpirt *vpi_type;
72 * Objects with this structure are used to represent a type of
73 * vpiHandle. A specific object becomes of this type by holding a
74 * pointer to an instance of this structure.
76 struct __vpirt {
77 int type_code;
79 /* These methods extract information from the handle. */
80 int (*vpi_get_)(int, vpiHandle);
81 char* (*vpi_get_str_)(int, vpiHandle);
82 void (*vpi_get_value_)(vpiHandle, p_vpi_value);
83 vpiHandle (*vpi_put_value_)(vpiHandle, p_vpi_value, int flags);
85 /* These methods follow references. */
86 vpiHandle (*handle_)(int, vpiHandle);
87 vpiHandle (*iterate_)(int, vpiHandle);
88 vpiHandle (*index_)(vpiHandle, int);
90 /* This implements the vpi_free_object method. */
91 int (*vpi_free_object_)(vpiHandle);
94 These two methods are used to read/write delay
95 values from/into modpath records
97 void (*vpi_get_delays_)(vpiHandle, p_vpi_delay);
98 void (*vpi_put_delays_)(vpiHandle, p_vpi_delay);
102 * In general a vpi object is a structure that contains the member
103 * "base" that is a __vpiHandle object. This template can convert any
104 * of those structures into a vpiHandle object.
106 template <class T> vpiHandle vpi_handle(T obj)
107 { return &obj->base; }
110 * The vpiHandle for an iterator has this structure. The definition of
111 * the methods lives in vpi_iter.c
113 * The args and nargs members point to the array of vpiHandle objects
114 * that are to be iterated over. The next member is the index of the
115 * next item to be returned by a vpi_scan.
117 * The free_args_flag member is true if when this iterator object is
118 * released it must also free the args array.
120 struct __vpiIterator {
121 struct __vpiHandle base;
122 vpiHandle *args;
123 unsigned nargs;
124 unsigned next;
125 bool free_args_flag;
128 extern vpiHandle vpip_make_iterator(unsigned nargs, vpiHandle*args,
129 bool free_args_flag);
132 * This represents callback handles. There are some private types that
133 * are defined and used in vpi_callback.cc.
135 struct __vpiCallback {
136 struct __vpiHandle base;
138 // user supplied callback data
139 struct t_cb_data cb_data;
140 struct t_vpi_time cb_time;
142 // scheduled event
143 struct sync_cb* cb_sync;
145 // Used for listing callbacks.
146 struct __vpiCallback*next;
149 extern struct __vpiCallback* new_vpi_callback();
150 extern void callback_execute(struct __vpiCallback*cur);
152 struct __vpiSystemTime {
153 struct __vpiHandle base;
154 struct __vpiScope *scope;
158 * Scopes are created by .scope statements in the source. These
159 * objects hold the items and properties that are knowingly bound to a
160 * scope.
162 struct __vpiScope {
163 struct __vpiHandle base;
164 struct __vpiScope *scope;
165 /* The scope has a name. */
166 const char*name;
167 const char*tname;
168 unsigned file_idx;
169 unsigned lineno;
170 unsigned def_file_idx;
171 unsigned def_lineno;
172 /* The scope has a system time of its own. */
173 struct __vpiSystemTime scoped_time;
174 struct __vpiSystemTime scoped_realtime;
175 /* Keep an array of internal scope items. */
176 struct __vpiHandle**intern;
177 unsigned nintern;
178 /* Keep a list of threads in the scope. */
179 vthread_t threads;
180 signed int time_units :8;
181 signed int time_precision :8;
184 extern struct __vpiScope* vpip_peek_current_scope(void);
185 extern void vpip_attach_to_current_scope(vpiHandle obj);
186 extern vpiHandle vpip_make_root_iterator(void);
187 extern void vpip_make_root_iterator(struct __vpiHandle**&table,
188 unsigned&ntable);
191 * Signals include the variable types (reg, integer, time) and are
192 * distinguished by the vpiType code. They also have a parent scope,
193 * a declared name and declaration indices.
195 struct __vpiSignal {
196 struct __vpiHandle base;
197 vpiHandle parent;
198 struct __vpiScope* scope;
199 /* The name of this reg/net, or the index for array words. */
200 union {
201 const char*name;
202 vpiHandle index;
203 } id;
204 /* The indices that define the width and access offset. */
205 int msb, lsb;
206 /* Flags */
207 unsigned signed_flag : 1;
208 unsigned isint_ : 1; // original type was integer
209 /* The represented value is here. */
210 vvp_net_t*node;
212 extern unsigned vpip_size(__vpiSignal *sig);
213 extern vpiHandle vpip_make_int(const char*name, int msb, int lsb,
214 vvp_net_t*vec);
215 extern vpiHandle vpip_make_reg(const char*name, int msb, int lsb,
216 bool signed_flag, vvp_net_t*net);
217 extern vpiHandle vpip_make_net(const char*name, int msb, int lsb,
218 bool signed_flag, vvp_net_t*node);
221 * This function safely converts a vpiHandle back to a
222 * __vpiSignal. Return a nil if the type is not appropriate.
224 extern __vpiSignal* vpip_signal_from_handle(vpiHandle obj);
227 struct __vpiModPathTerm {
228 struct __vpiHandle base;
229 vpiHandle expr;
230 /* The value returned by vpi_get(vpiEdge, ...); */
231 int edge;
234 struct __vpiModPathSrc {
235 struct __vpiHandle base;
236 struct __vpiModPath *dest;
237 int type;
239 /* This is the input expression for this modpath. */
240 struct __vpiModPathTerm path_term_in;
242 /* This is the input net for the modpath. signals on this net
243 are used to determine the modpath. They are *not* propagated
244 anywhere. */
245 vvp_net_t *net;
251 * The vpiMoaPath vpiHandle will define
252 * a vpiModPath of record .modpath as defined
253 * in the IEEE 1364
257 struct __vpiModPath {
258 struct __vpiScope *scope ;
260 class vvp_fun_modpath*modpath;
262 struct __vpiModPathTerm path_term_out;
263 vvp_net_t *input_net ;
266 extern struct __vpiModPathTerm* vpip_modpath_term_from_handle(vpiHandle ref);
267 extern struct __vpiModPathSrc* vpip_modpath_src_from_handle(vpiHandle ref);
271 * The Function is used to create the vpiHandle
272 * for vpiModPath && vpiModPathIn objects
275 extern struct __vpiModPathSrc* vpip_make_modpath_src (struct __vpiModPath*path_dest,
276 vvp_time64_t use_delay[12] ,
277 vvp_net_t *net ) ;
279 extern struct __vpiModPath* vpip_make_modpath(vvp_net_t *net) ;
283 * These methods support the vpi creation of events. The name string
284 * passed in will be saved, so the caller must allocate it (or not
285 * free it) after it is handed to this function.
287 struct __vpiNamedEvent {
288 struct __vpiHandle base;
289 /* base name of the event object */
290 const char*name;
291 /* Parent scope of this object. */
292 struct __vpiScope*scope;
293 /* The functor, used for %set operations. */
294 vvp_net_t*funct;
295 /* List of callbacks interested in this event. */
296 struct __vpiCallback*callbacks;
299 extern vpiHandle vpip_make_named_event(const char*name, vvp_net_t*f);
300 extern void vpip_run_named_event_callbacks(vpiHandle ref);
301 extern void vpip_real_value_change(struct __vpiCallback*cbh,
302 vpiHandle ref);
305 * Memory is an array of bits that is accessible in N-bit chunks, with
306 * N being the width of a word. The memory word handle just points
307 * back to the memory and uses an index to identify its position in
308 * the memory.
311 extern vpiHandle vpip_make_memory(vvp_memory_t mem, const char*name);
312 extern void vpip_memory_value_change(struct __vpiCallback*cbh,
313 vpiHandle ref);
315 extern void vpip_run_memory_value_change(vpiHandle ref, unsigned adr);
318 * These are the various variable types.
320 struct __vpiRealVar {
321 struct __vpiHandle base;
322 vpiHandle parent;
323 struct __vpiScope* scope;
324 /* The name of this variable, or the index for array words. */
325 union {
326 const char*name;
327 vpiHandle index;
328 } id;
329 vvp_net_t*net;
332 extern vpiHandle vpip_make_real_var(const char*name, vvp_net_t*net);
335 * When a loaded VPI module announces a system task/function, one
336 * __vpiUserSystf object is created to hold the definition of that
337 * task/function. The distinction between task and function is stored
338 * in the vpi_systf_data structure data that was supplied by the
339 * external module.
341 * When the compiler encounters a %vpi_call statement, it creates a
342 * __vpiSysTaskCall to represent that particular call. The call refers
343 * to the definition handle so that when the %vpi_call instruction is
344 * encountered at run-time, the definition can be located and used.
346 * The vpiSysTaskCall handles both functions and tasks, as the two are
347 * extremely similar. The different VPI type is reflected in a
348 * different vpi_type pointer in the base structure. The only
349 * additional part is the vbit/vwid that is used by the put of the
350 * system function call to place the values in the vthread bit space.
352 struct __vpiUserSystf {
353 struct __vpiHandle base;
354 s_vpi_systf_data info;
357 extern struct __vpiUserSystf* vpip_find_systf(const char*name);
360 struct __vpiSysTaskCall {
361 struct __vpiHandle base;
362 struct __vpiScope* scope;
363 struct __vpiUserSystf*defn;
364 unsigned nargs;
365 vpiHandle*args;
366 /* Support for vpi_get_userdata. */
367 void*userdata;
368 /* These represent where in the vthread to put the return value. */
369 unsigned vbit;
370 signed vwid;
371 class vvp_net_t*fnet;
372 unsigned file_idx;
373 unsigned lineno;
376 extern struct __vpiSysTaskCall*vpip_cur_task;
379 * These are implemented in vpi_const.cc. These are vpiHandles for
380 * constants.
382 * The persistent flag to vpip_make_string_const causes the created
383 * handle to be persistent. This is necessary for cases where the
384 * string handle may be reused, which is the normal case.
386 struct __vpiStringConst {
387 struct __vpiHandle base;
388 char*value;
389 size_t value_len;
392 vpiHandle vpip_make_string_const(char*text, bool persistent =true);
393 vpiHandle vpip_make_string_param(char*name, char*value,
394 long file_idx, long lineno);
396 struct __vpiBinaryConst {
397 struct __vpiHandle base;
398 vvp_vector4_t bits;
399 /* TRUE if this constant is signed. */
400 int signed_flag :1;
401 /* TRUE if this constant has an explicit size (i.e. 19'h0 vs. 'h0) */
402 int sized_flag :1;
405 vpiHandle vpip_make_binary_const(unsigned wid, const char*bits);
406 vpiHandle vpip_make_binary_param(char*name, const vvp_vector4_t&bits,
407 bool signed_flag,
408 long file_idx, long lineno);
410 struct __vpiDecConst {
411 struct __vpiHandle base;
412 int value;
415 vpiHandle vpip_make_dec_const(int value);
416 vpiHandle vpip_make_dec_const(struct __vpiDecConst*obj, int value);
418 struct __vpiRealConst {
419 struct __vpiHandle base;
420 double value;
423 vpiHandle vpip_make_real_const(double value);
424 vpiHandle vpip_make_real_param(char*name, double value,
425 long file_idx, long lineno);
428 * This one looks like a constant, but really is a vector in the current
429 * thread.
432 vpiHandle vpip_make_vthr_vector(unsigned base, unsigned wid, bool signed_flag);
434 vpiHandle vpip_make_vthr_word(unsigned base, const char*type);
437 * This function is called before any compilation to load VPI
438 * modules. This gives the modules a chance to announce their
439 * contained functions before compilation commences. It is called only
440 * once per module.
442 extern void vpip_load_module(const char*name);
444 # define VPIP_MODULE_PATH_MAX 64
445 extern const char* vpip_module_path[64];
446 extern unsigned vpip_module_path_cnt;
449 * The vpip_build_vpi_call function creates a __vpiSysTaskCall object
450 * and returns the handle. The compiler uses this function when it
451 * encounters a %vpi_call or %vpi_func statement.
453 * The %vpi_call instruction has as its only parameter the handle that
454 * is returned by the vpip_build_vpi_call. This includes all the
455 * information needed by vpip_execute_vpi_call to actually execute the
456 * call. However, the vpiSysTaskCall that is the returned handle,
457 * holds a parameter argument list that is passed in here.
459 * The vbit and vwid fields are used if this turns out to be a system
460 * function. In that case, the vbit and vwid are used to address the
461 * vector in thread bit space where the result is supposed to go.
463 * Note that the argv array is saved in the handle, and should should
464 * not be released by the caller.
466 extern vpiHandle vpip_build_vpi_call(const char*name,
467 unsigned vbit, int vwid,
468 class vvp_net_t*fnet,
469 unsigned argc,
470 vpiHandle*argv,
471 long file_idx,
472 long lineno);
474 extern vthread_t vpip_current_vthread;
476 extern void vpip_execute_vpi_call(vthread_t thr, vpiHandle obj);
480 * These are functions used by the compiler to prepare for compilation
481 * and to finish compilation in preparation for execution.
484 vpiHandle vpip_sim_time(struct __vpiScope*scope);
485 vpiHandle vpip_sim_realtime(struct __vpiScope*scope);
487 extern int vpip_get_time_precision(void);
488 extern void vpip_set_time_precision(int pres);
490 extern int vpip_time_units_from_handle(vpiHandle obj);
491 extern int vpip_time_precision_from_handle(vpiHandle obj);
493 extern void vpip_time_to_timestruct(struct t_vpi_time*ts, vvp_time64_t ti);
494 extern vvp_time64_t vpip_timestruct_to_time(const struct t_vpi_time*ts);
496 extern double vpip_time_to_scaled_real(vvp_time64_t ti, struct __vpiScope*sc);
497 extern vvp_time64_t vpip_scaled_real_to_time64(double val, struct __vpiScope*sc);
500 * These functions are used mostly as compile time to strings into
501 * permallocated memory. The vpip_string function is the most general,
502 * it allocates a fresh string no matter what. The vpip_name_string
503 * allocates a string and keeps a pointer in the hash, and tries to
504 * reuse it if it can. This us useful for handle names, which may be
505 * reused in different scopes.
507 extern const char* vpip_string(const char*str);
508 extern const char* vpip_name_string(const char*str);
512 * This function is used to make decimal string versions of various
513 * vectors. The input format is a vvp_vector4_t, and the result is
514 * written into buf, without overflowing nbuf.
516 extern unsigned vpip_vec4_to_dec_str(const vvp_vector4_t&vec4,
517 char *buf, unsigned int nbuf,
518 int signed_flag);
520 extern void vpip_bin_str_to_vec4(vvp_vector4_t&val,
521 const char*buf, bool signed_flag);
523 extern void vpip_vec4_to_hex_str(const vvp_vector4_t&bits, char*buf,
524 unsigned nbuf, bool signed_flag);
526 extern void vpip_vec4_to_oct_str(const vvp_vector4_t&bits, char*buf,
527 unsigned nbuf, bool signed_flag);
529 extern void vpip_oct_str_to_vec4(vvp_vector4_t&val, const char*str);
530 extern void vpip_dec_str_to_vec4(vvp_vector4_t&val, const char*str, bool sign);
531 extern void vpip_hex_str_to_vec4(vvp_vector4_t&val, const char*str);
533 extern void vpip_vec4_get_value(const vvp_vector4_t&word_val, unsigned width,
534 bool signed_flag, s_vpi_value*vp);
537 * Function defined in vpi_signal.cc to manage vpi_get_* persistent
538 * storage.
540 enum vpi_rbuf_t {
541 RBUF_VAL =0,
542 /* Storage for *_get_value() */
543 RBUF_STR
544 /* Storage for *_get_str() */
546 extern char *need_result_buf(unsigned cnt, vpi_rbuf_t type);
547 /* following two routines use need_result_buf(, RBUF_STR) */
548 extern char *simple_set_rbuf_str(const char *s1);
549 extern char *generic_get_str(int code, vpiHandle ref, const char *name, const char *index);
551 #endif