Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each."— Presentation transcript:

1 CS 320: Compiling Techniques David Walker

2 People David Walker (Professor) 412 Computer Science Building dpw@cs.princeton.edu office hours: after each class Dan Dantas (TA) 417 Computer Science Building ddantas@cs.princeton.edu office hours: Mondays 2-3 PM

3 Information Web site: www.cs.princeton.edu/courses/archive/spri ng04/cos320/index.htm www.cs.princeton.edu/courses/archive/spri ng04/cos320/index.htm Mailing list:

4 Books Modern Compiler Implementation in ML Andrew Appel required Elements of ML Programming Jeffrey D. Ullman also: online references; see Web site

5 Assignment 0 Write your name and other information on the sheet circulating Find, skim and bookmark the course web pages Subscribe to course e-mail list Begin assignment 1 Read chapter 1 Appel Figure out how to run & use SML Due next Thursday 12

6 onward!

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

8 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

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

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

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

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

13 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...

14 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

15 Course project Tiger Source Language simple imperative language Instruction Trees as intermediate form (IR) type checking; data layout on the stack Code Generation instruction selection algorithms; register allocation via graph coloring front-end middle-end back-end

16 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

17 Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Resources: Jeffrey Ullman “Elements of ML Programming” Robert Harper’s “an introduction to ML” See course webpage for pointers and info about how to get the software

18 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)

19 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

20 Preliminaries Read – Eval – Print – Loop - 3 + 2;

21 Preliminaries Read – Eval – Print – Loop - 3 + 2; > 5: int

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

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

24 Preliminaries Read – Eval – Print – Loop - 3 div 0; Failure : Div- run-time error

25 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

26 Basic Values Integers - 3 + 2 > 5 : int - 3 + (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; > 3.14 : real

27 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

28 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

29 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

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

31 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

32 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)

33 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 -- 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

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

35 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

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

37 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

38 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)

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

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

41 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?

42 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!)

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

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

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

46 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

47 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

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

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

50 Finishing the Evaluator ex.sml:25.2-35.12 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?

51 ML is all about functions There are many different ways to define functions! I almost always use “fun f x =...” When I am only going to use a function once and it is not recursive, I write an anonymous function: (fn x =>...)

52 Anonymous functions val n = 3 val isNumberValue = (fn t => case t of Num _ | Bool _ => true | _ => false) binds a variable (n) to a value (3) binds a variable (isNumberValue) to the anonymous function value fn keyword introduces anonymous fun

53 Anonymous functions type ifun = int -> int val intCompose : ifun * ifun -> ifun =... fun add3 x = intCompose ((fn x => x + 2), (fn y => y + 1)) x a pair of anonymous functions a type definition (very convenient)

54 Anonymous functions type ifun = int -> int val intCompose : ifun * ifun -> ifun = fn (f,g) => (fn x => f (g x)) fun add3 x = intCompose ((fn x => x + 2), (fn y => y + 1)) x result is a function! argument is pair of functions pattern match against arg

55 Another way to write a function fun f x =........ can be written as: val f = (fn x =>......) provided the function is not recursive; f does not appear in........

56 Another way to write a function fun f x =.... can always be written as: val rec f = (fn x =>...f can be used here...) keyword rec declares a recursive function value

57 Yet another way to write a function fun isValue (Num _) = true | isValue (Bool _) = true | isValue (_) = false This is just an abbreviation for fun isNumberValue t = case t of Num _ => true | Bool _ => true | _ => false

58 Yet another way to create a type error fun isValue 0 = true | isValue (Bool _) = true | isValue (_) = false ex.sml:9.1-11.29 Error: parameter or result constraints of clauses don't agree [literal] this clause: term -> 'Z previous clauses: int -> 'Z in declaration: isValue = (fn 0 => true | Bool _ => true | _ => false)

59 Parametric Polymorphism Functions like compose work on objects of many different types val compose = fn f => fn g => fn x => f (g x) compose (fn x => x + 1) (fn x => x + 2) compose not (fn x => x < 17)

60 Parametric Polymorphism Functions like compose work on objects of many different types val compose = fn f => fn g => fn x => f (g x) compose not (fn x => x < 17) compose (fn x => x < 17) not BAD!!

61 Parametric Polymorphism Functions like compose work on objects of many different types val compose = fn f => fn g => fn x => f (g x) compose: (‘a -> ‘b) -> (‘c -> ‘a) -> (‘c -> ‘b) Note: type variables are written with ‘

62 Parametric Polymorphism compose not : (‘c -> bool) -> (c’ -> bool) compose not not : bool -> bool compose : (‘a -> ‘b) -> (‘c -> ‘a) -> (‘c -> ‘b) not : bool -> bool

63 Parametric Polymorphism compose (fn x => x) : ? compose : (‘a -> ‘b) -> (‘c -> ‘a) -> (‘c -> ‘b) not : bool -> bool

64 Parametric Polymorphism compose (fn x => x) : ? compose : (‘a -> ‘b) -> (‘c -> ‘a) -> (‘c -> ‘b) not : bool -> bool ‘d -> ‘d

65 Parametric Polymorphism compose (fn x => x) : ? compose : (‘a -> ‘b) -> (‘c -> ‘a) -> (‘c -> ‘b) not : bool -> bool ‘d -> ‘d must be the same ie: ‘a = ‘d ‘b = ‘d

66 Parametric Polymorphism compose (fn x => x) : ? compose : (‘d -> ‘d) -> (‘c -> ‘d) -> (‘c -> ‘d) not : bool -> bool ‘d -> ‘d must be the same ie: ‘a = ‘d ‘b = ‘d

67 Parametric Polymorphism compose (fn x => x) : ? compose : (‘d -> ‘d) -> (‘c -> ‘d) -> (‘c -> ‘d) not : bool -> bool ‘d -> ‘d (‘c -> ‘d) -> (‘c -> ‘d)

68 Lists Lists: nil : ‘a list :: : ‘a * ‘a list -> ‘a list 3 :: 4 :: 5 :: nil : int list (fn x => x) :: nil : (‘a -> ‘a) list

69 List Processing Functions over lists are usually defined by case analysis (induction) over the structure of a list Hint: often, the structure of a function is guided by the type of the argument (recall eval) fun length l = case l of nil => 0 l x :: l => 1 + (length l)

70 List Processing fun map f l = case l of nil => [] l x :: l => (f x) :: (map f l) an incredibly useful function: - map (fn x => x+1) [1,2,3]; > val it = [2,3,4] ; int list

71 List Processing fun fold f a l = case l of nil => a l x :: l => f (fold f a l) x another incredibly useful function what does it do? use it to write map.

72 ML Modules Signatures Interfaces Structures Implementations Functors Parameterized structures Functions from structures to structures

73 Structures structure Queue = struct type ‘a queue = ‘a list * ‘a list exception Empty val empty = (nil, nil) fun insert (x, q) = … fun remove q = … end

74 Structures structure Queue = struct type ‘a queue = ‘a list * ‘a list exception Empty... end fun insert2 q x y = Queue.insert (y, Queue.insert (q, x))

75 Structures structure Queue = struct... end structure Q = Queue fun insert2 q x y = Q.insert (y, Q.insert (q, x)) convenient abbreviation

76 Structures structure Queue = struct... end open Queue fun insert2 q x y = insert (y, insert (q, x)) for lazy programmers -- not encouraged!

77 Structures structure Queue = struct type ‘a queue = ‘a list * ‘a list... end fun insert2 (q1,q2) x y : ‘a queue = (x::y::q1,q2) by default, all components of the structure may be used -- we know the type ‘ a queue

78 Signatures signature QUEUE = sig type ‘a queue exception Empty val empty : ‘a queue val insert : ‘a * ‘a queue -> ‘a queue val remove : ‘a queue -> ‘a * ‘a queue end abstract type -- we don ’ t know the type ‘ a queue

79 Information hiding signature QUEUE = sig type ‘a queue... end structure Queue :> QUEUE = struct type ‘ a queue = ‘ a list * ‘ a list val empty = (nil, nil) … end fun insert2 (q1,q2) x y : ‘ a queue = (x::y::q1,q2) does not type check

80 Signature Ascription Opaque ascription Provides abstract types structure Queue :> QUEUE = … Transparent ascription A special case of opaque ascription Hides fields but does not make types abstract structure Queue_E : QUEUE = … SEE Harper, chapters 18-22 for more on modules

81 Other things functors (functions from structures to structures) references (mutable data structures) ref e; !e; e1 := e2 while loops, for loops arrays (* comments (* can be *) nested *) a bunch of other stuff...

82 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


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

Similar presentations


Ads by Google