public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: Bartosz Szreder <bartosz.szreder@huuugegames.com>, jit@gcc.gnu.org
Subject: Re: Function pointer handling
Date: Sun, 01 Jan 2017 00:00:00 -0000	[thread overview]
Message-ID: <1506018592.10251.23.camel@redhat.com> (raw)
In-Reply-To: <CACDdPnvRcueTLRWEqxeUZJz5+392Shwt_bSL7WNvgXvFVWFOcA@mail.gmail.com>

On Thu, 2017-09-21 at 17:43 +0200, Bartosz Szreder wrote:
> I have some questions regarding pointers to functions in current
> (7.2)
> version of GCC-JIT.
> 
> 1. The documentation doesn't mention existence of
> gcc_jit_context_new_function_ptr_type() as a mechanism of handling
> function pointers, yet contains
> gcc_jit_context_new_call_through_ptr().

Oops; looks like I forgot to document the former.

> - Is the former function unsupported/buggy in some way and is not
> indented for general use, or just lacks proper documentation?

It's just missing documentation.  I'm working on fixing it.

Have a look at gcc/testsuite/jit.dg/test-calling-function-ptr.c
e.g.:
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/testsuite/jit.dg/test-calling-function-ptr.c;h=e21bd15e303f8cfcd37d2eeb294188cc6702066f;hb=HEAD


> - What is the proper way of obtaining a function pointer to be passed
> to gcc_jit_context_new_call_through_ptr()? There doesn't seem to be
> any counterpart to gcc_jit_lvalue_get_address() for functions. As the
> name suggests, gcc_jit_lvalue_get_address() works on an L-value and
> gcc_jit_function type isn't an ancestor of gcc_jit_lvalue in the
> internal type system, therefore upcasting is impossible.

If it's a pre-existing function, then you can take its address in your
C code, and then use gcc_jit_context_new_rvalue_from_ptr, which assumes
that function pointers can be cast to void *, but it's very rare for
that not to hold (Harvard architecture, where code and data are
disjoint).

If it's a function that's being compiled as part of the same
gcc_jit_context, then I think you've identified a weakness in the
current API.  As you say, one fix would be to make gcc_jit_function be
a subclass of gcc_jit_lvalue (and add casting functions); I don't yet
know how easy that would be to implement.

> 2. Is it safe to pass a function pointer as a GCC_JIT_TYPE_VOID_PTR
> and use gcc_jit_context_new_call_through_ptr() on such value?

It uses the type of the function pointer when building the call, so the
function pointer needs to be of a type created using
gcc_jit_context_new_function_ptr_type.  You may be able to get away
with adding casts, but the casting API is rather fussy compared to the
rules in C.

Hope this is helpful

Dave

  reply	other threads:[~2017-09-21 18:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-01  0:00 Bartosz Szreder
2017-01-01  0:00 ` David Malcolm [this message]
2017-01-01  0:00   ` Bartosz Szreder
2017-01-01  0:00     ` [committed] jit: implement gcc_jit_function_get_address David Malcolm
2017-01-01  0:00   ` [committed] jit: document function pointers David Malcolm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1506018592.10251.23.camel@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=bartosz.szreder@huuugegames.com \
    --cc=jit@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).