2001-03-16 02:44:34 +01:00
|
|
|
/*
|
2018-10-29 21:33:52 +01:00
|
|
|
* Copyright (c) 2001-2018 Stephen Williams (steve@icarus.com)
|
2001-03-16 02:44:34 +01:00
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form under the terms of the GNU
|
|
|
|
|
* General Public License as published by the Free Software
|
|
|
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2001-03-16 02:44:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
# include "config.h"
|
|
|
|
|
# include "vpi_priv.h"
|
|
|
|
|
# include "ivl_dlfcn.h"
|
2009-01-24 01:04:44 +01:00
|
|
|
# include "vvp_cleanup.h"
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cstdio>
|
|
|
|
|
# include <cstring>
|
2002-03-05 06:31:52 +01:00
|
|
|
# include <sys/types.h>
|
|
|
|
|
# include <sys/stat.h>
|
2010-10-24 00:52:56 +02:00
|
|
|
# include "ivl_alloc.h"
|
2001-03-16 02:44:34 +01:00
|
|
|
|
2009-01-24 01:04:44 +01:00
|
|
|
static ivl_dll_t*dll_list = 0;
|
|
|
|
|
static unsigned dll_list_cnt = 0;
|
|
|
|
|
|
2001-03-16 02:44:34 +01:00
|
|
|
typedef void (*vlog_startup_routines_t)(void);
|
|
|
|
|
|
2018-10-29 21:33:52 +01:00
|
|
|
# define VPIP_MODULE_PATH_MAX 64
|
2001-07-26 05:13:51 +02:00
|
|
|
|
2018-10-29 21:33:52 +01:00
|
|
|
static const char* vpip_module_path[VPIP_MODULE_PATH_MAX] = {0};
|
2001-07-26 05:13:51 +02:00
|
|
|
|
2018-10-29 21:33:52 +01:00
|
|
|
static unsigned vpip_module_path_cnt = 0;
|
|
|
|
|
|
|
|
|
|
static bool disable_default_paths = false;
|
|
|
|
|
|
|
|
|
|
void vpip_clear_module_paths()
|
|
|
|
|
{
|
|
|
|
|
vpip_module_path_cnt = 0;
|
|
|
|
|
vpip_module_path[0] = 0;
|
|
|
|
|
disable_default_paths = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vpip_add_module_path(const char*path)
|
|
|
|
|
{
|
|
|
|
|
if (vpip_module_path_cnt >= VPIP_MODULE_PATH_MAX) {
|
|
|
|
|
fprintf(stderr, "Too many module paths specified\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
vpip_module_path[vpip_module_path_cnt++] = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vpip_add_env_and_default_module_paths()
|
|
|
|
|
{
|
|
|
|
|
if (disable_default_paths)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (char *var = ::getenv("IVERILOG_VPI_MODULE_PATH")) {
|
|
|
|
|
char *ptr = var;
|
|
|
|
|
char *end = var+strlen(var);
|
|
|
|
|
int len = 0;
|
|
|
|
|
while (ptr <= end) {
|
|
|
|
|
if (*ptr == 0 || *ptr == ':' || *ptr == ';') {
|
|
|
|
|
if (len > 0) {
|
|
|
|
|
vpip_add_module_path(strndup(var, len));
|
|
|
|
|
}
|
|
|
|
|
len = 0;
|
|
|
|
|
var = ptr+1;
|
|
|
|
|
} else {
|
|
|
|
|
len++;
|
|
|
|
|
}
|
|
|
|
|
ptr++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
/* Calculate the module path from the path to the command.
|
|
|
|
|
This is necessary because of the installation process on
|
|
|
|
|
Windows. Mostly, it is those darn drive letters, but oh
|
|
|
|
|
well. We know the command path is formed like this:
|
|
|
|
|
|
|
|
|
|
D:\iverilog\bin\iverilog.exe
|
|
|
|
|
|
|
|
|
|
The IVL_ROOT in a Windows installation is the path:
|
|
|
|
|
|
|
|
|
|
D:\iverilog\lib\ivl$(suffix)
|
|
|
|
|
|
|
|
|
|
so we chop the file name and the last directory by
|
|
|
|
|
turning the last two \ characters to null. Then we append
|
|
|
|
|
the lib\ivl$(suffix) to finish. */
|
|
|
|
|
char *s;
|
|
|
|
|
char basepath[4096], tmp[4096];
|
|
|
|
|
GetModuleFileName(NULL, tmp, sizeof tmp);
|
|
|
|
|
/* Convert to a short name to remove any embedded spaces. */
|
|
|
|
|
GetShortPathName(tmp, basepath, sizeof basepath);
|
|
|
|
|
s = strrchr(basepath, '\\');
|
|
|
|
|
if (s) *s = 0;
|
|
|
|
|
else {
|
|
|
|
|
fprintf(stderr, "%s: Missing first \\ in exe path!\n", argv[0]);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
s = strrchr(basepath, '\\');
|
|
|
|
|
if (s) *s = 0;
|
|
|
|
|
else {
|
|
|
|
|
fprintf(stderr, "%s: Missing second \\ in exe path!\n", argv[0]);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
strcat(s, "\\lib\\ivl" IVL_SUFFIX);
|
|
|
|
|
vpip_add_module_path(strdup(basepath));
|
|
|
|
|
#else
|
|
|
|
|
#ifdef MODULE_DIR1
|
|
|
|
|
vpip_add_module_path(MODULE_DIR1);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef MODULE_DIR2
|
|
|
|
|
vpip_add_module_path(MODULE_DIR2);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2001-07-26 05:13:51 +02:00
|
|
|
|
2009-01-24 01:04:44 +01:00
|
|
|
void load_module_delete(void)
|
|
|
|
|
{
|
|
|
|
|
for (unsigned idx = 0; idx < dll_list_cnt; idx += 1) {
|
|
|
|
|
ivl_dlclose(dll_list[idx]);
|
|
|
|
|
}
|
|
|
|
|
free(dll_list);
|
|
|
|
|
dll_list = 0;
|
|
|
|
|
dll_list_cnt = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-07-26 05:13:51 +02:00
|
|
|
void vpip_load_module(const char*name)
|
2001-03-16 02:44:34 +01:00
|
|
|
{
|
2002-03-05 06:31:52 +01:00
|
|
|
struct stat sb;
|
|
|
|
|
int rc;
|
2003-02-16 03:21:20 +01:00
|
|
|
bool export_flag = false;
|
2002-03-05 06:31:52 +01:00
|
|
|
char buf[4096];
|
|
|
|
|
|
2001-05-22 04:14:47 +02:00
|
|
|
#ifdef __MINGW32__
|
2001-07-26 05:13:51 +02:00
|
|
|
const char sep = '\\';
|
2001-05-22 04:14:47 +02:00
|
|
|
#else
|
2001-07-26 05:13:51 +02:00
|
|
|
const char sep = '/';
|
2001-05-22 04:14:47 +02:00
|
|
|
#endif
|
2001-03-16 02:44:34 +01:00
|
|
|
|
2001-07-26 05:13:51 +02:00
|
|
|
ivl_dll_t dll = 0;
|
2002-03-05 06:31:52 +01:00
|
|
|
buf[0] = 0; /* terminate the string */
|
2001-07-28 05:29:42 +02:00
|
|
|
if (strchr(name, sep)) {
|
|
|
|
|
/* If the name has at least one directory character in
|
2002-03-05 06:31:52 +01:00
|
|
|
it, then assume it is a complete name, maybe including any
|
2003-02-10 00:33:26 +01:00
|
|
|
possible .vpi suffix. */
|
2003-02-16 03:21:20 +01:00
|
|
|
export_flag = false;
|
2002-03-05 06:31:52 +01:00
|
|
|
rc = stat(name, &sb);
|
2001-07-28 05:29:42 +02:00
|
|
|
|
2002-03-05 06:31:52 +01:00
|
|
|
if (rc != 0) { /* did we find a file? */
|
|
|
|
|
/* no, try with a .vpi suffix too */
|
2003-02-16 03:21:20 +01:00
|
|
|
export_flag = false;
|
2001-10-14 20:42:46 +02:00
|
|
|
sprintf(buf, "%s.vpi", name);
|
2002-03-05 06:31:52 +01:00
|
|
|
rc = stat(buf, &sb);
|
2001-10-14 20:42:46 +02:00
|
|
|
|
2008-01-29 21:19:59 +01:00
|
|
|
/* Try also with the .vpl suffix. */
|
2003-02-16 03:21:20 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
|
export_flag = true;
|
|
|
|
|
sprintf(buf, "%s.vpl", name);
|
|
|
|
|
rc = stat(buf, &sb);
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-05 06:31:52 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
|
fprintf(stderr, "%s: Unable to find module file `%s' "
|
|
|
|
|
"or `%s.vpi'.\n", name,name,buf);
|
2001-10-14 20:42:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2002-03-05 06:31:52 +01:00
|
|
|
} else {
|
|
|
|
|
strcpy(buf,name); /* yes copy the name into the buffer */
|
2001-07-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
2002-03-05 06:31:52 +01:00
|
|
|
rc = -1;
|
2001-07-28 05:29:42 +02:00
|
|
|
for (unsigned idx = 0
|
2002-03-05 06:31:52 +01:00
|
|
|
; (rc != 0) && (idx < vpip_module_path_cnt)
|
2001-07-28 05:29:42 +02:00
|
|
|
; idx += 1) {
|
2003-02-16 03:21:20 +01:00
|
|
|
export_flag = false;
|
2001-07-28 05:29:42 +02:00
|
|
|
sprintf(buf, "%s%c%s.vpi", vpip_module_path[idx], sep, name);
|
2002-03-05 06:31:52 +01:00
|
|
|
rc = stat(buf,&sb);
|
2003-02-16 03:21:20 +01:00
|
|
|
|
|
|
|
|
if (rc != 0) {
|
|
|
|
|
export_flag = true;
|
|
|
|
|
sprintf(buf, "%s%c%s.vpl",
|
|
|
|
|
vpip_module_path[idx], sep, name);
|
|
|
|
|
rc = stat(buf,&sb);
|
|
|
|
|
}
|
2001-07-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
2002-03-05 06:31:52 +01:00
|
|
|
if (rc != 0) {
|
2001-07-28 05:29:42 +02:00
|
|
|
fprintf(stderr, "%s: Unable to find a "
|
2002-03-05 06:31:52 +01:00
|
|
|
"`%s.vpi' module on the search path.\n",
|
2004-10-04 03:10:51 +02:00
|
|
|
name, name);
|
2001-07-28 05:29:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2001-03-23 03:40:22 +01:00
|
|
|
}
|
2001-03-16 02:44:34 +01:00
|
|
|
|
2004-10-04 03:10:51 +02:00
|
|
|
/* must have found some file that could possibly be a vpi module
|
2002-03-05 06:31:52 +01:00
|
|
|
* try to open it as a shared object.
|
|
|
|
|
*/
|
2003-02-16 03:21:20 +01:00
|
|
|
dll = ivl_dlopen(buf, export_flag);
|
2002-03-05 06:31:52 +01:00
|
|
|
if(dll==0) {
|
|
|
|
|
/* hmm, this failed, let the user know what has really gone wrong */
|
|
|
|
|
fprintf(stderr,"%s:`%s' failed to open using dlopen() because:\n"
|
|
|
|
|
" %s.\n",name,buf,dlerror());
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-09-30 19:40:50 +02:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
/* For this check MinGW does not want the leading underscore! */
|
|
|
|
|
void*table = ivl_dlsym(dll, "vlog_startup_routines");
|
|
|
|
|
#else
|
2001-03-23 03:40:22 +01:00
|
|
|
void*table = ivl_dlsym(dll, LU "vlog_startup_routines" TU);
|
2008-09-30 19:40:50 +02:00
|
|
|
#endif
|
2001-03-23 03:40:22 +01:00
|
|
|
if (table == 0) {
|
|
|
|
|
fprintf(stderr, "%s: no vlog_startup_routines\n", name);
|
|
|
|
|
ivl_dlclose(dll);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2001-03-16 02:44:34 +01:00
|
|
|
|
2009-01-24 01:04:44 +01:00
|
|
|
/* Add the dll to the list so it can be closed when we are done. */
|
|
|
|
|
dll_list_cnt += 1;
|
|
|
|
|
dll_list = (ivl_dll_t*)realloc(dll_list, dll_list_cnt*sizeof(ivl_dll_t));
|
|
|
|
|
dll_list[dll_list_cnt-1] = dll;
|
|
|
|
|
|
2002-05-18 04:34:11 +02:00
|
|
|
vpi_mode_flag = VPI_MODE_REGISTER;
|
2001-03-23 03:40:22 +01:00
|
|
|
vlog_startup_routines_t*routines = (vlog_startup_routines_t*)table;
|
|
|
|
|
for (unsigned tmp = 0 ; routines[tmp] ; tmp += 1)
|
|
|
|
|
(routines[tmp])();
|
2002-05-18 04:34:11 +02:00
|
|
|
vpi_mode_flag = VPI_MODE_NONE;
|
2001-03-16 02:44:34 +01:00
|
|
|
}
|