Sign in Join
FPDoc Onlineexperiment

Name mangling

Contrary to most C compilers and assemblers, all labels generated to pascal variables and routines have mangled names5. This is done so that the compiler can do stronger type checking when parsing the Pascal code. It also permits function and procedure overloading.

Mangled names for data blocks

The rules for mangled names for variables and typed constants are as follows:

  • All variable names are converted to upper case.

  • Variables in the main program or private to a unit have an underscore (_) prepended to their names.

  • Typed constants in the main program have a TC__ prepended to their names.

  • Public variables in a unit have their unit name prepended to them : U_UNITNAME_

  • Public and private typed constants in a unit have their unit name prepended to them :TC__UNITNAME$$

  • Do not use public names that start with FPC_, because FPC uses these for internal system routines and constants.

Examples:

unit testvars;

interface

const
 publictypedconst : integer = 0;
var
 publicvar : integer;

implementation
const
 privatetypedconst : integer = 1;
var
 privatevar : integer;

end.

Will result in the following assembler code for the GNUassembler :

    .file "testvars.pas"

.text

.data
# [6] publictypedconst : integer = 0;
.globl  TC__TESTVARS$$_PUBLICTYPEDCONST
TC__TESTVARS$$_PUBLICTYPEDCONST:
    .short  0
# [12] privatetypedconst : integer = 1;
TC__TESTVARS$$_PRIVATETYPEDCONST:
    .short  1

.bss
# [8] publicvar : integer;
    .comm   U_TESTVARS_PUBLICVAR,2
# [14] privatevar : integer;
    .lcomm  _PRIVATEVAR,2

Mangled names for code blocks

The rules for mangled names for routines are as follows:

  • All routine names are converted to upper case.

  • Routines in a unit have their unit name prepended to them : _UNITNAME$$_

  • All Routines in the main program have a _ prepended to them.

  • All parameters in a routine are mangled using the type of the parameter (in uppercase) prepended by the $ character. This is done in left to right order for each parameter of the routine.

  • Objects and classes use special mangling : The class type or object type is given in the mangled name; The mangled name is as follows: _$$_TYPEDECL_$$ optionally preceded by mangled name of the unit and finishing with the method name.

  • Do not use public names that start with FPC_, because FPC uses these for internal system routines and constants.

The following constructs

unit testman;

interface
type
  myobject = object
   constructor init;
   procedure mymethod;
  end;

implementation

constructor myobject.init;
begin
end;

procedure myobject.mymethod;
begin
end;

function myfunc: pointer;
begin
end;

procedure myprocedure(var x: integer; y: longint; z : pchar);
begin
end;

end.

will result in the following assembler file for the Intel 80x86 target:

    .file "testman.pas"

.text
    .balign 16
.globl  _TESTMAN$$_$$_MYOBJECT_$$_INIT
_TESTMAN$$_$$_MYOBJECT_$$_INIT:
    pushl   %ebp
    movl    %esp,%ebp
    subl    $4,%esp
    movl    $0,%edi
    call    FPC_HELP_CONSTRUCTOR
    jz  .L5
    jmp .L7
.L5:
    movl    12(%ebp),%esi
    movl    $0,%edi
    call    FPC_HELP_FAIL
.L7:
    movl    %esi,%eax
    testl   %esi,%esi
    leave
    ret $8
    .balign 16
.globl  _TESTMAN$$_$$_MYOBJECT_$$_MYMETHOD
_TESTMAN$$_$$_MYOBJECT_$$_MYMETHOD:
    pushl   %ebp
    movl    %esp,%ebp
    leave
    ret $4
    .balign 16
_TESTMAN$$_MYFUNC:
    pushl   %ebp
    movl    %esp,%ebp
    subl    $4,%esp
    movl    -4(%ebp),%eax
    leave
    ret
    .balign 16
_TESTMAN$$_MYPROCEDURE$INTEGER$LONGINT$PCHAR:
    pushl   %ebp
    movl    %esp,%ebp
    leave
    ret $12

Modifying the mangled names

To make the symbols externally accessible, it is possible to give nicknames to mangled names, or to change the mangled name directly. Two modifiers can be used:

public:

For a function that has a public modifier, the mangled name will be the name exactly as it is declared.

alias:

The alias modifier can be used to assign a second assembler label to your function. This label has the same name as the alias name you declared. This doesn’t modify the calling conventions of the function. In other words, the alias modifier allows you to specify another name (a nickname) for your function or procedure.

The prototype for an aliased function or procedure is as follows:

Procedure AliasedProc; alias : 'AliasName';

The procedure AliasedProc will also be known as AliasName. Take care, the name you specify is case sensitive (as C is).

Furthermore, the exports section of a library is also used to declare the names that will be exported by the shared library. The names in the exports section are case-sensitive (while the actual declaration of the routine is not). For more information on the creating shared libraries, chapter Programming shared libraries.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.