getting to know dmd and druntime

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Sun Jul 22 03:42:46 PDT 2012


On Sun, 22 Jul 2012 11:30:14 +0200
maarten van damme <maartenvd1994 at gmail.com> wrote:

> Right now I'm a bit confused. I assume that the garbage collector and
> some other parts from druntime need startup code. But what gets run
> first is my main method in the d file I compile.

Actually, that's just a clever illusion. Your main() method isn't
really the first thing called, the first thing called is a function in
druntime called dmain (or Dmain or _dmain or something like that, I
forget offhand). This function does all the initial stuff like calling
all the static/module constructors, initializing druntime including the
GC, some other stuff, and then actually calling *your* main().

IIRC, this dmain function is in object.d.

> Does this mean that the first call to something in druntime calls that
> startup code? If not, what does get ran first? how can I intercept
> this and run something else?
> When compiling without druntime the program will crash when
> initializing an object. How can I implement my own limited druntime
> that provides it's own version of the new expression?
> Druntime contains it's own trimmed down garbage collector that is most
> likely not very efficient. How can I use that one instead of the other
> more advanced one?
> Is the garbage collector embedded in druntime or is it somehow linked
> at compile time from a c-source (I found gc.c in the dmd source).
> What else gets linked? What exactly does dmd do? Which parts of
> druntime does it rely on?
> 
> I really want to get to know that part of D a bit better. Is this
> somewhere documented?

You can just modify and recompile druntime itself. Or, according to
Walter you should be able to replace any druntime function by simply
writing your own and including it when you compile. As long as the name
& function signature are correct, the linker apparently will just use
your version instead of the one in druntime.

Beyond that though, I don't really know anything more specific about
druntime or the GC. And I don't know whether there's any documentation
anywhere (you could try just browsing through the druntime sources or
searching around on the D wiki:
http://www.prowiki.org/wiki4d/wiki.cgi ). Someone more familiar with
druntime will have to answer those questions.

> Ideally I want to compile a d file without druntime (and thus with a
> very limited subset of D). Then, I want to start writing my own
> version of druntime (that is, providing the needed methods to get
> something more advanced working but stubbing everything out).
> 
> I came accross this blog
> https://www.semitwist.com/articles/article/view/d-on-gba-nds-progress-thanks-to-oopman
> but the link to the documentation seems to not be working (and judging
> by the date it will most likely be about D1).
> 

That's mine actually, and yea it's super-old and completely out of date
(It was indeed a very early D1 - before D2 even existed. And in fact,
it's referring to the old, old, OLD GDC project, not same GDC we have
now.)

Back then, the stuff we now call druntime was actually part of Phobos
(with a separate incompatible version in Tango), and I never did get it
working with DevKitARM (which I think is called DevKitPro now). What's
there is as far as I ever got with it. Of course, at the time (and even
today, too) I didn't know a damn thing about the internals of GCC, GDC,
DevKitARM or, uhh, what was to become druntime. I was just compiling
GDC/DevKitARM, didn't really even touch any of the code inside.



More information about the Digitalmars-d-learn mailing list