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