systemc-clang icon indicating copy to clipboard operation
systemc-clang copied to clipboard

Subscripting of a function return value.

Open zhuanhao-wu opened this issue 3 years ago • 0 comments

Code in SystemC may exhibit the following pattern (see here), where the returned value is indexed.

bhdr.set_zb(!(get_window(b_wrk,w_wordoff) [0]));

Currently, the generated SystemVerilog code is as follows. The code will work under simulation but will not work for synthesis.

zhw__block_headerfp_t11_52__set_zb_func_4(
	bhdr_ms_proc_local_10_zb__ref_0,bhdr_ms_proc_local_10_exp__ref_0,
	!(zhw__get_window_func_3(
			b_wrk_ms_proc_local_6_f__ref_0,
			b_wrk_ms_proc_local_6_w__ref_0,
			w_wordoff_ms_proc_local_8,
			b_wrk_ms_proc_local_6_f__ref_0,
			b_wrk_ms_proc_local_6_w__ref_0
		)[(0)]
	),
	bhdr_ms_proc_local_10_zb__ref_0,
	bhdr_ms_proc_local_10_exp__ref_0
);

One tentative solution is to generate a local variable that holds the function return value.

logic res;
// ...
res = zhw__get_window_func_3(
			b_wrk_ms_proc_local_6_f__ref_0,
			b_wrk_ms_proc_local_6_w__ref_0,
			w_wordoff_ms_proc_local_8,
			b_wrk_ms_proc_local_6_f__ref_0,
			b_wrk_ms_proc_local_6_w__ref_0
		);

zhw__block_headerfp_t11_52__set_zb_func_4(
	bhdr_ms_proc_local_10_zb__ref_0,bhdr_ms_proc_local_10_exp__ref_0,
	!res[(0)],
	bhdr_ms_proc_local_10_zb__ref_0,
	bhdr_ms_proc_local_10_exp__ref_0
);

However, this is not general enough. For example, if we have func1_call()[0] || func2_call()[1], due to short circuit evaluation, func2_call[1] may not be executed. Hence, func2_call()[1] should be guarded by the result of the first condition to avoid any side effects in func2_call(). The most general way would be to expand all logical expressions and explicitly embed the short-circuit evaluation semantics.

logic[7:0] res1, res2;
res1= func1_call();
if(!res1) begin
  res2= func2_call();
end

zhuanhao-wu avatar Oct 24 '22 18:10 zhuanhao-wu