unit-iv-macro processor - sri eshwar
TRANSCRIPT
-
Macro ProcessorsA System Software
Veningston .KDepartment of CSE
Government College of Technology, [email protected]
-
Outline Basic macro processor functions
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 2System 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/2013System Software - Sri Eshwar College of
Engineering3
-
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/2013System Software - Sri Eshwar College of
Engineering4
-
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/2013System Software - Sri Eshwar College of
Engineering5
-
Typical Macro Processor
Recognize macro definitions
Save the macro definition
Recognize macro calls
Expand macro calls
21/September/2013System Software - Sri Eshwar College of
Engineering6
-
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/2013System Software - Sri Eshwar College of
Engineering7
-
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/2013System Software - Sri Eshwar College of
Engineering8
-
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/2013System Software - Sri Eshwar College of
Engineering9
-
Prototype for the Macro instructions
Each parameter begins with &
Body: the statements that will be generated as the expansion of the macro.
21/September/2013System Software - Sri Eshwar College of
Engineering10
-
Macro expansion
21/September/2013System Software - Sri Eshwar College of
Engineering11
-
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/2013System Software - Sri Eshwar College of
Engineering12
-
Example: Macro definition
21/September/2013System Software - Sri Eshwar College of
Engineering13
-
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/2013System Software - Sri Eshwar College of
Engineering14
-
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/2013System Software - Sri Eshwar College of
Engineering15
-
Note
The definition of a macro must appear in the source program before any statements that invoke that macro.
21/September/2013System Software - Sri Eshwar College of
Engineering16
-
Example: Assembly code
21/September/2013System Software - Sri Eshwar College of
Engineering17
(12 Lines)
-
21/September/2013System Software - Sri Eshwar College of
Engineering18
Example: Assembly code (6 Lines)
-
Swap two variables by macro
21/September/2013System Software - Sri Eshwar College of
Engineering19
-
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/2013System Software - Sri Eshwar College of
Engineering20
-
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/2013System Software - Sri Eshwar College of
Engineering21
-
Example of macro invocation
21/September/2013System Software - Sri Eshwar College of
Engineering22
-
Example of macro expansion [1/3]
21/September/2013System Software - Sri Eshwar College of
Engineering23
-
Example of macro expansion [2/3]
21/September/2013System Software - Sri Eshwar College of
Engineering24
-
Example of macro expansion [3/3]
21/September/2013System Software - Sri Eshwar College of
Engineering25
-
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/2013System Software - Sri Eshwar College of
Engineering26
-
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 *+11JLT *-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/2013System Software - Sri Eshwar College of
Engineering27
-
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/2013System Software - Sri Eshwar College of
Engineering28
-
Example of recursive macro definition [1/2]
MACROS (for SIC)
Contains the definitions of RDBUFF and WRBUFF written in SIC instructions.
21/September/2013System Software - Sri Eshwar College of
Engineering29
-
Example of recursive macro definition [2/2]
MACROX (for SIC/XE)
Contains the definitions of RDBUFF and WRBUFF written in SIC/XE instructions.
21/September/2013System Software - Sri Eshwar College of
Engineering30
-
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/2013System Software - Sri Eshwar College of
Engineering31
-
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/2013System Software - Sri Eshwar College of
Engineering32
-
One-pass macro processor
Sub-procedures
macro definition: DEFINE
macro invocation: EXPAND
21/September/2013System Software - Sri Eshwar College of
Engineering33
-
Data structuresfor 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/2013System Software - Sri Eshwar College of
Engineering35
-
Algorithm
21/September/2013System Software - Sri Eshwar College of
Engineering36
-
Algorithm for a one-pass macro processor [1/3]
21/September/2013System Software - Sri Eshwar College of
Engineering37
-
Algorithm for a one-pass macro processor [2/3]
21/September/2013System Software - Sri Eshwar College of
Engineering38
-
Algorithm for a one-pass macro processor [3/3]
21/September/2013System Software - Sri Eshwar College of
Engineering39
-
1-Pass Macro Processor Flow
21/September/2013System Software - Sri Eshwar College of
Engineering40
-
Handling nested macro definition [1/2]
One-Pass Macro Processor That Allows Nested Macro Definition
21/September/2013System Software - Sri Eshwar College of
Engineering41
-
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/2013System Software - Sri Eshwar College of
Engineering42
-
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/2013System Software - Sri Eshwar College of
Engineering43
-
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/2013System Software - Sri Eshwar College of
Engineering44
-
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:
21/September/2013System Software - Sri Eshwar College of
Engineering45
Beginning of the macro parameter is identified by &; however, the end of the parameter is not marked.
-
Concatenation of macro parameters [2/3]
Example
21/September/2013System Software - Sri Eshwar College of
Engineering46
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
-
Concatenation of macro parameters [3/3]
Ambiguity problem
If &ID and &ID1 are parameters
X&ID1 may mean
Solution to this ambiguity problem
Use a special concatenation operator -> to specify the end of the parameter
21/September/2013System Software - Sri Eshwar College of
Engineering47
-
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, error-prone, and difficult to read.
21/September/2013System Software - Sri Eshwar College of
Engineering48
-
Generation of Unique Labels [2/3]
Example:
21/September/2013System Software - Sri Eshwar College of
Engineering49
-
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/2013System Software - Sri Eshwar College of
Engineering50
-
Generation of Unique Labels within Macro expansion- Example [1/2]
21/September/2013System Software - Sri Eshwar College of
Engineering51
// Macro definition
-
Generation of Unique Labels within Macro expansion- Example [2/2]
21/September/2013System Software - Sri Eshwar College of
Engineering52
// Macro invocation
-
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/2013System Software - Sri Eshwar College of
Engineering53
-
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, GT21/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
21/September/2013System Software - Sri Eshwar College of
Engineering55
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.
-
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 SET 1
&EORCTR SET &EORCTR + 1
21/September/2013System Software - Sri Eshwar College of
Engineering56
Macro time variables
-
Use of macro-time conditional statements [1/4]
21/September/2013System Software - Sri Eshwar College of
Engineering57
-
Use of macro-time conditional statements [2/4]
21/September/2013System Software - Sri Eshwar College of
Engineering58
// Macro expansion
-
Use of macro-time conditional statements [3/4]
21/September/2013System Software - Sri Eshwar College of
Engineering59
// Macro expansion
-
Use of macro-time conditional statements [4/4]
21/September/2013System Software - Sri Eshwar College of
Engineering60
// Macro expansion
-
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
-
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/2013System Software - Sri Eshwar College of
Engineering62
-
Use of macro-time looping statements [1/2]
21/September/2013System Software - Sri Eshwar College of
Engineering63
-
Use of macro-time looping statements [1/2]
21/September/2013System Software - Sri Eshwar College of
Engineering64
-
Implementation of conditional macroexpansion [1/4]
IF-ELSE-ENDIF structure
WHILE-ENDW structure
21/September/2013System Software - Sri Eshwar College of
Engineering65
-
Implementation of conditional macroexpansion [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/2013System Software - Sri Eshwar College of
Engineering66
-
Implementation of conditional macroexpansion [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/2013System Software - Sri Eshwar College of
Engineering67
-
Implementation of conditional macroexpansion [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/2013System Software - Sri Eshwar College of
Engineering68
-
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/2013System Software - Sri Eshwar College of
Engineering69
-
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/2013System Software - Sri Eshwar College of
Engineering70
-
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/2013System Software - Sri Eshwar College of
Engineering71
-
Use of keyword parameters in macro [1/3]
21/September/2013System Software - Sri Eshwar College of
Engineering72
-
21/September/2013System Software - Sri Eshwar College of
Engineering73
Use of keyword parameters in macro [2/3]
-
21/September/2013System Software - Sri Eshwar College of
Engineering74
Use of keyword parameters in macro [3/3]
-
Macro Processor Design Options
Recursive Macro Expansion
General-Purpose Macro Processors
Macro Processing within Language Translators
21/September/2013System Software - Sri Eshwar College of
Engineering75
-
Recursive Macro Expansion [1/4]
Macro invocations within macros / the invocation of one macro by another
process macro invocation during expansion time
21/September/2013System Software - Sri Eshwar College of
Engineering76
-
Recursive Macro Expansion [2/4]
21/September/2013System Software - Sri Eshwar College of
Engineering77
-
Recursive Macro Expansion [3/4]
21/September/2013System Software - Sri Eshwar College of
Engineering78
RDBUFF BUFFER, LENGTH, F1
-
Recursive Macro Expansion [4/4]
21/September/2013System Software - Sri Eshwar College of
Engineering79
-
Problem ofRecursive 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/2013System Software - Sri Eshwar College of
Engineering80
-
Problem ofRecursive Macro Expansion [2/3]
21/September/2013System Software - Sri Eshwar College of
Engineering81
-
Problem ofRecursive 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/2013System Software - Sri Eshwar College of
Engineering82
-
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/2013System Software - Sri Eshwar College of
Engineering83
-
General-Purpose Macro Processors [2/2]
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/2013System Software - Sri Eshwar College of
Engineering84
-
Macro Processingwithin 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/2013System Software - Sri Eshwar College of
Engineering85
-
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/2013System Software - Sri Eshwar College of
Engineering86
-
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/2013System Software - Sri Eshwar College of
Engineering87
-
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/2013System Software - Sri Eshwar College of
Engineering88
-
Integrated Macro Processor [2/2]
An integrated macro processor can support macro instructions that depend upon the context in which they occur.
21/September/2013System Software - Sri Eshwar College of
Engineering89
-
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/2013System Software - Sri Eshwar College of
Engineering90
-
ANSI C Macro Language [2/5]
Example:
21/September/2013System Software - Sri Eshwar College of
Engineering91
-
ANSI C Macro Language [3/5]
Parameter substitutions are not performed within quoted strings:
Stringizing operator, #
21/September/2013System Software - Sri Eshwar College of
Engineering92
-
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/2013System Software - Sri Eshwar College of
Engineering93
-
ANSI C Macro Language [5/5]
Conditional compilation statements
Example 1
Example 2
21/September/2013System Software - Sri Eshwar College of
Engineering94
-
Summary
Basic macro processor functions
Machine-independent macro processor features
Macro processor design options
21/September/2013System Software - Sri Eshwar College of
Engineering95
-
Exercise 1
#define int charmain(){
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 10main(){
#define a 50printf("%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() 100main(){
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 0main() {
if(NULL)puts("NULL");
else if(FALSE)puts("TRUE");
elseputs("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 AnIntroduction to Systems Programming,Addison-wesley, 3rd Edition, 2000.
D.M. Dhamdhere, Systems Programming &Operating systems, Tata McGraw Hill, 2nd
Revised Edition, 1999.
21/September/2013System Software - Sri Eshwar College of
Engineering104
-
Discussions & Queries
21/September/2013System Software - Sri Eshwar College of
Engineering105