Compare commits

..

5 Commits

Author SHA1 Message Date
Enrico Lumetti 283be71809 Get number constant reading working 2022-05-12 11:57:48 +02:00
Enrico Lumetti 5aa1a031fa Add C function u64_to_num 2022-05-12 11:57:20 +02:00
Enrico Lumetti f457e31740 Add hello world C lib 2022-05-12 11:03:04 +02:00
Enrico Lumetti 8b9eafe94c Fix read uleb 2022-05-12 10:53:08 +02:00
Enrico Lumetti dd42c2a43f Use hex identifiers for opcodes to ease debugging 2022-05-12 10:52:47 +02:00
12 changed files with 57 additions and 6 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.luo
lunaclib.so

4
build.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
cc -shared $(pkg-config --cflags luajit) lunaclib.c $(pkg-config --libs luajit) -o lunaclib.so

31
lunaclib.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdint.h>
#include <lua.h>
#include <lauxlib.h>
static int u64_to_num(lua_State* L) {
#ifndef LUA_NUMBER_DOUBLE
#error Lua must be compiled with number as a double floating point
#endif
const uint64_t h1 = luaL_checkint(L, 1);
const uint64_t h2 = luaL_checkint(L, 2);
const uint64_t stacked = (h2 << 32) | h1;
double res = *(double *)((char *)&stacked);
lua_pushnumber(L, res);
return 1;
}
static const struct luaL_Reg lib[] = {
{"u64_to_num", u64_to_num},
{NULL, NULL}
};
int luaopen_lunaclib(lua_State *L) {
luaL_openlib(L, "lunaclib", lib, 0);
return 1;
}

View File

@ -2,7 +2,7 @@ local readbc = require('readbc')
local vm = require('vm')
local VM = vm.VM
fp = io.open('tests/test1.luo')
fp = io.open('snippets/test1.luo')
header = readbc.read_header(fp)
proto = readbc.read_proto(fp)
fp:close()

View File

@ -68,9 +68,9 @@ local Metamethod = {
}
local Opcodes_defs = gen_opcodes {
opcode(37, 'POW', Mode.dst, Mode.none, Mode.lits, Metamethod.none),
opcode(41, 'KSHORT', Mode.dst, Mode.var, Mode.var, Metamethod.pow),
opcode(75, 'RET0', Mode.rbase, Mode.none, Mode.lit, Mode.none),
opcode(0x25, 'POW', Mode.dst, Mode.none, Mode.lits, Metamethod.none),
opcode(0x29, 'KSHORT', Mode.dst, Mode.var, Mode.var, Metamethod.pow),
opcode(0x4b, 'RET0', Mode.rbase, Mode.none, Mode.lit, Mode.none),
}
local function decode(ins)

View File

@ -1,6 +1,8 @@
-- module for reading serialized lua bytecode
local bit=require('bit')
local bit = require('bit')
local lunaclib = require('lunaclib')
local u64_to_num = lunaclib.u64_to_num
local HEADER_MAGIC = {27, string.byte('L'), string.byte('J')}
local FLAG_STRIP = 0x02
@ -53,6 +55,7 @@ local function read_uleb128(fp)
while not last_byte do
local uleb_byte = read_u8(fp)
res = bit.bor(res, bit.lshift(bit.band(uleb_byte, 0x7f), shift))
shift = shift + 7
if bit.band(uleb_byte, 0x80) == 0 then
last_byte = true
@ -124,7 +127,16 @@ local function read_proto(fp)
local bcins = read_u32n(fp, numbc)
local uvdata = read_u16n(fp, numuv)
-- local kgc
-- local numkn
local num = {}
for i = 1, numkn do
local x = read_uleb128(fp)
if bit.band(x, 1) == 1 then
local y = read_uleb128(fp)
num[i] = u64_to_num(x, y)
else
num[i] = (x/2)-1
end
end
-- local debug = fp:read(debugLen)
-- TODO: remove when reading of kgc, numkn and debug is implemented

3
snippets/test5.lua Normal file
View File

@ -0,0 +1,3 @@
local num = 2^15+1
local num64 = 2^32
local fp = 0.75