Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Intermediate Code Generation
The University of Adelaide, School of Computer Science
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Compiler Designs and Constructions
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Pertemuan 12 Intermidiate Code Genarator Matakuliah: T0522 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Run time vs. Compile time
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Presented by Dr Ioanna Dionysiou
5.3 Machine-Independent Compiler Features
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
Multi-Dimensional Arrays
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
A First Book of ANSI C Fourth Edition
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
Compiler Chapter# 5 Intermediate code generation.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Intermediate Code Generation
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
CSC 8505 Compiler Construction Runtime Environments.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Chap. 4, Intermediate Code Generation
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Three Address Code Generation of Control Statements continued..
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Translation Scheme for Addressing Array Elements
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate code generation Jakub Yaghob
Compiler Construction
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Intermediate code generation
Compiler Construction
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
Intermediate Code Generation Part I
THREE ADDRESS CODE GENERATION
Intermediate Code Generation Part II
Intermediate Code Generation
Intermediate Code Generation Part II
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Intermediate Code Generation Part I
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program construct in C prevents the compiler from using the static allocation scheme? –What do we do in a calling sequence? In a return sequence? –Accessing nonlocal variables, is it a problem in static allocation scheme? –Accessing nonlocal variables, how to do it in the stack allocation scheme? What is the difference with and without nested loop? How to access the non-locals with access link, how to maintain the access link.

Some three-address statements that will be used later: –Assignment statements: With a binary operation: x := y op z With a unary operation: x:= op y With no operation(copy) : x := y –Branch statements Unconditional jump: goto L Conditional jumps: if x relop y goto L –Statement for procedure calls Param x, set a parameter for a procedure call Call p, n call procedure p with n parameters Return y return from a procedure with return value y

–Example: instructions for procedure call: p(x1, x2, x3, …, xn): param x1 param x2 … param xn call p, n –Indexed assignments: x := y[i] and x[i] := y –Address and pointer assignments x := &y, x := *y

Translation scheme to produce three address code for assignment statement. Emit (instruction): emit a three address statement to the output newtemp: return a new temporary lookup(identifier): check if the identifier is in the symbol table. Grammar: S->id:=E E->E1+E2 E->E1*E2 E->-E E->(E1) E->id E->num

S->id:=E {p:=lookup(id.name); if (p!=nil) then emit(p ‘:=‘ E.place); else error} E->E1+E2 {E.place = newtemp; emit(E.place ‘:=‘ E1.place ‘+’ E2.place);} E->E1*E2 {E.place = newtemp; emit(E.place ‘:=‘ E1.place ‘*’ E2.place);} E->-E1 {E.place = newtemp; emit(E.place ‘:=‘ ‘-’ E1.place);} E->(E1) {E.place = E1.place;} E->id {p = lookup(id.name); if (p!= nil) then E.place := p; else error;} E->num {E.place = num.val;} –Example: x := a * * -30 with bottom- up parsing.

Temporary names can be reused: –The code generated by E->E1+E2 has the general form: Evaluate E1 into t1 Evaluate E2 into t2 t3 := t1 + t2 »All temporaries used in E1 are dead after t1 is evaluated »All temporaries used in E2 are dead after t2 is evaluated »T1 and t2 are dead after t3 is assigned –Temporaries can be managed as a stack: »Keep a counter c, newtemp increases c, when a temporary is used, decreases c. »See the previous example.

Addressing array elements: –Arrays are typically stored in block of consecutive locations. –One-dimensional arrays A: array[low..high] of … the ith elements is at: base + (i-low)*width  i*width + (base – low*width) –Multi-dimensional arrays: Row major or column major forms –Row major: a[1,1], a[1,2], a[1,3], a[2,1], a[2,2], a[2,3] –Column major: a[1,1], a[2,1], a[1, 2], a[2, 2],a[1, 3],a[2,3] In raw major form, the address of a[i1, i2] is Base+((i1-low1)*(high2-low2+1)+i2-low2)*width

Addressing array elements: In raw major form, the address of a[i1, i2] is Base+((i1-low1)*(high2-low2+1)+i2-low2)*width

–In general, for any dimensional array, the address of a[i1, i2, i3, …, ik] can be computed as follows: Let highj and lowj be for bounds for ij Let nj = highj – lowj + 1 The address is ((…(i1*n2+i2)*n3+…)nk+ik)*width + base – ((…low1*n2+low2)*n3+…)nk+lowk) * width –When generating code for array references, we need to explicitly compute the address.

–Translation scheme for array elements: Limit(array, j) returns nj=highj-lowj+1.place: the temporarys or variables.offset: offset from the base, null if not an array reference Grammar: S->L := E E->E+E E->(E) E->L L->Elist ] L->id Elist->Elist, E Elist->id[E

S->L := E { if L.offset = null then emit(L.place ‘:=‘ E.place) else emit(L.place’[‘L.offset ‘]’ ‘:=‘ E.place);} E->E1+E2 {E.place := newtemp; emit(E.place ‘:=‘ E1.place ‘+’ E2.place);} E->(E1) {E.place := E1.place;} E->L {if L.offset = null then E.place = L.place else {E.place = newtemp; emit(E.place ‘:=‘ L.place ‘[‘ L.offset ‘]’); } L->Elist ] {L.place = newtemp; L.offset = newtemp; emit(L.place ‘:=‘ c(Elist.array)); emit(L.offset ‘:=‘ Elist.place ‘*’ width(Elist.array); }

L->id {L.place = lookup(id.name); L.offset = null; } Elist->Elist1, E { t := newtemp; m := Elist1.ndim + 1; emit(t ‘:=‘ Elist1.place ‘*’limit(Elist1.array, m)); emit(t, ‘:=‘ t ‘+’ E.place); Elist.array = Elist1.array; Elist.place := t; Elist.ndim := m; } Elist->id[E {Elist.array := lookup(id.name); Elist.place := E.place Elist.ndim := 1; }

Example: A: array[1..10, 1..20] of integer; Translate: x := A[y, z] Accessing fields in records? a.b.c.d := x X := a.b.c.d