Macro Preprocessor Part 1
Macro Preprocessor Part 1
Macro Preprocessor Part 1
Macro Processors
1
Chapter 4: Macro Processors
4.1 Basic Macro Processors Functions
4.2 Machine-Independent Macro Processors
Features
4.3 Macro Processors Design Options
4.4 Implementation Examples
2
Introduction to Macro Processors
A macro instruction (macro) is a notational
convenience for the programmer.
Allow the programmer to write a shorthand version of a
program
A macro represents a commonly used group of
statements in the source programming language.
Expanding the macros
The macro processor replaces each macro instruction with
the corresponding group of source language statements.
3
Introduction to Macro Processors
(Cont.)
A macro processor
Essentially involve the substitution of one group
of characters or lines for another.
Normally, it performs no analysis of the text it
handles.
It doesn’t concern the meaning of the involved
statements during macro expansion
The design of a macro processor generally is
machine independent.
4
Introduction to Macro Processors
(Cont.)
Three examples of actual macro processors:
A macro processor designed for use by assembler
language programmers
Used with a high-level programming language
General-purpose macro processor, which is not
tied to any particular language
5
Introduction to Macro Processors
(Cont.)
C uses a macro preprocessor to support
language extensions, such as named constants,
expressions, and file inclusion.
6
4.1 Basic Macro Processors Functions
Macro processor should processes the
Macro definitions
Define macro name, group of instructions
Macro invocation (macro calls)
A body is simply copied or substituted at the point of call
Expansion with substitution of parameters
Arguments are textually substituted for the parameters
The resulting procedure body is textually substituted for the call
7
Macro Definition
Two new assembler directives are used in macro definition:
MACRO: identify the beginning of a macro definition
MEND: identify the end of a macro definition
label op operands
name MACRO parameters
:
body
:
MEND
Parameters: the entries in the operand field identify the parameters of the
macro instruction
We require each parameter begins with ‘
&’
Body: the statements that will be generated as the expansion of the macro.
Prototype for the macro:
The macro name and parameters define a pattern or prototype for the macro
instructions used by the programmer 8
Fig 4.1: Macro Definition
•
Macro definition
12
Macro Expansion
Each macro invocation statement will be expanded
into the statements that form the body of the macro.
13
Macro Expansion
Expanded source program
Source program Macro .
definition
.
WD MACRO .
STA DATA1
STA DATA1
STB DATA2
Macro STB DATA2
MEND Expansion .
. by
WD STA DATA1
Macro
. processor STB DATA2
WD .
. STA DATA1
WD Macro STB DATA2
. invocation
. 14
Macro Expansion with Parameters
Substitution
Source program Macro Expanded source program
definition
.
WD MACRO &A1,&A2 .
STA &A1 STA DATA1
STB &A2 STB DATA2
Macro
MEND Expansion .
. by STA DATA3
WD DATA1,DATA2 Macro
STB DATA4
. processor
WD DATA3,DATA4 .
. STA DATA5
WD DATA5,DATA6 Macro STB DATA6
. invocation .
15
Program From Fig. 4.1 with Macros
Expanded (fig. 4.2)
•
Macro expansion
16
Program From Fig. 4.1 with Macros
Expanded (fig. 4.2)(Cont.)
•
Macro expansion
17
Program From Fig. 4.1 with Macros
Expanded (fig. 4.2)(Cont.)
•
Macro expansion
18
No Label in the Body of Macro
Problem of the label in the body of macro:
If the same macro is expanded multiple times at different places in the
program.
There will be duplicate labels, which will be treated as errors by the
assembler,
Solutions:
Simply not to use labels in the body of macro.
Explicitly use PC-relative addressing instead.
For example, in RDBUFF and WRBUFF macros,
JEQ * +11
JLT *-14
It is inconvenient and error-prone.
Other better solution?
Mentioned in Section 4.2.2.
19
4.1.2 Macro Processors Algorithm and
Data Structures
Two-pass macro processor
20
Two-pass macro processor
Two-pass macro processor
Pass1: process all macro definitions
Pass2: expand all macro invocation statements
Problem
Does not allow nested macro definitions
Nested macro definitions
The body of a macro contains definitions of other macros
Because all macros would have to be defined during the
first pass before any macro invocations were expanded
Solution
One-pass macro processor
21
Nested Macros Definition
MACROS (for SIC)
contains the definitions of RDBUFF and
WRBUFF written in SIC instructions.
MACROX (for SIC/XE)
contains the definitions of RDBUFF and
WRBUFF written in SIC/XE instructions.
Example 4.3
22
Macro Definition within a Macro Body
(Figure 4.3(a))
23
Macro Definition within a Macro Body
(Figure 4.3(b))
24
Nested Macros Definition (Cont.)
A program that is to be run on SIC system
could invoke MACROS whereas a program to
be run on SIC/XE can invoke MACROX.
26
Three Main Data Structures
DEFTAB
A definition table used to store macro definition including
macro prototype
macro body
Comment lines are omitted.
Positional notation has been used for the parameters for efficiency in
substituting arguments.
E.g. the first parameter &INDEV has been converted to ?1 (indicating the first
parameter in the prototype)
NAMTAB
A name table used to store the macro names
Serves as an index to DEFTAB
Pointers to the beginning and the end of the macro definition
ARGTAB
A argument table used to store the arguments used in the expansion of macro
invocation
As the macro is expanded, arguments are substituted for the corresponding
parameters in the macro body. 27
28
One-Pass Macro Processor
Procedures
Macro definition: DEFINE
Macro invocation: EXPAND
NAMTAB
One-Pass GETLINE
macro Macro DEFINE DEFTAB
definition
processor PROCESSLINE
Macro EXPAND ARGTAB
invocation
29
One-Pass Macro Processor Allows
Nested Macro Definition
Sub-procedure DEFINE should handle the
nested macro definition
Maintains a counter named LEVEL
Each time a MACRO directive is read, the value
of LEVEL is increased by 1
Each time an MEND directive is read, the value
of LEVEL is decreased by 1
31
Algorithm for one-pass macro processor (Fig. 4.5)
(Cont.)
32
33