language definition vs implementation
play

Language Definition vs. Implementation Most of 251 so far - PowerPoint PPT Presentation

9/23/15 Language Definition vs. Implementation Most of 251 so far Now a brief interlude Ideally distinct, but definitely influence each other. Impossible/infeasible


  1. 9/23/15 Language ¡Definition ¡vs. ¡Implementation Most ¡of ¡251 ¡so ¡far Now ¡a ¡brief ¡interlude Ideally ¡ distinct, ¡but ¡definitely ¡influence ¡each ¡other. • Impossible/infeasible ¡language ¡features? Lisp/Racket ¡and ¡Implementation Some ¡languages ¡ are ¡ defined by ¡implementations: • Abstraction? • Can ¡be ¡complicated, ¡difficult ¡to ¡reason ¡about • But ¡high-­‑level ¡implementation ¡can ¡help ¡understand ¡definition. • May ¡over-­‑fit ¡to ¡current ¡system, ¡introduce ¡unintended ¡corner ¡cases Garbage ¡Collection • Tends ¡to ¡happen ¡early ¡in ¡language ¡development ¡or ¡when ¡the ¡goal ¡is ¡to ¡ Later: "just ¡hack ¡something ¡up" ¡instead ¡of ¡design ¡a ¡clean ¡abstraction. ... ¡Programs ¡as ¡Data Lisp: ... ¡Eval and ¡Interpreters • Formal ¡definitions ¡first. • Some ¡practicalities ¡of ¡implementation ¡crept ¡into ¡surface ¡of ¡language. • Some ¡"implementation ¡details" ¡should ¡have ¡been ¡in ¡definition. • Definition ¡forced ¡new ¡implementation ¡features ¡and ¡simplified ¡others. Lisp ¡Memory ¡Model IBM ¡704 Address Prefix Decrement Tag Cons ¡cell: car cdr IBM ¡704 ¡ register/ Atom: ¡ memory location/ Atom value word structure (cons 'A (cons 'B (cons 'C null))) Racket ¡syntax ¡(Lisp ¡uses ¡ symbol slightly ¡different ¡names, ¡ e.g. nil ¡for ¡null) Atom A null Atom B Atom C 3 1

  2. 9/23/15 How ¡do ¡we ¡remember ¡partial ¡ result ¡and “what ¡to ¡do ¡next”? (car (cons 'A (cons 'B (cons 'C null)))) (car (cons 'A (cons 'B (cons 'C null)))) Garbage: cells ¡that ¡will ¡never ¡be ¡used ¡again, ¡ subexpression result but ¡still ¡occupy ¡storage ¡space. subexpression result expression ¡result expression ¡result subexpression result Atom A Atom A null null Atom B Atom B Where ¡are ¡these ¡ Atom C Atom C data ¡stored? Simplified ¡Machine ¡Model Storage (240 ¡view) NOW fixed ¡# ¡of ¡fixed ¡size Heap Registers Code Data revised ¡later • Dynamically allocated ¡data... Stack • Racket: ¡cons ¡cells! Stack • Local variables. • … • Arguments, ¡return ¡values. • Racket: ¡lots ¡of ¡cons ¡cells! Program ¡ • Where ¡to ¡continue ¡evaluating ¡ Counter after ¡current function ¡call/ ¡ • … ¡more ¡later ¡for ¡first-­‑class ¡ expression. functions… Heap Environment ¡ • Not ¡the ¡full ¡story ¡for ¡first-­‑class ¡ Pointer functions… 7 2

  3. 9/23/15 Garbage ¡Collection ¡(GC) GC: ¡Reachability Every ¡ cell ¡requires ¡ a ¡block ¡of ¡the ¡available ¡ fixed-­‑size ¡ heap . Goal: ¡ Reclaim ¡storage ¡ used ¡for ¡ all garbage ¡ cells. A ¡cell ¡is ¡ garbage once ¡the ¡remainder ¡ of ¡evaluation ¡ will ¡never ¡ Reality? ¡ ¡ ¡ ¡ (let ([ garbage (list 1 2 3)]) access ¡it. (if e (length garbage) 0) Garbage ¡ collection: Achievable ¡ goal: ¡ Reclaim ¡ storage ¡used ¡for ¡all ¡ unreachable cells. Reclaim ¡ storage ¡used ¡for ¡garbage ¡ cells. • All ¡unreachable ¡cells ¡are ¡garbage. • When ¡storage ¡full ¡(or ¡sooner), ¡reuse ¡garbage-­‑filled ¡space ¡for new ¡cells. • Some ¡garbage ¡cells ¡are ¡reachable. Required/invented ¡to ¡implement ¡ Lisp. A ¡cell ¡is ¡ reachable if ¡it ¡is: • Lisp/Racket ¡programs ¡tend ¡to ¡create ¡new ¡cells ¡ very rapidly (even ¡vs. ¡Java) a ¡subexpression of ¡the ¡expression ¡currently ¡being ¡evaluated; ¡or • roots • No ¡mutation ¡=> ¡create ¡fresh ¡copies ¡instead ¡of ¡modifying bound ¡in ¡the ¡current ¡environment; ¡or • • Cells ¡become ¡garbage ¡almost ¡as ¡rapidly ¡as ¡they ¡are ¡created. recursive bound ¡in ¡the ¡environment ¡of ¡any ¡reachable ¡closure; ¡or • heap • Can ¡fill ¡up ¡memory ¡rapidly ¡-­‑-­‑ much ¡of ¡it ¡is ¡garbage. the ¡referent ¡of ¡the ¡ car or ¡ cdr of ¡any ¡reachable ¡cons ¡cell. • cases Mark-­‑Sweep Heap Roots (car (cons 'A (cons 'B (cons 'C null)))) D Unreachable ¡cells A C expression ¡result Atom A B E null Atom B ... Atom C 13 3

  4. 9/23/15 Lisp ¡Memory ¡Model Mark-­‑Sweep: ¡Clear Address Prefix Decrement Tag Heap Cons ¡cell: IBM ¡704 ¡ car cdr Roots register/memory ¡ Atom: ¡ location/word Atom value structure 0 D 0 (cons 'A (cons 'B (cons 'C null))) symbol Racket ¡ syntax ¡ (Lisp ¡ uses ¡ slightly ¡ A 0 0 C different ¡names, ¡ e.g. nil ¡ for ¡null) 0 Atom A 0 0 B E null Atom B ... 0 Atom C 15 Mark-­‑Sweep: ¡Mark Mark-­‑Sweep: ¡Mark Heap Heap Roots Roots 1 D 0 1 D 0 A A 0 0 1 0 C C 0 0 0 0 0 0 B E B E ... 0 ... 0 16 17 4

  5. 9/23/15 Mark-­‑Sweep: ¡Mark Mark-­‑Sweep: ¡Mark Heap Heap Roots Roots 1 D 0 1 D 0 A A 1 1 1 1 C C 0 0 0 0 1 0 B E B E ... 0 ... 0 18 19 Mark-­‑Sweep: ¡Mark Mark-­‑Sweep: ¡Sweep Heap Heap Roots Roots 1 D 0 1 A A 1 1 1 1 C C 0 1 0 1 B E B ... 1 ... 1 20 21 5

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