1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// -*- rust -*-
import front.creader;
import front.parser;
import front.token;
import front.eval;
import middle.trans;
import middle.resolve;
import middle.capture;
import middle.ty;
import middle.typeck;
import middle.typestate_check;
import util.common;
import std.map.mk_hashmap;
import std.option;
import std.option.some;
import std.option.none;
import std._str;
import std._vec;
import std.io;
fn default_environment(session.session sess,
str argv0,
str input) -> eval.env {
auto libc = "libc.so";
alt (sess.get_targ_cfg().os) {
case (session.os_win32) { libc = "msvcrt.dll"; }
case (session.os_macos) { libc = "libc.dylib"; }
case (session.os_linux) { libc = "libc.so.6"; }
}
ret
vec(
// Target bindings.
tup("target_os", eval.val_str(std.os.target_os())),
tup("target_arch", eval.val_str("x86")),
tup("target_libc", eval.val_str(libc)),
// Build bindings.
tup("build_compiler", eval.val_str(argv0)),
tup("build_input", eval.val_str(input))
);
}
fn parse_input(session.session sess,
parser.parser p,
str input) -> @front.ast.crate {
if (_str.ends_with(input, ".rc")) {
ret parser.parse_crate_from_crate_file(p);
} else if (_str.ends_with(input, ".rs")) {
ret parser.parse_crate_from_source_file(p);
}
sess.err("unknown input file type: " + input);
fail;
}
fn compile_input(session.session sess,
eval.env env,
str input, str output,
bool shared,
bool optimize,
bool verify,
trans.output_type ot,
vec[str] library_search_paths) {
auto def = tup(0, 0);
auto p = parser.new_parser(sess, env, def, input, 0u);
auto crate = parse_input(sess, p, input);
if (ot == trans.output_type_none) {ret;}
crate = creader.read_crates(sess, crate, library_search_paths);
crate = resolve.resolve_crate(sess, crate);
capture.check_for_captures(sess, crate);
auto typeck_result = typeck.check_crate(sess, crate);
crate = typeck_result._0;
auto type_cache = typeck_result._1;
// FIXME: uncomment once typestate_check works
// crate = typestate_check.check_crate(crate);
trans.trans_crate(sess, crate, type_cache, output, shared, optimize,
verify, ot);
}
fn pretty_print_input(session.session sess,
eval.env env,
str input) {
auto def = tup(0, 0);
auto p = front.parser.new_parser(sess, env, def, input, 0u);
auto crate = front.parser.parse_crate_from_source_file(p);
pretty.pprust.print_file(crate.node.module, input, std.io.stdout());
}
fn warn_wrong_compiler() {
io.stdout().write_str("This is the rust 'self-hosted' compiler.
The one written in rust.
It is currently incomplete.
You may want rustboot instead, the compiler next door.\n");
}
fn usage(session.session sess, str argv0) {
io.stdout().write_str(#fmt("usage: %s [options] <input>\n", argv0) + "
options:
-o <filename> write output to <filename>
-nowarn suppress wrong-compiler warning
-glue generate glue.bc file
-shared compile a shared-library crate
-pp pretty-print the input instead of compiling
-ls list the symbols defined by a crate file
-L <path> add a directory to the library search path
-noverify suppress LLVM verification step (slight speedup)
-h display this message\n\n");
}
fn get_os() -> session.os {
auto s = std.os.target_os();
if (_str.eq(s, "win32")) { ret session.os_win32; }
if (_str.eq(s, "macos")) { ret session.os_macos; }
if (_str.eq(s, "linux")) { ret session.os_linux; }
}
fn main(vec[str] args) {
// FIXME: don't hard-wire this.
auto target_cfg = rec(os = get_os(),
arch = session.arch_x86,
int_type = common.ty_i32,
uint_type = common.ty_u32,
float_type = common.ty_f64 );
auto crate_cache = common.new_int_hash[session.crate_metadata]();
auto target_crate_num = 0;
let vec[@front.ast.meta_item] md = vec();
auto sess = session.session(target_crate_num, target_cfg, crate_cache,
md, front.codemap.new_codemap());
let option.t[str] input_file = none[str];
let option.t[str] output_file = none[str];
let vec[str] library_search_paths = vec();
let bool do_warn = true;
let bool shared = false;
let bool pretty = false;
let bool ls = false;
auto ot = trans.output_type_bitcode;
let bool glue = false;
let bool verify = true;
// FIXME: Maybe we should support -O0, -O1, -Os, etc
let bool optimize = false;
auto i = 1u;
auto len = _vec.len[str](args);
// FIXME: a getopt module would be nice.
while (i < len) {
auto arg = args.(i);
if (_str.byte_len(arg) > 0u && arg.(0) == '-' as u8) {
if (_str.eq(arg, "-nowarn")) {
do_warn = false;
} else if (_str.eq(arg, "-O")) {
optimize = true;
} else if (_str.eq(arg, "-glue")) {
glue = true;
} else if (_str.eq(arg, "-shared")) {
shared = true;
} else if (_str.eq(arg, "-pp")) {
pretty = true;
} else if (_str.eq(arg, "-ls")) {
ls = true;
} else if (_str.eq(arg, "-parse-only")) {
ot = trans.output_type_none;
} else if (_str.eq(arg, "-S")) {
ot = trans.output_type_assembly;
} else if (_str.eq(arg, "-c")) {
ot = trans.output_type_object;
} else if (_str.eq(arg, "-o")) {
if (i+1u < len) {
output_file = some(args.(i+1u));
i += 1u;
} else {
usage(sess, args.(0));
sess.err("-o requires an argument");
}
} else if (_str.eq(arg, "-L")) {
if (i+1u < len) {
library_search_paths += vec(args.(i+1u));
i += 1u;
} else {
usage(sess, args.(0));
sess.err("-L requires an argument");
}
} else if (_str.eq(arg, "-noverify")) {
verify = false;
} else if (_str.eq(arg, "-h")) {
usage(sess, args.(0));
} else {
usage(sess, args.(0));
sess.err("unrecognized option: " + arg);
}
} else {
alt (input_file) {
case (some[str](_)) {
usage(sess, args.(0));
sess.err("multiple inputs provided");
}
case (none[str]) {
input_file = some[str](arg);
}
}
}
i += 1u;
}
if (do_warn) {
warn_wrong_compiler();
}
if (glue) {
alt (output_file) {
case (none[str]) {
middle.trans.make_common_glue("glue.bc", optimize, verify,
ot);
}
case (some[str](?s)) {
middle.trans.make_common_glue(s, optimize, verify, ot);
}
}
ret;
}
alt (input_file) {
case (none[str]) {
usage(sess, args.(0));
sess.err("no input filename");
}
case (some[str](?ifile)) {
auto env = default_environment(sess, args.(0), ifile);
if (pretty) {
pretty_print_input(sess, env, ifile);
} else if (ls) {
front.creader.list_file_metadata(ifile, std.io.stdout());
} else {
alt (output_file) {
case (none[str]) {
let vec[str] parts = _str.split(ifile, '.' as u8);
_vec.pop[str](parts);
parts += vec(".bc");
auto ofile = _str.concat(parts);
compile_input(sess, env, ifile, ofile, shared,
optimize, verify, ot,
library_search_paths);
}
case (some[str](?ofile)) {
compile_input(sess, env, ifile, ofile, shared,
optimize, verify, ot,
library_search_paths);
}
}
}
}
}
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
|