Macro Processor Notes
Macro Processor Notes
Macro Processor Notes
ENDM
Part I is expanded if condition part is true, otherwise part II is expanded. Compare
operators: NE, EQ, LE, GT.
Macro-Time Variables:
Macro-time variables (often called as SET Symbol) can be used to store working
values during the macro expansion. Any symbol that begins with symbol & and not a
macro instruction parameter is considered as macro-time variable. All such variables are
initialized to zero.
Fig 4.9(a)
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 15
Figure 4.5(a) gives the definition of the macro RDBUFF with the parameters &INDEV,
&BUFADR, &RECLTH, &EOR, &MAXLTH. According to the above program if
&EOR has any value, then &EORCK is set to 1 by using the directive SET, otherwise it
retains its default value 0.
Fig 4.9(b) Use of Macro-Time Variable with EOF being NOT NULL
Fig 4.9(c) Use of Macro-Time conditional statement with EOF being NULL
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 16
Fig 4.9(d) Use of Time-variable with EOF NOT NULL and MAXLENGTH being NULL
The above programs show the expansion of Macro invocation statements with different
values for the time variables. In figure 4.9(b) the &EOF value is NULL. When the macro
invocation is done, IF statement is executed, if it is true EORCK is set to 1, otherwise
normal execution of the other part of the program is continued.
The macro processor must maintain a symbol table that contains the value of all macro-
time variables used. Entries in this table are modified when SET statements are
processed. The table is used to look up the current value of the macro-time variable
whenever it is required.
When an IF statement is encountered during the expansion of a macro, the specified
Boolean expression is evaluated.
If the value of this expression TRUE,
The macro processor continues to process lines from the DEFTAB until it
encounters the ELSE or ENDIF statement.
If an ELSE is found, macro processor skips lines in DEFTAB until the next
ENDIF.
Once it reaches ENDIF, it resumes expanding the macro in the usual way.
If the value of the expression is FALSE,
The macro processor skips ahead in DEFTAB until it encounters next ELSE or
ENDIF statement.
The macro processor then resumes normal macro expansion.
The macro-time IF-ELSE-ENDIF structure provides a mechanism for either
generating(once) or skipping selected statements in the macro body. There is another
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 17
construct WHILE statement which specifies that the following line until the next ENDW
statement, are to be generated repeatedly as long as a particular condition is true. The
testing of this condition, and the looping are done during the macro is under expansion.
The example shown below shows the usage of Macro-Time Looping statement.
WHILE-ENDW structure
When an WHILE statement is encountered during the expansion of a macro, the
specified Boolean expression is evaluated.
TRUE
o The macro processor continues to process lines from DEFTAB until it
encounters the next ENDWstatement.
o When ENDW is encountered, the macro processor returns to the preceding
WHILE, re-evaluates the Boolean expression, and takes action based
on the new value.
FALSE
o The macro processor skips ahead in DEFTAB until it finds the next
ENDW statement and then resumes normal macro expansion.
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 18
4.2.4 Keyword Macro Parameters
All the macro instruction definitions used positional parameters. Parameters and
arguments are matched according to their positions in the macro prototype and the macro
invocation statement. The programmer needs to be careful while specifying the
arguments. If an argument is to be omitted the macro invocation statement must contain a
null argument mentioned with two commas.
Positional parameters are suitable for the macro invocation. But if the macro
invocation has large number of parameters, and if only few of the values need to be used
in a typical invocation, a different type of parameter specification is required (for
example, in many cases most of the parameters may have default values, and the
invocation may mention only the changes from the default values).
Ex: XXX MACRO &P1, &P2, ., &P20, .
XXX A1, A2,,,,,,,,,,,,A20,..
Null arguments
Keyword parameters
Each argument value is written with a keyword that names the corresponding
parameter.
Arguments may appear in any order.
Null arguments no longer need to be used.
Ex: XXX P1=A1, P2=A2, P20=A20.
It is easier to read and much less error-prone than the positional method.
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 19
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 20
Fig 4.10 Example showing the usage of Keyword Parameter
4.3 Macro Processor Design Options
4.3.1 Recursive Macro Expansion
We have seen an example of the definition of one macro instruction by another. But we
have not dealt with the invocation of one macro by another. The following example
shows the invocation of one macro by another macro.
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 21
Problem of Recursive Expansion
Previous macro processor design cannot handle such kind of recursive macro
invocation and expansion
o The procedure EXPAND would be called recursively, thus the invocation
arguments in the ARGTAB will be overwritten. (P.201)
o The Boolean variable EXPANDING would be set to FALSE when the
inner macro expansion is finished, i.e., the macro process would forget
that it had been in the middle of expanding an outer macro.
Solutions
o Write the macro processor in a programming language that allows
recursive calls, thus local variables will be retained.
o If you are writing in a language without recursion support, use a stack to
take care of pushing and popping local variables and return addresses.
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 22
The procedure EXPAND would be called when the macro was recognized. The
arguments from the macro invocation would be entered into ARGTAB as follows:
Parameter Value
1 BUFFER
2 LENGTH
3 F1
4 (unused)
- -
The Boolean variable EXPANDING would be set to TRUE, and expansion of the macro
invocation statement would begin. The processing would proceed normally until
statement invoking RDCHAR is processed. This time, ARGTAB would look like
Parameter Value
1 F1
2 (Unused)
-- --
At the expansion, when the end of RDCHAR is recognized, EXPANDING would be set
to FALSE. Thus the macro processor would forget that it had been in the middle of
expanding a macro when it encountered the RDCHAR statement. In addition, the
arguments from the original macro invocation (RDBUFF) would be lost because the
value in ARGTAB was overwritten with the arguments from the invocation of
RDCHAR.
4.3.2 General-Purpose Macro Processors
Macro processors that do not dependent on any particular programming language,
but can be used with a variety of different languages
Pros
o Programmers do not need to learn many macro languages.
o Although its development costs are somewhat greater than those for a
language specific macro processor, this expense does not need to be
repeated for each language, thus save substantial overall cost.
Cons
o Large number of details must be dealt with in a real programming
language
Situations in which normal macro parameter substitution should
not occur, e.g., comments.
Facilities for grouping together terms, expressions, or statements
Tokens, e.g., identifiers, constants, operators, keywords
Syntax had better be consistent with the source programming
language
Chapter 4 Macro Processor
S. Seema Dept of CSE, MSRIT 23
4.3.3 Macro Processing within Language Translators
The macro processors we discussed are called Preprocessors.
o Process macro definitions
o Expand macro invocations
o Produce an expanded version of the source program, which is then used as
input to an assembler or compiler
You may also combine the macro processing functions with the language
translator:
o Line-by-line macro processor
o Integrated macro processor
4.3.4 Line-by-Line Macro Processor
Used as a sort of input routine for the assembler or compiler
o Read source program
o Process macro definitions and expand macro invocations
o Pass output lines to the assembler or compiler
Benefits
o Avoid making an extra pass over the source program.
o Data structures required by the macro processor and the language translator
can be combined (e.g., OPTAB and NAMTAB)
o Utility subroutines can be used by both macro processor and the language
translator.
Scanning input lines
Searching tables
Data format conversion
o It is easier to give diagnostic messages related to the source statements
4.3.5 Integrated Macro Processor
An integrated macro processor can potentially make use of any information about
the source program that is extracted by the language translator.
o Ex (blanks are not significant in FORTRAN)
DO 100 I = 1,20
a DO statement
DO 100 I = 1
An assignment statement
DO100I: variable (blanks are not significant in FORTRAN)
An integrated macro processor can support macro instructions that depend upon
the context in which they occur.