Interactively specified macros

For the compiled version of the language, macros are specified using the FHDL macro processor. Since this macro processor is not available at run time, the interactive command interpreter provides a native macro facility. A macro is defined as follows.

<label>: macro <argument-names>
... <macro body> ...
endmacro

The macro body may contain any interactive statement except "on" or "xon." (Macro definitions may not appear inside of an "on" block.) The list of argument names is optional. An example of a macro is given below.

cycle: macro
go;go
endmacro

Once a macro is defined, its name may be used as a command, so the following command would cause two "go" statements to be executed.

cycle

When argument names are specified, each name defines a local variable whose value is supplied from the command line that invoked the macro. (The name may duplicate that of an existing object such as a variable or on block.) Furthermore, new local variables are generated for each call, so recursive macros will work properly. The following is an example of a macro with arguments.

test1: macro a,b
for 0->bus1,bus1<a,bus1+1->bus1
for 0->bus2,bus2<b,bus2+1->bus2
go
endfor
endfor
endmacro

This macro would be invoked as follows.

test1 5,10

The number of arguments on the invoking command line does not have to match the number of arguments in the definition. Extra arguments are ignored, while missing arguments are assigned the value zero. If a variable or signal name is used as an argument, it will be passed by address, so macros may perform side-effects on their arguments. Constants and expressions are passed by value, so an argument of "(x)" will cause the variable x to be passed by value.

If a "variable" statement appears within the body of a macro, it defines local variables that exist only while the macro is being executed. Local variables are created and initialized to zero when the execution of the macro begins. If a local variable has the same name as some other object, the local definition will replace the former definition of the name while the macro is executing. The former definition is restored after the macro terminates, and its value is unaffected. Thus local variables will work properly in recursive macros, and it is not necessary for different macros to use unique local variable names, even if they invoke each other.

When a macro is invoked it inherits the environment of its caller. Any variable or signal that was accesible to the caller is also accessible to the macro. It is possible for a nested macro to perform side-effects on the local variables of its caller, although this practice is not recommended.