SlideShare a Scribd company logo
Activation Records

Guido Wachsmuth




       Delft
                              Course IN4303, 2012/13
       University of
       Technology               Compiler Construction
       Challenge the future
Assessment
last lecture

Given an optimisation step, explain
  •   the optimisation,
  •   its validity,
  •   its benefits.




                                      Activation Records   2
Code Generation
function 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
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
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
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
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
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
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
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
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
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
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
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
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
Overview
today’s lecture

activation records

  •   procedures in imperative and object-oriented languages
  •   Java Virtual Machine
  •   register-based machines
  •   calling conventions
second assignment
  •   general remarks
  •   namespace library
  •   environment library
  •   reports


                                                        Activation Records 16
I
Java Virtual Machine




                       Term Rewriting 17
Recap: Modularity
procedures

imperative 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
Recap: Modularity
procedures

imperative 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

machine code

  •   jumps: call and return
  •   call stack: return address, parameters, private data
  •   procedure prologue and epilogue

                                                             Activation Records 18
Java Virtual Machine
stack frames

          method area                       stack

pc: 03                          optop: 02       local variables
00   2A      aload_0            00 4303 4303   00 4303 4303
01   10      bipush             01 0000 0040   01
02   40                         02             02
03   B6      invokevirtual      03             03
04   00                         04             04
05   01      01                 05             05
06   AC      ireturn            06             06

                             heap



                                                Activation Records 19
Java Virtual Machine
stack frames

          method area                 stack
                                       stack
pc: 80                     optop: 01
                            optop: 00       local variables
                                             local variables
80   2B      iload_1       00 4303 4303
                            00            00 4303 4303
                                           00 4303 4303
81   59      dup           01 0000 0040
                            01            01 0000 0040
                                           01
82   68      imul          02
                            02            02
                                           02
83   AC      ireturn       03
                            03            03
                                           03
84   00                    04
                            04            04
                                           04
85   00                    05
                            05            05
                                           05
86   00                    06
                            06            06
                                           06

                        heap



                                           Activation Records 20
Java Virtual Machine
stack frames

          method area                  stack
                                        stack
pc: 81                     optop: 01
                            optop: 01        local variables
                                              local variables
80   2B      iload_1       00 4303 4303
                            00 0000 0040   00 4303 4303
                                            00 4303 4303
81   59      dup           01 0000 0040
                            01             01 0000 0040
                                            01
82   68      imul          02
                            02             02
                                            02
83   AC      ireturn       03
                            03             03
                                            03
84   00                    04
                            04             04
                                            04
85   00                    05
                            05             05
                                            05
86   00                    06
                            06             06
                                            06

                        heap



                                            Activation Records 21
Java Virtual Machine
stack frames

          method area                  stack
                                        stack
pc: 81                     optop: 01
                            optop: 02        local variables
                                              local variables
80   2B      iload_1       00 4303 4303
                            00 0000 0040   00 4303 4303
                                            00 4303 4303
81   59      dup           01 0000 0040
                            01 0000 0040   01 0000 0040
                                            01
82   68      imul          02
                            02             02
                                            02
83   AC      ireturn       03
                            03             03
                                            03
84   00                    04
                            04             04
                                            04
85   00                    05
                            05             05
                                            05
86   00                    06
                            06             06
                                            06

                        heap



                                            Activation Records 22
Java Virtual Machine
stack frames

          method area                  stack
                                        stack
pc: 82                     optop: 01
                            optop: 01        local variables
                                              local variables
80   2B      iload_1       00 4303 4303
                            00 0000 1000   00 4303 4303
                                            00 4303 4303
81   59      dup           01 0000 0040
                            01             01 0000 0040
                                            01
82   68      imul          02
                            02             02
                                            02
83   AC      ireturn       03
                            03             03
                                            03
84   00                    04
                            04             04
                                            04
85   00                    05
                            05             05
                                            05
86   00                    06
                            06             06
                                            06

                        heap



                                            Activation Records 23
Java Virtual Machine
stack frames

          method area                       stack

pc: 06                          optop: 01       local variables
00   2A      aload_0            00 0000 1000   00 4303 4303
01   10      bipush             01             01
02   40                         02             02
03   B6      invokevirtual      03             03
04   00                         04             04
05   01      01                 05             05
06   AC      ireturn            06             06

                             heap



                                                Activation Records 24
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
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
Code Pattern
dynamic method call

caller

   •     push object
   •     push parameters left-to-right
   •     call method




                                         Activation Records 27
Code Pattern
dynamic method call

caller

   •     push object
   •     push parameters left-to-right
   •     call method

virtual 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
Code Pattern
return from method call

callee

   •     parameters in local variables
   •     leave result on operand stack
   •     return to caller




                                         Activation Records 28
Code Pattern
return from method call

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




                                                 Activation Records 28
Implementation
heap-based



          3

         frame
          data
fac(3)




          3

          2




                 Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3

          2




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3                2

          2




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3

          2




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3                2

          2




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3                2

          2                2




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3                2

          2                2

                           1




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3                2

          2                1




                                  Activation Records 29
Implementation
heap-based



          3                2                       1

         frame            frame                 frame
          data             data                  data

                 fac(2)




                                       fac(1)
fac(3)




          3                2

          2




                                  Activation Records 29
Implementation
heap-based



          3                2                       1

         frame            frame                 frame
          data             data                  data

                 fac(2)




                                       fac(1)
fac(3)




          3                2                       1

          2




                                  Activation Records 29
Implementation
heap-based



          3                2                       1

         frame            frame                 frame
          data             data                  data

                 fac(2)




                                       fac(1)
fac(3)




          3                2

          2




                                  Activation Records 29
Implementation
heap-based



          3                2                       1

         frame            frame                 frame
          data             data                  data

                 fac(2)




                                       fac(1)
fac(3)




          3                2                       1

          2




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3                2

          2                1




                                  Activation Records 29
Implementation
heap-based



          3                2

         frame            frame
          data             data

                 fac(2)
fac(3)




          3                2

          2




                                  Activation Records 29
Implementation
heap-based



          3

         frame
          data
fac(3)




          3

          2




                 Activation Records 29
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3

                           2




                                  Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           2




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           2

                           1




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           1




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           1

                          frame
                 fac(1)    data




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           1

                          frame
                 fac(1)    data

                           1




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           1

                          frame
                 fac(1)    data




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           1

                          frame
                 fac(1)    data

                           1




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3
                           2

                          frame
                           data




                                  fac(2)
                           2

                           1




                                           Activation Records 30
3

Implementation            frame
                           data
stack-based




                 fac(3)
                           3

                           2




                                  Activation Records 30
coffee break




               Activation Records 31
II
register-based machines




                          Activation Records 32
Registers
x86 family

general 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
Registers
x86 family

general 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

special purpose registers

  •   segments SS, CS, DS, ES, FS, GS
  •   flags EFLAGS

                                                          Activation Records 33
Stack and Stack Frames

stack

  •     temporary storage
  •     grows from high to low memory addresses
  •     starts at SS




                                                  Activation Records 34
Stack and Stack Frames

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

                                                  Activation Records 34
Calling Conventions
CDECL

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

                                                      Activation Records 35
Calling Conventions
CDECL

caller                                                push   21
                                                      push   42
   •     push parameters right-to-left on the stack   call
                                                      add
                                                             _f
                                                             ESP 8
   •     clean-up stack after call

callee

   •     save old BP
   •     initialise new BP
   •     save registers
   •     return result in AX
   •     restore registers
   •     restore BP

                                                             Activation Records 35
Calling Conventions
CDECL

caller                                                push   21
                                                      push   42
   •     push parameters right-to-left on the stack   call
                                                      add
                                                             _f
                                                             ESP 8
   •     clean-up stack after call

callee

   •     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
Calling Conventions
STDCALL

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



                                                      Activation Records 36
Calling Conventions
STDCALL

caller                                                push 21
                                                      push 42
   •     push parameters right-to-left on the stack   call _f@8


callee

   •     save old BP
   •     initialise new BP
   •     save registers
   •     return result in AX
   •     restore registers
   •     restore BP



                                                           Activation Records 36
Calling Conventions
STDCALL

caller                                                push 21
                                                      push 42
   •     push parameters right-to-left on the stack   call _f@8


callee

   •     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
Calling Conventions
FASTCALL

caller

   •     passes parameters in registers
   •     pushes additional 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

                                                             Activation Records 37
Calling Conventions
FASTCALL

caller                                                  mov ECX 21
                                                        mov EDX 42
   •     passes parameters in registers                 call @f@8

   •     pushes additional 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

                                                             Activation Records 37
Calling Conventions
FASTCALL

caller                                                  mov ECX 21
                                                        mov EDX 42
   •     passes parameters in registers                 call @f@8

   •     pushes additional parameters right-to-left on the stack

callee

   •     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
III
optimisations revisited




                          Term Rewriting 38
Optimisations

reasons

  •   code overhead
  •   execution overhead




                           Activation Records 39
Optimisations

reasons

   •   code overhead
   •   execution overhead

inlining

   •   replace calls by body of the procedure
   •   source code level




                                                Activation Records 39
Optimisations

reasons

   •   code overhead
   •   execution overhead

inlining

   •   replace calls by body of the procedure
   •   source code level

tail recursion

   •   replace recursive calls by loops or jumps
   •   source or machine code level


                                                   Activation Records 39
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
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
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
IV
summary




          Term Rewriting 43
Summary
lessons learned

stack frames in the Java Virtual Machine

  •   parameter passing, returning results
  •   implementation strategies




                                             Activation Records 44
Summary
lessons learned

stack frames in the Java Virtual Machine

  •   parameter passing, returning results
  •   implementation strategies

stack frames in register-based machines

  •   registers x86 family
  •   manipulating stack registers
  •   calling conventions




                                             Activation Records 44
Summary
lessons learned

stack frames in the Java Virtual Machine

  •   parameter passing, returning results
  •   implementation strategies

stack frames in register-based machines

  •   registers x86 family
  •   manipulating stack registers
  •   calling conventions

optimisations



                                             Activation Records 44
Literature
learn more

Java 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
Literature
learn more

Java 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
Outlook
coming next

imperative and object-oriented languages

  •   Lecture 10: Dataflow Analysis Nov 13
  •   Lecture 11: Register Allocation Nov 20
  •   Lecture 12: Garbage Collection Nov 27

Lab Oct 25

  •   name analysis
  •   name-based errors
  •   type analysis for named elements
  •   type-based errors



                                               Activation Records 46
copyrights & credits




                       Activation Records 47
Activation Records 48
Pictures
copyrights

Slide 1:
   Framed by LexnGer, some rights reserved

Slide 37:
   The Lemon Tree by Dominica Williamson, some rights reserved

Slide 46:
   Oude Kerk by M.M. R, some rights reserved




                                                                 Activation Records 49
Ad

More Related Content

More from Guido Wachsmuth (13)

Compiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage CollectionCompiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Guido Wachsmuth
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
Guido Wachsmuth
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Guido Wachsmuth
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Guido Wachsmuth
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
Guido Wachsmuth
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation
Guido Wachsmuth
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
Guido Wachsmuth
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
Guido Wachsmuth
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
Guido Wachsmuth
 
Software Languages
Software LanguagesSoftware Languages
Software Languages
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage CollectionCompiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Guido Wachsmuth
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
Guido Wachsmuth
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Guido Wachsmuth
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Guido Wachsmuth
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
Guido Wachsmuth
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation
Guido Wachsmuth
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
Guido Wachsmuth
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
Guido Wachsmuth
 

Recently uploaded (20)

The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Ad

Compiling Imperative and Object-Oriented Languages - Activation Records

  • 1. Activation Records Guido Wachsmuth Delft Course IN4303, 2012/13 University of Technology Compiler Construction Challenge the future
  • 2. Assessment last lecture Given an optimisation step, explain • the optimisation, • its validity, • its benefits. Activation Records 2
  • 3. Code Generation function 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Overview today’s lecture activation records • procedures in imperative and object-oriented languages • Java Virtual Machine • register-based machines • calling conventions second assignment • general remarks • namespace library • environment library • reports Activation Records 16
  • 17. I Java Virtual Machine Term Rewriting 17
  • 18. Recap: Modularity procedures imperative 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. Recap: Modularity procedures imperative 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 machine code • jumps: call and return • call stack: return address, parameters, private data • procedure prologue and epilogue Activation Records 18
  • 20. Java Virtual Machine stack frames method area stack pc: 03 optop: 02 local variables 00 2A aload_0 00 4303 4303 00 4303 4303 01 10 bipush 01 0000 0040 01 02 40 02 02 03 B6 invokevirtual 03 03 04 00 04 04 05 01 01 05 05 06 AC ireturn 06 06 heap Activation Records 19
  • 21. Java Virtual Machine stack frames method area stack stack pc: 80 optop: 01 optop: 00 local variables local variables 80 2B iload_1 00 4303 4303 00 00 4303 4303 00 4303 4303 81 59 dup 01 0000 0040 01 01 0000 0040 01 82 68 imul 02 02 02 02 83 AC ireturn 03 03 03 03 84 00 04 04 04 04 85 00 05 05 05 05 86 00 06 06 06 06 heap Activation Records 20
  • 22. Java Virtual Machine stack frames method area stack stack pc: 81 optop: 01 optop: 01 local variables local variables 80 2B iload_1 00 4303 4303 00 0000 0040 00 4303 4303 00 4303 4303 81 59 dup 01 0000 0040 01 01 0000 0040 01 82 68 imul 02 02 02 02 83 AC ireturn 03 03 03 03 84 00 04 04 04 04 85 00 05 05 05 05 86 00 06 06 06 06 heap Activation Records 21
  • 23. Java Virtual Machine stack frames method area stack stack pc: 81 optop: 01 optop: 02 local variables local variables 80 2B iload_1 00 4303 4303 00 0000 0040 00 4303 4303 00 4303 4303 81 59 dup 01 0000 0040 01 0000 0040 01 0000 0040 01 82 68 imul 02 02 02 02 83 AC ireturn 03 03 03 03 84 00 04 04 04 04 85 00 05 05 05 05 86 00 06 06 06 06 heap Activation Records 22
  • 24. Java Virtual Machine stack frames method area stack stack pc: 82 optop: 01 optop: 01 local variables local variables 80 2B iload_1 00 4303 4303 00 0000 1000 00 4303 4303 00 4303 4303 81 59 dup 01 0000 0040 01 01 0000 0040 01 82 68 imul 02 02 02 02 83 AC ireturn 03 03 03 03 84 00 04 04 04 04 85 00 05 05 05 05 86 00 06 06 06 06 heap Activation Records 23
  • 25. Java Virtual Machine stack frames method area stack pc: 06 optop: 01 local variables 00 2A aload_0 00 0000 1000 00 4303 4303 01 10 bipush 01 01 02 40 02 02 03 B6 invokevirtual 03 03 04 00 04 04 05 01 01 05 05 06 AC ireturn 06 06 heap Activation Records 24
  • 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. 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. Code Pattern dynamic method call caller • push object • push parameters left-to-right • call method Activation Records 27
  • 29. Code Pattern dynamic method call caller • push object • push parameters left-to-right • call method virtual 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. Code Pattern return from method call callee • parameters in local variables • leave result on operand stack • return to caller Activation Records 28
  • 31. Code Pattern return from method call 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 Activation Records 28
  • 32. Implementation heap-based 3 frame data fac(3) 3 2 Activation Records 29
  • 33. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 Activation Records 29
  • 34. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 2 Activation Records 29
  • 35. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 Activation Records 29
  • 36. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 2 Activation Records 29
  • 37. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 2 2 Activation Records 29
  • 38. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 2 2 1 Activation Records 29
  • 39. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 2 1 Activation Records 29
  • 40. Implementation heap-based 3 2 1 frame frame frame data data data fac(2) fac(1) fac(3) 3 2 2 Activation Records 29
  • 41. Implementation heap-based 3 2 1 frame frame frame data data data fac(2) fac(1) fac(3) 3 2 1 2 Activation Records 29
  • 42. Implementation heap-based 3 2 1 frame frame frame data data data fac(2) fac(1) fac(3) 3 2 2 Activation Records 29
  • 43. Implementation heap-based 3 2 1 frame frame frame data data data fac(2) fac(1) fac(3) 3 2 1 2 Activation Records 29
  • 44. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 2 1 Activation Records 29
  • 45. Implementation heap-based 3 2 frame frame data data fac(2) fac(3) 3 2 2 Activation Records 29
  • 46. Implementation heap-based 3 frame data fac(3) 3 2 Activation Records 29
  • 47. 3 Implementation frame data stack-based fac(3) 3 2 Activation Records 30
  • 48. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) Activation Records 30
  • 49. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 Activation Records 30
  • 50. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) Activation Records 30
  • 51. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 Activation Records 30
  • 52. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 2 Activation Records 30
  • 53. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 2 1 Activation Records 30
  • 54. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 1 Activation Records 30
  • 55. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data Activation Records 30
  • 56. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data 1 Activation Records 30
  • 57. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data Activation Records 30
  • 58. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 1 frame fac(1) data 1 Activation Records 30
  • 59. 3 Implementation frame data stack-based fac(3) 3 2 frame data fac(2) 2 1 Activation Records 30
  • 60. 3 Implementation frame data stack-based fac(3) 3 2 Activation Records 30
  • 61. coffee break Activation Records 31
  • 62. II register-based machines Activation Records 32
  • 63. Registers x86 family general 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. Registers x86 family general 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 special purpose registers • segments SS, CS, DS, ES, FS, GS • flags EFLAGS Activation Records 33
  • 65. Stack and Stack Frames stack • temporary storage • grows from high to low memory addresses • starts at SS Activation Records 34
  • 66. Stack and Stack Frames 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 Activation Records 34
  • 67. Calling Conventions CDECL 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 Activation Records 35
  • 68. Calling Conventions CDECL caller push 21 push 42 • push parameters right-to-left on the stack call add _f ESP 8 • clean-up stack after call callee • save old BP • initialise new BP • save registers • return result in AX • restore registers • restore BP Activation Records 35
  • 69. Calling Conventions CDECL caller push 21 push 42 • push parameters right-to-left on the stack call add _f ESP 8 • clean-up stack after call callee • 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. Calling Conventions STDCALL 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 Activation Records 36
  • 71. Calling Conventions STDCALL caller push 21 push 42 • push parameters right-to-left on the stack call _f@8 callee • save old BP • initialise new BP • save registers • return result in AX • restore registers • restore BP Activation Records 36
  • 72. Calling Conventions STDCALL caller push 21 push 42 • push parameters right-to-left on the stack call _f@8 callee • 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. Calling Conventions FASTCALL caller • passes parameters in registers • pushes additional 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 Activation Records 37
  • 74. Calling Conventions FASTCALL caller mov ECX 21 mov EDX 42 • passes parameters in registers call @f@8 • pushes additional 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 Activation Records 37
  • 75. Calling Conventions FASTCALL caller mov ECX 21 mov EDX 42 • passes parameters in registers call @f@8 • pushes additional parameters right-to-left on the stack callee • 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. III optimisations revisited Term Rewriting 38
  • 77. Optimisations reasons • code overhead • execution overhead Activation Records 39
  • 78. Optimisations reasons • code overhead • execution overhead inlining • replace calls by body of the procedure • source code level Activation Records 39
  • 79. Optimisations reasons • code overhead • execution overhead inlining • replace calls by body of the procedure • source code level tail recursion • replace recursive calls by loops or jumps • source or machine code level Activation Records 39
  • 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. 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. 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. IV summary Term Rewriting 43
  • 84. Summary lessons learned stack frames in the Java Virtual Machine • parameter passing, returning results • implementation strategies Activation Records 44
  • 85. Summary lessons learned stack frames in the Java Virtual Machine • parameter passing, returning results • implementation strategies stack frames in register-based machines • registers x86 family • manipulating stack registers • calling conventions Activation Records 44
  • 86. Summary lessons learned stack frames in the Java Virtual Machine • parameter passing, returning results • implementation strategies stack frames in register-based machines • registers x86 family • manipulating stack registers • calling conventions optimisations Activation Records 44
  • 87. Literature learn more Java 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. Literature learn more Java 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. Outlook coming next imperative and object-oriented languages • Lecture 10: Dataflow Analysis Nov 13 • Lecture 11: Register Allocation Nov 20 • Lecture 12: Garbage Collection Nov 27 Lab Oct 25 • name analysis • name-based errors • type analysis for named elements • type-based errors Activation Records 46
  • 90. copyrights & credits Activation Records 47
  • 92. Pictures copyrights Slide 1: Framed by LexnGer, some rights reserved Slide 37: The Lemon Tree by Dominica Williamson, some rights reserved Slide 46: Oude Kerk by M.M. R, some rights reserved Activation Records 49

Editor's Notes