MacroPPT Module3
MacroPPT Module3
MacroPPT Module3
I NTRODUCTION
Macros are used to provide a program generation
facility through macro expansion.
Many languages provide build-in facilities for
writing macros like PL/I, C, Ada AND C++.
Assembly languages also provide such facilities.
Lexical expansion:
⚫ Lexical expansion implies replacement of a character
string by another character string during program
generation.
⚫ Lexical expansion is to replace occurrences of formal
parameters by corresponding actual parameters.
Semantic expansion:
⚫ Semantic expansion implies generation of instructions
tailored to the requirements of a specific usage.
⚫ Semantic expansion is characterized by the fact that
different uses of a macro can lead to codes which differ in
the number, sequence and opcodes of instructions.
⚫ Eg: Generation of type specific instructions for
manipulation of byte and word operands.
E XAMPLE
The following sequence of instructions is used to
increment the value in a memory word by a constant.
⚫ 1. Move the value from the memory word into a machine-
register.
⚫ 2. Increment the value in the machine register.
⚫ 3. Move the new value into the memory word.
Since the instruction sequence MOVE-ADD-MOVE
may be used a number of times in a program, it is
convenient to define a macro named INCR.
Using Lexical expansion the macro call INCR
A,B,AREG can lead to the generation of a MOVE-
ADD-MOVE instruction sequence to increment A by
the value of B using AREG to perform the arithmetic.
Use of Semantic expansion can enable the instruction
sequence to be adapted to the types of A and B.
For example a n INC instruction could be generated if
A is a byte operand and B has the value „1‟.
H OWDOES MACRO DIFFER FROM
SUBROUTINE ?
Macros differ from subroutines in one
fundamental respect.
Use of a macro name in the mnemonic field of
a n assembly statement leads to its expansion,
whereas use of subroutine name in a call
instruction leads to its execution.
So there is difference in
⚫Size
⚫Execution Efficiency
Macros can be said to trade program size for
execution efficiency.
More difference would be discussed a t the time of
discussion of macro expansion.
MACRO DEFINITION AND CALL
MACRO DEFINITION
A macro definition is enclosed between a macro header statement and
a macro end statement.
Macro definitions are typically located a t the start of a program.
A macro definition consists of.
⚫ A macro prototype statement
⚫ One or more model statements
⚫ Macro preprocessor statements
The macro prototype statement declares the name of a macro
and the names and kinds of its parameters.
It has the following syntax
<macro name> [< formal parameter spec > [,..]]
Where <macro name> appears in the mnemonic field of an
assembly statement and
< formal parameter spec> is of the form
&<parameter name> [<parameter kind>]
MACRO
COMPUTE &FIRST, &SECOND
MOVEM BREG, TMP
INCR_D &FIRST, &SECOND, REG=BREG
MOVER BREG, TMP
MEND
COMPUTE X,Y:
+ MOVEM BREG, TMP [1]
+ INCR_D X,Y
+ MOVER BREG,X [2]
+ ADD BREG,Y [3]
+ MOVEM BREG,X [4]
+ MOVER BREG,TMP [5]
A DVANCED M ACRO F ACILITIES
Advanced macro facilities are aimed to
supporting semantic expansion.
Used for:
⚫ performing conditional expansion of model
statements and
⚫ in writing expansion time loops.
MACRO
DCL_CONST &A
AIF (L‟&A EQ 1) .NEXT
----
.NEXT----
---
MEND
C ONDITIONAL E XPANSION
While writing a general purpose macro it is
important to ensure execution efficiency of its
generated code.
This is achieved by ensuring t hat a model
statement is visited only under specific
conditions during the expansion of a macro.
How to do that?
Programs Programs
with macro Macro Without Target
Macros Assembler
definitions Preprocessor Program
and calls
D ESIGN OVERVIEW
We begin the design by listing all tasks involved in
macro expansion.
⚫ 1. Identify macro calls in the program.
⚫ 2. Determine the values of formal parameters.
⚫ 3. Maintain the values of expansion time variables
declared in a macro.
⚫ 4. Organize expansion time control flow.
⚫ 5. Determine the values of sequencing symbols.
⚫ 6. Perform expansion of a model statement.
Following 4 step procedure is followed to arrive a t a
design specification for each task.
⚫ Identifythe information necessary to perform a task.
⚫ Design a suitable data structure to record the information.
⚫ Determine the processing necessary to obtain the
information.
⚫ Determine the processing necessary to perform the task.
1. I DENTIFY M ACRO CALLS
A table called the Macro Name Table (MNT) is
designed to hold the names of all macros defined
in a program.
A macro name is entered in this table when
macro definition is processed.
While processing a statement in the source
program, the preprocessor compares the string
found in its mnemonic field with the macro
names in MNT.
A match indicate t h a t the current statement is a
macro call.
2. D ETERMINE THE VALUES OF FORMAL
PARAMETERS
A table called the Actual Parameter Table (APT) is
designed to hold the values of formal parameters
during the expansion of a macro call.
Each entry in the table is a pair (<formal parameter
name>, <value>).
Two items of information are needed to construct this
table, names of formal parameters and default values
of keyword parameters.
A table called the Parameter Default Table (PDT) is
used for each macro.
It would contain pairs of the form
(<formal parameter name>, <default value>)
If a macro call statement does not specify a value for
some parameter par, its default value would be copied
from PDT to APT.
3. M AINTAIN EXPANSION TIME VARIABLES
Table Fields
MNT (Macro Name Table) Macro Name
Number of Positional Parameter
(#PP)
Number of keyword parameter
(#KP)
Number of Expansion Time
Variable (#EV)
MDT pointer (MDTP)
KPDTAB pointer (KPDTABP)
SSTAB pointer (SSTP)
(CONTI…..) T ABLES
ARE CONSTRUCTED
FOR MACRO PREPROCESSOR .
Tables Fields
PNTAB (Parameter Name Table) Parameter name
EVNTAB (EV Name Table) EV Name
SSNTAB (SS Name Table) SS Name
KPDTAB (Keyword Parameter Parameter name, default value
Default Table)
MDT (Macro Definition Table) Label, Opcode, Operands Value
APTAB (Actual Parameter Value
Table)
EVTAB (EV Table) Value
SSTAB (SS Table) MDT entry #
C ONSTRUCTION AND USE OF THE MACRO
PREPROCESSOR DATA STRUCTURES CAN BE
SUMMARIZED AS FOLLOWS .
PNTAB and KPDTAB are constructed by processing
the prototype statement.
Entries are added to EVNTAB and SSNTAB as
EV declarations and SS definitions/references are
encountered.
MDT are constructed while processing the model
statements and preprocessor statements in the
macro body.
An entry is added to SSTAB when the definition of a
sequencing symbol is encountered.
APTAB is constructed while processing a macro call.
EVTAB is constructed a t the start of expansion of a
macro.
See Pg.151, Fig 5.8.
P ROCESSING OF M ACRO D EFINITIONS
The following initializations are performed before
initiating the processing of macro definitions in a
program
KPDTAB_pointer:=1;
SSTAB_ptr:=1;
MDT_ptr:=1;
No. Statement
1. TOS := RB-1;
2. RB := RB*;