Unit-IV-Macro Processor - Sri Eshwar
Unit-IV-Macro Processor - Sri Eshwar
Unit-IV-Macro Processor - Sri Eshwar
A System Software
Veningston .K
Department of CSE Government College of Technology, Coimbatore veningstonk@gmail.com
Outline
Macro Definition and Expansion Macro Processor Algorithm and data structures
21/September/2013
21/September/2013
21/September/2013
21/September/2013
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
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.
21/September/2013
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
11
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
21/September/2013
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
14
Procedure call: statements of the subroutine appear only once, regardless of how many times the subroutine is called.
21/September/2013
15
Note
The definition of a macro must appear in the source program before any statements that invoke that macro.
21/September/2013
16
(12 Lines)
21/September/2013
17
(6 Lines)
21/September/2013
18
21/September/2013
19
20
21/September/2013
21
21/September/2013
22
21/September/2013
23
21/September/2013
24
21/September/2013
25
21/September/2013
26
27
Pass 2:
Expand all macro invocation statements
Moreover, the body of one macro can contain definitions of other macros.
21/September/2013
System Software - Sri Eshwar College of Engineering
28
21/September/2013
29
21/September/2013
30
31
32
21/September/2013
33
34
Data structures
21/September/2013
35
Algorithm
21/September/2013
36
21/September/2013
37
21/September/2013
38
21/September/2013
39
21/September/2013
40
21/September/2013
41
42
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
21/September/2013
44
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
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
21/September/2013
47
21/September/2013
48
21/September/2013
49
50
21/September/2013
51
21/September/2013
52
53
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
56
21/September/2013
57
21/September/2013
58
21/September/2013
59
21/September/2013
60
61
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
21/September/2013
63
21/September/2013
64
21/September/2013
65
21/September/2013
66
FALSE
The macro processor skips ahead in DEFTAB until it finds the next ELSE or ENDIF statement.
21/September/2013
67
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
21/September/2013
69
21/September/2013
70
It is easier to read and much less error-prone than the positional method.
21/September/2013
71
21/September/2013
72
21/September/2013
73
21/September/2013
74
21/September/2013
75
21/September/2013
76
21/September/2013
77
78
21/September/2013
79
80
21/September/2013
81
21/September/2013
82
21/September/2013
83
Pros
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
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
21/September/2013
86
87
21/September/2013
88
21/September/2013
89
21/September/2013
90
21/September/2013
91
Stringizing operator, #
21/September/2013
92
21/September/2013
93
Example 2
21/September/2013
94
Summary
Basic macro processor functions Machine-independent macro processor features Macro processor design options
21/September/2013
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
104
21/September/2013
105