lightning
[Top][All Lists]
Advanced

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

Re: Issue when using jit_label() before a jump or branch


From: Paul Cercueil
Subject: Re: Issue when using jit_label() before a jump or branch
Date: Sat, 29 Jan 2022 17:42:24 +0000

Hi Franz,

I'm not really sure what could be the problem, sorry.

One thing I noticed though, is that you use jit_forward(). You actually do not need to use that.

Try this:

label = jit_label();
jump = jit_jmpi();

jit_addi(JIT_R0, JIT_R0, 42);

jit_patch(jump);

jit_addi(JIT_R0, JIT_R0, 42);

jump2 = jit_jmpi();
jit_patch_at(jump2, label);

jit_retr(JIT_R0);

A second thing to try: replace your jit_label() with a jit_indirect(). I actually always do that for backwards branches.

Cheers,
-Paul


Le dim., janv. 23 2022 at 13:00:13 +0100, Franz Flasch <franz.flasch@gmx.at> a écrit :
Hi!

I think there is kind of an issue in GNU lightning when using a label as jump target if this label was created immediately before a jump or branch. Here is an example:


    /* get a label here */
     label = jit_label();

     /* Prepare forward jump */
     jump = jit_jmpi();
     forward = jit_forward();
     jit_patch_at(jump, forward);

     /* Some random instructions */
     jit_addi(JIT_R0, JIT_R0, 42);

     /* Link forward jump */
     jit_link(forward);

     /* Some random instructions */
     jit_addi(JIT_R0, JIT_R0, 42);

     /* Now prepare backward jump to first label */
     jump2 = jit_jmpi();
     jit_patch_at(jump2, label);

     jit_retr(JIT_R0);


The above instructions result into the following GNU lightning instructions:

    L0: %rax /* prolog */
         jmpi L2
     L4: %rax
         addi %rax %rax 0x2a
     L2: %rax
         addi %rax %rax 0x2a
         jmpi L2
     L5: %rax
         retr %rax
          \__ live %rax
          \__ ret
     L3: /* epilog */


As you can see the second jump is wrong. It jumps back to L2 even though it should jump to L0 (before the first jump). When I use the label from the first jump like this

    jit_patch_at(jump2, jump);

it works as expected.

But why can't I use a normal label here? Is this the intended behavior or is this a bug?

If there is another instruction between jit_label() and jit_jmpi() it also works as expected.


The same behavior also occurs with branches.


I could just use the first jump label to solve this issue, however my code generation engine currently does not know about this in advance so I rely on normal labels to work correctly.


Thanks,
 Franz







reply via email to

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