1 / 16

Lecture 12

Lecture 12. Code Generation II. Generating Stack code. Generating Stack Code. Generating stack code simple expressions boolean expressions control flow Code for complex expressions procedures function calls mixed type expressions array references Storage local variables stack limit.

moeshe
Download Presentation

Lecture 12

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. Lecture 12 Code Generation II Generating Stack code

  2. Generating Stack Code • Generating stack code • simple expressions • boolean expressions • control flow • Code for complex expressions • procedures • function calls • mixed type expressions • array references • Storage • local variables • stack limit

  3. Simple expressions • make use of implicit stack • still need variable addresses expr( node ) switch( node.kind ) case PLUS: if (node.type() == INT) emit( iadd ); break; case ID: int offset = addr( node.str ); if (node.type() == INT) emit( iload, offset ); break; case NUM: emit( ldc int, node.val ); break; return result

  4. Example: iload 1// push addr(x) [... x ldc_int, 4// push constant 4 […x 4 iload 2// push addr(y) [... x 4 y iadd // add 4 & y [... x 4+y iadd // add x & (4 + y) [... x+(4+y)

  5. E1 = = E2 ==> E1 E2 if_icmpeq L1 iconst 0 goto L2 L1: iconst 1 L2: Boolean Expressions • Algorithm • evaluate expression • leave 1 on top of stack if true, and 0 if true

  6. E1 < E2 ==> E1 E2 if_icmplt L1 iconst 0 goto L2 L1: iconst 1 L2: Boolean Expression

  7. E1 && E2==> E1 dup ifeq L1 pop E2 L1: Boolean Expressions

  8. E1 || E2 ==> E1 dup ifne L1 pop E2 L1: Boolean Expressions

  9. ! E ==> E ifeq L1 iconst 0 goto L2 L1: iconst 1 L2: Boolean Expressions

  10. X = E ==> if ( E ) S ==> if(E) S1 else S2 ==> E istore addr(x) E ifeq L S L: E ifeq L1 S1 goto L2 L1: S2 L2: Control Structures

  11. while ( E ) S==> L1: E ifeq L2 S goto L1 L2: Control Structures

  12. f(E1,E2,…,En) ==> E1 E2 … En invoke f procedure / function call

  13. Procedure calls • Code for procedure calls (FP is frame pointer, RA is return address) save registers prolog code extend basic frame for local data find static data area if needed initialize locals ... allocate child's frame start of a call evaluate & store parameters store FP and RA in child’s frame set FP for child jump to child may handle RA

  14. Procedure calls ... RA: copy return value post-call code restore parameters. if needed free child's frame ... store return value epilog code unextend basic frame restore registers restore parent's FP jump to RA hardware ret

  15. Function calls • How do we handle a function call in an expression? • Example: a + foo(1) • Treat it like a procedure call • set up the arguments • generate the call and return sequence • get the return value into a register • Cautions • function may have side effects • evaluation order is suddenly important • register save-restore covers intermediate values

  16. Example • a + foo(a,b) + b // b is call-by reference iload addr(a) dup iload addr(b) invoke foo(int,int) istore addr(b) // copy b back iadd iload addr(b) iadd

More Related