[Dlang-internal] Detect CTFE in AssignExp:semantic

Andrei Alexandrescu via Dlang-internal dlang-internal at puremagic.com
Wed Jan 11 10:59:29 PST 2017


On 1/11/17 7:02 PM, Martin Nowak wrote:
> Don't really understand your question. What are the 2 problems you refer
> to?

The current implementation:

D _d_arraycopyT(S, D)(S from, D to, uint size)
{
     import core.stdc.string;
     (() @trusted => memcpy(cast(void*)to.ptr, from.ptr, to.length * 
size))();
     return to;
}

The current error:

ctfe.d(28): Error: 2 variable __r57 cannot be read at compile time
ctfe.d(28):        called from here: _d_arraycopyT(this.s[], __r57, 1u)

i.e. NOT in the use of cast, ptr, or memcpy. The error points to the 
call site.

The recommended implementation:

D _d_arraycopyT(S, D)(S from, D to, uint size)
{
     if (__ctfe)
         import core.stdc.string;
         (() @trusted => memcpy(cast(void*)to.ptr, from.ptr, to.length * 
size))();
     }
     else
     {
         foreach (i, ref e; from) to[i] = e;
     }
     return to;
}

It doesn't seem like the recommended implementation will make the error 
go away.

> The difference is fairly simple but huge:
>
> - C intrinsics
>   - AssignExp.semantic
>     - e2ir => call RTLSYM_SYM
>     - interpret => special handling
>
> - D lowering
>   - AssignExp.semantic lowered to CallExp of object._arrayCopy
>     - normal function call and no special CTFE handling

I'm not familiar with the code, so although yes these are different I 
don't know how they translate into what needs to be done to make this work.

>> Also, as an aside: the _d_arraycopyT should probably go like this:
>>
>> D _d_arraycopyT(S, D)(S from, D to) { ... }
>> You don't need size because it's from[0].sizeof. Correct?
>
> Just convert the assignment to a function call, the backend deals with
> optimizations et.al. Also this seems to be used not only for static arrays.

I'm saying the third parameter is entirely redundant in all cases. It's 
the sizeof the element.

> NB:
>   - leave aways the _d prefix it's only needed to namespace extern(C)
> functions with flat mangling
>   - prolly should be _arrayCopy(T)(in T[] from, T[] to) as
> AssignExp.semantic already takes care of conversions

Yah, once we're past the hurdle of getting this to basically work these 
are good touches.


Andrei



More information about the Dlang-internal mailing list