CSE302: Compiler Design

Similar documents
Intermediate Code Generation

Intermediate Code Generation

intermediate-code Generation

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Type Checking. Chapter 6, Section 6.3, 6.5

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs(dot)nl

Intermediate Code Generation Part II

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Intermediate Code Generation

CSE302: Compiler Design

Concepts Introduced in Chapter 6

5. Syntax-Directed Definitions & Type Analysis

Module 27 Switch-case statements and Run-time storage management

Intermediate-Code Generat ion

Formal Languages and Compilers Lecture X Intermediate Code Generation

Concepts Introduced in Chapter 6

Chapter 6 Intermediate Code Generation

CS2210: Compiler Construction. Code Generation

Type systems. Static typing

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018

CSE302: Compiler Design

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Short Notes of CS201

Introduction to Programming Using Java (98-388)

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

CS201 - Introduction to Programming Glossary By

Intermediate Representations

Class Information ANNOUCEMENTS

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

CSE302: Compiler Design

CMPSC 160 Translation of Programming Languages. Three-Address Code

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

In Java we have the keyword null, which is the value of an uninitialized reference type

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Time : 1 Hour Max Marks : 30

Static Checking and Type Systems

Run Time Environment

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Final CSE 131B Spring 2004

Type Checking. Error Checking

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Dynamic Data Structures. CSCI 112: Programming in C

Types. What is a type?

Principles of Programming Languages

Final CSE 131B Spring 2005

Java Identifiers, Data Types & Variables

2. Reachability in garbage collection is just an approximation of garbage.

Motivation was to facilitate development of systems software, especially OS development.

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Intermediate Code Generation

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Motivation was to facilitate development of systems software, especially OS development.

Procedure and Object- Oriented Abstraction

ECS 142 Project: Code generation hints

Midterm CSE 131B Spring 2005

September 10,

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Run-time Environments

Run-time Environments

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 19 November 2014

Assist. Prof. Dr. Caner ÖZCAN

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Implementing Subprograms

C11: Garbage Collection and Constructors

Compiler Theory. (Semantic Analysis and Run-Time Environments)

CSE302: Compiler Design

CSE 431S Type Checking. Washington University Spring 2013

Intermediate Code Generation

Types II. Hwansoo Han

Module 25 Control Flow statements and Boolean Expressions

CA Compiler Construction

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

[0569] p 0318 garbage

a data type is Types

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lecture 12: Data Types (and Some Leftover ML)

CSc 453 Intermediate Code Generation

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

VARIABLES AND TYPES CITS1001

Syntax-Directed Translation

Compiler construction 2009

CSE 307: Principles of Programming Languages

Dynamic Data Structures (II)

THEORY OF COMPILATION

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

CSE398: Network Systems Design

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

CSE302: Compiler Design

CS201 Some Important Definitions

Implementing Subroutines. Outline [1]

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Lecture 3: C Programm

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Transcription:

CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 24, 2007

Outline Recap Three address code generation Type checking Brief introduction to runtime environments Summary

Translation of Expressions S id = E ; S.code = E.code gen(top.get(id.lexeme) = E.addr) E E1 + E2 E.addr = new Temp() E.code = E1.code E2.code gen(e.addr = E1.addr + E2.addr) E -E1 E.addr = new Temp() E.code = E1.code gen(e.addr = minus E1.addr) E ( E1 ) E.addr = E1.addr E.code = E1.code E id E.addr = top.get(id.lexeme) E.code =

SDT of Array Type Declaration T B {t = B.type; w = B.width;} C { T.type = C.type; T.width = C.width; } B int {B.type = integer; B.width = 4;} B float {B.type = float; B.width = 8;} C [num] C1 { C.type = array(num.value, C1.type); C.width = num.value C1.width; } C ε{c.type = t; C.width = w;}

Translation of Array References L id [ E ] L.array = top.get(id.lexeme) L.type = L.array.type.elem L.addr = new Temp() gen(l.addr = E.addr * L.type.width) L L1 [ E ] L.array = L1.array L.type = L1.type.elem t = new Temp() L.addr = new Temp() gen(t = E.addr * L.type.width) gen(l.addr = L1.addr + t) S L = E ; gen(l.array.base [ L.addr ] = E.addr E L E.addr = new Temp() gen(e.addr = L.array.base [ L.addr ] )

Translation of Control Flows and Boolean Expressions S if( {B.true=newlabel(); B.false=S.next;} B) S1 {S.code=B.code label(b.true) S1.code if B goto L1 goto L2 L1: S1 L2: B B1 B2 S if( {B.true=newlabel(); B.false=S.next; B1.true=B.true; B1.false=newlabel(); B2.true=B.true; B2.false=B.false;} B1 B2) {B.code=B1.code label(b1.false) B2.code} S1 {S.code=B.code label(b.true) S1.code if B1 goto L1 goto L2 L2: if B2 goto L1 goto L3 L1: S1 L3:

Translation of Boolean Expressions B B1 && B2 S if( { B.true=newlabel(); B.false=S.next; B1.true=newlabel(); B1.false=B.false; B2.true=B.true; B2.false=B.false;} B1&&B2) {B.code=B1.code label(b1.true) B2.code} S1 {S.code=B.code label(b.true) S1.code B!B1 S if( {B.true=newlabel(); B.false=S.next; B1.true=B.false; B1.false=B.true;}!B1) {B.code=B1.code} S1 {S.code=B.code label(b.true) S1.code} B E1 rel E2 S if( {B.true=newlabel(); B.false=S.next;} E1 rel E2) {B.code=E1.code E2.code gen( if E1.addr rel.op E2.addr goto B.true gen( goto B.false); } S1 {S.code=B.code label(b.true) S1.code}

An Example while(a<b && a<c) a=(a+d)

An Example B B1 && B2 S if( {B.true=newlabel(); B.false=S.next; B1.true=newlabel(); B1.false=B.false; B2.true=B.true; B2.false=B.false;} B1&&B2) {B.code=B1.code label(b1.true) B2.code} S1 {S.code=B.code label(b.true) S1.code B E1 rel E2 S if( {B.true=newlabel(); B.false=S.next;} E1 rel E2) {B.code=E1.code E2.code gen( if E1.addr rel.op E2.addr goto B.true gen( goto B.false); } S1 {S.code=B.code label(b.true) S1.code} S while( {begin = newlabel(); B.true=newlabel(); B.false=S.next;} B) {S1.next=begin;} S1 {S.code=label(begin) B.code label(b.true) S1.code gen( goto begin);} E id {E.addr = top.get(id.lexeme); E.code = ;} S id = E ; {S.code = E.code gen(top.get(id.lexeme) = E.addr);} E E1 + E2 {E.addr = new Temp(); E.code = E1.code E2.code gen(e.addr = E1.addr + E2.addr);}

Outline Recap Type checking Brief introduction to runtime environments Summary

Type Expressions Declarations D T id; D ε T B C record { D } B int float C [num] C ε A type expression A basic type A type constructor array record

Type Equivalence if two type expressions are equal then return a certain type else error Same basic type Same constructor applied to structurally equivalent types A type name denoting the other typedef int aaa, bbb, ccc; /* all ints */ aaa int1; bbb int2; ccc int3; struct _node { char *name; char *value; struct _node *next; }; typedef struct _node Node;

Type Checking Each component has a type expression assigned Conform to the type system Strongly typed languages

Type Synthesis if f has type s t and x has type s then expression f(x) has type t Type declarations are needed A type conversion SDD E E1 + E2 E.addr = new Temp() E.code = E1.code E2.code gen(e.addr = E1.addr + E2.addr)

Type Conversion Hierarchy Double, float, long, int, short, char, byte Widening conversion hierarchy Narrowing conversion hierarchy Addr widen(addr a, Type t, Type w) { if (t=w) return a; else if (t=int and w=float) { temp = new Temp(); gen(temp = (float) a); return temp; } else error; } max(t1,t2)

Type Synthesis if f has type s t and x has type s then expression f(x) has type t A type conversion SDD E E1 + E2 E.type = max(e1.type, E2.type) a1 = widen(e1.addr, E1.type, E.type) a2 = widen(e2.addr, E2.type, E.type) E.addr = new Temp() E.code = E1.code E2.code gen(e.addr = a1 + a2)

Function/Operator Overloading void err() { }; void err(string s) { } 1+2; A+B; if f have type s i t i and x has type s k then expression f(x k ) has type t k

Type Inference No need to declare type before usage A Scheme example length.scm (+ (length '("String A" "String B") ) (length '(1 2 3))) The type expression of length α. list(α) integer

Outline Recap Type checking Brief introduction to runtime environments Summary

Run-Time Environments Storage layout and allocation Subprogram linkages Variable reference accessing mechanisms Parameter passing mechanisms Interface to OS and I/O

Storage Organization

Subprogram Linkages Activation record instance Stack frame Subprogram call Push stack frames Subprogram exit Pop stack frames Stack-dynamic variable

Heap-dynamic Variables C malloc and free C++ new and delete int *intnode; intnode = new int; delete intnode; Java All objects

Heap Management Reference counters (eager approach) Maintain a counter in every cell that store the number of pointers currently pointing at the cell Garbage collection (lazy approach) Allocate and disconnect until all available cells allocated; then begin gathering all garbage

Garbage Collection Every heap cell has an extra bit used by collection algorithm All cells initially set to garbage All pointers traced into heap, and reachable cells marked as not garbage All garbage cells returned to a list of available cells

Outline Recap Type checking Brief introduction to runtime environments Summary