Anda di halaman 1dari 3

Macros and Macro Processors Examples of macro calls: These examples

use the macros defined in Handout 9.1.


Ref: Chapter 4 of [Beck].
SAVREG LOC
Macros: GETBYT A0,CBUF,LOC

• Abbreviations which are expanded by the macro Notes:


processor (also called a macro preprocessor).
• In a macro call, the name of the macro ap-
• Can also be used with assembly languages. pears in the opcode field.
• Improve readability and facilitate program • The actual parameters are specified in the operand
modification. field.
Macros in SIC/XE: • The correspondence between dummy param-
eters and actual parameters is by position.
• Examples: See Handout 9.1.
• Note the use of LC-relative addressing in macros
• New directives MACRO and MEND. (Example 2 of Handout 9.1). This is to avoid
using labels in macro definitions. (Why?)
• The MACRO directive specifies all the dummy
parameters.
• Dummy parameters start with ’&’.

9–1 9–2
Design of a simple macro processor: • Modifications to improve efficiency of macro
expansion: To be discussed in class.
Assumptions:
Carrying out expansion (Step 2.2(a)):
(a) All macro definitions appear at the
beginning. • See Handout 9.2.

(b) The body of a macro does not contain an- Extending the simple design:
other macro definition or call.
I. Handling use-before-definition:
High-level Outline: See Handout 9.2.

Notes regarding the outline: • Definitions and call may occur in any order.

• Step 1: Need to choose a data structure. • Assume no nested definitions or nested calls.

• Steps 2.1 and 2.2(b): Trivial. • Two pass macro processor.

• Step 2.2(a): To be discussed. • Pass 1: Collect all macro definitions and


construct MNT.
Data structure for Step 1:
• Pass 2: Carry out expansion.
• Macro Name Table (MNT): Stores the macro • The definition-before-use restriction allows
bodies. one-pass design.
• Implementation of MNT: To be discussed in • The definition-before-use restriction is
class. reasonable for macros.
9–3 9–4
II. Handling nested definitions: • Algorithm outlined in Figure 4.5 of [Beck].

• Example: Handout 9.3. – Boolean flag EXPANDING to remember whether


the program is handling definition or
• To read in nested definitions: expansion.
– Need to match MACRO and MEND directives. – Procedure GETLINE obtains next line either
– Similar to matching parentheses in arith- from the input file or from MNT depending
metic expressions. on the value of EXPANDING.
– Can be done using a level counter (or a – Procedure PROCESSLINE calls DEFINE when
stack). a definition is seen.
• Macro expansion may cause new macros to be – Procedure DEFINE maintains the level counter.
defined.
• Example: Consider a call to MACS (or MACX)
in Handout 9.3.
• Important: Macros RDBUF and WRBUF in
Handout 9.3 get defined only after a call to
MACS (or MACX).
• Macro processor may need to alternate be-
tween definition phase and expansion phase.

9–5 9–6

Anda mungkin juga menyukai