10
1 4.1Basic macro processor functions 2 Introduction A macro instruction (abbr. macro) is a notational convenience for the programmer. A macro represents a commonly used group of statements in the source programming language. The macro processor replaces each macro instruction with the corresponding group of source language statements. This is called expanding the macros.

Basic Macro Processor Functions 1

Embed Size (px)

Citation preview

1

4.1Basic macro processor functions

2

Introduction

A macro instruction (abbr. macro) is a notational convenience for the programmer.A macro represents a commonly used group of statements in the source programming language.The macro processor replaces each macro instruction with the corresponding group of source language statements. • This is called expanding the macros.

2

3

Introduction (cont’d)

The mostly common use of macro processors is in assembler language programming.The design of a macro processor is not directly related to the architecture of the computer on which it is to run.

4

Macro definition

Two new assembler directives are used in macro definition.

macro_name MACRO &para1, &para2...... MEND

• The macro name and parameters define a pattern or prototype for the macro instructions used by the programmer.

• Following the MACRO directive are the statements that make up the body of the macro definition.

3

5

Macro invocation

A macro invocation statement gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro.The process of macro invocation and subroutine call are quite different.• The statements that form the expansion of a macro

are generated (and assembled) each time the macro in invoked.

• Statements in a subroutine appear only once, regardless of how many times the subroutine is called.

A macro definition

Macro invocation

Use of Macro

4

7

Macro expansion

The macro instruction definitions are deleted since they are no longer needed after the macros are expanded.Each macro invocation statement is expanded into the statements that form the body of the macro, with the arguments from the macro invocation substituted for the parameters in the macro prototype.• Can a label appear in the body of a macro? Yes

8

Macro expansion (cont’d)

After macro processing, the expanded file can be used as input to the assembler.

Macro processor AssemblerSource program

Expanded file

Object program

5

10

Macro processor algorithm and data structures

A simple two-pass macro processor• all macro definitions are processed during the first pass• all macro invocation statements are expanded during

the second passHowever, such a simple two-pass macro processor would not allow definitions of macros within other macros• 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

6

11

Definitions of macros within other macrosIf MACROS is invoked, the macros RDBUFF and WRBUFF for SIC version are expanded.

If MACROX is invoked, the macros RDBUFF and WRBUFF for SIC version are expanded.

12

One-pass macro processor

A one-pass macro processor that can alternate between macro definition and macro expansion is able to handle macros like definitions of macros within other macros.• Requirement:

The definition of a macro must appear in the source program before any statements that invoke that macro.

7

13

Three main data structuresDEFTABThe macro definitions are stored in a definition table (DEFTAB), which contains the macro prototype and the statements that make up themacro body.NAMTABThe macro names are enter into NAMTAB, which serves as an index to DEFTAB. For each macro instruction defined NAMTAB contains pointers to the beginning and end of the definition in DEFTAB.ARGTABAn argument table (ARGTAB) is used during the expansion of macroinvocations. When a macro invocation statement is recognized, the arguments are stored in ARGTAB according to their position in the argument list. As the macro is expanded, arguments from ARGTAB are substituted for the corresponding parameters in the macro body.

Invocation of RDBUFF on line 190

The parameter &DEV has been converted to ?1, &BUFADR has been converted to ?2, and &RECLTH has been converted to ?3.

The parameter &DEV has been converted to ?1, &BUFADR has been converted to ?2, and &RECLTH has been converted to ?3.

8

15

Algorithms

16

Algorithms (cont’d)

for MACRO-MEND pairs

9

17

Algorithms (cont’d)

GETLINE reads a line either from input file or from DEFTAB.

GETLINE reads a line either from input file or from DEFTAB.

A macro definition

Macro invocation

MACRO-MEND pair

10