Professional Documents
Culture Documents
Introduction
z
Concept
A macro instruction is a notational convenience for the
programmer
Macros represents a commonly used group of statements in the
source programming language
It allows the programmer to write shorthand version of a program
(module programming) and leave mechanical details to be
handled by the macro processor
The macro processor replaces each macro invocation with the
corresponding sequence of statements (expanding)
Macro
the statement of expansion are generated each time the
macro are invoked
Subroutine
the statement in a subroutine appears only once
Macro Processor
z
z
z
z
Source
Code
(with macro)
Macro
Processor
Expanded
Code
Compiler or
Assembler
obj
Why macro?
z
Macro call
z
example
MACRO
INCR
MOVER
ADD
MOVEM
MEND
Macro expansion
z
z
z
Default- sequential
Absence of pre-processor statement: model statements
are visited sequentially
starting with statement - macro prototype statement ending with statement preceding MEND statement
A pre-processor statement can alter the flow of control
during expansion -----why??
Some model statements are either never visited during expansion,
or
Repeatedly visited during expansion
Algorithm
I.
II.
i.
III.
Lexical substitution
z
Expanded souce
.
.
.
STA
STB
STX
.
STA
STB
STX
.
{
{
DATA1
DATA2
DATA3
DATA4
DATA5
DATA6
13
Positional parameter
z
14
Keyword parameter
z
MEND
15
Nested Macro
z
16
Example
17
18
19
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
20
Data structures
z
22
EXPANDING
ARGTAB
F1
BUFFER
LENGTH
23
Sub-procedures
macro definition: DEFINE
macro invocation: EXPAND
MACRO
DEFINE
CALL
EXPAND
PROCESSLINE
MACRO Definition
24
25
28
Solution
Define procedure maintains counter named LEVEL
Each time a MACRO directive is read, the value of LEVEL is
increased by 1
Each time a MEND directive is read, the value of LEVEL is
decreased by 1
When LEVEL reaches 0, MEND corresponding to original MACRO
directive has been found
29
MACRO
PROCESSOR
GETLINE
EXPANDING=FALSE
PROCESSLINE
EXPAND
GETLINE
PROCESSLINE
EXPANDING=TRUE
GETLINE
PROCESSLINE
GETLINE
EXPANDING
FALSE
TRUE
READ FROM
DEFTAB
READ FROM
INPUT
30
ARGTAB
DEFTAB
MACRO Definition
NAMTAB
DEFINE
GETLINE
PROCESSLINE
Macro Invocation
EXPAND
ARGTAB
31
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
32
Machine-independent macro
processor features
z
z
z
z
33
34
X&ID1
X&ID1
Example:
35
z
z
MACRO
=X&OUTDEV
TD
=X&INDEV
TD
=XF1
TD
=XF1
1st call:
$AALOOP
2nd call:
$ABLOOP
37
z
z
z
z
For the first macro expansion, xx will have the value AA.
For succeeding expansions, xx will be set to AB, AC, etc.
If only alphabetic and numeric characters are allowed in xx, the
counter may provide 1296 macro expansions in a single program
38
RDBUFF
Macro-time variables
any symbol that begins with the character & and that is not a
macro instruction parameter is assumed a MTV
macro-time variables are initialized to 0
macro-time variables can be changed with their values using SET
E.g., RDBUFF in next figure has two additional parameter
&EOR (specifies a hexadecimal character code that marks the end of a
record)
&MAXLTH (specifies the maximum length record that can be read)
42
RDBUFF
RDBUFF
RDBUFF
Implementation of conditional
macro-expansion features
z
z
48
49
50
51
Consider a situation
RDCHAR is a macro and is invoked in macro RDBUFF
Unfortunately, the MP algorithm discussed so far cannot process
this. Why?
When RDBUFF is invoked, procedure EXPAND is called,
EXPANDING is set to true, and arguments (BUFFER, LENGTH, F1)
are entered to ARGTAB. During expansion, RDCHAR is invoked,
EXPAND is called again, new argument (F1) enters ARGTAB (to erase
the arguments of RDBUFF). After returning from EXPAND,
EXPANDING is set to false, which stops the expansion of RDBUFF.
52
VALUE
BUFFER
LENGTH
F1
(unused)
VALUE
F1
(unused)
.
53
54
55
z
z
z
56
57
Difficulties / Disadvantages
Case 1: Comments are usually ignored by a macro processor (at least
in scanning for parameters). However, each programming language
has its own methods for identifying comments.
58
59
Advantages
9 Programmer does not need to learn about a different macro facility
for each compiler or assembler language
9 Training time and cost is reduced
9Costs involved in producing a GP-MP is greater than languagespecific processor, however, this expense does not need to be
repeated for each language
9 Substantial overall saving in software development cost as well as
maintenance effort
60
61
62
63
Disadvantages
There are disadvantages to integrated and line-by-line macro
processors:
They must be specially designed and written to work with a
particular implementation of an assembler or compiler.
The costs of macro processor development must be added to the
cost of the language translator, resulting in a more expensive piece
of software.
The size may be a problem if the translator is to run on a
computer with limited memory.
64