From e430ea13f479af82c8eb5c8d1ba5e0a60e45cebe Mon Sep 17 00:00:00 2001 From: Marlon James Date: Tue, 2 Jan 2024 13:25:14 -0800 Subject: [PATCH] Fix `vpi_get()` and `vpi_get64()` to return vpiUndefined on errors (#4795) See IEEE 1800-2017 section 38.6 and 38.7. --- include/verilated_vpi.cpp | 18 +++++++++--------- test_regress/t/t_vpi_memory.cpp | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/verilated_vpi.cpp b/include/verilated_vpi.cpp index 80cee6346..c450e1420 100644 --- a/include/verilated_vpi.cpp +++ b/include/verilated_vpi.cpp @@ -1991,41 +1991,41 @@ PLI_INT32 vpi_get(PLI_INT32 property, vpiHandle object) { } case vpiType: { const VerilatedVpio* const vop = VerilatedVpio::castp(object); - if (VL_UNLIKELY(!vop)) return 0; + if (VL_UNLIKELY(!vop)) return vpiUndefined; return vop->type(); } case vpiConstType: { const VerilatedVpio* const vop = VerilatedVpio::castp(object); - if (VL_UNLIKELY(!vop)) return 0; + if (VL_UNLIKELY(!vop)) return vpiUndefined; return vop->constType(); } case vpiDirection: { // By forethought, the directions already are vpi enumerated const VerilatedVpioVarBase* const vop = VerilatedVpioVarBase::castp(object); - if (VL_UNLIKELY(!vop)) return 0; + if (VL_UNLIKELY(!vop)) return vpiUndefined; return vop->varp()->vldir(); } case vpiScalar: // FALLTHRU case vpiVector: { const VerilatedVpioVarBase* const vop = VerilatedVpioVarBase::castp(object); - if (VL_UNLIKELY(!vop)) return 0; + if (VL_UNLIKELY(!vop)) return vpiUndefined; return (property == vpiVector) ^ (vop->varp()->dims() == 0); } case vpiSize: { const VerilatedVpioVarBase* const vop = VerilatedVpioVarBase::castp(object); - if (VL_UNLIKELY(!vop)) return 0; + if (VL_UNLIKELY(!vop)) return vpiUndefined; return vop->size(); } default: - VL_VPI_WARNING_(__FILE__, __LINE__, "%s: Unsupported type %s, nothing will be returned", - __func__, VerilatedVpiError::strFromVpiProp(property)); - return 0; + VL_VPI_ERROR_(__FILE__, __LINE__, "%s: Unsupported property %s, nothing will be returned", + __func__, VerilatedVpiError::strFromVpiProp(property)); + return vpiUndefined; } } PLI_INT64 vpi_get64(PLI_INT32 /*property*/, vpiHandle /*object*/) { VL_VPI_UNIMP_(); - return 0; + return vpiUndefined; } PLI_BYTE8* vpi_get_str(PLI_INT32 property, vpiHandle object) { diff --git a/test_regress/t/t_vpi_memory.cpp b/test_regress/t/t_vpi_memory.cpp index 66014e71d..9ef3fbe97 100644 --- a/test_regress/t/t_vpi_memory.cpp +++ b/test_regress/t/t_vpi_memory.cpp @@ -152,10 +152,10 @@ void _mem_check(const char* name, int size, int left, int right, int words) { // make sure trying to get properties that don't exist // doesn't crash TestVpiHandle iter_h = vpi_iterate(vpiMemoryWord, mem_h); - int should_be_0 = vpi_get(vpiSize, iter_h); - TEST_CHECK_EQ(should_be_0, 0); - should_be_0 = vpi_get(vpiIndex, iter_h); - TEST_CHECK_EQ(should_be_0, 0); + int should_be_undefined = vpi_get(vpiSize, iter_h); + TEST_CHECK_EQ(should_be_undefined, vpiUndefined); + should_be_undefined = vpi_get(vpiIndex, iter_h); + TEST_CHECK_EQ(should_be_undefined, vpiUndefined); vpiHandle should_be_NULL = vpi_handle(vpiLeftRange, iter_h); TEST_CHECK_EQ(should_be_NULL, 0); should_be_NULL = vpi_handle(vpiRightRange, iter_h);