[fpc-devel] OO rewrite - technical questions

Nikolai Zhubr n-a-zhubr at yandex.ru
Sat Jul 24 13:42:46 CEST 2010


24.07.2010 6:55, Hans-Peter Diettrich:

>IMO the segment register is used implicitly in thread API calls, with
>no further use by application code.

Exactly the opposite (at least delphi on windows). See delphi's RTL.

 >In the "Using Thread Local Storage" entry in the MSDN library this 
 >value is the handle of a memory block, created with LocalAlloc.

> FS and GS may be used for special (OS, SEH and TLS) management, they are
> useless in application code.

fs and gs whould indeed hardly ever be touched by application programmer 
directly (by hand), however (as it was already metioned several times 
earlier) delphi (on windows) and gcc (supposedly both windows and linux) 
use them to implement this "special management" so as exceptions and 
threadvars can be actually used without explicitely using OS APIs.

In such case of course fs/gs will not map to the same flat address 
segment (as cs, ds, es, ss) common for the whole process and all of its 
threads. Instead, it will map so a special segment created by OS for a 
specific thread. This segment might also be accessible as some other 
offset in the flat address space, or might not, but it isn't the point. 
Point is that a thread's code basically just needs to do a segment 
override (add 1-byte opcode modifier on x86 AFAIK) each time it wishes 
to get into its instance-unique data block, and this is supposed to be 
very inexpensive compared to OS syscalls. Also note, that fs/gs are not 
much usefull for anything else in an application (can not do any 
arithmetics, can not index flat offsets directly, etc etc), so they 
would otherwise be most probably totally unused (wasted) which is kind 
of pity.

 >As long as the base values are immutable, the equivalent absolute 
 >addresses can be used instead (in contrast to PIC).

Sure. But, as discussed before, all threads share the same code (i.e. 
the offset can not be hardcoded). Therefore, you'll need some register 
to access the instance-specific value (unless you are going to make OS 
calls every time). Probably on any arcitecture some general-purpose 
register capable of holding address offsets can be used for that. Such 
register would then have to be mostly excluded from normal use in the 
application (in other words - mostly wasted). Now, on x86/x86_64 there 
is yet additional tricky option: imploy a segment register, which would 
otherwise be unused. This way no general-purpose register is wasted and 
no OS syscalls are necessary either.

>Consider a threadvar as a field of a related thread object (instance).

This might be fine most of the time (for end user in particular), 
however there still might be some non-OO code which it is 
impossible/impractical to convert into object-oriented model. See FPC RTL.

HTH.
Nikolai




More information about the fpc-devel mailing list