[Top][All Lists]

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

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

From: Franz Flasch
Subject: Re: Issue when using jit_label() before a jump or branch
Date: Sun, 30 Jan 2022 17:33:26 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0

Hi Paul!

Thanks for your answer! Meanwhile I came to the conclusion, that it might be an internal optimization of GNU Lightning causing this. Considering my code example it does not make any

difference to jump back to the label before the jump. I thought that I had also cases where the optimization was wrong, but at this point I am not sure anymore ':-)

I also tried jit_patch() and jit_indirect() but it does not make any difference at all.

Anyway, thanks for the tips! I didn't know that I can use jit_patch() directly for forward jumps. That's way easier to use.

Thanks, regards,

On 1/29/22 6:42 PM, Paul Cercueil wrote:
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_addi(JIT_R0, JIT_R0, 42);

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


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


Le dim., janv. 23 2022 at 13:00:13 +0100, Franz Flasch <> a écrit :

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 */

     /* 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);


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.


reply via email to

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