jump-table

GCC Jump Table initialization code generating movsxd and add?

爱⌒轻易说出口 提交于 2020-01-06 04:12:27
问题 When I compile a switch statement with optimization in GCC, it sets up a jump table like this, (fcn) sym.foo 148 sym.foo (unsigned int arg1); ; arg unsigned int arg1 @ rdi 0x000006e0 83ff06 cmp edi, 6 ; arg1 0x000006e3 0f87a7000000 ja case.default.0x790 0x000006e9 488d156c0100. lea rdx, [0x0000085c] 0x000006f0 89ff mov edi, edi 0x000006f2 4883ec08 sub rsp, 8 0x000006f6 486304ba movsxd rax, dword [rdx + rdi*4] 0x000006fa 4801d0 add rax, rdx ; '(' ;-- switch.0x000006fd: 0x000006fd ffe0 jmp rax

Switch-Case: declaration-with-initialization & declaration-and-then-assignment

本小妞迷上赌 提交于 2019-12-19 19:38:07
问题 In the switch-case statements declaration-with-initialization is invalid but declaration-and-then-assignment is allowed. As shown in the following code snippet. What is difference between these two type of initializations from the compiler side? And why is the first type of initialization invalid and second type a valid one. switch(val) { case 0: int newVal = 42; //Invalid break; case 1: int newVal2; //Valid newVal2 = 42; break; case 2: break; } 回答1: Effectively, the rule is that you can't

c switch and jump tables

≯℡__Kan透↙ 提交于 2019-12-12 10:36:18
问题 It is my understanding that a switch statement in c/c++ will sometimes compile to a jump table. My question is, are there any thumb rules to assure that? In my case I'm doing something like this: enum myenum{ MY_CASE0= 0, MY_CASE0= 1, . . . }; switch(foo) { case MY_CASE0: //do stuff break; case MY_CASE1: //do stuff break; . . . } I cover all the cases from 1 to n by order. Is safe to assume it will compile to a jump table? The original code was a long and messy if else statement, so at the

Does “default” switch case disturb jump table optimization?

孤人 提交于 2019-12-10 01:45:50
问题 In my code I'm used to write fall-back default cases containing asserts like the following, to guard me against forgetting to update the switch in case semantics change switch(mode) { case ModeA: ... ; case ModeB: ... ; case .. /* many of them ... */ default: { assert(0 && "Unknown mode!"); return ADummyValue(); } }; Now I wonder whether the artificial fall-back check default case will interfere with jump table generations? Imagine "ModeA" an "ModeB" etc are consecutive so the compiler could

Is there anything like branch/jump table in Java?

三世轮回 提交于 2019-12-08 19:47:46
问题 Does Java have something similar to a branch or jump table? A branch or jump table table is, according to wikipedia, a term used to describe an efficient method of transferring program control (branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch instructions. Does Java have something like this or do I just have to use if/else if/else or case statements? 回答1: Java has a switch statement, but whether it compiles to a

creating constant jump table; xcode; clang; asm

妖精的绣舞 提交于 2019-12-07 12:41:38
问题 I have quite strange issue when try to create the jump table in my asm program for iphone (arm64): .globl my_func my_func: ... //jump (switch) table .L.f_switch: .short .L.case0 - .L.f_switch .short .L.case1 - .L.f_switch ... .L.case0: //some case code ... .L.case1: //other case code After compilation this table is filled by zeros instead of actual values. It could be seen by dumping compiled object file. (__TEXT,__text) section _my_func: 0000000000000000 adr x4, #16 0000000000000004 ldrh w5,

Does “default” switch case disturb jump table optimization?

你离开我真会死。 提交于 2019-12-05 02:24:05
In my code I'm used to write fall-back default cases containing asserts like the following, to guard me against forgetting to update the switch in case semantics change switch(mode) { case ModeA: ... ; case ModeB: ... ; case .. /* many of them ... */ default: { assert(0 && "Unknown mode!"); return ADummyValue(); } }; Now I wonder whether the artificial fall-back check default case will interfere with jump table generations? Imagine "ModeA" an "ModeB" etc are consecutive so the compiler could optimize into a table. Since the "default" case contains an actual "return" statement (since the assert

Switch-Case: declaration-with-initialization & declaration-and-then-assignment

一笑奈何 提交于 2019-12-01 17:04:17
In the switch-case statements declaration-with-initialization is invalid but declaration-and-then-assignment is allowed. As shown in the following code snippet. What is difference between these two type of initializations from the compiler side? And why is the first type of initialization invalid and second type a valid one. switch(val) { case 0: int newVal = 42; //Invalid break; case 1: int newVal2; //Valid newVal2 = 42; break; case 2: break; } Effectively, the rule is that you can't jump into a block past a declaration that has an initialization (or past the declaration of a non-POD type

How to store goto labels in an array and then jump to them?

时光毁灭记忆、已成空白 提交于 2019-11-30 03:18:59
I want to declare an array of "jumplabels". Then I want to jump to a "jumplabel" in this array. But I have not any idea how to do this. It should look like the following code: function() { "gotolabel" s[3]; s[0] = s0; s[1] = s1; s[2] = s2; s0: .... goto s[v]; s1: .... goto s[v]; s2: .... goto s[v]; } Does anyone have a idea how to perform this? It is possible with GCC feature known as " labels as values ". void *s[3] = {&&s0, &&s1, &&s2}; if (n >= 0 && n <=2) goto *s[n]; s0: ... s1: ... s2: ... It works only with GCC! goto needs a compile-time label. From this example it seems that you are

How to store goto labels in an array and then jump to them?

坚强是说给别人听的谎言 提交于 2019-11-29 02:18:53
问题 I want to declare an array of "jumplabels". Then I want to jump to a "jumplabel" in this array. But I have not any idea how to do this. It should look like the following code: function() { "gotolabel" s[3]; s[0] = s0; s[1] = s1; s[2] = s2; s0: .... goto s[v]; s1: .... goto s[v]; s2: .... goto s[v]; } Does anyone have a idea how to perform this? 回答1: It is possible with GCC feature known as "labels as values". void *s[3] = {&&s0, &&s1, &&s2}; if (n >= 0 && n <=2) goto *s[n]; s0: ... s1: ... s2