CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Modern Programming Languages, 2nd ed.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
Standard ML- Part I Compiler Baojian Hua
Compiler Construction
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Chapter 9 Interactive Multimedia Authoring with Flash - Introduction to Programming “Computers and Creativity” Richard D. Webster, COSC 109 Instructor.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Imperative Programming
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Invitation to Computer Science, Java Version, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
CS453 LectureIntroduction1 CS453 Compiler Construction Instructor:Wim Bohm Computer Science Building 470 TA: tba
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
CS453 LectureIntroduction1 CS453 Compiler Construction Original Design: Michelle Strout Instructor:Wim Bohm
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Principles of programming languages 12: Functional programming
Chapter 1 Introduction.
Learning to Program D is for Digital.
ML: a quasi-functional language with strong typing
Information Science and Engineering
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 1 Introduction.
CS190/295 Programming in Python for Life Sciences: Lecture 1
CSE 341: Programming Langs
CSE401 Introduction to Compiler Construction
CSE 341 Section 5 Winter 2018.
Erik (Happi) Johansson Room:
CSE-321 Programming Languages Introduction to Functional Programming
CSE-321 Programming Languages Introduction to Functional Programming
Compiler Construction
General Computer Science for Engineers CISC 106 Lecture 03
Compiler Construction
Presentation transcript:

CS 320: Compiling Techniques David Walker

People David Walker (Professor) 412 Computer Science Building office hours: after each class Guilherme Ottoni (TA) 417 Computer Science Building office hours: Mondays 2-2:30 PM Fridays 2-3 PM

Information Web site: ng05/cos320/index.htm ng05/cos320/index.htm Mailing list: To subscribe: To post to this list, send your to:

Books Modern Compiler Implementation in ML, Andrew Appel A reference manual for SML best choice: Online references see course web site several hardcopy books Elements of ML Programming, Jeffrey D. Ullman

Assignment 0 Write your name and other information on the sheet circulating Find, skim and bookmark the course web pages Subscribe to course list Begin assignment 1 Figure out how to run & use SML Due next Thursday February 10

onward!

What is a compiler? A compiler is program that translates a source language into an equivalent target language

What is a compiler? while (i > 3) { a[i] = b[i]; i ++ } mov eax, ebx add eax, 1 cmp eax, 3 jcc eax, edx C program assembly program compiler does this

What is a compiler? class foo { int bar;... } struct foo { int bar;... } Java program compiler does this C program

What is a compiler? class foo { int bar;... } Java program compiler does this Java virtual machine program

What is a compiler? \newcommand{.... } \sfd\sf\fadg Latex program compiler does this Tex program

What is a compiler? \newcommand{.... } \sfd\sf\fadg Tex program compiler does this Postscript program

What is a compiler? Other places: Web scripts are compiled into HTML assembly language is compiled into machine language hardware description language is compiled into a hardware circuit...

Compilers are complex text file to abstract syntax lexing; parsing abstract syntax to intermediate form (IR) analysis; optimizations; data layout IR to machine code code generation; register allocation front-end middle-end back-end

Course project Fun Source Language simple imperative language Only 1 IR (the initial abstract syntax generated by the parser) type checking; high-level optimizations Code Generation instruction selection algorithms; register allocation via graph coloring front-end middle-end back-end

Standard ML Standard ML is a domain-specific language for building compilers Support for Complex data structures (abstract syntax, compiler intermediate forms) Memory management like Java Large projects with many modules Advanced type system for error detection

Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Resources: Robert Harper’s Online book “an introduction to ML” is a good place to start See course webpage for pointers and info about how to get the software

Intro to ML Highlights Data Structures for compilers Data type definitions Pattern matching Strongly-typed language Every expression has a type Certain errors cannot occur Polymorphic types provide flexibility Flexible Module System Abstract Types Higher-order modules (functors)

Intro to ML Interactive Language Type in expressions Evaluate and print type and result Compiler as well High-level programming features Data types Pattern matching Exceptions Mutable data discouraged

Preliminaries start sml in Unix by typing sml at a prompt: tux% sml Standard ML of New Jersey, Version , September 28, 2000 [CM; autoload enabled] - (* quit SML by pressing ctrl-D *) (* just so you know, comments can be (* nested *) *)

Preliminaries Read – Eval – Print – Loop ;

Preliminaries Read – Eval – Print – Loop ; > 5: int

Preliminaries Read – Eval – Print – Loop ; > 5: int - it + 7; > 12 : int

Preliminaries Read – Eval – Print – Loop ; > 5: int - it + 7; > 12 : int - it – 3; > 9 : int true; stdIn: Error: operator and operand don't agree [literal] operator domain: int * int operand: int * bool in expression: 4 + true

Preliminaries Read – Eval – Print – Loop - 3 div 0; Failure : Divrun-time error

Basic Values - (); > () : unit=> like “void” in C (sort of) => the uninteresting value/type - true; > true : bool - false; > false : bool - if it then 3+2 else 7;“else” clause is always necessary > 7 : int - false andalso loop_Forever; > false : booland also, or else short-circuit eval

Basic Values Integers ; > 5 : int (if not true then 5 else 7); > 10 : intNo division between expressions and statements Strings - “Dave” ^ “ “ ^ “Walker”; > “Dave Walker” : string - print “foo\n”; foo > 3 : int Reals ; > 3.14 : real

Using SML/NJ Interactive mode is a good way to start learning and to debug programs, but… Type in a series of declarations into a “.sml” file - use “foo.sml” [opening foo.sml] … list of declarations with their types

Larger Projects SML has its own built in interactive “make” Pros: It automatically does the dependency analysis for you No crazy makefile syntax to learn Cons: May be more difficult to interact with other languages or tools

Compilation Manager % sml - OS.FileSys.chDir “ ~/courses/510/a2 ” ; - CM.make(); looks for “ sources.cm ”, analyzes dependencies [compiling … ] compiles files in group [wrote … ] saves binaries in./CM/ - CM.make ’ “ myproj/ ” (); specify directory sources.cm c.smlb.smla.sig Group is a.sig b.sml c.sml

What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “Dave”, ssn = } Lists: 3::4::5::nil or Datatypes Functions And more! Rather than list all the details, we will write a couple of programs

An interpreter Interpreters are usually implemented as a series of transformers: stream of characters abstract syntax lexing/ parsing evaluate abstract value print stream of characters

A little language (LL) An arithmetic expression e is a boolean value an if statement (if e1 then e2 else e3) an integer an add operation a test for zero (isZero e)

LL abstract syntax in ML datatype term = Bool of bool | If of term * term * term | Num of int | Add of term * term | IsZero of term -- by convention, constructors are capitalized -- constructors can take a single argument of a particular type type of a tuple another eg: string * char vertical bar separates alternatives

LL abstract syntax in ML Add (Num 2, Num 3) represents the expression “2 + 3” Add Num 23

LL abstract syntax in ML If (Bool true, Num 0, Add (Num 2, Num 3)) represents “if true then 0 else 2 + 3” Add Num 2 3 true BoolNum 0 If

Function declarations fun isValue t = case t of Num n => true | Bool b => true | _ => false function name function parameter default pattern matches anything

What is the type of the parameter t? Of the function? fun isValue t = case t of Num n => true | Bool b => true | _ => false function name function parameter default pattern matches anything

What is the type of the parameter t? Of the function? fun isValue (t:term) : bool = case t of Num n => true | Bool b => true | _ => false val isValue : term -> bool ML does type inference => you need not annotate functions yourself (but it can be helpful)

A type error fun isValue t = case t of Num n => n | _ => false ex.sml: Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: Successor t2 => true

A type error Actually, ML will give you several errors in a row: ex.sml: Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: Successor t2 => true ex.sml: Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: _ => false

A very subtle error fun isValue t = case t of num => true | _ => false The code above type checks. But when we test it refined the function always returns “true.” What has gone wrong?

A very subtle error fun isValue t = case t of num => true | _ => false The code above type checks. But when we test it refined the function always returns “true.” What has gone wrong? -- num is not capitalized (and has no argument) -- ML treats it like a variable pattern (matches anything!)

Exceptions exception Error of string fun debug s : unit = raise (Error s)

Exceptions exception Error of string fun debug s : unit = raise (Error s) - debug "hello"; uncaught exception Error raised at: ex.sml: in SML interpreter:

Evaluator fun isValue t =... exception NoRule fun eval t = case t of Bool _ | Num _ => t |...

Evaluator... fun eval t = case t of Bool _ | Num _ => t | If(t1,t2,t3) => let val v = eval t1 in case v of Bool b => if b then (eval t2) else (eval t3) | _ => raise NoRule end let statement for remembering temporary results

Evaluator exception NoRule fun eval1 t = case t of Bool _ | Num _ =>... |... | Add (t1,t2) => case (eval v1, eval v2) of (Num n1, Num n2) => Num (n1 + n2) | (_,_) => raise NoRule

Finishing the Evaluator fun eval1 t = case t of... |... | Add (t1,t2) =>... | IsZero t =>... be sure your case is exhaustive

Finishing the Evaluator fun eval1 t = case t of... |... | Add (t1,t2) =>... What if we forgot a case?

Finishing the Evaluator ex.sml: Warning: match nonexhaustive (Bool _ | Zero) =>... If (t1,t2,t3) =>... Add (t1,t2) =>... fun eval1 t = case t of... |... | Add (t1,t2) =>... What if we forgot a case?

Last Things Learning to program in SML can be tricky at first But once you get used to it, you will never want to go back to imperative languages Check out the reference materials listed on the course homepage