4.2 Machine-Independent Macro Processor Features

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

4.

2 Machine-Independent Macro
Processor Features
o Extensions to the basic macro processor
functions
n Concatenation of Macro Parameters

n Generation of Unique Labels

n Conditional Macro Expansion

n Keyword Macro Parameters


1
4.2.1 Concatenation of Macro
Parameters
o Allow parameters to be concatenated with
other character strings
o 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)
2
4.2.1 Concatenation of Macro
Parameters
o Problem: ambiguous situation
n E.g., X&ID1 may mean Beginning of the macro parameter
o “X” + &ID + “1” is identified by &, the end of the
o “X” + &ID1 parameter is not marked.
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 parameter
n E.g. LDA X&ID®1
o Example: Figure 4.6

3
Concatenation of Macro Parameters
(Fig. 4.6)

4
4.2.2 Generation of Unique Labels
o 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 *-3
n Inconvenient, error-prone, difficult to read

5
4.2.2 Generation of Unique Labels
o 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.7
n $LOOP TD =X’&INDEV’
n 1st call:
o $AALOOP TD =X’F1’
n 2nd call:
o $ABLOOP TD =X’F1’

6
Generation of unique labels within
macro expansion (fig. 4.7)
•Macro definition

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

8
4.2.3 Conditional Macro Expansion
o 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.

9
4.2.3 Conditional Macro Expansion
o Macro-time conditional statements
n Macro processor directives:
o IF-ELSE-ENDIF
o 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 expansion
n Any symbol that begins with the character & and is not a macro
parameter
n Be initialized to 0
n Be changed with their values using SET
o &EORCK SET 1

10
IF-ELSE-ENDIF Structure

Macro-time
variable

Boolean expression

11
Use of Macro-time Conditional
Statements (Fig. 4.8) (Cont.)
RDBUFF F3, BUF, RECL, 04, 2048

12
Use of Macro-time Conditional
Statements (Fig. 4.8) (Cont.)
RDBUFF 0E, BUFFER, LENGTH, , 80

13
Use of Macro-time Conditional
Statements (Fig. 4.8) (Cont.)
RDBUFF F1, BUFF, RLENG, 04

14
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.
15
Conditional Macro Expansion (Cont.)
o Macro-time looping statement
n Macro processor directives:
o WHILE-ENDW
n Example: Figure 4.9
o Macro processor function
n %NITEMS: the number of members in an argument list
o E.g. &EOR=(00,03,04)
=> %NITEMS(&EOR) is 3
o Specify member in the list: &EOR[1]

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

Macro-time looping
statement

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

18
4.2.4 Keyword Macro Parameters
o 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.

19
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
20
Use of keyword parameters in macro
instructions (Fig. 4.10)
Default values of parameters

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

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

23
4.3 Macro Processors Design Options
o Recursive macro expansion

o General-purpose macro processors

o Macro processing within language translators

24
4.3.1 Recursive Macro Expansion
o Recursive macro expansion
n Macro invocations within macros
n Example: Figure 4.11
o Problems in previous macro processor design :
n Values in ARGTAB were overwritten
o The procedure EXPAND would be called recursively
o 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.
25
Example of nested macro invocation
(Fig. 4.11)

RDCHAR is also a macro

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

27
4.3.1 Recursive Macro Expansion
(Cont.)
o Solutions:
n Write the macro processor in a programming language
that allows recursive calls
o Thus local variables will be retained.
o Most high-level language have been supported recursive calls
o 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

28
4.3.2 General-Purpose Macro
processors
o Three examples of actual macro processors:
n A macro processor designed for use by assembler
language programmers
n Used with a high-level programming language
n General-purpose macro processor
o Not tied to any particular language
o Can be used with a variety of different languages.

Source Macro Expanded Compiler Object


Code Processor Code /Assembler program
(with macro)
29
General-Purpose Macro processors
(Cont.)
o General-purpose macro processors
n Advantages
o Programmers do not need to learn many macro languages.
o Overall saving in software development cost and software
maintenance effort
n Difficulties:
o Large number of details must be dealt with in a real
programming language
n Comment identifications ( //, /* */, …)
n Grouping together terms, expressions, statements (begin_end,
{ }, …)
n Tokens (keywords, operators)
n …

30
4.3.3 Macro processing within
language translators
o Macro processors can be
n Preprocessors
o Produce an expanded version of the source program, which is
then used as input to an assembler or compiler
n Line-by-line macro processor
o Used as a sort of input routine for the assembler or compiler
n Read source program
n Process macro definitions and expand macro invocations
n Pass output lines to the assembler or compiler
n Integrated macro processor

31
4.3.3 Macro processing within
language translators
o Preprocessors
Source Macro Expanded Compiler Object
Code Processor Code /Assembler program
(with macro)

o Combining macro processing functions with the


language translator itself
Source Macro Compiler Object
Code Processor /Assembler program
(with macro)

32
Macro processing within language
translators (Cont.)
o Combining macro processing functions with
the language translator itself
n Line-by-line macro processor:
n Integrated macro processor

n Advantages: share some data structures, functions


n Disadvantages: more complex

33
Line-by-Line Macro Processor
o 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 combined
o E.g., OPTAB and NAMTAB)
n Utility subroutines can be used by both macro processor and the
language translator.
o Scanning input lines
o Searching tables
o 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

34
Integrated Macro Processor
o 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
35
Drawbacks of Line-by-line or
Integrated Macro Processor
o They must be specially designed and written
n 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 translator
n Which results in a more expensive software.
o The assembler or compiler will be
considerably larger and more complex.
36
4.4 Implementation Examples
o MASM Macro Processor

o ANSI C Macro Language

o The ELENA Macro Processor

37
4.4.1 MASM Macro Processor
o Macro processor in Microsoft MASM
assembler
n Integrated with pass 1 of the assembler
n Supports all of the main macro processor
functions discussed previously
n Iteration statement:
o E.g. IRP S, <‘LEFT’,’DATA’,’RIGHT’>

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

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

40
Examples of MASM iteration
statements (Fig. 4.13)

41
4.4.2 ANSI C Macro Language
o Definitions and invocations of macros are
handled by a preprocessor.
n Simply makes string substitutions, without
considering the syntax of the C
o Macro definition:
n E.g. #define NULL 0
n E.g. #define AB(X,Y) ( X > Y ? X – Y : Y – X )
o Macro invocation
n E.g. AB(3,4)
42
4.4.3 The ELENA Macro Processor
o 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 marker
n body:
o the character & identifies a local label
o macro time instruction (.SET, .IF .JUMP, .E)
o macro time variables or labels (.)

43
Examples of ELENA macro definition
and invocation (Fig. 4.14)
•Macro definition
(header)
•Macro definition
(body)

•Macro invocation

•Macro expansion

44
Examples of ELENA macro definition
and invocation (Fig. 4.14) (Cont.)
•Macro definition
(body)

•Macro invocation

•Macro expansion

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

46
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 header
n ELENA selects the header with the fewest parameters if there are two
or more matching headers with the same number of parameters, the
most recently defined macro is selected.
n Examples:
o Macro definition:
n ADD %1 TO %2
n ADD %1 TO THE FIRST ELEMENT OF %2
o Macro invocation:
n DISPLAY TABLE for DISPLAY %1 or %1 TABLE
n A=B+1 for %1=%2+%3 or %1=%2+1
47

You might also like