Replacing C's memcpy with a D implementation

Mike Franklin slavo5150 at yahoo.com
Tue Jun 12 04:37:07 UTC 2018


On Monday, 11 June 2018 at 18:34:58 UTC, Johannes Pfau wrote:

> I understand that you actually need to reimplement memcpy, as 
> in your microcontroller usecase you don't want to have any C 
> runtime. So you'll basically have to rewrite the C runtime 
> parts D depends on.
>
> However, I think for memcpy and similar functions you're 
> probably better off keeping the C interface. This directly 
> provides the benefit of compiler intrinsics/optimizations. And 
> marking memcpy as nothrow/pure/ system/nogc is simple* either 
> way. For the D implementation, the compiler will verify this 
> for you, for the C implementation, you have to mark the 
> function depending on the C implementation. But that's mostly 
> trivial.

My plans go beyond microcontrollers.  Mostly, I'd like to be able 
to use more features of D without having to link in a pre-built 
runtime.  This is especially convenient for cross-compiling 
scenarios.  By replacing the runtime hooks with templates, and 
the software building blocks with D implementations, we'd no 
longer need to obtain a C toolchain and a pre-compiled druntime 
library just  to get a build for our target.  We'd just need a 
cross-compiler like LDC or GDC.  After adding druntime to the 
import path, we'd have everything we need for our target.  Try, 
today, to create an LDC cross-compiler for a Windows Host and an 
ARM target like the Raspberry Pi.  You'll quickly realize what a 
hassle it all is.

Another issue with memcpy being generated by compilers is noone 
knows what it's actually doing without building and looking at 
the assembly.  And the assembly will change based what's being 
built.  Have you tried to look at the GCC implementation.  I 
have, and I never want to again.  If the implementation were 
templated in D, we'd be able to see exactly what code would be 
generated when by simply viewing the D runtime source code.  It'd 
be easier to understand, predict, port, and enhance.

There isn't one single game-changing benefit this endeavor would 
bring, and I guess that's why it's difficult to comprehend, but 
collectively, all the little things it would enable would make it 
quite compelling.

Mike



More information about the Digitalmars-d mailing list