Can C cope with sub-byte instruction addressing?

139 Views Asked by At

For example if an architecture supports nibble length instructions but data is byte aligned, will:

void *PointerToAnything;

work? In other words, can the compiler handle this?

1

There are 1 best solutions below

2
Erlkoenig On

In C, it is impossible to access data elements in units smaller than 8 bits, as the smallest possible type is char, which has CHAR_BIT bits, which is 8 at least. Bitfields are an exception, but don't allow pointers to their members; a data-pointer with sub-(8-)byte-precision can not exist in C.

However, instructions (and therefore functions) might be stored differently, and function-pointers could have sub-byte-precision. In general, function pointers and data pointers are not interchangeable, so you can not (correctly) store such a function pointer in a void* pointer. C does not support accessing the machine code anyways, so there would be no support for accessing instructions that have sub-byte-alignment and/or size.

Even on platforms with at least byte-instruction size & alignment, function pointers and data pointers might not be interchangeable, as function pointers might be larger or smaller than data pointers (imagine a system with 256 RAM bytes for data, and 64kB flash bytes for program memory). Therefore, C does not guarantee that void* can point to everything. However, some platforms such as POSIX do explicitly allow this, to allow e.g. dlsym() to work.