Top Banner
Chapter 4 Macro Processors System Software Chih-Shun Hsu
53

Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Chapter 4 Macro Processors

System Software

Chih-Shun Hsu

Page 2: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Introduction

A macro represents a commonly used group of statements in the source programming language

The macro processor replaces each macro instruction with the corresponding group of source language statement, this is called expanding macros

The functions of a macro processor essentially involve the substitution of one group of characters or lines for another

Page 3: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Macro Definition and Expansion

The MACRO statement identifies the beginning of a macro definition

The symbol in the label field is the name of the instruction

The entries in the operand field identify the parameter of the macro instruction

Each parameter begins with the character & The MEND assembler directive marks the end of the

macro definition A macro invocation statement gives the name of the

macro instruction being invoked and the arguments to be used in expanding the macro

Page 4: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of macros in a SIC/XE Program(3/1)

Page 5: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of macros in a SIC/XE Program(3/2)

Page 6: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of macros in a SIC/XE Program(3/3)

Page 7: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Program with Macro Expanded(3/1)

Page 8: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Program with Macro Expanded(3/2)

Page 9: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Program with Macro Expanded(3/3)

Page 10: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Macro Processor Data Structures

The macro definitions themselves are stored in definition table (DEFTAB), which contains the macro prototype and the statements that make up the macro body

The macro names are entered into NAMTAB, which serves as an index to DEFTAB

For each macro instruction defined NAMTAB contains pointers to the beginning and end of the definition in DEFTAB

The third data structure is an argument table (ARGTAB), which is used during the expansion of macro invocations

When a macro invocation statement is recognized, the arguments are stored in ARGTAB according to their position in the argument list

Page 11: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Macro Processor Data Structures

Page 12: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Algorithm for a One-pass Macro Processor(3/1)

Page 13: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Algorithm for a One-pass Macro Processor(3/2)

Page 14: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Algorithm for a One-pass Macro Processor(3/3)

Page 15: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Machine-Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro Parameters

Page 16: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Concatenation of Macro Parameters(2/1) Most macro processors allow parameters to

concatenated with other character strings If similar processing is to be performed on each series of

variables, the programmer might want to incorporate this processing in to a macro instruction

The body of the macro definition might contain a statement like “LDA X&ID1” in which the parameter &ID is concatenated after the character string X and before the character string 1

If the macro definition contained both &ID and &ID1 as parameters, the situation would be ambiguous

Most macro processors deal with this problem by providing a special concatenation operator (e.g. )

LDA X&ID1

Page 17: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Concatenation of Macro Parameters(2/2)

Page 18: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Generation of Unique Labels

Relative addressing in a source statement may be acceptable for short jumps such as “JEQ *-3*

For longer jumps spanning several instructions, such notation is very inconvenient, error-prone and difficult to read

Allow the creation of special types of labels Each symbol beginning with $ has been modified by

replacing $ with $xx, where xx is a two character alphanumeric counter of the number of macro instructions expanded

For the first macro expansions, xx will have the value AA For succeeding macro expansions, xx will be set to AB,

AC, etc

Page 19: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Generation of Unique Labels with Macro Expansion(2/1)

Page 20: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Generation of Unique Labels with Macro Expansion(2/1)

Page 21: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Conditional Macro Expansion(2/1)

Most macro processors can modify the sequence of statements generated for a macro expansion, depending on the arguments supplied in the macro invocation

The IF statement evaluates a Boolean expression that is its operand

If the value of this expression is TRUE, the statements following the IF are generated until an ELSE is encountered

Otherwise, these statements are skipped, and the statements following the ELSE are generated

The ENDIF statement terminates the conditional expression that was begun by the IF statement

Page 22: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

The macro processor must maintain a symbol table that contains the values of all macro-time variables used

Entries in this table are made or modified when SET statements are processed

The implementation outlined above does not allow for nested IF structures

WHILE: a macro-time looping statement The WHILE statement specifies that the following lines,

until the next ENDW statement, are to be generated repeatedly as long as a particular condition is true

The macro-time variable &CTR is used to count the number of times the lines following the WHILE statement have been generated

Conditional Macro Expansion(2/2)

Page 23: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Macro-time Conditional Statements(5/1)

Page 24: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Macro-time Conditional Statements(5/2)

Page 25: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Macro-time Conditional Statements(5/3)

RDBUFF F3, BUF, RECL, 04, 2048

Page 26: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Macro-time Conditional Statements(5/4)

Page 27: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Macro-time Conditional Statements(5/5)

Page 28: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Macro-time looping Statements(2/1)

Page 29: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Macro-time looping Statements(2/2)

Page 30: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Keyword Macro Parameters Positional parameter: parameters and

arguments were associated with each other according to their positions in the macro prototype and the macro invocation statement

Keyword parameters: each argument value is written with a keyword that named the corresponding parameter

Each parameter name is followed by an equal sign, which identifies a keyword parameter

The parameter is assumed to have the default value if its name does not appear in the macro invocation statement

Page 31: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Keyword Parameters in Macro Instructions(3/1)

Page 32: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Keyword Parameters in Macro Instructions(3/2)

Page 33: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Use of Keyword Parameters in Macro Instructions(3/3)

Page 34: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Macro Processor Design Options

Recursive Macro expression General-Purpose Macro Processors Macro Processing within Language

Translators

Page 35: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Recursive Macro Expansion

Macro within macro can be solved if the macro processor is being written in a programming language that allows recursive calls

The compiler would be sure that previous value of any variables declared within a procedure were saved when that procedure was called recursively

If would take care of other details involving return from the procedure

Page 36: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Example of Nested Macro Invocation(2/1)

Page 37: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Example of Nested Macro Invocation(2/2)

Page 38: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

General-Purpose Macro Processors(2/1) Advantages of general-purpose macro

processors: The programmer does not need to learn about a

different macro facility for each compiler or assembler language—the time and expense involved in training are eliminated

The costs involved in producing a general-purpose macro processor are somewhat greater than those for developing a language-specific processor

However, this expense does not need to be repeated for each language; the result is substantial overall saving in software development cost

Page 39: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

A general-purpose facility must provide some way for a user to define the specific set of rules to be followed

Comments should usually be ignored by a macro processor, however, each programming language has its own methods for identifying comments

Each programming language has different facilities for grouping terms, expressions, or statements—a general-purpose macro processor needs to taking these grouping into account

Languages differ substantially in their restrictions on the length of identifiers and the rules for the formation of constants

Programming languages have different basic statement forms—syntax used for macro definitions and macro invocation statements

General-Purpose Macro Processors(2/2)

Page 40: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Macro Processing within Language Translators(2/1) The macro processor reads the source statements and

performs all of its functions, the output lines are passed to the language translator as they are generated

The macro processor operates as a sort of input routine for the assembler or compiler

The line-by-line approach avoids making an extra pass over the source program, so it can be more efficient than using a macro preprocessor

Some of the data structures required by the macro processor and the language translator can be combined

A line-by-line macro processor also makes it easier to give diagnostic messages that are related to the source statement containing the error

Page 41: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Macro Processing within Language Translators(2/2) An integrated macro processor can potentially make use

of any information about the source program that is extracted by the language translator

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

Line-by-line macro processors must be specially designed and written to work with a particular implementation of an assembler or compiler, which results in a more expensive piece of software

The assembler or compiler will be considerably larger and more complex than it would be

The additional complexity will add to the overhead of language translation

Page 42: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

MASM Macro Processor(2/1) The macro processor of MASM is integrated with

Pass 1 of the assembler MASM generates the unique names of local

labels in the form ??n, where n is a hexadecimal number in the range 0000 to FFFF

.ERR: signals to MASM that an error has been detected

EXITM: directs MASM to terminate the expansion of the macro

&: is a concatenation operator

Page 43: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

MASM Macro Processor(2/2)

;; is a macro comment, serves only as documentation for the macro definition

; is an ordinary assembler language comment, included as part of the macro expansion

IRP: sets the macro-time variable to a sequence of values specified in <…>

The statements between the TRP and the matching ENDM are generated once for each value of the variable

Page 44: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Examples of MASM Macro and Conditional Statements(3/1)

Page 45: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Examples of MASM Macro and Conditional Statements(3/2)

Page 46: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Examples of MASM Macro and Conditional Statements(3/3)

Page 47: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Example of MASM Iteration Statement

Page 48: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

ANSI C Macro Language In the ANSI C language, definitions and invocations of m

acros are handled by a preprocessor, which is generally not integrated with the rest of the compiler

#define ABSDIFF(X,Y) X>Y ? X-Y : Y-X #define DISPLAY(EXPR) printf(#EXPR”=%d\n”,EXPR) Macro in ANSI C may contain definitions or invocations o

f other macros DISPLAY(ABSDIFF(3,8))ABSDIFF(3,8)=5 The ANSI C preprocessor also provides conditional com

pilation statements #ifndef BUFFER_SIZE

#define BUFFER_SIZE 1024#endif

Page 49: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

The ELENA Macro Processor Macro definitions in ELENA are composed of a header

and a body The header consists of keywords and parameter

markers, which are identified by the character % At least one of the first two token s in a macro header

must be a keyword, not a parameter marker The macro processor appends a numeric value to create

unique labels ELENA provides macro-time variables and macro-time

instructions that can be used to control the macro expansion

The IF statement is a macro-time conditional “go to” statement

The macro is identified by the sequence of keywords that appear in its header

Page 50: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Examples of ELENA Macro Definition and Invocation(2/1)

Page 51: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Examples of ELENA Macro Definition and Invocation(2/2)

Page 52: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Example of ELENA Macro-time Instructions(2/1)

Page 53: Chapter 4 Macro Processors System Software Chih-Shun Hsu.

Example of ELENA Macro-time Instructions(2/1)