64
1 Macro Processors Dr. Poonam Saini [email protected]

Macro

Embed Size (px)

DESCRIPTION

Macros system software

Citation preview

1

Macro Processors

Dr. Poonam [email protected]

2

Introduction

Concept» A macro instruction is a notational convenience for the

programmer» Macros 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) and leave mechanical details to be handled by the macro processor

» The macro processor replaces each macro invocation with the corresponding sequence of statements (expanding)

3

Macro vs. Subroutine

Macro» the statement of expansion are generated each time the

macro are invokedSubroutine» the statement in a subroutine appears only once

• Use of a macro name in mnemonic field of an assembly statement leads to its expansion whereas use of a subroutine name in a call instruction leads to its execution

4

Macro Processor

Recognize macro definitionsSave the macro definitionRecognize macro callsExpand macro calls

Source Code

(with macro)

Macro Processor

Expanded Code

Compiler or Assembler

obj

5

Why macro?

Following sequence of instructions is used to increment the value in a memory word by a constant:» Move the value from memory word into a machine register» Increment the value in the machine register» Move the new value into the memory word

• as the instruction sequence- MOVE-ADD-MOVE may be used a number of times in a program, it is convenient to define a MACRO

• using lexical expansion- macro call INCR A, B, AREG can lead to generation of MOVE-ADD-MOVE instruction sequence to increment A by the value of B using AREG to perform arithmetic• use of semantic expansion can enable the instruction sequence to be adapted to the types A and B. E.g., for Intel 8088, an INC instruction would be generated iff A is a byte operand and B has the value 1.

6

Macro Definition and Call

A macro definition is enclosed between a macro header statement and a macro end statement

Typically located- start of program and consists of:

A macro prototype statementOne or more model statementsmacro pre-processor statements

7

Macro prototype statement declares the name of the macro and the names and kinds of its parameters

<macro name> [<formal parameter spec> [,..]]

(mnemonic field) &<parameter name> [ <parameter kind>]A model statement is a statement from which an assembly language statement may be generated during macro expansionA pre-processor statement is used to perform auxiliary functions during macro expansion

8

Macro call

A macro is called by writing the macro name in the mnemonic field of an assembly statement.

<macro name> [ <actual parameter spec>[, ..]]

Operand specification in assembly language statementexample» MACRO» INCR &MEM_VAL, &INCR_VAL, &REG» MOVER &REG, &MEM_VAL» ADD &REG, &INCR_VAL» MOVEM &REG, &MEM_VAL» MEND

9

Macro expansion

A macro call leads to macro expansionMacro call statement is replaced by a sequence of assembly statementsTo differentiate between the original statement and statement resulting from macro expansion ---- each expanded statement is marked with ‘+’ preceding its label fieldTwo notations-» Expansion time control flow» Lexical substitution

10

Flow of control during expansion

Default- sequential Absence of pre-processor statement: model statements are visited sequentially starting with statement - macro prototype statement -ending with statement preceding MEND statementA pre-processor statement can alter the flow of control during expansion -----why??» Some model statements are either never visited during expansion,

or» Repeatedly visited during expansion

Former results in conditional expansion and the latter in expansiontime loops

11

Outline of macro expansion

Flow of control during macro expansion is implemented using macro expansion counter (MEC)

AlgorithmI. MEC:= statement number of first statement following the prototype

statementII. While statement pointed by MEC is not a MEND statement

a) If a model statement theni. Expand the statementii. MEC:= MEC+1;

b) Else (i.e., a pre-processor directive)i. MEC:= new value specified in the statement;

III. Exit from macro expansion

12

Lexical substitution

Model statement has 3 types of strings» An ordinary string which stands for itself» The name of a formal parameter preceded by the character ‘&’» The name of a pre-processor variable also preceded by the

character ‘&’

During lexical expansion-string of type 1- retained without substitutionstring of type 2 and 3 – replaced by the ‘values’ of the formal

parameters or pre-processor variables

(rules for determining the value of a formal parameter depend on kind of parameter- positional and keyword)

13

Parameter Substitution -- Example

SourceSTRG MACRO &a1, &a2, &a3

STA &a1STB &a2STX &a3MEND

.STRG DATA1, DATA2, DATA3

.STRG DATA4, DATA5, DATA6

.

.

Expanded souce...

STA DATA1STB DATA2STX DATA3

.STA DATA4STB DATA5STX DATA6

.

{{

14

Positional parameter

The value of a positional formal parameter XYZ is determined by the rule of positional association as:

» Find the ordinal position of XYZ in the list of formal parameters in macro prototype statement

» Find the actual parameter specification occupying the same ordinal position in the list of actual parameters in the macro call statement

Consider call- INCR A, B, AREG ----following positional association: Formal parameter valueMEM_VAL AINCR_VAL BREG AREG

Lexical expansion of model statements leads to code+ MOVER AREG, A+ ADD AREG, B+ MOVEM AREG, A

15

Keyword parameter

The value of a formal parameter XYZ is determined by the rule of keyword association as:

» Find the actual parameter specification which has the form XYZ = <ordinary string>

Following macro call-----------are equivalent INCR_M MEM_VAL=A, INCR_VAL=B, REG=AREGINCR_M INCR_VAL=B, REG=AREG, MEM_VAL=A,

MACRO INCR_M &MEM_VAL=, &INCR_VAL=, &REGMOVER &REG, &MEM_VALADD &REG, &INCR_VALMOVEM &REG, &MEM_VALMEND

16

Nested Macro

A model statement in a macro may constitute a call to another macro. Such calls are known as nested macro calls.» macro containing nested call – outer macro» called macro – inner macro

Expansion of nested macro calls- LIFO rule» In a structure of nested macro calls, expansion of the latest macro

call (i.e., the innermost macro call) is completed first

17

Nested Macros Definition

Macro definition within macros» process macro definition during expansion time

Example

18

19

Macros and Macrox

MACROS- standard SIC systemMACROX- SIC/XE system

» Defining macros or macrox does not define RDBUFF and other macro instructions.

» The definitions are processed only when an invocation of MACROS or MACROX is expanded

20

One-Pass Macro Processor

Prerequisite» every macro must be defined before it is called

Sub-procedures» macro definition: DEFINE» macro invocation: EXPAND

DEFINE

EXPAND

PROCESSLINEDEFTAB

NAMTAB

ARGTAB

MACRO

CALL

21

Data structures

DEFTAB (Definition table)» Contains macro prototype and the statements that make up the

macro body » Comment lines from the macro definitions are not entered into

DEFTAB as they are not part of macro expansion» References to the macro instruction parameters are converted to a

positional notation for efficiency in substituting arguments NAMTAB (Name table)» Macro names are entered into NAMTAB» Serves as an index to DEFTAB» For each macro instruction defined, NAMTAB contains pointers to

the beginning and end of the definition in DEFTAB

22

ARGTAB (Argument table)» 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

» As the macro is expanded, arguments from AGRTAB are substituted for the corresponding parameters in the macro body

23

EXPANDING

F1

BUFFER

LENGTH

ARGTAB

CLOOP RDBUFF F1, BUFFER, LENGTH (Read record into buffer)

? Which notation / parameter substitution has been used in above figure

24

One-Pass Macro Processor That Allows Nested Macro Definition

Sub-procedures» macro definition: DEFINE» macro invocation: EXPAND

EXPAND may invoke DEFINE when encounter macro definition

DEFINE

EXPAND

PROCESSLINEDEFTAB

NAMTAB

ARGTAB

Expanding

MACRO

CALL

MACRO Definition

25

28

Macro processor algorithm

procedure DEFINE- makes appropriate entries in DEFTABand NAMTABEXPAND is called to set up the argument values in AGRTAB and expand a macro invocation statementprocedure GETLINE (can be called several times) gets the next line to be processed» Depending upon whether the boolean variable Expanding is set to

TRUE or FALSE, – the line may come from DEFTAB (next line of a macro being

expanded), or– from the input file

29

How to find the MEND that corresponds to original MACRO directive (refer slide 18 to identify problem)

Solution» Define procedure maintains counter named LEVEL» Each time a MACRO directive is read, the value of LEVEL is

increased by 1» Each time a MEND directive is read, the value of LEVEL is

decreased by 1» When LEVEL reaches 0, MEND corresponding to original MACRO

directive has been found

30

1-Pass Macro Processor

MACROPROCESSOR

GETLINEPROCESSLINE

EXPANDING=FALSE

PROCESSLINE

GETLINE

EXPANDING

TRUE

READ FROMINPUT

READ FROMDEFTAB

FALSE

DEFINE

EXPAND

EXPANDING=TRUE

GETLINEPROCESSLINE

GETLINE

31

ARGTAB

DEFINE

EXPAND

DEFTAB

NAMTAB

ARGTAB

MACRO Definition

Macro Invocation

PROCESSLINEGETLINE

32

Comparison of Macro Processors Design

Single pass» 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 notTwo pass algorithm» Pass1: Recognize macro definitions» Pass2: Recognize macro calls» nested macro definitions are not allowed

33

Machine-independent macro processor features

Concatenation of macro parametersGeneration of unique labelsConditional macro expansionKeyword macro parameters

34

Concatenation of Macro Parameters

Consider a situation-A program contains one series of variables named by the symbol XA1, XA2, XA3, …., another series named by XB1, XB2, XB3, ….etc.

If similar processing is to be done each series of variables, the processing is incorporated into macro instruction

Parameter to such a macro instruction could specify the series of variables to be operated on (A, B, etc.) Macro processor would use the parameter to construct the symbols required in macro expansion (XA1, XB1, etc.)

35

Concatenation of Macro Parameters

Suppose parameter is named &ID» LDA X&ID1

Special concatenation operator ‘→’» LDA X&ID→1

*Macro processor deletes all occurrences of →immediately after parameter substitution

Example:

36

Generation of Unique Labels

In general, it is not possible for the body of macro instruction to contain labels of usual kindThis leads to use of relative addressing at the source statement levelExample – consider definition of WRBUFF » WRBUFF MACRO &OUTDEV, &BUFADR, &RECLTH

– TD =X’&OUTDEV TEST OUTPUT DEVICE

Label on this instruction would be defined twice- once for each invocation of WRBUFF (see fig 4. of L.Beck) To avoid duplicate definitions which in turn would prevent correct assembly of the resulting expanded program, unique labels can be used

37

Generation of Unique Labels

No label- usage of relative operands for jump instructions e.g.,

– JEQ *-3 LOOP UNTILL READY– JLT *-14

» acceptable for shorter jumps» inconvenient, error-prone, difficult to read for longer jumps

spanning several instructionsMany macro processors allows creation of special labels within macro instructions

– $LOOP TD =X’&INDEV’» 1st call:

– $AALOOP TD =X’F1’» 2nd call:

– $ABLOOP TD =X’F1’

38

Labels used within macro body begin with special character $Each symbol beginning with $ has been modified by replacing $ with $AA» In general, $ will be replaced by $xx, where xx is a two-character

alphanumeric counter of the number of macro instructions expanded

For the first macro expansion, xx will have the value AA. For succeeding expansions, xx will be set to AB, AC, etc. » If only alphabetic and numeric characters are allowed in xx, the

counter may provide 1296 macro expansions in a single program

RDBUFF F1, BUFFER, LENGTH

41

Conditional Macro Expansion

ProblemTill now, each invocation of a particular macro was expanded into same sequence of statements Although, these statements could be varied by the substitution of parameters, however, the form of statement and order in which they appeared were unchanged

SolutionWith conditional macro expansion, we can modify the sequence of statements generated for a macro expansion, using appropriate arguments in macro invocations. » It increases flexibility and power of a macro language

42

Conditional Macro Expansion

Macro-time conditional statements» IF-ELSE-ENDIF

Macro-time variables» any symbol that begins with the character & and that is not a

macro instruction parameter is assumed a MTV» macro-time variables are initialized to 0» macro-time variables can be changed with their values using SET» E.g., RDBUFF in next figure has two additional parameter

– &EOR (specifies a hexadecimal character code that marks the end of a record)

– &MAXLTH (specifies the maximum length record that can be read)

RDBUFF F3, BUF, RECL, 04, 2048

RDBUFF 0E, BUFFER, LENGTH, , 80

RDBUFF F1, BUFF, RLENG, 04

46

Conditional Macro Expansion (Cont.)

Statements on lines 44-48 illustrate a simple example of macro-time conditional expansion» IF evaluates a Boolean expression that is its operand » If value of expression is TRUE, the statements following IF are

generated until an ELSE is encountered» Otherwise, these statements are skipped and statements following

ELSE are generated » ENDIF terminates conditional expression started by IF

If parameter &MAXLTH is equal to null string (i.e., if corresponding argument is omitted in the macro invocation), the statement on line 45 is generated, otherwise 47 is generated

47

Implementation of conditional macro-expansion features

Macro processor must maintain a symbol table that contains the values of all macro-time variables usedEntries in symbol table are made or modified when SET statements are processed.» The table is used to look up current value of a macro-time variable

whenever it is required» During expansion, boolean expression in IF is evaluated. » If value is TRUE, macro processor process lines from DEFTAB

until next ELSE or ENDIF– If ELSE found, macro processor skips lines from DEFTAB until next

ENDIF. » If value is FALSE, macro processor skips ahead in DEFTAB until

next ELSE or ENDIF

48

Keyword Macro Parameters

All macro instructions till now used positional parameters i.e., parameters and arguments are associated with each other according to their positions in macro prototype and macro invocationPositional parameters are suitable for most macro instructions, however, if a macro has large number of parameters and only few of these are given values in a typical invocation, different form of parameters are useful

49

Macro Processor Design Options

Recursive Macro Expansion

General-Purpose Macro Processors

Macro Processing within Language Translators

50

Previous examples deals with definition of one macro instruction by another

RME deals with invocation of one macro by another

Recursive Macro Expansion

51

52

RDCHAR is a macro and is invoked in macro RDBUFF

Unfortunately, the MP algorithm discussed so far cannot process this. Why?

When RDBUFF is invoked, procedure EXPAND is called,EXPANDING is set to true, and arguments (BUFFER, LENGTH, F1)are entered to ARGTAB. During expansion, RDCHAR is invoked,EXPAND is called again, new argument (F1) enters ARGTAB (to erasethe arguments of RDBUFF). After returning from EXPAND,EXPANDING is set to false, which stops the expansion of RDBUFF.

Consider a situation

53

ARGTABS during expansion

PARAMETER VALUE1 BUFFER2 LENGTH3 F14 (unused)

PARAMETER VALUE1 F12 (unused) . .. .

ARGTAB for Procedure EXPAND

ARGTAB for Procedure RDCHAR

54

When the end of definition of RDCHAR was recognized, EXPANDING would be set to false

Thus, macro processor would “forget” that it has been in the middle of expanding a macro when it has encountered RDCHAR statement

In addition, the arguments from original macro invocation (RDBUFF) would be lost---(overwrite)

Cause: recursive call of procedure EXPAND

When the RDBUFF macro invocation is encountered, EXPAND is called. Later, it calls PROCESSLINE for line 50, which results in another call to EXPAND before a return is made from the original call.

Problem and Solution

55

A similar problem would occur with PROCESSLINE since this procedure too would be called recursively.

These problems are not difficult to solve if the macro processor is being written in a programming language that allows recursive calls.

If a programming language that supports recursion is not available, the programmer must take care of handling such items as

• return addresses and • values of local variables (that is, handling by looping structure and data values being saved on a stack).

56

What is the most common use of macro processor?Simple- to aid assembler language programming

Often such macros are combined with or related to an assemblerMacro processor have also been developed for high level languages (?)Special-purpose macro processors are similar in general function and approachHowever, details differs from language to language

General-purpose Macro Processors

57

The general-purpose macro processors are not dependent on anyparticular programming language, but can be used with a variety ofdifferent languages.

There are relatively few general-purpose macro processors.The major reason is the large number of details that must be dealt within a

real programming language.A general-purpose facility must provide some way for a user to define the

specific set of rules to be followed.

General-purpose Macro Processors

58

Case 1: Comments are usually ignored by a macro processor (at leastin scanning for parameters). However, each programming languagehas its own methods for identifying comments.

Case 2: Another difference between programming languages isrelated to their facilities for grouping together terms, expressions, orstatements. Example: begin-end, { }, ..

A general-purpose macro processor may need to take these groupingsinto account in scanning the source statements.

Difficulties / Disadvantages

59

Case 3: Languages differ substantially in their restrictions on thelength of identifiers and the rules for the formation of constants(i.e. the tokens of the programming language – for example,identifiers, constants, operators, and keywords).Example: problem with multiple-character operators inFORTRAN like **, :=

Case 4: Another potential problem with general-purpose macroprocessors involves the syntax used for macro definitions andmacro invocation statements.

With most special-purpose macro processors, macroinvocations are very similar in form to statements in the sourceprogramming language.

60

Programmer does not need to learn about a different macro facilityfor each compiler or assembler language

Training time and cost is reduced

Costs involved in producing a GP-MP is greater than language-specific processor, however, this expense does not need to berepeated for each language

Substantial overall saving in software development cost as well asmaintenance effort

Advantages

61

The macro processors might be called preprocessors. Consider an alternative: combining the macro processing functions with the language translator itself.

The simplest method of achieving this sort of combination is a line-by-line macro processor. Using this approach, the macro processor reads the source program statements and performs all of its functions as previously described.

The output lines are then passed to the language translator as they are generated (one at a time), instead of being written to an expanded source file.

Thus, the macro processor operates as a sort of input routine for the assembler or compiler.

Macro processor within Language Translator

62

Although a line-by-line macro processor may use some of thesame utility routines as the language translator, the functions ofmacro processing and program translation are still relativelyindependent.

There exists even closer cooperation between the macroprocessor and the assembler or compiler. Such a scheme can bethought of as a language translator with an integrated macroprocessor.

63

An integrated macro processor can potentially make use of anyinformation about the source program that is extracted by thelanguage translator.

For example, at a relatively simple level of cooperation, the macroprocessor may use the results of such translator operations as scanning forsymbols, constants, etc.

The macro processor can simply use the results without being involved insuch details as multiple-character operators, continuation lines, and the rulesfor token formation.

64

There are disadvantages to integrated and line-by-line macro processors:

They must be specially designed and written to work with a particular implementation of an assembler or compiler.

The costs of macro processor development must be added to the cost of the language translator, resulting in a more expensive piece of software.

The size may be a problem if the translator is to run on a computer with limited memory.

Disadvantages