Macro
Macro
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.
MACRO
COMPUTE &FIRST, &SECOND
MOVEM BREG, TMP
INCR_D &FIRST, &SECOND, REG=BREG
MOVER BREG, TMP
MEND
COMPUTE X,Y:
+ BREG, TMP [1]
MOVEM X,Y
+ INCR_D BREG,X [2]
+ MOVER BREG,Y [3]
+ MOVEM
ADD BREG,X [4]
+ MOVER BREG,TMP [5]
ADVANCED MACRO FACILITIES
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
AIF EQ 1) .NEXT
(L‟&A
&A
----
.NEXT----
---
CONDITIONAL EXPANSION
While writing a general purpose macro it is
important to ensure execution efficiency of
its generated code.
This is achieved by 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 ANOP
OVER ANOP
MACRO
EVAL &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &X
SUB AREG, &Y
ADD AREG, &Z
AGO .OVER
.ONLY MOVER AREG, &Z
.OVER MEND
It is required to develop a macro EVAL such that a call
EVAL A,B,C generates efficient code to evaluate A-B+C in
AREG.
When the first two parameters of a call are identical, EVAL
should generate single MOVER instruction to load 3rd
parameter into AREG.
As formal parameter is corresponding to actual parameter,
AIF statement effectively compares names of first two actual
parameters.
If condition is true, expansion time control is transferred to
model statement MOVER AREG, &Z.
If false, MOVE-SUB-ADD sequence is generated and
expansion time control is transferred to statement
.OVER MEND which terminates expansion.
Thus, efficient code is generated under all conditions.
EXPANSION TIME LOOPS
It is often necessary to generate many similar statements
during the expansion of a macro.
This can be achieved by writing similar model statements in the
macro:
Example
MACRO
CLEAR &A
MOVER AREG, =‟0‟
MOVEM AREG, &A
MOVEM AREG, &A+1
MOVEM AREG, &A+2
MEND
When called as CLEAR B, The MOVER statement
puts the value „0‟ in AREG, while the
three MOVEM statements store this value in 3 consecutive bytes
with the addresses B, B+1 and B+2.
Alternatively, the same effect can be achieved by writing an
expansion time loop which visits a model statement, or a set of
model statement repeatedly during macro expansion.
Expansion time loops can be written using expansion time
variables (EV‟s) and expansion time control transfer statements AIF
and AGO.
Consider expansion of the macro call
CLEAR B, 3
Example
MACRO
CLEAR &X, &N
LCL &M
&M SET 0
MOVER AREG, =‟0‟
.MORE MOVEM AREG, &X + &M
&M SET &M+1
AIF (&M NE &N)
.MORE
MEND
OTHER FACILITIES FOR EXPANSION TIME
LOOPS
The assembler for M 68000 and Intel 8088 processors provide explicit
expansion time looping constructs.
<expression> should evaluate to a numerical value during macro
expansion.
The REPT statement
REPT <expression>
Statements between REPT and an ENDM statement would be
processed for expansion <expression> number of times.
Following example use REPT to declare 10 constant with the value 1,2,…
10.
MACRO
CONST10
LCL &M
&M SET 1
REPT 10
DC „&M‟
&M SET &M+1
ENDM
MEND
THE IRP STATEMENT
IRP <formal parameter> <argument list>
The formal parameter mentioned in the statement takes
successive values from the argument list.
For each value, the statements between the IRP and
ENDM statements are expanded once.
MACRO
CONSTS &M, &N, &Z
IRP &Z, &M, 7, &N
DC „&Z‟
ENDM
MEND
A macro call CONSTS 4, 10 leads to declaration of 3
constants with the value 4, 7 and 10.
SEMANTIC EXPANSION
Semantic expansion is the generation of instructions
to the requirements of a specific usage.
It can be achieved by a combination of
advanced macro facilitieslike AIF,
AGO statements and expansion time variables.
The CLEAR example is an instance of semantic
expansion. In this example the number of
MOVEM AREG,….. statement generated by a call on
CLEAR is determined by the value of the second parameter
of CLEAR.
Following example is another instance of conditional
expansion wherein one of two alternative code
sequences is generated depending on actual parameters of a
macro call.
EXAMPLE
This macro creates a constant „25‟ with the name
given by the 2nd parameter.
The type of the constant matches the type of the first
parameter.
MACRO
CREATE_CONST &X, &Y
AIF (T‟&X EQ
B) .BYTE
&Y DW 25
AGO .OVER
.BYTE ANOP
&Y DB 25
.OVER MEND
DESIGN OF A MACRO PREPROCESSOR
The macro preprocessor accepts an assembly program
containing definitions and calls and translates it into an
assembly program which does not contain any macro
definitions and calls.
The program form output by the macro
preprocessor can be handed over to an assembler to
obtain the target program.
Programs Programs
with macro Macro Without Target
Macros Assembler
definitions Preprocessor Program
and calls
DESIGN 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 at a
design specification for each task.
Identify the 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. IDENTIFY MACRO 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 that the current statement is a macro
call.
2. DETERMINE 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. MAINTAIN EXPANSION TIME VARIABLES
An Expansion time Variable Table (EVT) is
maintained for this purpose.
The table contains pairs of the form
(<EV name>, <value>).
The value field of a pair is accessed when
a preprocessor statement or a model statement
under expansion refers to an EV.
4. ORGANIZE EXPANSION TIME CONTROL
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.
It is updated after expanding a model statement
of on processing a macro preprocessor statement.
5. DETERMINE VALUES OF SEQUENCING
SYMBOLS
A Sequencing Symbols Table (SST) is
maintained to hold this information.
The table contains pairs of the form (<sequencing
symbol name>, <MDT entry #>)
Where <MDT entry #> is the number of the
MDT entry which contains the model statement
defining the sequencing symbol.
This entry is made on encountering a statement which
contains the sequencing symbol in its label field (for
back reference to symbol) or on encountering a
reference prior to its definition(forward reference).
6. PERFORM EXPANSION OF A MODEL
STATEMENT
This is trivial task given the following:
1. MEC points to the MDT entry containing the
model statement.
2. Values of formal parameters and EV‟s are
available in APT and EVT, respectively.
3. The model statement defining a sequencing symbol can be
identified from SST.
Expansion of a model statement is achieved
by performing a lexical substitution for the
parameters and EV‟s used in the 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.
For example the formal parameter name is used as the key to
obtain its value from APT.
This search can be eliminated if the position of an entity
within the table is known when its value is to be accessed.
In context of APT, the value of a formal parameter ABC is
needed while expanding a model statement using it.
MOVER AREG, &ABC
Let thepair (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 forthe words
“parameter #5”.
Thus macro expansion can be made more efficient by storing an
intermediate code for a statement in the MDT.
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.
Parameter names are entered in PNTAB in the same
order in which they appear in the prototype
statement.
Its entry number is used to replace the parameter
name in the model and preprocessor statements of the
macro while storing it in the MDT.
This implements the requirement that the
statement MOVER AREG, &ABC should appear
as MOVER
AREG, (P,5) in MDT.
In effect, the information (<formal parameter
name>,<value>) in APT has been split into two table
PNTAB which contains formal parameter names.
APTAB which contains formal parameter values.
PNTAB is used while processing a macro definition
while APTAB is used during macro expansion.
Similar Analysis leads to splitting
EVT into EVNTAB and EVTAB.
SST into SSNTAB and SSTAB.
EV names are entered into EVNTAB while processing EV
declaration statements.
SS names are entered in SSNTAB while processing an SS
reference or definition, whichever occurs earlier.
Entries only need to exist for default parameter,
therefore wereplace the parameter default table (PDT) by
a keyword parameter default table (KPDTAB).
We store the number of positional parameters of macro in a new
field of the MNT entry.
MNT has entries for all macros defined in a program.
Each MNT entry contains three pointers MDTP, KPDTP and
SSTP, which are pointers to MDT, KPDTAB and SSNTAB.
Instead of creating different MDT‟s for different macros, we can
create a single MDT and use different sections of this table for
different macros.
TABLES ARE CONSTRUCTED FOR MACRO
PREPROCESSOR.
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…..) TABLES 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 #
CONSTRUCTION AND USE OF THE MACRO PREPROCESSOR
DATA STRUCTURES CAN BE SUMMARIZED AS FOLLOWS.
SSTAB_ptr:=1;
MDT_ptr:=1;
No. Statement
1. TOS := RB-1;
2. RB := RB*;
Pg 158 to pg 160.