cycle dependencies

Steven Schveighoffer schveiguy at yahoo.com
Thu May 31 15:15:44 UTC 2018


On 5/31/18 2:14 AM, Simen Kjærås wrote:
> On Wednesday, 30 May 2018 at 20:57:32 UTC, DigitalDesigns wrote:
>> Why is this a runtime issue? It is not as if the execution of static 
>> this are non-deterministic. The compiler and linker must order the 
>> calls in some way. Maybe this is what you mean by own object/linker?  
>> But even then, they would only have to be checked once so why check 
>> every time the exe is ran when once it is ran it must remain 
>> statically oriented for all future.
> 
> Because of separate compilation, the compiler can't do it. Because the 
> generic linker doesn't do that sort of thing, the linker can't do it.
> 
> The first part is essentially intractable - e.g. module A's static this 
> uses a global variable in module B that's set by module C. Module A may 
> be compiled separately from module C, so the compiler can't see the 
> dependency.
> 
> If the linker is to do it, the compiler needs to encode the information 
> in the object file, and the linker must be made specially to support 
> this. Maybe this could be put in some optional section in the object 
> file, and linkers that don't support it would just ignore the 
> information. If a non-compliant linker is used, the runtime needs to 
> have a fallback, so even this won't get us entirely out of the woods. 
> Only supporting special linkers comes with its own set of problems.
> 

Yes, this is really the crux of it.

I want to stress that the cycle detection is not really for the sake of 
detecting cycles, it's because you need to sort the modules in the order 
their static ctors should be called. When you can't sort them in an 
order, you have to call them in an arbitrary order (probably just in the 
order they were linked).

It is really a shame we have to do this at runtime, but that's due to 
the compilation model we are stuck with, and the linker tools we deal with.

However, once the binary is formed, it's possible we could *edit* the 
binary to hard-code the order -- at that point, everything is known. I 
have thrown that out there in terms of a nice project someone with the 
correct skills could do: 
https://forum.dlang.org/post/nl3du7$1as5$1@digitalmars.com

One


More information about the Digitalmars-d mailing list