0% found this document useful (0 votes)
612 views

Macro Processor

This document discusses macro processors and macro expansion. Some key points: - A macro processor replaces macro invocations with the corresponding sequence of statements as defined by the macro definition. - Macro definitions are stored in a definition table and macro names are indexed in a name table. An argument table stores arguments during macro expansion. - When a macro is invoked, the macro processor looks up the definition, substitutes parameters, and generates the expanded code in place of the macro call. - Nested macro definitions and recursive macro expansion require more complex macro processor designs, such as using a stack or recursion.

Uploaded by

sumathi venisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
612 views

Macro Processor

This document discusses macro processors and macro expansion. Some key points: - A macro processor replaces macro invocations with the corresponding sequence of statements as defined by the macro definition. - Macro definitions are stored in a definition table and macro names are indexed in a name table. An argument table stores arguments during macro expansion. - When a macro is invoked, the macro processor looks up the definition, substitutes parameters, and generates the expanded code in place of the macro call. - Nested macro definitions and recursive macro expansion require more complex macro processor designs, such as using a stack or recursion.

Uploaded by

sumathi venisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 55

Macro Processors

1
Introduction

 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)

2
Macro

A macro represents a commonly used group of


statements in the source programming language
The macro processor replaces each macro instruction
with the corresponding group of source language
statement, this is called expanding macros
The functions of a macro processor essentially involve
the substitution of one group of characters or lines for
another

3
Macro Definition and Expansion
The MACRO statement identifies the beginning of a macro
definition
The symbol in the label field is the name of the instruction
The entries in the operand field identify the parameter of the
macro instruction
Each parameter begins with the character &
The MEND assembler directive marks the end of the macro
definition
A macro invocation statement gives the name of the macro
instruction being invoked and the arguments to be used in
expanding the macro

4
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

5
6
7
8
9
10
11
Macro Processor Data Structures
The macro definitions themselves are stored in definition table
(DEFTAB), which contains the macro prototype and the statements
that make up the macro body
The macro names are entered into NAMTAB, which serves as an
index to DEFTAB
For each macro instruction defined NAMTAB contains pointers to
the beginning and end of the definition in DEFTAB
The third data structure is an argument table (ARGTAB), which is
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

12
Macro Definition

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

13
Example

Source Expanded source


STRG MACRO .
STA DATA1 .
STB DATA2 .

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

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

14
Macro vs. Subroutine

 Macro
»the statement of expansion are generated each time the
macro are invoked
 Subroutine
»the statement in a subroutine appears only once

15
Parameter Substitution -- Example

Source Expanded souce


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

{
STX &a3 STA DATA1
MEND STB DATA2
. STX DATA3
STRG DATA1, DATA2, DATA3 .

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

16
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

17
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

18
Nested Macros Definition
 Macro definition within macros
»process macro definition during expansion time

19
20
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
ARGTAB
EXPAND

Expanding MACRO Definition

21
22
23
24
1-Pass Macro Processor
D E F IN E
M AC RO
PR O C ESSO R

G E T L IN E
E X P A N D IN G = F A L S E

P R O C E S S L IN E

G E T L IN E EXPAND
P R O C E S S L IN E

E X P A N D IN G = T R U E

G E T L IN E
G E T L IN E
P R O C E S S L IN E

E X P A N D IN G FA LSE

TRUE

READ FR O M READ FRO M


DEFTAB IN P U T
25
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

26
Concatenation of Macro Parameters

 Pre-concatenation
»LDA X&ID1
 Post-concatenation
»LDA X&ID1
 Example: Figure 4.6

27
Generation of Unique Labels

 Example
»JEQ *-3
»inconvenient, error-prone, difficult to read
 Example Figure 4.7
–$LOOP TD =X’&INDEV’
»1st call:
–$AALOOP TD =X’F1’
»2nd call:
–$ABLOOP TD =X’F1’

28
RDBUFF F1, BUFFER, LENGTH
Conditional Macro Expansion

 Macro-time conditional statements


»IF-ELSE-ENDIF

 Macro-time variables
»any symbol that begins with the character & and that is not a
macro parameter
»macro-time variables are initialized to 0
»macro-time variables can be changed with their values using
SET
–&EORCK SET 1

31
RDBUFF F3, BUF, RECL, 04, 2048

RDBUFF 0E, BUFFER, LENGTH, , 80


RDBUFF F1, BUFF, RLENG, 04
Conditional Macro Expansion (Cont.)

 Macro-time looping statement


»Example: Figure 4.9
»WHILE-ENDW
 Macro processor function
»%NITEMS: THE NUMBER OF MEMBERS IN AN
ARGUMENT LIST

35
Nested Macro Invocations

 Macro invocations within macros


»process macro invocation during expansion time
 Recursive macro expansion
»Example: Figure 4.11
»Problems:
–ARGTAB
–EXPANDING
»Solution
–Recursive call
–While loop with stack

36
37
38
39
40
4.3 Macro Processor Design Options
4.3.1 Recursive Macro Expansion
 In Fig. 4.3 we presented an example of the definition of one
macro instruction by another.
 Fig. 4.11(a) shows an example - Dealt with the invocation of
one macro by another.
 The purpose of RDCHAR Fig. 4.11(b) is to read one
character from a specified device into register A, taking care
of the necessary test-and-wait loop.

41
42
43
4.3.1 Recursive Macro Expansion

 Fig. 4.11(c), applied to the macro invocation statement


RDBUFF BUFFER, LENGTH, F1
 The procedure EXPAND would be called when the macro
was recognized.
 The arguments from the macro invocation would be
entered into ARGTAB as follows:

44
4.3.1 Recursive Macro Expansion

 The Boolean variable EXPANDING would be set to TRUE,


and expansion of the macro invocation statement would be
begin.
 The processing would proceed normally until line 50, which
contains a statement invoking RDCHAR. At that point,
PROCESSLINE would call EXPAND again.
 This time, ARGTAB would look like

45
4.3.1 Recursive Macro Expansion

 At the end of this expansion, however, a problem would


appear. When the end of the definition of RDCHAR was
recognized, EXPANDING would be set to FALSE.
 Thus the macro processor would “forget” that it had
been in middle of expanding a macro when it
encountered the RDCHAR statement.
 Use a Stack to save ARGTAB.
 Use a counter to identify the expansion.

46
Procedure EXPAND:
level=0;SP=-1
begin
set S(SP+N+2)=SP
Set SP=SP+N+2
Set S(SP+1)=DEFTAB index from NAMTAB
SET up macro call argument list array in S(SP+2), S(SP+3) ......S(SP+N+1)
where N is the number of arguments
while not end of macro definition and level!=0 do
begin
GETLINE
PROCESSLINE
end(While)
set N=SP-S(SP)-2
Set SP=S(SP)
end{EXPAND}

47
Procedure GETLINE
begin if SP!=-1 then
begin
increment DEFTAB pointer to next entry
set S(SP+1)=S(SP+1)+1
get the line from DEFTAB with the pointer S(SP+1)
Substitute the arguments from macro call S(SP+2),
S(SP+3) ......S(SP+N+1)

48
ARGTAB

DEFTAB MACRO Definition


DEFINE
NAMTAB

GETLINE
PROCESSLINE

Macro Invocation
EXPAND
ARGTAB

49
1-Pass Macro Processor
D E F IN E
M AC RO
PR O C ESSO R

G E T L IN E
E X P A N D IN G = F A L S E

P R O C E S S L IN E

G E T L IN E EXPAND
P R O C E S S L IN E

E X P A N D IN G = T R U E

G E T L IN E
G E T L IN E
P R O C E S S L IN E

E X P A N D IN G FA LSE

TRUE

READ FR O M READ FRO M


DEFTAB IN P U T
50
Allowing Nested Macro Invocation
D E F IN E (f)
M AC R O
PR O C ESSO R

G E T L IN E (f)

P R O C E S S L IN E (f)
G E T L IN E (0 )
P R O C E S S L IN E (0 ) EXPAND

G E T L IN E (1 )
G E T L IN E (f)
P R O C E S S L IN E (1 )

f FA LSE

TRUE

READ FR O M READ FRO M


DEFTAB IN P U T
51
Macro-Assembler

 Advantage
»reduce 1 pass
»share same data structure
 Disadvantage
»more complex

52
Pass 1 READ

Search Pass 2
R Type?
(Pseudo-Op Table)

Search NAMTAB MACRO Process


(Macro Name Table) Define pseudo-ops

Search R R
(Machine Op Table)

MACRO
Process Expand
machine
instruction
R
R
General Purpose Macro Processor

 ELENA
»Software: Practice and Experience, Vol. 14, pp. 519-531, Jun.
1984
 Macro definition
»header:
–a sequence of keywords and parameter markers (%)
–at least one of the first two tokens in a macro header must be a
keyword, not a parameter marker
»body:
–the character & identifies a local label
–macro time instruction (.SET, .IF .JUMP, .E)
–macro time variables or labels (.)

54
ELENA (cont.)

 Macro invocation
»There is no single token that constitutes the macro “name”
»Constructing an index of all macro headers according to the
keywords in the first two tokens of the header
»Example
–DEFINITION:
ADD %1 TO %2
ADD %1 TO THE FIRST ELEMENT OF %2

–INVOCATION: DISPLAY %1
DISPLAY TABLE
%1 TABLE

55

You might also like