Passing parameters and the return value using registers

When a compiler processes a function definition:

  int f( int[] A, int a, int b )           
  {
     ....
  }
   

When registers are used for parameter passing, the compiler will:

  • Remember the name and data type of each parameter:

  • Designate (= assign/reserve) a register for the parameter

    Example:

     Function f:
    
             Parameter Name     Data Type     Location
           ------------------------------------------------
       0           A              int[ ]        r0
       1           a              int           r1
       2           b              int           r2
    

Note:   the return value is usually returned in register r0 by compilers - we will adopt this.

How is header information used ?

The information in the function header is used to:

  • Map the variables in the function body to its corresponding designated registers

  • Use the designated registers as operands in the translation of the function body

Example: since the parameter a was designated as register r1, a statement using variable a will use register r1

  int f( int[] A, int a, int b )           
  {
      ....
                    Translated as
      a = a + 1;  ---------------->   add  r1, r1, #1

      ....
  }