Macro processors

Compiled By: Girish GS
Asst. Professor BNMIT

Macro Processors
Define macro?
A macro instruction (abbreviated to macro) is simply a notational convenience for the programmer. • A macro represents a commonly used group of statements in the source programming language It allows the programmer to write shorthand version of a program (module programming)

Define macroprocessor?
Macro processor are preprocessors that • • • • Recognize macro definitions Save the macro definition Recognize macro calls Expand macro calls


Macro processors

Basic Macro Processor Functions:
• •

Macro Definition and Expansion Macro Processor Algorithms and Data structures

Macro Definition and Expansion: Figure shows an example of macro definition in SIC/XE program. The program defines a macro named RDBUFF. Example of macro definition

Two new assembler directives are used in macro definition o MACRO: Identify the beginning of a macro definition o MEND: Identify the end of a macro definition The first MACRO statement identifies the beginning of a macro definition. The symbol in the label field (RDBUFF) is the name of the macro, and entries in the operand field identify the parameters of the macro instruction. Each parameter begins with the character & which facilitates the substitution of parameters during macro expansion. The macro name and parameters define a pattern or prototype for the macro. Following the MACRO directive are the statements that make up the body of the macro definition. These are the statements that will be generated as the expansion of the macro. MEND assembler directive marks the end of the macro definition.


Macro processors Macro invocation The Figure shows the main program that contains the macro invocation statement. The figure shows the MACRO expansion. 3 BNMIT-ISE . A macro invocation statement is often reffered to as a macro call. Macro expansion The program with macros is supplied to the macro processor. A macro invocation statement gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro.

Arguments from the macro invocation are substituted for the parameters in the macro prototype. The difference between Macros and Subroutines is that the statements from the body of the Macro is expanded the number of times the macro invocation is encountered. However. whereas the statement of the subroutine appears only once no matter how many times the subroutine is called. a two-pass macro processor would not allow the body of one macro instruction to contain definitions of other macros(recursive macro definitions). Note No label in the macro body • 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. For example. which will be treated as errors by the assembler. Explicitly use PC-relative addressing instead. • Solutions: • • Do not use labels in the body of macro. Such definitions of macros by other macros can be useful in certain cases.  Two-pass macro processor • • All macro definitionsare processed during the first pass. Consider the example of a Macro defining another Macro. 4 BNMIT-ISE . Macro Processor Algorithm and Data Structure: Design can be done as two-pass or a one-pass macro. JEQ *+11 JLT *-19 However it is still an inconvenient and error-prone method. In case of two-pass macro processor. All macro invocation statements are expanded during the second pass. The first argument in the macro matches with the first parameter in the macro prototype and so on. The arguments and the parameters are associated with one another according to their positions. the argument F1 is substituted for the parameter &INDEV whereever it occurs in the body of the macro. Ex. in RDBUFF macro.Macro processors Macro invocation statement will be expanded into the statements that form the body of the macro.

WRBUFF and other macro instructions for SIC machine. The design considered is for one-pass assembler. • The definition of a macro must appear in the source program before any statements that invoke that macro. Data Structures There three main data structures involved in one pass macro processor: DEFTAB. the body of the first Macro (MACROS) contains statement that define RDBUFF. One-Pass Macro Processor: A one-pass macro processor that can alternate between macro definition and macro expansion is able to handle macros like those in above figure. • 5 BNMIT-ISE . The body of one macro can contain definitions of other macros.Macro processors In the example below. ARGTAB DEFTAB (Definition Table) Stores the macro definition including macro prototype and macro body Comment lines are omitted. Because of one pass structure. o References to the macro instruction parameters are converted to a positional notation for efficiency in substituting arguments. NAMTAB.

(b) (a) In figure (a). with an entry in NAMTAB having the pointers to the beginning and the end of the definition. o As the macro is expanded the arguments from the Argument table are substituted for the corresponding parameters in the macro body. For example. The arguments referred by the instructions are denoted by the their positional notations.Macro processors • 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 according to their positions in the argument list. In the instruction this is replaced by its positional value? 1. 6 BNMIT-ISE . definition of RDBUFF is stored in DEFTAB. TD =X’?1’ The above instruction is to test the availability of the device whose number is given by the parameter &INDEV. The figure below shows the different data structures described and their relationship.

  The procedure GETLINE Called at several points in the algorithm. LENGTH One pass macroprocessor algorithm The algorithm of the Macro processor is given below.Macro processors Figure (b) shows the ARGTAB as it would appear during expansion of the RDBUFF statement as given below: RDBUFF F1. BUFFER. The procedure EXPAND Called to set up the argument values in ARGTAB and expand a macro invocation statement   •   Handling nested macro definition 7 BNMIT-ISE . The procedure PROCESSLINE The procedure DEFINE Called when the beginning of a macro definition is recognized. gets the next line to be processed either from the DEFTAB or from the file itself depending upon whether the boolean variable EXPANDING is set to TRUE or FALSE. makes the appropriate entries in DEFTAB and NAMTAB.

Macro processors Algorithm 8 BNMIT-ISE .

Macro processors 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 may be allowed but nested calls are not Two pass algorithm – Pass1: Recognize macro definitions – Pass2: Recognize macro calls – nested macro definitions are not allowed Machine-independent Macro-Processor Features The design of macro processor doesn’t depend on the architecture of the machine. These features are: 9 BNMIT-ISE . We will be studying some extended feature for this macro processor.

B.Concatenation of Macro Parameters .Macro processors . The parameter to such a macro instruction could specify the series of variables to be operated on (A. another series of variables named XB1. Most of the macro processors deal with this problem by providing a special concatenation operator. So in the case of &ID1.….. If similar processing is to be performed on each series of labels. 10 BNMIT-ISE . Thus X&ID1 may mean “X”+ID+”1” or “X”+ID1.Keyword Macro Parameters Concatenation of unique labels: Most macro processor allows parameters to be concatenated with other character strings.).Conditional Macro Expansion . etc. …. XB2. but the end of the parameter is not marked. this operator is the character →.Generation of unique labels . etc. the situation would be unavoidably ambiguous. The body of the macro definition might contain a statement like ` The beginning of the macro parameter is identified by the starting symbol & . the macro processor could deduce the meaning that was intended. The macro processor will deletes all occurences of the concatenation operator immediately after performing parameter substitution. XA3. Thus the statement LDA X&ID1 can be written as LDA X&ID→1 so that the end of the parameter &ID is clearly identified. so the character -> will not appear in the macro expansion. If the macro definition contains &ID and &ID1 as parameters. In the SIC macro language.). The macro processor would use this parameter to construct the symbols required in the macro expansion (XA1. Suppose that the parameter to such a macro instruction is named &ID. the programmer might put this as a macro instruction. XB1. Suppose that a program contains a series of variables named by the symbols XA1. XB3. XA2. etc.

such notation is very inconvenient.Macro processors The below figure(a) shows a macro definition that uses the concatenation operator as previously described. However. errorprone and difficult to read. for longer jumps spanning several instructions. Figure. The jump instruction were written using relative operands *-3 and *-14. The statement SUM A and SUM BETA in fig(b) and fig(c) shows the invocation statements and the corresponding macro expansion. 11 BNMIT-ISE . (a) Generation of Unique Labels It is in general not possible to use labels for the instructions in the macro definition. since every expansion of macro would include the label repeatedly which is not allowed by the assembler. This sort of relative in a source statement is acceptable for short jumps such as “ JEQ *-3”.

Labels used within the macro body begin with the special character $. Each symbol beginning with $ has been modified by replacing $ with $AA. The following program shows the macro definition with labels to the instruction. 12 BNMIT-ISE . More generally the character $ will be replaced by $xx.Macro processors Instead we can use the technique of generating unique labels for every macro invocation and expansion. where xx is a two-character alphanumeric counter of the number of macro instructions expanded. The following figure shows the macro invocation and expansion first time.

Conditional Macro Expansion Define conditional macro expansion If the macro is expanded depends upon some conditions in macro definition (depending on the arguments supplied in the macro expansion) then it is called as conditional macro expansion. &RECLTH. &BUFADR.Macro processors If the macro is invoked second time the labels may be expanded as $ABLOOP $ABEXIT. 13 BNMIT-ISE . Otherwise. IF-ELSE-ENDIF structure The IF statement evaluates a boolean expression that is its operand. The ENDIF statement terminates the conditional expression. and the statements following the ELSE are generated. If the value of this expression is TRUE. Figure (a) gives the definition of the macro RDBUFF with the parameters &INDEV. &EOR. these stetments are skipped. &MAXLTH. the statements following the IF are generated until an ELSE is encountered.

otherwise it retains its default value 0. The below program shows the expansion of Macro invocation statements that illustrate the operation of the IF statements . Macro-Time Variables: Macro-time variables (often called as SET Symbol) can be used to store working values during the macro expansion. 14 BNMIT-ISE . then &EORCK is set to 1 by using the directive SET. All such variables are initialized to zero by default. the symbol &EORCK is a macro time variable. Any symbol that begins with symbol & and not a macro instruction parameter is considered as macro-time variable. macro-time variables can be changed with their values using SET &EORCK SET 1 According to the above program if &EOR has any value.Macro processors In the above macro.

15 BNMIT-ISE . The testing of this condition. the specified Boolean expression is evaluated. are to be generated repeatedly as long as a particular condition is true.Macro processors The macro processor must maintain a symbol table that contains the value of all macro-time variables used. macro processor skips lines in DEFTAB until the next ENDIF. If the value of this expression TRUE. and the looping are done during the macro is under expansion. • Once it reaches ENDIF. The table is used to look up the current value of the macro-time variable whenever it is required. it resumes expanding the macro in the usual way. • The macro processor skips ahead in DEFTAB until it encounters next ELSE or ENDIF statement. • The macro processor then resumes normal macro expansion. • If an ELSE is found. There is another construct WHILE statement which specifies that the following line until the next ENDW statement. Entries in this table are modified when SET statements are processed. • The macro processor continues to process lines from the DEFTAB until it encounters the ELSE or ENDIF statement. WHILE-ENDW structure The macro-time IF-ELSE-ENDIF structure provides a mechanism for either generating(once) or skipping selected statements in the macro body. If the value of the expression is FALSE. The example shown below shows the usage of Macro-Time Looping statement. When an IF statement is encountered during the expansion of a macro.

Macro processors Figure below shows the expansion of a macro invocation statement. 16 BNMIT-ISE .

Positional parameters are suitable for the macro instruction. and if only few of the values need to be used in a typical invocation. o When ENDW is encountered. and takes action based on the new value. But if the macro has large number of parameters.Macro processors • • • When an WHILE statement is encountered during the expansion of a macro. FALSE o The macro processor skips ahead in DEFTAB until it finds the next ENDW statement and then resumes normal macro expansion. Arguments may appear in any order. the macro processor returns to the preceding WHILE. Null arguments no longer need to be used It is easier to read and much less error-prone than the positional method. a different type of parameter specification is required (for example. Keyword Macro Parameters All the macro instruction definitions used positional parameters. Example: Null arguments Keyword parameters • • • • Each argument value is written with a keyword that names the corresponding parameter. in many cases most of the parameters may have default values. re-evaluates the Boolean expression. the specified Boolean expression is evaluated. and the invocation may mention only the changes from the default values). TRUE o The macro processor continues to process lines from DEFTAB until it encounters the next ENDW statement. If an argument is to be omitted the macro invocation statement must contain a null argument mentioned with two consecutive commas. Example: 17 BNMIT-ISE . Parameters and arguments are matched according to their positions in the macro prototype and the macro invocation statement. The programmer needs to be careful while specifying the arguments.

Macro processors Macro Processor Design Options  Recursive Macro Expansion We have seen an example of the definition of one macro instruction by another. Then we use the macro RDCHAR in the defibition of RDBUFF. The Figure(a) shows the invocation of one macro by another macro.F1 Figure (c) In this case.LENGTH. The purpose of RDCHAR is to read one character from a specified device into register A. Figure (a) Figure (b) RDBUFF BUFFER. 18 BNMIT-ISE . But we have not dealt with the invocation of one macro by another. we assume that a related macro RDCHAR already exitsts. The definition of this macro appears in figure(b). however.

 General-Purpose Macro Processors 19 BNMIT-ISE .Macro processors Suppose that the algorithm one pass macroprocessor were applied to the macro invocation statement in figure(c).

which is then used as input to an assembler or compiler 20 BNMIT-ISE . o Process macro definitions o Expand macro invocations o Produce an expanded version of the source program.Macro processors  Macro Processing within Language Translators The macro processors we discussed are called “Preprocessors”.

Scanning input lines Searching tables Data format conversion It is easier to give diagnostic messages related to the source statements • You may also combine the macro processing functions with the Integrated Macro Processor • An integrated macro processor can potentially make use of any information about the source program that is extracted by the language translator. o Data structures required by the macro processor and the language translator can be combined (e.20 • a DO statement  DO 100 I = 1 • An assignment statement • DO100I: variable (blanks are not significant in FORTRAN) An integrated macro processor can support macro instructions that depend upon the context in which they occur. OPTAB and NAMTAB) o Utility subroutines can be used by both macro processor and the language translator. 21 BNMIT-ISE • .Macro processors language translator: Line-by-line macro processor Integrated macro processor Line-by-Line Macro Processor 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 Benefits o Avoid making an extra pass over the source program..g. o Ex (blanks are not significant in FORTRAN)  DO 100 I = 1.

Q. Q. List the various machine independent macro processor features. Q. Define macro. List and explain the different design options for a macro processor. What is macro? Develop a program for processing macro definition and macro calls. DEFTAB c. Explain the following a. Write algorithm for one pass macro processor and explain Q. With regard to machine independent macroprocessor features. What are the basic functions of a macro processor? Explain the various data structures used in the implementation of one pass macro processor Q.Macro processors Review Questions Q. Q.Write note on ‘Keyword macro parameters’ Q. NAMTAB b. Explain any two Q. explain the following i) Concatenation of Macro Parameters ii) Generation of unique labels Q. with example example how unique labels are generated during macro expnasion Q With an example explain conditional macro expansion or What is conditional macro expansion? With suitable examples explain any two structures used for conditional macro expnasion. List the data structures used in the design of a macro processor? Explain their functions. ARGTAB Q Write the algorithm for one pass macroprocessor? Q. write a note on processing macro within language translators 22 BNMIT-ISE .

Sign up to vote on this title
UsefulNot useful