- 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:
- all parameters use 4 bytes on the stack, or a multiple of 4 (BP:2)
- all procedures must preserve the contents of registers ebx, ecx, edx, ds,
and es!
- the direction bit should be cleared after the exit from a procedure, if it
has been modified by the procedure.
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_pascal | Left-to-right | Function | No |
arg_cdecl | Right-to-left | Caller | No |
arg_stdcall | Right-to-left | Function | No |
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.
- 2.1.2. -