Compiler Construction Compiler Construction 1 / 54 Mayer Goldberg \ Ben-Gurion University Tuesday 10 th December, 2019 Mayer Goldberg \ Ben-Gurion University
Chapter 5 Agenda tail-call-optimization Compiler Construction 2 / 54 ▶ Intuition about the tail-calls, tail-position, & the ▶ The tail-position, tail-call ▶ The TCO ▶ Loops & tail-recursion ▶ Annotating the tail-call ▶ What TCO code looks like ▶ Implementing the TCO Mayer Goldberg \ Ben-Gurion University
The tail-call ( intuitively ) A grade of 100 Compiler Construction A grade of 100 3 more wishes US$1,000,000 3 more wishes US$1,000,000 Second Person First Person 3 / 54 ▶ Two people are walking through a forest, and encounter a witch 🧚 ▶ The witch grants them three wishes… ▶ Here is what each person wished for: ▶ What is the difgerence? Mayer Goldberg \ Ben-Gurion University
The tail-call ( continued ) A grade of 100 Compiler Construction non-tail-call (Second Person) This is the difgerence between a tail-call (First Person) & a requests, in order… needs a stack of paper slips to manage the outstanding 4 / 54 3 more wishes US$1,000,000 A grade of 100 3 more wishes US$1,000,000 Second Person First Person ▶ Here is what each person wished for: ▶ The fjrst person’s wishes are simple to grant ▶ The second person’s wishes are annoying: ▶ The witch must remember what to do once she returned from granting the 3 new wishes… She still has work to do! ▶ Since this nonsense is going to go on for a while, the witch Mayer Goldberg \ Ben-Gurion University
The tail-call ( continued ) Back to reality: arguments & return addresses non-tail-calls tail-calls Compiler Construction 5 / 54 ▶ Non-tail-calls require additional stack frames to manage ▶ The stack depth (in frames) is proportional to the number of ▶ Tail-calls do not require additional stack frames ▶ The stack depth (in frames) is independent of the number of Mayer Goldberg \ Ben-Gurion University
The tail-call optimization ( intuitively ) additional frames to remove the frame Compiler Construction 6 / 54 ▶ You are surfjng a web browser ▶ The browser is broken: It has no ⟨ Back ⟩ key ▶ Once you click on a link, you are unable to return ▶ To read a web page, you therefore right-click on a link & select the option to open in a new frame ▶ You read the page in the new frame, possibly opening links in ▶ When you are done reading a page, you click on the ⊠ button Mayer Goldberg \ Ben-Gurion University
The tail-call optimization ( intuitively , cont ) Compiler Construction 7 / 54 Mayer Goldberg \ Ben-Gurion University
The tail-call optimization ( intuitively , cont ) Compiler Construction 8 / 54 ▶ Some web pages have special links: ▶ These links are not just the last links on the page ▶ These links are the last thing on the page ▶ There’s nothing to read past these links! ☞ Not all pages have such special links! This page does: Mayer Goldberg \ Ben-Gurion University
The tail-call optimization ( intuitively , cont ) link, there is yet something to read: Compiler Construction 9 / 54 ☞ This page does not have such a special link. The last link on the page is not the last thing on the page, so returning from that 🤕 Yeah? What’s there left to read??? ▶ Enjoy the logo; It is not a link! 😊 Mayer Goldberg \ Ben-Gurion University
The tail-call optimization ( intuitively , cont ) neither to open a new frame nor to return from it: replaces Compiler Construction 10 / 54 ▶ Because there’s nothing to read past these links, we need ▶ Rather than right-click & open the page in the new frame, we simply click on the link in place ▶ The new contents shall overwrite the old contents ▶ The new contents can be larger or smaller than the contents it ▶ The size of the frame can change ▶ The number of frames shall not change ▶ When we’re done reading the page, we close it with ⊠ Mayer Goldberg \ Ben-Gurion University
Compiler Construction The tail-call optimization ( intuitively , cont ) 11 / 54 a new frame a new frame a new frame The tail-calls Mayer Goldberg \ Ben-Gurion University
Chapter 5 Agenda tail-call-optimization Compiler Construction 12 / 54 🗹 Intuition about the tail-calls, tail-position, & the ▶ The tail-position, tail-call ▶ The TCO ▶ Loops & tail-recursion ▶ Annotating the tail-call ▶ What TCO code looks like ▶ Implementing the TCO Mayer Goldberg \ Ben-Gurion University
The tail-position where the last computation is performed raise/throw -statements also identify tail-positions! more than one tail-position return -statement or the ret instruction could be placed Compiler Construction 13 / 54 ▶ The tail-position is a point in the body of a function, procedure, method, subroutine, etc., just before the return-statement, ▶ Exception handling: Similarly to return -statements, ▶ Because computation may proceed non-linearly, there many be ▶ To fjnd the tail positions, fjnd all points in the code where a Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda (x) (f (g (g x)))) tail-call Compiler Construction 14 / 54 ▶ Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda (x) (f ( lambda (y) (g x y)))) tail-calls therefore, each lambda -expression has its own tail-position! Compiler Construction 15 / 54 ▶ ☞ Each lambda -expression has its own return -statement, and Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda (x y z w) ( if (foo? x) (goo y) (boo (doo z)))) tail-calls & else -expression are also in tail-position Compiler Construction 16 / 54 ▶ ☞ If an if -expression is in tail-position, then the then -expression Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda (x y z) (f ( if (g? x) (h y) (w z)))) tail-call then -expression & else -expression Compiler Construction 17 / 54 ▶ ☞ If an if -expression is not in tail-position, then neither are its Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda (a b) (f a) (g a b) (display "done!\n")) tail-call last expression in the sequence is also in tail-position Compiler Construction 18 / 54 ▶ ☞ If a sequence, whether explicit or implicit, is in tail-position, the Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda () ( and (f x) (g y) (h z))) also in tail-position expressions [within an and -expression], it is only from the last testing the value of the expression Compiler Construction 19 / 54 ▶ tail-call ☞ If an and -expression is in tail-position then its last expression is ▶ While it is possible to return after computing previous expression that return is possible immediately, without fjrst Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda () ( or (f (g x)) y)) then its last expression is in tail-position Compiler Construction 20 / 54 ▶ The above example contains no application in tail-position ☞ Similarly to and -expression, if an or -expression is in tail-position Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda () ( set ! x (f y))) Compiler Construction 21 / 54 ☞ The body of a set! -expression is never in tail-position! Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda () ( set ! x (f ( lambda (y) (g x y))))) tail-position, “every lambda -expression has its own return ” enclosing lambda -expression Compiler Construction 22 / 54 ▶ tail-call ☞ Even though the body of the set! -expression is not in ▶ The marked application is in tail-position relative to its Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda (x y z) ( cond ((f? x) (g y)) ((g? x) (f x) (f y)) ( else (h x) (f y) (g (f x))))) the implicit sequence of each cond -rib is also in tail-position Compiler Construction 23 / 54 ▶ tail-calls ☞ If a cond -expression is in tail-position then the last expression in Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( let ((x (f y)) (y (g x))) (goo (boo x) y)) tail-call lambda -expression. body is in tail-position Compiler Construction 24 / 54 ▶ ☞ The body of a let -expression is the body of a ▶ Therefore, the last expression in the implicit sequence of the Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: ( lambda (s) ( apply f s)) This is an interesting example: statically in the source code at run-time: The application of f must re-use the top activation frame! the tail-call-optimization, so that the frame for the call to f overwrites the frame for the call to apply ! Compiler Construction 25 / 54 ▶ On the one hand, there is only one tail-call that appears ▶ On the other hand, there is another tail-call that shall take place ☞ The implementation of apply duplicates part of the code for Mayer Goldberg \ Ben-Gurion University
The tail-position ( continued ) Example: let disj nt1 nt2 s = try (nt1 s) with X_no_match -> (nt2 s);; exception-handler! Compiler Construction 26 / 54 ▶ This applicatoin is not in tail-position 🤕 Upon returning successfully, the code needs to pop an ▶ This applicatoin is in tail-position Mayer Goldberg \ Ben-Gurion University
Recommend
More recommend