diff --git a/.gitignore b/.gitignore index c6092e7..1e51b2c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/*.a src/lua src/luac +creduce/src/* diff --git a/creduce/Makefile b/creduce/Makefile new file mode 100644 index 0000000..439bc3d --- /dev/null +++ b/creduce/Makefile @@ -0,0 +1,276 @@ +# Makefile for building Lua +# See ../doc/readme.html for installation and customization instructions. + +# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= + +# Your platform. See PLATS for possible values. +PLAT= guess + +CC= gcc -std=gnu99 +# Note that for the Yk JIT we have to use -O0 for now. +CFLAGS= -O0 -Wall -Wextra -DLUA_COMPAT_5_3 $(SYSCFLAGS) $(MYCFLAGS) +LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) +LIBS= -lm $(SYSLIBS) $(MYLIBS) + +AR= ar rcu +RANLIB= ranlib +RM= rm -f +UNAME= uname + +SYSCFLAGS= +SYSLDFLAGS= +SYSLIBS= + +# YKFIXME: LYK_DEBUG is a debug flag for the Yk integration. Once YkLua is stable we can remove it. +MYCFLAGS=-DLYK_DEBUG +MYLDFLAGS= +MYLIBS= +MYOBJS= + +# Flags for enabling the Yk JIT. +# +# The user is expected to have: +# - put `yk-config` in the `PATH` +# - set `YK_BUILD_TYPE` to either `debug` or `release`. +YK_CFLAGS= -DUSE_YK \ + -DLUA_USE_JUMPTABLE=0 \ + `yk-config ${YK_BUILD_TYPE} --cflags --cppflags` +YK_LDFLAGS= `yk-config ${YK_BUILD_TYPE} --ldflags` +YK_LIBS= `yk-config ${YK_BUILD_TYPE} --libs` + +# If $(YK_BUILD_TYPE) is set then build with Yk JIT support. +ifneq ($(strip $(YK_BUILD_TYPE)),) +CC= `yk-config ${YK_BUILD_TYPE} --cc` +AR= `yk-config ${YK_BUILD_TYPE} --ar` rcu +RANLIB= `yk-config ${YK_BUILD_TYPE} --ranlib` +MYCFLAGS += $(YK_CFLAGS) +MYLDFLAGS += $(YK_LDFLAGS) +MYLIBS += $(YK_LIBS) +endif + +# Special flags for compiler modules; -Os reduces code size. +CMCFLAGS= + +# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= + +PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris + +LUA_A= liblua.a +CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lyk.o +LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o +BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) + +LUA_T= lua +LUA_O= lua.o + +LUAC_T= luac +LUAC_O= luac.o + +ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) +ALL_T= $(LUA_T) $(LUAC_T) $(LUA_A) +ALL_A= $(LUA_A) + +# onelua vars start +ONE_LUA_T= onelua +ONE_LUA_O= onelua.o +ONE_LUA_C= onelua.c + +ONE_T= one +ONE_O= one.o +ONE_C= one.c +# onelua vars end + +# Targets start here. +default: $(PLAT) + +all: $(ALL_T) + +o: $(ALL_O) + +a: $(ALL_A) + +$(LUA_A): $(BASE_O) + $(AR) $@ $(BASE_O) + $(RANLIB) $@ + +$(LUA_T): $(LUA_O) $(LUA_A) + $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) + +$(LUAC_T): $(LUAC_O) $(LUA_A) + $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) + +# onelua start +$(ONE_LUA_T): $(ONE_LUA_O) + $(CC) -o $@ $(LDFLAGS) $(ONE_LUA_O) $(LIBS) + +$(ONE_LUA_O): $(ONE_LUA_C) + $(CC) -c $(CFLAGS) -o $@ $(ONE_LUA_C) + +$(ONE_T): $(ONE_C) + $(CC) -o $@ $(LDFLAGS) $(ONE_C) $(LIBS) + +$(ONE_C): + $(CC) -c $(CFLAGS) -o $@ $(ONE_LUA_C) + +$(ONE_LUA_C): $(ONE_C) + $(CC) $(CFLAGS) -E $(ONE_C) -o $(ONE_LUA_C) + +$(ONE_LUA_T)_clean: + $(RM) $(ONE_LUA_T) $(ONE_LUA_C) $(ONE_LUA_O) + +# onelua end + +test: + ./$(LUA_T) -v + +clean: $(ONE_LUA_T)_clean + $(RM) $(ALL_T) $(ALL_O) + +depend: + @$(CC) $(CFLAGS) -MM l*.c + +echo: + @echo "PLAT= $(PLAT)" + @echo "CC= $(CC)" + @echo "CFLAGS= $(CFLAGS)" + @echo "LDFLAGS= $(LDFLAGS)" + @echo "LIBS= $(LIBS)" + @echo "AR= $(AR)" + @echo "RANLIB= $(RANLIB)" + @echo "RM= $(RM)" + @echo "UNAME= $(UNAME)" + +# Convenience targets for popular platforms. +ALL= all + +help: + @echo "Do 'make PLATFORM' where PLATFORM is one of these:" + @echo " $(PLATS)" + @echo "See doc/readme.html for complete instructions." + +guess: + @echo Guessing `$(UNAME)` + @$(MAKE) `$(UNAME)` + +AIX aix: + $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" + +bsd: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" + +c89: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89" + @echo '' + @echo '*** C89 does not guarantee 64-bit integers for Lua.' + @echo '*** Make sure to compile all external Lua libraries' + @echo '*** with LUA_USE_C89 to ensure consistency' + @echo '' + +FreeBSD NetBSD OpenBSD freebsd: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" + +generic: $(ALL) + +Linux linux: linux-noreadline + +linux-noreadline: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" + +linux-readline: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline" + +Darwin macos macosx: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" + +mingw: + $(MAKE) "LUA_A=lua54.dll" "LUA_T=lua.exe" \ + "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ + "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe + $(MAKE) "LUAC_T=luac.exe" luac.exe + +posix: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" + +SunOS solaris: + $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" + +# Targets that do not create files (not all makes understand .PHONY). +.PHONY: all $(PLATS) help test clean default o a depend echo + +# Compiler modules may use special flags. +llex.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c + +lparser.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c + +lcode.o: + $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c + +# DO NOT DELETE + +lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ + ltable.h lundump.h lvm.h +lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h +lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lgc.h lstring.h ltable.h lvm.h +lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h +ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ + ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h +ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ + lparser.h lstring.h ltable.h lundump.h lvm.h +ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ + ltm.h lzio.h lmem.h lundump.h +lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h +lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h +linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h +liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ + lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ + lstring.h ltable.h +lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h +loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ + ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ + lvm.h +lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h +loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ + llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ + ldo.h lfunc.h lstring.h lgc.h ltable.h +lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ + lstring.h ltable.h +lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h +lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h +ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h +lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.h +lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ + lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ + lundump.h +lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ + ltable.h lvm.h ljumptab.h +lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ + lobject.h ltm.h lzio.h +lyk.o: lyk.h lyk.c +# (end of Makefile) diff --git a/creduce/README.md b/creduce/README.md new file mode 100644 index 0000000..8fe194d --- /dev/null +++ b/creduce/README.md @@ -0,0 +1,34 @@ +# Creduce with c-vise + +See [cvise](https://github.com/marxin/cvise) for more information. + +## Setup + +Set global environment variables: + +```shell +YKLUA_HOME="/path/to/yklua" # local path to https://github.com/ykjit/yklua +YK_BUILD_TYPE=debug +``` + +Add `yk-config` to the global `PATH`: + +```shell +PATH=/path/to/yk/bin:${PATH} # local path to https://github.com/ykjit/yk/blob/master/bin +``` + +## Run + +Build `onelua.c` +```shell +sh ./build_onelua.sh +``` + +Run creduce with `cvise`: + +```shell +cd src +cvise ./cvise.example.sh onelua.c +``` + +> cvice will reduce the lines of the `onelua.c` file as long as `./cvise.example.sh` executes successfully. It will overwrite the `onelua.c` file in place. Original content can be found in `onelua.c.orig`. diff --git a/creduce/build_onelua.sh b/creduce/build_onelua.sh new file mode 100644 index 0000000..c6e2e72 --- /dev/null +++ b/creduce/build_onelua.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -eu + +if ! command -v cvise 2>&1 ; then + echo "Binary 'cvise' is not accessible in the system or not in the PATH." + echo "See installation instructions: https://github.com/marxin/cvise." + exit 1 +fi + +if [ -d "./src" ]; then + rm -fr "./src" +fi + +cp -r "../src" "./src" +cp "Makefile" "./src/Makefile" +cp ./cvise.example.sh "./src/cvise.example.sh" +cp "./one.c" "./src/one.c" + +cd "./src" + +make onelua diff --git a/creduce/cvise.example.sh b/creduce/cvise.example.sh new file mode 100755 index 0000000..617d2ee --- /dev/null +++ b/creduce/cvise.example.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e + +if [ -z "${YKLUA_HOME}" ]; then + echo "YKLUA_HOME environment variable is not defined."; + exit 1 +fi + +if [ -z "${YK_BUILD_TYPE}" ]; then + echo "YK_BUILD_TYPE environment variable is not defined."; + exit 1 +fi + +if ! command -v yk-config 2>&1 ; then + echo "yk-config is not found." + echo "See installation instructions: https://github.com/marxin/cvise." + exit 1 +fi + +cp -r "${YKLUA_HOME}/creduce/src" ./ +cd ./src + +make onelua + +# oracle +./onelua -e "print('hello world')" | grep "hello world" diff --git a/creduce/one.c b/creduce/one.c new file mode 100644 index 0000000..3848f38 --- /dev/null +++ b/creduce/one.c @@ -0,0 +1,117 @@ +/* +* one.c -- Lua core, libraries, and interpreter in a single file (Lua 5.4) +*/ + +/* default is to build the full interpreter */ +#ifndef MAKE_LIB +#ifndef MAKE_LUAC +#ifndef MAKE_LUA +#define MAKE_LUA +#endif +#endif +#endif + +/* choose suitable platform-specific features */ +/* some of these may need extra libraries such as -ldl -lreadline -lncurses */ +/* +#define LUA_USE_LINUX +#define LUA_USE_MACOSX +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN +#define LUA_USE_READLINE +*/ + +/* other specific features */ +/* +#define LUA_COMPAT_5_3 +#define LUA_32BITS +#define LUA_USE_C89 +#define LUA_C89_NUMBERS +*/ + +/* no need to change anything below this line ----------------------------- */ + +/* activate system definitions in lprefix.h */ +#include "lprefix.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* setup for luaconf.h */ +#define LUA_CORE +#define LUA_LIB +#define ltable_c +#define lvm_c +#include "luaconf.h" + +/* do not export internal symbols */ +#undef LUAI_FUNC +#undef LUAI_DDEC +#undef LUAI_DDEF +#define LUAI_FUNC static +#define LUAI_DDEC(def) /* empty */ +#define LUAI_DDEF static + +/* core -- used by all */ +#include "lyk.c" +#include "lzio.c" +#include "lctype.c" +#include "lopcodes.c" +#include "lmem.c" +#include "lundump.c" +#include "ldump.c" +#include "lstate.c" +#include "lgc.c" +#include "llex.c" +#include "lcode.c" +#include "lparser.c" +#include "ldebug.c" +#include "lfunc.c" +#include "lobject.c" +#include "ltm.c" +#include "lstring.c" +#include "ltable.c" +#include "ldo.c" +#include "lvm.c" +#include "lapi.c" + +/* auxiliary library -- used by all */ +#include "lauxlib.c" + +/* standard library -- not used by luac */ +#ifndef MAKE_LUAC +#include "lbaselib.c" +#include "lcorolib.c" +#include "ldblib.c" +#include "liolib.c" +#include "lmathlib.c" +#include "loadlib.c" +#include "loslib.c" +#include "lstrlib.c" +#include "ltablib.c" +#include "lutf8lib.c" +#include "linit.c" +#endif + +/* lua */ +#ifdef MAKE_LUA +#include "lua.c" +#endif + +/* luac */ +#ifdef MAKE_LUAC +#include "luac.c" +#endif