Skip to content

Commit

Permalink
Use native LuaJIT embedded executable bytecode loader
Browse files Browse the repository at this point in the history
Required to use "link_whole" meson's option to link the library
containing the bytecode and "export_dynamic" to true.

Explanation here:

http://luajit.org/running.html

and

https://stackoverflow.com/questions/19416981/running-luajit-object-file-from-c

The rename of the lang/* files was needed because of how the native
bytecode loader translate '-' into '_'.
  • Loading branch information
franko committed May 21, 2020
1 parent 9c03ed6 commit 707d6a1
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 100 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
*~
.out*
.deps
*.o
*.a
build*
tests/log/*
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions lang/compile.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local lex_setup = require('lang.lexer')
local parse = require('lang.parser')
local lua_ast = require('lang.lua-ast')
local lua_ast = require('lang.lua_ast')
local reader = require('lang.reader')

-- Two kind of backend can be used to generate the code from the AST:
-- - "generator", generates LuaJIT bytecode
-- - "luacode-generator", generates Lua code
-- - "luacode_generator", generates Lua code
--
-- Both can be used interchangeably, they take the AST tree and produce
-- a string that can be passed to the function "loadstring".
Expand All @@ -24,7 +24,7 @@ end
local function compile(reader, filename, options)
local generator
if options and options.code then
generator = require('lang.luacode-generator')
generator = require('lang.luacode_generator')
else
generator = require('lang.generator')
end
Expand Down
4 changes: 2 additions & 2 deletions lang/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
--

local bc = require('lang.bytecode')
local const_eval = require("lang.ast-const-eval")
local boolean_const_eval = require("lang.ast-boolean-const-eval")
local const_eval = require("lang.ast_const_eval")
local boolean_const_eval = require("lang.ast_boolean_const_eval")

local ID = 0
local function genid()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lang/lua-ast.lua → lang/lua_ast.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local id_generator = require("lang.id-generator")
local id_generator = require("lang.id_generator")

local function build(kind, node)
node.kind = kind
Expand Down
2 changes: 1 addition & 1 deletion lang/luacode-generator.lua → lang/luacode_generator.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- luacode-generator.lua
-- luacode_generator.lua
--
-- This file is part of the LuaJIT Language Toolkit.
--
Expand Down
36 changes: 25 additions & 11 deletions lang/meson.build
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
lang_sources = ['ast-boolean-const-eval.lua', 'ast-const-eval.lua', 'bcread.lua', 'bcsave.lua', 'bytecode.lua', 'compile.lua', 'generator.lua', 'lexer.lua', 'id-generator.lua', 'lua-ast.lua', 'operator.lua', 'parser.lua', 'reader.lua']
lang_sources = [
'ast_boolean_const_eval.lua',
'ast_const_eval.lua',
'bcread.lua',
'bcsave.lua',
'bytecode.lua',
'compile.lua',
'generator.lua',
'lexer.lua',
'id_generator.lua',
'lua_ast.lua',
'operator.lua',
'parser.lua',
'reader.lua',
]

luajit = find_program('luajit')

lang_bc_headers = []

if bytecode_preload
lang_bc_sources = []
foreach lua_source : lang_sources
lang_bc_headers += custom_target(lua_source + '.h',
source_basename = lua_source.split('.')[0]
module_name = 'lang_' + source_basename.underscorify()
message(module_name + ' ' + source_basename + '.c')
lang_bc_sources += custom_target(source_basename + '.c',
input: lua_source,
output: '@BASENAME@.h',
command: [luajit, '-b', '@INPUT@', '@OUTPUT@']
output: '@BASENAME@.c',
command: [luajit, '-b', '-n', module_name, '@INPUT@', '@OUTPUT@']
)
endforeach
endif

lua_module_install_dir = 'share/lua/5.1'

if not bytecode_preload
lang_bc_lib = static_library('langbc', lang_bc_sources)
else
lua_module_install_dir = 'share/lua/5.1'
install_data(lang_sources, install_dir: lua_module_install_dir + '/lang')
endif
2 changes: 0 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ pkg = import('pkgconfig')

bytecode_preload = get_option('preload')

bc_headers_dir = include_directories('.')

subdir('lang')
subdir('src')
4 changes: 0 additions & 4 deletions src/language.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#include "lualib.h"
#include "language.h"

#ifdef BC_PRELOAD
#include "language_bcloader.h"
#endif

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

Expand Down
58 changes: 0 additions & 58 deletions src/language_bcloader.c

This file was deleted.

8 changes: 0 additions & 8 deletions src/language_bcloader.h

This file was deleted.

15 changes: 9 additions & 6 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ endif
lang_sources = ['language.c', 'language_loaders.c']

if bytecode_preload
lang_sources += 'language_bcloader.c'
lang_cflags += '-DBC_PRELOAD'
lang_whole_libs = [lang_bc_lib]
lang_export_dynamic = true
else
lang_whole_libs = []
lang_export_dynamic = false
endif

luajit_dep = dependency('luajit')

liblang = static_library('ljlangtk',
lang_sources + lang_bc_headers,
include_directories: bc_headers_dir,
lang_sources,
c_args: lang_cflags,
dependencies: luajit_dep,
install: true,
)

luajit_x = executable('luajit-x',
['luajit-x.c'] + lang_bc_headers,
include_directories: bc_headers_dir,
'luajit-x.c',
dependencies: luajit_dep,
c_args: lang_cflags,
link_with: liblang,
link_whole: lang_whole_libs,
export_dynamic: lang_export_dynamic,
install: true,
)

Expand Down

0 comments on commit 707d6a1

Please sign in to comment.