A Simple Macro Definition

Suppose you want to define a functional block that will invert three signals simultaneously. An example of the functional block, as it would appear in the FHDL text, is given below.

abc: not3 (a,b,c),(abar,bbar,cbar)

The following macro definition could be used to define the "not3" functional block. (This block is simple enough to be defined using FHDL subnetworks, but we will quickly move to more complicated examples.)

not3: 'macro
'input 'a,'b',c
'output 'd,'e,'f
not 'a,'d
not 'b,'e
not 'c,'f
'endmacro

This example contains several instances of macro keywords and variables. All macro keywords begin with the character ', as do all macro variables. This example also contains occurrences of the two types of statements recognized by the macro processor. Any statement that has a macro keyword for an opcode is a preprocessor statement. Any other statement is a text statement. Preprocessor statements are used to define macros and variables, and to control the processing of text statements. Text statements are used to generate text. In the preceding example, the first three statements are preprocessor statements, the next three are text statements, and the last statement is a preprocessor statement.

All macro definitions must begin with a 'macro statement and end with an 'endmacro statement. The label on the 'macro statement gives the name of the macro. Macro definitions may not be nested, and a macro may not generate the definition of another macro. The following statement, which invokes the "not3" macro, is an example of a macro call.

qed: not3 (x,y,z),(q,e,d)

The macro preprocessor removes all macro calls and replaces them with the text generated by the text statements of the macro definition. In this case the result will be as follows.

not x,q
not y,e
not z,d

Note that the label "qed" has been thrown away. The two macro statements 'input and 'output define macro variables. The value of these variables is taken from the macro call. The value of variable 'a will be the text of the first element of the input list of the macro call, while the value of the variable 'f will be the text of the third element of the output list. Variables defined using 'input and 'output statements are of the type "string." As will be explained below, macro variables may be of three different types, string, integer, and list. The scope of macro variables is the macro definition. When the name of a macro variable is encountered in a text statement, the value of the variable replaces the name. Only one scan of each text statement is done.

The number of inputs and outputs in the macro call need not match the number of inputs and outputs declared in the macro definition. Extra inputs and outputs are ignored, while variables corresponding to missing inputs and outputs contain the null string.