You are on page 1of 30

System Programming (SENG 3044)

Lecture 04: Macro Processors

Walelign A. (M.Sc.)

Debre Berhan University


Faculty of Computing
Department of Software Engineering

January 4, 2021

Lecture 04 Macro Processors January 4, 2021 1 / 31


Contents

Basic Macro Processors Functions


Machine-Independent Macro Processors Features
Macro Processors Design Options
Implementation Examples

Lecture 04 Macro Processors January 4, 2021 2 / 31


Introduction to Macro Processors

A macro instruction (macro) is a notational convenience for the


programmer.
I Allow the programmer to write a shorthand version of a program
A macro represents a commonly used group of statements in the
source programming language.
Expanding the macros
I The macro processor replaces each macro instruction with the
corresponding group of source language statements.

Lecture 04 Macro Processors January 4, 2021 3 / 31


Introduction to Macro Processors

A macro processor
I Essentially involve the substitution of one group of characters or lines
for another.
I Normally, it performs no analysis of the text it handles.
I It doesn’t concern the meaning of the involved statements during
macro expansion
The design of a macro processor generally is machine independent.

Lecture 04 Macro Processors January 4, 2021 4 / 31


Introduction to Macro Processors

Three examples of actual macro processors:


I A macro processor designed for use by assembler language programmers
I Used with a high-level programming language
I General-purpose macro processor, which is not tied to any particular
language

C uses a macro preprocessor to support language extensions, such as


named constants, expressions, and file inclusion.

Lecture 04 Macro Processors January 4, 2021 5 / 31


Basic Macro Processors Functions

Macro processor should processes the


I Macro Definitions
F Define macro name, group of instructions
I Macro invocation (macro calls)
F A body is simply copied or substituted at the point of call
I Expansion with substitution of parameters
F Arguments are textually substituted for the parameters
F The resulting procedure body is textually substituted for the call

Lecture 04 Macro Processors January 4, 2021 6 / 31


Macro Definition

Two new assembler directives are used in macro definition:


I MACRO: identify the beginning of a macro definition
I MEND: identify the end of a macro definition
label op operands
name MACRO parameters
.
body
.
MEND
Parameters: the entries in the operand field identify the parameters of
the macro instruction
I We require each parameter begins with ‘&
Body: the statements that will be generated as the expansion of the
macro.

Lecture 04 Macro Processors January 4, 2021 7 / 31


Macro Definition

Lecture 04 Macro Processors January 4, 2021 8 / 31


Macro Definition

Lecture 04 Macro Processors January 4, 2021 9 / 31


Macro Definition

Lecture 04 Macro Processors January 4, 2021 10 / 31


Macro Invocation

A macro invocation statement (a macro call) gives the name of the


macro instruction being invoked and the arguments in expanding the
macro.
Macro Invocation vs. Subroutine Call
I Statements of the macro body are expanded each time the macro is
invoked.
I Statements of the subroutine appear only one, regardless of how many
times the subroutine is called.
I Macro invocation is more efficient than subroutine call, however, the
code size is large

Lecture 04 Macro Processors January 4, 2021 11 / 31


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.
I The arguments and parameters are associated with one another
according to their positions.
F The first argument in the macro invocation corresponds to the first
parameter in the macro prototype, etc.

Lecture 04 Macro Processors January 4, 2021 12 / 31


Macro Expansion

Lecture 04 Macro Processors January 4, 2021 13 / 31


Macro Expansion with Parameters Substitution

Lecture 04 Macro Processors January 4, 2021 14 / 31


Macro Processors Algorithm and Data Structures

Two-pass macro processor


One-pass macro processor

Lecture 04 Macro Processors January 4, 2021 15 / 31


Two-pass macro processor

Two-pass Macro processor


I Pass1: process all macro definitions
I Pass2: expand all macro invocation statements
Problem
I Does not allow nested macro definitions
I Nested macro definitions
F The body of a macro contains definitions of other macros
I Because all macros would have to be defined during the first pass
before any macro invocations were expanded
Solution
I One-pass macro processor

Lecture 04 Macro Processors January 4, 2021 16 / 31


Nested Macros Definition

MACROS (for SIC)


I contains the definitions of RDBUFF and WRBUFF written in SIC
instructions
MACROX (for SIC/XE)
I contains the definitions of RDBUFF and WRBUFF written in SIC/XE
instructions.

Lecture 04 Macro Processors January 4, 2021 17 / 31


Nested Macros Definition

Lecture 04 Macro Processors January 4, 2021 18 / 31


Nested Macros Definition

Lecture 04 Macro Processors January 4, 2021 19 / 31


Nested Macros Definition

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 MACROX does not define RDBUFF and WRBUFF.
I These definitions are processed only when an invocation of MACROX is
expanded

Lecture 04 Macro Processors January 4, 2021 20 / 31


One-pass macro processor

One-pass macro processor


I Every macro must be defined before it is called
I One-pass processor can alternate between macro definition and macro
expansion
I Nested macro definitions are allowed

Lecture 04 Macro Processors January 4, 2021 21 / 31


Three Main Data Structures

DEFTAB
I A definition table used to store macro definition including
F macro prototype
F macro body
I Comment lines are omitted.
I Positional notation has been used for the parameters for efficiency in
substituting arguments.
NAMTAB
I A name table used to store the macro names
I Serves as an index to DEFTAB
F Pointers to the beginning and the end of the macro definition
ARGTAB
I A argument table used to store the arguments used in the expansion of
macro invocation
I As the macro is expanded, arguments are substituted for the
corresponding parameters in the macro body.

Lecture 04 Macro Processors January 4, 2021 22 / 31


Data Structures Snapshot

Lecture 04 Macro Processors January 4, 2021 23 / 31


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
I This is very much like matching left and right parentheses when
scanning an arithmetic expression.

Lecture 04 Macro Processors January 4, 2021 24 / 31


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.

Lecture 04 Macro Processors January 4, 2021 25 / 31


Concatenation Example

Lecture 04 Macro Processors January 4, 2021 26 / 31


Recursive Macro Expansion

If we want to allow a macro to be invoked in a 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++).

Lecture 04 Macro Processors January 4, 2021 27 / 31


Recursive Macro Examplee

Lecture 04 Macro Processors January 4, 2021 28 / 31


Recursive Macro Example

Lecture 04 Macro Processors January 4, 2021 29 / 31


End

Question
Many Thanks!

Lecture 04 Macro Processors January 4, 2021 30 / 31

You might also like