Previous: 2.1.1. Memory Organization To the Table of Contents Next: 2.1.3. Limitations
2.1.1. Memory Organization Table of Contents 2.1.3. Limitations

- 2.1.2. -
Table of Contents
2. TMT Pascal Language Description
2.1. Implementation Issues
2.1.2. Calling Conventions


2.1.2. Calling Conventions


Calling conventions match those in Borland Pascal with the following differences: To call external procedures written for different languages, TMT Pascal provides the conv operator. The conv operator should be used in the function (procedure) declaration to define a calling convention, which in turn will be used to call a declared function (procedure).

Syntax:
  [function] conv conv_method FunctionName [Arguments] : ReturnType;
Where conv_method is a constant, which defines the calling conversion to be used. The System units contains the following constants to define conventional method:
const
// Base calling conventions to construct any possible convention
  arg_reverse    = [0];
  arg_proc_16    = [2];
  arg_noregsave  = [3];
  arg_no_drop_1  = [4];
  arg_no_drop_2  = [5];
  arg_no_drop_3  = arg_no_drop_1 + arg_no_drop_2;
  arg_no_drop_4  = [6];
  arg_no_drop_5  = arg_no_drop_1 + arg_no_drop_4;
  arg_no_drop_6  = arg_no_drop_2 + arg_no_drop_4;
  arg_no_drop_all= [4..6];
  arg_IO_test    = [8];
  arg_save_edi   = [9];
  arg_save_esi   = [10];
// Composite calling conventions
  arg_pascal     = arg_noregsave;
  arg_stdcall    = arg_reverse + arg_noregsave + arg_save_edi +
                   arg_save_esi;
  arg_cdecl      = arg_reverse + arg_no_drop_all;
  arg_os2        = arg_cdecl + arg_noregsave;
  arg_os2_16     = arg_proc_16 + arg_no_drop_all + arg_noregsave;
The arg_pascal convention passes parameters from left to right, that is the leftmost parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last. The arg_cdecl, arg_stdcall, arg_os2 and arg_os2_16 conventions pass parameters from right to left. For all conventions except arg_cdecl, the procedure or function removes parameters from the stack upon returning.

With the arg_cdecl convention, the caller must remove parameters from the stack when the call returns. The register convention uses up to three CPU registers to pass parameters, whereas the other conventions always pass all parameters on the stack.

The calling conventions are summarized in the following table.
 Directive  Order  Cleanup  Registers 
arg_pascalLeft-to-rightFunctionNo
arg_cdeclRight-to-leftCallerNo
arg_stdcallRight-to-leftFunctionNo
The arg_pascal and arg_cdecl conventions are mostly useful for calling routines in dynamic-link libraries written in C, C++, or other languages. The arg_stdcall convention is used for calling Windows API routines.


Previous: 2.1.1. Memory Organization To the Table of Contents Next: 2.1.3. Limitations
2.1.1. Memory Organization Table of Contents 2.1.3. Limitations

- 2.1.2. -