Macro Processors Chapter 5 1 Introduction l Concept

  • Slides: 37
Download presentation
Macro Processors Chapter 5 1

Macro Processors Chapter 5 1

Introduction l Concept » A macro instruction is a notational convenience for the programmer

Introduction l 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) 2

Macro Processor l l Recognize macro definitions Save the macro definition Recognize macro calls

Macro Processor l l 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 3

Macro Definition l l copy code parameter substitution conditional macro expansion macro instruction defining

Macro Definition l l copy code parameter substitution conditional macro expansion macro instruction defining macros 4

Copy code -- Example Source STRG MACRO STA DATA 1 STB DATA 2 STX

Copy code -- Example Source STRG MACRO STA DATA 1 STB DATA 2 STX DATA 3 MEND. STRG. . Expanded source. . . STA DATA 1 STB DATA 2 STX DATA 3. { { 5

Macro vs. Subroutine l Macro » the statement of expansion are generated each time

Macro vs. Subroutine l Macro » the statement of expansion are generated each time the macro are invoked l Subroutine » the statement in a subroutine appears only once 6

Parameter Substitution -- Example Source STRG MACRO &a 1, &a 2, &a 3 STA

Parameter Substitution -- Example Source STRG MACRO &a 1, &a 2, &a 3 STA &a 1 STB &a 2 STX &a 3 MEND. STRG DATA 1, DATA 2, DATA 3. STRG DATA 4, DATA 5, DATA 6. . Expanded souce. . . STA STB STX. { { DATA 1 DATA 2 DATA 3 DATA 4 DATA 5 DATA 6 7

Parameter Substitution l Dummy arguments » Positional argument STRG DATA 1, DATA 2, DATA

Parameter Substitution l Dummy arguments » Positional argument STRG DATA 1, DATA 2, DATA 3 GENER , , DIRECT, , , 3 » Keyword argument STRG &a 3=DATA 1, &a 2=DATA 2, &a 1=DATA 3 GENER TYPE=DIRECT, CHANNEL=3 l Example: Fig. 4. 1, Fig. 4. 2 » Labels are avoided in macro definition 8

One-Pass Macro Processor l Prerequisite » every macro must be defined before it is

One-Pass Macro Processor l Prerequisite » every macro must be defined before it is called l Sub-procedures » macro definition: DEFINE » macro invocation: EXPAND NAMTAB MACRO DEFINE DEFTAB CALL EXPAND ARGTAB PROCESSLINE 9

Data Structures -- Global Variables l l l DEFTAB NAMTAB ARGTAB EXPANDING 11

Data Structures -- Global Variables l l l DEFTAB NAMTAB ARGTAB EXPANDING 11

ARGTAB 1 F 1 2 BUFFER 3 LENGTH 12

ARGTAB 1 F 1 2 BUFFER 3 LENGTH 12

Nested Macros Definition l Macro definition within macros » process macro definition during expansion

Nested Macros Definition l Macro definition within macros » process macro definition during expansion time l Example 4. 3 13

Figure 4. 3 (b) 14

Figure 4. 3 (b) 14

One-Pass Macro Processor That Allows Nested Macro Definition l Sub-procedures » macro definition: DEFINE

One-Pass Macro Processor That Allows Nested Macro Definition l Sub-procedures » macro definition: DEFINE » macro invocation: EXPAND l EXPAND may invoke DEFINE when encounter macro definition NAMTAB DEFTAB ARGTAB Expanding MACRO DEFINE CALL EXPAND PROCESSLINE MACRO Definition 15

1 -Pass Macro Processor 18

1 -Pass Macro Processor 18

Comparison of Macro Processors Design l Single pass » every macro must be defined

Comparison of Macro Processors Design l 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 l Two pass algorithm » Pass 1: Recognize macro definitions » Pass 2: Recognize macro calls » nested macro definitions are not allowed 19

Machine Independent Macro Processor Feature 20

Machine Independent Macro Processor Feature 20

Concatenation of Macro Parameters l Pre-concatenation » LDA l Post-concatenation » LDA l X&ID

Concatenation of Macro Parameters l Pre-concatenation » LDA l Post-concatenation » LDA l X&ID 1 Example: Figure 4. 6 21

Generation of Unique Labels l Example » JEQ *-3 » inconvenient, error-prone, difficult to

Generation of Unique Labels l Example » JEQ *-3 » inconvenient, error-prone, difficult to read l Example Figure 4. 7 – $LOOP TD =X’&INDEV’ TD =X’F 1’ » 1 st call: – $AALOOP » 2 nd call: – $ABLOOP 22

RDBUFF F 1, BUFFER, LENGTH

RDBUFF F 1, BUFFER, LENGTH

Conditional Macro Expansion l Macro-time conditional statements » Example: Figure 4. 8 » IF-ELSE-ENDIF

Conditional Macro Expansion l Macro-time conditional statements » Example: Figure 4. 8 » IF-ELSE-ENDIF l 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 25

RDBUFF F 3, BUF, RECL, 04, 2048 RDBUFF 0 E, BUFFER, LENGTH, , 80

RDBUFF F 3, BUF, RECL, 04, 2048 RDBUFF 0 E, BUFFER, LENGTH, , 80

RDBUFF F 1, BUFF, RLENG, 04

RDBUFF F 1, BUFF, RLENG, 04

Conditional Macro Expansion (Cont. ) l Macro-time looping statement » Example: Figure 4. 9

Conditional Macro Expansion (Cont. ) l Macro-time looping statement » Example: Figure 4. 9 » WHILE-ENDW l Macro processor function » %NITEMS: THE NUMBER OF MEMBERS IN AN ARGUMENT LIST 29

Nested Macro Invocations l Macro invocations within macros » process macro invocation during expansion

Nested Macro Invocations l Macro invocations within macros » process macro invocation during expansion time l Recursive macro expansion » Example: Figure 4. 11 » Problems: – ARGTAB – EXPANDING » Solution – Recursive call – While loop with stack 30

ARGTAB DEFTAB MACRO Definition NAMTAB DEFINE GETLINE PROCESSLINE Macro Invocation EXPAND ARGTAB 31

ARGTAB DEFTAB MACRO Definition NAMTAB DEFINE GETLINE PROCESSLINE Macro Invocation EXPAND ARGTAB 31

1 -Pass Macro Processor 32

1 -Pass Macro Processor 32

Allowing Nested Macro Invocation 33

Allowing Nested Macro Invocation 33

Macro-Assembler l Advantage » reduce 1 pass » share same data structure l Disadvantage

Macro-Assembler l Advantage » reduce 1 pass » share same data structure l Disadvantage » more complex 34

Pass 1 R READ Search (Pseudo-Op Table) Type? Search NAMTAB (Macro Name Table) MACRO

Pass 1 R READ Search (Pseudo-Op Table) Type? Search NAMTAB (Macro Name Table) MACRO Define Process pseudo-ops Search (Machine Op Table) R R Process machine instruction MACRO Expand R R Pass 2

General Purpose Macro Processor l ELENA » Software: Practice and Experience, Vol. 14, pp.

General Purpose Macro Processor l ELENA » Software: Practice and Experience, Vol. 14, pp. 519 -531, Jun. 1984 l 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 (. ) 36

ELENA (cont. ) l Macro invocation » There is no single token that constitutes

ELENA (cont. ) l 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: l l ADD %1 TO %2 ADD %1 TO THE FIRST ELEMENT OF %2 – INVOCATION: l DISPLAY TABLE DISPLAY %1 %1 TABLE 37