Macro Processors (MP) Macro: Macro Processors (MP): Examples:

Slides:



Advertisements
Similar presentations
COP 3402 Systems Programming
Advertisements

Intermediate Code Generation
1 Compiler Construction Intermediate Code Generation.
The Assembly Language Level
Macro Processor.
Lecture 2 Introduction to C Programming
An introduction to systems programming
1 Chapter 4 Macro Processors Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
CS 201 Functions Debzani Deb.
Chapter 4 Macro Processors
 2007 Pearson Education, Inc. All rights reserved C Program Control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Chapter 4 Macro Processors Professor Gwan-Hwan Hwang Dept. Computer Science and Information Engineering National Taiwan Normal University 9/17/2009.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
A Simple Two-Pass Assembler
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Chapter 1 Computer architecture Languages: machine, assembly, high
An introduction to systems programming
Machine Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro.
4. Macro Processors1 Chapter IV: Macro Processors Overview: r To study the design and implementation of macro processors. r A macro represents a commonly.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
JavaScript, Fourth Edition
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples.
Programming Fundamentals Enumerations and Functions.
1 Chapter 4 Macro Processors. 2 Introduction A macro instruction (abbreviated to macro) is simply a notational convenience for the programmer. A macro.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 11–Macro-Processors.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Chapter 4 – C Program Control
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Scripts & Functions Scripts and functions are contained in .m-files
Programmazione I a.a. 2017/2018.
User-Defined Functions
MACRO Processors CSCI/CMPE 3334 David Egle.
Chapter 4 Macro Processors
C Preprocessor(CPP).
Algorithm for One-pass Macro Processor
Chapter 6 Control Statements: Part 2
A Simple Two-Pass Assembler
Chapter 1 Computer architecture Languages: machine, assembly, high
UNIT – IV MACRO PROCESSORS
Conditional Compilation
CHAP 4 MACRO PROCESSORS.
SPL – PS1 Introduction to C++.
Presentation transcript:

Macro Processors (MP) Macro: Macro Processors (MP): Examples: A commonly used group of statements. Macro Processors (MP): Expands macros, i.e, each macro reference is replaced by its corresponding group of the statements Examples: SAVEREGS LOADREGS In general, a MP does not perform analysis of the text. So can be considered to be independent from machines. MP for assembler, compiler, OS command language Also general-purpose MP. Macros are also part of an assembly program.

Basic Macro processor Functions Macro definition, invocation, and expansion. Assembler Directives for Macro definition MACRO and MEND name & params forms a prototype for the macro. Use of Macros in a SIC/EX program (Figure 4.1) RDBUF and WRBUF Line 190: Macro invocation, name arguments Program from Figure 4.1 with Macros expanded (Figure 4.2) Macro invocation and function call: MP expands a macro for each invocation Replaces a parameter with its corresponding argument. Macro is removed from the object code. In contrast, a function is kept in the program, a function call will jump to the function, and the call returns after the function is finished. Similarly, arguments are used to replace parameters. Comparison of Macro and function call Notes: No labels in a macro body. Why? JEQ *-3 in Line 140 and JLT *-14 in Line 155. An inconvenient and error-prone method. How to avoid it?

MP algorithm and Data structures Typical two-pass MP: First pass: process all macro definitions Second pass: expand all macro invocations. But, it will not allow the body of one macro to contain definitions of other macros. Nested definition of macros is useful some time. Example of the definition of macros within a macro body (Figure 4.3) Change the invocation to MACROS or MACROX One-pass MP Suppose allowing alternation between macro definition and macro expansion Macro definition must be before its invocation. satisfy programmers’ expectation. Three Data structures: NAMTAB, DEFTAB, and ARGTAB NAMTAB points to both the beginning and end of a macro ?1, ?2, … are placed in the macro definition in DEFTAB ?1, ?2…, are replaced by the corresponding indexed arguments from ARGTAB during expansion Contents of MP tables for Program in Fig. 4.1 (Figure 4.4) Algoritm for a one-pass MP (Figure 4.5) and Figure 4.5 (Cont'd) Procedure DEFINE to process a macro definition and enter symbols and macro in NAMTAB and DEFTAB. Procedure EXPAND to enter arguments to ARGTAB and expand the macro. Procedure GETLINE to read a line from source file or DEFTAB. LEVEL to record the level of macro definition for nested macros. Standard macro library.

Machine-independent MP features Concatenation of MACRO parameters &ID is a parameter and X&ID1, X&ID2, … When argument is A, get XA1, XA2, … If &ID and &ID1 are both parameters, X&ID->1, X&ID->2, … Concatenation of macro parameters Generation of unique labels for macro Too long relative jump may be inconvenient, error prone, and difficult to read. $ prefixed to a symbol to indicate a dynamic label $ is replaced with $AA, $AB when expansion. So $Loop will be $AALoop for the first expansion and $ABLoop for the second expansion. Generation of unique labels within macro expansion Conditional Macro expansion Modify the sequence of statements during expansion depending on the arguments provided. More generally, called conditional assembly. &EOR, &MAXLTH: parameters to control conditional macro expansion. IF … ELSE …ENDIF to indicate the expansion depending on the condition SET is a MP directive, and a symbol beginning with & but not in parameters, is a macro time symbol, or a set symbol Use of macro-time conditional statements (Figure 4.8) and Figure 4.8 (Cont'd) (b,c,d) with three different invocations Maintain a macro time symbol table Evaluate the Boolean expression in IF Use the statements in IF or ELSE part for expansion. No nested IF. But can be extended to allow it. All conditional macro expansion must be finished before assembly (only on sequence of source statements without any conditional expansion directive) WHILE … ENDW to generate repeated expansion of statements within WHILE until the Boolean expression in WHILE is evaluated to FALSE. %NITEMS(…) : a macro processor function. Use of macro time looping statements Does not allow nested WHILE. But can be extended. Keyword parameters Previously, positional parameters, arguments match parameters by positions. Except the last arguments which may be omitted, other omitted argument must have ,, to indicate its position. So keyword parameters. &key-work-para=default-value. Use of keyword parameters in macro instructions (Figure 4.10) and Figure 4.10 (Cont'd)

MP Design Options --Recursive Macro Expansion Example of nested macro invocation (Figure 4.11) 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 erase the arguments of RDBUFF). After returning from EXPAND, EXPANDING is set to false, which stops the expansion of RDBUFF. EXPAND is not called recursively. If recursive call is allowed, it will work. The essential technique dealing with recursive calls is Stack. Chapter 5, compiler will determine whether a language allows recursive function call.

MP Design Options --MP within language translators MP as an input subroutine (pre-processing): Read the source program line by line Process the macro if definition and invocation are encountered The output lines are passed to assembler or compiler. Advantages: Avoid passing source program again Share Data structures. Share utility functions. Easier to diagnose errors. Integrated MP: Assembler or compiler takes main control to do the analysis DO 100 I = 1, 20 (which is a loop statement) and DO 100 I =100, which is an assignment. When encountering macros, call MP to finish necessary processing. Can support context-dependent features, For example, a macro can specify a substitution to be applied only to variables or constants of a certain type, or only to variables appearing as loop indices in DO statement. The expansion of a macro could also depend on a variety of characteristics of its arguments. Disadvantages: Specially designed Development cost Larger and more complex.

MP Design Options --General-purpose MPs Advantages: No need to care for different facilities for different languages. Once developed (even cost more), can be used for any language, and also for a long time. Disadvantages: Many details associated with a real language, so a general purpose MP must provide some way for a user to define specific set of rules to be followed. For examples, different comment tags for different language. Grouping delimiter: Begin … End, {…} Length of identifiers, format of constants, :=, ** (double symbols OP), blanks as delimiter?, line continuation, statement formatting. Different syntax for macro definitions and invocations in different languages. Briefly discuss a general purpose MP in 4.4.3.

Implementation examples --Microsoft MASM MP Integrated with the pass 1 of MASM assembler All basic functions of a MP, including nested macro definition. Also recursive macro invocation A macro may be redefined within a program. Main differences from SIC MP: Conditional macro expansion, more generally conditional assembly Not only related to macros but also outside macros. Examples of MASM macro and conditional statements (Figure 4.12) Word or double word, so register AX or EAX No need & for parameters EXIT is a local label. When the macro is expanded, local label is replaced by unique name ??xxxx. Line 4 to 8: nested conditional statements. Line 10, & is a concatenation operator. ;; comments Iteration statement IRP: Example of MASM iteration statement (Figure 4.13) Macro time variable S, which can have values from <…> For each value of S, statements between IRP and ENDM are generated once.

Implementation examples --ANSI C Macro language C high level language Called pre-processor For examples: #define NULL 0, #define EOF (-1), #define EQ == Also called constant definition More complicate, macro with parameters: #define ABSDIFF(x,y) ((x)>(y)? (x)-(y): (y)-(x)) So ABSDIFF (I+1,J-5), ABSDIFF(I,3.1415), ABSDIFF(‘D’,’A’). #define ABSDIFF(x,y) x>y? x-y: y-x will result in problem. Note: the importance of parentheses, due to the simple string substitution. Conditional compilation Example1 #ifndef BUFFER_SIZE #define BUFFER_SIZE 1024 #endif Example 2 #define DEBUG 1 … #if DEBUG==1 printf(….); pintf(…) will appears in the program. If change #define DEBUG 0, then the printf(…) will not. Also #ifdef DEBUG printf(…); In this case, #define DEBUG will have printf(…) in the program and printf(…) will not appear in program without #define DEBUG

Implementation examples --The ELENA Macro Processor (general-purpose) Macro definition: a header and a body Header is not required to have any special form. Consists of a sequence of keywords and parameter markers (beginning with %) and at least one of the first two tokens must be a keyword. Body can be defined in any language specific form. For examples: A macro header: %1 = %2 +%3 (=, + are keywords) Can be invoked as ALPHA=BETA+GAMMA A header: ADD %1 TO THE VALUE OF %2 Can be invoked as ADD 10 TO THE VALUE OF INDEX Examples of ELENA macro definition and invocation with different languages (Figure 4.14) Same header but the body is C or x86 assembly language. Example of ELENA macro-time variable and repeat expansion (Figure 4.15) Very generic in term of body Complexity: No macro “name” in the headers ADD %1 TO %2 or ADD %1 TO THE FIRST ELEMENT OF %2 Even not clear from macro invocation which token is keyword and which is parameter An invocation: DISPLAY TABLE can be for a macro with header DISPLAY %1 or %1 TABLE Note: DISPLAY and TABLE cannot be both parameters, since at least one must be keyword. ELENA deals with it By constructing an index of all macro headers according to the keywords in the first two tokens of the header. Potential macro invocations are compared against all headers with keywords that match at least one of their first two tokens. For example A SUM B, C would be compared with all macro headers in which the first token is A or the second token is SUM. When more than one headers match, the header with the fewest parameters is selected. A=B+1 can match %1=%2+%3 or %1=%2+1, in this case, %1=%2+1 is selected. If same amount of parameters, the most recently defined one is selected.

Summary Basic functions Features Data structures and algorithms Definitions and expansions Features Labels, nested definitions, recursive invocations. Conditional macro processing. IF…ENDIF, WHILE…ENDW, macro-time variables and instructions Keyword parameters Data structures and algorithms NAMTAB, DEFTAB, ARGTAB For recursive invocation, STACK. Relation between macro processors and assemblers