Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Macro Preprocessor Part 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Chapter 4

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

Source Macro Expanded Compiler Object


Code Processor Code /Assembler program
(with macro)

5
Introduction to Macro Processors
(Cont.)
 C uses a macro preprocessor to support
language extensions, such as named constants,
expressions, and file inclusion.

#define max(a,b) ((a<b)?(a):(b))


#define MACBUF 4
#include <stdio.h>

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

Macro body contains no label 9


Fig 4.1: Macro Definition (Cont.)

Macro definition

Macro body contains no label 10


Macro Invocation
 A macro invocation statement (a macro call) gives
the name of the macro instruction being invoked and
the arguments in expanding the macro.
 Macro Invocation vs. Subroutine Call.
 Statements of the macro body are expanded each time the
macro is invoked.
 Statements of the subroutine appear only one, regardless
of how many times the subroutine is called.
 Macro invocation is more efficient than subroutine call,
however, the code size is larger
11
Fig 4.1: Macro Invocation

Macro invocation

12
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.
 The arguments and parameters are associated with one
another according to their positions.
 The first argument in the macro invocation corresponds to the
first parameter in the macro prototype, etc.

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

 One-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.

 Defining MACROX does not define


RDBUFF and WRBUFF.
 These definitions are processed only when an
invocation of MACROX is expanded.
25
One-pass macro processor
 One-pass macro processor
 Every macro must be defined before it is called

 One-pass processor can alternate between macro


definition and macro expansion

 Nested macro definitions are allowed

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

You might also like