Skip to content

Commit

Permalink
Merge pull request #424 from pallene-lang/remove-pallenelib
Browse files Browse the repository at this point in the history
Move pallenelib to inside the Lua interpreter
  • Loading branch information
hugomg authored Jul 25, 2021
2 parents 9705276 + 2f85fc4 commit 2c9f338
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Build
run: |
luarocks make
make linux-noreadline
(cd vm && make linux)
- name: Install Busted
run: luarocks install busted
Expand Down
31 changes: 0 additions & 31 deletions Makefile

This file was deleted.

19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sudo apt install parallel # for Ubuntu & Debian-based distros
sudo dnf install parallel # for Fedora
```

Pallene requires Lua 5.3 to be installed on your system.
Pallene requires Lua 5.3 or newer to be installed on your system.
You can either install it [from source](https://www.lua.org/ftp/) or via the package manager for your Linux distro.
If you install via the package manager then make sure to also install the Lua headers, which are often in a separate "development" package.

Expand Down Expand Up @@ -70,18 +70,19 @@ $ echo 'eval `luarocks path`' >> ~/.bashrc
```
For further information, consult the [Luarocks documentation](https://github.com/luarocks/luarocks/wiki/path).

### Compiling the runtime libraries
### Compiling the custom interpreter

Pallene must be run against a custom-built version of the Lua interpreter, as
well as the Pallene runtime library. Both of these are written in C and must be
compiled before the Pallene compiler can be used.
Pallene must be run against a custom-built version of the Lua interpreter.
This custom version of Lua 5.4 doesn't have to be the same one that you will use to run the compiler itself,
or to install the Luarocks packages.

These two components can be built through the Makefile we provide. The command
to be used depends on your operating system:
To compile the custom version of Lua, follow the instructions found the [Lua README](https://www.lua.org/manual/5.4/readme.html), also found in the vm/doc/readme.html file.

For Linux, these are the commands you need to run:

```sh
make linux-readline # for Linux
make macosx # for MacOS
cd vm
make linux-readline -j
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion lint-project
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ echo "=== Other checks ==="
for file in \
pallene/*.lua spec/*.lua \
examples/*/*.lua examples/*/*.pln \
runtime/*.c runtime/*.h
vm/src/pallene_core.c vm/src/pallene_core.h
do
if grep "^$tab" "$file"; then
error "File $file has tab-based indentation"
Expand Down
1 change: 0 additions & 1 deletion pallene/c_compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function c_compiler.compile_o_to_so(in_filename, out_filename)
c_compiler.CFLAGS_SHARED,
"-o", util.shell_quote(out_filename),
util.shell_quote(in_filename),
"runtime/pallenelib.a"
})
end

Expand Down
4 changes: 2 additions & 2 deletions pallene/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ gen_cmd["GetTable"] = function(self, cmd, _func)

return util.render([[
{
static size_t cache = UINT_MAX;
static int cache = -1;
TValue *slot = pallene_getstr($field_len, $tab, $key, &cache);
${get_slot}
}
Expand All @@ -1289,7 +1289,7 @@ gen_cmd["SetTable"] = function(self, cmd, _func)
return util.render([[
{
TValue keyv; ${init_keyv}
static size_t cache = UINT_MAX;
static int cache = -1;
TValue *slot = pallene_getstr($field_len, $tab, $key, &cache);
if (PALLENE_UNLIKELY(isabstkey(slot))) {
TValue valv; ${init_valv}
Expand Down
22 changes: 0 additions & 22 deletions runtime/0001-Update-LUAI_FUNC.patch

This file was deleted.

6 changes: 5 additions & 1 deletion vm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix

# What to install.
TO_BIN= lua luac
TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
TO_INC= lua.hpp \
lapi.h lauxlib.h lcode.h lctype.h ldebug.h ldo.h lfunc.h lgc.h ljumptab.h \
llex.h llimits.h lmem.h lobject.h lopcodes.h lopnames.h lparser.h lprefix.h \
lstate.h lstring.h ltable.h ltm.h luaconf.h lua.h lualib.h lundump.h lvm.h \
lzio.h pallene_core.h
TO_LIB= liblua.a
TO_MAN= lua.1 luac.1

Expand Down
22 changes: 22 additions & 0 deletions vm/PALLENE-UPDATING
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
This file describes the adaptations we made to the Lua interpreter.
For the next release, we should consider preparing a proper patch file.

1) Make everything public in the luaconf.h

#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
defined(__ELF__) /* { */
#define LUAI_FUNC __attribute__((visibility("internal"))) extern
#else /* }{ */
#define LUAI_FUNC extern
#endif /* } */
---
#define LUAI_FUNC LUA_API /* Pallene exports everything */

2) Add the Linker Hack to the lua.c

See the end of the "lapi.c"

3) Copy the pallene_core files

vm/src/pallene_core.h
vm/src/pallene_core.c
14 changes: 14 additions & 0 deletions vm/src/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1453,3 +1453,17 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
}


/*
** PALLENE LINKER HACK
**
** When we build the final "lua" executable, bundling it with the "liblua.a" library, the linker
** only includes symbols from the object files where at least one of the symbols of the module is
** used by the executable itself. This is a problem for the Pallene runtime library, because those
** symbols are only going to be used by the extension modules, which we expect to dynamically link.
** The hacky workaround that we found is to compile the pallene_core stuff as part of the lapi.o,
** isntead of its own object file.
**
** As a bonus, we also avoid having to modify the Makefile. Conveniently, the set of header files
** that lapi.c depends on is a superset of the header files that pallene_core.c needs.
*/
#include "pallene_core.c"
3 changes: 3 additions & 0 deletions runtime/pallene_core.c → vm/src/pallene_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Please refer to the LICENSE and AUTHORS files for details
* SPDX-License-Identifier: MIT */

#define LUA_CORE

#include "pallene_core.h"

#include "lua.h"
Expand Down Expand Up @@ -159,6 +161,7 @@ void pallene_grow_array(lua_State *L, const char* file, int line, Table *arr, un

void pallene_io_write(lua_State *L, TString *str)
{
(void) L; /* unused parameter */
const char *s = getstr(str);
size_t len = tsslen(str);
fwrite(s, 1, len, stdout);
Expand Down
14 changes: 10 additions & 4 deletions runtime/pallene_core.h → vm/src/pallene_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@
#ifndef PALLENE_CORE_H
#define PALLENE_CORE_H

// This list on includes should include basically everything we need.
// It is copied from lapi.c (grep for PALLENE LINKER HACK for more details)

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"

#include <math.h>


#define PALLENE_NORETURN __attribute__((noreturn))
#define PALLENE_UNREACHABLE __builtin_unreachable()

Expand Down Expand Up @@ -245,9 +251,9 @@ void pallene_renormalize_array(
static const TValue PALLENE_ABSENTKEY = {ABSTKEYCONSTANT};

static inline
TValue *pallene_getshortstr(Table *t, TString *key, size_t *restrict cache)
TValue *pallene_getshortstr(Table *t, TString *key, int *restrict cache)
{
if (*cache < sizenode(t)) {
if (0 <= *cache && *cache < sizenode(t)) {
Node *n = gnode(t, *cache);
if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
return gval(n);
Expand All @@ -273,7 +279,7 @@ TValue *pallene_getshortstr(Table *t, TString *key, size_t *restrict cache)
}

static inline
TValue *pallene_getstr(size_t len, Table *t, TString *key, size_t *cache)
TValue *pallene_getstr(size_t len, Table *t, TString *key, int *cache)
{
if (len <= LUAI_MAXSHORTLEN) {
return pallene_getshortstr(t, key, cache);
Expand Down

0 comments on commit 2c9f338

Please sign in to comment.