[LLVMdev] Patching jump tables at run-time

Sean Silva chisophugis at gmail.com
Tue Aug 6 17:13:20 PDT 2013


On Tue, Aug 6, 2013 at 2:07 PM, Riyaz Puthiyapurayil <
Riyaz.Puthiyapurayil at synopsys.com> wrote:
>
> 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?
>
>
Another option is to use indirect tail calls and a table of function
pointers. E.g.

typedef uint32_t opcode;
typedef void dispatch_f(opcode *, long, long, long);
dispatch_f *jump_tab[256] = {
...
};
void add(opcode *pc, long r1, long r2) {
  return jump_tab[*pc & 0xFF](pc + 1, r1 + r2, r2);
}
void sub(opcode *pc, long r1, long r2) {
  return jump_tab[*pc & 0xFF](pc + 1, r1 - r2, r2);
}

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.

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).

-- Sean Silva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130806/7352bfee/attachment.html>


More information about the llvm-dev mailing list