A betterC base

Mike Franklin slavo5150 at yahoo.com
Thu Feb 8 12:55:09 UTC 2018


On Thursday, 8 February 2018 at 11:06:15 UTC, ixid wrote:
> How difficult would it be for D at this point to move towards a 
> pay for what you use system that out of the box is betterC and 
> requires the garbage collector to be explicitly imported?

I'm not sure if this is what you're looking for, but I've been 
trying to work on something like that, and have successfully 
submitted a few PRs:

Opt-in ModuleInfo
https://github.com/dlang/dmd/pull/7395
https://github.com/dlang/dmd/pull/7768

Opt-in Throwable
https://github.com/dlang/dmd/pull/7786

Opt-in TypeInfo
https://github.com/dlang/dmd/pull/7799 (not yet merged; someone 
please review it)

With all of the above PRs merged, the compiler will no longer 
complain about the above missing runtime features if your code 
doesn't use them.  It also allows one to create really small 
binaries with no dependencies.

Example 1
=========

object.d
--------
module object;

private alias extern(C) int function(char[][] args) MainFunc;
private extern (C) int _d_run_main(int argc, char** argv, 
MainFunc mainFunc)
{
     return mainFunc(null);
}

main.d
------
void main() { }

dmd -conf= -defaultlib= main.d object.d -of=main
size main
    text	   data	    bss	    dec	    hex	filename
    1403	    584	     16	   2003	    7d3	main


Example 2
This will avoid linking in the C standard library and C runtime.  
But you have to provide your own replacements.
=========

object.d
--------
module object;

extern(C) void __d_sys_exit(long arg1)
{
     asm
     {
         mov RAX, 60;
         mov RDI, arg1;
         syscall;
     }
}

extern void main();
private extern(C) void _start()
{
     main();
     __d_sys_exit(0);
}

main.d
------
void main() { }


dmd -c -lib main.d object.d -of=main.o
ld main.o -o main
size main
    text	   data	    bss	    dec	    hex	filename
      56	      0	      0	     56	     38	main

If you are creating a library to be consumed by another language, 
you just need to add an empty object.d file in your current 
directory.  I tried to remove that silly limitation, but it met 
resistance: https://github.com/dlang/dmd/pull/7825

I have a changelog PR describing all this at 
https://github.com/dlang/dmd/pull/7829, with the intention of it 
being available in the next DMD release, but I need my other PRs 
reviewed and merged before I can move forward.

This is just the tip of the iceberg, though.  After this stage, I 
would like to start tackling the overuse of TypeInfo in the 
coupling between the compiler and the runtime.  See this comment 
(https://issues.dlang.org/show_bug.cgi?id=18312#c2) for more 
about what I mean there.

Mike


More information about the Digitalmars-d mailing list