Macro_Preprocessor
Macro_Preprocessor
Macro_Preprocessor
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
Macro definition
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.
► 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.
Macro Definition