Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Tiger compiler Ivan Pribela. Contents The Tiger language The Tiger language Course structure Course structure Course sequence Course sequence Object-oriented.

Similar presentations


Presentation on theme: "The Tiger compiler Ivan Pribela. Contents The Tiger language The Tiger language Course structure Course structure Course sequence Course sequence Object-oriented."— Presentation transcript:

1 The Tiger compiler Ivan Pribela

2 Contents The Tiger language The Tiger language Course structure Course structure Course sequence Course sequence Object-oriented Tiger language Object-oriented Tiger language Functional Tiger language Functional Tiger language Existing course adaption to Tiger Existing course adaption to Tiger

3 Tiger compiler The Tiger language

4 Tiger programming language Tiger programming language is simple, but nontrivial language is simple, but nontrivial language belongs to Algol family with nested scope belongs to Algol family with nested scope heap-allocated records with implicit pointers heap-allocated records with implicit pointers arrays, integer and string variables arrays, integer and string variables few simple structured control constructs few simple structured control constructs Easily modified to Easily modified to a functional programming language a functional programming language be object-oriented be object-oriented The Tiger language

5 Sample Tiger programs let function do_nothing1(a: int, function do_nothing1(a: int, b: string): int = ( b: string): int = ( do_nothing2(a + 1); do_nothing2(a + 1); 0 ) function do_nothing2(d: int): function do_nothing2(d: int): string = ( string = ( do_nothing1(d, “str”); do_nothing1(d, “str”); “ ” “ ” )in do_nothing1(0, “str2”) do_nothing1(0, “str2”)endlet var a := 0 var a := 0in for i := 0 to 100 do ( for i := 0 to 100 do ( a := a + 1; a := a + 1; () () )end

6 Lexical issues Identifiers Identifiers sequence of letters, digits and underscores, starting with letter sequence of letters, digits and underscores, starting with letter Comments Comments starting with /* and ending with */ starting with /* and ending with */ can apear betwean any two tokens can apear betwean any two tokens can be nested can be nested

7 Declarations decs → { dec } dec → tydec | vardec | fundec Declaration sequence Declaration sequence a sequence of type, value and function declarations a sequence of type, value and function declarations no punctation separates or terminates individual declarations no punctation separates or terminates individual declarations

8 Data Types tydec → type id = ty ty → id | { tyfld } | array of id tyfld → λ | id : type-id {, id : type-id } Built-in types Built-in types int and string int and string ca be redefined ca be redefined Type equality Type equality by name by name Mutually recursive Mutually recursive consecutive sequence consecutive sequence list = {hd: int, tl: list } list = {hd: int, tl: list } Field name reusability Field name reusability

9 Variables vardec → var id := exp | var id [ : type-id ] := exp Variable type Variable type in short form, type of the expression is used in short form, type of the expression is used in long form, given type and type of expression must match in long form, given type and type of expression must match if expression is nil, long fom must be used if expression is nil, long fom must be used Variable lasts until end of scope Variable lasts until end of scope

10 Functions Parameters Parameters all parameters are passed by value all parameters are passed by value Mutually recursive Mutually recursive declared in consecutive sequence declared in consecutive sequence fundec → function id ( tyfld ) = exp | function id ( tyfld ) : type-id = exp

11 Scope rules Variables Variables let... vardec... in exp end let... vardec... in exp end Parameters Parameters function id (... id1 : id2... ) = exp function id (... id1 : id2... ) = exp Nested scopes Nested scopes access to a variable in outer scopes is permited access to a variable in outer scopes is permited Types Types let... typedec... in exp end let... typedec... in exp end

12 Scope rules Functions Functions let... fundec... in exp end let... fundec... in exp end Name spaces Name spaces two name spaces (types, varables & functions) two name spaces (types, varables & functions) Local redeclarations Local redeclarations object can be hidden in a smaller scope object can be hidden in a smaller scope mutually recurcive objects must have different names mutually recurcive objects must have different names

13 Values L-values L-values location whose value may be read or assigned location whose value may be read or assigned variables, procedure parameters, fields of records and elements of the array variables, procedure parameters, fields of records and elements of the array fundec → function id ( tyfld ) = exp | function id ( tyfld ) : type-id = exp

14 Expressions L-value L-value evaluates to the location contents evaluates to the location contents Valueless expressions Valueless expressions procedure calls, assignment, if-then, while, break, and sometimes if-then-else procedure calls, assignment, if-then, while, break, and sometimes if-then-else Nil Nil expression nil denotes a value nil expression nil denotes a value nil when used, it must have a type determined when used, it must have a type determined Sequencing Sequencing sequence of expressions (exp 1 ; exp 2 ;... exp n ) sequence of expressions (exp 1 ; exp 2 ;... exp n )

15 Expressions No value No value empty sequence empty sequence let expression with empty in...end let expression with empty in...end Integer literal Integer literal sequence of digits sequence of digits String literal String literal sequence of 0 or more printable characters betwean quotes sequence of 0 or more printable characters betwean quotes \\ \n \t \ddd \” \f...f\ \\ \n \t \ddd \” \f...f\ Negation Negation Function call Function call has value of function result, or produces no value has value of function result, or produces no value

16 Operations Arithmetic operators Arithmetic operators + - * / + - * / Comparison Comparison = = <> = = <> produces: 0 for false, 1 for true produces: 0 for false, 1 for true Boolean operators Boolean operators & | & | 0 is considered false, non zero is true 0 is considered false, non zero is true

17 Records and arrays Record creation Record creation type-id { id = exp {, id = exp} } type-id { id = exp {, id = exp} } Array creation Array creation type-id [ exp 1 ] of exp 2 type-id [ exp 1 ] of exp 2 Assignment and Extent Assignment and Extent records and arrays assignmen is by reference records and arrays assignmen is by reference records and arrays have infinite extent records and arrays have infinite extent

18 Statements If-then-else If-then-else if exp 1 then exp 2 [ else exp 3 ] if exp 1 then exp 2 [ else exp 3 ] exp 2 and exp 3 must be the same type exp 2 and exp 3 must be the same type While loop While loop while exp 1 do exp 2 while exp 1 do exp 2 For loop For loop for id := exp 1 to exp 2 do exp 3 for id := exp 1 to exp 2 do exp 3

19 Statements Break Break terminates evaluation of nearest while or for terminates evaluation of nearest while or for Let Let let desc in expseq end let desc in expseq end evaluates desc evaluates desc binds types variables and functions binds types variables and functions result (if any) is the result of last expression result (if any) is the result of last expression Parentheses Parentheses

20 Standard library function size (s: string): int function substring ( s: string, s: string, first: int, n: int first: int, n: int ): string function concat ( s1: string, s1: string, s2: string s2: string ): string function print (s: string) function flush () function getchar (): string function ord (s: string): int function chr (i: int): string function not (i: int): int function exit (i: int)

21 Tiger compiler Course structure

22 Lectures Lectures Lectures Students will see the theory behind different components of a compiler Students will see the theory behind different components of a compiler programming techniques used to put the theory into practice programming techniques used to put the theory into practice and the interfaces used to modularize the compiler and the interfaces used to modularize the compiler written in Java programming language written in Java programming language

23 Practical exercises Practical exercises Practical exercises The “student project compiler” is reasonably simple The “student project compiler” is reasonably simple is organized to demonstrate some important techniques is organized to demonstrate some important techniques Use abstract syntax trees to avoid tangling syntax and semantics Use abstract syntax trees to avoid tangling syntax and semantics separates instruction selection from register allocation separates instruction selection from register allocation

24 Paper exercises Each chapter has pencil-and-paper exercises Each chapter has pencil-and-paper exercises marked with a star are more challenging marked with a star are more challenging two-star problems are difficult but solvable two-star problems are difficult but solvable occasional three-star exercises are not known to have a solution. occasional three-star exercises are not known to have a solution.

25 Topic dependancy

26 One or two semester course One-semester course could cover all of Part I One-semester course could cover all of Part I Chapters 1-12 Chapters 1-12 students implement the project compiler students implement the project compiler working in groups working in groups in addition, selected topics from Part II. in addition, selected topics from Part II. An advanced or graduate course – cover Part II An advanced or graduate course – cover Part II as well as additional topics from the other literature as well as additional topics from the other literature many of the Part II chapters can stand independently many of the Part II chapters can stand independently In a two-quarter sequence In a two-quarter sequence the first quarter could cover Chapters 1-8 the first quarter could cover Chapters 1-8 and the second quarter could cover Chapters 9-12 and the second quarter could cover Chapters 9-12 and some chapters from Part II and some chapters from Part II

27 Course material Chapters in Part I Chapters in Part I are acompanied by skeleton code are acompanied by skeleton code easily built to a full working compiler module easily built to a full working compiler module can be used on practical exercises can be used on practical exercises Chapters in Part II Chapters in Part II give only theorethical knoledge give only theorethical knoledge and general instructions how to add discused feature to the Tiger compiler and general instructions how to add discused feature to the Tiger compiler there is no skeleton code there is no skeleton code

28 Target language CISC CISC few registers (16, 8, or 6) few registers (16, 8, or 6) registers divided in to classes registers divided in to classes some operations available only on certain registers some operations available only on certain registers aritmetic operations on registers and memory aritmetic operations on registers and memory two-address instructions of form r 1  r 2  3 two-address instructions of form r 1  r 2  3 various addressing modes various addressing modes variable length instructions variable length instructions instructions with side effects instructions with side effects RISC RISC 32 registers 32 registers only one class of integer/pointer registers only one class of integer/pointer registers arithmetic operations only betwean registers arithmetic operations only betwean registers tree-address instructions of form r 1  r 2  3 tree-address instructions of form r 1  r 2  3 load and store only with M[reg+const] addressing load and store only with M[reg+const] addressing every instruction is 32bit long every instruction is 32bit long one result effect per instruction one result effect per instruction

29 Tiger compiler Course sequence

30 1. Introduction 6. Activation records 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together Course sequence Phases Phases each phase is described in one section each phase is described in one section some compilers combine parse, semantic analysis some compilers combine parse, semantic analysis others put instruction selection much later others put instruction selection much later simple compilers omit control and dataflow analysis simple compilers omit control and dataflow analysis

31 1. Introduction 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together Introduction Modules and interfaces Modules and interfaces large software is much easies to understand large software is much easies to understand and to implement and to implement Tools and software Tools and software context-free grammars context-free grammars reguar expressions reguar expressions Data structures Data structures intermediate representations intermediate representations tables, trees tables, trees

32 2. Lexical analysis 6. Activation records 1. Introduction 3. Parsing 4. Abstract syntax 5. Semantic analysis 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together 2. Lexical analysis Lexical analysis Transforms program text Transforms program text reads program text reads program text outputs sequence of tokens outputs sequence of tokens Algorithm Algorithm generated from lexical specification generated from lexical specification JLex lexer generator JLex lexer generator

33 3. Parsing 6. Activation records 2. Lexical analysis 4. Abstract syntax 5. Semantic analysis 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together 3. Parsing Parsing 1. Introduction Checks program syntax Checks program syntax detects errors in order of tokens detects errors in order of tokens Parsing algorithm Parsing algorithm LALR(1) - parsing LALR(1) - parsing CUP parser generator CUP parser generator

34 4. Abstract syntax 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 5. Semantic analysis 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together 4. Abstract syntax Abstract syntax Improves modularuty Improves modularuty syntax analysis is separated form semantic analysis syntax analysis is separated form semantic analysis Semantic actions Semantic actions during parsing during parsing produce abstract parse tree produce abstract parse tree

35 5. Semantic analysis 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together 5. Semantic analysis Semantic analysis Checks program semantic Checks program semantic reports scope and type errors reports scope and type errors Actions Actions builds symbol tables builds symbol tables performes scope analysis performes scope analysis checks types checks types

36 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together 6. Activation records Activation records Functions, local variables Functions, local variables several invocations of the same function may coexist several invocations of the same function may coexist each invocation has its own instances of local variables each invocation has its own instances of local variables Stack frames Stack frames local variables, parameters local variables, parameters return address, temporaries return address, temporaries static and dynamic links static and dynamic links

37 7. Intermediate code 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together 7. Intermediate code Intermediate code Allows portability Allows portability only N front ends and M back ends only N front ends and M back ends Abstract machine language Abstract machine language can express target machine operations can express target machine operations indipendent of details of cource language indipendent of details of cource language represented by simple expression trees represented by simple expression trees

38 Intermediate code + a* b4 if = ab break:= x5 a + b * 4 if a = b then break else x:= 5

39 8. Blocks and traces 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together 8. Blocks and traces Blocks and traces 7. Intermediate code Basic blocks Basic blocks begins with a label begins with a label ends with jump ends with jump no other labels or jumps no other labels or jumps Traces Traces blocks can be arranged in any order blocks can be arranged in any order arrange that most jumps are followed by their label arrange that most jumps are followed by their label

40 9. Instruction selection 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 8. Blocks and traces 10. Liveness analysis 11. Register allocation 12. Putting all together Instruction selection 9. Instruction selection 7. Intermediate code Allows portability Allows portability finding apropriate machine instructions to implement IR finding apropriate machine instructions to implement IR Tree patterns Tree patterns one pattern represents one instruction one pattern represents one instruction instruction selection is tiling of IR tree instruction selection is tiling of IR tree

41 Instruction selection MOVE MEM + + FPCONST a * TEMP iCONST 4 MEM + FPCONST x MOVE MEM + + FPCONST a * TEMP iCONST 4 MEM + FPCONST x LOADr 1 → M [fp + a] ADDIr 2 → r MULr 2 → r i x r 2 ADDr 1 → r 1 + r 2 LOADr 2 → M [fp + x] STOREM [r 1 + 0] → r 2 LOADr 1 → M [fp + a] ADDIr 2 → r MULr 2 → r i x r 2 ADDr 1 → r 1 + r 2 ADDIr 2 → fp + x MOVEM [r 1 ] → M [r 2 ]

42 10. Liveness analysis 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 8. Blocks and traces 11. Register allocation 12. Putting all together 10. Liveness analysis Leveness analysis 9. Instruction selection 7. Intermediate code Detects needed values Detects needed values determines which variable will be needed in the future determines which variable will be needed in the future Problem Problem IR has unbounded number of temporaries IR has unbounded number of temporaries target machine has limited number of registers target machine has limited number of registers Solution Solution Control and dataflow graph Control and dataflow graph

43 11. Register allocation 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 8. Blocks and traces 10. Liveness analysis 12. Putting all together 11. Register allocation Register allocation 9. Instruction selection 7. Intermediate code Assignes registers Assignes registers links temporaries with registers links temporaries with registers Interference graph Interference graph is created from examination of control and dataflow graph is created from examination of control and dataflow graph is colored for registers to be assigned is colored for registers to be assigned

44 12. Putting all together 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 8. Blocks and traces 10. Liveness analysis 11. Register allocation 12. Putting all together Putting it all together 7. Intermediate code 9. Instruction selection Properties Properties nested functions nested functions missing structured values missing structured values tree intermediate representations tree intermediate representations register allocation register allocation Remains Remains list all registers list all registers procedure entry / exit procedure entry / exit implement strings implement strings

45 17. Dataflow analysis 18. Loop optimizations 19. Static single assignment form 20. Pipelinining, scheduling 21. Memory hierarchies 12. Putting all together 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 8. Blocks and traces 10. Liveness analysis 11. Register allocation Optimizations 7. Intermediate code 9. Instruction selection 17. Dataflow analysis 18. Loop optimizations 19. Static single assignment form 20. Pipelinining, scheduling 21. Memory hierarchies 13. Garbage collection 12. Putting all together Optimizing compiler Optimizing compiler transforms programs to improve efficiency transforms programs to improve efficiency uses dataflow analysis uses dataflow analysis Algorithms Algorithms Static single assignment form Static single assignment form use pipelining if available use pipelining if available utilize cache utilize cache

46 13. Garbage collection 12. Putting all together 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 8. Blocks and traces 10. Liveness analysis 11. Register allocation Garbage collection 7. Intermediate code 9. Instruction selection 17. Dataflow analysis 18. Loop optimizations 19. Static single assignment form 20. Pipelinining, scheduling 21. Memory hierarchies 13. Garbage collection 12. Putting all together Algorithms Algorithms mark and sweep mark and sweep reference counts reference counts copying collection copying collection generational collection generational collection Incremental collection Incremental collection

47 16. Polymorphic types 15. Functional languages 14. Object-oriented languages 6. Activation records 1. Introduction 2. Lexical analysis 3. Parsing 4. Abstract syntax 5. Semantic analysis 7. Intermediate code 8. Blocks and traces 9. Instruction selection 10. Liveness analysis 11. Register allocation 12. Putting all together Language modifications 17. Dataflow analysis 18. Loop optimizations 19. Static single assignment form 20. Pipelinining, scheduling 21. Memory hierarchies 13. Garbage collection 14. Object-oriented languages 16. Polymorphic types 15. Functional languages

48 Tiger compiler Object-oriented Tiger language

49 Object-oriented principles Information hiding Information hiding Useful software principle Useful software principle module provide values of given type module provide values of given type only that module knows its representation only that module knows its representation Extension Extension inheritance inheritance Object-Tiger Tiger can easily become object oriented

50 Program example class Truck extends Vehicle { class Truck extends Vehicle { method move (int x) = method move (int x) = if x <= 80 then if x <= 80 then position := position + x position := position + x } var t := new Truck var t := new Truck var v: Vehicle := t var v: Vehicle := tin t.move(50) t.move(50) v.move(100) v.move(100)endlet start := 10 start := 10 class Vehicle extends Object { class Vehicle extends Object { var position := start var position := start method move (int x) = ( method move (int x) = ( position := position + x position := position + x ) }

51 Classes in Object-Tiger Added class definitions Added class definitions No multiple inheritance No multiple inheritance Allows method override Allows method override dec → classdec classdec → class class-id extends class-id { { classfield} } classfield → vardec | method method → method id ( tyfld ) = exp method id ( tyfld ) : type-id = exp

52 Expressions in Object-Tiger exp → new class-id lvalue. id ( ) lvalue. id ( exp {, exp } ) Added object construction Added object construction And method invocation And method invocation

53 Tiger compiler Functional Tiger language

54 Functional languages Equational reasoning Equational reasoning if f(x) = a this time if f(x) = a this time f(x) must be a next time f(x) must be a next time Imperative languages Imperative languages functions have side effects functions have side effects x can be changed betwean calls x can be changed betwean calls

55 Functions in Fun-Tiger ty → ty → ty | ( ty {, ty } ) → ty | ( ) → ty exp → exp ( exp {, exp } ) | exp ( ) Added function type Added function type function type is equal to other data types function type is equal to other data types any expression can be called any expression can be called

56 PureFun-Tiger Modifications Modifications no assignment statement no assignment statement no while and for no while and for no conpound statements no conpound statements

57 Tiger compiler Existing course adaption to Tiger

58 First part Every chapter in Part I Every chapter in Part I starts with problem definition starts with problem definition describes few algorithms describes few algorithms discuses the best solution for Tiger discuses the best solution for Tiger specifies actions to build the part of compiler from given skeleton specifies actions to build the part of compiler from given skeleton recomends further reading recomends further reading contains pencil-and-paper exercises contains pencil-and-paper exercises

59 Second part Every chapter in Part II Every chapter in Part II starts with problem definition starts with problem definition describes few algorithms describes few algorithms discuses the best solution for Tiger discuses the best solution for Tiger there is no skeleton code there is no skeleton code recomends further reading recomends further reading contains pencil-and-paper exercises contains pencil-and-paper exercises

60 Tiger compiler The end


Download ppt "The Tiger compiler Ivan Pribela. Contents The Tiger language The Tiger language Course structure Course structure Course sequence Course sequence Object-oriented."

Similar presentations


Ads by Google