[Vm-dev] Never ending signedness problems with code generator

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Fri Mar 13 21:18:14 UTC 2015


Hi,

I want to generate code for this one

    csi < 16rFFFFFFFF ifTrue: [...]

Unfortunately, csi is signed (csi = C-SmallInteger = the value in C of a
SmallInteger)
And the generator generates the literal as unsigned long 0xffffffffUL
But my intention was rather to generate as signed long (for 64 bits VM)!

Guess what happens in C if you compare a negative int with an unsigned ?

#include <stdio.h>
int main()
{
    int csi = -1;
    printf( "-1>0xffUL => %d\n",csi>0xffUL);
    return 0;
}

above program prints 1 of course (thus the condition is true).

Obviously, the signed are promoted to unsigned -1 is thus converted to
2^n-1 where n is the number of bits of unsigned long (sizeof unsigned
long)*CHARBITS.

That happens for me in 64 bits, (unsigned long) -1 -> 0xffffffffffffffffUL
which is obviously greater than 0xffffffffUL.

Argh!

Does that means that literal translation cannot ignore the context?
That would mean that type analysis would have to be performed on the AST,
before the AST is transformed into a C-AST...
Or is there a simpler way?

Nicolas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20150313/bc70e29d/attachment.htm


More information about the Vm-dev mailing list