From 4a71579b757d3a2eb6902c84391f429838ad4912 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Thu, 30 Jan 2020 12:33:44 -0300 Subject: git subrepo clone https://git.savannah.gnu.org/git/lightning.git deps/lightning subrepo: subdir: "deps/lightning" merged: "b0b8eb5" upstream: origin: "https://git.savannah.gnu.org/git/lightning.git" branch: "master" commit: "b0b8eb5" git-subrepo: version: "0.4.1" origin: "https://github.com/ingydotnet/git-subrepo.git" commit: "a04d8c2" --- deps/lightning/doc/rfib.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 deps/lightning/doc/rfib.c (limited to 'deps/lightning/doc/rfib.c') diff --git a/deps/lightning/doc/rfib.c b/deps/lightning/doc/rfib.c new file mode 100644 index 0000000..f14da42 --- /dev/null +++ b/deps/lightning/doc/rfib.c @@ -0,0 +1,53 @@ +#include +#include + +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 */ + jit_node_t *zero; /* to patch the forward reference */ + + init_jit(argv[0]); + _jit = jit_new_state(); + + label = jit_label(); + jit_prolog (); + in = jit_arg (); + jit_getarg (JIT_R0, in); /* R0 = n */ + zero = jit_beqi (JIT_R0, 0); + jit_movr (JIT_V0, JIT_R0); /* V0 = R0 */ + jit_movi (JIT_R0, 1); + ref = jit_blei (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_R0); /* R0 = fib(n-2) */ + jit_addr(JIT_R0, JIT_R0, JIT_V1); /* R0 = R0 + V1 */ + + jit_patch(ref); /* patch jump */ + jit_patch(zero); /* patch jump */ + 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; +} -- cgit v1.2.3