lua-users home
lua-l archive

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


On Tue, Apr 28, 2020 at 2:50 PM Rodrigo Azevedo <rodrigoams@gmail.com> wrote:
Dear,
I was looking at the opcodes from lua-5.4.0-rc1 using "luac -l", and the simple code below

local v1 = 1
local v2 = 1.
local v3 = 1.0
local v4 = 1.01

is translated to

1 [1] VARARGPREP 0
2 [1] LOADI     0 1
3 [2] LOADF     1 1
4 [3] LOADF     2 1
5 [4] LOADK     3 0 ; 1.01

So, why "1." and "1.0" use LOADF but "1.01" uses LOADK?

The opcodes are explained in lopcodes.h.

LOADI loads an integer from an "immediate" value -- a value encoded directly in the instruction itself, eliminating the need to store this value in the list of constants.

LOADF does the same as LOADI, but casts the integer to a floating-point number.

Since immediate values can only be integers, LOADK exists to load a constant by indexing the list of constants, where 1.01 is stored.

Note that luac -l sometimes prints a value after the opcode arguments and a semicolon, as it does here with 1.01. This is done for opcodes that index the constant list, to show exactly what constant is being used. When such a notation is not present, it typically means that the opcode contains all needed values within its arguments.

Try listing the opcodes of this and compare it to the above, noting the arguments (I haven't tested this since I don't have a Lua 5.4 handy on Windows):

local v1 = 1337
local v2 = -1138.
local v3 = 42.0
local v4 = 3.14159
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org