Вы находитесь на странице: 1из 105

Macro Processors

A System Software

Veningston .K
Department of CSE Government College of Technology, Coimbatore veningstonk@gmail.com

Basic macro processor functions

Outline

Macro Definition and Expansion Macro Processor Algorithm and data structures

Machine-independent macro processor features


Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro Parameters

Macro processor design options


Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators

Macro Implementation Examples


ANSI C Macro language.
21/September/2013
System Software - Sri Eshwar College of Engineering

Machine Dependence Vs. Machine Independence


Software that runs only on a particular type of computer (Machine architecture). Software that run on a variety of different types of computers.

21/September/2013

System Software - Sri Eshwar College of Engineering

What is Macro processor? [1/2]


A macro processor is a program that reads a file (or files) and scans them for certain keywords. When a keyword is found, it is replaced by some text. The keyword/text combination is called a macro.

21/September/2013

System Software - Sri Eshwar College of Engineering

What is Macro processor? [2/2]


The preprocessor reads the first line and stores it as a macro definition. When it comes across the later reference to MAX_BANANAS in the for loop, it replaces it with the macro's definition, 6. The output of the C - preprocessor is then fed to the C compiler.

21/September/2013

System Software - Sri Eshwar College of Engineering

Typical Macro Processor


Recognize macro definitions Save the macro definition Recognize macro calls Expand macro calls

21/September/2013

System Software - Sri Eshwar College of Engineering

Note
The most common use of macro processors is in assembler language programming However, macro processors can also be used with high-level programming languages.

21/September/2013

System Software - Sri Eshwar College of Engineering

Basic macro processor functions


A macro instruction (macro) 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 instruction with the corresponding group of source language statements (expanding)
Normally, it performs no analysis of the text it handles. It does not concern the meaning of the involved statements during macro expansion.

The design of a macro processor generally is machine independent


21/September/2013
System Software - Sri Eshwar College of Engineering

Basic macro processor functions


Two new assembler directives are used in macro definition
MACRO: identify the beginning of a macro definition MEND: identify the end of a macro definition

21/September/2013

System Software - Sri Eshwar College of Engineering

Prototype for the Macro instructions


Each parameter begins with &

Body: the statements that will be generated as the expansion of the macro.
21/September/2013
System Software - Sri Eshwar College of Engineering

10

Macro expansion

21/September/2013

System Software - Sri Eshwar College of Engineering

11

Types of Macro expansion


Lexical expansion
Implies replacement of a character string by another character string during program generation
Eg: replace occurrences of formal parameters by corresponding actual parameters

Semantic expansion
Implies generation of instructions tailored to the requirements of a specific usage
Eg: generation of type specific instructions for manipulation of byte and word operands
21/September/2013
System Software - Sri Eshwar College of Engineering

12

Example: Macro definition

21/September/2013

System Software - Sri Eshwar College of Engineering

13

Macro invocation
A macro invocation statement (a macro call) gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro.
macro_name p1, p2,

21/September/2013

System Software - Sri Eshwar College of Engineering

14

Difference between macro call and procedure call


Macro call: statements of the macro body are expanded each time the macro is invoked.

Procedure call: statements of the subroutine appear only once, regardless of how many times the subroutine is called.

21/September/2013

System Software - Sri Eshwar College of Engineering

15

Note
The definition of a macro must appear in the source program before any statements that invoke that macro.

21/September/2013

System Software - Sri Eshwar College of Engineering

16

Example: Assembly code

(12 Lines)

21/September/2013

System Software - Sri Eshwar College of Engineering

17

Example: Assembly code

(6 Lines)

21/September/2013

System Software - Sri Eshwar College of Engineering

18

Swap two variables by macro

21/September/2013

System Software - Sri Eshwar College of Engineering

19

Macro expansion [1/2]


Each macro invocation statement will be expanded into the statements that form the body of the macro. Arguments from the macro invocation are substituted for the parameters in the macro prototype (according to their positions).
In the definition of macro: parameter In the macro invocation: argument
21/September/2013
System Software - Sri Eshwar College of Engineering

20

Macro expansion [2/2]


Comment lines within the macro body will be deleted. Macro invocation statement itself has been included as a comment line. The label on the macro invocation statement has been retained as a label on the first statement generated in the macro expansion.

21/September/2013

System Software - Sri Eshwar College of Engineering

21

Example of macro invocation

21/September/2013

System Software - Sri Eshwar College of Engineering

22

Example of macro expansion [1/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

23

Example of macro expansion [2/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

24

Example of macro expansion [3/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

25

Macro instructions - Label


Body of the macro contains no label Problem of the label in the body of macro
If the same macro is expanded multiple times at different places in the program There will be duplicate labels, which will be treated as errors by the assembler.

21/September/2013

System Software - Sri Eshwar College of Engineering

26

Label in the body of macro


Solutions
Do not use labels in the body of macro. Explicitly use PC-relative addressing instead.
Example: In RDBUFF and WRBUFF macros, [Refer Pg. 178]

JEQ *+11 JLT *-14


* denotes a literal refer to the current value of program counter Literals Write the value of a constant operand as a part of the instruction that uses it. It is inconvenient and error-prone

How to avoid such error-prone method?


21/September/2013
System Software - Sri Eshwar College of Engineering

27

Two-pass macro processor


Design of a two-pass macro processor
Pass 1:
Process all macro definitions

Pass 2:
Expand all macro invocation statements

However, one-pass may be enough


Because all macros would have to be defined during the first pass before any macro invocations were expanded.
The definition of a macro must appear before any statements that invoke that macro.

Moreover, the body of one macro can contain definitions of other macros.
21/September/2013
System Software - Sri Eshwar College of Engineering

28

MACROS (for SIC)

Example of recursive macro definition [1/2]


Contains the definitions of RDBUFF and WRBUFF written in SIC instructions.

21/September/2013

System Software - Sri Eshwar College of Engineering

29

MACROX (for SIC/XE)

Example of recursive macro definition [2/2]


Contains the definitions of RDBUFF and WRBUFF written in SIC/XE instructions.

21/September/2013

System Software - Sri Eshwar College of Engineering

30

Example of Macro definitions


A program that is to be run on SIC system could invoke MACROS whereas a program to be run on SIC/XE can invoke MACROX. However, defining MACROS or MACROX does not define RDBUFF and WRBUFF. These definitions are processed only when an invocation of MACROS or MACROX is expanded.
21/September/2013
System Software - Sri Eshwar College of Engineering

31

One-pass macro processor


A one-pass macro processor that alternate between macro definition and macro expansion in a recursive way is able to handle recursive macro definition. Restriction
The definition of a macro must appear in the source program before any statements that invoke that macro. This restriction does not create any real inconvenience.
21/September/2013
System Software - Sri Eshwar College of Engineering

32

One-pass macro processor


Sub-procedures
macro definition: DEFINE macro invocation: EXPAND

21/September/2013

System Software - Sri Eshwar College of Engineering

33

Data structures for one-pass macro processor


DEFTAB (definition table)
Stores the macro definition including macro prototype and macro body Comment lines are omitted. References to the macro instruction parameters are converted to a positional notation for efficiency in substituting arguments.

NAMTAB (name table)


Stores macro names Serves as an index to DEFTAB Pointers to the beginning and the end of the macro definition (DEFTAB)

ARGTAB (argument table)


Stores the arguments of macro invocation according to their positions in the argument list As the macro is expanded, arguments from ARGTAB are substituted for the corresponding parameters in the macro body.
21/September/2013
System Software - Sri Eshwar College of Engineering

34

Data structures

21/September/2013

System Software - Sri Eshwar College of Engineering

35

Algorithm

21/September/2013

System Software - Sri Eshwar College of Engineering

36

Algorithm for a one-pass macro processor [1/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

37

Algorithm for a one-pass macro processor [2/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

38

Algorithm for a one-pass macro processor [3/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

39

1-Pass Macro Processor Flow

21/September/2013

System Software - Sri Eshwar College of Engineering

40

Handling nested macro definition [1/2]


One-Pass Macro Processor That Allows Nested Macro Definition

21/September/2013

System Software - Sri Eshwar College of Engineering

41

Handling nested macro definition [2/2]


In DEFINE procedure
When a macro definition is being entered into DEFTAB, the normal approach is to continue until an MEND directive is reached. This would not work for nested macro definition because the first MEND encountered in the inner macro will terminate the whole macro definition process. To solve this problem, a counter LEVEL is used to keep track of the level of macro definitions.
Increase LEVEL by 1 each time a MACRO directive is read. Decrease LEVEL by 1 each time a MEND directive is read. A MEND terminates the whole macro definition process when LEVEL reaches 0. This process is very much like matching left and right parentheses when scanning an arithmetic expression.
21/September/2013
System Software - Sri Eshwar College of Engineering

42

Comparison of Macro Processors Design


One-pass algorithm
Every macro must be defined before it is called One-pass processor can alternate between macro definition and macro expansion Nested macro definitions are allowed but nested calls are not

Two-pass algorithm
Pass1: Recognize macro definitions Pass2: Recognize macro calls Nested macro definitions are not allowed
21/September/2013
System Software - Sri Eshwar College of Engineering

43

Machine-independent Macro Processor Features


Extended features that are not directly related to the architecture of the computer for which macro processor is written.
Concatenation of macro parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro Parameters

21/September/2013

System Software - Sri Eshwar College of Engineering

44

Concatenation of macro parameters [1/3]


Macro processors allow parameters to be concatenated with other character strings.
Used when a program contains a set of series of variables Example:

Beginning of the macro parameter is identified by &; however, the end of the parameter is not marked.
21/September/2013
System Software - Sri Eshwar College of Engineering

45

Example

Concatenation of macro parameters [2/3]

The parameter &ID is concatenated after the character string X and before the character string 1,2,3 & S - a special concatenation operator which identify the end of the macro parameter
21/September/2013
System Software - Sri Eshwar College of Engineering

46

Ambiguity problem
X&ID1 may mean

Concatenation of macro parameters [3/3]


If &ID and &ID1 are parameters

Solution to this ambiguity problem


Use a special concatenation operator -> to specify the end of the parameter

21/September/2013

System Software - Sri Eshwar College of Engineering

47

Generation of Unique Labels [1/3]


Labels in the macro body may cause duplicate labels problem if the macro is invocated and expanded multiple times. Use of relative addressing at the source statement level is very inconvenient, errorprone, and difficult to read.

21/September/2013

System Software - Sri Eshwar College of Engineering

48

Generation of Unique Labels [2/3]


Example:

21/September/2013

System Software - Sri Eshwar College of Engineering

49

Generation of Unique Labels [3/3]


Let the macro processor generate unique labels for each macro invocation and expansion.
Labels used within the macro body begin with the special character $ During macro expansion, the $ will be replaced with $xx, where xx is a two-character alpha-numeric counter of the number of macro instructions expanded. xx=AA,AB,AC,.., ZZ
This allows 1296 macro expansions in a single program.
21/September/2013
System Software - Sri Eshwar College of Engineering

50

Generation of Unique Labels within Macro expansion- Example [1/2]


// Macro definition

21/September/2013

System Software - Sri Eshwar College of Engineering

51

Generation of Unique Labels within Macro expansion- Example [2/2]


// Macro invocation

21/September/2013

System Software - Sri Eshwar College of Engineering

52

Conditional Macro Expansion


Typically, each invocation of a particular macro was expanded into the same sequence of statements These statements could be varied by the substitution of parameters, but the form of the statements, and the order in which they appeared, were unchanged Macro processors can also modify the sequence of statements generated for a macro expansion depending on the arguments supplied in the macro invocation
21/September/2013
System Software - Sri Eshwar College of Engineering

53

Conditional Macro Expansion


Conditional assembly depends on parameters provided

Part I is expanded if condition part is true, otherwise part II is expanded Compare operator: NE, EQ, LE, GT
21/September/2013
System Software - Sri Eshwar College of Engineering

54

Macro-time variables
Any symbol that begins with & and that is not a macro instruction parameter is assumed to be a macro-time variable Can be used to store working values during the macro expansion
Store the evaluation result of Boolean expression Control the macro-time conditional structures
Boolean expression in IF statements occurs at the time macros are expanded. By the time the program is assembled, all such decisions would have been made.
21/September/2013
System Software - Sri Eshwar College of Engineering

55

Macro-time variables
Be initialized to a value of 0 Be set by a macro processor directive, SET i.e. Conditional macro expansion directive
Example:
&EORCK &EORCTR SET SET
Macro time variables

1 &EORCTR + 1

21/September/2013

System Software - Sri Eshwar College of Engineering

56

Use of macro-time conditional statements [1/4]

21/September/2013

System Software - Sri Eshwar College of Engineering

57

Use of macro-time conditional statements [2/4] // Macro expansion

21/September/2013

System Software - Sri Eshwar College of Engineering

58

Use of macro-time conditional statements [3/4] // Macro expansion

21/September/2013

System Software - Sri Eshwar College of Engineering

59

Use of macro-time conditional statements [4/4] // Macro expansion

21/September/2013

System Software - Sri Eshwar College of Engineering

60

Macro-time looping statement [1/2]


&CTR- is used to count the number of times the lines following the WHILE statement have been generated The value of &CTR is initialized to 1 (line 63), and incremented by 1 each time through the loop (line 71) The while statement itself specifies that the macro-time loop will continue to be executed as long as the value of &CTR is <= the value of &EORCT (line 65 and 70)
21/September/2013
System Software - Sri Eshwar College of Engineering

61

Macro-time looping statement [2/2]

Macro processor function


%NITEMS: Returns the number of members in an argument list

The execution of testing of IF/WHILE, SET, %NITEMS() occurs at macro expansion time
21/September/2013
System Software - Sri Eshwar College of Engineering

62

Use of macro-time looping statements [1/2]

21/September/2013

System Software - Sri Eshwar College of Engineering

63

Use of macro-time looping statements [1/2]

21/September/2013

System Software - Sri Eshwar College of Engineering

64

Implementation of conditional macro expansion [1/4]


IF-ELSE-ENDIF structure WHILE-ENDW structure

21/September/2013

System Software - Sri Eshwar College of Engineering

65

Implementation of conditional macro expansion [2/4]


IF-ELSE-ENDIF structure
The macro processor must maintain a symbol table
This table contains the values of all macro-time variables used. Entries in this table are made or modified when SET statements are processed. This table is used to look up the current value of a macro-time variable whenever it is required.

21/September/2013

System Software - Sri Eshwar College of Engineering

66

Implementation of conditional macro expansion [3/4]


When an IF statement is encountered during the expansion of a macro, the specified Boolean expression is evaluated.
TRUE
The macro processor continues to process lines from DEFTAB until it encounters the next ELSE or ENDIF statement. If ELSE is encountered, then skips to ENDIF

FALSE
The macro processor skips ahead in DEFTAB until it finds the next ELSE or ENDIF statement.

21/September/2013

System Software - Sri Eshwar College of Engineering

67

Implementation of conditional macro expansion [4/4]


WHILE-ENDW structure
When a WHILE statement is encountered during the expansion of a macro, the specified Boolean expression is evaluated. TRUE
The macro processor continues to process lines from DEFTAB until it encounters the next ENDW statement. When ENDW is encountered, the macro processor returns to the preceding WHILE, re-evaluates the Boolean expression, and takes action based on the new value.

FALSE
The macro processor skips ahead in DEFTAB until it finds the next ENDW statement and then resumes normal macro expansion.
21/September/2013
System Software - Sri Eshwar College of Engineering

68

Keyword macro parameters [1/3]


Positional parameters [1/2]
Parameters and arguments are associated according to their positions in the macro prototype and invocation. If an argument is to be omitted, a null argument should be used to maintain the proper order in macro invocation Example:

21/September/2013

System Software - Sri Eshwar College of Engineering

69

Keyword macro parameters [2/3]


Positional parameters [2/2]
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.

21/September/2013

System Software - Sri Eshwar College of Engineering

70

Keyword macro parameters [3/3]


Keyword parameters
Each argument value is written with a keyword that names the corresponding parameter. Arguments may appear in any order. Null arguments no longer need to be used.
Example: XXX P1=A1, P2=A2, P20=A20.

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

21/September/2013

System Software - Sri Eshwar College of Engineering

71

Use of keyword parameters in macro [1/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

72

Use of keyword parameters in macro [2/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

73

Use of keyword parameters in macro [3/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

74

Macro Processor Design Options


Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators

21/September/2013

System Software - Sri Eshwar College of Engineering

75

Recursive Macro Expansion [1/4]


Macro invocations within macros / the invocation of one macro by another
process macro invocation during expansion time

21/September/2013

System Software - Sri Eshwar College of Engineering

76

Recursive Macro Expansion [2/4]

21/September/2013

System Software - Sri Eshwar College of Engineering

77

Recursive Macro Expansion [3/4]

RDBUFF BUFFER, LENGTH, F1


21/September/2013
System Software - Sri Eshwar College of Engineering

78

Recursive Macro Expansion [4/4]

21/September/2013

System Software - Sri Eshwar College of Engineering

79

Problem of Recursive Macro Expansion [1/3]


Previous macro processor design cannot handle such kind of recursive macro invocation and expansion
The procedure EXPAND would be called recursively, thus the invocation arguments in the ARGTAB will be overwritten. The Boolean variable EXPANDING would be set to FALSE when the inner macro expansion is finished, i.e., the macro process would forget that it had been in the middle of expanding an outer macro.
21/September/2013
System Software - Sri Eshwar College of Engineering

80

Problem of Recursive Macro Expansion [2/3]

21/September/2013

System Software - Sri Eshwar College of Engineering

81

Problem of Recursive Macro Expansion [3/3]


Solutions
Write the macro processor in a programming language that allows recursive calls, thus local variables will be retained. If you are writing in a language without recursion support, use a stack to take care of pushing and popping local variables and return addresses.

21/September/2013

System Software - Sri Eshwar College of Engineering

82

General-Purpose Macro Processors [1/2]


Macro processors that do not dependent on any particular programming language, but can be used with a variety of different languages

21/September/2013

System Software - Sri Eshwar College of Engineering

83

Pros

General-Purpose Macro Processors [2/2]

Programmers do not need to learn many macro languages. Although its development costs are somewhat greater than those for a language specific macro processor, this expense does not need to be repeated for each language, thus save substantial overall cost.

Cons
Large number of details must be dealt with in a real programming language
Situations in which normal macro parameter substitution should not occur, e.g., comments. Facilities for grouping together terms, expressions, or statements Involves the tokens of the programming language, e.g., identifiers, constants, operators, keywords Syntax had better be consistent with the source programming language
21/September/2013
System Software - Sri Eshwar College of Engineering

84

Macro Processing within Language Translators


The macro processors we discussed are called Preprocessors.
Process macro definitions Expand macro invocations Produce an expanded version of the source program, which is then used as input to an assembler or compiler

You may also combine the macro processing functions with the language translator:
Line-by-line macro processor Integrated macro processor
21/September/2013
System Software - Sri Eshwar College of Engineering

85

Line-by-Line Macro Processor [1/2]


Used as a sort of input routine for the assembler or compiler
Read source program Process macro definitions and expand macro invocations Pass output lines to the assembler or compiler

21/September/2013

System Software - Sri Eshwar College of Engineering

86

Line-by-Line Macro Processor [2/2]


Benefits
Avoid making an extra pass over the source program. Data structures required by the macro processor and the language translator can be combined (e.g., OPTAB and NAMTAB) Utility subroutines can be used by both macro processor and the language translator.
Scanning input lines Searching tables Data format conversion

It is easier to give diagnostic messages related to the source statements.


21/September/2013
System Software - Sri Eshwar College of Engineering

87

Integrated Macro Processor [1/2]


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

21/September/2013

System Software - Sri Eshwar College of Engineering

88

Integrated Macro Processor [2/2]


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

21/September/2013

System Software - Sri Eshwar College of Engineering

89

ANSI C Macro Language [1/5]


Definitions and invocations of macros are handled by a preprocessor, which is generally not integrated with the rest of the compiler. Example:

21/September/2013

System Software - Sri Eshwar College of Engineering

90

ANSI C Macro Language [2/5]


Example:

21/September/2013

System Software - Sri Eshwar College of Engineering

91

ANSI C Macro Language [3/5]


Parameter substitutions are not performed within quoted strings:

Stringizing operator, #

21/September/2013

System Software - Sri Eshwar College of Engineering

92

ANSI C Macro Language [4/5]


Recursive macro definitions or invocations
After a macro is expanded, the macro processor rescans the text that has been generated, looking for more macro definitions or invocations. Macro cannot invoke or define itself recursively. Example

21/September/2013

System Software - Sri Eshwar College of Engineering

93

ANSI C Macro Language [5/5]


Conditional compilation statements Example 1

Example 2

21/September/2013

System Software - Sri Eshwar College of Engineering

94

Summary
Basic macro processor functions Machine-independent macro processor features Macro processor design options

21/September/2013

System Software - Sri Eshwar College of Engineering

95

Exercise 1
#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }

Result
Answer: sizeof(i)=1 Explanation: Since the #define replaces the string int by the macro char

Exercise 2
#include #define a 10 main() { #define a 50 printf("%d",a); }

Result
Answer: 50 Explanation: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.

Exercise 3
#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

Result
Answer: 100 Explanation: Preprocessor executes as a separate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs. The input program to compiler looks like this : main() { 100; printf("%d\n",100); } Note: 100; is an executable statement but with no action. So it doesn't give any problem

Exercise 4
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

Result
Answer: TRUE Explanation: The input program to the compiler after processing by the preprocessor is, main(){ if(0) puts("NULL"); else if(-1) puts("TRUE"); else puts("FALSE"); } Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is Boolean value false so it goes to else. In second if -1 is Boolean value true hence "TRUE" is printed.

References
Leland L. Beck, System Software An Introduction to Systems Programming, Addison-wesley, 3rd Edition, 2000. D.M. Dhamdhere, Systems Programming & Operating systems, Tata McGraw Hill, 2nd Revised Edition, 1999.

21/September/2013

System Software - Sri Eshwar College of Engineering

104

Discussions & Queries

21/September/2013

System Software - Sri Eshwar College of Engineering

105

Вам также может понравиться