Check and translate 'as' cast-operator, lower target-specific types, reindent rustc.rs, enable uint test.

This commit is contained in:
Graydon Hoare 2010-11-22 16:27:00 -08:00
parent c262543d3b
commit d3cb25d5d1
5 changed files with 229 additions and 81 deletions

View File

@ -533,6 +533,7 @@ TEST_XFAILS_SELF := $(filter-out \
lazy-init.rs \
multiline-comment.rs \
return-nil.rs \
uint.rs \
unit.rs \
while-and-do-while.rs \
), \

View File

@ -5,6 +5,7 @@ import front.token;
import middle.trans;
import middle.resolve;
import middle.typeck;
import util.common;
import std.option;
import std.option.some;
@ -13,18 +14,18 @@ import std._str;
import std._vec;
impure fn compile_input(session.session sess, str input, str output) {
auto p = parser.new_parser(sess, 0, input);
auto crate = parser.parse_crate(p);
crate = resolve.resolve_crate(sess, crate);
crate = typeck.check_crate(sess, crate);
trans.trans_crate(sess, crate, output);
auto p = parser.new_parser(sess, 0, input);
auto crate = parser.parse_crate(p);
crate = resolve.resolve_crate(sess, crate);
crate = typeck.check_crate(sess, crate);
trans.trans_crate(sess, crate, output);
}
fn warn_wrong_compiler() {
log "This is the rust 'self-hosted' compiler.";
log "The one written in rust.";
log "It does nothing yet, it's a placeholder.";
log "You want rustboot, the compiler next door.";
log "This is the rust 'self-hosted' compiler.";
log "The one written in rust.";
log "It does nothing yet, it's a placeholder.";
log "You want rustboot, the compiler next door.";
}
fn usage(session.session sess, str argv0) {
@ -38,81 +39,95 @@ fn usage(session.session sess, str argv0) {
log "";
}
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; }
}
impure fn main(vec[str] args) {
auto sess = session.session();
let option.t[str] input_file = none[str];
let option.t[str] output_file = none[str];
let bool do_warn = true;
// 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 i = 1u;
auto len = _vec.len[str](args);
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;
// 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 {
// FIXME: rust could use an elif construct.
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, "-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);
}
}
// FIXME: dummy node to work around typestate mis-wiring bug.
i = i;
}
i += 1u;
}
auto i = 1u;
auto len = _vec.len[str](args);
if (do_warn) {
warn_wrong_compiler();
}
// 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 {
// FIXME: rust could use an elif construct.
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, "-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);
}
}
// FIXME: dummy node to work around typestate mis-wiring bug.
i = i;
}
i += 1u;
}
alt (input_file) {
case (none[str]) {
usage(sess, args.(0));
sess.err("no input filename");
}
case (some[str](?ifile)) {
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);
compile_input(sess, ifile, ofile);
}
case (some[str](?ofile)) {
compile_input(sess, ifile, ofile);
}
}
}
}
if (do_warn) {
warn_wrong_compiler();
}
alt (input_file) {
case (none[str]) {
usage(sess, args.(0));
sess.err("no input filename");
}
case (some[str](?ifile)) {
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);
compile_input(sess, ifile, ofile);
}
case (some[str](?ofile)) {
compile_input(sess, ifile, ofile);
}
}
}
}
}

View File

@ -1,7 +1,31 @@
import util.common.span;
import util.common.ty_mach;
import std._uint;
obj session() {
tag os {
os_win32;
os_macos;
os_linux;
}
tag arch {
arch_x86;
arch_x64;
arch_arm;
}
type cfg = rec(os os,
arch arch,
ty_mach int_type,
ty_mach uint_type,
ty_mach float_type);
obj session(cfg targ) {
fn get_targ_cfg() -> cfg {
ret targ;
}
fn span_err(span sp, str msg) {
log #fmt("%s:%u:%u:%u:%u: error: %s",
sp.filename,
@ -16,6 +40,11 @@ obj session() {
fail;
}
fn bug(str msg) {
log #fmt("error: internal compiler error %s", msg);
fail;
}
fn unimpl(str msg) {
log #fmt("error: unimplemented %s", msg);
fail;

View File

@ -551,18 +551,36 @@ impure fn trans_lit(@block_ctxt cx, &ast.lit lit) -> result {
}
}
fn node_type(@crate_ctxt cx, &ast.ann a) -> TypeRef {
fn target_type(@crate_ctxt cx, @typeck.ty t) -> @typeck.ty {
alt (t.struct) {
case (typeck.ty_int) {
auto tm = typeck.ty_machine(cx.sess.get_targ_cfg().int_type);
ret @rec(struct=tm with *t);
}
case (typeck.ty_uint) {
auto tm = typeck.ty_machine(cx.sess.get_targ_cfg().uint_type);
ret @rec(struct=tm with *t);
}
}
ret t;
}
fn node_ann_type(@crate_ctxt cx, &ast.ann a) -> @typeck.ty {
alt (a) {
case (ast.ann_none) {
log "missing type annotation";
fail;
}
case (ast.ann_type(?t)) {
ret type_of(cx, t);
ret target_type(cx, t);
}
}
}
fn node_type(@crate_ctxt cx, &ast.ann a) -> TypeRef {
ret type_of(cx, node_ann_type(cx, a));
}
impure fn trans_unary(@block_ctxt cx, ast.unop op,
&ast.expr e, &ast.ann a) -> result {
@ -962,6 +980,36 @@ impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
args_res._0.build.FastCall(f_res._0.val, llargs));
}
case (ast.expr_cast(?e, _, ?ann)) {
auto e_res = trans_expr(cx, *e);
auto llsrctype = val_ty(e_res.val);
auto t = node_ann_type(cx.fcx.ccx, ann);
auto lldsttype = type_of(cx.fcx.ccx, t);
if (!typeck.type_is_fp(t)) {
if (llvm.LLVMGetIntTypeWidth(lldsttype) >
llvm.LLVMGetIntTypeWidth(llsrctype)) {
if (typeck.type_is_signed(t)) {
// Widening signed cast.
e_res.val =
e_res.bcx.build.SExtOrBitCast(e_res.val,
lldsttype);
} else {
// Widening unsigned cast.
e_res.val =
e_res.bcx.build.ZExtOrBitCast(e_res.val,
lldsttype);
}
} else {
// Narrowing cast.
e_res.val =
e_res.bcx.build.TruncOrBitCast(e_res.val,
lldsttype);
}
} else {
cx.fcx.ccx.sess.unimpl("fp cast");
}
ret e_res;
}
}
cx.fcx.ccx.sess.unimpl("expr variant in trans_expr");
fail;

View File

@ -391,6 +391,44 @@ fn mode_is_alias(ast.mode m) -> bool {
}
}
fn type_is_scalar(@ty t) -> bool {
alt (t.struct) {
case (ty_bool) { ret true; }
case (ty_int) { ret true; }
case (ty_uint) { ret true; }
case (ty_machine(_)) { ret true; }
case (ty_char) { ret true; }
}
ret false;
}
fn type_is_fp(@ty t) -> bool {
alt (t.struct) {
case (ty_machine(?tm)) {
alt (tm) {
case (common.ty_f32) { ret true; }
case (common.ty_f64) { ret true; }
}
}
}
ret false;
}
fn type_is_signed(@ty t) -> bool {
alt (t.struct) {
case (ty_int) { ret true; }
case (ty_machine(?tm)) {
alt (tm) {
case (common.ty_i8) { ret true; }
case (common.ty_i16) { ret true; }
case (common.ty_i32) { ret true; }
case (common.ty_i64) { ret true; }
}
}
}
ret false;
}
fn plain_ty(&sty st) -> @ty {
ret @rec(struct=st, cname=none[str]);
}
@ -873,6 +911,23 @@ fn check_expr(&fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
ast.ann_type(ret_t)));
}
case (ast.expr_cast(?e, ?t, _)) {
auto e_1 = check_expr(fcx, e);
auto t_1 = ast_ty_to_ty_crate(fcx.ccx, t);
// FIXME: there are more forms of cast to support, eventually.
if (! (type_is_scalar(expr_ty(e_1)) &&
type_is_scalar(t_1))) {
fcx.ccx.sess.span_err(expr.span,
"non-scalar cast: "
+ ty_to_str(expr_ty(e_1))
+ " as "
+ ty_to_str(t_1));
}
ret @fold.respan[ast.expr_](expr.span,
ast.expr_cast(e_1, t,
ast.ann_type(t_1)));
}
case (_) {
// TODO
ret expr;