2010-06-23 23:03:09 -05:00
|
|
|
// -*- rust -*-
|
|
|
|
|
2011-03-15 18:30:43 -05:00
|
|
|
import front.creader;
|
2010-09-23 17:46:31 -05:00
|
|
|
import front.parser;
|
|
|
|
import front.token;
|
2011-03-01 17:57:55 -06:00
|
|
|
import front.eval;
|
2011-04-29 13:55:20 -05:00
|
|
|
import front.ast;
|
2010-09-23 17:46:31 -05:00
|
|
|
import middle.trans;
|
2010-10-14 17:53:28 -05:00
|
|
|
import middle.resolve;
|
2011-04-11 14:35:01 -05:00
|
|
|
import middle.capture;
|
2011-03-30 19:23:25 -05:00
|
|
|
import middle.ty;
|
2010-11-03 18:43:12 -05:00
|
|
|
import middle.typeck;
|
2011-04-01 19:23:56 -05:00
|
|
|
import middle.typestate_check;
|
2011-05-05 12:48:02 -05:00
|
|
|
import back.Link;
|
2011-05-02 19:45:07 -05:00
|
|
|
import lib.llvm;
|
2010-11-22 18:27:00 -06:00
|
|
|
import util.common;
|
2010-08-18 02:19:45 -05:00
|
|
|
|
2011-05-04 21:27:54 -05:00
|
|
|
import std.fs;
|
2011-03-25 12:42:57 -05:00
|
|
|
import std.map.mk_hashmap;
|
2010-11-03 19:10:37 -05:00
|
|
|
import std.option;
|
|
|
|
import std.option.some;
|
|
|
|
import std.option.none;
|
2010-10-22 13:47:28 -05:00
|
|
|
import std._str;
|
|
|
|
import std._vec;
|
2011-04-19 05:02:06 -05:00
|
|
|
import std.io;
|
2011-04-29 13:55:20 -05:00
|
|
|
import std.Time;
|
2010-10-22 13:47:28 -05:00
|
|
|
|
2011-04-26 13:32:08 -05:00
|
|
|
import std.GetOpts;
|
|
|
|
import std.GetOpts.optopt;
|
|
|
|
import std.GetOpts.optmulti;
|
|
|
|
import std.GetOpts.optflag;
|
|
|
|
import std.GetOpts.opt_present;
|
|
|
|
|
2011-05-05 12:48:02 -05:00
|
|
|
import back.Link.output_type;
|
|
|
|
|
2011-03-01 17:57:55 -06: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 15:35:49 -05:00
|
|
|
fn parse_input(session.session sess,
|
2011-01-03 22:41:11 -06:00
|
|
|
parser.parser p,
|
2011-04-29 13:55:20 -05:00
|
|
|
str input) -> @ast.crate {
|
2011-01-03 22:41:11 -06:00
|
|
|
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 17:07:41 -05:00
|
|
|
sess.err("unknown input file type: " + input);
|
2011-01-03 22:41:11 -06:00
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-04-29 13:55:20 -05:00
|
|
|
fn time[T](bool do_it, str what, fn()->T thunk) -> T {
|
|
|
|
if (!do_it) { ret thunk(); }
|
|
|
|
|
|
|
|
auto start = Time.get_time();
|
|
|
|
auto rv = thunk();
|
|
|
|
auto end = Time.get_time();
|
|
|
|
|
|
|
|
// FIXME: Actually do timeval math.
|
|
|
|
log_err #fmt("time: %s took %u s", what, (end.sec - start.sec) as uint);
|
|
|
|
ret rv;
|
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn compile_input(session.session sess,
|
2011-05-04 18:53:42 -05:00
|
|
|
eval.env env,
|
|
|
|
str input, str output) {
|
|
|
|
auto time_passes = sess.get_opts().time_passes;
|
2011-03-10 16:34:58 -06:00
|
|
|
auto def = tup(0, 0);
|
2011-04-08 11:44:20 -05:00
|
|
|
auto p = parser.new_parser(sess, env, def, input, 0u);
|
2011-04-29 13:55:20 -05:00
|
|
|
auto crate = time[@ast.crate](time_passes, "parsing",
|
2011-05-04 18:53:42 -05:00
|
|
|
bind parse_input(sess, p, input));
|
2011-05-05 12:48:02 -05:00
|
|
|
if (sess.get_opts().output_type == Link.output_type_none) {ret;}
|
2011-04-20 20:52:04 -05:00
|
|
|
|
2011-04-29 13:55:20 -05:00
|
|
|
crate = time[@ast.crate](time_passes, "external crate reading",
|
2011-05-04 18:53:42 -05:00
|
|
|
bind creader.read_crates(sess, crate));
|
2011-04-29 13:55:20 -05:00
|
|
|
crate = time[@ast.crate](time_passes, "resolution",
|
2011-05-04 18:53:42 -05:00
|
|
|
bind resolve.resolve_crate(sess, crate));
|
2011-04-29 13:55:20 -05:00
|
|
|
time[()](time_passes, "capture checking",
|
2011-05-04 18:53:42 -05:00
|
|
|
bind capture.check_for_captures(sess, crate));
|
2011-04-20 20:52:04 -05:00
|
|
|
|
2011-04-22 13:23:49 -05:00
|
|
|
auto ty_cx = ty.mk_ctxt(sess);
|
2011-04-29 13:55:20 -05:00
|
|
|
auto typeck_result =
|
|
|
|
time[typeck.typecheck_result](time_passes, "typechecking",
|
2011-05-04 18:53:42 -05:00
|
|
|
bind typeck.check_crate(ty_cx, crate));
|
2011-03-30 19:23:25 -05:00
|
|
|
crate = typeck_result._0;
|
|
|
|
auto type_cache = typeck_result._1;
|
2011-04-29 13:55:20 -05:00
|
|
|
|
2011-05-04 18:53:42 -05:00
|
|
|
if (sess.get_opts().run_typestate) {
|
2011-04-29 14:16:14 -05:00
|
|
|
crate = time[@ast.crate](time_passes, "typestate checking",
|
|
|
|
bind typestate_check.check_crate(crate));
|
|
|
|
}
|
2011-04-29 13:55:20 -05:00
|
|
|
|
2011-05-02 19:45:07 -05:00
|
|
|
auto llmod = time[llvm.ModuleRef](time_passes, "translation",
|
2011-05-04 18:53:42 -05:00
|
|
|
bind trans.trans_crate(sess, crate, ty_cx, type_cache, output));
|
2011-05-02 19:45:07 -05:00
|
|
|
|
|
|
|
time[()](time_passes, "LLVM passes",
|
2011-05-05 12:48:02 -05:00
|
|
|
bind Link.Write.run_passes(sess, llmod, output));
|
2010-10-22 13:47:28 -05:00
|
|
|
}
|
2010-07-13 16:26:59 -05:00
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn pretty_print_input(session.session sess,
|
2011-03-04 00:22:43 -06:00
|
|
|
eval.env env,
|
|
|
|
str input) {
|
2011-03-10 16:34:58 -06:00
|
|
|
auto def = tup(0, 0);
|
2011-04-08 11:44:20 -05:00
|
|
|
auto p = front.parser.new_parser(sess, env, def, input, 0u);
|
2011-03-04 00:22:43 -06:00
|
|
|
auto crate = front.parser.parse_crate_from_source_file(p);
|
2011-03-24 10:33:20 -05:00
|
|
|
pretty.pprust.print_file(crate.node.module, input, std.io.stdout());
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
|
|
|
|
2011-05-04 18:53:42 -05:00
|
|
|
fn usage(str argv0) {
|
2011-04-19 05:02:06 -05:00
|
|
|
io.stdout().write_str(#fmt("usage: %s [options] <input>\n", argv0) + "
|
|
|
|
options:
|
|
|
|
|
|
|
|
-o <filename> write output to <filename>
|
2011-04-26 13:32:08 -05:00
|
|
|
--glue generate glue.bc file
|
|
|
|
--shared compile a shared-library crate
|
|
|
|
--pretty pretty-print the input instead of compiling
|
|
|
|
--ls list the symbols defined by a crate file
|
2011-04-19 05:02:06 -05:00
|
|
|
-L <path> add a directory to the library search path
|
2011-04-26 13:32:08 -05:00
|
|
|
--noverify suppress LLVM verification step (slight speedup)
|
2011-05-03 17:50:56 -05:00
|
|
|
--depend print dependencies, in makefile-rule form
|
2011-04-26 13:32:08 -05:00
|
|
|
--parse-only parse only; do not compile, assemble, or link
|
2011-05-04 17:36:42 -05:00
|
|
|
-g produce debug info
|
2011-04-25 16:08:12 -05:00
|
|
|
-O optimize
|
|
|
|
-S compile only; do not assemble or link
|
|
|
|
-c compile and assemble, but do not link
|
2011-04-26 13:32:08 -05:00
|
|
|
--save-temps write intermediate files in addition to normal output
|
2011-04-29 13:55:20 -05:00
|
|
|
--time-passes time the individual phases of the compiler
|
2011-05-04 21:27:54 -05:00
|
|
|
--sysroot <path> override the system root (default: rustc's directory)
|
2011-04-29 14:16:14 -05:00
|
|
|
--no-typestate don't run the typestate pass (unsafe!)
|
2011-04-19 05:02:06 -05:00
|
|
|
-h display this message\n\n");
|
2010-10-22 13:47:28 -05:00
|
|
|
}
|
|
|
|
|
2010-11-22 18:27:00 -06: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-05-04 21:27:54 -05:00
|
|
|
fn get_default_sysroot(str binary) -> str {
|
|
|
|
auto dirname = fs.dirname(binary);
|
|
|
|
if (_str.eq(dirname, binary)) { ret "."; }
|
|
|
|
ret dirname;
|
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn main(vec[str] args) {
|
2010-07-13 16:26:59 -05:00
|
|
|
|
2010-11-22 18:27:00 -06:00
|
|
|
// FIXME: don't hard-wire this.
|
2011-05-04 18:53:42 -05:00
|
|
|
let @session.config 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 12:42:57 -05:00
|
|
|
|
2011-04-29 12:23:07 -05:00
|
|
|
auto opts = vec(optflag("h"), optflag("glue"),
|
2011-04-26 13:32:08 -05:00
|
|
|
optflag("pretty"), optflag("ls"), optflag("parse-only"),
|
|
|
|
optflag("O"), optflag("shared"), optmulti("L"),
|
2011-05-04 17:36:42 -05:00
|
|
|
optflag("S"), optflag("c"), optopt("o"), optopt("g"),
|
2011-05-04 21:27:54 -05:00
|
|
|
optflag("save-temps"), optopt("sysroot"),
|
|
|
|
optflag("time-passes"), optflag("no-typestate"),
|
|
|
|
optflag("noverify"));
|
2011-04-26 13:32:08 -05:00
|
|
|
auto binary = _vec.shift[str](args);
|
|
|
|
auto match;
|
|
|
|
alt (GetOpts.getopts(args, opts)) {
|
2011-05-04 18:53:42 -05:00
|
|
|
case (GetOpts.failure(?f)) {
|
|
|
|
log_err #fmt("error: %s", GetOpts.fail_str(f));
|
|
|
|
fail;
|
|
|
|
}
|
2011-04-26 13:32:08 -05:00
|
|
|
case (GetOpts.success(?m)) { match = m; }
|
|
|
|
}
|
|
|
|
if (opt_present(match, "h")) {
|
2011-05-04 18:53:42 -05:00
|
|
|
usage(binary);
|
2011-04-26 13:32:08 -05:00
|
|
|
ret;
|
|
|
|
}
|
2010-10-22 13:47:28 -05:00
|
|
|
|
2011-04-26 13:32:08 -05:00
|
|
|
auto pretty = opt_present(match, "pretty");
|
|
|
|
auto ls = opt_present(match, "ls");
|
|
|
|
auto glue = opt_present(match, "glue");
|
|
|
|
auto shared = opt_present(match, "shared");
|
|
|
|
auto output_file = GetOpts.opt_maybe_str(match, "o");
|
|
|
|
auto library_search_paths = GetOpts.opt_strs(match, "L");
|
2011-05-05 12:48:02 -05:00
|
|
|
|
|
|
|
auto output_type = Link.output_type_bitcode;
|
2011-04-26 13:32:08 -05:00
|
|
|
if (opt_present(match, "parse-only")) {
|
2011-05-05 12:48:02 -05:00
|
|
|
output_type = Link.output_type_none;
|
2011-04-26 13:32:08 -05:00
|
|
|
} else if (opt_present(match, "S")) {
|
2011-05-05 12:48:02 -05:00
|
|
|
output_type = Link.output_type_assembly;
|
2011-04-26 13:32:08 -05:00
|
|
|
} else if (opt_present(match, "c")) {
|
2011-05-05 12:48:02 -05:00
|
|
|
output_type = Link.output_type_object;
|
2011-04-26 13:32:08 -05:00
|
|
|
}
|
2011-05-05 12:48:02 -05:00
|
|
|
|
2011-04-26 13:32:08 -05:00
|
|
|
auto verify = !opt_present(match, "noverify");
|
|
|
|
auto save_temps = opt_present(match, "save-temps");
|
2011-04-07 14:42:06 -05:00
|
|
|
// FIXME: Maybe we should support -O0, -O1, -Os, etc
|
2011-04-26 13:32:08 -05:00
|
|
|
auto optimize = opt_present(match, "O");
|
2011-05-04 17:36:42 -05:00
|
|
|
auto debuginfo = opt_present(match, "g");
|
2011-04-29 13:55:20 -05:00
|
|
|
auto time_passes = opt_present(match, "time-passes");
|
2011-04-29 14:16:14 -05:00
|
|
|
auto run_typestate = !opt_present(match, "no-typestate");
|
2011-05-04 21:27:54 -05:00
|
|
|
auto sysroot_opt = GetOpts.opt_maybe_str(match, "sysroot");
|
|
|
|
|
|
|
|
auto sysroot;
|
|
|
|
alt (sysroot_opt) {
|
|
|
|
case (none[str]) { sysroot = get_default_sysroot(binary); }
|
|
|
|
case (some[str](?s)) { sysroot = s; }
|
|
|
|
}
|
2011-05-04 18:53:42 -05:00
|
|
|
|
|
|
|
let @session.options sopts =
|
|
|
|
@rec(shared = shared,
|
|
|
|
optimize = optimize,
|
|
|
|
debuginfo = debuginfo,
|
|
|
|
verify = verify,
|
|
|
|
run_typestate = run_typestate,
|
|
|
|
save_temps = save_temps,
|
|
|
|
time_passes = time_passes,
|
|
|
|
output_type = output_type,
|
2011-05-04 21:27:54 -05:00
|
|
|
library_search_paths = library_search_paths,
|
|
|
|
sysroot = sysroot);
|
2011-05-04 18:53:42 -05:00
|
|
|
|
|
|
|
auto crate_cache = common.new_int_hash[session.crate_metadata]();
|
|
|
|
auto target_crate_num = 0;
|
|
|
|
let vec[@ast.meta_item] md = vec();
|
|
|
|
auto sess =
|
|
|
|
session.session(target_crate_num, target_cfg, sopts,
|
|
|
|
crate_cache, md, front.codemap.new_codemap());
|
|
|
|
|
2011-04-26 13:32:08 -05:00
|
|
|
auto n_inputs = _vec.len[str](match.free);
|
2011-04-07 14:42:06 -05:00
|
|
|
|
2011-04-26 13:32:08 -05:00
|
|
|
if (glue) {
|
|
|
|
if (n_inputs > 0u) {
|
|
|
|
sess.err("No input files allowed with --glue.");
|
2010-11-22 18:27:00 -06:00
|
|
|
}
|
2011-04-26 13:32:08 -05:00
|
|
|
auto out = option.from_maybe[str]("glue.bc", output_file);
|
2011-05-04 18:53:42 -05:00
|
|
|
middle.trans.make_common_glue(sess, out);
|
2011-04-26 13:32:08 -05:00
|
|
|
ret;
|
2010-11-22 18:27:00 -06:00
|
|
|
}
|
2010-10-22 13:47:28 -05:00
|
|
|
|
2011-04-26 13:32:08 -05:00
|
|
|
if (n_inputs == 0u) {
|
|
|
|
sess.err("No input filename given.");
|
|
|
|
} else if (n_inputs > 1u) {
|
|
|
|
sess.err("Multiple input filenames provided.");
|
2010-11-22 18:27:00 -06:00
|
|
|
}
|
2010-10-22 13:47:28 -05:00
|
|
|
|
2011-04-26 13:32:08 -05:00
|
|
|
auto ifile = match.free.(0);
|
|
|
|
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 {
|
2011-03-10 19:25:11 -06:00
|
|
|
alt (output_file) {
|
|
|
|
case (none[str]) {
|
2011-04-26 13:32:08 -05:00
|
|
|
let vec[str] parts = _str.split(ifile, '.' as u8);
|
|
|
|
_vec.pop[str](parts);
|
2011-05-04 18:53:42 -05:00
|
|
|
alt (output_type) {
|
2011-05-05 12:48:02 -05:00
|
|
|
case (Link.output_type_none) { parts += vec("pp"); }
|
|
|
|
case (Link.output_type_bitcode) { parts += vec("bc"); }
|
|
|
|
case (Link.output_type_assembly) { parts += vec("s"); }
|
|
|
|
case (Link.output_type_object) { parts += vec("o"); }
|
2011-05-04 18:53:42 -05:00
|
|
|
}
|
2011-05-01 13:42:45 -05:00
|
|
|
auto ofile = _str.connect(parts, ".");
|
2011-05-04 18:53:42 -05:00
|
|
|
compile_input(sess, env, ifile, ofile);
|
2011-03-10 19:25:11 -06:00
|
|
|
}
|
2011-04-26 13:32:08 -05:00
|
|
|
case (some[str](?ofile)) {
|
2011-05-04 18:53:42 -05:00
|
|
|
compile_input(sess, env, ifile, ofile);
|
2010-11-22 18:27:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2010-08-12 12:29:23 -05: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 17:07:27 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-08-12 12:29:23 -05:00
|
|
|
// End:
|