Macro_Preprocessor

Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

Macro Processor

Macro Instruction
► A macro (or macro instruction)
► It is simply a notational convenience for the programmer.
► It represents a commonly used group of statements in the
source program.
► It allows the programmer to write shorthand version of a
program
Macro Preprocessor
► The macro pre-processor(or macro processor) is a system
software which replaces each macro instruction with the
corresponding group of source language statements.
► This operation is called expanding the macro.
► It does not concern the meaning of the involved statements
during macro expansion.
► The design of a macro processor generally is machine
independent
Machine Independent
• The functions of a macro processor 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 in assembler language
programming. However, it can also be used in
high-level programming languages such as C or
C++.
Basic Macro Processor Functions
► The fundamental functions common to all macro
processors are:
► Macro Definition
► Macro Invocation
► Macro Expansion
Macro Definition
► Macro definitions are typically located at the start of
a program.
► A macro definition is enclosed between a macro
header statement(MACRO) and a macro end
statement(MEND)
► Format of macro definition
Macro Definition
► A macro definition consist of macro prototype
statement and body of macro.
► A macro prototype statement declares the name of a
macro and its parameters. It has following format:
macroname MACRO parameters

► Parameters indicates the list of formal parameters.


► Parameters is of the form &parameter1,
&parameter2,…
► Each parameter begins with ‘&’
Macro Invocation(or Macro Call)
► A macro invocation statement (macro call) gives the
name of the macro instruction being invoked and
the arguments to be used in expanding the macro.

► The format of macro invocation :


macroname p1, p2,...pn

► Example: SUM P,Q


Macro Expansion
► Each macro invocation statement will be expanded
into the statements that form the body of the macro.
► Arguments from the macro invocation are 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.
► Comment lines within the macro body have been deleted, but
comments on individual statements have been retained.
► Macro invocation statement itself has been included
as a comment line.
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.(WRBUFF on
lines 210 and 220)
– Statements in a subroutine appear only once, regardless
of how many times the subroutine is called.
Avoid Uses of Labels in Macro
• Body of the macro contains no labels.
• Eg:- JEQ *-3 (line 140)
JLT *-14 (line 155)

In WRREC,
JEQ WLOOP
JLT WLOOP
WLOOP is a label on TD instruction, that tests output device.
• If such a label appear in macro body, it would be generated
twice, lines 210d and 220d(duplicate definition).
• To avoid duplication of symbols, labels are eliminated from the
body of 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.
• The same program could run on a standard SIC machine or a
SIC/XE machine.
• 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 (Definition Table)
– All the macro definitions in the program are stored in
DEFTAB, which includes macro prototype and macro body
statements.

– Comment lines from macro definition are not entered into


DEFTAB because they will not be a part of macro expansion.

– References to the macro instruction parameters are converted


to a positional notation for efficiency in substituting
arguments.
Data Structures
• NAMTAB (Name Table)
– Store macro names, which serves an index to DEFTAB.
– For each macro instruction defined, this table Contain
pointers to the beginning and end of the definition in
DEFTAB.

• ARGTAB (Argument Table)


– 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.
– When the macro is expanded, arguments from this table
are substituted for the corresponding parameters in the
macro body.
Data Structures Snapshot
• When the macro definition for RDBUFF is encountered,
the macro name RDBUFF along with its parameters
INDEV, BUFADR and RECLTH are entered into
DEFTAB.
• Then the statements in the body of macro is also entered
into DEFTAB.
• The positional notation is used for the parameters. The
parameter &INDEV has been converted to ?1, &BUFADR
has been converted to ?2 ….
• The macro name RDBUFF is entered into NAMTAB and
the beginning and end pointers are also marked.
• On processing the input code, opcode in each statement is
compared with the NAMTAB, to check whether it is a
macro call.
• When the macro call RFBUFF F1,BUFFER,LENGTH is
recognized, the arguments F1,BUFFER and LENGTH will
be entered into ARGTAB.
• The macro is expanded by taking the statements from
DEFTAB using the beginning and end pointers of
NAMTAB.
• When the ?n notation is recognized in a line from
DEFTAB, the corresponding argument is taken from
ARGTAB.
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.
• Each time a MACRO directive is read, value of LEVEL is
increased by 1;each time an MEND is read, its value is
decreased by 1.
• 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.
Algorithm-one pass macro
processor
► The algorithm uses 5 procedures
► MACROPROCESSOR (main function)

► DEFINE

► EXPAND

► PROCESSLINE

► GETLINE
► MACROPROCESSOR (MAIN function)
► This function initialize the variable named EXPANDING to
false.
► It then calls GETLINE procedure to get next line from the
source program and PROCESSLINE procedure to process
that line.
► This process will continue until the END of program.

► PROCESSLINE
► This procedure checks If the opcode of current statement is
present in NAMTAB.
► If so it is a macro invocation statement and calls the
procedure EXPAND
► Else if opcode =MACRO, then it indicates the beginning of
a macro definition and calls the procedure DEFINE
► Else it is identified as a normal statement(not a macro
definition or macro call) and write it to the output file.
Algorithm Pseudo Code
► DEFINE
► The control will reach in this procedure if and only if it is
identified as a macro definition statement. Then:
► Macro name is entered into NAMTAB

► Then the macro name along with its parameters are entered
into DEFTAB.
► The statements in body of macro is also entered into
DEFTAB. References to the macro instruction parameters
are converted to a positional notation for efficiency in
substituting arguments.
► Comment lines from macro definition are not entered into
DEFTAB because they will not be a part of macro
expansion.
► Store in NAMTAB the pointers to beginning and end of
definition in DEFTAB.
Handle Macro in Macro
► DEFINE
► To deal with Nested macro definitions DEFINE procedure
maintains a counter named LEVEL.
► When the assembler directive MACRO is read, the value of
LEVEL is incremented by 1.
► When MEND directive is read, the value of LEVEL is
decremented by 1.
► That is, whenever a new macro definition is encountered
within the current definition, the value of LEVEL will be
incremented and the while loop which is used to process the
macro definition will terminate only after the value of
LEVEL =0. With this we can ensure the nested macro
definitions are properly handled.
procedure DEFINE
begin
enter macro name into NAMTAB
enter macro prototype into DEFTAB
LEVEL := 1
while LEVEL > 0 do
begin
GETLINE
if this is not a comment line then
begin
substitute positional notation for parameters
enter line into DEFTAB
if OPCODE = ‘MACRO’ then
LEVEL := LEVEL + 1
else if OPCODE = ‘MEND’ then
LEVEL = LEVEL – 1
end {if not comment}
end {while}
store in NAMTAB pointers to beginning and end of defintion
end {DEFINE}
► EXPAND
► The control will reach in this procedure if and only if it is
identified as a macro call
► In this procedure, the variable EXPANDING is set to true.
It actually indicates the GETLINE procedure that it is going
to expand the macro call. So that GETLINE procedure will
read the next line from DEFTAB instead of reading from
input file.
► The arguments of macro call are entered into ARGTAB.

► The macro call is expanded with the lines from the


DEFTAB. When the ?n notation is recognized in a line from
DEFTAB, the corresponding argument is taken from
ARGTAB.
► GETLINE
► This procedure is used to get the next line.

► If EXPANDING = TRUE, the next line is fetched from


DEFTAB. (It means we are expanding the macro call)
► If EXPANDING = False, the next line is read from input
file.
Flow Diagram of a one pass macro
processor
Machine Independent Features

- Extended features of basic macro processor


functions
- Not directly related to the architecture of the
computer.
Concatenation of Macro Parameters
• Most macro processors allow macro parameters to be
concatenated with other character stings.
e.g. : suppose a program contains one series of variables
named XA1,XA2,XA3,..... and another series named
XB1,XB2,XB3,........etc.
-If similar processing is to be performed on each series of
variables, it can be incorporated into a macro instruction.
-The parameters of such a macro instruction specify the
series of variables to be operated on (A,B,..).
-The macro processor use these parameters to construct the
symbols required in the macro expansion(XA1,XB1, etc.)
► The macro call TOTAL A will be expanded as:
Concatenation Problem
• Suppose that the parameter to a macro instruction is named
&ID. The body of the macro definition may contain a
statement like
LDA X&ID1
in which parameter &ID is concatenated after the
string “X” and before the string “1”.
• Beginning of the macro parameter is identified by &.
• 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 LDA X&ID1
• End of the parameter &ID is identified.
•  will not appear in the macro expansion.
Concatenation Example

Macro Definition

Macro invocation statements & expansions


Generation of Unique Labels
• Labels of usual kind cannot be 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 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.
• For the first macro expansion, xx will have the value AA.
• For succeeding macro expansions , XX will be set to
AB,AC etc.
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.
• It also improves the power and flexibility of a
macro language.
Conditional Macro Example
• In the example, RDBUFF has two additional parameters:
– &EOR - a hexadecimal character marking the end of a record
– &MAXLTH - the maximum length record that can be read.
• These 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.(initialized to 0)
Macro time variable

Conditional macro control structure


• Line 26 through 28, controls the macro processor directive
SET.
• SET statement assigns the value 1 to &EORCK.
• &EORCK is a macro-time variable, used to store working
values during macro expansion.
• Any symbol that begin with ‘&’ and not a macro
instruction parameter is a macro-time variable.
• All such variable are initialized to 0.
• In the example, if &EOR is not null, &EORCK is set to 1.
• The value of &EORCK is used in the conditional
structures on line 38-43 and 63-73.
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 lines from DEFTAB until it
encounters the next ELSE or ENDIF.
• If ELSE is encountered, then skips lines in DEFTAB until next
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.
– 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.
Keyword Macro Example

Can specify default values


Keyword parameters
Design Options
Macro Containing Macro Example
Recursive Macro Expansion
• To perform invocation of one macro in another
macro definition, the already presented macro
processor implementation cannot be used.
• This is because the EXPAND routine is
recursively called but the variable used by it (e.g.,
EXPANDING) is not saved across these calls.
• It is easy to solve this problem if we use a
programming language that support recursive
functions. (e.g., C or C++).
Algorithm Pseudo Code
Recursive Macro Example
• Example: macro instruction RDBUFF
• A related macro instruction RDCHAR read one
character from a device to register A, taking care
of necessary test-and-wait loop.
• The programmer defining RDBUFF need not
worry about the details of device access and
control.
Recursive Macro Example
Recursive Macro Example

For easy implementation, we require that RDCHAR macro


be defined before it is used in RDBUFF macro. This
requirement is very reasonable.
• Already presented macro processor implementation
cannot handle invocation of macros within macros.
RDBUFF BUFFER,LENGTH,F1
• Procedure EXPAND enter details into ARGTAB as:
• EXPANDING is set to TRUE.
• Line 50 invokes RDCHAR.
• PROCESSLINE again call EXPAND.
• The ARGTAB would be

• Expansion of RDCHAR proceed normally.


• At the end of RDCHRAR expansion, variable
EXPANDING would be set to FALSE.
• Macro processor would “forget” that it had been in
the middle of expanding a macro when it
encountered the RDCHAR statement.
• Arguments from the original invocation
(RDBUFF) would be lost as the values in
ARGTAB were overwritten with the arguments
from the invocation of RDCHAR.
• Write microprocessor using a programming
language that allows recursive calls.
• Invoking RDCHAR in the definition of RDBUFF is useful
as the programmer defining RDBUFF need not worry
about the details of device access and control.
• RDCHAR might be written at a different time or by a
different programmer.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy