Скачать презентацию Cse 322 Programming Languages and Compilers Lecture 1 Скачать презентацию Cse 322 Programming Languages and Compilers Lecture 1

5a2228327c152ad9e8449278f473ed39.ppt

  • Количество слайдов: 34

Cse 322, Programming Languages and Compilers Lecture #1, April 3, 2007 • Course Mechanics Cse 322, Programming Languages and Compilers Lecture #1, April 3, 2007 • Course Mechanics • Text Book • Down-loading SML • Themes • Back-end tasks • Language design issues • I. R. • Machine Code • Optimization • Interpretation • This weeks assignment 3/17/2018 1

Cse 322, Programming Languages and Compilers Acknowledgements The material taught in this course was Cse 322, Programming Languages and Compilers Acknowledgements The material taught in this course was made possible by many people. Here is a partial list: • Andrew Tolmach • Nathan Linger • Harry Porter • Jinke Lee 3/17/2018 2

Cse 322, Programming Languages and Compilers Class Web Page • The CS 322 class Cse 322, Programming Languages and Compilers Class Web Page • The CS 322 class web page can be found at: – www. cs. pdx. edu/~sheard/course/Cs 322 • Contents of the page – – – 3/17/2018 Link to the department Course Syllabus Link to the ML home page Copies of the Power. Point slides used in lectures Copies of the assignments Project Description Copies of the SML code illustrated in the lectures 3

Cse 322, Programming Languages and Compilers Today’s Assignments Reading • Engineering a Compiler – Cse 322, Programming Languages and Compilers Today’s Assignments Reading • Engineering a Compiler – – Available In the PSU bookstore In CS 321 we covered chapters 1 -6 (You might want to review this material) Chapter 7. 1 – 7. 3, pp 207 -222 There will be a 5 minute quiz on the reading Wednesday. Search • Find the class webpage 1 page programming Assignment • Due Wednesday, April 5, 2005 • Login to some SML system. See how the system operates. Type in solutions (in a file) to some problems. Get them running, and print them out then turn them in on Wednesday. What matters here is that you try out the SML system, not that you get them perfect. 3/17/2018 4

Cse 322, Programming Languages and Compilers Course Information • CS 322 - Languages and Cse 322, Programming Languages and Compilers Course Information • CS 322 - Languages and Compiler Design – – – Time: Tuesday & Thursday 16: 00 -17: 50 pm Place: PCAT 28 Instructor: Tim Sheard office: room 115, CS Dept, 4 th Ave Building, Portland State Univ. phone: 503 -725 -2410 (work) 503 -649 -7242 (home) office hours: Before class in my office, or by Appt. • Assignments – Reading from text and handouts (quizzes on reading) – Daily, 1 page programming assignments – 3 part programming project • Grading: – – 3/17/2018 midterm exam (25%). Tuesday, May 1 st, 2007 3 parts of project (30%) Daily 1 page assignments and quizzes (15%) Final exam (30 %) 5

Cse 322, Programming Languages and Compilers Text Book • Text: Engineering a Compiler – Cse 322, Programming Languages and Compilers Text Book • Text: Engineering a Compiler – Keith D. Cooper, and Linda Torczon • Other Reference Materials – Auxilliary Material » Notes by Riccardo Pucella, about programming in ML • http: //www. cs. cornell. edu/riccardo/smlnj. html » Elements of Functional Programming (SML book) by Chris Reade, Addison Wesley, ISBN 0 -201 -12915 -9 » Using the SML/NJ System http: //www. cs. cmu. edu/~petel/smlguide/smlnj. htm • Class Handouts – In each class, a copy of that day’s slides will be available as a handout. – I will post files that contain the example programs used in each lecture on the class web page www. cs. pdx. edu/~sheard/course/Cs 322 – I will post Assignments there as well. 3/17/2018 6

Cse 322, Programming Languages and Compilers Copurse Schedule • Guest Lecturer. – There will Cse 322, Programming Languages and Compilers Copurse Schedule • Guest Lecturer. – There will be a guest Lecturer on Thursday May 24, as I will be out of town. 3/17/2018 7

Cse 322, Programming Languages and Compilers Academic Integrity Students are expected to be honest Cse 322, Programming Languages and Compilers Academic Integrity Students are expected to be honest in their academic dealings. Dishonesty is dealt with severely. • Homework. Pass in only your own work. • Program assignments. Program independently. • Examinations. Notes and such, only as each instructor allows. OK to discuss how to solve problems with other students, but each student should write up, debug, and turn in his own solution. 3/17/2018 8

Cse 322, Programming Languages and Compilers Standard ML • In this course we will Cse 322, Programming Languages and Compilers Standard ML • In this course we will use an implementation of the language Standard ML • The SML/NJ Homepage has lots of useful information: http: //www. smlnj. org// • You can get a version to install on your own machine there. I will use the version 110. 57 of SML. Earlier versions probably will work as well. I don’t foresee any problems with other versions, but if you want to use the identical version that I use in class then this is the one. 3/17/2018 9

Cse 322, Programming Languages and Compilers Course Thesis • This course is about programming Cse 322, Programming Languages and Compilers Course Thesis • This course is about programming languages. We study languages in two ways. – From the perspective of the user – From the perspective of the implementer (compiler writer) • We will learn about some languages you may never have heard of. We will learn to program in one of them (Standard ML). Its good to learn a new language in depth. • This course is also about programming. There will be extensive programming assignments in SML. If you don’t do them - you won’t learn – You’re deluding yourself if you think you can learn the material without doing the exercises! • We will write a comiler for a Java subset. Its good to understand the implementation details of a language you already know. 3/17/2018 10

Cse 322, Programming Languages and Compilers Course topics • • • Semantics of programming Cse 322, Programming Languages and Compilers Course topics • • • Semantics of programming languages Interpreters Run-time organizations and systems Intermediate code generation Backend analysis Transformations and optimizations for a number of different kinds of languages • Machine code generation 3/17/2018 11

Cse 322, Programming Languages and Compilers Project • Write a mini. Java interpreter • Cse 322, Programming Languages and Compilers Project • Write a mini. Java interpreter • Build a complete Mini. Java Compiler 3/17/2018 12

Cse 322, Programming Languages and Compilers Themes • • Mapping from High-level to Low-level Cse 322, Programming Languages and Compilers Themes • • Mapping from High-level to Low-level Implementing resource management Integration with OS and hardware environment Syntax directed techniques 3/17/2018 13

Cse 322, Programming Languages and Compilers Compiler Backend Tasks Type-checked Abstract Syntax for Source Cse 322, Programming Languages and Compilers Compiler Backend Tasks Type-checked Abstract Syntax for Source Language • Simplify expressions and statement into flat goto/label form • Fix location of variables and temporaies in memory and registers • Generate machine instructions • Manage machine resources • Interact with O/S, runtime systems Or, build interpreter for H. L. L. features Machine code for specific target machine 3/17/2018 14

Cse 322, Programming Languages and Compilers Language Design Issues Source Code Lexical Analysis Syntax Cse 322, Programming Languages and Compilers Language Design Issues Source Code Lexical Analysis Syntax Tokens Parsing Data Types Abstract Syntax Type-Checking Cs 321 Cs 322 Interpreter Intermediate code generation Control Structures Intermediate Code Optimization Machine independent Machine-dependent Machine code generation Machine Code 3/17/2018 15

Cse 322, Programming Languages and Compilers Translation to Abstract Syntax (CS 321) z = Cse 322, Programming Languages and Compilers Translation to Abstract Syntax (CS 321) z = x + pi * 12. 0 text: tokens: id(z) eql id(x) plus id(pi) times float(12. . 0) syntax tree: 3/17/2018 16

Cse 322, Programming Languages and Compilers Intermediate Code Generation • Emit I. C. (or Cse 322, Programming Languages and Compilers Intermediate Code Generation • Emit I. C. (or “I. R. ”) from abstract syntax or directly from parser • Advantages: • Keeps more of compiler machine-independent • Facilitates some optimizations • Typical examples: • Postfix • Trees or DAGs • Three-address code (quadruples, triples, etc. ) • Abstracts key features of machine architectures • E. g. , sequential execution, explicit jumps • But hides details • E. g. , # of registers, style of conditionals, etc. • Many possible levels 3/17/2018 17

Cse 322, Programming Languages and Compilers Three-address-code – a typical linear I. R. • Cse 322, Programming Languages and Compilers Three-address-code – a typical linear I. R. • • Generate list of “instructions” Each has an operator, up to 2 args, and up to 1 result Instructions can be labeled Operands are names for locations in some abstract memory e. g. , symbol table entries) • Examples of instructions: A : = B A = B op C A = op B goto L if A relop B goto L param A call P, N return N A[I] 3/17/2018 copy binary ops unary ops jumps conditional jumps procedure call setup procedure call procedure return array dereference 18

Cse 322, Programming Languages and Compilers 3 -Address Code Example : = a if Cse 322, Programming Languages and Compilers 3 -Address Code Example : = a if and > c 7 * = b 3 e • Linearized • Nested conditionals expanded (badly) • Temporaries for all intermediate results 3/17/2018 * + d e c + d d 2 if c > 7 goto L 1 goto L 2 L 1: if b = e goto L 3 L 2: t 1 : = d + 2 t 2 : = -c t 3 : = t 1 * t 2 goto L 4 L 3: t 1 : = d + e t 3 : = 3 * t 1 L 4: a : = t 3 19

Cse 322, Programming Languages and Compilers Machine-code Generation • “Read” I. R. ; generate Cse 322, Programming Languages and Compilers Machine-code Generation • “Read” I. R. ; generate assembly language (symbol or binary). • Must cooperate with I. R. to define and “enforce” runtime environment. • Must deal with idiosyncrasies of target machine, • e. g. , instruction selection • Must perform resource management, • e. g. , register assignment. • Lots of case analysis, especially for complex target architectures. • It can be done by hand, but it is hard. • Tools limited, but sometimes useful – mainly based on pattern matching 3/17/2018 20

Cse 322, Programming Languages and Compilers Sample machine code • Assumes – a global; Cse 322, Programming Languages and Compilers Sample machine code • Assumes – a global; – b, c args; – d, e locals. • Illustrates register conventions, delay slots, etc. sethi cmp ble or cmp bne sub add 3/17/2018 %hi(_a), %o 2 %i 1, 7 L 2 %o 2, %lo(_a), %l 1 %i 0, %l 2 L 4 %g 0, %i 1, %o 0 %l 0, %i 0, %o 1 sll add b st L 2: sub L 4: call add st L 3: %o 1, 1, %o 0, %o 1, %o 0 L 3 %o 0, [%o 2+%lo(_a)] %g 0, %i 1, %o 0. umul, 0 %l 0, 2, %o 1 %o 0, [%l 1] 21

Cse 322, Programming Languages and Compilers Optimization • Improve (don't perfect) code by removing Cse 322, Programming Languages and Compilers Optimization • Improve (don't perfect) code by removing inefficiencies: • In original program • Introduced by compiler itself • Can operate on source, I. R. , or object code. • Local Improvements • Example: changing if c > 7 goto L 1 goto L 2 L 1: . . . L 2: . . . • to if c <= 7 goto L 2 L 1: . . . L 2: … 3/17/2018 22

Cse 322, Programming Languages and Compilers Optimization (continued) “Global” Improvements • Example: changing for Cse 322, Programming Languages and Compilers Optimization (continued) “Global” Improvements • Example: changing for (i : = 0; i < 1000; i++) a[i] : = b*c + i; • to t 1 : = b * c; for (i = 0; i < 1000; i++) a[i] = t 1 + i; • Inter-procedural improvements • Example: In-lining a function • Most of a modern compiler is devoted to optimization. 3/17/2018 23

Cse 322, Programming Languages and Compilers Interpretation • Simulate execution of program (source, AST, Cse 322, Programming Languages and Compilers Interpretation • Simulate execution of program (source, AST, or other IR) on an abstract machine. • Implement abstract machine on a real machine. • Inputs to interpreter are • Program to be interpreted • Input to that program • Simpler than compiling and takes no time up front, but interpreted code runs (~10 X) more slowly than compiled code. • Much more portable than real machine code (as for Java). • Helps with semantic definition. 3/17/2018 24

Cse 322, Programming Languages and Compilers Using ML • We will use ML to Cse 322, Programming Languages and Compilers Using ML • We will use ML to program the projects. – ML page to download the compiler http: //www. smlnj. org// – A set of notes from CS 321. Available from cs 321 website – Notes by Riccardo Pucella, about programming in ML » http: //www. cs. cornell. edu/riccardo/smlnj. html – Elements of Functional Programming (SML book) by Chris Reade, Addison Wesley, ISBN 0 -201 -12915 -9 – Using the SML/NJ System http: //www. cs. cmu. edu/~petel/smlguide/smlnj. htm 3/17/2018 25

Cse 322, Programming Languages and Compilers ML modules • Code in ML is broken Cse 322, Programming Languages and Compilers ML modules • Code in ML is broken into modules. – Another name for a module is a library • Each module contains a set of type declarations and a set of functions that operate on those types. • Modules are separately compiled. • Modules can depend on other modules. • Modules are tied together by the compile-manager 3/17/2018 26

Cse 322, Programming Languages and Compilers The Compile-manager • The compile manager is a Cse 322, Programming Languages and Compilers The Compile-manager • The compile manager is a “make” like facility for SML. • It has some documentation found here. – http: //www. smlnj. org/doc/CM/index. html • Peter Lee’s notes also contains a brief (but of date) introduction – http: //www. cs. cmu. edu/~petel/smlguide/smlnj. htm • The basic approach is that a single file contains a list of all the pieces that comprise a project. – A complete scan of all the pieces determines a dependency graph of which pieces depend on which other pieces. – It determines the time-stamp of each piece – It recompiles all the pieces that are out of date – It links everything together 3/17/2018 27

Cse 322, Programming Languages and Compilers Using the Compile manager • The compile manager Cse 322, Programming Languages and Compilers Using the Compile manager • The compile manager compiles whole compilation units called structures or libraries. • Break program into files where each file contains 1 module or library. • If a file needs stuff from another module or library, open that library inside of the file. • Create a compile manager source file that lists all the libraries. 3/17/2018 28

Cse 322, Programming Languages and Compilers Example. cm group is Program. Types. sml Printer. Cse 322, Programming Languages and Compilers Example. cm group is Program. Types. sml Printer. sml Program. Types. sml structure Program. Types = struct type Id = string; $/basis. cm datatype Basic = Bool | Int | Real; datatype Type = Basic. Type of Basic | Array. Type of Basic | Obj. Type of Id | Void. Type; Printer. sml structure Printer = struct open Program. Types; fun show. B Int = "int" | show. B Real = "real" | show. B Bool = "boolean"; 3/17/2018 29

Cse 322, Programming Languages and Compilers Libraries are bracketed by “structure name = struct” Cse 322, Programming Languages and Compilers Libraries are bracketed by “structure name = struct” and “end” Name of the library, can be different from the name of the file. File: Program. Types. sml structure Program. Types = struct type Id = string; datatype Basic = Bool | Int | Real; datatype Type = Basic. Type of Basic | Array. Type of Basic | Obj. Type of Id | Void. Type; 3/17/2018 end; 30

Cse 322, Programming Languages and Compilers Opens libraries with needed components File: Printer. sml Cse 322, Programming Languages and Compilers Opens libraries with needed components File: Printer. sml structure Printer = struct open Program. Types; fun show. B Int = "int" | show. B Real = "real" | show. B Bool = "boolean"; 3/17/2018 31

Cse 322, Programming Languages and Compilers The sources file The name of the file Cse 322, Programming Languages and Compilers The sources file The name of the file where this list resides File: Example. cm group is Program. Types. sml Printer. sml All the user defined pieces $/basis. cm System libraries 3/17/2018 32

Cse 322, Programming Languages and Compilers Putting it all together. - CM. make Cse 322, Programming Languages and Compilers Putting it all together. - CM. make "Example. cm"; [scanning Example. cm] [parsing (Example. cm): Program. Types. sml] [parsing (Example. cm): Printer. sml] [compiling (Example. cm): Program. Types. sml] [code: 76, env: 3383 bytes] [compiling (Example. cm): Printer. sml] [code: 11030, data: 446, env: 2700 bytes] [New bindings added. ] val it = true : bool It compiles the “*. sml” files I open the Printer Library to get at the function show. Stmt - open Printer; opening Printer val show. B : Basic -> string val show. Constant : Constant -> string val show. Exp : Exp -> string 3/17/2018 33

Cse 322, Programming Languages and Compilers Assignment #1 CS 322 Prog Lang & Compilers Cse 322, Programming Languages and Compilers Assignment #1 CS 322 Prog Lang & Compilers Assignment # 1 Assigned: April 3, 206 April 5, 2006 • • • Due: Wed. Read Chapters 7. 1 - 7. 3 of the text book Find (and install if necessary) a version of SML Create a files with some ML program Load these into SML. Test them out by typing some examples. Cut and paste your session, print it out, and hand it in during class on Wednesday. Don’t forget your name. 3/17/2018 34