Factor out the common code for checking if a FD/MCD is valid

This commit is contained in:
Cary R 2020-05-16 18:02:46 -07:00
parent cf44f05cd3
commit 9b9be11cf6
4 changed files with 109 additions and 93 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999-2018 Stephen Williams (steve@icarus.com) * Copyright (c) 1999-2020 Stephen Williams (steve@icarus.com)
* *
* This source code is free software; you can redistribute it * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * and/or modify it in source code form under the terms of the GNU
@ -27,8 +27,6 @@
# include <math.h> # include <math.h>
# include "ivl_alloc.h" # include "ivl_alloc.h"
#define IS_MCD(mcd) !((mcd)>>31&1)
// Flag to enable better compatibility with other simulators // Flag to enable better compatibility with other simulators
static unsigned compatible_flag = 0; static unsigned compatible_flag = 0;
@ -1297,8 +1295,7 @@ static PLI_INT32 sys_display_calltf(ICARUS_VPI_CONST PLI_BYTE8 *name)
return 0; return 0;
} }
if ((! IS_MCD(fd_mcd) && vpi_get_file(fd_mcd) == NULL) || if (! is_valid_fd_mcd(fd_mcd)) {
( IS_MCD(fd_mcd) && my_mcd_printf(fd_mcd, "") == EOF)) {
vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh)); (int)vpi_get(vpiLineNo, callh));
vpi_printf("invalid file descriptor/MCD (0x%x) given " vpi_printf("invalid file descriptor/MCD (0x%x) given "
@ -1361,8 +1358,7 @@ static PLI_INT32 strobe_cb(p_cb_data cb)
/* We really need to cancel any $fstrobe() calls for a file when it /* We really need to cancel any $fstrobe() calls for a file when it
* is closed, but for now we will just skip processing the result. * is closed, but for now we will just skip processing the result.
* Which has the same basic effect. */ * Which has the same basic effect. */
if ((! IS_MCD(info->fd_mcd) && vpi_get_file(info->fd_mcd) != NULL) || if (is_valid_fd_mcd(info->fd_mcd)) {
( IS_MCD(info->fd_mcd) && my_mcd_printf(info->fd_mcd, "") != EOF)) {
char* result; char* result;
unsigned int size; unsigned int size;
/* Because %u and %z may put embedded NULL characters into the /* Because %u and %z may put embedded NULL characters into the
@ -1413,8 +1409,7 @@ static PLI_INT32 sys_strobe_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
return 0; return 0;
} }
if ((! IS_MCD(fd_mcd) && vpi_get_file(fd_mcd) == NULL) || if (! is_valid_fd_mcd(fd_mcd)) {
( IS_MCD(fd_mcd) && my_mcd_printf(fd_mcd, "") == EOF)) {
vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh)); (int)vpi_get(vpiLineNo, callh));
vpi_printf("invalid file descriptor/MCD (0x%x) given " vpi_printf("invalid file descriptor/MCD (0x%x) given "
@ -1452,6 +1447,7 @@ static PLI_INT32 sys_strobe_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
cb.value = 0; cb.value = 0;
cb.user_data = (char*)info; cb.user_data = (char*)info;
vpi_register_cb(&cb); vpi_register_cb(&cb);
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003-2018 Stephen Williams (steve@icarus.com) * Copyright (c) 2003-2020 Stephen Williams (steve@icarus.com)
* *
* This source code is free software; you can redistribute it * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * and/or modify it in source code form under the terms of the GNU
@ -26,7 +26,6 @@
# include <stdlib.h> # include <stdlib.h>
# include "ivl_alloc.h" # include "ivl_alloc.h"
#define IS_MCD(mcd) !((mcd)>>31&1)
/* /*
* Implement the $fopen system function. * Implement the $fopen system function.
@ -223,9 +222,10 @@ static PLI_INT32 sys_fclose_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
vpi_get_value(fd, &val); vpi_get_value(fd, &val);
fd_mcd = val.value.integer; fd_mcd = val.value.integer;
if ((! IS_MCD(fd_mcd) && vpi_get_file(fd_mcd) == NULL) || /* If the MCD is zero we have nothing to do so just return. */
( IS_MCD(fd_mcd) && vpi_mcd_printf(fd_mcd, "%s", "") == EOF) || if (fd_mcd == 0) return 0;
(! fd_mcd)) {
if (! is_valid_fd_mcd(fd_mcd)) {
vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh)); (int)vpi_get(vpiLineNo, callh));
vpi_printf("invalid file descriptor/MCD (0x%x) given to %s.\n", vpi_printf("invalid file descriptor/MCD (0x%x) given to %s.\n",
@ -270,9 +270,7 @@ static PLI_INT32 sys_fflush_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
/* If the MCD is zero we have nothing to do so just return. */ /* If the MCD is zero we have nothing to do so just return. */
if (fd_mcd == 0) return 0; if (fd_mcd == 0) return 0;
if ((! IS_MCD(fd_mcd) && vpi_get_file(fd_mcd) == NULL) || if (! is_valid_fd_mcd(fd_mcd)) {
( IS_MCD(fd_mcd) && vpi_mcd_printf(fd_mcd, "%s", "") == EOF) ||
(! fd_mcd)) {
vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh)); (int)vpi_get(vpiLineNo, callh));
vpi_printf("invalid file descriptor/MCD (0x%x) given to %s.\n", vpi_printf("invalid file descriptor/MCD (0x%x) given to %s.\n",
@ -886,8 +884,7 @@ static PLI_INT32 sys_fseek_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
if (oper < 0) { if (oper < 0) {
val.value.integer = EOF; val.value.integer = EOF;
errno = EINVAL; errno = EINVAL;
} } else
else
val.value.integer = fseek(fp, offset, oper); val.value.integer = fseek(fp, offset, oper);
#endif #endif
vpi_put_value(callh, &val, 0 , vpiNoDelay); vpi_put_value(callh, &val, 0 , vpiNoDelay);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003-2018 Stephen Williams (steve@icarus.com) * Copyright (c) 2003-2020 Stephen Williams (steve@icarus.com)
* *
* This source code is free software; you can redistribute it * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * and/or modify it in source code form under the terms of the GNU
@ -248,6 +248,25 @@ unsigned is_string_obj(vpiHandle obj)
} }
/*
* Check if the file descriptor or MCD is valid.
* For a MCD check that every bit set is a valid file and
* for a FD make sure it exists.
*/
unsigned is_valid_fd_mcd(PLI_UINT32 fd_mcd)
{
assert(fd_mcd); // Should already be handled!
if (IS_MCD(fd_mcd)){
if (vpi_mcd_printf(fd_mcd, "%s", "") == EOF) return 0;
} else {
if (vpi_get_file(fd_mcd) == NULL) return 0;
}
return 1;
}
/* /*
* Find the enclosing module. If there is no enclosing module (which can be * Find the enclosing module. If there is no enclosing module (which can be
* the case in SystemVerilog), return the highest enclosing scope. * the case in SystemVerilog), return the highest enclosing scope.

View File

@ -1,7 +1,7 @@
#ifndef IVL_sys_priv_H #ifndef IVL_sys_priv_H
#define IVL_sys_priv_H #define IVL_sys_priv_H
/* /*
* Copyright (c) 2002-2018 Stephen Williams (steve@icarus.com) * Copyright (c) 2002-2020 Stephen Williams (steve@icarus.com)
* *
* This source code is free software; you can redistribute it * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * and/or modify it in source code form under the terms of the GNU
@ -22,6 +22,8 @@
#include "vpi_config.h" #include "vpi_config.h"
#include "sv_vpi_user.h" #include "sv_vpi_user.h"
#define IS_MCD(mcd) !((mcd)>>31&1)
/* /*
* Context structure for PRNG in mt19937int.c * Context structure for PRNG in mt19937int.c
*/ */
@ -56,6 +58,8 @@ extern unsigned is_constant_obj(vpiHandle obj);
extern unsigned is_numeric_obj(vpiHandle obj); extern unsigned is_numeric_obj(vpiHandle obj);
extern unsigned is_string_obj(vpiHandle obj); extern unsigned is_string_obj(vpiHandle obj);
extern unsigned is_valid_fd_mcd(PLI_UINT32 fd_mcd);
extern vpiHandle sys_func_module(vpiHandle obj); extern vpiHandle sys_func_module(vpiHandle obj);
/* /*