[cfe-dev] Unexpected behaviour for constant expression initializer

Arnaud Allard de Grandmaison Arnaud.AllardDeGrandMaison at dibcom.com
Thu Oct 21 07:46:54 PDT 2010


Yes, int is i16 on the MSP430.

I was not aware that expression is evaluated with the target's "int type", unless specified otherwise (using L/UL/..).

Thanks for your help,
--
Arnaud de Grandmaison

-----Original Message-----
From: Frits van Bommel [mailto:fvbommel at gmail.com]
Sent: Thursday, October 21, 2010 3:51 PM
To: Arnaud Allard de Grandmaison
Cc: cfe-dev at cs.uiuc.edu
Subject: Re: [cfe-dev] Unexpected behaviour for constant expression initializer

On Thu, Oct 21, 2010 at 3:01 PM, Arnaud Allard de Grandmaison
<Arnaud.AllardDeGrandMaison at dibcom.com> wrote:
> The following reduced C testcase :
>
> $ cat initializer.c
>
> unsigned long ok = 8509568; // 0x81D880
>
> unsigned long bogus = 0x800000 + 2*3780*16; // 0x81D880

I'm pretty sure the small integers in those initializers are of type
int until they're "assigned" to the unsigned long global and/or used
in arithmetic with an integer constant with a larger[1] type. So if
'int' is something like i16 (which I'm guessing is the case for
MSP430?), the intermediate value 2*3780*16 will overflow before being
added to 0x800000 (which doesn't fit in i16, so it's probably
automatically a long int).
Try appending 'L' (or 'UL') to the 2, 3780 and/or 16.


[1] (Where long > int, even if both are 32 bit)




More information about the cfe-dev mailing list