Skip to content

Commit ea5cfb9

Browse files
Added ChangeLog, Improved lua-compiler & lua-cross-compiler.
Added -mcpu option to *-compiler scripts. Changed -arch to -target option. Embed the install prefix into lua-compiler so it can find liblua_main.a Added gen_changelog.sh script to generate ChangeLog from svn history. git-svn-id: http://llvm-lua.googlecode.com/svn/trunk@103 f87b780f-1855-0410-a4d3-fd11cda3f415
1 parent 31bfd17 commit ea5cfb9

File tree

8 files changed

+662
-45
lines changed

8 files changed

+662
-45
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

ChangeLog

Lines changed: 534 additions & 0 deletions
Large diffs are not rendered by default.

gen_changelog.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
#
3+
4+
svn2cl --authors=AUTHORS --break-before-msg --group-by-day -o ChangeLog
5+

llvm-lua/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ if(CROSS_COMPILE)
202202
set_target_properties(llvm-luac PROPERTIES OUTPUT_NAME ${CROSS_TRIPLE}-llvm-luac)
203203
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/lua-cross-compiler.in" "${LUA_COMPILER}" @ONLY)
204204
else(CROSS_COMPILE)
205-
set(LUA_COMPILER "lua-compiler")
205+
set(LUA_COMPILER "${CMAKE_CURRENT_BINARY_DIR}/lua-compiler")
206+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/lua-compiler.in" "${LUA_COMPILER}" @ONLY)
206207
endif(CROSS_COMPILE)
207208

208209
#

llvm-lua/lua-compiler renamed to llvm-lua/lua-compiler.in

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
#!/usr/bin/env bash
22
#
33

4+
# path to lua-compiler
5+
DIR=`realpath $0`
6+
DIR=`dirname $DIR`
7+
48
CC=gcc
59
LIBTOOL="libtool --tag=CC --silent"
610
RPATH=`pwd`
711
LLVM_LUAC="./llvm-luac"
12+
PREFIX="@CMAKE_INSTALL_PREFIX@"
13+
14+
# find llvm-luac
15+
if [[ ! -x "$LLVM_LUAC" ]]; then
16+
LLVM_LUAC="$DIR/llvm-luac"
17+
fi
818
if [[ ! -x "$LLVM_LUAC" ]]; then
919
LLVM_LUAC=`which llvm-luac`
1020
fi
1121

12-
#ARCH=i686
13-
#ARCH=pentium4
14-
#ARCH=athlon64
15-
FORCE_ARCH="0"
22+
TARGET=
23+
FORCE_TARGET="0"
24+
CPU=i686
25+
#CPU=pentium4
26+
#CPU=athlon64
27+
FORCE_CPU="0"
1628
FILE=
1729
FILES=""
1830
OUTPUT_FILE=""
@@ -40,17 +52,19 @@ OPTIONS:
4052
info and gcc debug symbols are enabled.
4153
-keep-tmps - Don't delete temp. files generated by intermediate stages. Use only
4254
for debuging generated code or if you are really curious!
43-
-arch=<arch> - <arch> is passed to gcc as '-march=<arch>'
55+
-target=<target> - <target> is passed to llc as '-target=<target>', see llc --version for targets
56+
-mcpu=<arch> - <cpu> is passed to gcc as '-march=<cpu>'
4457
-mode=<mode> - Switch how LLVM bitcode is generated and compiled into native code.
45-
- cbe - compile Lua script into C-code from LLVM bitcode, then use
46-
gcc to compile to native executable binary.
4758
- full_bc - (default mode) compiles Lua scripts into LLVM bitcode
4859
linked with bitcode liblua_main.bc then converted to assembly
4960
before being compiled with gcc to a native executable.
50-
- c - compiles Lua scripts into C-code.
51-
- ll - compiles Lua scripts into LLVM IR code.
5261
- lua_mod - Only used by the '-lua-module' option. Don't use
5362
it directly.
63+
The following modes are for testing only:
64+
- cbe - compile Lua script into C-code from LLVM bitcode, then use
65+
gcc to compile to native executable binary.
66+
- c - compiles Lua scripts into C-code.
67+
- ll - compiles Lua scripts into LLVM IR code.
5468
-****** - All other options passed to 'llvm-luac'. See below for a list of
5569
options supported by 'llvm-luac'.
5670
@@ -78,7 +92,8 @@ for arg in "$@" ; do
7892
-debug) DEBUG="1" ;;
7993
-keep-tmps) KEEP_TMPS="1" ;;
8094
-mode=*) MODE=` echo "$arg" | sed -e 's/-mode=//'` ;;
81-
-arch=*) FORCE_ARCH="1"; ARCH=` echo "$arg" | sed -e 's/-arch=//'` ;;
95+
-target=*) FORCE_TARGET="1"; TARGET=` echo "$arg" | sed -e 's/-target=//'` ;;
96+
-mcpu=*) FORCE_CPU="1"; CPU=` echo "$arg" | sed -e 's/-mcpu=//'` ;;
8297
-help|--help|-h) usage ;;
8398
-version|--version|-v) version ;;
8499
-o|-L) CONSUME="$arg" ;;
@@ -113,35 +128,36 @@ if [[ $DEBUG == "1" ]]; then
113128
#CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall "
114129
OPT_FLAGS=" -disable-opt "
115130
LLC_FLAGS=" "
116-
if [[ ! -z $ARCH && $FORCE_ARCH == "1" ]]; then
117-
CFLAGS=" -march=$ARCH $CFLAGS "
131+
if [[ ! -z $CPU && $FORCE_CPU == "1" ]]; then
132+
CFLAGS=" -march=$CPU $CFLAGS "
118133
fi
119134
else
120135
LUA_FLAGS=" -O3 -s "
121136
#LUA_FLAGS=" -O3 -g "
122137
#CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall "
123138
CFLAGS=" -O3 -fomit-frame-pointer -pipe "
124-
if [[ ! -z $ARCH ]]; then
125-
CFLAGS=" -march=$ARCH $CFLAGS "
139+
if [[ ! -z $CPU ]]; then
140+
CFLAGS=" -march=$CPU $CFLAGS "
126141
fi
127142
OPT_FLAGS=" -O3 -std-compile-opts -tailcallelim -tailduplicate "
128143
LLC_FLAGS=" -tailcallopt "
129144
fi
130-
if [[ ! -z $ARCH && $FORCE_ARCH == "1" ]]; then
131-
LLC_FLAGS=" -march=$ARCH $LLC_FLAGS "
145+
if [[ ! -z $CPU && $FORCE_CPU == "1" ]]; then
146+
LLC_FLAGS=" -mcpu=$CPU $LLC_FLAGS "
132147
fi
133148

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

137-
# find path to liblua_main.a
138-
# TODO: find a better way.
139-
DIR=`dirname $LLVM_LUAC`
140-
LIBS="$LIBS -L$DIR -L$DIR/../lib "
141-
DIR=`dirname $0`
142-
LIBS="$LIBS -L$DIR -L$DIR/../lib "
152+
# path to liblua_main.a
153+
LIBS="$LIBS -L$DIR -L$DIR/../lib -L$PREFIX/lib "
143154
elif [[ $MODE == "c" ]]; then
144155
EXTRA_ARGS="$EXTRA_ARGS -no-main "
156+
else
157+
# if mode not "cbe" or "c"
158+
if [[ ! -z $TARGET && $FORCE_TARGET == "1" ]]; then
159+
LLC_FLAGS=" -march=$TARGET $LLC_FLAGS "
160+
fi
145161
fi
146162

147163
#
@@ -158,27 +174,38 @@ TMPS="${FILE}.bc"
158174
case "$MODE" in
159175
cbe)
160176
TMPS="$TMPS ${FILE}_opt.bc ${FILE}_run.c"
161-
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc && \
162-
llc $LLC_FLAGS -f --march=c -o ${FILE}_run.c ${FILE}_opt.bc && \
177+
echo "opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc"
178+
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc
179+
echo "llc $LLC_FLAGS -f --march=c -o ${FILE}_run.c ${FILE}_opt.bc"
180+
llc $LLC_FLAGS -f --march=c -o ${FILE}_run.c ${FILE}_opt.bc
181+
echo "$CC $CFLAGS $LIBS -o ${OUTPUT_FILE} ${FILE}_run.c -llua_main -lm -ldl"
163182
$CC $CFLAGS $LIBS -o ${OUTPUT_FILE} ${FILE}_run.c -llua_main -lm -ldl
164183
;;
165184
c)
166185
TMPS="$TMPS ${FILE}_opt.bc"
167-
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc && \
186+
echo "opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc"
187+
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc
188+
echo "llc $LLC_FLAGS -f --march=c -o ${OUTPUT_FILE} ${FILE}_opt.bc"
168189
llc $LLC_FLAGS -f --march=c -o ${OUTPUT_FILE} ${FILE}_opt.bc
169190
;;
170191
ll)
192+
echo "llvm-dis -f -o ${OUTPUT_FILE} ${FILE}.bc"
171193
llvm-dis -f -o ${OUTPUT_FILE} ${FILE}.bc
172194
;;
173195
full_bc)
174196
TMPS="$TMPS ${FILE}_opt.bc ${FILE}_run.bc ${FILE}_run.s"
175-
opt $OPT_FLAGS -f -o ${FILE}_run.bc ${FILE}.bc && \
176-
llc $LLC_FLAGS -f -filetype=asm -o ${FILE}_run.s ${FILE}_run.bc && \
197+
echo "opt $OPT_FLAGS -f -o ${FILE}_run.bc ${FILE}.bc"
198+
opt $OPT_FLAGS -f -o ${FILE}_run.bc ${FILE}.bc
199+
echo "llc $LLC_FLAGS -f -filetype=asm -o ${FILE}_run.s ${FILE}_run.bc"
200+
llc $LLC_FLAGS -f -filetype=asm -o ${FILE}_run.s ${FILE}_run.bc
201+
echo "$CC $CFLAGS -o ${OUTPUT_FILE} ${FILE}_run.s -lm -ldl"
177202
$CC $CFLAGS -o ${OUTPUT_FILE} ${FILE}_run.s -lm -ldl
178203
;;
179204
lua_mod)
180205
TMPS="$TMPS ${FILE}_opt.bc ${FILE}_mod.s ${FPATH}/${FNAME}.lo ${FPATH}/lib${FNAME}.la"
181-
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc && \
206+
echo "opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc"
207+
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc
208+
echo "llc $LLC_FLAGS -f -filetype=asm -o ${FILE}_mod.s ${FILE}_opt.bc"
182209
llc $LLC_FLAGS -f -filetype=asm -o ${FILE}_mod.s ${FILE}_opt.bc || {
183210
echo "Error compiling LLVM bitcode to assembly code."
184211
exit 1;

llvm-lua/lua-cross-compiler.in

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
#!/usr/bin/env bash
22
#
33

4+
# path to lua-cross-compiler
5+
DIR=`realpath $0`
6+
DIR=`dirname $DIR`
7+
48
CC=@CROSS_TRIPLE@-gcc
59
LLVM_LUAC="./@CROSS_TRIPLE@-llvm-luac"
10+
11+
# find llvm-luac cross-compiler
12+
if [[ ! -x "$LLVM_LUAC" ]]; then
13+
LLVM_LUAC="$DIR/@CROSS_TRIPLE@-llvm-luac"
14+
fi
615
if [[ ! -x "$LLVM_LUAC" ]]; then
716
LLVM_LUAC=`which @CROSS_TRIPLE@-llvm-luac`
817
fi
918

1019
ARCH=@CROSS_ARCH@
1120
CPU=@CROSS_CPU@
1221
MODULE="0"
22+
NO_ASSEMBLE="0"
1323
FILE=lua-native-out
1424
FILES=""
1525
DEBUG="0"
@@ -28,6 +38,7 @@ function usage() {
2838
OPTIONS:
2939
-lua-module - Compile Lua script into a loadable module instead of a standalone
3040
executable.
41+
-no-assemble - Only compile to native Assembly code or C-code, don't compile to machine code.
3142
-debug - Turns off all optimizations and turns on debug info. Both Lua debug
3243
info and gcc debug symbols are enabled.
3344
-keep-tmps - Don't delete temp. files generated by intermediate stages. Use only
@@ -45,22 +56,44 @@ llvm-luac '-help' output:
4556
}
4657

4758
# parse command line parameters.
59+
CONSUME=""
4860
for arg in "$@" ; do
61+
case "$CONSUME" in
62+
-o) OUTPUT_FILE=` echo "$arg" | sed -e 's/-o=//'` ;;
63+
-L) EXTRA_ARGS="$EXTRA_ARGS -L $arg" ;;
64+
esac
65+
if [[ ! -z "$CONSUME" ]]; then
66+
CONSUME=""
67+
continue
68+
fi
4969
case "$arg" in
50-
-lua-module*) MODULE="1"; EXTRA_ARGS="$EXTRA_ARGS $arg" ;;
70+
-lua-mod*) MODULE="1"; EXTRA_ARGS="$EXTRA_ARGS $arg" ;;
5171
-debug) DEBUG="1" ;;
72+
-no-assemble) NO_ASSEMBLE="1" ;;
5273
-keep-tmps) KEEP_TMPS="1" ;;
5374
-mode=*) MODE=` echo "$arg" | sed -e 's/-mode=//'` ;;
5475
-help|--help|-h) usage ;;
5576
-version|--version|-v) version ;;
77+
-o|-L) CONSUME="$arg" ;;
78+
-L*) EXTRA_ARGS="$EXTRA_ARGS $arg" ;;
5679
-*) EXTRA_ARGS="$EXTRA_ARGS $arg" ;;
5780
*) FILE=${arg/.lua/}; FILES="$FILES $arg" ;;
5881
esac
5982
done
6083

61-
# get source file's path & filename.
62-
FPATH=`dirname ${FILE}`
63-
FNAME=`basename ${FILE}`
84+
# find the mode's output file extension.
85+
OUTPUT_EXT=""
86+
MODE_OUT=""
87+
case "$MODE" in
88+
cbe) OUTPUT_EXT=".c" ;;
89+
asm) OUTPUT_EXT=".s" ;;
90+
esac
91+
if [[ -z "$OUTPUT_FILE" ]]; then
92+
OUTPUT_FILE="${FILE}${OUTPUT_EXT}"
93+
else
94+
FILE=${OUTPUT_FILE}
95+
fi
96+
MODE_OUT="${FILE}${OUTPUT_EXT}"
6497

6598
# select debug/optimize parameters.
6699
if [[ $DEBUG == "1" ]]; then
@@ -72,7 +105,7 @@ else
72105
OPT_FLAGS=" -O3 -std-compile-opts -tailcallelim -tailduplicate "
73106
LLC_FLAGS=" -tailcallopt "
74107
fi
75-
LLC_FLAGS=" -march=$ARCH -mcpu=$CPU $LLC_FLAGS "
108+
LLC_FLAGS=" -mcpu=$CPU $LLC_FLAGS "
76109

77110
#
78111
# run llvm-luac to compile Lua source/bytecode to LLVM bitcode.
@@ -84,36 +117,43 @@ $LLVM_LUAC $EXTRA_ARGS $LUA_FLAGS -bc -o ${FILE}.bc ${FILES} || {
84117
}
85118
TMPS="${FILE}.bc"
86119

87-
MODE_OUT=""
88120
# use one of the compile modes.
89121
case "$MODE" in
90122
cbe)
91123
TMPS="$TMPS ${FILE}_opt.bc"
92124
echo "opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc"
93125
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc
94-
echo "llc -f -march=c -o ${FILE}.c ${FILE}_opt.bc"
95-
MODE_OUT=${FILE}.c
96-
llc -f -march=c -o ${MODE_OUT} ${FILE}_opt.bc
126+
echo "llc $LLC_FLAGS -f -march=c -o ${FILE}.c ${FILE}_opt.bc"
127+
llc $LLC_FLAGS -f -march=c -o ${MODE_OUT} ${FILE}_opt.bc
97128
;;
98129
asm)
99130
TMPS="$TMPS ${FILE}_opt.bc"
100131
echo "opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc"
101132
opt $OPT_FLAGS -f -o ${FILE}_opt.bc ${FILE}.bc
102-
echo "llc $LLC_FLAGS -f -filetype=asm -o ${FILE}.s ${FILE}_opt.bc"
103-
MODE_OUT=${FILE}.s
104-
llc $LLC_FLAGS -f -filetype=asm -o ${MODE_OUT} ${FILE}_opt.bc
133+
echo "llc -march=$ARCH $LLC_FLAGS -f -filetype=asm -o ${FILE}.s ${FILE}_opt.bc"
134+
llc -march=$ARCH $LLC_FLAGS -f -filetype=asm -o ${MODE_OUT} ${FILE}_opt.bc
105135
;;
106136
*)
107137
echo "Invalid compile mode: $MODE"
108138
;;
109139
esac
110140

111141
# compile to stand-alone
112-
if [[ $MODULE == "0" ]]; then
113-
${CC} -o ${FILE} ${MODE_OUT} -lm -ldl && \
142+
if [[ $MODULE == "0" && $NO_ASSEMBLE == "0" ]]; then
143+
echo "${CC} -o ${OUTPUT_FILE} ${MODE_OUT} -lm -ldl"
144+
${CC} -o ${OUTPUT_FILE} ${MODE_OUT} -lm -ldl && \
114145
TMPS="$TMPS ${MODE_OUT}"
115146
fi
116147

148+
# keep assembly/c-code output.
149+
if [[ $NO_ASSEMBLE == "1" && $MODE_OUT != $OUTPUT_FILE ]]; then
150+
echo "1 no-asm=$NO_ASSEMBLE, mode_out=$MODE_OUT, out=$OUTPUT_FILE"
151+
echo "mv $MODE_OUT $OUTPUT_FILE"
152+
mv $MODE_OUT $OUTPUT_FILE
153+
else
154+
echo "2 no-asm=$NO_ASSEMBLE, mode_out=$MODE_OUT, out=$OUTPUT_FILE"
155+
fi
156+
117157
if [[ $KEEP_TMPS == "0" ]]; then
118158
rm -f $TMPS
119159
fi

llvm-lua/lua_compiler.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ static int doargs(int argc, char* argv[]) {
102102
else if (IS("-bc")) { /* output LLVM bitcode */
103103
llvm_bitcode=1;
104104
dumping=0;
105-
} else if (IS("-L")) { /* */
105+
} else if (IS("-L")) { /* preload library */
106106
if (preloads >= MAX_PRELOADS) usage(LUA_QL("-L") " too many preloads");
107107
preload_libs[preloads]=argv[++i];
108-
if (IS("-")) preload_libs[preloads]=NULL;
109108
preloads++;
110109
} else if (IS("-l")) /* list */
111110
++listing;

llvm-lua/run_tests.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
#
3+
4+
for script in `ls tests/*.lua`; do
5+
echo "run test: $script"
6+
llvm-lua -g -O0 $script >/dev/null
7+
#llvm-lua -g -O0 $script
8+
#lua $script
9+
done
10+

0 commit comments

Comments
 (0)