Parameter Passing

Stony Brook Compilers do not guarantee the process of passingparameters to procedures, unless:

Two methods are used to pass parameters to procedures:

The method used to pass a specific parameter is determined bythe data type of the parameter, and whether or not it is a reference parameter.  Thefollowing conditions determine the method used:

Some languages (notably C) always pass parameters by value.  Others(like FORTRAN) always pass parameters by address.  When interfacing to suchlanguages, you must take this into account when declaring the procedure.

Passing a pointer by value is equivalent to passing data of thetype it refers to by address.  In C, the effect of Modula-2 VAR, or Ada out/inout parameters is achieved by passing pointers.  If you are interfacing witha C procedure, you should determine whether you really want to pass a pointer orwhether you should translate it to an address parameter type.

The CPU Word

In order to more clearly discuss parameter passing with 16-bitand 32-bit compilers, we introduce the concept of a CPU word.  In order to efficientlypass parameters (put things on the processor stack), all items are passed as a multipleof the CPU word size.  CPU word sizes are as follows:

Address Parameters

Address parameters of all types are passed the same way.  Theaddress passed can be either near or far addresses.

Value Parameters

Value parameters are passed by pushing onto the stack the actualvalue of the data that is being passed.  At least one CPU word is pushed foreach parameter passed. The size of the value parameter passed is determined by thetype of the formal parameter, and not by the type of the actual parameter.  Forexample, you may be able pass an 8 byte integer value to a procedure that definesa formal parameter as a 4 byte integer value.  Language assignment rules applyhere. In this case, the length of the 4 byte integer type is pushed onto the stack,not the length of 8 byte integer.  The 8 byte integer value is truncated toa 4 byte integer value.

Value parameters are passed as follows:

IA-32

SPARC

Modula-2 Open array parameters

Open array parameters have HIGH bounds passed along with thearray address. The HIGH bounds are passed by value, have a type of CARDINAL, andthe highest array dimension is passed first. This means that the first array dimensionHIGH bound is the last passed and has the lowest address on the stack and is passedjust before the array address. Therefore HIGH bounds are found above the array addresson the stack and the first array dimension HIGH bound has the lowest address.

For example with a left to right parameter pass.

PROCEDURE Demo(first : CARDINAL; twoDim : ARRAY OF ARRAY OF CARDINAL);

  1. first <= highest stack address
  2. HIGH of dimension 2 of twoDim
  3. HIGH of dimension 1 of twoDim
  4. twoDim Address <= lowest stack address

Ada95 unconstrained array parameters

Unconstrained array parameters have their 'First and 'Last boundspassed along with the array address. The bounds are passed by value as an Integer,'Last is passed before 'First, and the highest array dimension is passed first. Thismeans that the first array dimension bound is the last passed and has the lowestaddress on the stack and is passed just before the array address. Therefore boundsare found above the array address on the stack and the first array dimension bound'shave the lowest address, and 'First has a lower address than 'Last.

For example with a left to right parameter pass.

TYPE TwoDimArray is array (1..10, 1..10) of Natural;

Procedure Demo(first : natural; twoDim : TwoDimArray);

  1. first <= highest stack address
  2. 'Last of dimension 2 of twoDim
  3. 'First of dimension 2 of twoDim
  4. 'Last of dimension 2 of twoDim
  5. 'First of dimension 2 of twoDim
  6. twoDim Address <= lowest stack address

Order of parameter passing

By default the compiler uses the StonyBrook calling convention.See Procedure attributes for detailed information.

Register parameter passing (IA-32 only)

Using procedure attributes you canforce the compiler to pass parameters in specific registers. This is not allowedfor procedures implemented in Modula-2. You can only use this for procedures implementedin Assembly code, or external procedures implemented in some other language or system.You can register parameter pass to Assembler and PureAsm procedures, since theseare not Modula-2 procedures.

Returning Values

Function procedures are procedures that return a value to thecaller.  The value of a function is returned either in registers or on the stack,depending on the data type.

The compiler uses the following registers to return values ofother data types:

IA-32

SPARC