macro processors

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

Upload: kylar

Post on 11-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Macro Processors. Chapter 4 System Software An introduction to systems programming Leland L. Beck. 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 ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Macro Processors

1

Macro Processors

Chapter 4

System SoftwareAn introduction to systems programming

Leland L. Beck

Page 2: Macro Processors

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: Macro Processors

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: Macro Processors

4

Macro Definition

copy code parameter substitution conditional macro expansion macro instruction defining macros

Page 5: Macro Processors

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: Macro Processors

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: Macro Processors

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: Macro Processors

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: Macro Processors

9

Macro processor algorithm and data structures

Macro definition within macros» process macro definition during expansion time

Example 4.3(nested macro definition)

Page 10: Macro Processors

10

One-Pass Macro Processor Data Structures -- Global Variables

DEFTAB NAMTAB ARGTAB

EXPANDING

Page 11: Macro Processors

11

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 12: Macro Processors
Page 13: Macro Processors

13

Page 14: Macro Processors

14

Page 15: Macro Processors

15

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 16: Macro Processors

16

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 17: Macro Processors

17

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 18: Macro Processors

18

Machine Independent Macro Processor Features

Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro parameters

Page 19: Macro Processors

19

Concatenation of Macro Parameters

Pre-concatenation» LDA X&ID1

Post-concatenation» LDA X&ID1

Example: Figure 4.6

Page 20: Macro Processors

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: Macro Processors

21

Page 22: Macro Processors

22

RDBUFF F1, BUFFER, LENGTH

Page 23: Macro Processors

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: Macro Processors
Page 25: Macro Processors

RDBUFF F3, BUF, RECL, 04, 2048

RDBUFF 0E, BUFFER, LENGTH, , 80

Page 26: Macro Processors

RDBUFF F1, BUFF, RLENG, 04

Page 27: Macro Processors

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: Macro Processors

28

Parameter Substitution

Dummy arguments» Positional argument

STRG DATA1, DATA2, DATA3

GENER ,,DIRECT,,,,,,3

» Keyword argument fig 4.10STRG &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 29: Macro Processors

29

Macro Processor Design Options

Recursive Macro Expansion General Purpose Macro Processors Macro Processing within Language Translators

Page 30: Macro Processors

30

Recursive Macro Expansion 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 31: Macro Processors

31

ARGTAB

DEFINE

EXPAND

DEFTAB

NAMTAB

ARGTAB

MACRO Definition

Macro Invocation

PROCESSLINE

GETLINE

Page 32: Macro Processors

32

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 33: Macro Processors

33

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 34: Macro Processors

34

Implementation Examples

Fig 4.12 Fig 4.13 MASM Macro Processor ANSI C Language

Page 35: Macro Processors

35

General Purpose Macro Processor

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

Jun. 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 36: Macro Processors

36

ANSI C Macro Language

Page 37: Macro Processors

37

ANSI C Macro Language

Definitions and invocations of macros are handled by a preprocessor, which is generally not integrated with the rest of the compiler.

Examples:#define NULL 0

#define EOF (-1)

#define EQ == syntactic modification

#define ABSDIFF (X,Y) ( (X)>(Y) ? (X)-(Y) : (Y)-(X) )

Page 38: Macro Processors

38

ANSI C Macro Language

Parameter substitutions are not performed within quoted strings:#define DISPLAY(EXPR) printf(“EXPR= %d\n”, EXPR)» Macro expansion example

DISPLAY(I*J+1) printf(“EXPR= %d\n”, I*J+1) A special “stringizing” operator, #, can be used to perform argument

substitution in quoted strings: #define DISPLAY(EXPR) printf(#EXPR “= %d\n”, EXPR)

» Macro expansion example

DISPLAY(I*J+1) printf(“I*J+1” “= %d\n”, I*J+1)

Page 39: Macro Processors

39

ANSI C Macro Language

Recursive macro definitions or invocations» After a macro is expanded, the macro processor rescans the text

that has been generated, looking for more macro definitions or invocations.

» Macro cannot invoke or define itself recursively.

DISPLAY(ABSDIFF(3,8))

printf(“ABSDIFF(3,8)” “= %d\n”, ABSDIFF(3,8))

printf(“ABSDIFF(3,8)” “= %d\n”, ( (3)>(8) ? (3)-(8) : (8)-(3) ))rescan

scan

Page 40: Macro Processors

40

ANSI C Macro Language

Conditional compilation statements Example 1:

#ifndef BUFFER_SIZE

#define BUFFER_SIZE 1024

#endif

Example 2:

#define DEBUG 1

:

#if DEBUG == 1

printf(…) /* debugging outout */

#endif

Page 41: Macro Processors

41

ANSI C Macro Language

Miscellaneous functions of the preprocessor of ANSI C» Trigraph sequences are replaced by their single-character

equipments, e.g., ??< {

» Any source line that ends with a backlash, \, and a newline is spliced together with the following line.

» Any source files included in response to an #include directive are processed.

» Escape sequences are converted e.g., \n, \0

» Adjacent string literals are concatenated, e.g., “hello,” “world” “hello, world”.

Page 42: Macro Processors

42

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