Skip to content

Commit 2028878

Browse files
committed
---
yaml --- r: 938 b: refs/heads/master c: d3cb25d h: refs/heads/master v: v3
1 parent 54945d6 commit 2028878

File tree

6 files changed

+230
-82
lines changed

6 files changed

+230
-82
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: c262543d3b353c46611059b70ee7ee3771b5df14
2+
refs/heads/master: d3cb25d5d1ede9d9ef06db6c30cd8448ed425127

trunk/src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ TEST_XFAILS_SELF := $(filter-out \
533533
lazy-init.rs \
534534
multiline-comment.rs \
535535
return-nil.rs \
536+
uint.rs \
536537
unit.rs \
537538
while-and-do-while.rs \
538539
), \

trunk/src/comp/driver/rustc.rs

Lines changed: 93 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import front.token;
55
import middle.trans;
66
import middle.resolve;
77
import middle.typeck;
8+
import util.common;
89

910
import std.option;
1011
import std.option.some;
@@ -13,18 +14,18 @@ import std._str;
1314
import std._vec;
1415

1516
impure fn compile_input(session.session sess, str input, str output) {
16-
auto p = parser.new_parser(sess, 0, input);
17-
auto crate = parser.parse_crate(p);
18-
crate = resolve.resolve_crate(sess, crate);
19-
crate = typeck.check_crate(sess, crate);
20-
trans.trans_crate(sess, crate, output);
17+
auto p = parser.new_parser(sess, 0, input);
18+
auto crate = parser.parse_crate(p);
19+
crate = resolve.resolve_crate(sess, crate);
20+
crate = typeck.check_crate(sess, crate);
21+
trans.trans_crate(sess, crate, output);
2122
}
2223

2324
fn warn_wrong_compiler() {
24-
log "This is the rust 'self-hosted' compiler.";
25-
log "The one written in rust.";
26-
log "It does nothing yet, it's a placeholder.";
27-
log "You want rustboot, the compiler next door.";
25+
log "This is the rust 'self-hosted' compiler.";
26+
log "The one written in rust.";
27+
log "It does nothing yet, it's a placeholder.";
28+
log "You want rustboot, the compiler next door.";
2829
}
2930

3031
fn usage(session.session sess, str argv0) {
@@ -38,81 +39,95 @@ fn usage(session.session sess, str argv0) {
3839
log "";
3940
}
4041

42+
fn get_os() -> session.os {
43+
auto s = std.os.target_os();
44+
if (_str.eq(s, "win32")) { ret session.os_win32; }
45+
if (_str.eq(s, "macos")) { ret session.os_macos; }
46+
if (_str.eq(s, "linux")) { ret session.os_linux; }
47+
}
48+
4149
impure fn main(vec[str] args) {
4250

43-
auto sess = session.session();
44-
let option.t[str] input_file = none[str];
45-
let option.t[str] output_file = none[str];
46-
let bool do_warn = true;
51+
// FIXME: don't hard-wire this.
52+
auto target_cfg = rec(os = get_os(),
53+
arch = session.arch_x86,
54+
int_type = common.ty_i32,
55+
uint_type = common.ty_u32,
56+
float_type = common.ty_f64 );
57+
58+
auto sess = session.session(target_cfg);
59+
let option.t[str] input_file = none[str];
60+
let option.t[str] output_file = none[str];
61+
let bool do_warn = true;
4762

48-
auto i = 1u;
49-
auto len = _vec.len[str](args);
63+
auto i = 1u;
64+
auto len = _vec.len[str](args);
5065

51-
// FIXME: a getopt module would be nice.
52-
while (i < len) {
53-
auto arg = args.(i);
54-
if (_str.byte_len(arg) > 0u && arg.(0) == '-' as u8) {
55-
if (_str.eq(arg, "-nowarn")) {
56-
do_warn = false;
57-
} else {
58-
// FIXME: rust could use an elif construct.
59-
if (_str.eq(arg, "-o")) {
60-
if (i+1u < len) {
61-
output_file = some(args.(i+1u));
62-
i += 1u;
63-
} else {
64-
usage(sess, args.(0));
65-
sess.err("-o requires an argument");
66-
}
67-
} else {
68-
if (_str.eq(arg, "-h")) {
69-
usage(sess, args.(0));
70-
} else {
71-
usage(sess, args.(0));
72-
sess.err("unrecognized option: " + arg);
73-
}
74-
}
75-
}
76-
} else {
77-
alt (input_file) {
78-
case (some[str](_)) {
79-
usage(sess, args.(0));
80-
sess.err("multiple inputs provided");
81-
}
82-
case (none[str]) {
83-
input_file = some[str](arg);
84-
}
85-
}
86-
// FIXME: dummy node to work around typestate mis-wiring bug.
87-
i = i;
88-
}
89-
i += 1u;
90-
}
66+
// FIXME: a getopt module would be nice.
67+
while (i < len) {
68+
auto arg = args.(i);
69+
if (_str.byte_len(arg) > 0u && arg.(0) == '-' as u8) {
70+
if (_str.eq(arg, "-nowarn")) {
71+
do_warn = false;
72+
} else {
73+
// FIXME: rust could use an elif construct.
74+
if (_str.eq(arg, "-o")) {
75+
if (i+1u < len) {
76+
output_file = some(args.(i+1u));
77+
i += 1u;
78+
} else {
79+
usage(sess, args.(0));
80+
sess.err("-o requires an argument");
81+
}
82+
} else {
83+
if (_str.eq(arg, "-h")) {
84+
usage(sess, args.(0));
85+
} else {
86+
usage(sess, args.(0));
87+
sess.err("unrecognized option: " + arg);
88+
}
89+
}
90+
}
91+
} else {
92+
alt (input_file) {
93+
case (some[str](_)) {
94+
usage(sess, args.(0));
95+
sess.err("multiple inputs provided");
96+
}
97+
case (none[str]) {
98+
input_file = some[str](arg);
99+
}
100+
}
101+
// FIXME: dummy node to work around typestate mis-wiring bug.
102+
i = i;
103+
}
104+
i += 1u;
105+
}
91106

92-
if (do_warn) {
93-
warn_wrong_compiler();
94-
}
107+
if (do_warn) {
108+
warn_wrong_compiler();
109+
}
95110

96-
alt (input_file) {
97-
case (none[str]) {
98-
usage(sess, args.(0));
99-
sess.err("no input filename");
100-
}
101-
case (some[str](?ifile)) {
102-
alt (output_file) {
103-
case (none[str]) {
104-
let vec[str] parts = _str.split(ifile, '.' as u8);
105-
parts = _vec.pop[str](parts);
106-
parts += ".bc";
107-
auto ofile = _str.concat(parts);
108-
compile_input(sess, ifile, ofile);
109-
}
110-
case (some[str](?ofile)) {
111-
compile_input(sess, ifile, ofile);
112-
}
113-
}
114-
}
115-
}
111+
alt (input_file) {
112+
case (none[str]) {
113+
usage(sess, args.(0));
114+
sess.err("no input filename");
115+
}
116+
case (some[str](?ifile)) {
117+
alt (output_file) {
118+
case (none[str]) {
119+
let vec[str] parts = _str.split(ifile, '.' as u8);
120+
parts = _vec.pop[str](parts);
121+
parts += ".bc";
122+
auto ofile = _str.concat(parts);
123+
compile_input(sess, ifile, ofile);
124+
}
125+
case (some[str](?ofile)) {
126+
compile_input(sess, ifile, ofile);
127+
}
128+
}
129+
}
130+
}
116131
}
117132

118133

trunk/src/comp/driver/session.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
import util.common.span;
2+
import util.common.ty_mach;
23
import std._uint;
34

4-
obj session() {
5+
tag os {
6+
os_win32;
7+
os_macos;
8+
os_linux;
9+
}
10+
11+
tag arch {
12+
arch_x86;
13+
arch_x64;
14+
arch_arm;
15+
}
16+
17+
type cfg = rec(os os,
18+
arch arch,
19+
ty_mach int_type,
20+
ty_mach uint_type,
21+
ty_mach float_type);
22+
23+
obj session(cfg targ) {
24+
25+
fn get_targ_cfg() -> cfg {
26+
ret targ;
27+
}
28+
529
fn span_err(span sp, str msg) {
630
log #fmt("%s:%u:%u:%u:%u: error: %s",
731
sp.filename,
@@ -16,6 +40,11 @@ obj session() {
1640
fail;
1741
}
1842

43+
fn bug(str msg) {
44+
log #fmt("error: internal compiler error %s", msg);
45+
fail;
46+
}
47+
1948
fn unimpl(str msg) {
2049
log #fmt("error: unimplemented %s", msg);
2150
fail;

trunk/src/comp/middle/trans.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,18 +551,36 @@ impure fn trans_lit(@block_ctxt cx, &ast.lit lit) -> result {
551551
}
552552
}
553553

554-
fn node_type(@crate_ctxt cx, &ast.ann a) -> TypeRef {
554+
fn target_type(@crate_ctxt cx, @typeck.ty t) -> @typeck.ty {
555+
alt (t.struct) {
556+
case (typeck.ty_int) {
557+
auto tm = typeck.ty_machine(cx.sess.get_targ_cfg().int_type);
558+
ret @rec(struct=tm with *t);
559+
}
560+
case (typeck.ty_uint) {
561+
auto tm = typeck.ty_machine(cx.sess.get_targ_cfg().uint_type);
562+
ret @rec(struct=tm with *t);
563+
}
564+
}
565+
ret t;
566+
}
567+
568+
fn node_ann_type(@crate_ctxt cx, &ast.ann a) -> @typeck.ty {
555569
alt (a) {
556570
case (ast.ann_none) {
557571
log "missing type annotation";
558572
fail;
559573
}
560574
case (ast.ann_type(?t)) {
561-
ret type_of(cx, t);
575+
ret target_type(cx, t);
562576
}
563577
}
564578
}
565579

580+
fn node_type(@crate_ctxt cx, &ast.ann a) -> TypeRef {
581+
ret type_of(cx, node_ann_type(cx, a));
582+
}
583+
566584
impure fn trans_unary(@block_ctxt cx, ast.unop op,
567585
&ast.expr e, &ast.ann a) -> result {
568586

@@ -962,6 +980,36 @@ impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
962980
args_res._0.build.FastCall(f_res._0.val, llargs));
963981
}
964982

983+
case (ast.expr_cast(?e, _, ?ann)) {
984+
auto e_res = trans_expr(cx, *e);
985+
auto llsrctype = val_ty(e_res.val);
986+
auto t = node_ann_type(cx.fcx.ccx, ann);
987+
auto lldsttype = type_of(cx.fcx.ccx, t);
988+
if (!typeck.type_is_fp(t)) {
989+
if (llvm.LLVMGetIntTypeWidth(lldsttype) >
990+
llvm.LLVMGetIntTypeWidth(llsrctype)) {
991+
if (typeck.type_is_signed(t)) {
992+
// Widening signed cast.
993+
e_res.val =
994+
e_res.bcx.build.SExtOrBitCast(e_res.val,
995+
lldsttype);
996+
} else {
997+
// Widening unsigned cast.
998+
e_res.val =
999+
e_res.bcx.build.ZExtOrBitCast(e_res.val,
1000+
lldsttype);
1001+
}
1002+
} else {
1003+
// Narrowing cast.
1004+
e_res.val =
1005+
e_res.bcx.build.TruncOrBitCast(e_res.val,
1006+
lldsttype);
1007+
}
1008+
} else {
1009+
cx.fcx.ccx.sess.unimpl("fp cast");
1010+
}
1011+
ret e_res;
1012+
}
9651013
}
9661014
cx.fcx.ccx.sess.unimpl("expr variant in trans_expr");
9671015
fail;

0 commit comments

Comments
 (0)