[fpc-devel] Proposal: Supporting initial references for weakexternal

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Feb 17 13:52:59 CET 2011


On 16 Feb 2011, at 00:42, Jeppe Johansen wrote:

> I don't know what you mean about the high level semantics.

Defining the syntax according to high level semantics (which is what  
GCC does, and what I also want to do in FPC):

  extern int test(void) __attribute__((weak, alias ("xxx")));
and
  function test: cint; cdecl; weak; external name 'xxx';

Both mean "references to the C/Pascal symbol 'test' should be  
translated into weak references to the symbol 'xxx'". When removing  
"weak", the semantics stay identical except that the reference is no  
longer weak. In other words, the syntax is orthogonal. It's high level  
because this syntax completely ignores what happens at the assembler  
level with the different high level constructs.


Defining the syntax according to low level semantics, which is what  
you propose:
* the consequence at the assembler level on ELF platforms of calling  
"test" as declared above but *without* "weak", is a reference to an  
assembler symbol with the name 'xxx' (so the reference goes to the  
symbol whose name matches whatever comes after 'name')
* when adding "weak", references to the language symbol must keep  
going to a symbol with name 'xxx' at the assembler level, because  
that's what happens at the low level if "weak" is not specified
* therefore, in the case "weak" is specified, the 'xxx' can no longer  
be used to specify the external function that is (weakly) referenced  
by this declaration. Instead, it must specify the name of the  
assembler level symbol defined by the ".set" directive that's  
generated for this weak declaration (since that's where references to  
this weak symbol will go to)
* as a result, we need some extra modifier (default in your proposal)  
to define the external function that is weakly referenced by this  
declaration

And since simply removing "weak" from a declaration does not turn it  
into an equivalent normal external declaration (since those don't have  
a "default" modifier), it is not orthogonal. It's low level because  
it's completely based on how things work at the assembler level.

> I don't think forcing cdecl is a good idea. I think the optimal case  
> would be that weak procedures should just be called by their real  
> name in the default calling convention. I know this is a bit hard to  
> implement(I still haven't found out where that magic happens)


It is not that hard to implement, but that will definitely not be  
done. Adding special cases like that is simply not how programming  
languages can be designed. It makes both the compiler more complex  
(special cases to implement the exceptions) and learning the language  
harder (special cases that are not explicit in the syntax and which  
the compiler therefore cannot check, and which you just have to  
"know"). A small typing convenience (not having to add "cdecl") is no  
reason to change the normal behaviour of the compiler for a particular  
feature.


Jonas



More information about the fpc-devel mailing list