SFX: interface between jit and C++

42 篇文章 0 订阅

转自:http://webkit.sed.hu/blog/20090430/technical-discussion-part-1-interface-between-jit-and-c

interface between jit and C++

Perhaps one might think that this topic is not a big deal. We have a guideline, called ARM-EABI (Embedded Application Binary Interface). Among other things, this paper specifies which registers must be preserved, how the stack must be handled, etc. We just have to follow these rules, don't we?

Well, life is not so simple. The developers of SquirellFish Extreme, the JavaScript JIT compiler of WebKit, pushed the limitations of x86 ABI in two ways.

The first one is the unusual way of argument passing. A typical compiler pushes the arguments onto the stack. Those arguments are freed either by the caller or by the callee function. In SquirellFish Extreme the arguments are never freed, however. The same argument list is passed to the high level C++ callback functions again and again. The first 16 items of this list contain the temporary arguments which may be changed by the JITted code, but the items over 16 are constants (for example, there is a pointer to the global data among them). In this way, the JIT can pass all important constants to the C++ callbacks without any stack manipulation (thus, the code runs faster). Fortunately, the ARM-EABI has a similar way of passing arguments, except that the first 4 are passed through registers. Since those are only temporary arguments, we can mimic the x86-jit constant argument passing on ARM as well.

The second feature of SqurellFish Extreme is much less portable. On x86 architectures, even the return address is stored on the stack.

[ ARG n ][ ARG n-1 ]...[ arg 2 ][ arg 1 ][ return address ]

Figure 1

When an exception occures, SquirellFish Extreme changes the return address of the C++ callback function. The new return address will points to the exception handler in the jit code. Note: only high level callbacks can throw exceptions, jit code itself never throws any. As you can see on Figure 1 (above), overwriting this return address is not that difficult on x86.

On arm, the return address is stored in the link register. The link register is a scratch register, the callee function can do anything with it as long as it is capable to return to the caller. However, we noticed that most of the time the compiler simply pushes it onto the top of the stack by an stmdb sp!, { ... , lr } or an str lr, [sp - 4]! instruction.

Our method is the following: if the callback function starts with one of these instructions above, we know that the return value is stored on the top of the stack (exactly the same way as for x86). We wrote a smart algorithm to detect these instructions, even if the instruction scheduler of the compiler reordered them. If the algorithm cannot find a matching instruction, a stub function is created for that specific callback function by the JIT. The stub function provides a way to overwrite the return address even for those functions. Fail rate is very low, only 1 stub needs to be generated for every 40-50 callbacks.

If you like this kind of technical related posts, just write an encouraging comment, and I will continue them. :)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值