1 / 66

Chapter 4 Macro Processors

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.

andrean
Download Presentation

Chapter 4 Macro Processors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Macro Processors UNIT -6 MACRO PROCESSORS Subject Name:Sysytem Software Subject Code: 10SCS52 Department:CSE Date:3-11-2014

  2. 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++) { ...; }

  3. 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

  4. 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

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

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

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

  8. Program with Macro Expanded(3/1)

  9. Program with Macro Expanded(3/2)

  10. Program with Macro Expanded(3/3)

  11. 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

  12. Macro Processor Data Structures

  13. Algorithm for a One-pass Macro Processor

  14. Algorithm for a One-pass Macro Processor

  15. Algorithm for a One-pass Macro Processor

  16. 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.

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

  18. 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&ID1

  19. Concatenation of Macro Parameters

  20. 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

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

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

  23. 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

  24. Use of Macro-time Conditional Statements

  25. Use of Macro-time Conditional Statements

  26. Use of Macro-time Conditional Statements RDBUFF F3, BUF, RECL, 04, 2048

  27. Use of Macro-time Conditional Statements

  28. Use of Macro-time Conditional Statements

  29. 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

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

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

  32. 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

  33. 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

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

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

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

  37. Macro Processor Design Options • Recursive Macro expression • General-Purpose Macro Processors • Macro Processing within Language Translators

  38. 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

  39. Example of Nested Macro Invocation(2/1)

  40. Example of Nested Macro Invocation(2/2)

  41. 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

  42. 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

  43. 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.

  44. 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.

  45. 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.

  46. 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.

  47. 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.

  48. 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.

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

  50. 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

More Related