chapter 4 macro processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/sp93_2/chapter...

33
1 Chapter 4 Macro Processors

Upload: doantruc

Post on 02-Apr-2018

249 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

1

Chapter 4 Macro Processors

Page 2: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 3: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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.

Page 4: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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.

Page 5: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 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

Page 6: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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>

Page 7: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 8: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 9: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

9

Fig 4.1: Macro Definition

•Macro definition

Macro body contains no label

Page 10: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

10

Fig 4.1: Macro Definition (Cont.)•Macro definition

Macro body contains no label

Page 11: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 12: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

12

Fig 4.1: Macro Invocation•Macro invocation

Page 13: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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.

Page 14: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 15: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 16: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

16

Program From Fig. 4.1 with Macros Expanded (fig. 4.2)

•Macro expansion

Page 17: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

17

Program From Fig. 4.1 with Macros Expanded (fig. 4.2)(Cont.)

•Macro expansion

.

Page 18: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

18

Program From Fig. 4.1 with Macros Expanded (fig. 4.2)(Cont.)

•Macro expansion

Page 19: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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.

Page 20: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

20

4.1.2 Macro Processors Algorithm and Data Structureso Two-pass macro processor

o One-pass macro processor

Page 21: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 22: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 23: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

23

Macro Definition within a Macro Body (Figure 4.3(a))

Page 24: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

24

Macro Definition within a Macro Body (Figure 4.3(b))

Page 25: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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.

Page 26: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 27: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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.

Page 28: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

28

Page 29: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

29

One-Pass Macro Processoro Proceduresn Macro definition: DEFINEn Macro invocation: EXPAND

DEFINE

EXPANDPROCESSLINE

DEFTAB

NAMTAB

ARGTAB

Macrodefinition

Macroinvocation

GETLINEOne-Pass macro processor

Page 30: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent
Page 31: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

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

Page 32: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

32

Algorithm for one-pass macro processor (Fig. 4.5) (Cont.)

Page 33: Chapter 4 Macro Processors - 國立中興大學osnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-1.pdf · Chapter 4: Macro Processors o 4.1 Basic Macro Processors Functions o 4.2 Machine-Independent

33