4.2 machine-independent macro processor featuresosnet.cs.nchu.edu.tw/powpoint/sp93_2/chapter...

Post on 15-Mar-2020

17 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

4.2 Machine-Independent Macro Processor Featureso Extensions to the basic macro processor

functionsn Concatenation of Macro Parameters

n Generation of Unique Labels

n Conditional Macro Expansion

n Keyword Macro Parameters

2

4.2.1 Concatenation of Macro Parameterso Allow parameters to be concatenated with

other character stringso Suppose the parameter is named &ID, the

macro body may contain a statement:LDA X&ID1

n &ID is concatenated after the string “X” and before the string “1”.à LDA XA1 (&ID=A)à LDA XB1 (&ID=B)

3

4.2.1 Concatenation of Macro Parameterso Problem: ambiguous situation

n E.g., X&ID1 may meano “X” + &ID + “1”o “X” + &ID1

n This problem occurs because the end of the parameter is not marked.

o Solution:n Use a special concatenation operator “->” to specify the

end of the parametern E.g. LDA X&ID→1

o Example: Figure 4.6

Beginning of the macro parameter is identified by &, the end of the

parameter is not marked.

4

Concatenation of Macro Parameters (Fig. 4.6)

5

4.2.2 Generation of Unique Labelso Labels in the macro body may have duplicate

labels problem n If the macro is invocated multiple times.n Use of relative addressing is very inconvenient,

error-prone, and difficult to read.o Example

n JEQ *-3n Inconvenient, error-prone, difficult to read

6

4.2.2 Generation of Unique Labelso Generating unique labels within macro expansions

n Labels within the macro body begin with the character $n During macro invocation, $ will be replaced by $xx,

o xx is a two-character alphanumeric counter of the number of macro instructions expanded.

o Example: Figure 4.7n $LOOP TD =X’&INDEV’n 1st call:

o $AALOOP TD =X’F1’n 2nd call:

o $ABLOOP TD =X’F1’

7

Generation of unique labels within macro expansion (fig. 4.7)

•Macro definition

8

Generation of unique labels within macro expansion (fig. 4.7) (Cont.)RDBUFF F1, BUFFER, LENGTH •Macro expansion

9

4.2.3 Conditional Macro Expansiono Arguments in macro invocation can be used

to:n Substitute the parameters in the macro body.

n Modify the sequence of statements in macro body for conditional macro expansion.o This capability adds greatly to the power and

flexibility of a macro language.

10

4.2.3 Conditional Macro Expansiono Macro-time conditional statements

n Macro processor directives:o IF-ELSE-ENDIFo SET

n Example: Figure 4.8

o Macro-time variables (also called a set symbol)n Be used to store working values during the macro expansionn Any symbol that begins with the character & and is not a macro

parametern Be initialized to 0n Be changed with their values using SET

o &EORCK SET 1

11

Boolean expression

Macro-time variable

IF-ELSE-ENDIF Structure

12

Use of Macro-time Conditional Statements (Fig. 4.8) (Cont.)

RDBUFF F3, BUF, RECL, 04, 2048

13

Use of Macro-time Conditional Statements (Fig. 4.8) (Cont.)

RDBUFF 0E, BUFFER, LENGTH, , 80

14

Use of Macro-time Conditional Statements (Fig. 4.8) (Cont.)

RDBUFF F1, BUFF, RLENG, 04

15

Conditional Macro Expansion (Cont.)o The testing of Boolean expression in IF statements

occurs at the time macros are expanded. n By the time the program is assembled, all such decisions

have been made.n There is only one sequence of source statements during

program execution.

o In contrast, the COMPR instruction tests data values during program execution. n The sequence of statements that are executed during

program execution may be different.

16

Conditional Macro Expansion (Cont.)o Macro-time looping statement

n Macro processor directives:o WHILE-ENDW

n Example: Figure 4.9o Macro processor function

n %NITEMS: the number of members in an argument listo E.g. &EOR=(00,03,04)

=> %NITEMS(&EOR) is 3o Specify member in the list: &EOR[1]

17

Use of Macro-time Looping Statements (Fig. 4.9)

Macro processor function

Macro-time looping statement

18

Use of Macro-time Looping Statements (Fig. 4.9) (Cont.) A list of end-of-

record characters

19

4.2.4 Keyword Macro Parameterso Positional parameters

n Parameters and arguments are associated according to their positions in the macro prototype and invocation.

n If an argument is to be omitted, a null argument (two consecutive commas) should be used to maintain the proper order in macro invocation:o E.g. RDBUFF 0E, BUFFER, LENGTH, , 80

n It is not suitable if a macro has a large number of parameters, and only a few of these are given values in a typical invocation.

20

4.2.4 Keyword Macro Parameters (Cont.)o Keyword parameters

n Each argument value is written with a keyword that names the corresponding parameter.

n Arguments may appear in any order.o Null arguments no longer need to be used.

n E.g. o GENER TYPE=DIRECT, CHANNEL=3

n It is easier to read and much less error-prone than the positional method.

n E.g. Fig. 4.10

21

Use of keyword parameters in macro instructions (Fig. 4.10)

Default values of parameters

22

Use of keyword parameters in macro instructions (Fig. 4.10) (Cont.)

23

Use of keyword parameters in macro instructions (Fig. 4.10) (Cont.)

24

4.3 Macro Processors Design Optionso Recursive macro expansion

o General-purpose macro processors

o Macro processing within language translators

25

4.3.1 Recursive Macro Expansiono Recursive macro expansion

n Macro invocations within macrosn Example: Figure 4.11

o Problems in previous macro processor design : n Values in ARGTAB were overwritten

o The procedure EXPAND would be called recursivelyo Thus the invocation arguments in the ARGTAB will be

overwritten.n Recursive call of the procedure EXPANDING

o The Boolean variable EXPANDING would be set to FALSE when the “inner” macro expansion is finished

o That is, the macro process would forget that it had been in the middle of expanding an “outer” macro.

26

Example of nested macro invocation (Fig. 4.11)

RDCHAR is also a macro

27

Example of nested macro invocation (Fig. 4.11) (Cont.)

28

4.3.1 Recursive Macro Expansion (Cont.)o Solutions:

n Write the macro processor in a programming language that allows recursive callso Thus local variables will be retained.o Most high-level language have been supported recursive callso The compiler would be sure that previous values of any variables

declared within a procedure were saved when the procedure was called recursively

n Use a stack to take care of pushing and popping local variables and return addresses

29

4.3.2 General-Purpose Macro processorso 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

o Not tied to any particular languageo Can be used with a variety of different languages.

Source Code

(with macro)

Macro Processor

Expanded Code

Compiler /Assembler

Objectprogram

30

General-Purpose Macro processors (Cont.)o General-purpose macro processors

n Advantageso Programmers do not need to learn many macro languages.o Overall saving in software development cost and software

maintenance effortn Difficulties:

o Large number of details must be dealt with in a real programming languagen Comment identifications ( //, /* */, …)n Grouping together terms, expressions, statements (begin_end,

{ }, …)n Tokens (keywords, operators)n …

31

4.3.3 Macro processing within language translatorso Macro processors can be

n Preprocessorso Produce an expanded version of the source program, which is

then used as input to an assembler or compiler

n Line-by-line macro processoro Used as a sort of input routine for the assembler or compiler

n Read source programn Process macro definitions and expand macro invocationsn Pass output lines to the assembler or compiler

n Integrated macro processor

32

4.3.3 Macro processing within language translatorso Preprocessors

o Combining macro processing functions with the language translator itself

Source Code

(with macro)

Macro Processor

Compiler /Assembler

Objectprogram

Source Code

(with macro)

Macro Processor

Expanded Code

Compiler /Assembler

Objectprogram

33

Macro processing within language translators (Cont.)o Combining macro processing functions with

the language translator itselfn Line-by-line macro processor: n Integrated macro processor

n Advantages: share some data structures, functionsn Disadvantages: more complex

34

Line-by-Line Macro Processoro Benefits

n It avoids making an extra pass over the source program.n Data structures required by the macro processor and the language

translator can be combinedo E.g., OPTAB and NAMTAB)

n Utility subroutines can be used by both macro processor and the language translator.o Scanning input lineso Searching tableso Data format conversion

n It is easier to give diagnostic messages related to the source statements.o i.e., the source statement error can be quickly identified without

need to backtrack the source

35

Integrated Macro Processoro Integrate a macro processor with a language

translator (e.g., compiler)o Advantages

n An integrated macro processor can potentially make use of any information about the source program that is extracted by the language translator.

n An integrated macro processor can support macro instructions that depend upon the context in which they occur.

n Since the Macro Processor may recognize the meaning of source language

36

Drawbacks of Line-by-line or Integrated Macro Processoro They must be specially designed and writtenn To work with a particular implementation of an

assembler or compiler.o The costs of macro processor development is

added to the costs of the language translatorn Which results in a more expensive software.

o The assembler or compiler will be considerably larger and more complex.

37

4.4 Implementation Exampleso MASM Macro Processor

o ANSI C Macro Language

o The ELENA Macro Processor

38

4.4.1 MASM Macro Processoro Macro processor in Microsoft MASM

assemblern Integrated with pass 1 of the assemblern Supports all of the main macro processor

functions discussed previouslyn Iteration statement:

o E.g. IRP S, <‘LEFT’,’DATA’,’RIGHT’>

39

Examples of MASM macro and conditional statements (Fig. 4.12)

40

Examples of MASM macro and conditional statements (Fig. 4.12) (Cont.)

41

Examples of MASM iteration statements (Fig. 4.13)

42

4.4.2 ANSI C Macro Languageo Definitions and invocations of macros are

handled by a preprocessor.n Simply makes string substitutions, without

considering the syntax of the Co Macro definition:n E.g. #define NULL 0n E.g. #define AB(X,Y) ( X > Y ? X – Y : Y – X )

o Macro invocationn E.g. AB(3,4)

43

4.4.3 The ELENA Macro Processoro ELENA

n A research tool, not as a commercial software product.n Software: Practice and Experience, Vol. 14, pp. 519-531, Jun. 1984

o Macro definitions are composed of a header and a body.n header:

o a sequence of keywords and parameter markers (%)o at least one of the first two tokens in a macro header must be a keyword,

not a parameter markern body:

o the character & identifies a local labelo macro time instruction (.SET, .IF .JUMP, .E)o macro time variables or labels (.)

44

Examples of ELENA macro definition and invocation (Fig. 4.14)

•Macro definition(header)•Macro definition(body)

•Macro invocation

•Macro expansion

45

Examples of ELENA macro definition and invocation (Fig. 4.14) (Cont.)

•Macro definition(body)

•Macro invocation

•Macro expansion

46

Examples of ELENA macro-time instructions (Fig. 4.15)

47

The ELENA Macro Processor (Cont.)o Macro invocation

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

in the first two tokens of the headern ELENA selects the header with the fewest parameters if there are two

or more matching headers with the same number of parameters, themost recently defined macro is selected.

n Examples:o Macro definition:

n ADD %1 TO %2n ADD %1 TO THE FIRST ELEMENT OF %2

o Macro invocation: n DISPLAY TABLE for DISPLAY %1 or %1 TABLEn A=B+1 for %1=%2+%3 or %1=%2+1

top related