You are on page 1of 51

SPCC Chapter 3.

Macro Processors

Prof. Anil Hingmire


VCET, Computer Engg. Dept.
Learning Objectives

1. Introduction to Macros
2. Features of Macros
3. What is Macro processor
4. Design of Single Pass Macro processor
4.1 Handling Nested Macros
4.2 Handling Conditional Macros
5. Design of Two-Pass Macro Processor

2
1. Introduction to Macro
 Concept:
» A macro instruction 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 invocation with
the corresponding sequence of statements (expanding)
» Macro:- A macro is a unit of specification for program
generation through expansion.
» A macro consists of a name, a set of formal parameters and
a body of code.
» The use of macro name with a set of actual parameters is
replaced by code generated from its body.

3
1.Introduction to Macro

Macro definition & Call:-


A macro definition is enclosed between the name of a macro
header and a macro end statement.
It consists of :-
1) A macro prototype statement
2) One or more model statement
3) Macro processor statement.
Macro prototype statement
Syntax:
<macro name> [<formal parameters>]

4
1. Introduction to Macro
Macro Call:-
A macro is called by writing the macro name in the mnemonic field of an
assembly statement.
Syntax:-
<macro name> [<actual parameter list>]
Example:-
MACRO
TEST &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &Y
AGO .LAST
.ONLY MOVER AREG, &Z
.LAST MEND

Macro Call:-
TEST S,10
5
2. Features of Macros

 Macro with Formal Parameters

 Conditional Macros

 Macro call within macro

 Nested Macros

6
3. Macro Processor

 Recognize macro definitions


 Save the macro definition
 Recognize macro calls
 Expand macro calls

Source
Macro Expanded Compiler or obj
Code
(with macro)
Processor Code Assembler

7
Macro vs. Subroutine (Function)

 Macro
» the statement of expansion are generated each time the
macro are invoked
» In macro call, entire macro code is substituted for macro call
 Subroutine
» the statement in a subroutine appears only once
» In Function call, control is transferred where it is defined.

8
Macro Definition

 copy code
 parameter substitution
 conditional macro expansion
 macro instruction defining macros

9
Copy code -- Example

Source Expanded source


STRG MACRO .
STA DATA1 .
STB DATA2 .
STX DATA3 STA DATA1

.
STRG
MEND
{
.
STB
STX
DATA2
DATA3

. STA DATA1
STRG
.
.
{
.
STB
STX
DATA2
DATA3

10
Parameter Substitution -- Example

Source Expanded souce


STRG MACRO &a1, &a2, &a3 .
STA &a1 .
STB &a2 .
STX &a3 STA DATA1

.
MEND

STRG DATA1, DATA2, DATA3


{
.
STB
STX
DATA2
DATA3

. STA DATA4
STRG DATA4, DATA5, DATA6
.
.
{
.
STB
STX
DATA5
DATA6

11
Parameter Substitution

 Dummy arguments
» Positional argument
STRG DATA1, DATA2, DATA3
GENER ,,DIRECT,,,,,,3

» Keyword argument
STRG &a3=DATA1, &a2=DATA2, &a1=DATA3
GENER TYPE=DIRECT, CHANNEL=3

12
4. One-Pass Macro Processor

 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

13
Nested Macros Definition

 Macro definition within macros


» process macro definition during expansion time
 Example 4.3

14
Nested Macros Definition

15
NO LABEL IN MACRO BODY
 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:
◦ Do not use labels in the body of macro.
◦ Explicitly use PC-relative addressing instead.
 Ex, in RDBUFF and WRBUFF macros,
DESIGN
OF

ONE-PASS MACRO PROCESSOR


One-Pass Macro Processor That Allows
Nested Macro Definition

 Sub-procedures
» macro definition: DEFINE
» macro invocation: EXPAND
 EXPAND may invoke DEFINE when encounter
macro definition
NAMTAB
MACRO DEFINE
DEFTAB PROCESSLINE
CALL EXPAND
ARGTAB

Expanding MACRO Definition

18
ONE-PASS MACRO PROCESSOR
 A one-pass macro processor that alternate
between macro definition and macro expansion
in a recursive way is able to handle recursive
macro definition.
 Restriction
◦ The definition of a macro must appear in the
◦ source program before any statements that
◦ invoke that macro.
◦ This restriction does not create any real
◦ inconvenience.
DATA STRUCTURE FOR ONE-PASS MACRO
PROCESSOR

 DEFTAB (definition table)


◦ Stores the macro definition including macro
◦ prototype and macro body
◦ Comment lines are omitted.
◦ References to the macro instruction parameters are
◦ converted to a positional notation for efficiency in
◦ substituting arguments.
DATA STRUCTURE FOR ONE-PASS MACRO
PROCESSOR
 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 STRUCTURE
ALGORITHM
ALGORITHM
ALGORITHM
ALGORITHM
1-Pass Macro Processor
DEFINE
MACRO
PROCESSOR

GETLINE
EXPANDING=FALSE

PROCESSLINE

GETLINE EXPAND
PROCESSLINE

EXPANDING=TRUE

GETLINE
GETLINE
PROCESSLINE

EXPANDING FALSE

TRUE

READ FROM READ FROM


DEFTAB INPUT
27
HANDLING NESTED MACRO
 In DEFINE procedure
◦ When a macro definition is being entered into
◦ DEFTAB, the normal approach is to continue
◦ until an MEND directive is reached.
◦ This would not work for nested macro definition
because the first MEND encountered in the inner
macro will terminate the whole macro definition
process.
HANDLING NESTED MACRO
 To solve this problem, a counter LEVEL is used to
keep track of the level of macro definitions.
◦ Increase LEVEL by 1 each time a MACRO
directive is read.
◦ Decrease LEVEL by 1 each time a MEND
directive is read.
◦ A MEND terminates the whole macro definition
process when LEVEL reaches 0.
◦ This process is very much like matching left and
right parentheses when scanning an arithmetic
expression.
IMPLEMENTATION-CONDITIONAL
MACRO EXPANSION
 IF-ELSE-ENDIF structure
◦ The macro processor must maintain a symbol table
 -This table contains the values of all macro-time
variables used.

 - Entries in this table are made or modified when SET


statements are processed.

 - This table is used to look up the current value of a


macro-time variable whenever it is required.
IMPLEMENTATION-CONDITIONAL
MACRO EXPANSION
 When an IF statement is encountered during the expansion of a
macro, the specified Boolean expression is evaluated.

 TRUE
◦The macro processor continues to process lines from DEFTAB
until it encounters the next ELSE or ENDIF statement.
◦If ELSE is encountered, then skips to END

 FALSE
◦The macro processor skips ahead in DEFTAB until it finds the
next ELSE or ENDIF statement.
IMPLEMENTATION-CONDITIONAL
MACRO EXPANSION
 When an IF statement is encountered during the expansion of a
macro, the specified Boolean expression is evaluated.
 -TRUE

The macro processor continues to process lines from DEFTAB


until it encounters the next ENDW statement.

When ENDW is encountered, the macro processor returns to the


preceding WHILE, re-evaluates the Boolean expression, and
takes action based on the new value.
IMPLEMENTATION-CONDITIONAL
MACRO EXPANSION

 FALSE
◦ -- The macro processor skips ahead in DEFTAB
until it finds the next ENDW statement and then
resumes normal macro expansion.
KEYWORD MACRO PARAMETERS
 Keyword macro parameters
◦ Positional parameters and arguments are associated
according to their positions in the macro prototype
and invocation.
◦ If an argument is to be omitted, a null argument
should be used to maintain the proper order in macro
invocation:
◦ Ex: XXX MACRO &P1, &P2, …., &P20, ….
XXX A1, A2,,,,,,,,,,…,,A20,…..
KEYWORD MACRO PARAMETERS
 Keyword Parameters
◦ Each argument is written with the keyword that names
the corresponding parameter.

◦ Argument may appear any order.


◦ Null arguments are no longer required to be used.

◦ It is easier, less error prone and readable form to have


keyword parameter than positional parameter.
 Ex P1=A1, P2=A2, … P20=A20
COMPARISON OF MACRO
PROCESSOR DESIGN
 One-pass algorithm
 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 but nested calls
are not
COMPARISON OF MACRO PROCESSOR
DESIGN

 Two-pass algorithm
◦ Pass1: Recognize macro definitions
◦ Pass2: Recognize macro calls
◦ Nested macro definitions are not allowed
Comparison of Macro Processors Design

 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

38
Macro Processor Design Options
 Recursive Macro expression
 General-Purpose Macro Processors
 Macro Processing within Language Translators
Recursive Macro Expansion
 Macro within macro can be solved if the macro
processor is being written in a programming language
that allows recursive calls

 The compiler would be sure that previous value of any


variables declared within a procedure were saved when
that procedure was called recursively

 If would take care of other details involving return from


the procedure
General-Purpose Macro Processors(2/1)

 Advantages of general-purpose macro processors:


 The programmer does not need to learn about a different macro
facility for each compiler or assembler language—the time and
expense involved in training are eliminated

 The costs involved in producing a general-purpose macro


processor are somewhat greater than those for developing a
language-specific processor

 However, this expense does not need to be repeated for each


language; the result is substantial overall saving in software
development cost
General-Purpose Macro
Processors(2/2)
 A general-purpose facility must provide some way for a user to
define the specific set of rules to be followed

 Comments should usually be ignored by a macro processor,


however, each programming language has its own methods for
identifying comments

 Each programming language has different facilities for grouping


terms, expressions, or statements—a general-purpose macro
processor needs to taking these grouping into account

 Languages differ substantially in their restrictions on the length of


identifiers and the rules for the formation of constants

 Programming languages have different basic statement forms—


syntax used for macro definitions and macro invocation statements
Macro Processing within Language
Translators(2/1)
 The macro processor reads the source statements and performs all
of its functions, the output lines are passed to the language
translator as they are generated

 The macro processor operates as a sort of input routine for the


assembler or compiler

 The line-by-line approach avoids making an extra pass over the


source program, so it can be more efficient than using a macro
preprocessor

 Some of the data structures required by the macro processor and


the language translator can be combined
 A line-by-line macro processor also makes it easier to give
diagnostic messages that are related to the source statement
containing the error
Macro Processing within Language
Translators(2/2)
 An integrated macro processor can potentially make use of any
information about the source program that is extracted by the
language translator

 An integrated macro processor can support macro instructions that


depend upon the context in which they occur

 Line-by-line macro processors must be specially designed and


written to work with a particular implementation of an assembler or
compiler, which results in a more expensive piece of software

 The assembler or compiler will be considerably larger and more


complex than it would be

 The additional complexity will add to the overhead of language


translation
MASM Macro Processor(2/1)
 The macro processor of MASM is integrated with Pass 1 of the
assembler

 MASM generates the unique names of local labels in the form ??n,
where n is a hexadecimal number in the range 0000 to FFFF

 .ERR: signals to MASM that an error has been detected

 EXITM: directs MASM to terminate the expansion of the macro


 &: is a concatenation operator
MASM Macro Processor(2/2)
 ;; is a macro comment, serves only as
documentation for the macro definition
 ; is an ordinary assembler language comment,
included as part of the macro expansion
 IRP: sets the macro-time variable to a sequence
of values specified in <…>
 The statements between the TRP and the
matching ENDM are generated once for each
value of the variable
DESIGN OF
TWO-PASS MACRO PROCESSOR
TWO-PASS MACRO PROCESSOR

 You may design a two-pass macro processor


◦ Pass 1:
 Process all macro definitions
◦ Pass 2:
 Expand all macro invocation statements
Pass 1
Pass 1 – processing macro definitions

MDTC<-1
MNTC<-1

Read next
source card

MACRO No
Write copy of
pseudo-
source card
op?
Yes
END
No
pseudo-
Read next
op?
source card
Yes

Enter macro name and Go to Pass 2


current value of MDTC Read next
in MNT entry number source card
MNTC
Substitute
index
MNTC <- MNTC + 1 notation for
arguments

Prepare argument list array


Enter line into MDT

Enter macro name card


into MDT MDTC <- MDTC + 1

MDTC <- MDTC + 1


MEND
pseudo-
Yes op? No
Pass 2
Pass 2 – processing macro calls and expansion

Read next
source card
(copied by
pass 1)

Search MNT for match


with operation code

MACRO No Write into


name expanded
found? source card
file
Yes
END
No
pseudo-
MDTP <- MDT index
op?
from MNT entry
Yes
Supply expanded
Set up argument list
source file to
array
assembler
processing
MDTP <- MDTP + 1

Get line from MDT

Substitute arguments
from macro call

MEND Write
pseudo- expanded
Yes op? source card
No
Thank You

You might also like