postincrement behaviour (differences between dmd and gdc)

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Apr 20 04:36:47 PDT 2011


On 20.04.2011 14:16, Mafi wrote:
> Am 20.04.2011 12:01, schrieb Dmitry Olshansky:
>> On 20.04.2011 13:53, Jonathan M Davis wrote:
>>>> This was partially discussed before some time ago -
>>>> http://www.digitalmars.com/d/archives/digitalmars/D/postincrement_behaviour 
>>>>
>>>>
>>>> _differences_between_dmd_and_gdc_47334.html
>>>>
>>>>
>>>> The following statement has different behaviours in dmd and gdc.
>>>>
>>>> int x;
>>>> x = x++;
>>>>
>>>>
>>>> Where the backend for DMD interprets it as:
>>>>
>>>> tmp = x;
>>>> x++;
>>>> x = tmp;
>>>>
>>>> And the backend for GDC interprets it as:
>>>>
>>>> x = x;
>>>> x++;
>>>>
>>>>
>>>> Skipping all undefined behaviour/which one is correct talk, an
>>>> alternative
>>>> suggestion would be to treat such nonsensical code as an error - like
>>>> "Cannot modify the value of a variable twice in the same expression."
>>>> - as
>>>> this kind of thing I would expect to be almost always a bug.
>>> Actually, I would have expected postincrement and preincrement to be
>>> identical
>>> in this case. It was my understanding that the entire expression to
>>> the right
>>> of the assignment operator was evaluated before the assignment was
>>> made, but I
>>> guess I could see how that could be open to interpretation with
>>> regards to the
>>> postincrement operator.
>>>
>> No, I think post-increment is done after the whole expression is
>> evaluated, see the common C idiom:
>>
>> *p++ = *q++;
>> it's always interpreted as:
>> *p = *q;
>> p++,q++; // or q++,p++;
>>
>
> IMHO it should be interpreted as
>
> typeof(p) tmp_p;
> typeof(q) tmp_q;
> *((tmp_p = p), ++p, tmp_p) = *((tmp_q = q), q++, tmp_q);

Yes, I got the idea about this interpretation  right after posting (BTW 
it should '++q' vs 'q++') ...
In the  end should be expanded to autoincrementing address mode, in case 
of x86 the equivalent would be "string" ops.
>
> In this case the overall semantics are identical but your expandation 
> looks like gdc's and mine like dmd's. Therefore I think dmd's is more 
> correct although less useful.
>
> Generally I think it should just be an error to assign twice in the 
> same expression.
>
Why not ? I kind of liked this one:
a = b = 0;

> Mafi

-- 
Dmitry Olshansky



More information about the Digitalmars-d mailing list