tenderlove/tenderjit

Runtime#call_cfunc etc should be smarter

tenderlove opened this issue · 8 comments

Right now we're using R9 as a scratch register, but R9 is a caller saved register meaning that the callee is free to clobber the value in R9. If we want to save the value in R9 then we have to push the register before calling the C function.

However, our register allocator has us check out and check back in temp registers meaning that we should be able to know that the "lifespan" of a temp register spans a C function call.

We should make the runtime smart enough to detect that R9 (or any other caller saved register) "lives" past a C function call and have it automatically push / pop the register.

Also now that I'm thinking about it, if we only ever use the "rt.push_reg" / "rt.pop_reg" for managing registers, then the runtime object should also know the stack alignment and be able to automatically figure out how much we need to push.

Also now that I'm thinking about it, if we only ever use the "rt.push_reg" / "rt.pop_reg" for managing registers, then the runtime object should also know the stack alignment and be able to automatically figure out how much we need to push.

I'd like to take this one, if it's ok. It's actually something I've previously thought as a mean to simplify the call_cfunc UX.

@64kramsystem it's all yours!

I think it'd be a good idea to use dependency versions (e.g. tags) in the Gemfile, to ensure versioning consistency; it's not strictly needed, but I think that long-term, it may save minor headaches.

I'd also take the other smartness task, if/after the auto alignment goes in. I'm not sure how long it will take me, as I hadn't look at it (and I'm also studying Fisk in parallel).

I think it'd be a good idea to use dependency versions (e.g. tags) in the Gemfile, to ensure versioning consistency; it's not strictly needed, but I think that long-term, it may save minor headaches.

Makes sense, I think that would be fine.

I'd also take the other smartness task, if/after the auto alignment goes in. I'm not sure how long it will take me, as I hadn't look at it (and I'm also studying Fisk in parallel).

👍🏻

I'd also take the other smartness task, if/after the auto alignment goes in. I'm not sure how long it will take me, as I hadn't look at it (and I'm also studying Fisk in parallel).

Before starting this, I'm going to spend a bit of time to try to think about how to add some form of testing to call_cfunc. Right now for example, on my system, aligning or not doesn't make any difference (I presume due to the fact that FP registers are not used), and there is some risk that subtle issues may be inadvertly present/added.

Makes sense. AFAIK the 16 byte alignment only matters on macOS (but I could be totally wrong). I'm not sure how we can test, but it would be great to have a test.