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.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 6 Intermediate Code Generation
Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
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.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
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
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
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.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
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.
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.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
國立台灣大學 資訊工程學系 薛智文 98 Spring Intermediate Code Generation (textbook ch# 6.1–6.4, 6.5.1–6.5.3,
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.
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..
1 Chapter 6: Semantic Analysis. 2 Semantic Analyzer ==> Semantic Structure - What is the program supposed to do? - Semantics analysis can be done during.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Translation Scheme for Addressing Array Elements
Java Array Object Chuen-Liang Chen Department of Computer Science
Expressions and Assignment
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate code generation Jakub Yaghob
Review: Chapter 5: Syntax directed translation
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
Intermediate Code Generation Part II
Three Address Code Generations for Boolean Functions
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
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.
7.4 Boolean Expression and Control Flow Statements
Pages:51-59 Section: Control1 : decisions
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Three Address Code Generation – Backpatching
Intermediate Code Generation
THREE ADDRESS CODE GENERATION
Intermediate Code Generation Part II
Intermediate Code Generation
Intermediate Code Generation
Intermediate Code Generation Part II
Pages:51-59 Section: Control1 : decisions
Intermediate Code Generation Part I
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Code generation and data types
Presentation transcript:

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 of array element a[i, j, k], assuming that the array is declared as a[10..20, 30..40, 40..50]? How to compute the address for the array element a[i1, i2, …, ik] assuming that the array is declared as a[low1..high1, …, lowk..hightk]?

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: Function limit(array, j) returns nj=highj-lowj+1 Place: an attribute represent the temporarys or variables offset: an attribute represent the offset from the base for an array element, 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

Intermediate code generation for boolean expressions. Boolean expressions are composed of boolean operators (and, or and not), boolean variables and relational expressions (E1 relop E2) Grammer: E->E or E | E and E | not E | (E) | id relop id | true | false Two methods can be used to generate code for boolean expressions: Encode true and false numerically and evaluate the boolean expression just like evaluating the arithmetic expression. Implement boolean expressions as flow of controls (short circuit code)

Representing the boolean value numerically. How to do code generation: E -> E or E E-> E and E E ->not E E -> (E) E ->id relop id E ->true E->false

E -> E or E E-> E and E E ->not E E -> (E) E ->id relop id { E.place = newtemp; emit(if id1.place relop.op id2.place ‘goto’ nextstat+3); emit(E.place ‘:=‘ 0); emit(‘goto’ nextstat + 2); emit(E.place ‘:=‘ 1); } E ->true E->false