compiler construction
play

Compiler Construction Compiler Construction 1 / 193 Mayer Goldberg - PowerPoint PPT Presentation

Compiler Construction Compiler Construction 1 / 193 Mayer Goldberg \ Ben-Gurion University Friday 23 rd October, 2020 Mayer Goldberg \ Ben-Gurion University Chapter 1 Goals Agenda Compiler Construction 2 / 193 Establishing common language


  1. Imperative vs functional languages ( continued ) electrical engineering Computers their outputs to memory frequency measured in Hz, KHz, MHz, GHz…) Compiler Construction 22 / 193 ▶ Computer science is the illegitimate child of mathematics and ▶ Electrical engineering gave us the hardware , the actual machine ▶ Nearly all ideas come from mathematics ▶ Digital electronics: Gates, fmip-fmops, latches, memory, etc ▶ Boolean functions that read their inputs from memory & write ▶ Reading & writing are synchronized using a clock (with a ▶ A fjnite-state machine Mayer Goldberg \ Ben-Gurion University

  2. Imperative vs functional languages ( continued ) level of digital electronics computation Compiler Construction 23 / 193 ▶ We cannot design large software systems while thinking at the ▶ We need some theoretical foundations for programming & Mayer Goldberg \ Ben-Gurion University

  3. Imperative vs functional languages ( continued ) What is mathematics? expressed in this language What computer science takes from mathematics? In short: Everything! Compiler Construction 24 / 193 ▶ A language ▶ A set of ideas, notions, defjnitions, techniques, results, all ▶ Programming is based on a computable mathematics ▶ Theoretical computer science uses all of mathematics Mayer Goldberg \ Ben-Gurion University

  4. Imperative vs functional languages ( continued ) What programming languages take from mathematics? numbers, types, sets, ordered n tuples, structures, etc tuples, sets, etc), abstraction, mapping, folding, etc. Nearly all of the ideas in computer science come from mathematics! Compiler Construction 25 / 193 ▶ A language ▶ Notions such as functions, variables, expressions, evaluation, ▶ Operations such as arithmetic, Boolean, structural (e.g., on n Mayer Goldberg \ Ben-Gurion University

  5. Imperative vs functional languages ( continued ) Mathematical objects are free of the limitations that beset computers: Real-world compromises physical computer: Compiler Construction 26 / 193 ▶ Computers can only approximate real numbers ▶ Computers cannot implement infjnite tape (Turing machines) ▶ Mathematical objects are cheaper than objects created on a ▶ Functions are mappings; They take no time! ▶ Knowing that an object exists is often all we need! ▶ Bad things cannot happen: ▶ No exceptions, errors, incorrect results ▶ Nothing is lost, nothing is “too big” or “too much” Mayer Goldberg \ Ben-Gurion University

  6. Imperative vs functional languages ( continued ) Functional programming languages Imperative programming languages Compiler Construction 27 / 193 ▶ Closer to mathematics ▶ Easier to reason about ▶ Easier to transform ▶ Easier to generate automatically ▶ Farther from mathematics ▶ Harder to reason about ▶ Harder to transform ▶ Harder to generate automatically Mayer Goldberg \ Ben-Gurion University

  7. Imperative vs functional languages ( continued ) Example of non-mathematical ideas: Side efgects Compiler Construction string string -> int that returns the number of characters in the 28 / 193 (rather than char * ) Imagine having to teach C programming to a logician ▶ To simplify matters, let’s pretend there is a string type in C ▶ You teach them a simplifjed version of printf : ▶ Only takes a single string argument ▶ Returns an int : the number of characters in the string ▶ Roughly: printf : string -> int ▶ But the logician objects: He already knows of a function from ▶ strlen : string -> int ▶ He wants to know the difgerence between printf and strlen Mayer Goldberg \ Ben-Gurion University

  8. Imperative vs functional languages ( continued ) Side efgects: The Dialogue the screen & also returns the number of characters it printed!” right?” printf !” Compiler Construction 29 / 193 ▶ You: “Simple, printf also prints the string to the screen!” ▶ Logician: “What does it mean to print??” ▶ You: “Seriously?? The printf function prints its argument to ▶ Logician: “But you said the domain of printf is string -> int , ▶ You: “Yes, so?” ▶ Logician: “Then where’s the screen??” ▶ You: “In front of you!” ▶ Logician: “Where’s the screen in the domain of the function Mayer Goldberg \ Ben-Gurion University

  9. Imperative vs functional languages ( continued ) Side efgects: The Dialogue ( continued ) global variable.” be a variable when it’s not passed as a parameter, and its type is not expressed in the domain of the function??” efgect: It is not expressed in the type!” domain of printf all wrong!” Compiler Construction 30 / 193 ▶ You: “It isn’t in the domain. You can think of the screen as a ▶ Logician: “I have no idea what you mean: How can the screen ▶ You: “But that’s the whole point of this printing being a side ▶ Logician: “Well, then printf isn’t a function!” ▶ You: “Ummm…” ▶ Logician (having a Eureka!-moment): “I get it now! You got the Mayer Goldberg \ Ben-Gurion University

  10. Imperative vs functional languages ( continued ) Side efgects from a logical point of view Compiler Construction been mapped into another screen in the range. the function call an illusion of change, as if the environment has changed: An 31 / 193 written explicitly in the original type given for printf ▶ The real type of printf is string × screen → int × screen ▶ The underlined parts of the type are implicit, i.e., they are not ▶ The implicit parts of the type form the environment ▶ The function call mentions only the explicit arguments ▶ Leaving out the implicit arguments in the function call creates illusory notion of time has been created; We have the environment before the function call, and the environment after ▶ In fact, nothing has changed: The screen in the domain has Mayer Goldberg \ Ben-Gurion University

  11. Imperative vs functional languages ( continued ) Side efgects from a logical point of view ( continued ) { printf("Hello "); printf("world!\n"); } screen object in the domain of printf("world!\n"); “ordering” matters! Compiler Construction 32 / 193 ▶ Introducing side efgects introduces discrete time ▶ Having introduced time, we must now introduce sequencing: ▶ The notion of sequencing is, like time, illusory: ▶ The screen object in the range of printf("Hello "); is the ▶ So the two printf expressions are nested, and this is why their Mayer Goldberg \ Ben-Gurion University

  12. Imperative vs functional languages ( continued ) Imperative C Compiler Construction printf to printf 33 / 193 Functional Rendition } printf("World!\n"); printf("Hello "); { ⟨ "Hello " , screen ⟩ = ⇒ ⟨ 6 , screen ⊕ "Hello " ⟩ ⟨ "World!\n" , screen ⊕ "Hello " ⟩ = ⇒ ⟨ 7 , screen ⊕ "Hello World\n" ⟩ ▶ The return value of the second call ▶ The screen after the second call to Mayer Goldberg \ Ben-Gurion University

  13. Imperative vs functional languages ( continued ) Functional programming languages computation they are used to implement it on a real computer!) Compiler Construction 34 / 193 ▶ Closer to the mathematical notions of function, variable, ▶ Nothing is implicit ▶ Easier to reason about ▶ Side efgects are not an explicit part of the language (although ▶ Ofgers many other advantages Mayer Goldberg \ Ben-Gurion University

  14. Imperative vs functional languages ( continued ) Imperative programming languages variable, evaluation convenience) sequences, environment, etc., that require translation before we can reason about them various parts of a software system, making it harder to debug Compiler Construction 35 / 193 ▶ Farther away from the mathematical notions such as function, ▶ Hides information through the use of implicit arguments (for ▶ Harder to reason about: Contains notions such as side efgects, ▶ Abstraction is harder, prone to errors ▶ Side efgects create implicit, knotty inter-dependencies between Mayer Goldberg \ Ben-Gurion University

  15. Imperative vs functional languages ( continued ) Abstraction in functional programming languages etc Abstraction in imperative programming languages command in Latin) Compiler Construction 36 / 193 ▶ Values ⇒ Expressions ⇒ Functions ▶ Higher-order functions ▶ Mathematical operators: mapping, folding, fjltering, partitioning, ▶ The interpreter evaluates expressions ▶ State ⇒ Change ⇒ Commands ⇒ Procedures ▶ Object-oriented programming ▶ Imperative ≡ Based upon commands ( imperare means to ▶ The interpreter performs commands Mayer Goldberg \ Ben-Gurion University

  16. Programming paradigms: A reality check in which side-efgects cannot be expressed impossible to use side-efgects, but they don’t make these easy or fun to use functional languages Compiler Construction 37 / 193 ▶ There are very few strictly functional languages, i.e., languages ▶ Most languages are quasi-functional: They don’t make it ▶ Most new imperative languages do include features from ▶ anonymous functions (“lambda”) ▶ higher-order functions ▶ modules/namespaces/functors Mayer Goldberg \ Ben-Gurion University

  17. Imperative vs functional languages ( continued ) Question Languages based on the functional paradigm — Compiler Construction 38 / 193 👏 do not allow us to perform side efgects 👏 do not support object-oriented programming 👏 are not suited to model and solve “real” problems 👏 are slow and ineffjcient 👎 support high order functions Mayer Goldberg \ Ben-Gurion University

  18. Further Reading http://blog.jenkster.com/2015/12/what-is-functional-programming.html , or Compiler Construction 39 / 193 🔘 Comparing programming paradigms 🔘 What is functional programming at Mayer Goldberg \ Ben-Gurion University

  19. Interpreters evaluate/perform Introduction to compiler construction The functional picture (evaluate) Compiler Construction 40 / 193 ▶ L 1 : Language ▶ L 2 : Language ▶ P L : A program in language L ▶ Values: A set of values ▶ � · � : Semantic brackets ▶ An interpreter is a function Int L : L → Values ▶ Interpreters map expressions to their values ▶ For example: In functional Scheme, we have Int Scheme � (+ 3 5) � = 8 Mayer Goldberg \ Ben-Gurion University

  20. Introduction to compiler construction Interpreters evaluate/perform Compiler Construction created: “Something changed in the environment” environment in the range, an illusion of change has been 41 / 193 environment to the product of a value and an environment The imperative picture (perform) ▶ An interpreter is a function Int L : L × Environment → Values × Environment ▶ Interpreters map the product of an expression and an ▶ The environments are implicit in the imperative language ▶ When the environment in the domain is not equal to the ▶ For example: In imperative Scheme, we have Int Scheme � ⟨ ( defjne x 3) , { x �→ undefjned }⟩ � = ⟨ # ⟨ void ⟩ , { x �→ 3 }⟩ Mayer Goldberg \ Ben-Gurion University

  21. Introduction to compiler construction Compilers translate interpreters for both the source and target language: Compiler Construction 42 / 193 L 1 : L 1 → L 2 ▶ A compiler is a function Comp L 2 ▶ Compilers translate programs from one language to another ▶ Let P L 1 ∈ L 1 , then Comp L 2 L 1 � P L 1 � ∈ L 2 ▶ The correctness of the translation is established using Int L 1 � P L 1 � = Int L 2 � Comp L 2 L 1 � P L 1 �� Mayer Goldberg \ Ben-Gurion University

  22. Introduction to compiler construction Compilers translate ( continued ) Compiler Construction 43 / 193 ▶ We may chain any number of compilers together: Int L 1 � P L 1 � = Int L 2 � Comp L 2 L 1 � P L 1 �� = Int L 3 � Comp L 3 L 2 � Comp L 2 L 1 � P L 1 ��� = Int L 4 � Comp L 4 L 3 � Comp L 3 L 2 � Comp L 2 L 1 � P L 1 ���� = . . . etc. Mayer Goldberg \ Ben-Gurion University

  23. Introduction to compiler construction Question Compiled code is generally faster than interpreted code. So why do we need interpreters? Why not stick with compiled code? Compiler Construction 44 / 193 👏 It’s easier to program in interpreted languages 👏 It’s easier to debug interpreted code 👏 Interpreters are more fmexible than compilers 👏 The difgerence is pretty much a matter of personal taste 👎 Interpreters are the only way to reach values Mayer Goldberg \ Ben-Gurion University

  24. Introduction to compiler construction Question When we program directly in machine language, where’s the interpreter? underlying interpretation interpreter for the given micro-architecture Compiler Construction 45 / 193 👏 The operating system is the interpreter 👏 The shell is the interpreter 👏 There is no interpreter 👏 The process of translating code to machine language is the 👎 The microprocessor is a hardware implementation of an Mayer Goldberg \ Ben-Gurion University

  25. Introduction to compiler construction Question If interpreters are a logical necessity, why do we need a compiler? Compiler Construction 46 / 193 👏 For generality 👏 For ease 👏 For fmexibility 👏 For portability 👎 For optimizations Mayer Goldberg \ Ben-Gurion University

  26. Introduction to compiler construction Another way of looking at the question A compiler from language L to language L (itself!) is… …just an optimizer! Compiler Construction 47 / 193 Mayer Goldberg \ Ben-Gurion University

  27. What is an optimization More effjcient code Compiler Construction or any other resource consumed by the code A compiler optimization is a semantics-preserving transformation that Resources include: 48 / 193 interpretation results in more effjcient code Semantics-preserving ( ≡ meaning-preserving) ▶ Returns the same value (as the original code) under ▶ The interpreter takes less resources to evaluate the code. ☞ Execution time ▶ Computer memory ▶ Network traffjc ▶ Microprocessor registers Mayer Goldberg \ Ben-Gurion University

  28. What is an optimization ( continued ) Special-purpose compilers may optimize for non-standard “resources”: motion crossovers of edges “widows”, “orphans”, hyphenations, and other typographically problematic conditions Compiler Construction 49 / 193 ▶ Compilers to g-code , a language for CNCs, will try to minimize ▶ Compilers of graphs to visual presentations will try to minimize ▶ Compilers of document-layout languages will try to minimize Mayer Goldberg \ Ben-Gurion University

  29. What is an optimization ( continued ) Question Why do programmers write less-than-optimized code? What is the rationale for having compilers be in charge of optimizations? Are All these reasons are true, but irrelevant! intentionally write less-effjcient code? Compiler Construction 50 / 193 good programmers really that rare?? ▶ Good code is hard to write ▶ Some programmers are incompetent ▶ Compilers are faster at detecting opportunities for optimizations ▶ Consistent output: Once debugged, compilers make no mistakes ☞ Can you envision a reason why a programmer might Mayer Goldberg \ Ben-Gurion University

  30. What is an optimization ( continued ) Which code is better: Code I for (i = 0; i < 200; ++i) { A[i] = 0; } for (i = 0; i < 200; ++i) { B[i] = 0; } Code II for (i = 0; i < 200; ++i) { A[i] = 0; B[i] = 0; } Code III #define M 200 #define N 200 ... for (i = 0; i < M; ++i) { A[i] = 0; } for (i = 0; i < N; ++i) { B[i] = 0; } Compiler Construction 51 / 193 Mayer Goldberg \ Ben-Gurion University

  31. What is an optimization ( continued ) Analysis Compiler Construction 52 / 193 ▶ Code I ▶ less general than Code III ▶ less effjcient than Code II ▶ less maintainable than Code III ▶ Code II ▶ less general than Code I ▶ more effjcient than Code I, Code III ▶ less maintainable than Code I ▶ Code III ▶ more general than Code I, Code II ▶ less effjcient than Code II ▶ more maintainable than Code I, Code II Mayer Goldberg \ Ben-Gurion University

  32. What is an optimization ( continued ) So which code is better? FACT: Optimizing the loops in Code III, converting it to Code II, is performed by most compilers today Conclusion People write less-than-optimal code because: Compiler Construction 53 / 193 ▶ Clearly Code III is better! ▶ It is more general ▶ It is more maintainable ▶ It is more fmexible ▶ Most compilers optimize away such ineffjciency Mayer Goldberg \ Ben-Gurion University

  33. What is an optimization ( continued ) How people use compilers Compiler Construction obtain effjcient code. unmaintainable, overly-specifjc, machine-dependent code in order to Without optimizing compilers, we would be forced to write 54 / 193 effjciency: ▶ Want to program in a more general, maintainable, fmexible way ▶ Compilation is a point-in-time where generality is traded for ▶ Compilers are opportunistic ▶ Compilers identify opportunities to trade generality for effjciency ▶ The resulting code is unreadable, unmaintainable, machine-specifjc, and fast ▶ We [normally] don’t touch the compiled code: For programmers, the quality of only the source code matters Mayer Goldberg \ Ben-Gurion University

  34. Composing & combining processors, interpreters, compilers, & programs Compiler Construction for you! We have 5 new blocks Remember Lego? languages!) into equivalent programs written in in language L programs written in language L We can compose 55 / 193 ▶ Processors running programs written ▶ Interpreters written in L ′ , running ▶ Compilers written in L ′ , running on L , compile programs written in L ′′ L ′′′ (we are talking about 4 ▶ Programs written in L ′ , running on L Mayer Goldberg \ Ben-Gurion University

  35. Composing & combing processors, interpreters, compilers, & programs A processor interpreters for some language The languages they provide What it looks like Compiler Construction 56 / 193 provides ▶ Hardware-implementations of lang processor ▶ They have only 1 docking point: Mayer Goldberg \ Ben-Gurion University

  36. Composing & combing processors, interpreters, source language and compiled to Compiler Construction compilers, & programs additional docking point compiler. Such programs have an some other language using a What it looks like these days point: The language they run on A program 57 / 193 binary. This is hardly ever done hand-written directly in hex or ▶ Programs have at least 1 docking machine- ▶ Mach Lang programs are runs language on program ▶ Other programs are written in a program lang runs src on Mayer Goldberg \ Ben-Gurion University

  37. Composing & combing processors, interpreters, which they run Compiler Construction compilers, & programs they were written What it looks like points: An interpreter 58 / 193 provides ▶ Interpreters come with 3 docking lang interpreter ▶ The language they provide ▶ The language [interpreter] on lang runs src on ▶ The [source] language in which Mayer Goldberg \ Ben-Gurion University

  38. Composing & combing processors, interpreters, written Compiler Construction compilers, & programs What it looks like which they run 59 / 193 A compiler points: ▶ Compilers come with 4 docking compiling program ▶ The language they compile from compiler ▶ The language they compile to ▶ The language in which they were lang runs lang src dst on ▶ The language [interpreter] on Mayer Goldberg \ Ben-Gurion University

  39. Composing & combing processors, interpreters, compilers, & programs Why bother with these pics?? compositions are correct Compiler Construction 60 / 193 ▶ Interpreters & compilers are often composed in complex ways ▶ Diagrams provide a simple, visual way to make sure that the Mayer Goldberg \ Ben-Gurion University

  40. Composing & combing processors, interpreters, by the processor Compiler Construction compilers, & programs What it looks like 61 / 193 same machine-language interpreted running A machine language program machine- runs language on ▶ The program must be written in the program provides lang processor ▶ The two blocks join naturally Mayer Goldberg \ Ben-Gurion University

  41. Composing & combing processors, interpreters, the language the program was Compiler Construction compilers, & programs written in (though we could!) What it looks like processor compiled into the same A compiled program running 62 / 193 machine-language interpreted by the ▶ The program must have been program lang runs src on provides lang ▶ The two blocks join naturally ▶ We are not saying anything about processor Mayer Goldberg \ Ben-Gurion University

  42. Composing & combing processors, interpreters, language Compiler Construction compilers, & programs What it looks like 63 / 193 in a given language An interpreter running a compiled program (I) program ▶ Interpreters are similar to processors lang runs src on ▶ They execute/evaluate programs provides lang ▶ Interpreters are programs too! interpreter ▶ Written in some source language lang runs src on ▶ Compiled into some target provides lang ▶ They run on an interpreter: processor ▶ a processor (hardware) ▶ a program (another interpreter) Mayer Goldberg \ Ben-Gurion University

  43. Composing & combing processors, interpreters, source, and Compiler Construction compilers, & programs What it looks like executed right) as target 64 / 193 program (II) An interpreter running a compiled another language. Note that the programs that were compiled from compiler program ▶ Interpreters can execute/evaluate lang runs src on compiling program compiler program lang runs lang lang runs src dst src on on ▶ takes the program (on top) as provides lang interpreter lang runs src on ▶ outputs the program (on the provides lang processor ▶ It is the target program that is Mayer Goldberg \ Ben-Gurion University

  44. Composing & combing processors, interpreters, interpreter Compiler Construction compilers, & programs What it looks like compiler 65 / 193 program (III) which the compiler executes. An interpreter running a compiled program ▶ We may add additional details lang runs src on ▶ the processor/interpreter on compiling program compiler program lang runs lang lang runs src dst src on on ▶ We are still missing details provides provides lang lang ▶ the compiler that compiled the processor interpreter lang runs src on provides ▶ the compiled that compiled the lang processor ▶ …this can go on! Mayer Goldberg \ Ben-Gurion University

  45. Composing & combing processors, interpreters, compilers, & programs Cross-compilers one one which the [compiled] program runs Compiler Construction 66 / 193 ▶ The processor on which the compiler runs is difgerent from the ▶ It crosses the boundaries of architectures. ▶ Java compiler javac is an example of a cross-compiler: ▶ It runs on [e.g.,] x86 ▶ It generates Java-byte-code that runs on the JVM ▶ The JVM is an interpreter ( java ) running on [e.g.,] x86 Mayer Goldberg \ Ben-Gurion University

  46. Composing & combing processors, interpreters, slowdown, this is not really a Compiler Construction compilers, & programs What it looks like actually does this! stacked up as towers of interpreters tower! 67 / 193 tower Towers of interpreters height of the tower ▶ Interpreters may be stacked up in a program lang src runs on ▶ Towers of interpreters consume provides lang interpreter resources that are exponential to the runs lang src on provides lang ▶ Unless there is a marked interpreter lang runs src on provides lang interpreter ▶ Virtual machines (VMs) can be lang runs src on provides lang ▶ IBM mainframe architecture processor Mayer Goldberg \ Ben-Gurion University

  47. Composing & combing processors, interpreters, compilers, & programs How are compilers and interpreters made? of composing & combining compilers and interpreters… Compiler Construction 68 / 193 ▶ Using previously-written compilers and interpreters, of course! ▶ This process is known as bootstrapping, and it is a specifjc form Mayer Goldberg \ Ben-Gurion University

  48. Bootstrapping (I) x86 Compiler Construction mainframe! detach from the mainframe! re-done from scratch in x86 assembly language if we are to mainframe x86 exe x86 asm x86 c 2 x86 asm compiler x86 exe x86 asm c 3 c 1 c 1 written in compiled by runs on compiles x86 asm outputs pascal ibm pascalvs ibm 370 x86 asm x86 exe c 2 69 / 193 ▶ c 1 is an assembler acting as a cross-compiler ▶ c 2 already runs on our PC, but it was created on an IBM ▶ All the efgort of writing an assembler (in Pascal) has to be ▶ Any updates, upgrades, bug fjxes, changes, require that we re-compile c 2 on the mainframe! ▶ c 3 is essentially c 2 compiling itself ▶ With c 3 , we are free from our old environment: Pascal on IBM Mayer Goldberg \ Ben-Gurion University

  49. Bootstrapping (II) x86 exe Compiler Construction compiler supports! x86 exe C (v. 0.2) x86 c 5 C (v. 0.2) c 6 x86 exe C (v. 0.2) x86 compiler C (v. 0.1) c 5 c 4 C (v. 0.1) x86 asm written in compiled by runs on x86 outputs c 3 compiles c 2 x86 x86 asm x86 exe c 4 x86 asm c 3 70 / 193 ▶ With c 4 we’re diverging: ▶ c 4 is a C compiler ▶ We don’t yet support many features ▶ c 5 is a C compiler written in C! Notice that ▶ it is written in an older version of C (v. 0.1) ▶ it supports a newer version of C (v. 0.2) ▶ In writing c 6 , we fjnally get to use all the language features our Mayer Goldberg \ Ben-Gurion University

  50. Why study compiler construction course Compiler Construction 71 / 193 ▶ Almost all of you shall use compilers ▶ Most of you shall never work on another compiler after this ☞ So why study an entire course on writing compilers?? Mayer Goldberg \ Ben-Gurion University

  51. Why study compiler construction ( cont ) There are many benefjts to studying compilers language Compiler Construction 72 / 193 ▶ Better understanding of programming languages ▶ Reduce the learning-curve for learning a new programming ▶ Better understanding of what compilers can & cannot do ▶ Demystify the compilation process ☞ Compilers are examples of code that writes code Mayer Goldberg \ Ben-Gurion University

  52. bugs never re-appear Code that writes code Compiler Construction 73 / 193 ▶ Like having a team of programmers working for you ▶ Save time; Write code quickly ▶ Gain consistency ▶ Spread your bugs everywhere 🎊 ▶ A bug in the code generator will spread bugs to many places ▶ This actually makes the bugs easier to fjnd! ▶ Once debugged, the code is generated again, and the same Mayer Goldberg \ Ben-Gurion University

  53. Why study compiler construction ( cont ) Bottom line programmer Compiler Construction 74 / 193 ▶ Knowledge of how compilers work will make you a more efgective ▶ Learning to write code that writes code is a force-multiplying technique you can use anywhere (think DSLs!) Mayer Goldberg \ Ben-Gurion University

  54. Further Reading Introduction) Compiler Construction 75 / 193 🕯 Modern compiler design (2nd edition), Page 1 (Section 1: 🔘 Self hosting 🔘 Bootstrapping 🔘 Interpreters Mayer Goldberg \ Ben-Gurion University

  55. Chapter 1 Goals Agenda Compiler Construction 76 / 193 🗹 Establishing common language & vocabulary 🗹 Understanding the “big picture” 🗹 Some background in programming languages ▶ Abstraction ▶ Dynamic vs Static ▶ Functional vs Imperative languages 🗹 Introduction to compiler construction ☞ Introduction to the ocaml programming language Mayer Goldberg \ Ben-Gurion University

  56. Languages used in this course In this course, we shall be working with 3 languages: language Compiler Construction 77 / 193 ▶ The language in which to write the compiler: ocaml ▶ The language we shall be compiling: Scheme ▶ The language we shall be compiling to: x86/64 assembly Mayer Goldberg \ Ben-Gurion University

  57. Introduction to ocaml (1) languages Compiler Construction 78 / 193 ▶ ML is a family of statically-typed, quasi-functional programming ▶ The main members of ML are ▶ SML (Standard ML) ▶ ocaml ▶ In Microsoftese , ocaml is called F#… Mayer Goldberg \ Ben-Gurion University

  58. What kind of language is ocaml Ocaml — rich toolset Compiler Construction 79 / 193 ▶ is used all over the world ▶ is used in commercial and open source projects ▶ is powerful, effjcient, convenient, modern, elegant, and has a ▶ supports both functional and object-oriented programming ▶ The ocaml object system is very powerful! ▶ makes it very diffjcult to have run-time errors! Mayer Goldberg \ Ben-Gurion University

  59. The advantages of learning ocaml system programming, etc Compiler Construction 80 / 193 ▶ Very rich language ▶ Great support for abstractions of various kinds ▶ Great library support: dbms, networking, web programming, ▶ Compiles effjciently, either to bytecode or native Mayer Goldberg \ Ben-Gurion University

  60. Why we are using ocaml in the course programming sophisticated, abstract, clean, easy, elegant, re-usable, safe Compiler Construction 81 / 193 ▶ Pattern-matching, modules, object-orientation, and types make ▶ Easy to enforce an API Mayer Goldberg \ Ben-Gurion University

  61. Getting, installing & using ocaml Compiler Construction 82 / 193 ▶ https://ocaml.org/ , or Mayer Goldberg \ Ben-Gurion University

  62. Getting, installing & using ocaml ( https://www.gnu.org/software/emacs/ ) which is the best text editor. Period. in the line: #use "topfind";; This will load some basic tools you will want to use. Compiler Construction 83 / 193 ▶ I run ocaml under GNU Emacs ▶ Create the fjle .ocamlinit in your home directory, and place it Mayer Goldberg \ Ben-Gurion University

  63. Getting, installing & using ocaml Compiler Construction 84 / 193 Mayer Goldberg \ Ben-Gurion University

  64. Getting, installing & using ocaml http://www.algo-prog.info/ocaide/ , or Compiler Construction 85 / 193 ▶ You are free to use ocaml under any editor/environment you like ▶ For example, for ocaml under Eclipse, try OcaIDE at Mayer Goldberg \ Ben-Gurion University

  65. Further Reading Whitington Yaron Minsky & Anil Madhavapeddy http://caml.inria.fr/pub/docs/manual-ocaml/ , or Compiler Construction 86 / 193 🕯 OCaml from the Very Beginning, by John Whitington 🕯 More OCaml: Algorithms, Methods & Diversions, by John 🕯 Real World OCaml: Functional programming for the masses, by 🕯 Practical OCaml, by Joshua B. Smith 🔘 The online manual at Mayer Goldberg \ Ben-Gurion University

  66. Expressions & types Compiler Construction 87 / 193 ▶ Ocaml is a functional programming language ▶ Ocaml is interactive ▶ You enter expressions at the prompt, and get their values and their types ▶ Expressions are ended with ;; Mayer Goldberg \ Ben-Gurion University

  67. Expressions & types - : float = 3.1415 Compiler Construction - : int list list = [[1]; [2; 3]; [4; 5; 6]] # [[1]; [2; 3]; [4; 5; 6]];; - : int list = [1; 2; 3; 5; 8; 13] # [1; 2; 3; 5; 8; 13];; # 3.1415;; An interaction at the ocaml prompt: - : char = 'm' # 'm';; - : string = "asdf" # "asdf";; - : int = 3 # 3;; 88 / 193 Mayer Goldberg \ Ben-Gurion University

  68. What’s available?? Modules system variables, while controlling their visibility modules. Compiler Construction 89 / 193 ▶ We shall learn about modules later on, as part of the module ▶ In the meantime,modules are ways of aggregating functions & ▶ Functionality in ocaml is managed via loading and using Mayer Goldberg \ Ben-Gurion University

  69. What’s available?? Directives Some useful directives Compiler Construction 90 / 193 ▶ Commands that start with # ▶ Non-programmable ▶ Tell you about the run-time system ▶ Change the run-time system ▶ #list;; to list available modules ▶ #cd <string>;; to change to a directory ▶ #require <string>;; to specify that a module is required ▶ #show_module <module>;; to see the signature of the module ▶ #trace <function>;; to trace a function ▶ #untrace <function>;; to untrace a function Mayer Goldberg \ Ben-Gurion University

  70. What’s available?? What directives are available? Hashtbl.iter (fun k _v -> print_endline k) Toploop.directive_table;; This is nasty! We can defjne a function to do that: let directives () = Hashtbl.iter (fun k _v -> print_endline k) Toploop.directive_table;; and now we can just run directives();; to see the list of directives. Compiler Construction 91 / 193 Mayer Goldberg \ Ben-Gurion University

  71. What’s available? Pervasives Compiler Construction ... exception Exit val failwith : string -> 'a val invalid_arg : string -> 'a exn -> 'a = "%raise_notrace" external raise_notrace : external raise : exn -> 'a = "%raise" sig module Pervasives : get! ocaml. 92 / 193 ▶ The module Pervasives contains all the “builtin” procedures in ▶ Try executing #show_module Pervasives;; and see what you Mayer Goldberg \ Ben-Gurion University

  72. Integers in ocaml # 1 + 2;; - : int = 3 # 3 * 4;; - : int = 12 # 8 / 3;; - : int = 2 # 8 mod 3;; - : int = 2 Compiler Construction 93 / 193 Mayer Goldberg \ Ben-Gurion University

  73. Read your error messages! # 1.2 + 3.4;; Characters 0-3: 1.2 + 3.4;; ^^^ Error: This expression has type float but an expression was expected of type int # cos(3);; Characters 3-6: cos(3);; ^^^ Error: This expression has type int but an expression was expected of type float Compiler Construction 94 / 193 Mayer Goldberg \ Ben-Gurion University

  74. Floating-point numbers in ocaml Operators take a . after them to denote fmoating-point ops: # 3.4 +. 4.5;; - : float = 7.9 # 3.2 *. 5.1;; - : float = 16.32 # cos(2.0);; - : float = -0.416146836547142407 Compiler Construction 95 / 193 Mayer Goldberg \ Ben-Gurion University

  75. Booelans in ocaml # true;; Compiler Construction - : bool = true # 3 != 4;; - : bool = false # 3 = 4;; - : bool = true # 3 = 3;; - : bool = false # false || false;; - : bool = false # true && false;; - : bool = false # false;; - : bool = true 96 / 193 Mayer Goldberg \ Ben-Gurion University

  76. Read your error messages! In ocaml, unlike in Scheme, the then -clause and else -clause of if -expressions must be of the same type! # if 3 = 3 then 123 else 456;; - : int = 123 # if 3 = 4 then "moshe" else "yosi";; - : string = "yosi" # if 4 = 4 then 123 else "moshe";; Characters 23-30: if 4 = 4 then 123 else "moshe";; ^^^^^^^ Error: This expression has type string but an expression was expected of type int Compiler Construction 97 / 193 Mayer Goldberg \ Ben-Gurion University

  77. Bitwise Boolean functions over the integers # 5 land 3;; - : int = 1 # 8 lor 3;; - : int = 11 # 5 lxor 3;; - : int = 6 Compiler Construction 98 / 193 Mayer Goldberg \ Ben-Gurion University

  78. Characters in ocaml # '*';; - : char = '*' # '\t';; - : char = '\t' # '\n';; - : char = '\n' # '\\';; - : char = '\\' # '\"';; - : char = '"' # '\065';; - : char = 'A' Compiler Construction 99 / 193 Mayer Goldberg \ Ben-Gurion University

  79. Strings in ocaml # "moshe!";; - : string = "moshe!" # "moshe\n";; - : string = "moshe\n" # "hello" ^ " " ^ "world";; - : string = "hello world" # "moshe".[3];; - : char = 'h' #show_module String;; will show you what string functions are available Compiler Construction 100 / 193 Mayer Goldberg \ Ben-Gurion University

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend