240 likes | 389 Views
COS 441 Exam Stuff. David Walker. Logistics. take-home exam will become available on the course web site Jan 15-18 write down when you download & when you turn in email to kenny or deliver to his office by hand you have 24 hours to complete the exam
E N D
COS 441 Exam Stuff David Walker
Logistics • take-home exam will become available on the course web site Jan 15-18 • write down when you download & when you turn in • email to kenny or deliver to his office by hand • you have 24 hours to complete the exam • content: anything from class, assignments, or assigned textbook readings TAL
Content: Pre-midterm • Judgments, inductive definitions, proofs by induction (Chapter 3) • Intuitionistic logic: formulas, proofs, proof checking & the Curry-Howard isomorphism • Untyped lambda calculus, operational semantics, properties, encodings (Chapter 5) • Typed lambda calculus: syntax, operational semantics, typing rules, properties including type safety, progress, preservation, canonical forms, substitution, inversion principles, etc. (Chapter 8,9,11) • Typed datastructures: tuples, sums (Chapter 11) • Implementation of programming language concepts (syntax, substitution, operational semantics, type checking) TAL
Content: Post-midterm • recursive types (Chap 20.1, 20.2) • effectful computations: references, exceptions, semantics using evaluation contexts (Chap 13,14; evaluation contexts note above) • quantified types: universal polymorphism, existential types, type inference (Chap 22.1-22.6, 23.1-23.5, 24) • subtyping: subtyping relations, co-, contra-, and in-variance, subsumption rule, proving soundness of declarative system, showing subtyping rules are “bad”, don’t worry about relating declarative and algorithmic subtyping formally (Chap 15.1-5, 16.1-3) • class-based, object-oriented languages: featherweight Java (Chap 19.1-19.5) • applications of operational semantics & type systems: stack inspection • stuff we cover today in lecture • implementation of any of the concepts above TAL
Typed Assembly Language David Walker Slides stolen from: Greg Morrisett
Types “Type systems for programming languages are a syntactic mechanism for enforcing abstraction.” J. Reynolds TAL
What is TAL? A type system for assembly language(s): • built-in abstractions (tuple,code) • operators to build new abstractions (,,l) • annotations on assembly code • an abstraction checker Thm: well-annotated code cannot violate abstractions. TAL
What We Did [popl 98, toplas 99 & others] Theory: • small RISC-style assembly language • compiler from System F to TAL • soundness and preservation theorems Practice: • most of IA32 (32-bit Intel x86) • more type constructors • everything you can think of and more • safe C compiler • ~40,000LOC & compiles itself TAL
Why Type Assembly? Theory: • simplifies proofs of compiler correctness • deeper understanding of compilation Practice: • compiler debugging • software-based protection TAL
Type-Based Protection (JVM) JVM verifier System Interface Low-Level IL Java Source “Kernel” javac Optimizer JVM bytecodes System Binary Binary TAL
JVM Pros & Cons Pros: • portable • hype: $, tools, libraries, books, training Cons: • trusted computing base includes JIT • requires many run-time tests • “down” casts, arrays, null pointers, etc. • only suitable for Java (too high-level) • no formal spec (when we started with TAL) TAL
Ideally: verifier System Interface Your favorite language Low-Level IL (SSA) System Binary “Kernel” optimizer machine code TAL
Rest of the Lecture: Examples • TAL core types: • bytes, tuples, code, • Control-Flow: • calling conventions, stacks, exns • I won’t get to: • closures, objects, modules, type analysis, ADTs TAL
Simple Built-In Types • Bytes: b1, b2, b4 • Tuples: (t1f1,…,tnfn) (f = 0,1) • Code: {r1:t1,…, rn:tn} • like a pre-condition • argument type of function • no return type because code doesn’t really return, just jumps somewhere else... • Polymorphic types: a.t, a.t TAL
Simple Loop sum: {ecx:b4, ebx:{eax:b4}}; int sum(int x) { mov eax,0 ; int a = 0; jmp test ; loop: {eax:b4, ecx:b4, ebx:{eax:b4}}; while(!x) { add eax,ecx ; a += x; dec ecx ; x--; FALLTHRU; } test: {eax:b4, ecx:b4, ebx:{eax:b4}}; cmp ecx,0 ; jne loop ; return(a); jmp ebx ; } TAL
Allocation: mkpair: {eax:b4, ebx:{eax:(b41, b41)}} mov ecx,eax MALLOC eax,8,(b4, b4); eax : (b40, b40) mov [eax+0],ecx ; eax : (b41, b40) mov [eax+4],ecx ; eax : (b41, b41) jmp ebx TAL
Callee-Saves Register addone: a.{eax:b4, ecx:a, ebx:{eax:b4, ecx:a}} inc eax ; x+1 jmp ebx ; return main: {ebx:{eax:b4}} mov eax,3 mov ecx,ebx ; save main’s return address mov ebx,done jmp addone[{eax:b4}] done: {eax:b4,ecx:{eax:b4}} inc eax jmp ecx TAL
In General: Need to save more stuff (e.g., locals): MALLOC ecx,4n,(t1,…,tn); frame for storage mov [ecx+0],r1 … ; save locals mov [ecx+4n-4],rn jmp addone[(t1,…,tn)] Heap-Allocated Activation Records TAL
Stacks Want to use stack for activation frames. Stack types: s ::= nil | tf::s | r | s1 @ s2 TAL
Typing Stack Operations { esp: s } { esp: t1f::t2f::…::tif::s } sub esp,i*4 add esp,i*4 { esp: b40::b40::…::b40::s } { esp : s } { r: t, esp: t1f::t2f::…::tif::s } { r: t, esp: s } mov [esp+i*4],r push r { r: t, esp: t1f::t2f::…::t1::s } { r: t, esp: t1::s } { esp: t1f::t2f::…::ti1::s } { esp: t1::s } mov r,[esp+i*4] pop r { r: ti, esp: t1f::t2f::…::ti1::s } { r: t, esp: s } TAL
Recursion thru Stack Variables fact: r.{eax:b4, esp:{eax:b4, esp:r}::r} cmp eax,1 jne L[r] retn L:r’.{eax:b4, esp:{eax:b4, esp:r’}::r’} push eax dec eax call fact[b4::{eax:b4, esp:r’}::r’] pop ecx imul eax,ecx retn TAL
Fact Fact fact: r.{eax:b4, esp:{eax:b4, esp:r}::r} Because ris abstract, fact cannot read or write this portion of the stack. Caller’s frame is protected from callee… TAL
Other TAL Features • Module system • interfaces, implementations, ADTs • Sum type/datatype support • Fancy arrays/vector typing • (Higher Order) Type constructors • Fault tolerance checking • Other people still writing papers about more ... TAL
Long Term? Low-level, portable, safe language: • OO-support of Java • typing support of ML • programmer control of C • good model of space • good model of running time • many optimizations expressible in the language Microsoft research working on a new compiler (Phoenix) to generate TAL TAL