Chapter 4 Macro Processors

Slides:



Advertisements
Similar presentations
COP 3402 Systems Programming
Advertisements

Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
The Assembly Language Level
Macro Processor.
Macro Processors (MP) Macro: Macro Processors (MP): Examples:
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
An introduction to systems programming
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
1 Chapter 4 Macro Processors Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
CS 201 Functions Debzani Deb.
Chapter 4 Macro Processors
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
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.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
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 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Objective At the conclusion of this chapter you will be able to:
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
Chapter 9: Value-Returning Functions
Chapter 4 – C Program Control
User-Written Functions
Introduction to the C Language
Introduction to C++ Systems Programming.
Chapter 2 - Introduction to C Programming
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Introduction To Flowcharting
Programming Fundamentals
Chapter 2 - Introduction to C Programming
Programmazione I a.a. 2017/2018.
User-Defined Functions
Assembler Design Options
MACRO Processors CSCI/CMPE 3334 David Egle.
Chapter 10 Programming Fundamentals with JavaScript
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Chapter 6 Control Statements: Part 2
Chapter 2 - Introduction to C Programming
A Simple Two-Pass Assembler
Chapter 2 - Introduction to C Programming
UNIT – IV MACRO PROCESSORS
CHAP 4 MACRO PROCESSORS.
Presentation transcript:

Chapter 4 Macro Processors UNIT -6 MACRO PROCESSORS Subject Name:Sysytem Software Subject Code: 10SCS52 Department:CSE Date:3-11-2014

Introduction A macro processor is a program that reads a file (or files) and scans them for certain keywords. When a keyword is found, it is replaced by some text. The keyword/text combination is called a macro. A simple example is the C language preprocessor. When you write #define MAX_BANANAS 6 int banana; for (banana = 0; banana < MAX_BANANAS; banana++) { ...; }

Introduction A macro represents a commonly used group of statements in the source programming language The macro processor replaces each macro instruction with the corresponding group of source language statement, this is called expanding macros The functions of a macro processor essentially involve the substitution of one group of characters or lines for another

Macro Definition and Expansion The MACRO statement identifies the beginning of a macro definition The symbol in the label field(RDBUFF) is the name of the instruction. The entries in the operand field identify the parameter of the macro instruction Each parameter begins with the character & The MEND assembler directive marks the end of the macro definition A macro invocation statement gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro

Use of macros in a SIC/XE Program(3/1)

Use of macros in a SIC/XE Program(3/2)

Use of macros in a SIC/XE Program(3/3)

Program with Macro Expanded(3/1)

Program with Macro Expanded(3/2)

Program with Macro Expanded(3/3)

Macro Processor Data Structures The macro definitions themselves are stored in definition table (DEFTAB), which contains the macro prototype and the statements that make up the macro body The macro names are entered into NAMTAB, which serves as an index to DEFTAB For each macro instruction defined NAMTAB contains pointers to the beginning and end of the definition in DEFTAB The third data structure is an argument table (ARGTAB), which is 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

Macro Processor Data Structures

Algorithm for a One-pass Macro Processor

Algorithm for a One-pass Macro Processor

Algorithm for a One-pass Macro Processor

The procedure PROCESSING The procedure DEFINE Called when the beginning of a macro definition is recognized, makes the appropriate entries in DEFTAB and NAMTAB. The procedure EXPAND Called to set up the argument values in ARGTAB and expand a macro invocation statement. The procedure GETLINE Called at several points in the algorithm, gets the next line to be processed. EXPANDING is set to TRUE or FALSE.

Machine-Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro Parameters

Concatenation of Macro Parameters Most macro processors allow parameters to concatenated with other character strings If similar processing is to be performed on each series of variables, the programmer might want to incorporate this processing in to a macro instruction. Suppose there is a parameter named &ID, then The body of the macro definition might contain a statement like “LDA X&ID1” in which the parameter &ID is concatenated after the character string X and before the character string 1 If the macro definition contained both &ID and &ID1 as parameters, the situation would be ambiguous Most macro processors deal with this problem by providing a special concatenation operator (e.g. ) LDA X&ID1

Concatenation of Macro Parameters

Generation of Unique Labels Following figure shows one method to generate unique labels within a macro expansion. Labels used within the macro body begin with special character $. Each symbol beginning with $ has been modified by replacing $ with $xx, where xx is a two character alphanumeric counter of the number of macro instructions expanded For the first macro expansions, xx will have the value AA For succeeding macro expansions, xx will be set to AB, AC, etc

Generation of Unique Labels with Macro Expansion(2/1)

Generation of Unique Labels with Macro Expansion(2/1)

Conditional Macro Expansion Most macro processors can modify the sequence of statements generated for a macro expansion, depending on the arguments supplied in the macro invocation The IF statement evaluates a Boolean expression that is its operand If the value of this expression is TRUE, the statements following the IF are generated until an ELSE is encountered Otherwise, these statements are skipped, and the statements following the ELSE are generated The ENDIF statement terminates the conditional expression that was begun by the IF statement

Use of Macro-time Conditional Statements

Use of Macro-time Conditional Statements

Use of Macro-time Conditional Statements RDBUFF F3, BUF, RECL, 04, 2048

Use of Macro-time Conditional Statements

Use of Macro-time Conditional Statements

Conditional Macro Expansion(2/2) WHILE: a macro-time looping statement The WHILE statement specifies that the following lines, until the next ENDW statement, are to be generated repeatedly as long as a particular condition is true The macro-time variable &CTR is used to count the number of times the lines following the WHILE statement have been generated

Use of Macro-time looping Statements(2/1)

Use of Macro-time looping Statements(2/2)

Keyword Macro Parameters Positional parameter: parameters and arguments were associated with each other according to their positions in the macro prototype and the macro invocation statement

Keyword parameters: each argument value is written with a keyword that named the corresponding parameter Arguments may appear in any order. Each parameter name is followed by an equal sign, which identifies a keyword parameter The parameter is assumed to have the default value if its name does not appear in the macro invocation statement

Use of Keyword Parameters in Macro Instructions(3/1)

Use of Keyword Parameters in Macro Instructions(3/2)

Use of Keyword Parameters in Macro Instructions(3/3)

Macro Processor Design Options Recursive Macro expression General-Purpose Macro Processors Macro Processing within Language Translators

Recursive Macro Expansion Macro within macro can be solved if the macro processor is being written in a programming language that allows recursive calls The compiler would be sure that previous value of any variables declared within a procedure were saved when that procedure was called recursively If would take care of other details involving return from the procedure

Example of Nested Macro Invocation(2/1)

Example of Nested Macro Invocation(2/2)

General-Purpose Macro Processors(2/1) General-purpose macro processors are not dependent on any particular programming language, but can be used with a variety of different languages. Advantages of general-purpose macro processors: The programmer does not need to learn about a different macro facility for each compiler or assembler language—the time and expense involved in training are eliminated The costs involved in producing a general-purpose macro processor are somewhat greater than those for developing a language-specific processor However, this expense does not need to be repeated for each language; the result is substantial overall saving in software development cost

General-Purpose Macro Processors(2/2) In spite of the advantages noted, there are still few general purpose macro processors. A general-purpose facility must provide some way for a user to define the specific set of rules to be followed Comments should usually be ignored by a macro processor, however, each programming language has its own methods for identifying comments Each programming language has different facilities for grouping terms, expressions, or statements—a general-purpose macro processor needs to taking these grouping into account Languages differ substantially in their restrictions on the length of identifiers and the rules for the formation of constants Programming languages have different basic statement forms—syntax used for macro definitions and macro invocation statements

Macro Processing within Language Translators The macro processors that we have discussed so far are called preprocessors. That is, they process macro definitions and expand macro invocations, producing an expanded version of the source program. This expanded program is then used as input to an assembler or compiler. In this section we are going to use an alternative: combining the macro processing functions with the language translator itself.

The simplest method of achieving this is a line-by-line macro processor. These processors reads the source program and perform all of it’s functions as previously described. But the output lines are passed to the language translators as they are generated, instead of being written to an expanded source file.

Advantages It avoids making an extra pass over the source program, so it can be more efficient than using a macro processor. Some of the data structures required by the macro processor and the language translator can be combined. For eg: OPTAB in an assembler and NAMTAB in the macro processor could be implemented in the same table. In addition, many utility subroutines and functions can be used by both the language translator and the macro processor. A line-by-line macro processor also makes it easier to give diagnostic messages that are related to the source statement containing the error.

With a macro processor , such an error might be detected only in relation to some statement in the macro expansion. Then the programmer need to backtrack to discover the original source of trouble.

Disadvantages Line-by-line macro processors must be specially designed and written to work with a particular implementation of an assembler or compiler. The cost of macro processor development must therefore be added to the cost of the language translator, which results in a more expensive piece of software. In addition, the assembler or compiler will be considerably larger and more complex than it would be if a macro processor were used. The size may be a problem if the translator is to run on a computer with limited memory.

MASM Macro Processor This section describes some of the macro processing features of the Microsoft MASM assembler. In MASM conditional macro expansions are called conditional assembly statements. Following is an example for MASM conditional assembly statements.

Examples of MASM Macro and Conditional Statements(3/1)

It computes the absolute difference between the values of it’s first two parameters. These parameters may be either words(16 bits) or double words(32 bits). If they are double words, the third parameter has the value E and the calculation uses the double word register EAX. If the first two parameters are words, the third parameter is omitted. In the previous figure the first line gives the name of the macro and it’s parameters. Notice that in MASM parameters need not begin with &. The end of the macro is marked by ENDM

In that program EXIT is a local label. When the macro is expanded each local label is replaced by a unique name. MASM generates the unique names of local labels in the form ??n, where n is a hexadecimal number in the range 0000 to FFFF .ERR: signals to MASM that an error has been detected EXITM: directs MASM to terminate the expansion of the macro &: is a concatenation operator

Examples of MASM Macro and Conditional Statements(3/2)

Examples of MASM Macro and Conditional Statements(3/3)

MASM Macro Processor ;; is a macro comment, serves only as documentation for the macro definition. ; is an ordinary assembler language comment, included as part of the macro expansion.

ANSI C Macro Language In the ANSI C language, definitions and invocations of macros are handled by a preprocessor, which is generally not integrated with the rest of the compiler. Here are two examples of ANSI C macro definitions: #define NULL 0 #define EOF (-1) After these definitions appear in the program, every occurrence of NULL will be replaced by 0 and every occurrence of EOF will be replaced by (-1).

Consider another eg: #define EQ == A programmer could write while(I EQ 0) The macro processor would convert this into while(I == 0)

ANSI C macros can also be defined with parameters: For eg: #define ABSDIFF(X,Y) ((X)>(Y) ? (X)-(Y) : (Y)-(X) When the macro is expanded, each occurrence of the macro parameter is replaced by the corresponding argument. ABSDIFF(I +1, J-5) ((I+1)> (J-5) ? (I+1) – (J-5) : (J-5) – (I+1)

It is necessary to be very careful in writing macro definitions with parameters. The macro processor simply makes string substitutions, without considering the syntax of the C language. For eg: If we had written the macro definition of ABSDIFF as #define ABSDIFF(X,Y) X>Y ? X-Y : Y-X And the macro invocation is like this ABSDIFF(3+1,10-8) And it is expanded to 3+1 > 10-8 ? 3+1 - 10-8 : 10-8 - 3+1 which would not produce the intended result.

In ANSI C, parameter substitutions are not performed within quoted strings. For eg: Consider the macro definition # define DISPLAY(EXPR) printf(“EXPR=%d\n”,EXPR); The macro invocation is DISPLAY(I*J+1) Would be expanded to printf(“EXPR=%d\n”, I*J+1);

To avoid this problem, ANSI C provides a special “stringizing ” operator #. When the name of a macro parameter is preceded by #, argument substitution is performed in the usual way. For eg: #define DISPLAY(EXPR) printf(#EXPR “=%d\n”, EXPR); Then the invocation DISPLAY(I*J+1) Would be expanded to printf(“I*J+1” “=%d\n”, EXPR)

Macros in ANSI C may contain definitions or invocations of other macros. After a macro is expanded, the macro processor rescans the text, looking for more macro definitions or invocations. For eg:, the macro invocation DISPLAY(ABSDIFF(3,8)) Would be expanded to printf(“ABSDIFF(3,8)” “=%d\n”, ABSDIFF(3,8)) After rescanning this would become printf(“ABSDIFF(3,8)” “=%d\n”, ((3)>(8) ? (3) – (8 : (8) -(3)) This will produce the output ABSDIFF(3,8) =5

The ANSI C preprocessor also provides conditional compilation statements. These statements can be used to be sure that a macro is defined at least once. For eg: #ifndef BUFFER_SIZE #define BUFFER_SIZE 1024 #endif The #define will be processed only if BUFFER_SIZE has not already been defined.

Conditionals are also often used to control the inclusion of debugging statements in a program. For eg: #define DEBUG 1 . #if DEBUG==1 printf(…) #endif

In this case, the printf statement will be included in the output from preprocessor. If the first line changed to # define DEBUG 0 The printf would not be included in the program. The same thing could also be accomplished by writing #ifdef DEBUG printf(…) #endif In this case, the printf would be included if a #define statement for DEBUG appeared in the program.

Questions Define MACRO, Briefly explain the arious data structures used in the design of MACRO processor. With an example, explain generation of unique labels in macros. Explain the advantages and disadvantages of general purpose macro processor. List machine independent macro processor features. Explain any two with an example. What are the basic functions of macro processor? Explain the various data structures used in the implementation of 1-pass macro processor. List and explain the different design options for a macro processor.

7) Write a short note on ANSI C macro processing language 7) Write a short note on ANSI C macro processing language. 8) Write a note on MASM macro processor. 9) What are the advantages and disadvantages of macro processing within language translators?