From d746c592b24c970e8fe0c8a6a92a46b00dddeebc Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sat, 1 Jan 2022 10:58:18 +0100 Subject: [PATCH] tgt-vvp: Fix out-of-bounds compressed assignment to arrays If the index of an array access is known to be out-of-bounds during elaboration it is replaced with 'x. In the tgt-vvp backend that is handling compressed array assignments there is an assert() that triggers if the index is an undefined immediate. There is already an existing code path that is capable of handling out-of-bounds access. Remove the assert and set the index to ULONG_MAX to trigger taking the out-of-bound access path. On this out-of-bounds path the write to the array is skipped. But this leaves the result on the vector stack. Insert the `%pop/vec4` instruction to make sure it is removed. Signed-off-by: Lars-Peter Clausen --- tgt-vvp/stmt_assign.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tgt-vvp/stmt_assign.c b/tgt-vvp/stmt_assign.c index e240fd619..257b53aea 100644 --- a/tgt-vvp/stmt_assign.c +++ b/tgt-vvp/stmt_assign.c @@ -21,6 +21,7 @@ # include # include # include +# include /* * These functions handle the blocking assignment. Use the %set @@ -101,8 +102,10 @@ static void get_vec_from_lval_slice(ivl_lval_t lval, struct vec_slice_info*slice it to select the word, and pay no further heed to the expression itself. */ if (word_ix && number_is_immediate(word_ix, IMM_WID, 0)) { - assert(! number_is_unknown(word_ix)); - use_word = get_number_immediate(word_ix); + if (number_is_unknown(word_ix)) + use_word = ULONG_MAX; // The largest valid index is ULONG_MAX - 1 + else + use_word = get_number_immediate(word_ix); word_ix = 0; } @@ -321,6 +324,7 @@ static void put_vec_to_lval_slice(ivl_lval_t lval, struct vec_slice_info*slice, clr_word(word_idx); } else { fprintf(vvp_out," ; Skip this slice write to v%p [%lu]\n", sig, slice->u_.memory_word_static.use_word); + fprintf(vvp_out," %%pop/vec4 1;\n"); } break;