Switch embedded in enum definition?
Posted: Sat Nov 14, 2020 12:42 pm
I was experimenting with the Raspberry Pi bare bones code, to see what I could learn from it, and ran across an odd bit of code in the C source. In the definition of the enum containing the MMIO, GPIO, and UART constants, the first entry, meant to define the MMIO base address, is:
Now, I will admit that I tried to compile this blind, as part of what I wanted to see was how easy the tutorial was to follow. Not surprisingly, GCC did not recognize this idiom as a legitimate way to define an enum constant. While the intent seems clear, it isn't legal C code, as far as I can tell.
I simply replaced it with the appropriate sub-entry (the case 3 entry), but the code as given caught my interest.
However, I am curious as to why this was in the code sample. Is there an actual way to do this using a switch() statement (as opposed to, say, #define and #ifdef directives)?
Is this in the code primarily as a 'copy trap' (by which I mean, it is there to force the programmers following the tutorial to actually read the code), or is there a real idiom which this is meant to apply?
As an aside, AFAICT the triplet for AArch64 has changed to 'aarch64-none-elf-' rather than just 'aarch64-elf-', presumably to improve the consistency with other triplets. I don't want to change this in the wiki page until I have confirmed this, however.
Code: Select all
// The MMIO area base address.
switch (raspi) {
case 2:
case 3: MMIO_BASE = 0x3F000000; break; // for raspi2 & 3
case 4: MMIO_BASE = 0xFE000000; break; // for raspi4
default: MMIO_BASE = 0x20000000; break; // for raspi1, raspi zero etc.
}
I simply replaced it with the appropriate sub-entry (the case 3 entry), but the code as given caught my interest.
However, I am curious as to why this was in the code sample. Is there an actual way to do this using a switch() statement (as opposed to, say, #define and #ifdef directives)?
Is this in the code primarily as a 'copy trap' (by which I mean, it is there to force the programmers following the tutorial to actually read the code), or is there a real idiom which this is meant to apply?
As an aside, AFAICT the triplet for AArch64 has changed to 'aarch64-none-elf-' rather than just 'aarch64-elf-', presumably to improve the consistency with other triplets. I don't want to change this in the wiki page until I have confirmed this, however.