|
From: | Marc Nieper-Wißkirchen |
Subject: | Re: Two entry points for a function |
Date: | Mon, 8 Aug 2022 23:25:32 +0200 |
Le lun., août 8 2022 at 23:04:02 +0200, Marc Nieper-Wißkirchen
<marc.nieper+gnu@gmail.com> a écrit :
> Hi Paul,
>
> thanks for chiming in.
>
> I didn't know about jit_align. Maybe it should be documented (along
> with some instruction that embeds given bytes in the code stream?).
>
> So what I have in mind could probably be done with GNU lightning as
> follows:
I guess that works, yes. Be careful that your "short piece of code"
cannot do much (Lightning won't save/restore the callee-saved registers
for you, stack won't work, jit_ret() won't work, etc).
-Paul
> jit_prolog ();
> jit_tramp (256);
> jit_align (16);
> entry1 = label_indirect ();
> /* short piece of code */
> jit_align (16);
> entry2 = label_indirect ();
> ...
> jit_epilog ();
> jit_emit ();
> char *addr1 = jit_address (entry1);
> char *addr2 = jit_address (entry2);
> assert (addr2 - addr1 == 16);
> ...
>
> Am Mo., 8. Aug. 2022 um 22:53 Uhr schrieb Paul Cercueil
> <paul@crapouillou.net>:
>> Hi Marc,
>>
>> I don't think you can have multiple entry points of a function (or
>> maybe with jit_indirect()?) but you can cheese things a bit with a
>> trampoline.
>>
>> Here's what I do, more or less:
>>
>> ----
>>
>> jit_prolog();
>> jit_tramp(256);
>>
>> for (i = 0; i < nb_entry_points; i++) {
>> jit_addi(JIT_R0, 1);
>> jit_align(8);
>> }
>>
>> jit_epilog();
>> jit_prolog();
>>
>> /* Read JIT_R0, its value give us what entry point was taken */
>>
>> jit_epilog();
>>
>> ----
>>
>> In that example my JIT_R0 is always 0 on entry. The jit_align()
>> makes
>> sure that each one of your entry points is aligned to 8 bytes.
>> There is
>> still some decoding needed (I read a C function pointer from a
>> table at
>> the index pointed by JIT_R0), but the decoding is done in that
>> function
>> and not in the callers.
>>
>> Cheers,
>> -Paul
>>
>> Le lun., août 8 2022 at 22:16:28 +0200, Marc Nieper-Wißkirchen
>> <marc.nieper+gnu@gmail.com> a écrit :
>> > I am looking for a way to implement a function (pointer) with GNU
>> > lightning that has two entry points.
>> >
>> > In native assembly, I would code something like this:
>> >
>> > .align 16
>> > entry1: jmp L0
>> > .align 16
>> > entry2: ...
>> > ...
>> > L0: ...
>> >
>> > The function would be represented by a pointer to entry1. If I
>> > wanted to jump to the second entry point, I would just add 16 to
>> the
>> > pointer.
>> >
>> > How can a similar thing be achieved with GNU lightning? So far,
>> the
>> > best that has come to my mind is to use one more indirection. The
>> > "function pointer" points to a structure having the addresses of
>> > entry1 and entry2 as fields. (Let us assume we are in a
>> trampoline
>> > so that the stack is already set up). But this is less efficient
>> due
>> > to the extra indirection.
>> >
>> > Is there a way to emulate the above native code with GNU
>> lightning?
>> > And, if not, what would have to be added?
>> >
>> > Marc
>>
>>
[Prev in Thread] | Current Thread | [Next in Thread] |