Search code examples
clanguage-lawyer

is it legal to have a c environment where char is 16 bits and short is also 16 bits


I am trying to create (new backend for tinyc) a c compiler for a very odd machine. Its only addressable unit is a 16 bit word, ie it cannot address at the 8 bit byte level. Its natural int size is 16 too.

As far as I can see the c standard says that char must be at least 8 bits, and that short must be at least 16. But there is nothing that says sizeof(char) cannot = sizeof(short).

It feels weird to be even thinking about a system like that, we all have such a strong mental model of what chars and shorts etc look like in ram.

My alternative is to be 'normal' in data types and emulate 8 bit behavior by fetching words and chopping them up. My issue there is that there are no shift or rotate instructions so it would be very expensive to do (emulating right shift even by one bit in order to do a word fetch for a byte needs at least 32 instructions, test and set each bit, or do a long rotate left). Feels like my 100mhz fpga would be running at 2mhz


Solution

  • is it legal to have a c environment where char is 16 bits and short is also 16 bits

    Yes.