Compiling Imperative and Object-Oriented Languages - Activation Records

1,646 views

Published on

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

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,646
On SlideShare
0
From Embeds
0
Number of Embeds
674
Actions
Shares
0
Downloads
28
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \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
  • \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
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \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
  • Compiling Imperative and Object-Oriented Languages - Activation Records

    1. 1. Activation RecordsGuido Wachsmuth Delft Course IN4303, 2012/13 University of Technology Compiler Construction Challenge the future
    2. 2. Assessmentlast lectureGiven an optimisation step, explain • the optimisation, • its validity, • its benefits. Activation Records 2
    3. 3. Code Generationfunction fac0(n0: int): int= .method public static fac0(I)I if n0 = 0 iload 1 then ldc 0 1 if_icmpeq label0 else ldc 0 n0 * fac0(n0 - 1) goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method Activation Records 3
    4. 4. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload_1 ldc 0 ifne else0 if_icmpeq label0 ldc 0 iconst_1 goto label1 ireturn label0: ldc 1 label1: ifeq else0 else0: iload_1 ldc 1 dup goto end0 iconst_1 else0: iload 1 isub iload 1 invokestatic ldc 1 Exp/fac0(I)I isub imul invokestatic ireturn Exp/fac0(I)I .end method imul end0: ireturn.end method Activation Records 4
    5. 5. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ldc 0 ifeq label0 if_icmpeq label0 ldc 0 ldc 0 goto label1 goto label1 label0: ldc 1 label0: ldc 1 label1: ifeq else0 label1: ifeq else0 ldc 1 ldc 1 goto end0 goto end0 else0: iload 1 else0: iload 1 iload 1 iload 1 ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn .end method.end method Activation Records 5
    6. 6. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifeq label0 ifeq label0 ldc 0 ldc 0 goto label1 ifeq else0 label0: ldc 1 label0: ldc 1 label1: ifeq else0 label1: ifeq else0 ldc 1 ldc 1 goto end0 goto end0 else0: iload 1 else0: iload 1 iload 1 iload 1 ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn.end method .end method Activation Records 6
    7. 7. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifeq label0 ifeq label0 ldc 0 goto else0 ifeq else0 label0: ldc 1 label0: ldc 1 label1: ifeq else0 label1: ifeq else0 ldc 1 ldc 1 goto end0 goto end0 else0: iload 1 else0: iload 1 iload 1 iload 1 ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn .end method.end method Activation Records 7
    8. 8. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifeq label0 ifeq label0 goto else0 goto else0 label0: ldc 1 label0: ldc 1 label1: ifeq else0 ifeq else0 ldc 1 ldc 1 goto end0 goto end0 else0: iload 1 else0: iload 1 iload 1 iload 1 ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn.end method .end method Activation Records 8
    9. 9. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifeq label0 ifeq label0 goto else0 goto else0 label0: ldc 1 label0: ldc 1 ifeq else0 goto end0 ldc 1 else0: iload 1 goto end0 iload 1 else0: iload 1 ldc 1 iload 1 isub ldc 1 invokestatic isub Exp/fac0(I)I invokestatic imul Exp/fac0(I)I end0: ireturn imul .end method end0: ireturn.end method Activation Records 9
    10. 10. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifeq label0 ifneq else0 goto else0 label0: ldc 1 label0: ldc 1 goto end0 goto end0 else0: iload 1 else0: iload 1 iload 1 iload 1 ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn .end method.end method Activation Records 10
    11. 11. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifneq else0 ifneq else0 label0: ldc 1 ldc 1 goto end0 goto end0 else0: iload 1 else0: iload 1 iload 1 iload 1 ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn.end method .end method Activation Records 11
    12. 12. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifneq else0 ifneq else0 ldc 1 ldc 1 goto end0 ireturn else0: iload 1 else0: iload 1 iload 1 iload 1 ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn.end method .end method Activation Records 12
    13. 13. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload 1 ifneq else0 ifneq else0 ldc 1 ldc 1 ireturn ireturn else0: iload 1 else0: iload 1 iload 1 dup ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn.end method .end method Activation Records 13
    14. 14. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload 1 iload_1 ifneq else0 ifneq else0 ldc 1 ldc 1 ireturn ireturn else0: iload 1 else0: iload_1 dup dup ldc 1 ldc 1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn.end method .end method Activation Records 14
    15. 15. Optimisation.method public static fac0(I)I .method public static fac0(I)I iload_1 iload_1 ifneq else0 ifneq else0 ldc 1 iconst_1 ireturn ireturn else0: iload_1 else0: iload_1 dup dup ldc 1 iconst_1 isub isub invokestatic invokestatic Exp/fac0(I)I Exp/fac0(I)I imul imul end0: ireturn end0: ireturn.end method .end method Activation Records 15
    16. 16. Overviewtoday’s lectureactivation records • procedures in imperative and object-oriented languages • Java Virtual Machine • register-based machines • calling conventionssecond assignment • general remarks • namespace library • environment library • reports Activation Records 16
    17. 17. IJava Virtual Machine Term Rewriting 17
    18. 18. Recap: Modularityproceduresimperative languages • subroutines, routines, procedures, functions, methods • scoping: local variables • declarations with parameters (formal parameters) • calls with arguments (actual parameters) • pass by value, pass by reference Activation Records 18
    19. 19. Recap: Modularityproceduresimperative languages • subroutines, routines, procedures, functions, methods • scoping: local variables • declarations with parameters (formal parameters) • calls with arguments (actual parameters) • pass by value, pass by referencemachine code • jumps: call and return • call stack: return address, parameters, private data • procedure prologue and epilogue Activation Records 18
    20. 20. Java Virtual Machinestack frames method area stackpc: 03 optop: 02 local variables00 2A aload_0 00 4303 4303 00 4303 430301 10 bipush 01 0000 0040 0102 40 02 0203 B6 invokevirtual 03 0304 00 04 0405 01 01 05 0506 AC ireturn 06 06 heap Activation Records 19
    21. 21. Java Virtual Machinestack frames method area stack stackpc: 80 optop: 01 optop: 00 local variables local variables80 2B iload_1 00 4303 4303 00 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Activation Records 20
    22. 22. Java Virtual Machinestack frames method area stack stackpc: 81 optop: 01 optop: 01 local variables local variables80 2B iload_1 00 4303 4303 00 0000 0040 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Activation Records 21
    23. 23. Java Virtual Machinestack frames method area stack stackpc: 81 optop: 01 optop: 02 local variables local variables80 2B iload_1 00 4303 4303 00 0000 0040 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 0000 0040 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Activation Records 22
    24. 24. Java Virtual Machinestack frames method area stack stackpc: 82 optop: 01 optop: 01 local variables local variables80 2B iload_1 00 4303 4303 00 0000 1000 00 4303 4303 00 4303 430381 59 dup 01 0000 0040 01 01 0000 0040 0182 68 imul 02 02 02 0283 AC ireturn 03 03 03 0384 00 04 04 04 0485 00 05 05 05 0586 00 06 06 06 06 heap Activation Records 23
    25. 25. Java Virtual Machinestack frames method area stackpc: 06 optop: 01 local variables00 2A aload_0 00 0000 1000 00 4303 430301 10 bipush 01 0102 40 02 0203 B6 invokevirtual 03 0304 00 04 0405 01 01 05 0506 AC ireturn 06 06 heap Activation Records 24
    26. 26. Example: static call.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 Activation Records 25
    27. 27. Example: dynamic call.class public Exp .method public fac(I)I iload 1 ifne else iconst_1 ireturn else: iload 0 iload 1 dup iconst_1 isub invokevirtual Exp/fac(I)I imul ireturn .end method Activation Records 26
    28. 28. Code Patterndynamic method callcaller • push object • push parameters left-to-right • call method Activation Records 27
    29. 29. Code Patterndynamic method callcaller • push object • push parameters left-to-right • call methodvirtual machine on 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 Activation Records 27
    30. 30. Code Patternreturn from method callcallee • parameters in local variables • leave result on operand stack • return to caller Activation Records 28
    31. 31. Code Patternreturn from method callcallee • parameters in local variables • leave result on operand stack • return to callervirtual machine on return • push result on caller’s operand stack • point pc to return address • destroy frame Activation Records 28
    32. 32. Implementationheap-based 3 frame datafac(3) 3 2 Activation Records 29
    33. 33. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 Activation Records 29
    34. 34. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 2 Activation Records 29
    35. 35. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 Activation Records 29
    36. 36. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 2 Activation Records 29
    37. 37. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 2 2 Activation Records 29
    38. 38. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 2 2 1 Activation Records 29
    39. 39. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 2 1 Activation Records 29
    40. 40. Implementationheap-based 3 2 1 frame frame frame data data data fac(2) fac(1)fac(3) 3 2 2 Activation Records 29
    41. 41. Implementationheap-based 3 2 1 frame frame frame data data data fac(2) fac(1)fac(3) 3 2 1 2 Activation Records 29
    42. 42. Implementationheap-based 3 2 1 frame frame frame data data data fac(2) fac(1)fac(3) 3 2 2 Activation Records 29
    43. 43. Implementationheap-based 3 2 1 frame frame frame data data data fac(2) fac(1)fac(3) 3 2 1 2 Activation Records 29
    44. 44. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 2 1 Activation Records 29
    45. 45. Implementationheap-based 3 2 frame frame data data fac(2)fac(3) 3 2 2 Activation Records 29
    46. 46. Implementationheap-based 3 frame datafac(3) 3 2 Activation Records 29
    47. 47. 3Implementation frame datastack-based fac(3) 3 2 Activation Records 30
    48. 48. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) Activation Records 30
    49. 49. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 Activation Records 30
    50. 50. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) Activation Records 30
    51. 51. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 Activation Records 30
    52. 52. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 2 Activation Records 30
    53. 53. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 2 1 Activation Records 30
    54. 54. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 1 Activation Records 30
    55. 55. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data Activation Records 30
    56. 56. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data 1 Activation Records 30
    57. 57. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data Activation Records 30
    58. 58. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data 1 Activation Records 30
    59. 59. 3Implementation frame datastack-based fac(3) 3 2 frame data fac(2) 2 1 Activation Records 30
    60. 60. 3Implementation frame datastack-based fac(3) 3 2 Activation Records 30
    61. 61. coffee break Activation Records 31
    62. 62. IIregister-based machines Activation Records 32
    63. 63. Registersx86 familygeneral purpose registers • 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 Activation Records 33
    64. 64. Registersx86 familygeneral purpose registers • 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 operationsspecial purpose registers • segments SS, CS, DS, ES, FS, GS • flags EFLAGS Activation Records 33
    65. 65. Stack and Stack Framesstack • temporary storage • grows from high to low memory addresses • starts at SS Activation Records 34
    66. 66. Stack and Stack Framesstack • temporary storage • grows from high to low memory addresses • starts at SSstack frames • return address • local variables • parameters • stack base: BP • stack top: SP Activation Records 34
    67. 67. Calling ConventionsCDECLcaller • push parameters right-to-left on the stack • clean-up stack after callcallee • save old BP • initialise new BP • save registers • return result in AX • restore registers • restore BP Activation Records 35
    68. 68. Calling ConventionsCDECLcaller push 21 push 42 • push parameters right-to-left on the stack call add _f ESP 8 • clean-up stack after callcallee • save old BP • initialise new BP • save registers • return result in AX • restore registers • restore BP Activation Records 35
    69. 69. Calling ConventionsCDECLcaller push 21 push 42 • push parameters right-to-left on the stack call add _f ESP 8 • clean-up stack after callcallee • save old BP push EBP • initialise new BP mov mov EBP EAX ESP [EBP + 8] • save registers mov EDX [EBP + 12] • return result in AX add pop EAX EBP EDX • restore registers ret • restore BP Activation Records 35
    70. 70. Calling ConventionsSTDCALLcaller • push parameters right-to-left on the stackcallee • save old BP • initialise new BP • save registers • return result in AX • restore registers • restore BP Activation Records 36
    71. 71. Calling ConventionsSTDCALLcaller push 21 push 42 • push parameters right-to-left on the stack call _f@8callee • save old BP • initialise new BP • save registers • return result in AX • restore registers • restore BP Activation Records 36
    72. 72. Calling ConventionsSTDCALLcaller push 21 push 42 • push parameters right-to-left on the stack call _f@8callee • save old BP • initialise new BP push EBP • save registers mov mov EBP EAX ESP [EBP + 8] • return result in AX mov EDX [EBP + 12] • restore registers add pop EAX EBP EDX • restore BP ret 8 Activation Records 36
    73. 73. Calling ConventionsFASTCALLcaller • passes parameters in registers • pushes additional parameters right-to-left on the stackcallee • save old BP, initialise new BP • save registers • return result in AX • restore registers • restore BP • cleans up the stack Activation Records 37
    74. 74. Calling ConventionsFASTCALLcaller mov ECX 21 mov EDX 42 • passes parameters in registers call @f@8 • pushes additional parameters right-to-left on the stackcallee • save old BP, initialise new BP • save registers • return result in AX • restore registers • restore BP • cleans up the stack Activation Records 37
    75. 75. Calling ConventionsFASTCALLcaller mov ECX 21 mov EDX 42 • passes parameters in registers call @f@8 • pushes additional parameters right-to-left on the stackcallee • save old BP, initialise new BP push EBP • save registers mov mov EBP ESP EAX ECX • return result in AX add EAX EDX • restore registers pop ret EBP • restore BP • cleans up the stack Activation Records 37
    76. 76. IIIoptimisations revisited Term Rewriting 38
    77. 77. Optimisationsreasons • code overhead • execution overhead Activation Records 39
    78. 78. Optimisationsreasons • code overhead • execution overheadinlining • replace calls by body of the procedure • source code level Activation Records 39
    79. 79. Optimisationsreasons • code overhead • execution overheadinlining • replace calls by body of the procedure • source code leveltail recursion • replace recursive calls by loops or jumps • source or machine code level Activation Records 39
    80. 80. Example: Tail Recursion.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 Activation Records 40
    81. 81. Example: Tail Recursion.class public Exp .method public static fac(II)I iload 1 ifne else iload 2 ireturn else: iload 1 iconst_1 isub iload 1 iload 2 imul invokestatic Exp/fac(II)I ireturn .end method Activation Records 41
    82. 82. Example: Tail Recursion.class public Exp .method public static fac(II)I strt: iload 1 ifne else iload 2 ireturn else: iload 1 iconst_1 isub iload 1 iload 2 imul istore 2 istore 1 goto strt Activation Records 42
    83. 83. IVsummary Term Rewriting 43
    84. 84. Summarylessons learnedstack frames in the Java Virtual Machine • parameter passing, returning results • implementation strategies Activation Records 44
    85. 85. Summarylessons learnedstack frames in the Java Virtual Machine • parameter passing, returning results • implementation strategiesstack frames in register-based machines • registers x86 family • manipulating stack registers • calling conventions Activation Records 44
    86. 86. Summarylessons learnedstack frames in the Java Virtual Machine • parameter passing, returning results • implementation strategiesstack frames in register-based machines • registers x86 family • manipulating stack registers • calling conventionsoptimisations Activation Records 44
    87. 87. Literaturelearn moreJava Virtual Machine Tim Lindholm, Frank Yellin: The Java Virtual Machine Specification, 2nd edition. Addison-Wesley, 1999. Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000. Activation Records 45
    88. 88. Literaturelearn moreJava Virtual Machine Tim Lindholm, Frank Yellin: The Java Virtual Machine Specification, 2nd edition. Addison-Wesley, 1999. Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000.Activation Records Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Activation Records 45
    89. 89. Outlookcoming nextimperative and object-oriented languages • Lecture 10: Dataflow Analysis Nov 13 • Lecture 11: Register Allocation Nov 20 • Lecture 12: Garbage Collection Nov 27Lab Oct 25 • name analysis • name-based errors • type analysis for named elements • type-based errors Activation Records 46
    90. 90. copyrights & credits Activation Records 47
    91. 91. Activation Records 48
    92. 92. PicturescopyrightsSlide 1: Framed by LexnGer, some rights reservedSlide 37: The Lemon Tree by Dominica Williamson, some rights reservedSlide 46: Oude Kerk by M.M. R, some rights reserved Activation Records 49

    ×