Should pure nothrow ---> @pure @nothrow ?
Don
nospam at nospam.com
Fri Nov 27 04:35:22 PST 2009
Denis Koroskin wrote:
> On Fri, 27 Nov 2009 13:58:59 +0300, Don <nospam at nospam.com> wrote:
>
>> Denis Koroskin wrote:
>>> On Fri, 27 Nov 2009 12:50:19 +0300, bearophile
>>> <bearophileHUGS at lycos.com> wrote:
>>>
>>>> Walter Bright:
>>>>> Naked is not an externally visible attribute of a function,
>>>>> signature or
>>>>> type, it only concerns the internals. Therefore, it shouldn't be an
>>>>> attribute.
>>>>
>>>> On the other hand I agree with them that currently "naked" is not in
>>>> the best place. So let's try another alternative:
>>>>
>>>> void foo() {
>>>> @naked asm {
>>>> ...
>>>> }
>>>> }
>>>>
>>> No, it applies @naked to an asm block, which is misleading: naked
>>> should be applied to the whole function body.
>>
>> Yes, but if a function is naked, it should be illegal for it to
>> contain any non-asm executable code. The compiler can't generate
>> correct code when it's in a naked function. For all it knows, the
>> function might even have swapped stack pointers!
>>
>
> I definitely saw code that uses D inside naked functions (and wrote such
> code myself). There is an example in
> src/druntime/src/compiler/dmd/rt/trace.d
> I agree it might not be portable, but so is any code written in asm.
Thanks, that one should be changed. It's just a call to a void function,
and should be changed to a single call instruction. It wouldn't compile
in LDC.
> In fact, I'm using naked to make code /more portable/ in my DynamicCall
> module:
>
> void push(T)(T arg) // pass an argument to a function however compiler
> // wants (e.g. pass argument in EAX, if it fits)
> {
> asm { naked; ret; }
> }
>
> void invokeFunction(void* funcptr, Arg arg)
> {
> switch (arg.type) {
> case ArgType.Float:
> // so that I don't care how exactly floating-point variables
> // are passed to function, let compiler do it for me
> push!(float)(*cast(float*)arg.ptr);
> break;
> ...
> }
>
> asm { call funcptr; }
> }
>
> (This is a simplified code for a single-argument function call)
>
> I'm not sure how correct it is, though (I asked for a comment but no one
> answered).
That doesn't involve any mixing of naked and D in a single function. Of
course, your 'push' function leaves the stack in a corrupt state.
Definitely an unsafe function!
>> I believe D is quite correct in making 'naked' an asm instruction. Not
>> all CPUs might support it. (It's only relevant for CPUs/compilers
>> where a frame pointer is used).
>>
>
> Sure, but it only makes naked a vendor-specific extension, it doesn't make
> it illegal to use. Since it's not a user-defined annotation those
> compilers that don't support would just issue a compile-time error (the
> same way they would do it for asm { naked; } so it's just a matter of
> syntax). I prefer it to be an annotation because it's not an asm
> instruction at all. It has a lot in common with extern (Foo), so I'd
> like for them share syntax, too (@extern(C) void* malloc(size_t size); ?)
It's absolutely none of the caller's business whether the function is
naked. Naked has no consequences outside of the function body.
>>> void foo()
>>> @naked body
>>> {
>>
>> LOL! Spam filters would love that!!
>
> Indeed!
More information about the Digitalmars-d
mailing list