cse443 compilers
play

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - PowerPoint PPT Presentation

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Phases of a compiler Intermediate Representation (IR): specification and generation Figure 1.6, page 5 of text Switch [p. 419] TEXTBOOK OUR LANGUAGE switch (E)


  1. CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall

  2. Phases of a compiler Intermediate Representation (IR): specification and generation Figure 1.6, page 5 of text

  3. Switch [p. 419] TEXTBOOK OUR LANGUAGE switch (E) { switch (E) { case C 1 : S 1 case C 1 : sblock 1 case C 2 : S 2 case C 2 : sblock 2 … … case C n-1 : S n-1 case C n-1 : sblock n-1 default : S n otherwise : sblock n } }

  4. Exercise: What switch (E) { case C 1 : sblock 1 intermediate case C 2 : sblock 2 code would … you generate? case C n-1 : sblock n-1 otherwise : sblock n } Discuss with your team for ~5 minutes.

  5. Switch (6.8.2) start E.code [read p.420-421] goto test L 1 sblock 1 .code switch (E) { goto next case C 1 : sblock 1 L 2 sblock 2 .code case C 2 : sblock 2 goto next … … L n-1 sblock n-1 .code case C n-1 : sblock n-1 goto next otherwise : sblock n L n sblock n .code goto next } test if t=C 1 goto L 1 if t=C 2 goto L 2 … if t=C n-1 goto L n-1 if t=C n goto L n next

  6. Function calls Basic form: id(e1,e2,…,ek) General form: assignable(e1,e2,…,ek) - If f is a function, g(4,5) returns a function, and r.h yields a function, then the following are legal: f(3) g(4,5)(3) r.h(3)

  7. How is function call carried out? 1. evaluate each of the argument expressions 2. mark the resulting values as parameters 3. invoke the function

  8. examples f(x+1)

  9. examples f(x+1) Remember that the function call has structure.

  10. examples f(x+1) t1 = x + 1 Generate code for the argument expression

  11. examples f(x+1) t1 = x + 1 param t1 Mark the result as a parameter of the function call

  12. examples f(x+1) t1 = x + 1 param t1 call(f,1) Call the function. The second argument of the call indicates the arity of the function (i.e. how many parameters it has)

  13. examples f(x+1) f(x+1,2*y) t1 = x + 1 param t1 call(f,1)

  14. examples f(x+1) f(x+1,2*y) t1 = x + 1 param t1 Remember that the call(f,1) function call has structure.

  15. examples f(x+1) f(x+1,2*y) t1 = x + 1 t1 = x + 1 param t1 call(f,1) Evaluate the first argument expression.

  16. examples f(x+1) f(x+1,2*y) t1 = x + 1 t1 = x + 1 param t1 param t1 call(f,1) Mark the result as a parameter.

  17. examples f(x+1) f(x+1,2*y) t1 = x + 1 t1 = x + 1 param t1 param t1 call(f,1) t2 = 2 * y Evaluate the second argument expresssion.

  18. examples f(x+1) f(x+1,2*y) t1 = x + 1 t1 = x + 1 param t1 param t1 call(f,1) t2 = 2 * y param t2 Mark the result as a parameter.

  19. examples f(x+1) f(x+1,2*y) t1 = x + 1 t1 = x + 1 param t1 param t1 call(f,1) t2 = 2 * y param t2 call(f,2) Call the function.

  20. examples f(x+1) f(x+1,2*y) t1 = x + 1 t1 = x + 1 t1 = x + 1 param t1 param t1 t2 = 2 * y call(f,1) t2 = 2 * y param t1 param t2 param t2 call(f,2) call(f,2) An alternative to intermingling the 'param' instructions with the argument evaluation is to gather them in a queue, then place them between the argument evaluations and before the 'call' instruction.

  21. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 param t1 param t1 t2 = 2 * y call(f,1) t2 = 2 * y param t1 param t2 param t2 call(f,2) call(f,2) A slightly more involved example.

  22. exercise f(g(3*z),h(a+b,a*b)) What intermediate code do you come up with for this example?

  23. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 param t1 param t1 t2 = 2 * y call(f,1) t2 = 2 * y param t1 param t2 param t2 call(f,2) call(f,2) As before, remember the structure…

  24. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y call(f,1) t2 = 2 * y param t1 param t2 param t2 call(f,2) call(f,2) …but not just the top-level structure!

  25. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 call(f,2) call(f,2) This translation will happen automatically due to the recursive structure of the function call for f…

  26. examples f(x+1) f(x+1,2*y) g(3*z) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 call(f,2) call(f,2) …view this as a function call in isolation.

  27. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 param t2 call(f,2) call(f,2) Mark the result as a parameter.

  28. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) More param t2 param t2 param t2 structure! call(f,2) call(f,2)

  29. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 param t2 call(f,2) call(f,2) t3 = a + b expression

  30. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 param t2 call(f,2) call(f,2) t3 = a + b param t3 parameter marking

  31. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 param t2 call(f,2) call(f,2) t3 = a + b param t3 t4 = a * b expression

  32. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 param t2 call(f,2) call(f,2) t3 = a + b param t3 t4 = a * b param t4 t5 = call(h,2) parameter marking and call

  33. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) param t2 param t2 param t2 call(f,2) call(f,2) t3 = a + b param t3 t4 = a * b param t4 t5 = call(h,2) parameter marking param t5 call(f,2) and call

  34. examples f(x+1) f(x+1,2*y) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t1 = x + 1 t1 = x + 1 t1 = 3 * z t1 = 3 * z param t1 param t1 t2 = 2 * y param t1 param t1 call(f,1) t2 = 2 * y param t1 t2 = call(g,1) t2 = call(g,1) param t2 param t2 param t2 t3 = a + b call(f,2) call(f,2) t3 = a + b t4 = a * b param t3 param t3 t4 = a * b param t4 param t4 t5 = call(h,2) t5 = call(h,2) param t2 param t5 param t5 call(f,2) call(f,2) Alternate translation gathering 'param' instructions together with call to function.

  35. exercise How will you modify your grammar rules to generate intermediate code for function calls? Assume that type checking and argument list length checking has already been accounted for in the semantic actions attached to productions: - type checking of each argument with corresponding parameter declaration (remembering that there is no coercion allowed in either an explicit or an implicit assignment) - checking that the number of arguments and the number of parameters is the same

  36. SP17 exercise outcome Basic approach teams took was to gather up information about argument expressions in an expression list, and generate the 'param' instructions at the end of the 'assignable ablock' rule, but only if assignable is a function (as opposed to an array). After the param instructions have been generated the 'call' instruction is generated, including the arity of the function (which is determined either by looking it up in the symbol table or by counting the number of arguments supplied).

  37. Phases of a compiler Target machine code generation Figure 1.6, page 5 of text

  38. Memory organization code static heap free memory stack

  39. Memory organization code static machine heap language instructions of the program free memory stack

  40. Memory organization code static statically allocated heap memory (e.g. constants, string literals) free memory stack

  41. Memory organization code static dynamically heap allocated memory (e.g. records, arrays) free memory stack

  42. Memory organization code static heap heap grows towards stack free memory stack

  43. Memory organization code 'free static memory' denotes the heap unallocated memory between heap and free memory stack stack

  44. Memory organization code static stack is used for heap function invocation records ("stack free memory frames") stack

  45. Memory organization code static heap stack grows towards heap free memory stack

  46. Memory organization code The size, static layout and heap contents of both the code and static regions free memory are determined at compile time stack

  47. Memory organization code static heap These regions are handled dynamically (i.e. at runtime) free memory stack

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