Docsity

Case Statements and Procedure Calls: Attribute Grammar for Intermediate Code Generation, Slides of Programming Languages

Partial preview of the text

Download Case Statements and Procedure Calls: Attribute Grammar for Intermediate Code Generation and more Slides Programming Languages in PDF only on Docsity!

Chap 8: Intermedicate Code Generation

Overview

 Goal: Generate a Machine Independent Intermediate

Form that is Suitable for Optimization and Portability

 We’ll Focus on

 Revisit AST and DAGs with Attribute Grammars  Introduce and Elaborate on Three Address Coding  Review 3AC for:  Declarations  Assignments and Boolean Expressions  Case Statements and Procedure Calls  Review Prof. Michel’s Approach to Intermediate Code Generation  Concluding Remarks/Looking Ahead

What We Want

 A Representation that

 Is closer to actual machine  Is easy to manipulate  Is target neutral (hardware independent)  Can be interpreted

Recall ASTs and DAGs

 Intermediate Forms of a Program:

 ASTs: Abstract Syntax Trees (below left)  DAGs: Directed Acyclic Graphs (below right)  What is the Expression? assign

a +


b

c

uminus b

c

uminus

assign

a +


b

c

uminus

Docsity.com

Representing Attribute Grammars

 Two Different Forms:

 Linked Data Structure  Multi-Dimensional Array

Objective

 Directly Generate Code From AST or DAG as a Side

Effect of Parsing Process (Attribute Grammar)

 Consider Code Below:

 Each is Referred to as “3 Address Coding (3AC)” since

there are at Most 3 Addresses per Statement  One for Result and At Most 2 for Operands

Temporaries

 The machine has an infinite number of temporaries

 Call them t 0 , t 1 , t 2 , ....

 Temporaries can hold values of any type  The type of the temporary is derived from the generation  Temporaries go out of scope with the function they are in

What is Three-Address Coding?

 A simple type of instruction

 3 / 2 Operands x,y,z  Each operand could be  A literal  A variable  A temporary

 Example

x := y op z

x + y * z tt^01 := y * z:= x + t 0

x := op z

Types of Three Address Statements

 Conditional Jumps of Form:

 if x relop y goto L  with relop as relational operators and the goto executed if the x relop y is true

 Parameter Operations of Form:

 param a (a parameter of function)  call p, n (call function p with n parameters)  return y (return value y from function – optional)  param a  param b  param c  call p, 3

Types of Three Address Statements

 Indexed Assignments of Form:

 X := Y[i] (Set X to i-th memory location of Y)  X[i] := Y (Set i-th memory location of X to Y)  Note the limit of 3 Addresses (X, Y, i)  Cannot do: x[i] := y[j]; (4 addresses!)

 Address and Pointer Assignments of Form:

 X := & Y (X set to the Address of Y)  X := * Y (X set to the contents pointed to by Y)  * X := Y (Contents of X set to Value of Y)

Attribute Grammar

 How would Grammar work for:

 A := - B  X := Y + Z  W := B*C + D

Three Address Code for While Loops

 Grammar Rule:

 S → while E do S (^1)

 Conceptually:

 Check Expression E  If Not True, Skip to First Statement after S (^1)  Execute S (^1)  Goto: Check Expression E

 Function:

 newlabel : Generates a new label for goto/jump  Generate labels for Check Expression and 1 st Statement after S (^1)

Declarations

 Recall Runtime Environment in Chapter 7

 One Critical Issue was Memory Layout (Static, Stack,

Heap)

 Stack Utilized during Procedure/Function Calls to

 Allocate Space for P/F Variables  This now Includes Temporaries for 3AC

 We need to Track

 Name  Type (Int, real, boolean, etc.)  Offset (with respect to some relative address)

 Function

 enter (name, type, offset) creates symbol table entry  offset global initially 0

Attribute Grammar for Memory Layout

 Note: Does not include Temporaries (Need to also

Allocate Memory for them as well)

 How could this be supported?