[Top][All Lists]

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

Re: [Libjit] Getting address of current function

From: Jakob Löw
Subject: Re: [Libjit] Getting address of current function
Date: Sat, 28 Jul 2018 03:27:46 +0200


Currently there is no built-in way to retrieve the pointer to a
function inside the function itself.
There is a JIT_OP_LOAD_PC opcode which loads the PC at/after the
current instruction, however this is not identical to what
jit_function_to_closure returns. If this is what you were looking for
adding a simple function to jit-insn.c as shown in [1] works.
If you really need a pointer to the entry point of a function the most
efficient way would be to create a small memory area where you later
put the pointer to the function. [2] is a simple example. It's similar
to what you already seem to doing. Is there a reason why this isn't
good enough?

- Jakob

// put this into jit-insn.c
jit_insn_load_pc(jit_function_t func)
        return create_dest_note(func, JIT_OP_LOAD_PC, jit_type_void_ptr);

// put this into jit-insn.h
jit_value_t jit_insn_load_pc(jit_function_t func);

// when compiling the function
void **func_ptr = malloc(sizeof(void *));
jit_value_t ptr = jit_value_create_nint_constant(func, jit_type_void_ptr, 

jit_value_t pc = jit_insn_load_relative(func, ptr, 0, jit_type_void_ptr);
//pc holds a pointer to the entry point of func

// later when finished compiling func
*func_ptr = jit_function_to_closure(func);

On Fri, 2018-07-27 at 16:34 +0200, Aritz Erkiaga wrote:
> Hello,
> I know that jit_insn_call can be used to call the current function
> recursively, and I was wondering if it was possible to get the
> address of the function currently being built for a reason other than
> calling. Currently I get it from a data structure holding the
> addresses of all compiled closures. It is important that this address
> the implementation substitutes as an immediate value or otherwise be
> numerically equal to that returned by jit_function_to_closure after
> calling jit_function_compile on the function.
> Thanks in advance
> Regards, Aritz

Attachment: signature.asc
Description: This is a digitally signed message part

reply via email to

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