CSE 131
Quiz 4 Review

Arrays
  Memory layout in C vs. Java (arrays are real objects)
  Multidimensional arrays vs. Row-Pointer layout (array of pointers)
    Traversal pointer
  Storage map equation / address calculation for code gen
  a[i] ==> *(a + i)   - scalar pointer arithmetic

  Passing arrays to functions
    How to pass entire array by value

  Legal operations on pointers vs. legal operations on arrays

Pointers/References
  Scalar arithmetic on pointers
  References as pointers or handles
  Explicit memory management vs. automatic garbage collection
    When C++ ctors/dtors are automatically invoked

Runtime Environments
  Subroutines
    Open / Closed / Leaf subroutines
    Stack Frame / Activation Record
    Calling Sequence / Calling Convention
      Tasks done on the way IN
      Tasks done on the way OUT
      Prologue / Epilogue
      Caller's responsibility / Called function's responsibility

              .global main
              .section ".text"

      main:
              save  %sp, -(92 + Bytes_Of_Local_Variables) & -8, %sp
                                          ! Allocate space for local vars
                                          ! and preallocate 92 bytes for
                                          ! future function calls.
                                          ! Slide Reg Window down 16 regs.

              ! Copy actual parameters out
              mov   %l2, %o0              ! Use mov for reg->reg
              set   420420, %o1           ! Use set for constant->reg
              set   a, %o2                ! Use set for label/address->reg
              call  foo                   ! Saves %pc->%o7 (return address)
              nop                         ! Delay slot

              mov   %o0, %l3              ! Retrieve the return value

              ...

              mov   %l0, %i0              ! Put return value in %i0

              ret                         ! Retrieve return addr from %i7->%pc
              restore                     ! Slide Reg Window back up 16 regs


    SPARC sliding register window
      Pass parameters
      Local variables
      Return value

  C/C++ Runtime Environment

SPARC architecture specifics
  in / local / out registers (register window sets)
  Parameter passing     %oX  --> %iX
  Return value          %o0 <--  %i0
  Simple local vars may be mapped to local registers %l0-%l7

  Stack Frames          local3          %fp-12
                        local2          %fp-8
                        local1          %fp-4
                %fp --->
                         ...
                        param1          %fp+68  ! if real memory
                        param2          %fp+72  ! locations needed.
                         ...
                        param6          %fp+88


  Loops
  -----
    Many ways to implement loops

                ba    test
                nop
        loop:
                ...
        test:
                cmp   ___, ___
                b__   loop                      ! positive logic
                nop

  --------------------------------------------

        loop:
                cmp   ___, ___                  ! opposite logic
                b__   endloop
                nop

                ...

                ba    loop
                nop
        endloop:

  --------------------------------------------

  How real compilers set up loops:

                cmp   ___, ___
                b__   endloop                   ! opposite logic
                nop
        loop:
                ...

                cmp   ___, ___
                b__   loop                      ! positive logic
                nop
        endloop:

--------------------------------------------------------------------------

  Conditionals      if / if-else
  ------------

                cmp   ___, ___
                b__   endif                     ! opposite logic
                nop
                  ...
        endif:

  --------------------------------------------

                cmp   ___, ___
                b__   else                      ! opposite logic
                  ...
                ba    endif                     ! jump over else code
                nop
        else:
                  ...
        endif:

  Logical AND and OR
    Short-circuit evaluation

Local variable allocation on the runtime Stack
  save %sp, -(92 + local-var-space) & -8, %sp

      ld [%fp-X], %l0
      st %l0, [%fp-X]

How real compilers set up global and static variable allocation
in the Data/BSS segments

      .global a, b, s   ! a, b, and s are global
			! could have been generated individually with each def.

      .section ".data"

      .align 4
  a:  .word 15          ! int a = 15;

      .align 8
  b:  .double 1.23      ! double b = 1.23;

      .section ".bss"

      .align 4          ! uninitialized
  c:  .skip 40          ! static int c[10]; -- not listed in .global directive

      .section ".rodata"

  s:  .asciz "Boo!\n"   ! char s[] = "Boo!\n";

        ...

      set   a, %l0      ! address of global var &a -> %l0
      ld    [%l0], %l1  ! dereference address to get the value of a
                        !   a -> %l1
        ...

      set   5678, %l2
      set   a, %l0
      st    %l2, [%l0]  ! a = 5678

Other ways of implementing global variables in your Reduced-C compiler
  Global pointer in a global register pointing to space allocated in Data/BSS
    (either + or - offsets from this global register)
  
  Global pointer in a global register pointing to space allocated in
    Local Variable section of main's Stack Frame
    (probably just a - offset from this global register like local offsets
     from %fp)

  Right-Left Rule


Subroutine Parameter Passing (Calling Convention)
  Call-by-Value

  Call-by-Reference
    VAR, &, ref

  const / READONLY parameters

  in parameters

  out parameters
    call-by-reference
    call-by-result

  in out parameters
    call-by-reference
    call-by-value/result

  Compiler-generated subroutine signatures for subroutine calls
    - subroutine overloading quiz

  default parameters, variable number of arguments

  setjmp/longjmp

  recursion