Re: [sv-bc] For Future Consideration: Macros & Semicolons


Subject: Re: [sv-bc] For Future Consideration: Macros & Semicolons
From: Steven Sharp (sharp@cadence.com)
Date: Tue Feb 18 2003 - 16:29:59 PST


I don't think this is as significant of an issue as it is in C. In C, you
don't necessarily know whether you are using a macro or a function (and it
may change between compiles), so you need to have the same syntax either way.
In Verilog, you always know when you are using a macro, because you have to
put a ` in front of it. So you can just follow the rule of never putting a
semicolon after a macro use that expands into a statement or declaration.
It may go against the typing habits you have developed, but it will work.

Note that your dangling "if(1)" workaround doesn't work in all situations.
For example:

`define foobar(d) \
  begin \
    d = d + 1; \
  end if (1)
  
initial
  if (a)
    `foo(b);
  else
    e = f;
    
expands into (with some indentation change to show meaning)

initial
  if (a)
    begin
      d = d + 1;
    end
  if (1)
    ;
  else
    e = f;
    
Note that the "else" is now paired with the "if (1)" and will never execute,
while the original intent was to pair it with the "if (a)" and have it
execute if "a" is false.

Nor does it work in situations where only one statement is legal, not a
statement sequence. For example

always @(a)
  foobar(b);
  
expands into the illegal

always @(a)
  begin
    d = d + 1;
  end
  if (1) ;

If you want to do this, I would suggest instead using

`define foobar(d) \
  if (1) begin \
    begin \
      d = d + 1; \
    end \
  end else

where I believe the "begin d = d + 1; end" could be an arbitrary statement
or compound statement. You can probably dispense with the extra begin-end
inside the "if (1)" unless the arbitrary statement is an if-statement with
no else clause (because the begin-end keeps it from stealing the empty else
clause and leaving the "if (1)" unmatched to steal a later else clause).

This does still have the problem that if you forget the semicolon after it,
it won't necessarily give you an error. Instead, the next statement will
go into the never-executed else clause. So it doesn't work as well as the
C do-while trick.

Steven Sharp
sharp@cadence.com



This archive was generated by hypermail 2b28 : Tue Feb 18 2003 - 16:30:49 PST