dlang / project-ideas

Collection of impactful projects in the D ecosystem
36 stars 12 forks source link

Replace Runtime Hooks with Templates #25

Closed burner closed 9 months ago

burner commented 5 years ago

D’s betterC mode is an important tool to be able to use D on bare-metal and embedded platforms. By disabling, for example the class support, the compiles does not need to as many runtime functions and types to be implemented for the code to compile and link. This makes the life of a bare-metal developer much easier.

But there is a catch! A lot of the language features runtime hooks, which in turn requires the TypeInfo class to be able to function. One way of solving both of these problems is by moving runtime hooks to use templates instead. This solves the betterC by removing the dependency of classes (the TypeInfo class), and it solves the safety issue because now the compiler will have all the information about the hook and it can verify it itself and not just trust that the runtime developer remembered to mark the hook correctly.

This proposal will work on translating all the array hooks from using the TypeInfo class to templates.

Point of contact: @RazvanN7

burner commented 5 years ago

this is being worked on by Dan Printzell during GSoC 2019

wilzbach commented 5 years ago

CC @vild

RazvanN7 commented 4 years ago

@Vild maybe you can share what is the status of this project so that future contributors know where to pick it up from.

Vild commented 4 years ago

I haven't had time to work on this for a long while now due to school, so I haven't been able to finish my "information extraction" document, but most important parts are there: https://github.com/Vild/GSOC2019/blob/master/TechnicalInformationExtraction/TechnicalInformationExtraction.org

Generally the changes I've done contains hacks around security features. For example:

size_t a() pure {
    int[] a;
    a.length = 10;
    return a.length;
}

The array lengths setting is lowered to _d_arraysetlengthT!int, and because this hook is called inside a pure function, the hook also needs to be pure. So I had to fake pureness when needed, but I also need to make sure that the compiler won't remove the function call. As function calls to pure functions, where you don't care about the return value can be removed. (More info on this is on the document above). I've also had to fake @safety to make sure I don't break code.

But even with all of this hacks it does not decrease the safety of how it was before, as the underlining code is (often) the same.

I want to continue on my WIP druntime & dmd PRs, but I don't know when I will have time to invest on fixing them. So anyone can feel free to continue working on them.

Generally what you need to do to translate a hook:

I would guess that most hook (if not all) should be moved to use a template entrypoint, that still calls the old hook function, before trying to rewrite the hook code. As there is a lot of usage of typeid within the old it is basically impossible to change everything to be template functions without making huge PR that change way too much. It is probably better to start at the top (the hook entrypoint) and then move downwards (the functions it calls), where each step is its own PR. And then when everything is done you can start removing the pure and @safe hacks to make sure that hooks are actually safe. It is much easier to do this later than at the beginning of the translation process.

mdparker commented 9 months ago

Given that this project has been ongoing for a few years now and is nearly completed, there's no reason for it to be on this list anymore.