Can Lua bytecode for status failures "if"?

I am writing Lua code that can read the bytecode generated by the string.dump () function. I assume (since it allows several optimizations and less coding) that all OP_JMP instructions increment the instruction pointer forward when used for if statements. They could technically jump back because they use the sBx value (which may be negative). I'm only interested in the bytecode of the if statements from the standard Lua 5.1 implementation.

I used chunkspy (awesome btw tool) to look at the bytecode for several samples.

Here is the basic if statement:

a, b = 1, 2 if a == b then print '=' elseif a < b then print '<' else print '>' end 

He makes four jumps, none of which are negative:

 [08] jmp 4; to [13] [12] jmp 11; to [24] [16] jmp 4; to [21] [20] jmp 3; to [24] 

I tried to find the answers in the Lua source code , but it just turned out to be confusing (I'm sure this is super elegant code if I took the time to handle this).

Does anyone know of a case for if statements, where the lua OP_JMP statement has a negative value for sBx, or knows if they are always positive values?

+6
source share
1 answer

Short answer: IF statements cannot create negative JMPs (on any optimized compiler independent of langauage). Lua OP_JMP may be negative for goto loops and statements ( http://lua-users.org/wiki/GotoStatement )

Long answer: This is because the reverse JUMP is generated only by any compiler if it needs to repeat the specific code that it has already translated (for while while). If it executes the β€œnew” IF statement, it will always put the conditional JMP and the resulting code / bytecode as the next instance.

On the other hand, a β€œstrange” compiler can create negative IF transitions. But that does not make sense. In order to have a negative IF JMP in a certain place, in the past he already skipped that place (positive JMP), so it cannot be optimized in terms of execution speed.

+5
source

Source: https://habr.com/ru/post/927416/


All Articles