1
1
#! /usr/bin/env bash
2
2
#
3
3
4
+ # path to lua-compiler
5
+ DIR=` realpath $0 `
6
+ DIR=` dirname $DIR `
7
+
4
8
CC=gcc
5
9
LIBTOOL=" libtool --tag=CC --silent"
6
10
RPATH=` pwd`
7
11
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
8
18
if [[ ! -x " $LLVM_LUAC " ]]; then
9
19
LLVM_LUAC=` which llvm-luac`
10
20
fi
11
21
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"
16
28
FILE=
17
29
FILES=" "
18
30
OUTPUT_FILE=" "
@@ -40,17 +52,19 @@ OPTIONS:
40
52
info and gcc debug symbols are enabled.
41
53
-keep-tmps - Don't delete temp. files generated by intermediate stages. Use only
42
54
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>'
44
57
-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.
47
58
- full_bc - (default mode) compiles Lua scripts into LLVM bitcode
48
59
linked with bitcode liblua_main.bc then converted to assembly
49
60
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.
52
61
- lua_mod - Only used by the '-lua-module' option. Don't use
53
62
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.
54
68
-****** - All other options passed to 'llvm-luac'. See below for a list of
55
69
options supported by 'llvm-luac'.
56
70
@@ -78,7 +92,8 @@ for arg in "$@" ; do
78
92
-debug) DEBUG=" 1" ;;
79
93
-keep-tmps) KEEP_TMPS=" 1" ;;
80
94
-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=//' ` ;;
82
97
-help|--help|-h) usage ;;
83
98
-version|--version|-v) version ;;
84
99
-o|-L) CONSUME=" $arg " ;;
@@ -113,35 +128,36 @@ if [[ $DEBUG == "1" ]]; then
113
128
# CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall "
114
129
OPT_FLAGS=" -disable-opt "
115
130
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 "
118
133
fi
119
134
else
120
135
LUA_FLAGS=" -O3 -s "
121
136
# LUA_FLAGS=" -O3 -g "
122
137
# CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall "
123
138
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 "
126
141
fi
127
142
OPT_FLAGS=" -O3 -std-compile-opts -tailcallelim -tailduplicate "
128
143
LLC_FLAGS=" -tailcallopt "
129
144
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 "
132
147
fi
133
148
134
149
if [[ $MODE == " cbe" ]]; then
135
150
EXTRA_ARGS=" $EXTRA_ARGS -no-main "
136
151
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 "
143
154
elif [[ $MODE == " c" ]]; then
144
155
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
145
161
fi
146
162
147
163
#
@@ -158,27 +174,38 @@ TMPS="${FILE}.bc"
158
174
case " $MODE " in
159
175
cbe)
160
176
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"
163
182
$CC $CFLAGS $LIBS -o ${OUTPUT_FILE} ${FILE} _run.c -llua_main -lm -ldl
164
183
;;
165
184
c)
166
185
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"
168
189
llc $LLC_FLAGS -f --march=c -o ${OUTPUT_FILE} ${FILE} _opt.bc
169
190
;;
170
191
ll)
192
+ echo " llvm-dis -f -o ${OUTPUT_FILE} ${FILE} .bc"
171
193
llvm-dis -f -o ${OUTPUT_FILE} ${FILE} .bc
172
194
;;
173
195
full_bc)
174
196
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"
177
202
$CC $CFLAGS -o ${OUTPUT_FILE} ${FILE} _run.s -lm -ldl
178
203
;;
179
204
lua_mod)
180
205
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"
182
209
llc $LLC_FLAGS -f -filetype=asm -o ${FILE} _mod.s ${FILE} _opt.bc || {
183
210
echo " Error compiling LLVM bitcode to assembly code."
184
211
exit 1;
0 commit comments