Re: [sv-bc] FW: [sv-ec] comment in compiler macros

From: Greg Jaxon <Greg.Jaxon_at_.....>
Date: Thu Jun 25 2009 - 15:03:28 PDT
Verilog and SV seem to have consciously avoided specifying preprocessing steps in any detail.
Compare our text with the C and C++ standard translation phase specification is like comparing
fog to sunshine.  Given that, any reading for "original intent" is delusional at best, but here goes...

Both section 25.5.1:

The macro text can be any arbitrary text specified on the same line as the text macro name. If more than one line is necessary to specify the text, the newline shall be preceded by a backslash ( \ ). The first newline not preceded by a backslash shall end the macro text. The newline preceded by a backslash shall be replaced in the expanded macro with a newline (but without the preceding backslash character).
... and section 5.4:
A one-line comment shall start with the two characters // and end with a newline.
... presume to own the input stream until the next newline character.
Unlike C/C++, backslash line continuation is not described as taking the highest precedence.  In fact it
is described exactly twice -in 25.5.1 (above) and in 5.9 quoted below
A string literal shall be contained in a single line unless the new line is immediately preceded by a \ (backslash). In this case, the backslash and the new line are ignored.
[Note to editor: you don't really mean that the new line is ignored! Only the newline character; please compound those words]

In these two cases \ has selectively appeared as a special syntax for producing two special kinds of BNF nonterminals.  As a corollary, we expect that trying to break an expression across multiple lines with a backslash continuation is not a transparent noop - it will usually produce a syntax error for the unknown \ operator.  Similarly, the lack of a line continuation rule for one-line comments suggests that the comment text absorbs the backslash character.

Having thus violated one of the earliest Unix conventions, Verilog has been free to be its own unique kind of crazy ever since. Implementations seem to be free to give precedence to either one-line comment or macro text in these cases.  I'm strongly in favor of letting the market decide which precedence is best.
  1. If // comes first, we might wonder whether the newline delimits both the comment and  the macro text.  I think  22.5.1 tried to resolve this by saying "If a one-line comment (that is, a comment specified with the characters //) is included in the text, then the comment shall not become part of the substituted text.".  This was necessary for  lines like `define asciiNL 8'h0A // Newline character
  2. If the macro text sees the line continuation first, the newline that it leaves in its place gives us a delimiter for the one-line comment, and the above sentence seems superfluous.
In neither case would the macro pick up any extraordinary power to eat the remainder of a line on which it was invoked.  The LRM prohibition on one-line comment text substitution doesn't address comment text which might be separately recognized as a pragma (a non-standard extension).  Those can continue to be substituted with the macro text, so long as they don't accidentally eat the ends of macro invocation lines.

I think either of these readings are consistent with the current text in both letter and spirit.  Reading 1 terminates the macro text immediately before the first one-line comment; Dan's example fails because a $display() call cannot appear in $unit.  Reading 2 allows Dan's example to parse as legal Verilog, and to cause $display() calls to happen.
A C/C++-like reading (early folding of line continuation) also makes Dan's example legal, but the $display() then
becomes part of the comment text.

Greg

Disclaimer:  I offer no voting position here - just checking that our diverging systems are within the letter of the law.


Arturo Salz wrote:

 

 

From: owner-sv-ec@eda.org [mailto:owner-sv-ec@eda.org] On Behalf Of Daniel Mlynek
Sent: Thursday, June 25, 2009 1:56 AM
To: sv-ec@eda.org
Subject: [sv-ec] comment in compiler macros

 

Question is how should compiler treat comments  in precompiler macros - i've browse LRM and I haven't found any answer. LRM states only :

If a one-line comment (that is, a comment specified with the characters //) is included in the text, then the comment shall not become part of the substituted text. The macro text can be blank, in which case the text macro is defined to be empty and no text is substituted when the macro is used.

But what for below case -  "//" comment out the end of line '\" needed to split lines of define - so imho comments finishes the macros and below code should fail

CODE:

 

`define DAN \
// test \
// test \
 $display("ok");
`define DAN1 //ref \
\
 $display("ok1"); 
 
module top;
 initial begin
  `DAN
  `DAN1
 end
  
endmodule

 

 

 

DANiel


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


--
This message has been scanned for viruses and
dangerous content by MailScanner, 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 Thu Jun 25 15:04:22 2009

This archive was generated by hypermail 2.1.8 : Thu Jun 25 2009 - 15:04:39 PDT