SlideShare une entreprise Scribd logo
1  sur  92
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

Contenu connexe

Plus de 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 CollectionGuido 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 AnalysisGuido 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 AlgorithmsGuido 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 AllocationGuido 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 AnalysisGuido 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 RegainedGuido Wachsmuth
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesGuido 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 CheckingGuido Wachsmuth
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingGuido Wachsmuth
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesGuido Wachsmuth
 

Plus de 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
 
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
 
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
 
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
 
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
 
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
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation
 
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
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
 
Software Languages
Software LanguagesSoftware Languages
Software Languages
 

Dernier

How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Dernier (20)

How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

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

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n
  106. round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  107. \n
  108. \n