Declarative Semantics Definition - Code Generation

4,909 views
4,892 views

Published on

Presentation slides for lecture 5 of course IN4303 on Compiler Construction at TU Delft.

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,909
On SlideShare
0
From Embeds
0
Number of Embeds
1,445
Actions
Shares
0
Downloads
81
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • feedback loop\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • console interaction\n
  • console interaction\n
  • console interaction\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 1. ldc 0 if_icmpeq label0 → ifeq label0\n2. ldc 0 goto label1 ifeq else0 → goto else0\n3. ifeq label0 goto else0 → ifne else0\n4. ldc 1 ifeq else0 → nop\n5. goto end0 → ireturn\n6. iload 1 → iload_1\n7. ldc 1 → iconst_1\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  • \n
  • \n
  • Declarative Semantics Definition - Code Generation

    1. 1. IN4303 2014/15 Compiler Construction Declarative Semantics Definition code generation Guido Wachsmuth
    2. 2. Code Generation 2 source code
    3. 3. Code Generation 2 source code parse
    4. 4. Code Generation 2 source code errors parse check
    5. 5. Code Generation 2 source code parse generate check errors machine code
    6. 6. Code Generation 3 source code
    7. 7. Code Generation 3 source code parse
    8. 8. Code Generation 3 source code parse check
    9. 9. Code Generation 3 source code parse check
    10. 10. Code Generation 3 source code parse generate machine code check
    11. 11. Code Generation SDF3 NaBL TS Stratego ESV editor SPT tests 4 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
    12. 12. Code Generation SDF3 NaBL TS Stratego ESV editor SPT tests 5 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
    13. 13. Code Generation 6 JVM operand stack constant pool local variables heap stack frames class files code generation printing strings string concatenation string interpolation transformation calling conventions JVM register-based machines
    14. 14. Java Virtual Machine Code Generation 7
    15. 15. Code Generation 8 bytecode instructions pc: 00 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
    16. 16. Code Generation 9 bytecode instructions pc: 04 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
    17. 17. Code Generation 10 bytecode instructions pc: 03 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
    18. 18. Code Generation 11 bytecode instructions pc: 04 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
    19. 19. Code Generation 12 operand stack
    20. 20. Code Generation 13 operand stack pc: 00 method area stack optop: 00 00 01 02 03 04 05 06 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush ! sipush !
    21. 21. Code Generation 14 operand stack method area stack pc: 01 optop: 01 00 04 iconst_1 00 0000 0001 01 05 iconst_2 01 02 10 bipush 02 03 2A ! 03 04 11 sipush 04 05 43 ! 05 06 03 06
    22. 22. Code Generation operand stack pc: 02 optop: 02 00 01 02 03 04 05 06 0000 0001 0000 0002 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush ! sipush ! 15 method area stack
    23. 23. Code Generation operand stack pc: 04 optop: 03 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 002A 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush ! sipush ! 16 method area stack
    24. 24. Code Generation operand stack pc: 07 optop: 04 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 002A 0000 4303 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush ! sipush ! 17 method area stack
    25. 25. Code Generation operand stack optop: 04 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 002A 0000 4303 pc: 07 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne ! 00 18 method area stack
    26. 26. Code Generation operand stack optop: 03 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 432D pc: 08 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne ! 00 19 method area stack
    27. 27. Code Generation operand stack optop: 02 00 01 02 03 04 05 06 0000 0001 0000 865A pc: 09 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne ! 00 20 method area stack
    28. 28. Code Generation operand stack optop: 02 00 01 02 03 04 05 06 0000 865A 0000 0001 pc: 0A 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne ! 00 21 method area stack
    29. 29. Code Generation operand stack optop: 01 00 01 02 03 04 05 06 0000 8659 pc: 0B 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne ! 00 22 method area stack
    30. 30. Code Generation operand stack pc: 00 optop: 00 00 04 iconst_1 00 01 05 iconst_2 01 02 10 bipush 02 03 2A ! 03 04 11 sipush 04 05 43 ! 05 06 03 06 23 method area stack
    31. 31. Code Generation 24 constant pool
    32. 32. Code Generation Java Virtual Maccohnisnteant pool pc: 00 25 method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w ! 02 00 01 02 03 04 05 06 constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 00
    33. 33. Code Generation 26 Java Virtual Maccohnisnteant pool method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w ! 02 00 01 02 03 04 05 06 0000 002A constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 01 pc: 02
    34. 34. Code Generation 27 Java Virtual Maccohnisnteant pool method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w ! 02 00 01 02 03 04 05 06 0000 002A 0000 4303 constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 02 pc: 04
    35. 35. Code Generation 28 Java Virtual Maccohnisnteant pool method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w ! 02 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 04 pc: 07
    36. 36. Code Generation 29 local variables
    37. 37. Code Generation 30 local variables method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 00 01 02 03 04 05 06 local variables 00 01 02 03 04 05 06 optop: 00 pc: 00
    38. 38. Code Generation local variables 00 01 02 03 04 05 06 0000 0001 31 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 01 02 03 04 05 06 optop: 01 pc: 01
    39. 39. Code Generation local variables 00 01 02 03 04 05 06 32 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 0000 0001 01 02 03 04 05 06 optop: 00 pc: 02
    40. 40. Code Generation local variables 00 01 02 03 04 05 06 0000 0001 33 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 0000 0001 01 02 03 04 05 06 optop: 01 pc: 03
    41. 41. Code Generation local variables 00 01 02 03 04 05 06 34 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 0000 0001 01 0000 0001 02 03 04 05 06 optop: 00 pc: 04
    42. 42. Code Generation local variables 00 01 02 03 04 05 06 35 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 0000 0001 01 0000 0002 02 03 04 05 06 optop: 00 pc: 07
    43. 43. Code Generation 36 heap
    44. 44. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 37 heap method area stack optop: 00 00 01 02 03 04 05 06 heap pc: 00 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
    45. 45. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 38 heap method area stack optop: 01 00 01 02 03 04 05 06 heap pc: 02 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
    46. 46. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 39 heap method area stack optop: 02 00 01 02 03 04 05 06 heap pc: 04 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 002A 002A constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
    47. 47. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 40 heap method area stack optop: 03 00 01 02 03 04 05 06 heap pc: 06 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 002A 002A 0000 0004 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
    48. 48. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 41 heap method area stack optop: 02 00 01 02 03 04 05 06 heap pc: 07 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 0000 0042 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
    49. 49. Code Generation 42 stack frames
    50. 50. Code Generation 43 Type Systems dispatch static vs. dynamic dispatch link method call to method static dispatch type information at compile-time dynamic dispatch recap type information at run-time single dispatch: one parameter multiple dispatch: more parameters
    51. 51. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 02 44 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 pc: 03 stack frames
    52. 52. local variables 00 01 02 03 04 05 06 Code Generation pc: 80 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 45 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 00 stack 00 01 02 03 04 05 06
    53. 53. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 46 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 0040
    54. 54. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 47 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 02 stack 00 01 02 03 04 05 06 0000 0040 0000 0040
    55. 55. local variables 00 01 02 03 04 05 06 Code Generation pc: 82 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 48 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 1000
    56. 56. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 01 49 method area stack 00 01 02 03 04 05 06 heap 0000 1000 local variables 00 4303 4303 01 02 03 04 05 06 pc: 06 stack frames
    57. 57. Java Virtual Machine Code Generation 50 class files
    58. 58. Code Generation 51 > ls Course.java > javac -verbose Course.java [parsing started Course.java] [parsing completed 8ms] [loading java/lang/Object.class(java/lang:Object.class)] [checking university.Course] [wrote Course.class] [total 411ms] > ls Course.class Course.java recap traditional compilers
    59. 59. Code Generation 52 class files format magic number CAFEBABE class file version (minor, major) constant pool count + constant pool access flags this class super class interfaces count + interfaces fields count + fields methods count + methods attribute count + attributes
    60. 60. Code Generation 53 .class public Exp .method public static fac(I)I ! iload 1 ifne else ! iconst_1 ireturn ! else: iload 1 dup iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method Jasmin intermediate language
    61. 61. Code Generation 54 code generation strings
    62. 62. Code Generation 55 printing strings to-jbc = ?Nil() ; <printstring> "aconst_nulln" to-jbc = ?NoVal() ; <printstring> "nopn" to-jbc = ?Seq(es) ; <list-loop(to-jbc)> es to-jbc = ?Int(i); <printstring> "ldc "; <printstring> i; <printstring> "n" to-jbc = ?Bop(op, e1, e2) ; <to-jbc> e1 ; <to-jbc> e2 ; <to-jbc> op to-jbc = ?PLUS() ; <printstring> "iaddn" to-jbc = ?MINUS() ; <printstring> "isubn" to-jbc = ?MUL() ; <printstring> "imuln" to-jbc = ?DIV() ; <printstring> "idivn"
    63. 63. Code Generation 56 string concatenation to-jbc: Nil() -> "aconst_nulln" to-jbc: NoVal() -> "nopn" to-jbc: Seq(es) -> <concat-strings> <map(to-jbc)> es to-jbc: Int(i) -> <concat-strings> ["ldc ", i, "n"] to-jbc: Bop(op, e1, e2) -> <concat-strings> [ <to-jbc> e1, <to-jbc> e2, <to-jbc> op ] to-jbc: PLUS() -> "iaddn" to-jbc: MINUS() -> "isubn" to-jbc: MUL() -> "imuln" to-jbc: DIV() -> "idivn"
    64. 64. Code Generation 57 string interpolation to-jbc: Nil() -> $[aconst_null] to-jbc: NoVal() -> $[nop] to-jbc: Seq(es) -> <map-to-jbc> es ! map-to-jbc: [] -> $[] map-to-jbc: [h|t] -> $[[<to-jbc> h] [<map-to-jbc> t]] to-jbc: Int(i) -> $[ldc [i]] to-jbc: Bop(op, e1, e2) -> $[[<to-jbc> e1] [<to-jbc> e2] [<to-jbc> op]] to-jbc: PLUS() -> $[iadd] to-jbc: MINUS() -> $[isub] to-jbc: MUL() -> $[imul] to-jbc: DIV() -> $[idiv]
    65. 65. Code Generation 58 code generation transformation
    66. 66. parse desugar analyse normalise Code Generation 59 recap compilation by transformation backend frontend generate optimise format
    67. 67. Code Generation 60 transformation to-jbc: Nil() -> [ ACONST_NULL() ] to-jbc: NoVal() -> [ NOP() ] to-jbc: Seq(es) -> <mapconcat(to-jbc)> es to-jbc: Int(i) -> [ LDC(Int(i)) ] to-jbc: String(s) -> [ LDC(String(s)) ] to-jbc: Bop(op, e1, e2) -> <mapconcat(to-jbc)> [ e1, e2, op ] to-jbc: PLUS() -> [ IADD() ] to-jbc: MINUS() -> [ ISUB() ] to-jbc: MUL() -> [ IMUL() ] to-jbc: DIV() -> [ IDIV() ] ! to-jbc: Assign(lhs, e) -> <concat> [ <to-jbc> e, <lhs-to-jbc> lhs ] to-jbc: Var(x) -> [ ILOAD(x) ] where <type-of> Var(x) => INT() to-jbc: Var(x) -> [ ALOAD(x) ] where <type-of> Var(x) => STRING() lhs-to-jbc: Var(x) -> [ ISTORE(x) ] where <type-of> Var(x) => INT() lhs-to-jbc: Var(x) -> [ ASTORE(x) ] where <type-of> Var(x) => STRING()
    68. 68. Code Generation 61 to-jbc: transformation IfThenElse(e1, e2, e3) -> <concat> [ <to-jbc> e1 , [ IFEQ(LabelRef(else)) ] , <to-jbc> e2 , [ GOTO(LabelRef(end)), Label(else) ] , <to-jbc> e3 , [ Label(end) ] ] where <newname> "else" => else where <newname> "end" => end to-jbc: While(e1, e2) -> <concat> [ [ GOTO(LabelRef(check)), Label(body) ] , <to-jbc> e2 , [ Label(check) ] , <to-jbc> e1 , [ IFNE(LabelRef(body)) ] ] where <newname> "test" => check where <newname> "body" => body
    69. 69. Code Generation 62 function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1) transformation example .method public static fac(I)I ! iload 1 ldc 0 if_icmpeq label0 ldc 0 goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac(I)I imul end0: ireturn .end method
    70. 70. C X86 optimise frontends backends Code Generation 63 LLVM compiler infrastructure Fortran Ada PowerPC ARM intermediate representation
    71. 71. calling conventions Code Generation 64 Java Virtual Machine
    72. 72. Code Generation 65 function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1) example static call .class public Exp .method public static fac(I)I ! iload 1 ifne else ! iconst_1 ireturn ! else: iload 1 iload 1 iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method
    73. 73. Code Generation 66 function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1) example dynamic call .class public Exp .method public fac(I)I ! iload 1 ifne else ! iconst_1 ireturn ! else: iload 1 iload 0 iload 1 iconst_1 isub invokevirtual Exp/fac(I)I imul ireturn .end method
    74. 74. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 02 67 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 pc: 03 stack frames
    75. 75. local variables 00 01 02 03 04 05 06 Code Generation pc: 80 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 68 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 00 stack 00 01 02 03 04 05 06
    76. 76. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 69 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 0040
    77. 77. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 70 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 02 stack 00 01 02 03 04 05 06 0000 0040 0000 0040
    78. 78. local variables 00 01 02 03 04 05 06 Code Generation pc: 82 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 71 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 1000
    79. 79. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 01 72 method area stack 00 01 02 03 04 05 06 heap 0000 1000 local variables 00 4303 4303 01 02 03 04 05 06 pc: 06 stack frames
    80. 80. Code Generation 73 responsibilities Type Systems caller push object push parameters left-to-right call method virtual machine on call method call allocate space (frame data, operand stack, local variables) store frame data (data pointer, return address, exception table) store parameters as local variables dynamic dispatch point pc to method code
    81. 81. Code Generation 74 responsibilities return from method call Type Systems callee parameters in local variables leave result on operand stack return to caller virtual machine on return push result on caller’s operand stack point pc to return address destroy frame
    82. 82. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based
    83. 83. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2)
    84. 84. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2
    85. 85. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2)
    86. 86. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2
    87. 87. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 2
    88. 88. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 2 1
    89. 89. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 1
    90. 90. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2
    91. 91. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2 1
    92. 92. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2
    93. 93. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2 1
    94. 94. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 1
    95. 95. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2
    96. 96. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based
    97. 97. 3 frame data 3 2 fac(3) Code Generation 76 implementation stack-based
    98. 98. 3 frame data 3 fac(3) 2 frame data fac(2) Code Generation 76 implementation stack-based
    99. 99. 3 frame data 3 fac(3) 2 frame data fac(2) 2 Code Generation 76 implementation stack-based
    100. 100. 3 frame data 3 fac(3) 2 frame data fac(2) Code Generation 76 implementation stack-based
    101. 101. 3 frame data 3 fac(3) 2 frame data fac(2) 2 Code Generation 76 implementation stack-based
    102. 102. 3 frame data 3 fac(3) 2 frame data fac(2) 2 2 Code Generation 76 implementation stack-based
    103. 103. 3 frame data 3 fac(3) 2 frame data fac(2) 2 2 1 Code Generation 76 implementation stack-based
    104. 104. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 Code Generation 76 implementation stack-based
    105. 105. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) Code Generation 76 implementation stack-based
    106. 106. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) 1 Code Generation 76 implementation stack-based
    107. 107. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) Code Generation 76 implementation stack-based
    108. 108. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) 1 Code Generation 76 implementation stack-based
    109. 109. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 Code Generation 76 implementation stack-based
    110. 110. 3 frame data 3 2 fac(3) Code Generation 76 implementation stack-based
    111. 111. calling conventions Code Generation 77 register-based machines
    112. 112. Code Generation 78 Type Systems general purpose registers recap x86 family accumulator AX - arithmetic operations counter CX - shift/rotate instructions, loops data DX - arithmetic operations, I/O base BX - pointer to data stack pointer SP, base pointer BP - top and base of stack source SI, destination DI - stream operations special purpose registers segments SS, CS, DS, ES, FS, GS flags EFLAGS
    113. 113. Code Generation 79 Type Systems stack temporary storage grows from high to low memory addresses starts at SS stack frames return address local variables parameters stack base: BP stack top: SP stack and stack frames
    114. 114. Code Generation 80 Type Systems caller push parameters right-to-left on the stack clean-up stack after call callee save old BP initialise new BP save registers return result in AX restore registers restore BP calling conventions CDECL
    115. 115. Code Generation 80 Type Systems caller push parameters right-to-left on the stack clean-up stack after call callee save old BP initialise new BP save registers return result in AX restore registers restore BP calling conventions CDECL push 21 push 42 call _f add ESP 8 push EBP mov EBP ESP mov EAX [EBP + 8] mov EDX [EBP + 12] add EAX EDX pop EBP ret
    116. 116. Code Generation 81 calling conventions Type Systems caller push parameters right-to-left on the stack callee save old BP, initialise new BP save registers return result in AX restore registers restore BP cleans up the stack STDCALL
    117. 117. push 21 push 42 call _f@8 push EBP mov EBP ESP mov EAX [EBP + 8] mov EDX [EBP + 12] add EAX EDX pop EBP ret 8 Code Generation 81 calling conventions Type Systems caller push parameters right-to-left on the stack callee save old BP, initialise new BP save registers return result in AX restore registers restore BP cleans up the stack STDCALL
    118. 118. Code Generation 82 calling conventions Type Systems caller pass parameters in registers push remaining parameters right-to-left on the stack callee save old BP, initialise new BP save registers return result in AX restore registers restore BP cleans up the stack FASTCALL
    119. 119. Code Generation 82 calling conventions Type Systems caller pass parameters in registers push remaining parameters right-to-left on the stack callee save old BP, initialise new BP save registers return result in AX restore registers restore BP cleans up the stack FASTCALL mov ECX 21 mov EDX 42 call @f@8 push EBP mov EBP ESP mov EAX ECX add EAX EDX pop EBP ret
    120. 120. Code Generation 83 calling conventions Type Systems method declarations in principle: full freedom project constraints target platform constraints method calls compilation need to match method declarations precompiled libraries avoid recompilation source code not always available
    121. 121. Except where otherwise noted, this work is licensed under Code Generation 84
    122. 122. Code Generation 85 attribution slide title author license 1 Matrix Gamaliel Espinoza Macedo CC BY-NC 2.0 2, 3, 59, 63 PICOL icons Melih Bilgil CC BY 3.0 12 Gray Legos wallpaper monohex CC BY-NC-SA 2.0 24 Billiard Balls Darren Hester some rights reserved 29 Autoturm m.prinke CC BY-SA 2.0 36 Spy Hill Landfill D'Arcy Norman some rights reserved 42 Framed LexnGer CC BY-NC 2.0

    ×