<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Aug 6, 2013 at 2:07 PM, Riyaz Puthiyapurayil <span dir="ltr"><<a href="mailto:Riyaz.Puthiyapurayil@synopsys.com" target="_blank">Riyaz.Puthiyapurayil@synopsys.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
One approach is not to use the switch instruction to lower my switch statement to LLVM. Instead, implement the jump table myself and use the indirectbr instruction. Then I have full control over the jump table. Is this feasible? How would I initialize my global array representing the jump table with the local labels in a function?<br>
<br></blockquote><div><br></div><div>Another option is to use indirect tail calls and a table of function pointers. E.g.</div><div><br></div><div>typedef uint32_t opcode;<br></div><div>typedef void dispatch_f(opcode *, long, long, long);</div>
<div>dispatch_f *jump_tab[256] = {</div><div>...<br>};</div><div>void add(opcode *pc, long r1, long r2) {</div><div> return jump_tab[*pc & 0xFF](pc + 1, r1 + r2, r2);</div><div>}</div><div><div>void sub(opcode *pc, long r1, long r2) {</div>
<div> return jump_tab[*pc & 0xFF](pc + 1, r1 - r2, r2);</div><div>}</div></div><div><br></div><div>This has the advantage that you can be sure that you get get a fixed register assignment (which depends on the calling convention), which is typically one of the most difficult things for the compiler to get right for "interpreter-like" code in switches or with indirect branches.</div>
<div><br></div><div>To achieve what you want with this approach, you can just modify jump_tab. Alternatively, you can swap out the jump table itself (e.g. to switch into a tracing mode for a tracing jit).</div><div><br></div>
<div>-- Sean Silva</div><div><br></div><div><br></div></div></div></div>