3 ms·
> Do you want to have signed integer overflow not be undefined behavior? Sorry, you can't do that in C. If you write a compiler that targets C, you can have th
by Someone 3y ago
> Do you want to have signed integer overflow not be undefined behavior? Sorry, you can't do that in C.
If you write a compiler that targets C, you can have that. ‘Just’ generate code that avoids it. If you want it to wrap around, for example, generate something like this (I’m not 100% sure it’s right)):
int sum = (int)((unsigned int) a + (unsigned int) b);
instead of
int sum = à + b;
If you want that to trap on overflow, the code gets more convoluted (see e.g. https://codereview.stackexchange.com/a/37178 https://codereview.stackexchange.com/a/37178 for what’s needed to detect overflow)
Packed structures and unaligned data similarly can be supported by generating C code that manipulates char arrays instead of C structs.
I wouldn’t count on having any C compiler generate efficient code for it, though.
That applies to most of the other features, too. You can have garbage collection, exception handling and coroutines, for example, but if you generate C code implementing it, chances are zero that the C compiler turns that into efficient code.