Chapter 4 Macro Processors: - Basic Macro Processor Functions
Chapter 4 Macro Processors: - Basic Macro Processor Functions
Introduction
A macro instruction (macro) is a notational convenience for the programmer
It allows the programmer to write shorthand version of a program (module programming)
The macro processor replaces each macro instruction with the corresponding group of source language statements (expanding)
Normally, it performs no analysis of the text it handles. It does not concern the meaning of the involved statements during macro expansion.
Macro expansion
Source M1 MACRO &D1, &D2 STA &D1 STB &D2 MEND . M1 DATA1, DATA2 . M1 DATA4, DATA3 Expanded source . . . STA DATA1 STB DATA2 . STA DATA4 STB DATA3 .
4 4 5 5 , ,
"
'
3 " # ! !
"
) " 3'
3'
Macro invocation
A macro invocation statement (a macro call) gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro.
macro_name p1, p2,
Question
How does a programmer decide to use macro calls or procedure calls?
From the viewpoint of a programmer From the viewpoint of the CPU
-
Pass by Reference
void exchange(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } main() { int i=1, j=3; printf("BEFORE - %d %d\n", i, j); exchange(&i, &j); printf("AFTER - %d %d\n", i, j); }
main() { int i=1, j=3; printf("BEFORE - %d %d\n", i, j); swap(i,j); printf("AFTER - %d %d\n", i, j); }
Macro expansion
Each macro invocation statement will be expanded into the statements that form the body of the macro. Arguments from the macro invocation are substituted for the parameters in the macro prototype (according to their positions).
In the definition of macro: parameter In the macro invocation: argument
Comment lines within the macro body will be deleted. Macro invocation statement itself has been included as a comment line. The label on the macro invocation statement has been retained as a label on the first statement generated in the macro expansion.
We can use a macro instruction in exactly the same way as an assembler language mnemonic.
Solutions:
Do not use labels in the body of macro. Explicitly use PC-relative addressing instead.
Ex, in RDBUFF and WRBUFF macros, JEQ *+11 JLT *-14 It is inconvenient and error-prone.
The way of avoiding such error-prone method will be discussed in Section 4.2.2
Pass 2:
Expand all macro invocation statements
Moreover, the body of one macro can contain definitions of other macros.
5
'
'
A ; < 6 # ! C
NAMTAB
Stores macro names Serves as an index to DEFTAB
Pointers to the beginning and the end of the macro definition (DEFTAB)
ARGTAB
Stores the arguments of macro invocation according to their positions in the argument list As the macro is expanded, arguments from ARGTAB are substituted for the corresponding parameters in the macro body.
Data structures
Algorithm
MAIN program - Iterations of GETLINE PROCESSLINE
Procedure GETLINE If EXPANDING then get the next line to be processed from DEFTAB Else read next line from input file
Procedure EXPAND Set up the argument values in ARGTAB Expand a macro invocation statement (like in MAIN procedure) - Iterations of GETLINE PROCESSLINE
: = <E ; ) " F A
"
G H ; ;< @
A "
) A) 3
A 3
; G@ < C
H E< @ 5
Two-pass algorithm
Pass1: Recognize macro definitions Pass2: Recognize macro calls Nested macro definitions are not allowed