I was learning about how switch-statements work under the hood in C, so I decided to look at the compiled code. From what I understand, C uses a jump table to implement switch-statements. However, I don't see any labels or jump instructions in the assembly code.
I am on Apple silicon, so it is ARM assembly. Please take a look and tell me what you think. Thanks :)
C code:
int main (int argc, char *argv[]) {
int one;
int two;
int three;
int four;
switch (1) {
case 1:
one = 1;
break;
case 2:
two = 2;
break;
case 3:
three = 3;
break;
default:
four = 4;
break;
}
return 0;
}
ARM code:
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 14, 0 sdk_version 14, 4
.globl _main ; -- Begin function main
.p2align 2
_main: ; @main
.cfi_startproc
; %bb.0:
sub sp, sp, #32
.cfi_def_cfa_offset 32
mov x8, x0
mov w0, #0
str wzr, [sp, #28]
str w8, [sp, #24]
str x1, [sp, #16]
mov w8, #1
str w8, [sp, #12]
add sp, sp, #32
ret
.cfi_endproc
; -- End function
.subsections_via_symbols
return 0;
return
with-O3
(assuming gcc and derivatives)