Top Banner
1 Macro Processors Chapter 4 System Software An introduction to systems programming Leland L. Beck
35

Macros PPt

Oct 29, 2014

Download

Documents

Manoj Bojja

System software macros ppt
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Macros PPt

1

Macro Processors

Chapter 4

System SoftwareAn introduction to systems programming

Leland L. Beck

Page 2: Macros PPt

2

Introduction

Concept» A macro instruction is a notational convenience for the

programmer» It allows the programmer to write shorthand version of a

program (module programming)» The macro processor replaces each macro invocation with

the corresponding sequence of statements (expanding)

Page 3: Macros PPt

3

Macro Processor

Recognize macro definitions Save the macro definition Recognize macro calls Expand macro calls

Source Code

(with macro)

Macro Processor

Expanded Code

Compiler or Assembler

obj

Page 4: Macros PPt

4

Macro Definition

copy code parameter substitution conditional macro expansion macro instruction defining macros

Page 5: Macros PPt

5

Copy code -- Example

Source

STRG MACRO

STA DATA1

STB DATA2

STX DATA3

MEND

.

STRG

.

STRG

.

.

Expanded source

.

.

.

STA DATA1

STB DATA2

STX DATA3

.

STA DATA1

STB DATA2

STX DATA3

.

{{

Page 6: Macros PPt

6

Macro vs. Subroutine

Macro» the statement of expansion are generated each time the

macro are invoked

Subroutine» the statement in a subroutine appears only once

Page 7: Macros PPt

7

Parameter Substitution -- Example

Source

STRG MACRO &a1, &a2, &a3

STA &a1

STB &a2

STX &a3

MEND

.

STRG DATA1, DATA2, DATA3

.

STRG DATA4, DATA5, DATA6

.

.

Expanded souce

.

.

.

STA DATA1

STB DATA2

STX DATA3

.

STA DATA4

STB DATA5

STX DATA6

.

{{

Page 8: Macros PPt

8

Parameter Substitution

Dummy arguments» Positional argument

STRG DATA1, DATA2, DATA3

GENER ,,DIRECT,,,,,,3

» Keyword argumentSTRG &a3=DATA1, &a2=DATA2, &a1=DATA3

GENER TYPE=DIRECT, CHANNEL=3

Example: Fig. 4.1, Fig. 4.2» Labels are avoided in macro definition

Page 9: Macros PPt

9

One-Pass Macro Processor

Prerequisite» every macro must be defined before it is called

Sub-procedures» macro definition: DEFINE» macro invocation: EXPAND

DEFINE

EXPAND

PROCESSLINE

DEFTAB

NAMTAB

ARGTAB

MACRO

CALL

Page 10: Macros PPt
Page 11: Macros PPt

11

Data Structures -- Global Variables

DEFTAB NAMTAB ARGTAB

EXPANDING

Page 12: Macros PPt

12

Nested Macros Definition

Macro definition within macros» process macro definition during expansion time

Example 4.3

Page 13: Macros PPt

13

Figure 4.3 (b)

Page 14: Macros PPt

14

One-Pass Macro Processor That Allows Nested Macro Definition

Sub-procedures» macro definition: DEFINE» macro invocation: EXPAND

EXPAND may invoke DEFINE when encounter macro definition

DEFINE

EXPAND

PROCESSLINEDEFTAB

NAMTAB

ARGTAB

Expanding

MACRO

CALL

MACRO Definition

Page 15: Macros PPt
Page 16: Macros PPt
Page 17: Macros PPt

17

1-Pass Macro Processor

M AC R OPR O C ESSO R

G ETLIN EPR O C ESSLIN E

EXPANDING=FALSE

PR O C ESSLIN E

G ETLIN E

EXPAN D IN G

TRUE

R EAD FR O MIN PU T

R EAD FR O MD EFTAB

FALSE

D EFIN E

EXPAN D

EXPANDING=TRUE

G ETLIN EPR O C ESSLIN E

G ETLIN E

Page 18: Macros PPt

18

Comparison of Macro Processors Design

Single pass» every macro must be defined before it is called» one-pass processor can alternate between macro definition

and macro expansion» nested macro definitions may be allowed but nested calls

are not

Two pass algorithm» Pass1: Recognize macro definitions» Pass2: Recognize macro calls» nested macro definitions are not allowed

Page 19: Macros PPt

19

Concatenation of Macro Parameters

Pre-concatenation» LDA X&ID1

Post-concatenation» LDA X&ID1

Example: Figure 4.6

Page 20: Macros PPt

20

Generation of Unique Labels

Example» JEQ *-3» inconvenient, error-prone, difficult to read

Example Figure 4.7– $LOOP TD =X’&INDEV’

» 1st call: – $AALOOP TD =X’F1’

» 2nd call:– $ABLOOP TD =X’F1’

Page 21: Macros PPt
Page 22: Macros PPt

RDBUFF F1, BUFFER, LENGTH

Page 23: Macros PPt

23

Conditional Macro Expansion

Macro-time conditional statements» Example: Figure 4.8» IF-ELSE-ENDIF

Macro-time variables» any symbol that begins with the character & and that is not a

macro parameter» macro-time variables are initialized to 0» macro-time variables can be changed with their values using

SET– &EORCK SET 1

Page 24: Macros PPt
Page 25: Macros PPt

RDBUFF F3, BUF, RECL, 04, 2048

RDBUFF 0E, BUFFER, LENGTH, , 80

Page 26: Macros PPt

RDBUFF F1, BUFF, RLENG, 04

Page 27: Macros PPt

27

Conditional Macro Expansion (Cont.)

Macro-time looping statement» Example: Figure 4.9» WHILE-ENDW

Macro processor function» %NITEMS: THE NUMBER OF MEMBERS IN AN

ARGUMENT LIST

Page 28: Macros PPt

28

Nested Macro Invocations

Macro invocations within macros» process macro invocation during expansion time

Recursive macro expansion» Example: Figure 4.11» Problems:

– ARGTAB

– EXPANDING

» Solution– Recursive call

– While loop with stack

Page 29: Macros PPt

29

ARGTAB

DEFINE

EXPAND

DEFTAB

NAMTAB

ARGTAB

MACRO Definition

Macro Invocation

PROCESSLINE

GETLINE

Page 30: Macros PPt

30

1-Pass Macro Processor

M AC R OPR O C ESSO R

G ETLIN EPR O C ESSLIN E

EXPANDING=FALSE

PR O C ESSLIN E

G ETLIN E

EXPAN D IN G

TRUE

R EAD FR O MIN PU T

R EAD FR O MD EFTAB

FALSE

D EFIN E

EXPAN D

EXPANDING=TRUE

G ETLIN EPR O C ESSLIN E

G ETLIN E

Page 31: Macros PPt

31

Allowing Nested Macro Invocation

M AC R OPR O C ESSO R

G ETLIN E(0)PR O C ESSLIN E(0)

PR O C ESSLIN E(f)

G ETLIN E(f)

f

TRUE

R EAD FR O MIN PU T

R EAD FR O MD EFTAB

FALSE

D EFIN E(f)

EXPAN D

G ETLIN E(1)PR O C ESSLIN E(1)

G ETLIN E(f)

Page 32: Macros PPt

32

Macro-Assembler

Advantage» reduce 1 pass» share same data structure

Disadvantage» more complex

Page 33: Macros PPt

READ

Search (Pseudo-Op Table)

Search NAMTAB(Macro Name Table)

Search (Machine Op Table)

Process machine

instruction

R

R

Type?

MACRODefine

Processpseudo-ops

R R

MACROExpand

R

Pass 2

Pass 1

Page 34: Macros PPt

34

General Purpose Macro Processor

ELENA» Software: Practice and Experience, Vol. 14, pp. 519-531, Ju

n. 1984

Macro definition» header:

– a sequence of keywords and parameter markers (%)

– at least one of the first two tokens in a macro header must be a keyword, not a parameter marker

» body: – the character & identifies a local label

– macro time instruction (.SET, .IF .JUMP, .E)

– macro time variables or labels (.)

Page 35: Macros PPt

35

ELENA (cont.)

Macro invocation» There is no single token that constitutes the macro “name”» Constructing an index of all macro headers according to the

keywords in the first two tokens of the header» Example

– DEFINITION: ADD %1 TO %2 ADD %1 TO THE FIRST ELEMENT OF %2

– INVOCATION: DISPLAY TABLE

DISPLAY %1

%1 TABLE