Macro and Macro Preprocessor
Macro and Macro Preprocessor
INTRODUCTION
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.
When a
language does not support
build-in
facilities for writing macros what is to be
done?
A programmer may achieve an equivalent
MACRO
a name,
a set of formal parameters and
a body of code.
CLASSIFICATION
Lexical expansion:
OF MACROS:
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:
EXAMPLE
1.
Move the value from the memory word into a
machineregister.
2.
Increment the value in the machine register.
3.
Move the new value into the memory word.
HOW
Size
Execution Efficiency
MACRO DEFINITION
MACRO CALL
A macro is called by writing
the macro name
in
the mnemonic field.
Macro call has the following syntax.
EXAMPLE
MEM_VAL,
INCR_VAL and
REG exists
for the
macro.
Since
parameter
kind is
not
specified
MACRO
INCR
MOVER
ADD
MOVEM
MEND
MACRO
EXPANSION
A. EXPANSION
ALGOTIRHM (MACRO
EXPANSION)
a.
b.
3.
B. LEXICAL SUBSTITUTION
Positional Parameter
Keyword Parameter
Default specification of parameters
Macros with mixed parameter lists
POSITIONAL PARAMETERS
EXAMPLE
Consider the
call: A,B,AREG
INCR following rule of
On macro INCR,
positional association, values of formal
parameters
Formal parameter
value
are:
MEM_VAL
A
INCR_VAL
Lexical expansion
of the model statements now
leads to the code
B
+ MOVER AREG,A
REG
+ ADD AREG,B
+ MOVEM AREG,A
AREG
KEYWORD PARAMETER
<parameter
<parameter
EXAMPLE:
Following are macro call statement:
INCR_M MEM_VAL=A, INCR_VAL=B, REG=AREG
-----INCR_M INCR_VAL=B, REG=AREG, MEM_VAL=A
Both are equivalent.
Following is macro definition using
keyword parameter:
MACRO
INCR_M
&MEM_VAL=,
&INCR_VAL=,®=
MOVER
®, &MEM_VAL
ADD
®, &INCR_VAL
MOVEM
®,&MEM_VAL
MEND
DEFAULT
SPECIFICATIONS OF
PARAMETERS
A default
value is a standard assumption
in the absenceof an explicit specificationby
the programmer.
Default
specificationof parametersis useful
insituations where a parameter has
the same value in most calls.
When the desired value is different from
the default value, the desired value can be
specified explicitly in a macro call.
The syntax for formal parameter
specification, as follows:
&<parameter name>
[<parameter kind> [<default
EXAMPLE
MACROS
OTHER
USES OF PARAMETERS
MEND
&LAB
EXAMPLE:
MOVEM
BREG, TMP
+
+
+
+
MOVER
ADD
MOVEM
MOVER
BREG,
BREG,
BREG,
BREG,
&FIRST, &SECOND
BREG, TMP
&FIRST, &SECOND, REG=BREG
BREG, TMP
MACRO
COMPUTE
MOVEM
INCR_D
MOVER
MEND
COMPUTE X,Y:
X
Y
X
TMP
BREG, TMP
X,Y
[1]
MOVEM
INCR_D
+ MOVER BREG,X
[2]
+ ADD
BREG,Y
[3]
+ MOVEM BREG,X
[4]
1. ALTERATION
OF FLOW OF
DURING EXPANSION
CONTROL
AIF,
AGO and
ANOP.
AIF
STATEMENT
AN AGO
STATEMENT
AN
ANOP STATEMENT
EXAMPLE
&A
&A
MACRO
CONSTANTS
LCL
SET
DB
SET
&A
1
&A
&A+
1
&A
DB
MEND
A call on macro CONSTANTS is expanded as
follows.
The local EV A is created.
The first SET state ment assigns the value 1 to it.
The first DB statem ent th u s declares a byte constant 1.
The second SET state ment assigns
the value 2 to A
And
the second DB statement declares a constant 2.
3. ATTRIBUTES
OF FORMAL PARAMETERS
EXAMPLE
CONDITIONAL EXPANSION
While writing
a
general
purpose macroit is important
to
ensure execution efficiency
of
its generated code.
This
is achievedby ensuring that a
model statement is visited only under
specific conditions during the expansion
of a macro.
How to do that?
Ans: The AIF and AGO statements are used
for this purpose.
Let us take example which would clear
our doubts for the same.
EXAMPLE: A-B+C
.ONLY
.OVER
ONLY
ANOP
OVER
ANOP
MACRO
EVAL &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &X
SUB AREG, &Y
ADD
AREG,
&Z AGO
.OVER
MOVER AREG, &Z
MEND
Example
&M
.MORE
&M
CLEAR
LCL SET
MOVER
MACRO
MOVEM
SET
AIF
MEND
B, 3 &N
&X,
&M
0
AREG, = 0
AREG, &X +
&M
&M+1
(&M NE &N)
.MORE
OTHER FACILITIES
LOOPS
FOR
EXPANSION TIME
ENDM
MEND
A macro call CONSTS 4, 10 leads to declaration
of 3 constants with the value 4, 7 and 10.
SEMANTIC EXPANSION
EXAMPLE
This m acro creates a constant 25 with the n ame
given by the 2nd parameter.
The type of the constant matches the type
of the first parameter.
MACRO
(T'&X EQ B) .BYTE
AIF
&Y
DW
25
AGO
.OVER
.BYTE
ANOP
&Y
DB
25
.OVER
MEND
DESIGN
OF A
MACRO PREPROCESSOR
Programs
with
macro
definitions
and calls
Macro
Preprocess
or
Programs
Without
Macros
Assemble
r
Target
Progra
m
DESIGN OVERVIEW
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.
2. DETERMINE
PARAMETERS
3. MAINTAIN
4. ORGANIZE
FLOW
The body of a macro, i.e. the set of
preprocessor statements and model
statements in it, is stored in a table called
the Macro Definition Table (MDT) for use
during macro expansion.
The
flow of control during macro
expansion determines when a model
statement
is to
be visited
for
expansion.
For this purpose MEC (Macro Expansion
Counter) is initialized to the first
statement of the macro body in the MDT.
5. DETERMINE
VALUES OF SEQUENCING
SYMBOLS
6. PERFORM
EXPANSION OF A MODEL
STATEMENT
DATA STRUCTURE
The tables APT, PDT and EVT contain pairs which are
searched using the first component of the pair as a
key.
Let
the pair (ABC, ALPHA) occupy entry#5 in
APT. The search in APT
can
be
avoided
if the model statement appears as MOVER AREG,
(P,5) in the
MDT, where (P,5)
stands
for t he
All
the parameter
names could
be
replace by pairs of the form
(P,n) in model
statements and
preprocessor statements stored in MDT.
The
information (P,5) appearing in a
model statement is sufficient
to access
the
value of formal parameter
ABC.
Hence APT containing (<formal parameter
name> , <value>)
is replace by
another table called APTAB which only
contains <value>s.
To implement this, ordinal numbers are
assigned to all parameters of a macro.
A table named
Parameter
Name
Table (PNTAB) is used
for
this
purpose. PNTAB is used while processing
the definition of a macro.
TABLES
Macro Name,
Number of Positional Parameter (#PP),
Number of keyword parameter (#KP),
Number of Expansion Time
Variables (#EV),
MDT pointer (MDTP),
KPDTAB pointer (KPDTABP),
SSTAB pointer (SSTP)
Parameter name
Expansion time Variable Name
CONSTRUCTION
PROCESSING
OF
MACRO DEFINITIONS
ALGORITHM (PROCESSING
DEFINITION)
1. SSNTAB_ptr:=
1;
PNTAB_ptr:=1;
2. Process the
macro
prototype
statement and
form the MNT
entry.
a. Name:=mac
ro name;
b. For each
positional
paramete
r
i. Enter
para
meter
name
in
PNTA
B[PN
OF A MACRO
4. (MEND statement)
If SSNTAB_ptr=1 (i.e. SSNTAB is
empty) then
SSTP:=0;
Else
SSTAB_ptr:=SSTAB_ptr+SSNTAB_
ptr-1;
If #KP=0 then KPDTP=0;
MACRO EXPANSION
CONSIDER
THE SITUATION
REQUIRED PROVISION
1.
DATA STRUCTURE
Previous
Expansion
Record
RB
Reserved
Pointer
1(RB) MEC
2(RB) EVTAB_ptr
3(RB) APTAB
TOS->EVTAB
FOR
NESTED MACRO
Data Structure
Reserved
Pointer
MEC
EVTAB_ptr
APTAB
EVTAB
Address
0(RB)
1(RB)
2(RB)
3(RB) to entry
of APTAB +
2(RB)
Contents
of
EVTAB_ptr
ACTIONS
Statement
2. TOS* := RB;
3. RB := TOS;
4. 1(RB) := MDTP entry of MNT;
5. 2(RB) := RB + 3 + #e of APTAB;
6.
No.
1. TOS := RB-1;
Statement
2. RB := RB*;
OF
MACRO