DOC PREVIEW
Princeton COS 320 - Lecture

This preview shows page 1-2-3-24-25-26-27-48-49-50 out of 50 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 320: Compiling TechniquesPeopleInformationBooksAssignment 0onward!What is a compiler?Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Compilers are complexCourse projectStandard MLIntroduction to MLIntro to MLSlide 19PreliminariesSlide 21Slide 22Slide 23Slide 24Basic ValuesSlide 26Using SML/NJLarger ProjectsCompilation ManagerWhat is next?An interpreterA little language (LL)LL abstract syntax in MLSlide 34Slide 35Function declarationsWhat is the type of the parameter t? Of the function?Slide 38A type errorSlide 40A very subtle errorSlide 42ExceptionsSlide 44EvaluatorSlide 46Slide 47Finishing the EvaluatorSlide 49Slide 50CS 320: Compiling TechniquesDavid WalkerPeopleDavid Walker (Professor)412 Computer Science Building[email protected]office hours: after each classDan Dantas (TA)417 Computer Science Building[email protected] office hours: Mondays 2-3 PMInformationWeb site:www.cs.princeton.edu/courses/archive/spring04/cos320/index.htmMailing list:BooksModern Compiler Implementation in MLAndrew AppelrequiredElements of ML ProgrammingJeffrey D. Ullmanalso: online references; see Web siteAssignment 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 12onward!What is a compiler?A compiler is program that translates a source language into an equivalent target languageWhat is a compiler?while (i > 3) { a[i] = b[i]; i ++}mov eax, ebxadd eax, 1cmp eax, 3jcc eax, edxC programassemblyprogramcompiler does thisWhat is a compiler?class foo { int bar; ...}struct foo { int bar; ...}Java programcompiler does thisC programWhat is a compiler?class foo { int bar; ...}.........................Java programcompiler does thisJava virtual machine programWhat is a compiler?\newcommand{....}\sfd\sf\fadgLatex programcompiler does thisTex programWhat is a compiler?\newcommand{....}\sfd\sf\fadgTex programcompiler does thisPostscript programWhat is a compiler?Other places:Web scripts are compiled into HTMLassembly language is compiled into machine languagehardware description language is compiled into a hardware circuit...Compilers are complextext file to abstract syntaxlexing; parsingabstract syntax to intermediate form (IR)analysis; optimizations; data layoutIR to machine codecode generation; register allocationfront-endmiddle-endback-endCourse 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 coloringfront-endmiddle-endback-endStandard 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 detectionIntroduction 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 softwareIntro to MLHighlightsData Structures for compilersData type definitionsPattern matchingStrongly-typed languageEvery expression has a typeCertain errors cannot occurPolymorphic types provide flexibilityFlexible Module SystemAbstract TypesHigher-order modules (functors)Intro to MLInteractive LanguageType in expressionsEvaluate and print type and resultCompiler as wellHigh-level programming featuresData typesPattern matchingExceptionsMutable data discouragedPreliminariesRead – Eval – Print – Loop- 3 + 2;PreliminariesRead – Eval – Print – Loop- 3 + 2;> 5: intPreliminariesRead – Eval – Print – Loop- 3 + 2;> 5: int- it + 7;> 12 : intPreliminaries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 + truePreliminariesRead – Eval – Print – Loop - 3 div 0;Failure : Div - run-time errorBasic 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 : bool and also, or else short-circuit evalBasic ValuesIntegers- 3 + 2> 5 : int- 3 + (if not true then 5 else 7);> 10 : int No division between expressionsand statementsStrings- “Dave” ^ “ “ ^ “Walker”;> “Dave Walker” : string- print “foo\n”;foo> 3 : intReals- 3.14;> 3.14 : realUsing 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 declarationswith their typesLarger 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 toolsCompilation 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 directorysources.cmc.smlb.smla.sigGroup isa.sigb.smlc.smlWhat 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 programsAn interpreterInterpreters are usually implemented as a series of transformers:stream ofcharactersabstractsyntaxlexing/parsingevaluateabstractvalueprintstream ofcharactersA little language (LL) An arithmetic expression e isa boolean valuean if statement (if e1 then e2 else e3)an integeran add operationa test for zero (isZero e)LL abstract syntax in MLdatatype term = Bool of bool| If of term * term * term| Num of int| Add


View Full Document

Princeton COS 320 - Lecture

Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?