Macro Processor
Macro Processor
1
Introduction
• A macro instruction (abbreviated to macro) is
simply
i l a notational
i l convenience
i for
f the
h
KTU ASSIST
programmer.
• A macro represents a commonly used group of
statements in the source programming language
• Expanding a macros
– Replace
p each macro instruction with the corresponding
p g
group of source language statements
2
Introduction (Cont’d)
• E.g.
– On SIC/XE requires a sequence of seven instructions to
KTU ASSIST
save the contents of all registers
• Write
W it one statement
t t t like
lik SAVERGS
• A macro processor is not directly related to the
architecture
hit t off the
th computer
t on which
hi h it is
i to
t run
• Macro processors can also be used with high-level
programming languages, OS command languages,
etc.
3
Basic Macro Processor Functions
Expanded program
A program with
Macro A program without
ih
Macro definitions and
Processor Macro definitions
Macro invocations
Object program
4
Basic Macro Processor Functions
• Macro Definition
– Two new assembler directives
KTU ASSIST
• MACRO
• MEND
– A pattern or prototype for the macro instruction
• Macro name and parameters
– See figure 4.1
41
5
KTU ASSIST
6
Basic Macro Processor Functions
• Macro invocation
– Often
Oft referred
f d tto as a macro call
ll
• Need the name of the macro instruction begin invoked and the
KTU ASSIST
arguments to be used in expanding the macro
• Expanded program
– Figure
g 4.2
– No macro instruction definitions
– Each macro invocation statement has been expanded
into the statements that form the body of the macro,
with the arguments from the macro invocation
substituted for the parameters in the prototype
7
KTU ASSIST
8
Basic Macro Processor Functions
• Macro invocations and subroutine calls are
different
KTU ASSIST
• Note also that the macro instructions have
been written so that the body of the macro
contains no label
– Why?
9
Macro Processor Algorithm and
Data Structures
• It is easy to design a two-pass macro processor
– Pass 1:
KTU ASSIST
• All macro definitions are processed
– Pass
P 22:
• All macro invocation statements are expanded
• H
However, a ttwo-pass macro processor would ld nott
allow the body of one macro instruction to contain
d fi iti
definitions off other
th macros
– See Figure 4.3
10
KTU ASSIST
11
Macro Processor Algorithm and
Data Structures
• Sub-Macro definitions are only processed
when an invocation of their Super-Macros
KTU ASSIST
p
are expanded
– See Figure 4.3: RDBUFF
• A one-pass macro processor that
th t can
alternate between macro definition and
macro expansions able to handle macros
g
like those in Figure 4.3
12
Macro Processor Algorithm and
Data Structures
• Because of the one-pass structure, the
definition of a macro must appear in the
KTU ASSIST
source pprogram
g
invoke that macro
before anyy statements that
13
KTU ASSIST
14
KTU ASSIST
15
KTU ASSIST
16
Machine-Independent
Machine Independent Macro
Processor Feature
• Concatenation of Macro Parameters
• Generation of Unique Labels
KTU ASSIST
•
•
Conditional Macro Expansion
Keyword Macro Parameters
17
Concatenation of Macro
Parameters
• Most macro processors allow parameters to
be concatenated with other character strings
KTU ASSIST
– The need of a special catenation operator
• LDA X&ID1
• LDA X&ID
– The catenation operator
• LDA X&ID1
• See figure 4.6
18
KTU ASSIST
19
Generation of Unique Labels
• It is in general not possible for the body of a
macro instruction to contain labels of the usual
kind
KTU ASSIST
– Leading to the use of relative addressing at the source
statement level
• Only be acceptable for short jumps
• Solution:
– Allowing the creation of special types of labels within
macro instructions
– See Figure 4.7
20
KTU ASSIST
21
Generation of Unique Labels
• Solution:
– Allowing the creation of special types of labels
KTU ASSIST
within macro instructions
– See Figure 4.7
• Labels used within he macro body begin with the
special character $
– Programmers are instructed no to use $ in their
source programs
22
Conditional Macro Expansion
• Most macro processors can modify the
sequence of statements generated for a
KTU ASSIST p
macro expansion, , depending
p g on the
arguments supplied in the macro invocation
• See Figure 4.8
48
23
KTU ASSIST
24
KTU ASSIST
25
Conditional Macro Expansion
• Most macro processors can modify the sequence
of statements generated for a macro expansion,
depending on the arguments supplied in the macro
invocation
KTU ASSIST
• See Figure 4.8
– Macro processor directive
• IF, ELSE, ENDIF
• SET
– Macro-time variable (set symbol)
• WHILE-ENDW
WHILE ENDW
– See Figure 4.9
26
KTU ASSIST
27
Keyword Macro Parameters
• Positional parameters
– Parameters and arguments were associated with
KTU ASSIST
each other accordingg to their ppositions in the
macro prototype and the macro invocation
statement
– Consecutive commas is necessary for a null
argument
GENER ,,DIRECT,,,,,,3
28
Keyword Macro Parameters
• Keyword parameters
– Each argument value is written with a keyword
KTU ASSIST
that names the corresponding
p g pparameter
– A macro may have a large number of
parameters , and only a few of these are given
values in a typical invocation
GENER TYPE=DIRECT,
TYPE DIRECT, CHANNEL=3
CHANNEL 3
29
KTU ASSIST
30
KTU ASSIST
31
Macro Processor Design
g Options
p
• Recursive Macro Expansion
– In Figure 4.3, we presented an example of the
definition of on macro instruction byy another.
• We have not dealt with the invocation of one macro
KTU ASSIST
by another (nested macro invocation)
– See Figure 4.11
32
KTU ASSIST
33
Macro Processor Design
g Options
p
• Recursive Macro Expansion Applying
Al ith off Fig.
Algorithm Fi 4.5
45
• Problem:
– The processing would proceed normally until line 50,
which contains a statement invoking RDCHAR
KTU ASSIST
– In add
addition,
t o , the
t e argument
a gu e t from
o the
t e original
o g a macro
ac o
invocation (RDBUFF) would be lost because the values in
ARGTAB were overwritten with the arguments from the
invocation of RDCHAR
• Solution:
– These problems are not difficult to solve if the macro
processor is begin written in a programming language that
allows recursive call
34
General-Purpose
General Purpose Macro
Processors
• Macro processors have been developed for
some high-level programming languages
KTU ASSIST
• These special
special-purpose
purpose macro processors are
similar in general function and approach;
however the details differ from language to
however,
language
35
General-Purpose
General Purpose Macro
Processors
• The advantages of such a general-purpose
approach to macro processing are obvious
KTU ASSIST
– The programmer does not need to learn about a
different macro facility for each compiler or
assembler
asse b e language,
a guage, so muchuc of
o the
t e time
t e and
a d
expense involved in training are eliminated
– A substantial overall saving in software
development cost
36
General-Purpose
General Purpose Macro
Processors
• In spite of the advantages noted, there are
still relatively few general-purpose macro
KTU ASSIST
processors. Why?
p y
1. In a typical programming language, there are
several situations in which normal macro
parameter substitution should no occur
– E.g.
E g comments should usually be ignored by a
macro processor
37
General-Purpose
General Purpose Macro
Processors
2. Another difference between programming
l
languages is
i related
l d to their
h i facilities
f ili i for
f
KTU ASSIST
grouping together terms, expressions, or
statements
– E.g. Some languages use keywords such as begin
andd endd for
f grouping
i statements.
t t t Others
Oth use
special characters such as { and }.
38
General-Purpose
General Purpose Macro
Processors
3. A more general problem involves the tokens
off the
h programming
i language
l
KTU ASSIST
– E.g. identifiers, constants, operators, and
k
keywordsd
– E.g. blanks
39
General-Purpose
General Purpose Macro
Processors
4. Another potential problem with general-
purpose macro processors involves
i l the
h syntax
KTU ASSIST
used for macro definitions and macro
i
invocation
i statements. With
Wi h most special-
i l
purpose macro processors, macro invocations
are very similar
i il ini form
f to statements in
i theh
source programming language
40
KTU ASSIST
41
KTU ASSIST
END