1 / 10

Code for function calls (corresponds to ic12 code generator)

Code for function calls (corresponds to ic12 code generator). Lecture 29. Compilation of two vars, initialized. (top-comp (parse-string "let var x:=100 var y:=x in print(x+y) end") ) 0: pushenv 2 // (x y) 4: pushi 100

tuvya
Download Presentation

Code for function calls (corresponds to ic12 code generator)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Code for function calls(corresponds to ic12 code generator) Lecture 29 Prof. Fateman CS 164 Lecture 29

  2. Compilation of two vars, initialized (top-comp (parse-string "let var x:=100 var y:=x in print(x+y) end") ) 0: pushenv 2 // (x y) 4: pushi 100 8: lset 2 0 // x 12: pop 16: lvar 2 0 // x 20: lset 2 1 // y 24: pop 28: save L0960 32: lvar 2 0 // x 36: lvar 2 1 // y 40: + 44: lvar 0 13 // print 48: callj 1 L0960: 52: popenv Prof. Fateman CS 164 Lecture 29

  3. Compilation of two vars, transformed into 2 lets (top-comp (parse-string "let var x:=100 var y:=x in print(x+y) end") ) 0: args 0 4: pushenv 1 // (x) 8: pushi 100 12: lset 2 0 // x 16: pop 20: pushenv 1 // (y) ;; lets were nested 24: lvar 2 0 // x 28: lset 3 0 // y 32: pop 36: save L0959 40: lvar 2 0 // x 44: lvar 3 0 // y 48: + 52: lvar 0 13 // print 56: callj 1 L0959: 60: popenv 64: popenv 68: exit 0 Prof. Fateman CS 164 Lecture 29

  4. Run some of this… (defun doit(x)(run(tla (parse-string x)))) (defun run (exp) ;; from assembler.cl "compile and run tiger code" (machine (assemble (top-comp1 exp)))) (doit "let var x:=100 var y:=x in print(x+y) end") 200 Result of print exit code 0 Message from “machine” 200 Return value back to lisp, top of stack Note that this program doesn’t really pass typechecking.. Since print takes strings. Our virtual machine is able to handle this though.  Prof. Fateman CS 164 Lecture 29

  5. An actual legal program (doit "let var x:=\"ab\" var y:=\"cd\" in print(concat(x,y)) end") abcd exit code 0 void Prof. Fateman CS 164 Lecture 29

  6. Compilation of a function def / call (top-comp (parse-string "let function f(x:int):int=3 in f(4) end")) 0: args 0 4: pushenv 1 // (f) 8: fn 0: args 1 4: pushi 3 8: return 12: lset 2 0 // f 16: pop 20: save L0956 24: pushi 4 28: lvar 2 0 // f 32: callj 1 L0956: 36: popenv 40: exit 0 Prof. Fateman CS 164 Lecture 29

  7. Running a def / call (doit "let function f(x:int):int=3 in f(4) end") exit code 0 3 Prof. Fateman CS 164 Lecture 29

  8. A for-loop (top-comp (parse-string "for i:=1 to 10 do print(i)")) “the body must produce no value” p 528. What to do? If we have EVERY expression return a value, but some of the values are ‘VOID’ then we can be more consistent: functions OR procedures (functions with no returns) all leave something on the stack. We must pop off the non-values. Thus the body of the for-loop, print(i) leaves a value on the stack; and the compilation of the for-loop pops off the value. What if we didn’t pop it off? There would be 10 carcasses on the stack. Not a good thing. Prof. Fateman CS 164 Lecture 29

  9. A for-loop (top-comp (parse-string "for i:=1 to 10 do print(i)")) 0: args 0 4: pushenv 2 // for-loop 8: pushi 10 12: lset 2 1 // for-end 16: pop 20: pushi 1 24: lset 2 0 // i 28: lvar 2 1 // for-end 32: > 36: jumpn 76 40: save 56 44: lvar 2 0 // i 48: lvar 0 13 // print 52: callj 1 56: pop 60: lvar 2 0 // i 64: addi 1 68: lset 2 0 // i 72: jump 28 76: popenv 80: pushi VOID 84: exit 0 Prof. Fateman CS 164 Lecture 29

  10. Running the for-loop tiger(978): (doit "for i:=1 to 10 do print(i)") 12345678910 exit code 0 nil // stack is empty Prof. Fateman CS 164 Lecture 29

More Related