You are on page 1of 64

Macro Processors

Dr. Poonam Saini


poonamsaini@pec.ac.in

Introduction
z

Concept
A macro instruction is a notational convenience for the
programmer
Macros represents a commonly used group of statements in the
source programming language
It allows the programmer to write shorthand version of a program
(module programming) and leave mechanical details to be
handled by the macro processor
The macro processor replaces each macro invocation with the
corresponding sequence of statements (expanding)

Macro vs. Subroutine


z

Macro
the statement of expansion are generated each time the
macro are invoked

Subroutine
the statement in a subroutine appears only once

Use of a macro name in mnemonic field of an assembly statement


leads to its expansion whereas use of a subroutine name in a call
instruction leads to its execution

Macro Processor
z
z
z
z

Recognize macro definitions


Save the macro definition
Recognize macro calls
Expand macro calls

Source
Code
(with macro)

Macro
Processor

Expanded
Code

Compiler or
Assembler

obj

Why macro?
z

Following sequence of instructions is used to increment the


value in a memory word by a constant:
Move the value from memory word into a machine register
Increment the value in the machine register
Move the new value into the memory word

as the instruction sequence- MOVE-ADD-MOVE may be used a number


of times in a program, it is convenient to define a MACRO
using lexical expansion- macro call INCR A, B, AREG can lead to
generation of MOVE-ADD-MOVE instruction sequence to increment A by
the value of B using AREG to perform arithmetic
use of semantic expansion can enable the instruction sequence to be
adapted to the types A and B. E.g., for Intel 8088, an INC instruction
would be generated iff A is a byte operand and B has the value 1.
5

Macro Definition and Call


A macro definition is enclosed between a macro header
statement and a macro end statement
Typically located- start of program and consists of:
z
z
z

A macro prototype statement


One or more model statements
macro pre-processor statements

Macro prototype statement declares the name of the macro


and the names and kinds of its parameters
<macro name> [<formal parameter spec> [,..]]
(mnemonic field)

&<parameter name> [ <parameter kind>]

A model statement is a statement from which an assembly


language statement may be generated during macro
expansion
A pre-processor statement is used to perform auxiliary
functions during macro expansion
7

Macro call
z

A macro is called by writing the macro name in the


mnemonic field of an assembly statement.
<macro name> [ <actual parameter spec>[, ..]]
Operand specification in assembly language statement

example

MACRO
INCR
MOVER
ADD
MOVEM
MEND

&MEM_VAL, &INCR_VAL, &REG


&REG, &MEM_VAL
&REG, &INCR_VAL
&REG, &MEM_VAL
8

Macro expansion
z
z
z

A macro call leads to macro expansion


Macro call statement is replaced by a sequence of
assembly statements
To differentiate between the original statement and
statement resulting from macro expansion ---- each
expanded statement is marked with + preceding its
label field
Two notations Expansion time control flow
Lexical substitution

Flow of control during expansion


z
z
z
z

Default- sequential
Absence of pre-processor statement: model statements
are visited sequentially
starting with statement - macro prototype statement ending with statement preceding MEND statement
A pre-processor statement can alter the flow of control
during expansion -----why??
Some model statements are either never visited during expansion,
or
Repeatedly visited during expansion

Former results in conditional expansion and the latter in expansion


time loops
10

Outline of macro expansion


z

Flow of control during macro expansion is implemented


using macro expansion counter (MEC)

Algorithm
I.

II.

MEC:= statement number of first statement following the prototype


statement
While statement pointed by MEC is not a MEND statement
a)

If a model statement then

i. Expand the statement


ii. MEC:= MEC+1;
b)

Else (i.e., a pre-processor directive)

i.
III.

MEC:= new value specified in the statement;

Exit from macro expansion


11

Lexical substitution
z

Model statement has 3 types of strings


An ordinary string which stands for itself
The name of a formal parameter preceded by the character &
The name of a pre-processor variable also preceded by the
character &

During lexical expansion string of type 1- retained without substitution


string of type 2 and 3 replaced by the values of the formal
parameters or pre-processor variables
(rules for determining the value of a formal parameter depend on kind of
parameter- positional and keyword)
12

Parameter Substitution -- Example


Source
STRG MACRO &a1, &a2, &a3
STA
&a1
STB
&a2
STX
&a3
MEND
.
STRG DATA1, DATA2, DATA3
.
STRG DATA4, DATA5, DATA6
.
.

Expanded souce
.
.
.
STA
STB
STX
.
STA
STB
STX
.

{
{

DATA1
DATA2
DATA3
DATA4
DATA5
DATA6

13

Positional parameter
z

The value of a positional formal parameter XYZ is determined by the rule


of positional association as:
Find the ordinal position of XYZ in the list of formal parameters in macro prototype
statement
Find the actual parameter specification occupying the same ordinal position in the list
of actual parameters in the macro call statement

Consider callINCR A, B, AREG ----following positional association:


Formal parameter
value
MEM_VAL
A
INCR_VAL
B
REG
AREG
Lexical expansion of model statements leads to code
+
MOVER
AREG, A
+
ADD
AREG, B
+
MOVEM
AREG, A

14

Keyword parameter
z

The value of a formal parameter XYZ is determined by the rule of


keyword association as:
Find the actual parameter specification which has the form XYZ = <ordinary string>

Following macro call-----------are equivalent


INCR_M
MEM_VAL=A, INCR_VAL=B, REG=AREG
INCR_M
INCR_VAL=B, REG=AREG, MEM_VAL=A,
MACRO
INCR_M
MOVER
ADD
MOVEM

&MEM_VAL=, &INCR_VAL=, &REG


&REG, &MEM_VAL
&REG, &INCR_VAL
&REG, &MEM_VAL

MEND
15

Nested Macro
z

A model statement in a macro may constitute a call to


another macro. Such calls are known as nested macro
calls.
macro containing nested call outer macro
called macro inner macro

Expansion of nested macro calls- LIFO rule


In a structure of nested macro calls, expansion of the latest macro
call (i.e., the innermost macro call) is completed first

16

Nested Macros Definition


z

Macro definition within macros


process macro definition during expansion time

Example

17

18

Macros and Macrox


z
z

MACROS- standard SIC system


MACROX- SIC/XE system
Defining macros or macrox does not define RDBUFF and
other macro instructions.
The definitions are processed only when an invocation of
MACROS or MACROX is expanded

19

One-Pass Macro Processor


z

Prerequisite
every macro must be defined before it is called

Sub-procedures
macro definition: DEFINE
macro invocation: EXPAND
NAMTAB
MACRO

DEFINE
DEFTAB

PROCESSLINE
CALL

EXPAND

ARGTAB

20

Data structures
z

DEFTAB (Definition table)


Contains macro prototype and the statements that make up the
macro body
Comment lines from the macro definitions are not entered into
DEFTAB as they are not part of macro expansion
References to the macro instruction parameters are converted to a
positional notation for efficiency in substituting arguments

NAMTAB (Name table)


Macro names are entered into NAMTAB
Serves as an index to DEFTAB
For each macro instruction defined, NAMTAB contains pointers to
the beginning and end of the definition in DEFTAB
21

ARGTAB (Argument table)


Used during the expansion of macro invocations
When a macro invocation statement is recognized, the
arguments are stored in ARGTAB according to their position
in the argument list
As the macro is expanded, arguments from AGRTAB are
substituted for the corresponding parameters in the macro
body

22

EXPANDING

ARGTAB
F1
BUFFER
LENGTH

CLOOP RDBUFF F1, BUFFER, LENGTH


(Read record into buffer)
? Which notation / parameter substitution has been used in above figure

23

One-Pass Macro Processor That Allows

Nested Macro Definition


z

Sub-procedures
macro definition: DEFINE
macro invocation: EXPAND

EXPAND may invoke DEFINE when encounter


macro definition
NAMTAB
DEFTAB
ARGTAB
Expanding

MACRO

DEFINE

CALL

EXPAND

PROCESSLINE

MACRO Definition

24

25

Macro processor algorithm


z
z
z

procedure DEFINE- makes appropriate entries in DEFTAB


and NAMTAB
EXPAND is called to set up the argument values in AGRTAB
and expand a macro invocation statement
procedure GETLINE (can be called several times) gets the
next line to be processed
Depending upon whether the boolean variable Expanding is set to
TRUE or FALSE,
the line may come from DEFTAB (next line of a macro being
expanded), or
from the input file

28

How to find the MEND that corresponds to original MACRO


directive (refer slide 18 to identify problem)

Solution
Define procedure maintains counter named LEVEL
Each time a MACRO directive is read, the value of LEVEL is
increased by 1
Each time a MEND directive is read, the value of LEVEL is
decreased by 1
When LEVEL reaches 0, MEND corresponding to original MACRO
directive has been found

29

1-Pass Macro Processor


DEFINE

MACRO
PROCESSOR

GETLINE
EXPANDING=FALSE

PROCESSLINE
EXPAND

GETLINE
PROCESSLINE

EXPANDING=TRUE

GETLINE
PROCESSLINE

GETLINE

EXPANDING

FALSE

TRUE

READ FROM
DEFTAB

READ FROM
INPUT

30

ARGTAB
DEFTAB

MACRO Definition

NAMTAB

DEFINE
GETLINE

PROCESSLINE

Macro Invocation

EXPAND

ARGTAB

31

Comparison of Macro Processors Design


z

Single pass
every macro must be defined before it is called
one-pass processor can alternate between macro definition
and macro expansion
nested macro definitions may be allowed but nested calls
are not

Two pass algorithm


Pass1: Recognize macro definitions
Pass2: Recognize macro calls
nested macro definitions are not allowed

32

Machine-independent macro
processor features
z
z
z
z

Concatenation of macro parameters


Generation of unique labels
Conditional macro expansion
Keyword macro parameters

33

Concatenation of Macro Parameters


Consider a situation A program contains one series of variables named by the
symbol XA1, XA2, XA3, ., another series named by
XB1, XB2, XB3, .etc.
If similar processing is to be done each series of variables, the
processing is incorporated into macro instruction

Parameter to such a macro instruction could specify the


series of variables to be operated on (A, B, etc.)
Macro processor would use the parameter to construct
the symbols required in macro expansion (XA1, XB1, etc.)

34

Concatenation of Macro Parameters


z

Suppose parameter is named &ID


LDA

X&ID1

Special concatenation operator


LDA

X&ID1

*Macro processor deletes all occurrences of


immediately after parameter substitution

Example:

35

Generation of Unique Labels


z
z
z

In general, it is not possible for the body of macro


instruction to contain labels of usual kind
This leads to use of relative addressing at the source
statement level
Example consider definition of WRBUFF
WRBUFF
TD

z
z

MACRO
=X&OUTDEV

&OUTDEV, &BUFADR, &RECLTH


TEST OUTPUT DEVICE

Label on this instruction would be defined twice- once for


each invocation of WRBUFF (see fig 4. of L.Beck)
To avoid duplicate definitions which in turn would prevent
correct assembly of the resulting expanded program,
unique labels can be used
36

Generation of Unique Labels


z

No label- usage of relative operands for jump instructions


e.g.,
JEQ *-3
JLT *-14

LOOP UNTILL READY

acceptable for shorter jumps


inconvenient, error-prone, difficult to read for longer jumps
spanning several instructions
z

Many macro processors allows creation of special labels


within macro instructions
$LOOP

TD

=X&INDEV

TD

=XF1

TD

=XF1

1st call:
$AALOOP

2nd call:
$ABLOOP

37

z
z

Labels used within macro body begin with special


character $
Each symbol beginning with $ has been modified by
replacing $ with $AA
In general, $ will be replaced by $xx, where xx is a two-character
alphanumeric counter of the number of macro instructions
expanded

z
z

For the first macro expansion, xx will have the value AA.
For succeeding expansions, xx will be set to AB, AC, etc.
If only alphabetic and numeric characters are allowed in xx, the
counter may provide 1296 macro expansions in a single program
38

RDBUFF

F1, BUFFER, LENGTH

Conditional Macro Expansion


Problem
z Till now, each invocation of a particular macro was
expanded into same sequence of statements
z Although, these statements could be varied by the
substitution of parameters, however, the form of statement
and order in which they appeared were unchanged
Solution
z With conditional macro expansion, we can modify the
sequence of statements generated for a macro expansion,
using appropriate arguments in macro invocations.
It increases flexibility and power of a macro language
41

Conditional Macro Expansion


z

Macro-time conditional statements


IF-ELSE-ENDIF

Macro-time variables
any symbol that begins with the character & and that is not a
macro instruction parameter is assumed a MTV
macro-time variables are initialized to 0
macro-time variables can be changed with their values using SET
E.g., RDBUFF in next figure has two additional parameter
&EOR (specifies a hexadecimal character code that marks the end of a
record)
&MAXLTH (specifies the maximum length record that can be read)
42

RDBUFF

F3, BUF, RECL, 04, 2048

RDBUFF

0E, BUFFER, LENGTH, , 80

RDBUFF

F1, BUFF, RLENG, 04

Conditional Macro Expansion (Cont.)


z

Statements on lines 44-48 illustrate a simple example of


macro-time conditional expansion
IF evaluates a Boolean expression that is its operand
If value of expression is TRUE, the statements following IF are
generated until an ELSE is encountered
Otherwise, these statements are skipped and statements following
ELSE are generated
ENDIF terminates conditional expression started by IF

If parameter &MAXLTH is equal to null string (i.e., if


corresponding argument is omitted in the macro invocation),
the statement on line 45 is generated, otherwise 47 is
generated
46

Implementation of conditional
macro-expansion features
z
z

Macro processor must maintain a symbol table that


contains the values of all macro-time variables used
Entries in symbol table are made or modified when SET
statements are processed.
The table is used to look up current value of a macro-time variable
whenever it is required
During expansion, boolean expression in IF is evaluated.
If value is TRUE, macro processor process lines from DEFTAB
until next ELSE or ENDIF
If ELSE found, macro processor skips lines from DEFTAB until next
ENDIF.

If value is FALSE, macro processor skips ahead in DEFTAB until


next ELSE or ENDIF
47

Keyword Macro Parameters


z

All macro instructions till now used positional parameters


i.e., parameters and arguments are associated with each
other according to their positions in macro prototype and
macro invocation
Positional parameters are suitable for most macro
instructions, however, if a macro has large number of
parameters and only few of these are given values in a
typical invocation, different form of parameters are useful

48

Macro Processor Design Options


z

Recursive Macro Expansion

General-Purpose Macro Processors

Macro Processing within Language Translators

49

Recursive Macro Expansion


z

Previous examples deals with definition of one macro


instruction by another

RME deals with invocation of one macro by another

50

51

Consider a situation
RDCHAR is a macro and is invoked in macro RDBUFF
Unfortunately, the MP algorithm discussed so far cannot process
this. Why?
When RDBUFF is invoked, procedure EXPAND is called,
EXPANDING is set to true, and arguments (BUFFER, LENGTH, F1)
are entered to ARGTAB. During expansion, RDCHAR is invoked,
EXPAND is called again, new argument (F1) enters ARGTAB (to erase
the arguments of RDBUFF). After returning from EXPAND,
EXPANDING is set to false, which stops the expansion of RDBUFF.

52

ARGTABS during expansion


ARGTAB for Procedure EXPAND
PARAMETER

VALUE

BUFFER

LENGTH

F1

(unused)

ARGTAB for Procedure RDCHAR


PARAMETER

VALUE

F1

(unused)

.
53

Problem and Solution


When the end of definition of RDCHAR was recognized,
EXPANDING would be set to false
Thus, macro processor would forget that it has been in the
middle of expanding a macro when it has encountered RDCHAR
statement
In addition, the arguments from original macro invocation
(RDBUFF) would be lost---(overwrite)
Cause: recursive call of procedure EXPAND
When the RDBUFF macro invocation is encountered, EXPAND is called.
Later, it calls PROCESSLINE for line 50, which results in another call to
EXPAND before a return is made from the original call.

54

A similar problem would occur with PROCESSLINE since this


procedure too would be called recursively.
These problems are not difficult to solve if the macro processor is
being written in a programming language that allows recursive calls.
If a programming language that supports recursion is not available,
the programmer must take care of handling such items as
return addresses and
values of local variables (that is, handling by looping structure
and data values being saved on a stack).

55

General-purpose Macro Processors


z

What is the most common use of macro processor?


9 Simple- to aid assembler language programming

Often such macros are combined with or related to an


assembler
Macro processor have also been developed for high level
languages (?)
Special-purpose macro processors are similar in general
function and approach
However, details differs from language to language

z
z
z

56

General-purpose Macro Processors


The general-purpose macro processors are not dependent on any
particular programming language, but can be used with a variety of
different languages.
There are relatively few general-purpose macro processors.
The major reason is the large number of details that must be dealt within a
real programming language.
A general-purpose facility must provide some way for a user to define the
specific set of rules to be followed.

57

Difficulties / Disadvantages
Case 1: Comments are usually ignored by a macro processor (at least
in scanning for parameters). However, each programming language
has its own methods for identifying comments.

Case 2: Another difference between programming languages is


related to their facilities for grouping together terms, expressions, or
statements. Example: begin-end, { }, ..
9
A general-purpose macro processor may need to take these groupings
into account in scanning the source statements.

58

Case 3: Languages differ substantially in their restrictions on the


length of identifiers and the rules for the formation of constants
(i.e. the tokens of the programming language for example,
identifiers, constants, operators, and keywords).
Example: problem with multiple-character operators in
FORTRAN like **, :=
Case 4: Another potential problem with general-purpose macro
processors involves the syntax used for macro definitions and
macro invocation statements.
9 With most special-purpose macro processors, macro
invocations are very similar in form to statements in the source
programming language.

59

Advantages
9 Programmer does not need to learn about a different macro facility
for each compiler or assembler language
9 Training time and cost is reduced

9Costs involved in producing a GP-MP is greater than languagespecific processor, however, this expense does not need to be
repeated for each language
9 Substantial overall saving in software development cost as well as
maintenance effort

60

Macro processor within Language Translator


The macro processors might be called preprocessors. Consider an
alternative: combining the macro processing functions with the
language translator itself.
9 The simplest method of achieving this sort of combination is a line-by-line
macro processor. Using this approach, the macro processor reads the source
program statements and performs all of its functions as previously described.
9 The output lines are then passed to the language translator as they are
generated (one at a time), instead of being written to an expanded source file.

Thus, the macro processor operates as a sort of input routine for


the assembler or compiler.

61

Although a line-by-line macro processor may use some of the


same utility routines as the language translator, the functions of
macro processing and program translation are still relatively
independent.
There exists even closer cooperation between the macro
processor and the assembler or compiler. Such a scheme can be
thought of as a language translator with an integrated macro
processor.

62

An integrated macro processor can potentially make use of any


information about the source program that is extracted by the
language translator.
9 For example, at a relatively simple level of cooperation, the macro
processor may use the results of such translator operations as scanning for
symbols, constants, etc.
9The macro processor can simply use the results without being involved in
such details as multiple-character operators, continuation lines, and the rules
for token formation.

63

Disadvantages
There are disadvantages to integrated and line-by-line macro
processors:
They must be specially designed and written to work with a
particular implementation of an assembler or compiler.
The costs of macro processor development must be added to the
cost of the language translator, resulting in a more expensive piece
of software.
The size may be a problem if the translator is to run on a
computer with limited memory.
64

You might also like