RE: [sv-bc] Mantis 1338 - Clarificaion on String literal behavior in macros

From: Alsop, Thomas R <thomas.r.alsop_at_.....>
Date: Tue Nov 20 2007 - 16:48:09 PST
HI Eric,

 

Excellent feedback.  I think I'm finally grasping this issue completely.


 

I think your interpretation of the second macro is correct.  In fact,
the more I look at the wording in the LRM the more I realize I need to
rewrite this, I'll quote it here again:

 

An `" overrides the usual lexical meaning of " and indicates that the
expansion should include an actual quotation mark. This allows string
literals to be constructed from macro arguments. A string literal "
embedded inside a macro will not replace a macro argument within that
string.

 

 

The key sentence there is underlined. Shalom eluded to this and I think
your interpretation is one that we should expect.  If a string literal
exists inside of a macro, we do not do any macro replacement on it.  By
string literal I mean " and not `".  The `" in my mind means we want to
construct a string literal as part of the macro replacement using the
macro arguments.

 

So how does this read now:

 

An `" overrides the usual lexical meaning of " and indicates that the
expansion should include an actual quotation mark and the macro argument
replacements. This allows string literals to be constructed from macro
arguments. A string literal embedded inside a macro will not replace
macro arguments within that string.

 

I have also fixed the example to read better and clarify the meaning of
the wording enhancements above. Note the extra string literal terminator
added (Highlighted)

 

`define nosubx(x,y) "x: " `\`"y`\`"`"

 

Expands to :

 

$display("x: \"right side\"");

 

I think with the extra wording and the example, this should be clear
now.  And I tend to agree with the proposal now. I'll upload these
changes shortly. -Tom

 

 

________________________________

From: Coffin, Eric [mailto:eric_coffin@mentor.com] 
Sent: Tuesday, November 20, 2007 1:56 PM
To: Alsop, Thomas R
Cc: sv-bc@server.eda.org
Subject: Re: [sv-bc] Mantis 1338 - Clarificaion on String literal
behavior in macros

 

Hi Thomas,

Thanks for writing the 1338 proposal.  I've found this area of the
language a bit problematic myself, and I agree with you in that it needs
clarification.

My take on the three macro-operator character sequences `", `` and `\`"
is that they do not have any special meaning within a string literal.
If they did have special meaning why would the `" macro-operator be
needed at all?

The 1338 proposal defines two macros.  They are:

`define msg(x,y) `"x: `\`"y`\`"`"
`define nosubx(x,y) "x: `\`"y`\`"`"

And the examples of their use are:

$display(`msg(left side,right side));
$display(`nosubx(left side,right side));

I agree with mantis 1338's interpretation of the first macro's
expansion:

$display("left side: \"right side\"");

I disagree with mantis 1338's interpretation of the second macro.  The
second macro should be expanded to:
"x: `\`"right side`\`"`"  which is an ill-formed argument to the display
systask.  The first double-quote starts a string literal.  The `\` are
just characters within the string literal, and have no special meaning.
The string literal is terminated with the second double-quote forming
the literal:  "x:`\`".  The next two preprocessor tokens are the
identifiers 'right' and 'side'.  These two idents are followed by the
special macro-operators  `\`" and `".

As an aside, P1800/D4 table 5.9.1 does not explicitly state how to
interpret characters within string literals that are prefixed with the
back slash character but that are not in the table.  Take, for example,
the string literal "\`"-- is it two characters long (a backslash
followed by a tick) or one character (a tick)?  ModelSim 

-Eric



Alsop, Thomas R wrote: 

I have updated mantis item 1338 with a proposal on what I believe is the
intended behavior of string literals inside of macros WRT to argument
substitution. 

 

I am slightly hesitant on adding this wording simply because some vendor
tools do not distinguish on this behavior.  Shalom is asking for
clarification about how we treat macro argument substitution WRT string
literals.  I agree about the ambiguity of the current wording.  The fact
that we have `" in macros that allow us keep the string character after
substitution means that string literals without the back tick have an
alternative meaning.  I would interpret that meaning as a string
literal, not a replacement on the string token.  

 

This does collide with current implementation.  For example I ran VCS on
the following code snippets:

 

`define msg(x,y) `"x: `\`"y`\`"`"

`define msg_nobacktick(x,y) "x: \"y\""

`define msg_nosubsitute(x) "x"

`define msg_subsitute(x) `"x`"

 

module foo();

 

initial begin

  //$fsdbDumpvars(0,foo,"+all");

 

  $display(`msg(left side,right side));

  $display(`msg_nobacktick(left side,right side));

  $display(`msg_nosubsitute(left side));

  $display(`msg_subsitute(left side));

 

end

endmodule

 

 

This produced the following results:

 

left side: "right side"

left side: "right side"

left side

left side

 

The conclusion being that " is treated exactly the same way as `".

 

With the change in the proposal this would have the following results
instead:

 

left side: "right side"

x: "y"

x

left side

 

I am happy to shoot down the proposal.  The intent is to come up with
one and discuss it.  I don't see a strong need for this change.  The
alternative proposal is to clarify that " and `" behave the same.

 

 


-- 
This message has been scanned for viruses and 
dangerous content by MailScanner <http://www.mailscanner.info/> , and is

believed to be clean. 


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Tue Nov 20 16:48:46 2007

This archive was generated by hypermail 2.1.8 : Tue Nov 20 2007 - 16:49:02 PST