Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Intermediate Code Generation. Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and.

Similar presentations


Presentation on theme: "Chapter 5 Intermediate Code Generation. Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and."— Presentation transcript:

1 Chapter 5 Intermediate Code Generation

2 Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and parsed it.  We know the structure of the program and of every statement in it,  and we have presumably established that it is free of grammatical errors.  It would appear that we are ready to start translating it.

3 Chapter 5 -- Intermediate Code Generation3 1. Semantic Actions and Syntax- Directed Translation  We can attach a meaning to every production  Because the sequence of productions guides the generation of intermediate code, we call this process Syntax-Directed Translation.  Def: The computations or other operations attached to the productions impute meaning to each production, and so these operations are called Semantic Actions

4 Chapter 5 -- Intermediate Code Generation4  Def: The information obtained by the semantic actions is associated with the symbols of the grammar; it is normally put in fields of records associated with the symbols; these fields are called attributes  Note: as far as the parser is concerned, neither the semantic actions nor the attributes are a part of the grammar; they are only used as a device for bridging the gap between parsing and constructing an intermediate representation.

5 Chapter 5 -- Intermediate Code Generation5  Things we must take care of with the semantic actions:  making sure the variables are declared before use.  type checking  making sure actual and formal parameters are matched  These things are called semantic analysis  So we can now have it both ways, we can Put context dependent information and actions together into a language that is still context free.

6 Chapter 5 -- Intermediate Code Generation6 2. Intermediate Representations  We will look at several different representations  Syntax Trees  Directed Acyclic Graphs  Postfix notation  Three-Address Code  Other Forms.

7 Chapter 5 -- Intermediate Code Generation7 2.1 Syntax Trees  typically used when intermediate code is to be generated later (maybe after an optimization pass)

8 Chapter 5 -- Intermediate Code Generation8  Statement: x = a * b + a * b  Parse Tree

9 Chapter 5 -- Intermediate Code Generation9  Syntax Tree

10 Chapter 5 -- Intermediate Code Generation10  In a Parse Tree the emphasis is on the grammatical structure of the statement.  In a Syntax Tree the emphasis is on the actual computation to be performed.

11 Chapter 5 -- Intermediate Code Generation11 2.2 Directed Acyclic Graphs  The directed acyclic graph (DAG) is a relative of a Syntax Tree.  The difference is that nodes for variables or repeated sub-expressions are merged.

12 Chapter 5 -- Intermediate Code Generation12  DAG -- Notice that this is not the same computations as in the previous examples  They had a typo -- (swapped the * and +)  But it still explains the concept

13 Chapter 5 -- Intermediate Code Generation13  The use of DAG's to eliminate redundant code is our first instance of optimization. We will see more optimization in Chapter 6.  Redundant code really comes into play when we do array subscripts.  When you start generating intermediate code, you will be amazed at how much is generated for array subscripts.

14 Chapter 5 -- Intermediate Code Generation14 2.3 Postfix Notation  Very easy to generate from a Bottom-Up parse.  You can also generate it from a Syntax Tree via a postorder traversal.  The chief virtue of postsfix is that it can be evaluated with the use of a stack.  Nested if statements can cause problems.

15 Chapter 5 -- Intermediate Code Generation15 2.4 Three-Address Code  This form breaks the program down into elementary statements having no more than 3 variables and no more than one operator.  Sample Statement: x = a + b * b  Translation:  T := b * b  x := a + T  Note: T is a temporary variable.

16 Chapter 5 -- Intermediate Code Generation16  The notation is a compromise; it has the general form of a high-level language, but the individual statements are simple enough that they map into assembly language in a reasonably straight forward manner.  3AC may be:  Generated from a traversal of a Syntax Tree or a DAG.  or it may be generated as intermediate code directly in the course of the parse.

17 Chapter 5 -- Intermediate Code Generation17 2.5 Intermediate Languages  Sometimes the Intermediate representation may be a language of its own.  This helps uncouple the front end of the compiler from the back end.  You can then have a front end for each language that generate the same intermediate language, and then one back end for each type of computer.

18 Chapter 5 -- Intermediate Code Generation18  Examples:  UNCOL (1961) -- UNiversal Compiler- Oriented Language.  P-Code (1981) -- UCSD -- based upon a p-code interpreter (they also built p-code compilers.)  GNU Intermediate Code -- gcc, g++, g77, gada,  -- a Lispish type intermediate language.

19 Chapter 5 -- Intermediate Code Generation19 3. Bottom-Up Translation  Bottom-Up parsing generally lends itself to intermediate code generation more readily than does top-down parsing.  In either case, we must keep track of the various elements or pieces of the intermediate representation we are using, so we can get at them when we need them.

20 Chapter 5 -- Intermediate Code Generation20  These elements will be attributes of symbols in the grammar.  For an identifier, the attribute will usually be its address in the symbol table.  For a non-terminal, the attribute will be some appropriate reference to part of the intermediate representation.

21 Chapter 5 -- Intermediate Code Generation21  The most convenient way to keep track of these attributes is by keeping them in a stack (known as the semantic stack).  In the case of bottom-up parsing, the semantic stack and the parser stack move in synchronism.  When we pop from the parse stack we pop the semantic stack, and when we push something onto the parse stack we will push something onto the semantic stack

22 Chapter 5 -- Intermediate Code Generation22 3.1 Trees  Syntax Trees  The semantic action associated with each production will include planting a tree and taking care of the grafts.  The attribute will contain a pointer to the root of the tree for this expression.  You will need functions like make_tree( ) and make_leaf( )

23 Chapter 5 -- Intermediate Code Generation23  DAGS  The main difference between constructing a DAG and constructing a Syntax Tree is that we do not create redundant nodes in a DAG.  That means that the functions to create trees and grafts must be modified to check for duplicates.  This is typically done with a hash function.

24 Chapter 5 -- Intermediate Code Generation24 3.2 Postfix Notation  Postfix notation is particularly easy to generate from a bottom-up parse.  Grammar:

25 Chapter 5 -- Intermediate Code Generation25 3.3 Three-Address Code  We can obtain 3AC from a tree or a DAG, or we can generate it directly in the course of a bottom-up parse.  To generate 3AC during the parse we need the following functions:  MakeQuad( ) -- puts its parameters into the listing file in the proper format.  GetTemp( ) -- Generates a new temporary number.

26 Chapter 5 -- Intermediate Code Generation26 4. Top-Down Translation  It is, to be blunt, a mess  Two stacks, one for the parser, and one for the attributes. Thus the semantic stack must now be separate and will not move in synchronism with the parser stack

27 Chapter 5 -- Intermediate Code Generation27  Up to now, we picked up attributes from children, and the bottom-up parser provided a handy way to do this. Now we have to be able to transfer attributes between siblings, and in some cases it may be necessary to transfer them from parent to child.

28 Chapter 5 -- Intermediate Code Generation28 4.1 Synthesized and Inherited Attributes  synthesized -- from your children (like we have looked at).  inherited -- from your parent, or sibling  Def: A grammar in which all attributes are synthesized is called an S-attribute grammar

29 Chapter 5 -- Intermediate Code Generation29  Notice that inheritance so far is from left to right.  That may not always be the case, and that can really mess things up.  Def: Grammars in which no non-terminal ever inherits from a younger brother are called L- attributed grammars

30 Chapter 5 -- Intermediate Code Generation30 4.2 Attributes in a Top-Down Parse  Since so many of our problems arise from the need to eliminate left recursion in the underlying grammar, the place to start is to see how a normal semantic action has to be modified when removing left recursion.

31 Chapter 5 -- Intermediate Code Generation31  In this example,.s is used to denote a synthesized attribute, and.i an inherited one.

32 Chapter 5 -- Intermediate Code Generation32  An sample parse tree of a+b

33 Chapter 5 -- Intermediate Code Generation33 4.3 Removal of Left Recursion  When transforming a grammar to remove left recursions, we must also transform the semantic actions, as the example we just saw implies.  There are some basic rules on how to do this, but we will not cover them at this time.

34 Chapter 5 -- Intermediate Code Generation34 5. More about Bottom-Up Translation  We are now going to consider the cases where semantic actions must be embedded in the midst of the right-hand side of a production.  And cases where it is expedient to use inherited attributes.

35 Chapter 5 -- Intermediate Code Generation35 5.1 Embedded Semantic Actions  You will recall that in S-attributed grammars, the semantic actions occur in one piece at the end of the right-hand side of the production.  For some statements that is fine, but for others it is not.  The typical example is: if C then S  because based upon the value of C we need a conditional jump to the end of the statement S.

36 Chapter 5 -- Intermediate Code Generation36  There are a few ways around this:  Add Semantic actions in the middle of the production (YACC/BISON allow this)  Breaking Productions up  Adding Marker Non-Terminals  Another problem:  The limit a jump non zero can go (jnz limit). On the Intel architecture, this is 127 bytes.

37 Chapter 5 -- Intermediate Code Generation37 5.2 Inherited Attributes  Example: Fortran Variables  The data type comes first, so you can enter lexemes into the symbol table with the data type (an inherited attribute)

38 Chapter 5 -- Intermediate Code Generation38 6. Pascal-Type Declarations  Problem:  The data type comes last, so you can’t add the variables into the Symbol table as they come.  Solution:  Save the identifiers (or their symbol table pointer) onto some stack/list  When the data types come around, update the list elements.

39 Chapter 5 -- Intermediate Code Generation39 7. Type Checking and Coercion  integer operation / float operation  is it integer_add, float_add,...  The problem is that this is an overloaded operator  It is up to the Semantic Analyzer  to determine which operation is desired and to choose the appropriate implementation.  And if the user has specified two incompatible operands, it must take some action.

40 Chapter 5 -- Intermediate Code Generation40

41 Chapter 5 -- Intermediate Code Generation41 8. Summary  We have seen some of the principal methods for intermediate code generation and some of the principal problems.  Now we have nearly reached the conclusion of the front end of the compiler.  Some front-ends also include some optimization, and some interpreters also stop here.


Download ppt "Chapter 5 Intermediate Code Generation. Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and."

Similar presentations


Ads by Google