1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-01 04:10:18 +02:00
guile/doc/rfib.c
pcpa 8f020fe044 Add code to release all memory used by the jit state.
* include/lightning.h, lib/lightning.c: Implement the new
	jit_clear_state and jit_destroy_state calls. jit_clear_state
	releases all memory not required during jit_execution; that
	is, leaves only the mmap'ed data and code buffers allocated.
	jit_destroy_state releases the mmap'ed buffers as well as
	the jit_state_t object itself, that holds pointers to the
	code and data buffers, as well as annotation pointers (for
	disassembly or backtrace) in the data buffer.

	* lib/jit_note.c: Correct invalid vector offset access.

	* check/ccall.c, check/lightning.c, doc/ifib.c, doc/incr.c,
	doc/printf.c, doc/rfib.c, doc/rpn.c: Use the new jit_clear_state
	and jit_destroy_state calls, to demonstrate the new code to
	release all jit memory.

	* doc/body.texi: Add basic documentation and usage description
	of jit_clear_state and jit_destroy_state.
2013-02-11 15:51:55 -02:00

51 lines
1.5 KiB
C

#include <stdio.h>
#include <lightning.h>
static jit_state_t *_jit;
typedef int (*pifi)(int); /* Pointer to Int Function of Int */
int main(int argc, char *argv[])
{
pifi fib;
jit_node_t *label;
jit_node_t *call;
jit_node_t *in; /* offset of the argument */
jit_node_t *ref; /* to patch the forward reference */
init_jit(argv[0]);
_jit = jit_new_state();
label = jit_label();
jit_prolog ();
in = jit_arg ();
jit_getarg (JIT_V0, in); /* V0 = n */
ref = jit_blti (JIT_V0, 2);
jit_subi (JIT_V1, JIT_V0, 1); /* V1 = n-1 */
jit_subi (JIT_V2, JIT_V0, 2); /* V2 = n-2 */
jit_prepare();
jit_pushargr(JIT_V1);
call = jit_finishi(NULL);
jit_patch_at(call, label);
jit_retval(JIT_V1); /* V1 = fib(n-1) */
jit_prepare();
jit_pushargr(JIT_V2);
call = jit_finishi(NULL);
jit_patch_at(call, label);
jit_retval(JIT_V2); /* V2 = fib(n-2) */
jit_addi(JIT_V1, JIT_V1, 1);
jit_addr(JIT_R0, JIT_V1, JIT_V2); /* R0 = V1 + V2 + 1 */
jit_retr(JIT_R0);
jit_patch(ref); /* patch jump */
jit_movi(JIT_R0, 1); /* R0 = 1 */
jit_retr(JIT_R0);
/* call the generated code, passing 32 as an argument */
fib = jit_emit();
jit_clear_state();
printf("fib(%d) = %d\n", 32, fib(32));
jit_destroy_state();
finish_jit();
return 0;
}