Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Functional Programming Lecture 10 - type checking.
CPSC 388 – Compiler Design and Construction
NP-Completeness.
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Intermediate Code Generation
1 Finite Constraint Domains. 2 u Constraint satisfaction problems (CSP) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
1 Compiler Construction Intermediate Code Generation.
Exercise: Balanced Parentheses
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
Compiler Construction
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) © Naik & Tripathy 1 Software Testing and Quality Assurance Theory and Practice.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
Compiler Chapter# 5 Intermediate code generation.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
More Examples of Abstract Interpretation Register Allocation.
2/6/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
CS 598 Scripting Languages Design and Implementation 9. Constant propagation and Type Inference.
Review on Program Challenge CSc3210 Yuan Long.
Arrays Using array as an expression, on the right-hand side Assigning to an array.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Programming Languages and Compilers (CS 421)
Control Flow Testing Handouts
Context-Sensitive Analysis
Handouts Software Testing and Quality Assurance Theory and Practice Chapter 4 Control Flow Testing
Analysis of Algorithms
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Graph-Based Operational Semantics
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
CSE 341 Section 5 Winter 2018.
What is a programming language?
CISC124 Labs start this week in JEFF 155. Fall 2018
Declarative Computation Model Single assignment store (VRH 2
Compiler Construction
CSc 453 Interpreters & Interpretation
Compiler Construction
Presentation transcript:

Exercise 1 Generics and Assignments

Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T => T | [Ident]T expressions:E ::= ident | { val ident:T = ident(ident); E } | { val ident:T = {(Ident:T)=>E}; E } Initial environment plus : Int => (Int => Int)one : Inttwo : Int less : Int => (Int => Boolean) A generic type [A]T can be used as T[A:=T’], which we call an instance of [A]T. e.g. Int=>Int instance of [A](A=>A) Example:{ val f: [A](A => A) = {(x:A)=>x}; (type checks) { val x : Int = f(one); 1) give rules { val above : Int => Bool = less(one); 2) type check { val outcome : Bool = above(two); { val res : Bool = f(outcome); res }}}}}

Here are the interesting cases, involving instantiation and introduction of generic variables

Adding Assignment Statements expressions:E ::= ident | { var ident:T = ident(ident); E } | { var ident:T = {(Ident:T)=>E}; E } | { ident = ident; E } Use same rules for ‘var’ as for ‘val’ Give rule for assignment statement that are as permissive as possible, and still sound

Try to Type Check These Examples 1) { var f: [A](A => A) = {(x:A)=>x} var p : Int => Int = plus(one) f = p var above : Int => Bool = less(one) var outcome : Bool = above(two) var res : Bool = f(outcome); res }}}}}breaks 2) { var f: [A](A => A) = {(x:A)=>x} var p : Int => Int = plus(one) p = f var x : Int = p(one) var above : Int => Bool = less(x) var outcome : Bool = above(two) var res : Bool = f(outcome); res }}}}}works

Subtyping Suppose we wish to introduce subtyping rule into the previous system There should be some sort of subtyping relation between [Ident]T and its instance T[Ident:=T’]. Which type should be subtype of which?

Exercise 2 Computing Stack Depth

Control-Flow Graph with Bytecodes Consider assignment statements Right hand sides have only – integer binary minus (-) – integer multiplication (*) – local variables We compile them into iload,istore,isub,imul Consider sequence of such statements as a simple control-flow graph (a line)

CFG for Expression. Stack Depth Assume x,y,z are in slots 1,2,3 Statement x = x*(x – y*z – z*x) Compute for how many integers stack content increased after every point, relative to initial size Design data-flow analysis for CFG containing these bytecode instructions that maintains interval of possible increases of stack sizes (stack depth), starting from entry Define analysis domain as arbitrary intervals of stack size, and give transfer functions for iload,istore,isub,imul.

Run Analysis on This Program x = y while (x > 0) { x = y – x – z } What is the maximal number of steps for such analysis for: - arbitrary graphs with these bytecodes - graphs obtained by compiling Tool programs (if the analysis is extended with all bytecode instructions) Observation: we can use constant propagation to compute places where stack depth is constant Remark: JVM class loader ensures that stack depth for each progrma poitn is constant, otherwise rejects the bytecode.

Constant Stack Depth Consider CFGs with bytecodes, generated from Tool Suppose we wish to store local variables in the memory and temporary variables in registers Give an upper bound on the number of registers and a way to convert bytecodes iload,istore,isub,imul into instructions that use registers

Exercise 3 Pattern Matching

Matching on Polymorphic Lists Suppose we have language with these types: Int, Bool List[T] for any type T (T1,T2) if T1,T2 are types Patterns have types: true,false : Bool integer constant : Int Nil[T]:List[T] (x :: xs) : List[T] where x:T and xs:List[T] (x,y) : Tx, Ty where x:Tx, y:Ty Consider expressions containing the above constructs as expressions, in addition to the expression of the kind: e match { case p1 => e1; … ; case pN => eN } Give type checking rules for this language, and pay attention to introducing and removing fresh variables into the scope. Type rules should prevent the use of unbound variables.

Sketch of Solution: Constants  |– e : T1  |– T1 { case p i => e i } : T for all i, 1 ≤ i ≤ N ________________________________________________  |– e match { case p 1 => e 1 ; … ; case p N => e N } : T  |– e : T _________________________  |– Bool { case true => e } : T  |– e : TK is any integer literal, such as 42 _________________________  |– Int { case K => e } : T

Sketch of Solution: Non-nested lists  |– e : T _________________________  |– List[H] { case Nil[H] => e } : T   {(x,H),(xs,List[H])} |– e : T ____________________________  |– List[H] { case (x::xs) => e } : T

From Nested Patterns to Tuples  |– (T1,…,Tk,H,List[H]) {case (x1,…,xk,p1,p2) => e }:Tk ≥ 0 _______________________________________  |– (T1,…,Tk,List[H]) { case (x1,…,xk,(p1::p2)) => e }:T Handling Tuples   {(x1,T1),…,(xn,Tn)} |– e : T xi – all variables _______________________________  |– (T1,…,Tn) { case (x1,…,xn) => e } : T (also add rules to eliminate constant parts from a tuple)