660 likes | 783 Views
Chapter 4 Macro Processors. UNIT -6 MACRO PROCESSORS. Subject Name:Sysytem Software Subject Code : 10SCS52 Department:CSE Date:3-11-2014. Introduction. A macro processor is a program that reads a file (or files) and scans them for certain keywords.
E N D
Chapter 4 Macro Processors UNIT -6 MACRO PROCESSORS Subject Name:Sysytem Software Subject Code: 10SCS52 Department:CSE Date:3-11-2014
Introduction • 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. • A simple example is the C language preprocessor. When you write #define MAX_BANANAS 6 int banana; for (banana = 0; banana < MAX_BANANAS; banana++) { ...; }
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
Macro Definition and Expansion • The MACRO statement identifies the beginning of a macro definition • The symbol in the label field(RDBUFF) 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
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
The procedure PROCESSING • The procedure DEFINE • Called when the beginning of a macro definition is recognized, makes the appropriate entries in DEFTAB and NAMTAB. • The procedure EXPAND • Called to set up the argument values in ARGTAB and expand a macro invocation statement. • The procedure GETLINE • Called at several points in the algorithm, gets the next line to be processed. • EXPANDING is set to TRUE or FALSE.
Machine-Independent Macro Processor Features • Concatenation of Macro Parameters • Generation of Unique Labels • Conditional Macro Expansion • Keyword Macro Parameters
Concatenation of Macro Parameters • 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. • Suppose there is a parameter named &ID, then • 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
Generation of Unique Labels • Following figure shows one method to generate unique labels within a macro expansion. • Labels used within the macro body begin with special character $. • 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
Conditional Macro Expansion • 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
Use of Macro-time Conditional Statements RDBUFF F3, BUF, RECL, 04, 2048
Conditional Macro Expansion(2/2) • 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
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 • Arguments may appear in any order. • 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
Macro Processor Design Options • Recursive Macro expression • General-Purpose Macro Processors • Macro Processing within Language Translators
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
General-Purpose Macro Processors(2/1) • General-purpose macro processors are not dependent on any particular programming language, but can be used with a variety of different languages. • 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
General-Purpose Macro Processors(2/2) • In spite of the advantages noted, there are still few general purpose macro processors. • 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
Macro Processing within Language Translators • The macro processors that we have discussed so far are called preprocessors. • That is, they process macro definitions and expand macro invocations, producing an expanded version of the source program. • This expanded program is then used as input to an assembler or compiler. • In this section we are going to use an alternative: combining the macro processing functions with the language translator itself.
The simplest method of achieving this is a line-by-line macro processor. • These processors reads the source program and perform all of it’s functions as previously described. • But the output lines are passed to the language translators as they are generated, instead of being written to an expanded source file.
Advantages • It avoids making an extra pass over the source program, so it can be more efficient than using a macro processor. • Some of the data structures required by the macro processor and the language translator can be combined. • For eg: OPTAB in an assembler and NAMTAB in the macro processor could be implemented in the same table. • In addition, many utility subroutines and functions can be used by both the language translator and the macro processor. • A line-by-line macro processor also makes it easier to give diagnostic messages that are related to the source statement containing the error.
With a macro processor , such an error might be detected only in relation to some statement in the macro expansion. • Then the programmer need to backtrack to discover the original source of trouble.
Disadvantages • Line-by-line macro processors must be specially designed and written to work with a particular implementation of an assembler or compiler. • The cost of macro processor development must therefore be added to the cost of the language translator, which results in a more expensive piece of software. • In addition, the assembler or compiler will be considerably larger and more complex than it would be if a macro processor were used. • The size may be a problem if the translator is to run on a computer with limited memory.
MASM Macro Processor • This section describes some of the macro processing features of the Microsoft MASM assembler. • In MASM conditional macro expansions are called conditional assembly statements. • Following is an example for MASM conditional assembly statements.
It computes the absolute difference between the values of it’s first two parameters. • These parameters may be either words(16 bits) or double words(32 bits). • If they are double words, the third parameter has the value E and the calculation uses the double word register EAX. • If the first two parameters are words, the third parameter is omitted. • In the previous figure the first line gives the name of the macro and it’s parameters. • Notice that in MASM parameters need not begin with &. • The end of the macro is marked by ENDM