2010-06-23 23:03:09 -05:00
|
|
|
// -*- rust -*-
|
|
|
|
|
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;
|
2010-09-23 17:46:31 -05:00
|
|
|
import middle.trans;
|
2010-10-14 17:53:28 -05:00
|
|
|
import middle.resolve;
|
2010-11-03 18:43:12 -05:00
|
|
|
import middle.typeck;
|
2010-11-22 18:27:00 -06:00
|
|
|
import util.common;
|
2010-08-18 02:19:45 -05:00
|
|
|
|
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-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-01-03 22:41:11 -06:00
|
|
|
impure 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 unput file type: " + input);
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-03-01 17:57:55 -06:00
|
|
|
impure fn compile_input(session.session sess,
|
|
|
|
eval.env env,
|
|
|
|
str input, str output,
|
2010-12-29 10:21:16 -06:00
|
|
|
bool shared) {
|
2011-03-01 17:57:55 -06:00
|
|
|
auto p = parser.new_parser(sess, env, 0, input);
|
2011-01-03 22:41:11 -06:00
|
|
|
auto crate = parse_input(sess, p, input);
|
2010-11-22 18:27:00 -06:00
|
|
|
crate = resolve.resolve_crate(sess, crate);
|
|
|
|
crate = typeck.check_crate(sess, crate);
|
2010-12-29 10:21:16 -06:00
|
|
|
trans.trans_crate(sess, crate, output, shared);
|
2010-10-22 13:47:28 -05:00
|
|
|
}
|
2010-07-13 16:26:59 -05:00
|
|
|
|
2010-10-22 13:47:28 -05:00
|
|
|
fn warn_wrong_compiler() {
|
2010-11-22 18:27:00 -06:00
|
|
|
log "This is the rust 'self-hosted' compiler.";
|
|
|
|
log "The one written in rust.";
|
2011-01-26 15:51:29 -06:00
|
|
|
log "It is currently incomplete.";
|
2011-03-01 17:13:50 -06:00
|
|
|
log "You may want rustboot instead, the compiler next door.";
|
2010-10-22 13:47:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(session.session sess, str argv0) {
|
|
|
|
log #fmt("usage: %s [options] <input>", argv0);
|
|
|
|
log "options:";
|
|
|
|
log "";
|
|
|
|
log " -o <filename> write output to <filename>";
|
|
|
|
log " -nowarn suppress wrong-compiler warning";
|
2010-12-29 10:21:16 -06:00
|
|
|
log " -shared compile a shared-library crate";
|
2010-10-22 13:47:28 -05:00
|
|
|
log " -h display this message";
|
|
|
|
log "";
|
|
|
|
log "";
|
|
|
|
}
|
|
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
2010-11-02 13:11:58 -05:00
|
|
|
impure 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.
|
|
|
|
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 sess = session.session(target_cfg);
|
|
|
|
let option.t[str] input_file = none[str];
|
|
|
|
let option.t[str] output_file = none[str];
|
|
|
|
let bool do_warn = true;
|
2010-12-29 10:21:16 -06:00
|
|
|
let bool shared = false;
|
2010-10-22 13:47:28 -05:00
|
|
|
|
2010-11-22 18:27:00 -06:00
|
|
|
auto i = 1u;
|
|
|
|
auto len = _vec.len[str](args);
|
2010-10-22 13:47:28 -05:00
|
|
|
|
2010-11-22 18:27:00 -06: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;
|
2010-12-29 10:21:16 -06:00
|
|
|
} else if (_str.eq(arg, "-shared")) {
|
|
|
|
shared = true;
|
2011-03-06 12:00:52 -06:00
|
|
|
} else if (_str.eq(arg, "-o")) {
|
|
|
|
if (i+1u < len) {
|
|
|
|
output_file = some(args.(i+1u));
|
|
|
|
i += 1u;
|
2010-11-22 18:27:00 -06:00
|
|
|
} else {
|
2011-03-06 12:00:52 -06:00
|
|
|
usage(sess, args.(0));
|
|
|
|
sess.err("-o requires an argument");
|
2010-11-22 18:27:00 -06:00
|
|
|
}
|
2011-03-06 12:00:52 -06:00
|
|
|
} else if (_str.eq(arg, "-h")) {
|
|
|
|
usage(sess, args.(0));
|
|
|
|
} else {
|
|
|
|
usage(sess, args.(0));
|
|
|
|
sess.err("unrecognized option: " + arg);
|
2010-11-22 18:27:00 -06: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 13:47:28 -05:00
|
|
|
|
2010-11-22 18:27:00 -06:00
|
|
|
if (do_warn) {
|
|
|
|
warn_wrong_compiler();
|
|
|
|
}
|
2010-10-22 13:47:28 -05:00
|
|
|
|
2010-11-22 18:27:00 -06:00
|
|
|
alt (input_file) {
|
|
|
|
case (none[str]) {
|
|
|
|
usage(sess, args.(0));
|
|
|
|
sess.err("no input filename");
|
|
|
|
}
|
|
|
|
case (some[str](?ifile)) {
|
2011-03-01 17:57:55 -06:00
|
|
|
|
|
|
|
auto env = default_environment(sess, args.(0), ifile);
|
|
|
|
|
2010-11-22 18:27:00 -06:00
|
|
|
alt (output_file) {
|
|
|
|
case (none[str]) {
|
|
|
|
let vec[str] parts = _str.split(ifile, '.' as u8);
|
|
|
|
parts = _vec.pop[str](parts);
|
|
|
|
parts += ".bc";
|
|
|
|
auto ofile = _str.concat(parts);
|
2011-03-01 17:57:55 -06:00
|
|
|
compile_input(sess, env, ifile, ofile, shared);
|
2010-11-22 18:27:00 -06:00
|
|
|
}
|
|
|
|
case (some[str](?ofile)) {
|
2011-03-01 17:57:55 -06:00
|
|
|
compile_input(sess, env, ifile, ofile, shared);
|
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
|
|
|
|
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|