J
Jonathan Bromley
Guest
I got a nasty shock today when I found that system
functions such as $sformat are not re-entrant.
This is somewhat inconvenient. Consider, for
example, this simple recursive string reverser
in SystemVerilog:
function automatic string str_rev(string s);
string r;
if (s.len() < 2) return s;
$sformat(r, "%s%s",
s[s.len()-1],
str_rev(s.substr(0, s.len()-2)));
return r;
endfunction : str_rev
Two simulators silently give erroneous results;
a third sometimes reports the re-entrancy as an
error and sometimes segfaults, depending on the
exact details of the function. Moving the
recursive call of str_rev outside the call to
$sformat restores sanity at the cost of an
extra variable.
I can't find, anywhere in the 1364 or 1800 standards,
anything that says that these TFs are static. What
have I missed? Is this a shortcoming in the LRM, an
intentional piece of language definition for some
implementation-related reason, or a tool bug?
Is it, perhaps, that they are in effect PLI calls
and such calls have static storage for arguments
and return values?
Insights gratefully received.
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
functions such as $sformat are not re-entrant.
This is somewhat inconvenient. Consider, for
example, this simple recursive string reverser
in SystemVerilog:
function automatic string str_rev(string s);
string r;
if (s.len() < 2) return s;
$sformat(r, "%s%s",
s[s.len()-1],
str_rev(s.substr(0, s.len()-2)));
return r;
endfunction : str_rev
Two simulators silently give erroneous results;
a third sometimes reports the re-entrancy as an
error and sometimes segfaults, depending on the
exact details of the function. Moving the
recursive call of str_rev outside the call to
$sformat restores sanity at the cost of an
extra variable.
I can't find, anywhere in the 1364 or 1800 standards,
anything that says that these TFs are static. What
have I missed? Is this a shortcoming in the LRM, an
intentional piece of language definition for some
implementation-related reason, or a tool bug?
Is it, perhaps, that they are in effect PLI calls
and such calls have static storage for arguments
and return values?
Insights gratefully received.
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.