COP 3402 Systems Programming Dr. Ali Orooji School of EECS University of Central Florida
Chapter 4 - MACRO PROCESSORS Function macro expansion; substitute macro bodies for macro invocations Source Program with macro invocations Macro procesor Source Program with macros expanded assembler …
Two-Pass Macro Processor process macro definitions Pass 2 expand macro invocations Problem would not allow the body of one macro to contain definitions of other macros
PMAC MACRO ... C1MAC MACRO ... ... MEND C2MAC MACRO ...
One-Pass Macro Processor . will process macro definitions and expand macro invocations at the same time; will alternate between macro definition and macro expansion . the definition of a macro must come before any of its invocations Macro Processor Data Structures definition table DEFTAB DEFTBL - macro definitions are stored in this table - references to parameters are converted to a positional notation for efficiency in substituting arguments
name table NAMTAB NAMTBL - macro names are stored in this table along with pointers to the beginning and end of definitions in DEFTAB; serves as an index to DEFTAB argument table ARGTAB ARGTBL - used during the expansion of macro invocations - arguments in a macro invocation are stored in this table Macro Processor Logic pages 184-185 of text Figure 4.5 Algorithm for a one-pass macro processor
Machine-Independent Macro Processor Features Section 4.2 Machine-Independent Macro Processor Features Generation of Unique Labels MAC MACRO ... ... LBL ... MEND first macro expansion AALBL second macro expansion ABLBL
Conditional Macro Expansion IF ( Boolean expression ) ... ELSE ENDIF - - - - - - - - - - WHILE ( Boolean expression ) ENDW
Macro Processor Design Options Section 4.3 Macro Processor Design Options Recursive Macro Expansion Problems with previous macro processor - when a macro invokes another macro ARGTAB is overwritten - when the second macro finishes EXPANDING is set to FALSE
Solution - if macro processor is written in a high-level language that supports recursion, then there won't be problems since the compiler saves the previous values of variables on recursive calls - if macro processor must be written in a programming language that does not support recursion, then the system programmer must save the data values (this can be done using a stack)
General-Purpose Macro Processors not dependent on any particular programming language; can be used with a variety of different languages Problems - problems related to different programming languages . comments . facilities for grouping . tokens (identifiers, operators, ...) - syntax used for macro definitions and macro invocations
Macro Processing within Language Translators previous macro processor: pre-processor alternative: combining the macro processing functions with the language translator . line-by-line macro processor . integrated macro processor