Is the D language completely dependant upon the D runtime?
Asked Answered
B

2

8

Lately, I've been studying on the D language. I've always been kind of confused about the runtime.

From the information I can gather about it, (which isn't a whole lot) I understand that it's sort of a, well, runtime that helps with some of D's features. Like garbage collection, it runs along with your own programs. But since D is compiled to machine code, does it really need features such as garbage collection, if our program doesn't need it?

What really confuses me is statements such as:

"You can write an operating system in D."

I know that you can't really do that because there's more to an operating system than any compiled language can give without using some assembly. But if you had a kernel that called D code, would the D runtime prevent D from running in such a bare-bones environment? Or is the D runtime simpler than that? Can it be thought of as simply an "automatic" inclusion of sourcefile/libraries, that when compiled with your application make no more of a difference than writing that code yourself?

Maybe I'm just looking at it all wrong. But I'm sure some information on the subject could do a lot of people good.

Balthasar answered 9/11, 2012 at 10:8 Comment(0)
P
13

Yes, indeed, you can implement the functions of DRuntime that the compiler expects right in your main module (or wherever), compile without a runtime, and it'll Just Work (tm).

If you just build your code without a runtime, the compiler will emit errors when it's missing a symbol that it expects to be implemented by the runtime. You can then go and look at how DRuntime implements it to see what it does, and then implement it in whatever way you prefer. This is what XOmB, the kernel written in D (language version 1, though, but same deal), does: http://xomb.net/index.php?title=Main_Page

A lot of DRuntime isn't actually used by many applications, but it's the most convenient way to include the runtime components of D into applications, so that's why it's done as a static library (hopefully a shared library in the future).

Paestum answered 9/11, 2012 at 10:14 Comment(5)
Thanks! Great info. my overall outlook on D just went up a few points. I cant understand why D hasnt gained more ground. Its a wonderfull language.Balthasar
@kbzombie: It's because it's still very buggy.Stradivarius
I did a little file that works without a runtime just to see how small we can go: arsdnet.net/dcode/minimal.d You can compile that on linux (see the comment at the top of the file) and get a tiny program that works without druntime. However, as you go into more of D's features, you've gotta bring in more and more runtime functions. But you can go reasonably far with very little if you take the time to make it happen.Smoking
and here's one that gives a few more functions, command line args, environment variables, and structs: arsdnet.net/dcode/minimal.zipSmoking
would any one be up to giving a crash course on setting up and compiling a bare-bones D app in windows? Then we can just keep on adding implementations. Adam D. Ruppe has a great example. But im struggling on getting started here.Balthasar
A
6

It's pretty much the same as C and C++ I expect. The language itsself compiles to native code and just runs. But there is some code that is always needed to set everything up to run your program, for example processing command line parameters.

And some more complex language facilities are better implemented by calling some standard code rather than generating the code everywhere it is used. For example throwing an exception needs to find the relevent handler function. No doubt the compiler could insert the code to do there everywhere it was used, but it's much more sensible to write the code in a library and call that. Plus there are many pre-written library functions in the standard library.

All of this taken together is the runtime.

If you write C you can use it to write an operating system because you can write the startup code yourself, you can write all the code for handing memory allocation yourself, you can write all the code for standard functions like strcat yourself instead of using the provided ones in the runtime. But you'd not want to do that for any application program.

Alten answered 9/11, 2012 at 10:15 Comment(1)
Even more great info! Thanks!Balthasar

© 2022 - 2024 — McMap. All rights reserved.