chapter 4 macro processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/sp93_2/chapter...
TRANSCRIPT
1
Chapter 4 Macro Processors
2
Chapter 4: Macro Processorso 4.1 Basic Macro Processors Functionso 4.2 Machine-Independent Macro Processors
Featureso 4.3 Macro Processors Design Optionso 4.4 Implementation Examples
3
Introduction to Macro Processorso A macro instruction (macro) is a notational
convenience for the programmer.n Allow the programmer to write a shorthand version of a
programo A macro represents a commonly used group of
statements in the source programming language.o Expanding the macros
n The macro processor replaces each macro instruction with the corresponding group of source language statements.
4
Introduction to Macro Processors (Cont.)o A macro processorn Essentially involve the substitution of one group
of characters or lines for another.n Normally, it performs no analysis of the text it
handles.n It doesn’t concern the meaning of the involved
statements during macro expansiono The design of a macro processor generally is
machine independent.
5
Introduction to Macro Processors (Cont.)o Three examples of actual macro processors:n A macro processor designed for use by assembler
language programmersn Used with a high-level programming languagen General-purpose macro processor, which is not
tied to any particular language
Source Code
(with macro)
Macro Processor
Expanded Code
Compiler /Assembler
Objectprogram
6
Introduction to Macro Processors (Cont.)o C uses a macro preprocessor to support
language extensions, such as named constants, expressions, and file inclusion.
#define max(a,b) ((a<b)?(a):(b))#define MACBUF 4#include <stdio.h>
7
4.1 Basic Macro Processors Functionso Macro processor should processes then Macro definitions
o Define macro name, group of instructionsn Macro invocation (macro calls)
o A body is simply copied or substituted at the point of call
n Expansion with substitution of parameterso Arguments are textually substituted for the parameterso The resulting procedure body is textually substituted for the call
8
Macro Definitiono Two new assembler directives are used in macro definition:
n MACRO: identify the beginning of a macro definitionn MEND: identify the end of a macro definition
o label op operandsname MACRO parameters
:body
:MEND
o Parameters: the entries in the operand field identify the parameters of the macro instructionn We require each parameter begins with ‘&’
o Body: the statements that will be generated as the expansion of the macro.o Prototype for the macro:
n The macro name and parameters define a pattern or prototype for the macro instructions used by the programmer
9
Fig 4.1: Macro Definition
•Macro definition
Macro body contains no label
10
Fig 4.1: Macro Definition (Cont.)•Macro definition
Macro body contains no label
11
Macro Invocationo A macro invocation statement (a macro call) gives
the name of the macro instruction being invoked and the arguments in expanding the macro.
o Macro Invocation vs. Subroutine Call.n Statements of the macro body are expanded each time the
macro is invoked.n Statements of the subroutine appear only one, regardless
of how many times the subroutine is called.n Macro invocation is more efficient than subroutine call,
however, the code size is larger
12
Fig 4.1: Macro Invocation•Macro invocation
13
Macro Expansiono Each macro invocation statement will be expanded
into the statements that form the body of the macro.
o Arguments from the macro invocation are substituted for the parameters in the macro prototype.n The arguments and parameters are associated with one
another according to their positions.o The first argument in the macro invocation corresponds to the
first parameter in the macro prototype, etc.
14
Macro ExpansionSource program
WD MACROSTA DATA1STB DATA2MEND
.WD
.WD
.WD
.
Expanded source program...
STA DATA1STB DATA2
.STA DATA1STB DATA2
.STA DATA1STB DATA2
.
MacroExpansion
byMacro
processor
Macro definition
Macro invocation
15
Macro Expansion with Parameters SubstitutionSource program
WD MACRO &A1,&A2STA &A1STB &A2MEND
.WD DATA1,DATA2
.WD DATA3,DATA4
.WD DATA5,DATA6
.
Expanded source program..
STA DATA1STB DATA2
.STA DATA3STB DATA4
.STA DATA5STB DATA6
.
MacroExpansion
byMacro
processor
Macro definition
Macro invocation
16
Program From Fig. 4.1 with Macros Expanded (fig. 4.2)
•Macro expansion
17
Program From Fig. 4.1 with Macros Expanded (fig. 4.2)(Cont.)
•Macro expansion
.
18
Program From Fig. 4.1 with Macros Expanded (fig. 4.2)(Cont.)
•Macro expansion
19
No Label in the Body of Macroo Problem of the label in the body of macro:
n If the same macro is expanded multiple times at different places in the program. o There will be duplicate labels, which will be treated as errors by the
assembler, o Solutions:
n Simply not to use labels in the body of macro. n Explicitly use PC-relative addressing instead.
o For example, in RDBUFF and WRBUFF macros,JEQ * +11JLT *-14
o It is inconvenient and error-prone.n Other better solution?
o Mentioned in Section 4.2.2.
20
4.1.2 Macro Processors Algorithm and Data Structureso Two-pass macro processor
o One-pass macro processor
21
Two-pass macro processoro Two-pass macro processor
n Pass1: process all macro definitionsn Pass2: expand all macro invocation statements
o Problemn Does not allow nested macro definitionsn Nested macro definitions
o The body of a macro contains definitions of other macrosn Because all macros would have to be defined during the
first pass before any macro invocations were expandedo Solution
n One-pass macro processor
22
Nested Macros Definitiono MACROS (for SIC)n contains the definitions of RDBUFF and
WRBUFF written in SIC instructions.o MACROX (for SIC/XE)n contains the definitions of RDBUFF and
WRBUFF written in SIC/XE instructions.o Example 4.3
23
Macro Definition within a Macro Body (Figure 4.3(a))
24
Macro Definition within a Macro Body (Figure 4.3(b))
25
Nested Macros Definition (Cont.)o 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.
o Defining MACROX does not define RDBUFF and WRBUFF. n These definitions are processed only when an
invocation of MACROX is expanded.
26
One-pass macro processoro One-pass macro processorn Every macro must be defined before it is called
n One-pass processor can alternate between macro definition and macro expansion
n Nested macro definitions are allowed
27
Three Main Data Structureso DEFTAB
n A definition table used to store macro definition includingo macro prototypeo macro body
n Comment lines are omitted.n Positional notation has been used for the parameters for efficiency in
substituting arguments.o E.g. the first parameter &INDEV has been converted to ?1 (indicating the first
parameter in the prototype)o NAMTAB
n A name table used to store the macro namesn Serves as an index to DEFTAB
o Pointers to the beginning and the end of the macro definitiono ARGTAB
n A argument table used to store the arguments used in the expansion of macro invocation
n As the macro is expanded, arguments are substituted for the corresponding parameters in the macro body.
28
29
One-Pass Macro Processoro Proceduresn Macro definition: DEFINEn Macro invocation: EXPAND
DEFINE
EXPANDPROCESSLINE
DEFTAB
NAMTAB
ARGTAB
Macrodefinition
Macroinvocation
GETLINEOne-Pass macro processor
31
One-Pass Macro Processor Allows Nested Macro Definitiono Sub-procedure DEFINE should handle the
nested macro definitionn Maintains a counter named LEVELn Each time a MACRO directive is read, the value
of LEVEL is increased by 1n Each time an MEND directive is read, the value
of LEVEL is decreased by 1
32
Algorithm for one-pass macro processor (Fig. 4.5) (Cont.)
33