SlideShare ist ein Scribd-Unternehmen logo
1 von 92
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxKatherine Villaluna
 

Kürzlich hochgeladen (20)

How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
 

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

Hinweis der Redaktion

  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