Slide S4-01 Chapter 4 - MACRO PROCESSORS Function macro expansion; substitute macro bodies for macro invocations | source program | | with macro | | invocations | | | macro processor | | | source program | | with macros | | expanded | | | assembler | | ... ====================================================================== Slide S4-02 Two-Pass Macro Processor Pass 1 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 ... ... MEND ... MEND ... ====================================================================== Slide S4-03 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 Slide S4-04 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 ====================================================================== Slide S4-05 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 ====================================================================== Slide S4-06 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) Slide S4-07 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 ====================================================================== ====================================================================== ======================================================================