2010-06-23 21:03:09 -07:00
|
|
|
// -*- rust -*-
|
|
|
|
|
2011-03-15 16:30:43 -07:00
|
|
|
import front.creader;
|
2010-09-23 15:46:31 -07:00
|
|
|
import front.parser;
|
|
|
|
import front.token;
|
2011-03-01 15:57:55 -08:00
|
|
|
import front.eval;
|
2010-09-23 15:46:31 -07:00
|
|
|
import middle.trans;
|
2010-10-14 15:53:28 -07:00
|
|
|
import middle.resolve;
|
2011-04-11 15:35:01 -04:00
|
|
|
import middle.capture;
|
2011-03-30 17:23:25 -07:00
|
|
|
import middle.ty;
|
2010-11-03 16:43:12 -07:00
|
|
|
import middle.typeck;
|
2011-04-01 17:23:56 -07:00
|
|
|
import middle.typestate_check;
|
2010-11-22 16:27:00 -08:00
|
|
|
import util.common;
|
2010-08-18 00:19:45 -07:00
|
|
|
|
2011-03-25 10:42:57 -07:00
|
|
|
import std.map.mk_hashmap;
|
2010-11-03 17:10:37 -07:00
|
|
|
import std.option;
|
|
|
|
import std.option.some;
|
|
|
|
import std.option.none;
|
2010-10-22 11:47:28 -07:00
|
|
|
import std._str;
|
|
|
|
import std._vec;
|
2011-04-19 12:02:06 +02:00
|
|
|
import std.io;
|
2010-10-22 11:47:28 -07:00
|
|
|
|
2011-03-01 15:57:55 -08:00
|
|
|
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))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-04-19 13:35:49 -07:00
|
|
|
fn parse_input(session.session sess,
|
2011-01-03 20:41:11 -08:00
|
|
|
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);
|
|
|
|
}
|
2011-03-15 15:07:41 -07:00
|
|
|
sess.err("unknown input file type: " + input);
|
2011-01-03 20:41:11 -08:00
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-04-19 13:35:49 -07:00
|
|
|
fn compile_input(session.session sess,
|
2011-03-01 15:57:55 -08:00
|
|
|
eval.env env,
|
|
|
|
str input, str output,
|
2011-03-15 16:30:43 -07:00
|
|
|
bool shared,
|
2011-04-07 15:42:06 -04:00
|
|
|
bool optimize,
|
2011-04-15 17:35:46 -04:00
|
|
|
trans.output_type ot,
|
2011-03-15 16:30:43 -07:00
|
|
|
vec[str] library_search_paths) {
|
2011-03-10 17:34:58 -05:00
|
|
|
auto def = tup(0, 0);
|
2011-04-08 18:44:20 +02:00
|
|
|
auto p = parser.new_parser(sess, env, def, input, 0u);
|
2011-01-03 20:41:11 -08:00
|
|
|
auto crate = parse_input(sess, p, input);
|
2011-04-15 17:35:46 -04:00
|
|
|
if (ot == trans.output_type_none) {ret;}
|
2011-03-15 17:33:05 -07:00
|
|
|
crate = creader.read_crates(sess, crate, library_search_paths);
|
2010-11-22 16:27:00 -08:00
|
|
|
crate = resolve.resolve_crate(sess, crate);
|
2011-04-11 15:35:01 -04:00
|
|
|
capture.check_for_captures(sess, crate);
|
2011-03-30 17:23:25 -07:00
|
|
|
auto typeck_result = typeck.check_crate(sess, crate);
|
|
|
|
crate = typeck_result._0;
|
|
|
|
auto type_cache = typeck_result._1;
|
2011-03-24 12:12:04 -07:00
|
|
|
// FIXME: uncomment once typestate_check works
|
|
|
|
// crate = typestate_check.check_crate(crate);
|
2011-04-15 17:35:46 -04:00
|
|
|
trans.trans_crate(sess, crate, type_cache, output, shared, optimize,
|
|
|
|
ot);
|
2010-10-22 11:47:28 -07:00
|
|
|
}
|
2010-07-13 14:26:59 -07:00
|
|
|
|
2011-04-19 13:35:49 -07:00
|
|
|
fn pretty_print_input(session.session sess,
|
2011-03-04 07:22:43 +01:00
|
|
|
eval.env env,
|
|
|
|
str input) {
|
2011-03-10 17:34:58 -05:00
|
|
|
auto def = tup(0, 0);
|
2011-04-08 18:44:20 +02:00
|
|
|
auto p = front.parser.new_parser(sess, env, def, input, 0u);
|
2011-03-04 07:22:43 +01:00
|
|
|
auto crate = front.parser.parse_crate_from_source_file(p);
|
2011-03-24 16:33:20 +01:00
|
|
|
pretty.pprust.print_file(crate.node.module, input, std.io.stdout());
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
|
2010-10-22 11:47:28 -07:00
|
|
|
fn warn_wrong_compiler() {
|
2011-04-19 12:02:06 +02:00
|
|
|
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");
|
2010-10-22 11:47:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(session.session sess, str argv0) {
|
2011-04-19 12:02:06 +02:00
|
|
|
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
|
|
|
|
-h display this message\n\n");
|
2010-10-22 11:47:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-22 16:27:00 -08:00
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
2011-04-19 13:35:49 -07:00
|
|
|
fn main(vec[str] args) {
|
2010-07-13 14:26:59 -07:00
|
|
|
|
2010-11-22 16:27:00 -08:00
|
|
|
// 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 );
|
|
|
|
|
2011-03-25 10:42:57 -07:00
|
|
|
auto crate_cache = common.new_int_hash[session.crate_metadata]();
|
2011-03-25 14:32:39 -07:00
|
|
|
auto target_crate_num = 0;
|
2011-04-18 09:22:23 +02:00
|
|
|
let vec[@front.ast.meta_item] md = vec();
|
2011-04-08 18:44:20 +02:00
|
|
|
auto sess = session.session(target_crate_num, target_cfg, crate_cache,
|
2011-04-18 09:22:23 +02:00
|
|
|
md, front.codemap.new_codemap());
|
2011-03-25 10:42:57 -07:00
|
|
|
|
2010-11-22 16:27:00 -08:00
|
|
|
let option.t[str] input_file = none[str];
|
|
|
|
let option.t[str] output_file = none[str];
|
2011-03-15 16:30:43 -07:00
|
|
|
let vec[str] library_search_paths = vec();
|
2010-11-22 16:27:00 -08:00
|
|
|
let bool do_warn = true;
|
2010-12-29 11:21:16 -05:00
|
|
|
let bool shared = false;
|
2011-03-04 07:22:43 +01:00
|
|
|
let bool pretty = false;
|
2011-04-07 20:20:57 +02:00
|
|
|
let bool ls = false;
|
2011-04-15 17:35:46 -04:00
|
|
|
auto ot = trans.output_type_bitcode;
|
2011-03-10 17:25:11 -08:00
|
|
|
let bool glue = false;
|
2010-10-22 11:47:28 -07:00
|
|
|
|
2011-04-07 15:42:06 -04:00
|
|
|
// FIXME: Maybe we should support -O0, -O1, -Os, etc
|
|
|
|
let bool optimize = false;
|
|
|
|
|
2010-11-22 16:27:00 -08:00
|
|
|
auto i = 1u;
|
|
|
|
auto len = _vec.len[str](args);
|
2010-10-22 11:47:28 -07:00
|
|
|
|
2010-11-22 16:27:00 -08:00
|
|
|
// 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;
|
2011-04-07 15:42:06 -04:00
|
|
|
} else if (_str.eq(arg, "-O")) {
|
|
|
|
optimize = true;
|
2011-03-10 17:25:11 -08:00
|
|
|
} else if (_str.eq(arg, "-glue")) {
|
|
|
|
glue = true;
|
2010-12-29 11:21:16 -05:00
|
|
|
} else if (_str.eq(arg, "-shared")) {
|
|
|
|
shared = true;
|
2011-03-04 07:22:43 +01:00
|
|
|
} else if (_str.eq(arg, "-pp")) {
|
|
|
|
pretty = true;
|
2011-04-07 20:20:57 +02:00
|
|
|
} else if (_str.eq(arg, "-ls")) {
|
|
|
|
ls = true;
|
2011-04-07 23:55:02 +02:00
|
|
|
} else if (_str.eq(arg, "-parse-only")) {
|
2011-04-15 17:35:46 -04:00
|
|
|
ot = trans.output_type_none;
|
|
|
|
} else if (_str.eq(arg, "-S")) {
|
|
|
|
ot = trans.output_type_assembly;
|
2011-04-18 10:02:34 -04:00
|
|
|
} else if (_str.eq(arg, "-c")) {
|
|
|
|
ot = trans.output_type_object;
|
2011-03-06 13:00:52 -05:00
|
|
|
} else if (_str.eq(arg, "-o")) {
|
|
|
|
if (i+1u < len) {
|
|
|
|
output_file = some(args.(i+1u));
|
|
|
|
i += 1u;
|
2010-11-22 16:27:00 -08:00
|
|
|
} else {
|
2011-03-06 13:00:52 -05:00
|
|
|
usage(sess, args.(0));
|
|
|
|
sess.err("-o requires an argument");
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
2011-03-15 16:30:43 -07:00
|
|
|
} 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");
|
|
|
|
}
|
2011-03-06 13:00:52 -05:00
|
|
|
} else if (_str.eq(arg, "-h")) {
|
|
|
|
usage(sess, args.(0));
|
|
|
|
} else {
|
|
|
|
usage(sess, args.(0));
|
|
|
|
sess.err("unrecognized option: " + arg);
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
2010-10-22 11:47:28 -07:00
|
|
|
|
2010-11-22 16:27:00 -08:00
|
|
|
if (do_warn) {
|
|
|
|
warn_wrong_compiler();
|
|
|
|
}
|
2010-10-22 11:47:28 -07:00
|
|
|
|
2011-03-10 17:25:11 -08:00
|
|
|
if (glue) {
|
|
|
|
alt (output_file) {
|
|
|
|
case (none[str]) {
|
2011-04-15 17:35:46 -04:00
|
|
|
middle.trans.make_common_glue("glue.bc", optimize, ot);
|
2011-03-10 17:25:11 -08:00
|
|
|
}
|
|
|
|
case (some[str](?s)) {
|
2011-04-15 17:35:46 -04:00
|
|
|
middle.trans.make_common_glue(s, optimize, ot);
|
2011-03-10 17:25:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2010-11-22 16:27:00 -08:00
|
|
|
alt (input_file) {
|
|
|
|
case (none[str]) {
|
|
|
|
usage(sess, args.(0));
|
|
|
|
sess.err("no input filename");
|
|
|
|
}
|
|
|
|
case (some[str](?ifile)) {
|
2011-03-01 15:57:55 -08:00
|
|
|
|
|
|
|
auto env = default_environment(sess, args.(0), ifile);
|
2011-03-04 07:22:43 +01:00
|
|
|
if (pretty) {
|
|
|
|
pretty_print_input(sess, env, ifile);
|
2011-04-07 20:20:57 +02:00
|
|
|
} else if (ls) {
|
|
|
|
front.creader.list_file_metadata(ifile, std.io.stdout());
|
|
|
|
} else {
|
2011-03-04 07:22:43 +01:00
|
|
|
alt (output_file) {
|
|
|
|
case (none[str]) {
|
|
|
|
let vec[str] parts = _str.split(ifile, '.' as u8);
|
2011-03-16 14:58:02 -07:00
|
|
|
_vec.pop[str](parts);
|
|
|
|
parts += vec(".bc");
|
2011-03-04 07:22:43 +01:00
|
|
|
auto ofile = _str.concat(parts);
|
2011-03-15 16:30:43 -07:00
|
|
|
compile_input(sess, env, ifile, ofile, shared,
|
2011-04-15 17:35:46 -04:00
|
|
|
optimize, ot,
|
2011-04-07 23:55:02 +02:00
|
|
|
library_search_paths);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
|
|
|
case (some[str](?ofile)) {
|
2011-03-15 16:30:43 -07:00
|
|
|
compile_input(sess, env, ifile, ofile, shared,
|
2011-04-15 17:35:46 -04:00
|
|
|
optimize, ot,
|
2011-04-07 23:55:02 +02:00
|
|
|
library_search_paths);
|
2011-03-04 07:22:43 +01:00
|
|
|
}
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
2010-08-12 10:29:23 -07:00
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 15:07:27 -07:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-08-12 10:29:23 -07:00
|
|
|
// End:
|