Skip to content

Commit

Permalink
Major cleanup of lua-compiler & lua-cross-compiler scripts.
Browse files Browse the repository at this point in the history
Remove dead/old/broken compiling modes.
  • Loading branch information
Neopallium committed Jun 18, 2012
1 parent aeb602a commit 65123d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 102 deletions.
84 changes: 14 additions & 70 deletions llvm-lua/lua-compiler.in
Expand Up @@ -5,7 +5,7 @@
DIR=`realpath $0`
DIR=`dirname $DIR`

CC=gcc
CC=clang
LIBTOOL="libtool --tag=CC --silent"
RPATH=`pwd`
LLVM_LUAC="./llvm-luac"
Expand All @@ -19,8 +19,7 @@ if [[ ! -x "$LLVM_LUAC" ]]; then
LLVM_LUAC=`which llvm-luac`
fi

TARGET=
FORCE_TARGET="0"
ARCH=
CPU=i686
#CPU=pentium4
#CPU=athlon64
Expand All @@ -31,9 +30,8 @@ OUTPUT_FILE=""
DEBUG="0"
KEEP_TMPS="0"
STATIC="0"
MODE="full_bc"
MODE="standalone"
EXPORT_SYMBOLS=" -Wl,-E "
LLC_FLAGS=" -relocation-model=pic "
EXTRA_ARGS=
LIBS=

Expand Down Expand Up @@ -61,19 +59,8 @@ OPTIONS:
info and gcc debug symbols are enabled.
-keep-tmps - Don't delete temp. files generated by intermediate stages. Use only
for debuging generated code or if you are really curious!
-target=<target> - <target> is passed to llc as '-target=<target>', see llc --version for targets
-march=<march> - <march> is passed to llc as '-march=<march>', see llc --version for targets
-mcpu=<arch> - <cpu> is passed to gcc as '-march=<cpu>'
-mode=<mode> - Switch how LLVM bitcode is generated and compiled into native code.
- full_bc - (default mode) compiles Lua scripts into LLVM bitcode
linked with bitcode liblua_main.bc then converted to assembly
before being compiled with gcc to a native executable.
- lua_mod - Only used by the '-lua-module' option. Don't use
it directly.
The following modes are for testing only:
- cbe - compile Lua script into C-code from LLVM bitcode, then use
gcc to compile to native executable binary.
- c - compiles Lua scripts into C-code.
- ll - compiles Lua scripts into LLVM IR code.
-****** - All other options passed to 'llvm-luac'. See below for a list of
options supported by 'llvm-luac'.
Expand All @@ -97,11 +84,11 @@ for arg in "$@" ; do
case "$arg" in
-lua-mod*) MODE="lua_mod"; EXTRA_ARGS="$EXTRA_ARGS $arg" ;;
-static) STATIC="1" ;;
-c++) CC=g++ ;;
-c++) CC=clang++ ;;
-debug) DEBUG="1" ;;
-keep-tmps) KEEP_TMPS="1" ;;
-mode=*) MODE=` echo "$arg" | sed -e 's/-mode=//'` ;;
-target=*) FORCE_TARGET="1"; TARGET=` echo "$arg" | sed -e 's/-target=//'` ;;
-march=*) ARCH=` echo "$arg" | sed -e 's/-march=//'` ;;
-mcpu=*) FORCE_CPU="1"; CPU=` echo "$arg" | sed -e 's/-mcpu=//'` ;;
-help|--help|-h) usage ;;
-version|--version|-v) version ;;
Expand All @@ -115,7 +102,6 @@ done
# find the mode's output file extension.
OUTPUT_EXT=""
case "$MODE" in
c) OUTPUT_EXT=".c" ;;
ll) OUTPUT_EXT=".ll" ;;
lua_mod) OUTPUT_EXT=".so" ;;
esac
Expand All @@ -128,46 +114,25 @@ fi
# get source file's path & filename.
FPATH=`dirname ${FILE}`
FNAME=`basename ${FILE}`
# strip extension from filename.
FILE="${FILE%\.*}"

# select debug/optimize parameters.
if [[ $DEBUG == "1" ]]; then
CFLAGS=" -O0 -ggdb "
LUA_FLAGS=" -O0 -g "
#LUA_FLAGS=" -O3 -do-not-inline-opcodes "
#CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall "
OPT_FLAGS=" -disable-opt "
if [[ ! -z $CPU && $FORCE_CPU == "1" ]]; then
CFLAGS=" -march=$CPU $CFLAGS "
fi
else
LUA_FLAGS=" -O3 -s "
#LUA_FLAGS=" -O3 -g "
#CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall "
CFLAGS=" -O3 -fomit-frame-pointer -pipe "
if [[ ! -z $CPU ]]; then
CFLAGS=" -march=$CPU $CFLAGS "
fi
OPT_FLAGS=" -O3 -std-compile-opts -tailcallelim "
LLC_FLAGS=" -tailcallopt $LLC_FLAGS "
fi
if [[ ! -z $CPU && $FORCE_CPU == "1" ]]; then
LLC_FLAGS=" -mcpu=$CPU $LLC_FLAGS "
CFLAGS=" -mcpu=$CPU $LLC_FLAGS "
fi

if [[ $MODE == "cbe" ]]; then
EXTRA_ARGS="$EXTRA_ARGS -no-main "

# path to liblua_main.a
LIBS="$LIBS -L$DIR -L$DIR/../lib -L$PREFIX/lib "
elif [[ $MODE == "c" ]]; then
EXTRA_ARGS="$EXTRA_ARGS -no-main "
else
# if mode not "cbe" or "c"
if [[ ! -z $TARGET && $FORCE_TARGET == "1" ]]; then
LLC_FLAGS=" -march=$TARGET $LLC_FLAGS "
fi
if [[ ! -z $ARCH && $FORCE_ARCH == "1" ]]; then
CFLAGS=" -march=$ARCH $LLC_FLAGS "
fi

#
Expand All @@ -181,35 +146,14 @@ TMPS="${FILE}.bc"

# use one of the compile modes.
case "$MODE" in
cbe)
TMPS="$TMPS ${FILE}_opt.bc ${FILE}_run.c"
echo_cmd opt $OPT_FLAGS -o ${FILE}_opt.bc ${FILE}.bc
echo_cmd llc $LLC_FLAGS --march=c -o ${FILE}_run.c ${FILE}_opt.bc
echo_cmd $CC $EXPORT_SYMBOLS $CFLAGS $LIBS -o ${OUTPUT_FILE} ${FILE}_run.c -lllvm-lua_main -lm -ldl
;;
c)
TMPS="$TMPS ${FILE}_opt.bc"
echo_cmd opt $OPT_FLAGS -o ${FILE}_opt.bc ${FILE}.bc
echo_cmd llc $LLC_FLAGS --march=c -o ${OUTPUT_FILE} ${FILE}_opt.bc
;;
ll)
echo_cmd llvm-dis -o ${OUTPUT_FILE} ${FILE}.bc
;;
full_bc)
TMPS="$TMPS ${FILE}_opt.bc ${FILE}_run.bc ${FILE}_run.s"
echo_cmd opt $OPT_FLAGS -o ${FILE}_run.bc ${FILE}.bc
echo_cmd llc $LLC_FLAGS -filetype=asm -o ${FILE}_run.s ${FILE}_run.bc
echo_cmd $CC $EXPORT_SYMBOLS $CFLAGS -o ${OUTPUT_FILE} ${FILE}_run.s -lm -ldl
standalone)
TMPS="$TMPS ${FILE}_run.s"
echo_cmd $CC $EXPORT_SYMBOLS $CFLAGS -o ${OUTPUT_FILE} ${FILE}.bc -lm -ldl
;;
lua_mod)
TMPS="$TMPS ${FILE}_opt.bc ${FILE}_mod.s ${FPATH}/${FNAME}.lo ${FPATH}/lib${FNAME}.la"
echo_cmd opt $OPT_FLAGS -o ${FILE}_opt.bc ${FILE}.bc
echo_cmd llc $LLC_FLAGS -filetype=asm -o ${FILE}_mod.s ${FILE}_opt.bc || {
echo "Error compiling LLVM bitcode to assembly code."
exit 1;
}
TMPS="$TMPS ${FILE}_mod.s ${FPATH}/${FNAME}.lo ${FPATH}/lib${FNAME}.la"
# compile assembly code to object file.
$LIBTOOL --mode=compile $CC $CFLAGS -c -o ${FILE}.lo ${FILE}_mod.s
$LIBTOOL --mode=compile $CC $CFLAGS -c -o ${FILE}.lo ${FILE}.bc
# compile to dynamic module
if [[ $STATIC == "0" ]]; then
$LIBTOOL --mode=link $CC -rpath ${RPATH} -o ${FPATH}/lib${FNAME}.la ${FILE}.lo && \
Expand Down
47 changes: 15 additions & 32 deletions llvm-lua/lua-cross-compiler.in
Expand Up @@ -18,13 +18,13 @@ fi

ARCH=@CROSS_ARCH@
CPU=@CROSS_CPU@
FORCE_CPU="0"
MODULE="0"
NO_ASSEMBLE="0"
FILE=lua-native-out
FILES=""
DEBUG="0"
KEEP_TMPS="0"
MODE="asm"
EXTRA_ARGS=

function echo_cmd() {
Expand All @@ -50,9 +50,8 @@ OPTIONS:
info and gcc debug symbols are enabled.
-keep-tmps - Don't delete temp. files generated by intermediate stages. Use only
for debuging generated code or if you are really curious!
-mode=<mode> - Switch how LLVM bitcode is generated and compiled into native code.
- asm - compiles Lua scripts into asm-code.
- cbe - compiles Lua scripts into C-code. (only for testing, don't use it.)
-march=<march> - <march> is passed to llc as '-march=<march>', see llc --version for targets
-mcpu=<arch> - <cpu> is passed to gcc as '-march=<cpu>'
-****** - All other options passed to 'llvm-luac'. See below for a list of
options supported by 'llvm-luac'.
Expand All @@ -78,7 +77,8 @@ for arg in "$@" ; do
-debug) DEBUG="1" ;;
-no-assemble) NO_ASSEMBLE="1" ;;
-keep-tmps) KEEP_TMPS="1" ;;
-mode=*) MODE=` echo "$arg" | sed -e 's/-mode=//'` ;;
-march=*) ARCH=` echo "$arg" | sed -e 's/-march=//'` ;;
-mcpu=*) FORCE_CPU="1"; CPU=` echo "$arg" | sed -e 's/-mcpu=//'` ;;
-help|--help|-h) usage ;;
-version|--version|-v) version ;;
-o|-L) CONSUME="$arg" ;;
Expand All @@ -91,10 +91,7 @@ done
# find the mode's output file extension.
OUTPUT_EXT=""
MODE_OUT=""
case "$MODE" in
cbe) OUTPUT_EXT=".c" ;;
asm) OUTPUT_EXT=".s" ;;
esac
OUTPUT_EXT=".s"
if [[ -z "$OUTPUT_FILE" ]]; then
if [[ $MODULE == "0" && $NO_ASSEMBLE == "0" ]]; then
OUTPUT_FILE="${FILE}"
Expand All @@ -109,14 +106,17 @@ MODE_OUT="${FILE}${OUTPUT_EXT}"
# select debug/optimize parameters.
if [[ $DEBUG == "1" ]]; then
LUA_FLAGS=" -O0 -g "
OPT_FLAGS=" -disable-opt "
LLC_FLAGS=" "
LLC_FLAGS=" -O0 "
else
LUA_FLAGS=" -O3 -s "
OPT_FLAGS=" -O3 -std-compile-opts -tailcallelim "
LLC_FLAGS=" -tailcallopt "
LLC_FLAGS=" -O3 -tailcallopt "
fi
if [[ ! -z "$CPU" ]]; then
LLC_FLAGS=" -mcpu=$CPU $LLC_FLAGS "
fi
if [[ ! -z $ARCH ]]; then
LLC_FLAGS=" -march=$ARCH $LLC_FLAGS "
fi
LLC_FLAGS=" -mcpu=$CPU $LLC_FLAGS "

#
# run llvm-luac to compile Lua source/bytecode to LLVM bitcode.
Expand All @@ -126,24 +126,7 @@ echo_cmd $LLVM_LUAC $EXTRA_ARGS $LUA_FLAGS -bc -o ${FILE}.bc ${FILES} || {
exit 1;
}
TMPS="${FILE}.bc"

# use one of the compile modes.
case "$MODE" in
cbe)
TMPS="$TMPS ${FILE}_opt.bc ${FILE}_run.c"
echo_cmd opt $OPT_FLAGS -o ${FILE}_opt.bc ${FILE}.bc
echo_cmd llc $LLC_FLAGS --march=c -o ${FILE}_run.c ${FILE}_opt.bc
echo_cmd $CC $EXPORT_SYMBOLS $CFLAGS $LIBS -o ${OUTPUT_FILE} ${FILE}_run.c -lllvm-lua_main -lm -ldl
;;
asm)
TMPS="$TMPS ${FILE}_opt.bc"
echo_cmd opt $OPT_FLAGS -o ${FILE}_opt.bc ${FILE}.bc
echo_cmd llc -march=$ARCH $LLC_FLAGS -filetype=asm -o ${MODE_OUT} ${FILE}_opt.bc
;;
*)
echo "Invalid compile mode: $MODE"
;;
esac
echo_cmd llc $LLC_FLAGS -filetype=asm -o ${MODE_OUT} ${FILE}.bc

# compile to stand-alone
if [[ $MODULE == "0" && $NO_ASSEMBLE == "0" ]]; then
Expand Down

0 comments on commit 65123d2

Please sign in to comment.