lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Lightning] Are tail calls possible in GNU lightning?


From: Marc Nieper-Wißkirchen
Subject: Re: [Lightning] Are tail calls possible in GNU lightning?
Date: Sat, 10 Jan 2015 08:23:12 +0100



2015-01-10 3:56 GMT+01:00 Paulo César Pereira de Andrade <address@hidden>:
2015-01-09 19:55 GMT-02:00 Marc Nieper-Wißkirchen <address@hidden>:
> P.S.: I have written a small example program that demonstrates what I want
> to achieve:
>
> #include <stdio.h>
> #include <lightning.h>
>
> static jit_state_t *_jit;
>
> jit_node_t *compile() {
>   jit_state_t *_jit;
>   _jit = jit_new_state();
>   jit_prolog();
>   jit_node_t *label = jit_indirect();
>   jit_reti(42);
>   jit_emit();
>   jit_clear_state();
>   return jit_address(label);
> }

  This will leak a jit_state_t, but I presume it is only for example,
otherwise, need to jit_destroy_state it once no longer used.

Exactly. In the real application, I would store the pointer to _jit somewhere and calling jit_destroy_state when the code is to be GCed.
 

> int main(int argc, char* argv[]) {
>
>   int (*code)();
>
>   jit_node_t *jump, *label, *ref;
>
>   init_jit(argv[0]);
>   _jit = jit_new_state();
>
>   jit_prolog();
>   jit_prepare();
>   jit_finishi(compile);

  This works because neither function receives (stack) arguments,
but if whatever "compile" returns needs more arguments than the
code calling it, it will corrupt the stack. Lightning could fix some
of the cases (same jit_context_t calling jit compiled functions),

For my purposes (using a virtual stack), I will never have to do a real C-style function call (except when doing FFI calls). Instead there will be (indirect) jumps everywhere and the real stack will not be touched. Thus the assumption that all generated functions take the same number of parameters (namely 0) would be fine for me.
 
>   jit_retval(JIT_R0);
>   jit_jmpr(JIT_R0);

  This will not work on some backends, like ia64 or big endian
powerpc (function descriptors), or, mips or little endian powerpc
where the function pointer must be in a specific register, to
compute GOT, PIC, etc. hppa also needs some special
handling. Also, for the sake of simplicity/safety, lightning would
still need to spill/reload modified callee save registers.


How to do indirect jumps then? In the manual to GNU lightning, the code jit_jmpr(R0) is used in several examples. How can I use an indirect label then?

Marc

reply via email to

[Prev in Thread] Current Thread [Next in Thread]