0% found this document useful (0 votes)
147 views78 pages

Unit Iv

The document discusses macro processors and their basic functions. It covers macro definition, expansion, and invocation. Key aspects include saving macro definitions in a DEFTAB, recognizing and expanding macro calls by substituting arguments, and implementing a one-pass algorithm using data structures like NAMTAB and ARGTAB to handle macros defined within other macros.

Uploaded by

Sudhakar Selvam
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)
147 views78 pages

Unit Iv

The document discusses macro processors and their basic functions. It covers macro definition, expansion, and invocation. Key aspects include saving macro definitions in a DEFTAB, recognizing and expanding macro calls by substituting arguments, and implementing a one-pass algorithm using data structures like NAMTAB and ARGTAB to handle macros defined within other macros.

Uploaded by

Sudhakar Selvam
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/ 78

Macro Processor

UNIT-IV
Basic Macro Processor functions:
Macro Definition and Expansion
Macro Processor Algorithm and data structures.
Machine-independent Macro Processor features:
Concatenation of Macro Parameters
Generation of Unique Labels
Conditional Macro Expansion
Keyword Macro Parameters
Macro Processor Design Options
Recursive Macro Expansion
General Purpose Macro Processors
Macro Processing within Language Translators
Implementation examples:
MASM Macro Processor – ANSI C Macro language.
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
Copy code -- 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
. .
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
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
. .
Macro Instruction
• A macro instruction (macro) is simply a notational convenience
for the programmer.
• A macro represents a commonly used group of statements in the
source program.
• The macro processor replaces each macro instruction with the
corresponding group of source statements.
– This operation is called “expanding the macro”
• Using macros allows a programmer to write a shorthand version
of a program.
• For example, before calling a subroutine, the contents of all
registers may need to be stored. This routine work can be done
using a macro.
Machine Independent
• The functions of a macro processor essentially involve the
substitution of one group of lines for another. Normally,
the processor performs no analysis of the text it handles.
• The meaning of these statements are of no concern during
macro expansion.
• Therefore, the design of a macro processor generally is
machine independent.
• Macro mostly are used un assembler language
programming. However, it can also be used in high-level
programming languages such as C or C++.
Basic Functions
• Macro definition
– The two directive MACRO and MEND are used in
macro definition.
– The macro’s name appears before the MACRO
directive.
– The macro’s parameters appear after the MACRO
directive.
– Each parameter begins with ‘&’
– Between MACRO and MEND is the body of the macro.
These are the statements that will be generated as the
expansion of the macro definition.
Basic Functions
• Macro expansion (or invocation)
– Give the name of the macro to be expanded and the
arguments to be used in expanding the macro.
– Each macro invocation statement will be expanded into
the statements that form the body of the macro, with
arguments from the macro invocation substituted for the
parameters in the macro prototype.
– The arguments and parameters are associated with one
another according to their positions.
• The first argument corresponds to the first parameter, and so on.
Macro Program Example

Macro definition

Avoid the use of labels in a macro


Macro definition

Avoid the use of labels in a macro


Macro invocations
Expanded Macro Program
Retain Labels on Expanded Macro

• The label on the macro invocation


statement CLOOP has been retained as a
label on the first statement generated in the
macro expansion.
• This allows the programmer to use a macro
instruction in exactly the same way as an
assembler language mnemonic.
Differences between Macro and
Subroutine
• After macro processing, the expanded file can be used
as input to the assembler.
• The statements generated from the macro expansions
will be assembled exactly as though they had been
written directly by the programmer.
• The differences between macro invocation and
subroutine call
– The statements that form the body of the macro are
generated each time a macro is expanded.
– Statements in a subroutine appear only once, regardless of
how many times the subroutine is called.
Avoid Uses of Labels in Macro
• In RDBUFF and WRBUFF macros, many
program-counter relative addressing instructions
are used to avoid the uses of labels in a macro.
– For example, JLT * - 19
• This is to avoid generating duplicate labels when
the same macro is expanded multiple time at
different places in the program. (will be treated as
error by the assembler)
• Later on, we will present a method which allows a
programmer to use labels in a macro definition.
Two-Pass Macro Processor
• Like an assembler or a loader, we can design a
two-pass macro processor in which all macro
definitions are processed during the first pass, and
all macro invocation statements are expanded
during the second pass.
• However, such a macro processor cannot allow the
body of one macro instruction to contain
definitions of other macros.
– Because all macros would have to be defined during the
first pass before any macro invocations were expanded.
Macro Containing Macro
Example
Macro Containing Macro Example
• MACROS contains the definitions of RDBUFF and
WRBUFF which are written in SIC instructions.
• MACROX contains the definitions of RDBUFF and
WRBUFF which are written in SIC/XE instructions.
• A program that is to be run on SIC system could invoke
MACROS whereas a program to be run on SIC/XE can
invoke MACROX.
• Defining MACROS or MACROX does not define RDBUFF
and WRBUFF. These definitions are processed only when
an invocation of MACROS or MACROX is expanded.
One-Pass Macro Processor
• A one-pass macro processor that alternate between
macro definition and macro expansion is able to
handle “macro in macro”.
• However, because of the one-pass structure, the
definition of a macro must appear in the source
program before any statements that invoke that
macro.
– This restriction is reasonable.
Data Structures
• DEFTAB
– Store the definition statements of macros
– Comment lines are omitted.
– References to the macro instruction parameters are converted to a positional
notation for efficiency in substituting arguments.
• NAMTAB
– Store macro names, which serves an index to DEFTAB
– Contain pointers to the beginning and end of the definition
• ARGTAB
– Used during the expansion of macro invocations.
– When a macro invocation statement is encountered, the arguments are stored
in this table according to their position in the argument list.
Data Structures Snapshot
Algorithm
• Procedure DEFINE
– Called when the beginning of a macro definition is
recognized. Make appropriate entries in DEFTAB and
NAMTAB.
• Procedure EXPAND
– Called to set up the argument values in ARGTAB and
expand a macro invocation statement
• Procedure GETLINE
– Get the next line to be processed
Handle Macro in Macro
• When a macro definition is being entered into DEFTAB, the
normal approach is to continue until an MEND directive is
reached.
• This will not work for “macro in macro” because the MEND
first encountered (for the inner macro) will prematurely end the
definition of the outer macro.
• To solve this problem, a counter LEVEL is used to keep track of
the level of macro definitions. A MEND will end the definition
of the macro currently being processed only when LEVEL is 0.
– This is very much like matching left and right parentheses when
scanning an arithmetic expression.
Macro Processor Algorithm
DEFINE (line)
{
enter line.label into NAMTAB; /* enter macro name into NAMTAB */
enter macro prototype into DEFTAB; /* the first declaration statement */
LEVEL = 1;
while (LEVEL > 0) {
GETLINE (line); /* get the next line of the macro definition */
if (line != comment line) {
substitute positional notation for parameters; /* &param_1  ?1, etc. */
enter line into DEFTAB;
if (OPCODE = ‘MACRO’)
LEVEL = LEVEL +1;
else if (OPCODE = ‘MEND’)
LEVEL = LEVEL –1;
}
}
store pointers (label->begin and label->end) in NAMTAB; /* store macro def. begin and end pointers */
}
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
Machine Independent Features
Concatenation of Macro
Parameters
• Most macro processors allow parameters to
be concatenated with other character stings.
• E.g., to flexibly and easily generate the
variables XA1, XA2, XA3, …, or XB1,
XB2, XB3, “A” or “B” can be input as an
argument. We just need to concatenate “X”,
the argument, and the “1” , “2”, “3” ..
together.
Concatenation Problem
• Suppose that the parameter to such a macro instruction
is named &ID, the body of the macro definition may
contain a statement like LDA X&ID1, in which &ID is
concatenated after the string “X” and before the string
“1”.
• The problem is that the end of the parameter is not
marked. Thus X&ID1 may mean “X” + ID + “1” or
“X” + ID1.
• To avoid this ambiguity, a special concatenation
operator -> is used. The new form becomes X&ID->1.
Of course, -> will not appear in the macro expansion.
Concatenation Example
Generation of Unique Labels
• Previously we see that, without special processing, if
labels are used in macro definition, we may encounter
the “duplicate labels” problem if a macro is invocated
multiple time.
• To generate unique labels for each macro invocation,
when writing macro definition, we must begin a label
with $.
• During macro expansion, the $ will be replaced with
$xx, where xx is a two-character alphanumeric counter
of the number of macro instructions expanded.
– XX will start from AA, AB, AC,…..
Unique Labels Macro Definition
Unique Labels Macro Expansion
Conditional Macro Expansion
• So far, when a macro instruction is invoked, the
same sequence of statements are used to expand
the macro.
• Here, we allow conditional assembly to be used.
– Depending on the arguments supplied in the macro
invocation, the sequence of statements generated for a
macro expansion can be modified.
• Conditional macro expansion can be very useful.
It can generate code that is suitable for a particular
application.
Conditional Macro Example
• In the following example, the values of &EOR and &MAXLTH
parameters are used to determine which parts of a macro definition
need to be generated.
• There are some macro-time control structures introduced for doing
conditional macro expansion:
– IF- ELSE-ENDIF
– WHILE-ENDW
• Macro-time variables can also be used to store values that are used
by these macro-time control structures.
– Used to store the boolean expression evaluation result
– A variable that starts with & but not defined in the parameter list is treated
as a macro-time variable.
Macro time variable

Conditional macro control structure


Conditional macro expansion 1
Conditional macro expansion 2
Conditional macro expansion 3
Conditional Macro Implementation
• The assembler maintains a symbol table that contains the
values of all macro-time variables used.
• Entries in this table are made or modified when SET
statements are processed.
• When an IF statement is encountered during the expansion
of a macro, the specified boolean expression is evaluated.
– If the value of this expression is TRUE, the macro processor
continues to process until it encounters the next ELSE or ENDIF.
• If ELSE is encountered, then skips to ENDIF
– Otherwise, the assembler skips to ELSE and continues to process
until it reaches ENDIF.
Conditional Macro Example
Macro processor function
Conditional Macro Expansion v.s.
Conditional Jump Instructions
• The testing of Boolean expression in IF statements
occurs at the time macros are expanded.
• By the time the program is assembled, all such
decisions have been made.
• There is only one sequence of source statements during
program execution.
• In contrast, the COMPR instruction test data values
during program execution. The sequence of statements
that are executed during program execution may be
different in different program executions.
Keyword Macro Parameters
• So far, all macro instructions use positional parameters.
– If an argument is to be omitted, the macro invocation statement
must contain a null argument to maintain the correct argument
positions.
– GENER MACRO &1, &2, &type, …, &channel, &10
– E.g., GENER, ,DIRECT, , , , , , 3.
• If keyword parameters are used, each argument value is
written with a keyword that names the corresponding
parameters.
– Arguments thus can appear in any order.
– Null arguments no longer need to be used.
– E.g., GENER TYPE=DIRECT, CHANNEL=3
• Keyword parameter method can make a program easier to
read than the positional method.
4.2.4 Keyword Macro Parameters

• Positional parameters
– Parameters and arguments were associated
with each other according to their positions in
the macro prototype and the macro
invocation statements.
– A certain macro instruction GENER has 10
possible parameters.
GENER MACRO &1, &2, &type, …, &channel, &10

GENER , , DIRECT, , , , , , 3
4.2.4 Keyword Macro Parameters

• Keyword parameters
– Each argument value is written with a
keyword that names the corresponding
parameter.
– Arguments may appear in any order.
GENER MACRO &1, &2, &type, …, &channel, &10

GENER , , DIRECT, , , , , , 3 (positional)


GENER TYPE=DIRECT, CHANNEL=3 (keyword)
GENER CHANNEL=3, TYPE=DIRECT (keyword)
parameter=argument
Keyword Macro Example

Can specify default values


Keyword parameters
Macro Processor Design Options

• Recursive Macro expression


• General-Purpose Macro Processors
• Macro Processing within Language
Translators
Recursive Macro Expansion
• Macro within macro can be solved if the macro
processor is being written in a programming
language that allows recursive calls
• The compiler would be sure that previous value of
any variables declared within a procedure were
saved when that procedure was called recursively
• If would take care of other details involving return
from the procedure
Example of Nested Macro
Invocation(2/1)
Example of Nested Macro
Invocation(2/2)
Macro Processor Design Options

Recursive Macro Expansion

Problems with previous macro processor

- when a macro invokes another macro


ARGTAB is overwritten

- when the second macro finishes


EXPANDING is set to FALSE
Solution

- if macro processor is written in a high-level


language that supports recursion, then there
won't be problems since the compiler saves the
previous values of variables on recursive calls

- if macro processor must be written in a


programming language that does not support
recursion, then the system programmer must
save the data values (this can be done using a
stack)
General-Purpose Macro
Processors
A general purpose macro processor is
a macro processor that is not tied to any
particular language.

Macro processors that do not dependent on


any particular programming language, but
can be used with a variety of different
languages
General-Purpose Macro
Processors(2/1)
• Advantages of general-purpose macro processors:

– The programmer does not need to learn about a different


macro facility for each compiler or assembler.

– Over all software development cost can be reduced


General-Purpose Macro
Processors(2/2)
• Comments should usually be ignored by a macro processor,
however, each programming language has its own methods for
identifying comments
• Each programming language has different facilities for
grouping terms, expressions, or statements—a general-purpose
macro processor needs to taking these grouping into account
• Languages differ substantially in their restrictions on the
length of identifiers and the rules for the formation of
constants
• Programming languages have different basic statement forms
—syntax used for macro definitions and macro invocation
statements
Macro Processing within Language
Translators(2/1)
• The macro processor reads the source statements and performs
all of its functions, the output lines are passed to the language
translator as they are generated
• The macro processor operates as a sort of input routine for the
assembler or compiler
• The line-by-line approach avoids making an extra pass over
the source program, so it can be more efficient than using a
macro preprocessor
• Some of the data structures required by the macro processor
and the language translator can be combined
• A line-by-line macro processor also makes it easier to give
diagnostic messages that are related to the source statement
containing the error
Macro Processing within Language
Translators(2/2)
• An integrated macro processor can potentially make use of
any information about the source program that is extracted by
the language translator
• An integrated macro processor can support macro instructions
that depend upon the context in which they occur
• Line-by-line macro processors must be specially designed and
written to work with a particular implementation of an
assembler or compiler, which results in a more expensive
piece of software
• The assembler or compiler will be considerably larger and
more complex than it would be
• The additional complexity will add to the overhead of
language translation
MASM Macro Processor(2/1)
• The macro processor of MASM is integrated with
Pass 1 of the assembler
• MASM generates the unique names of local labels in
the form ??n, where n is a hexadecimal number in the
range 0000 to FFFF
• .ERR: signals to MASM that an error has been
detected
• EXITM: directs MASM to terminate the expansion of
the macro
• &: is a concatenation operator
MASM Macro Processor(2/2)
• ;; is a macro comment, serves only as
documentation for the macro definition
• ; is an ordinary assembler language comment,
included as part of the macro expansion
• IRP: sets the macro-time variable to a sequence of
values specified in <…>
• The statements between the TRP and the matching
ENDM are generated once for each value of the
variable
Examples of MASM Macro and
Conditional Statements(3/1)
Examples of MASM Macro and Conditional
Statements(3/2)
Examples of MASM Macro and Conditional
Statements(3/3)
ANSI C Macro Language
• In the ANSI C language, definitions and invocations of macros
are handled by a preprocessor, which is generally not
integrated with the rest of the compiler.
• Two simple example of ANSI C macro definition
• #define NULL 0
• #define EOF -1
• Defining Macro
• #define EQ = = ->syntactic modification
• Programmer could write as
• While (I EQ 0)
• Macro processor convert this into
• While(I = = 0)
ANSI C Macro Language
• ANSI C macros defined with parameters
• #define ABSDIFF (X,Y) ( (X)>(Y) ? (X)-(Y) : (Y)-(X) )
ANSI C Macro Language
• Conditional compilation statements
– Example 1:
• #ifndef BUFFER_SIZE
• #define BUFFER_SIZE 1024
• #endif
The ELENA Macro Processor
• Macro definitions in ELENA are composed of a header and a
body
• The header consists of keywords and parameter markers,
which are identified by the character %
• At least one of the first two token s in a macro header must be
a keyword, not a parameter marker
• The macro processor appends a numeric value to create unique
labels
• ELENA provides macro-time variables and macro-time
instructions that can be used to control the macro expansion
• The IF statement is a macro-time conditional “go to”
statement
• The macro is identified by the sequence of keywords that
appear in its header
Examples of ELENA Macro Definition
and Invocation(2/1)
Examples of ELENA Macro Definition and Invocation(2/2)
1. Expand the following macro invocation statements, using the
macro invocation and definition given below.
RDBUFF F3, BUF, LEN, (01, 04, 09)
RDBUFF F1, BUFFER, LENGTH
 
RDBUFF MACRO &INDEV,&BUFADR,&RECLTH,&EOR
&EORCT SET %NITEMS(&EOR)
CLEAR X
CLEAR A
+LDT #4096
$LOOP TD =X’&INDEV’
JEQ $LOOP
RD =X’&INDEV’
&CTR SET 1
WHILE (&CTR LE &EORCT)
COMP =X’0000&EOR(&CTR)
JEQ $EXIT
&CTR SET &CTR+1
ENDW
STCH &BUFADR,X
TIXR T
JLT $LOOP
$EXIT STX &RECLTH
MEND

You might also like