Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untitled #278

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ self: $(CFG_RUSTC)
# Testing
######################################################################

# Float doesn't work in boot

FLOAT_XFAILS := test/run-pass/float.rs

# Temporarily xfail tests broken by the nominal-tags change.

NOMINAL_TAG_XFAILS := test/run-pass/mlist.rs
Expand All @@ -441,6 +445,7 @@ TASK_XFAILS := test/run-pass/task-comm-8.rs \
TEST_XFAILS_BOOT := $(TASK_XFAILS) \
$(NOMINAL_TAG_XFAILS) \
$(CONST_TAG_XFAILS) \
$(FLOAT_XFAILS) \
test/run-pass/arith-unsigned.rs \
test/run-pass/box-compare.rs \
test/run-pass/child-outlives-parent.rs \
Expand Down
2 changes: 2 additions & 0 deletions src/comp/front/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ tag lit_ {
lit_int(int);
lit_uint(uint);
lit_mach_int(ty_mach, int);
lit_float(str); /* represent float as string for now */
lit_nil;
lit_bool(bool);
}
Expand All @@ -274,6 +275,7 @@ tag ty_ {
ty_bool;
ty_int;
ty_uint;
ty_float;
ty_machine(util.common.ty_mach);
ty_char;
ty_str;
Expand Down
43 changes: 35 additions & 8 deletions src/comp/front/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import std.io;
import std._str;
import std._int;
import std.map;
import std.map.hashmap;
import util.common;
Expand Down Expand Up @@ -314,6 +315,24 @@ impure fn consume_block_comment(reader rdr) {
be consume_any_whitespace(rdr);
}

impure fn scan_dec_digits(reader rdr) -> int {

auto c = rdr.curr();

let int accum_int = 0;

while (is_dec_digit(c) || c == '_') {
if (c != '_') {
accum_int *= 10;
accum_int += dec_digit_val(c);
}
rdr.bump();
c = rdr.curr();
}

ret accum_int;
}

impure fn scan_number(mutable char c, reader rdr) -> token.token {
auto accum_int = 0;
auto n = rdr.next();
Expand Down Expand Up @@ -346,15 +365,12 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
}
}

while (is_dec_digit(c) || c == '_') {
if (c != '_') {
accum_int *= 10;
accum_int += dec_digit_val(c);
}
rdr.bump();
c = rdr.curr();
if (c != '0' || (n != 'x' && n != 'b')) {
accum_int = scan_dec_digits(rdr);
}

c = rdr.curr();

if (c == 'u' || c == 'i') {
let bool signed = (c == 'i');
rdr.bump();
Expand Down Expand Up @@ -405,7 +421,18 @@ impure fn scan_number(mutable char c, reader rdr) -> token.token {
ret token.LIT_UINT(accum_int as uint);
}
}
ret token.LIT_INT(accum_int);
n = rdr.curr();
if(n == '.') {
/* parse a floating-point number */
rdr.bump();
auto accum_int1 = scan_dec_digits(rdr);
ret token.LIT_FLOAT(_int.to_str(accum_int, 10u) + "."
+ _int.to_str(accum_int1, 10u));
/* TODO: exponent */
}
else {
ret token.LIT_INT(accum_int);
}
}

impure fn next_token(reader rdr) -> token.token {
Expand Down
4 changes: 4 additions & 0 deletions src/comp/front/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ impure fn parse_lit(parser p) -> ast.lit {
p.bump();
lit = ast.lit_uint(u);
}
case (token.LIT_FLOAT(?s)) {
p.bump();
lit = ast.lit_float(s);
}
case (token.LIT_MACH_INT(?tm, ?i)) {
p.bump();
lit = ast.lit_mach_int(tm, i);
Expand Down
3 changes: 2 additions & 1 deletion src/comp/front/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ tag token {
LIT_INT(int);
LIT_UINT(uint);
LIT_MACH_INT(ty_mach, int);
LIT_FLOAT(str); /* represent floating-point as string for now */
LIT_STR(str);
LIT_CHAR(char);
LIT_BOOL(bool);
Expand Down Expand Up @@ -295,7 +296,7 @@ fn to_str(token t) -> str {
ret _int.to_str(i, 10u)
+ "_" + ty_mach_to_str(tm);
}

case (LIT_FLOAT(?s)) { ret s; }
case (LIT_STR(?s)) {
// FIXME: escape.
ret "\"" + s + "\"";
Expand Down
80 changes: 74 additions & 6 deletions src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ fn type_of_inner(@crate_ctxt cx, @ty.t t, bool boxed) -> TypeRef {
case (ty.ty_nil) { llty = T_nil(); }
case (ty.ty_bool) { llty = T_bool(); }
case (ty.ty_int) { llty = T_int(); }
case (ty.ty_float) { llty = T_f64(); }
case (ty.ty_uint) { llty = T_int(); }
case (ty.ty_machine(?tm)) {
alt (tm) {
Expand Down Expand Up @@ -743,6 +744,12 @@ fn C_integral(int i, TypeRef t) -> ValueRef {
ret llvm.LLVMConstIntOfString(t, _str.buf(istr(i)), 10);
}

fn C_float(str s) -> ValueRef {
// FIXME: shouldn't use strings
// also, assuming 64-bit floats for now.
ret llvm.LLVMConstRealOfString(T_f64(), _str.buf(s));
}

fn C_nil() -> ValueRef {
// NB: See comment above in T_void().
ret C_integral(0, T_i1());
Expand Down Expand Up @@ -871,7 +878,7 @@ fn trans_upcall2(builder b, @glue_fns glues,

let ValueRef llglue = glues.upcall_glues.(n);
let vec[ValueRef] call_args = vec(llupcall);

for (ValueRef a in args) {
call_args += vec(b.ZExtOrBitCast(a, T_int()));
}
Expand Down Expand Up @@ -2279,6 +2286,9 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
}
ret C_integral(i, t);
}
case(ast.lit_float(?fs)) {
ret C_float(fs);
}
case (ast.lit_char(?c)) {
ret C_integral(c as int, T_char());
}
Expand Down Expand Up @@ -2346,6 +2356,7 @@ fn trans_unary(@block_ctxt cx, ast.unop op,
@ast.expr e, &ast.ann a) -> result {

auto sub = trans_expr(cx, e);
auto e_ty = ty.expr_ty(e);

alt (op) {
case (ast.bitnot) {
Expand All @@ -2358,7 +2369,12 @@ fn trans_unary(@block_ctxt cx, ast.unop op,
}
case (ast.neg) {
sub = autoderef(sub.bcx, sub.val, ty.expr_ty(e));
ret res(sub.bcx, sub.bcx.build.Neg(sub.val));
if(e_ty.struct == ty.ty_float) {
ret res(sub.bcx, sub.bcx.build.FNeg(sub.val));
}
else {
ret res(sub.bcx, sub.bcx.build.Neg(sub.val));
}
}
case (ast.box) {
auto e_ty = ty.expr_ty(e);
Expand Down Expand Up @@ -2653,24 +2669,60 @@ fn trans_vec_add(@block_ctxt cx, @ty.t t,
fn trans_eager_binop(@block_ctxt cx, ast.binop op, @ty.t intype,
ValueRef lhs, ValueRef rhs) -> result {

auto is_float = false;
alt(intype.struct) {
case (ty.ty_float) {
is_float = true;
}
case (_) {
is_float = false;
}
}

alt (op) {
case (ast.add) {
if (ty.type_is_sequence(intype)) {
ret trans_vec_add(cx, intype, lhs, rhs);
}
ret res(cx, cx.build.Add(lhs, rhs));
if (is_float) {
ret res(cx, cx.build.FAdd(lhs, rhs));
}
else {
ret res(cx, cx.build.Add(lhs, rhs));
}
}
case (ast.sub) {
if (is_float) {
ret res(cx, cx.build.FSub(lhs, rhs));
}
else {
ret res(cx, cx.build.Sub(lhs, rhs));
}
}

case (ast.mul) {
if (is_float) {
ret res(cx, cx.build.FMul(lhs, rhs));
}
else {
ret res(cx, cx.build.Mul(lhs, rhs));
}
}
case (ast.sub) { ret res(cx, cx.build.Sub(lhs, rhs)); }

case (ast.mul) { ret res(cx, cx.build.Mul(lhs, rhs)); }
case (ast.div) {
if (is_float) {
ret res(cx, cx.build.FDiv(lhs, rhs));
}
if (ty.type_is_signed(intype)) {
ret res(cx, cx.build.SDiv(lhs, rhs));
} else {
ret res(cx, cx.build.UDiv(lhs, rhs));
}
}
case (ast.rem) {
if (is_float) {
ret res(cx, cx.build.FRem(lhs, rhs));
}
if (ty.type_is_signed(intype)) {
ret res(cx, cx.build.SRem(lhs, rhs));
} else {
Expand Down Expand Up @@ -4465,13 +4517,30 @@ fn trans_log(@block_ctxt cx, @ast.expr e) -> result {

auto sub = trans_expr(cx, e);
auto e_ty = ty.expr_ty(e);
alt (e_ty.struct) {
case(ty.ty_float) {
auto sub1 = (sub.bcx).build.Alloca(T_double()); /* guess we're assuming double = float64 for now */
(sub.bcx).build.Store(sub.val, sub1);
sub.val = sub1;
}
case(_) {
sub = trans_expr(cx, e);
}
}

alt (e_ty.struct) {
case (ty.ty_str) {
auto v = vp2i(sub.bcx, sub.val);
ret trans_upcall(sub.bcx,
"upcall_log_str",
vec(v));
}
case (ty.ty_float) {
auto v = vp2i(sub.bcx, sub.val);
ret trans_upcall(sub.bcx,
"upcall_log_float",
vec(v));
}
case (_) {
ret trans_upcall(sub.bcx,
"upcall_log_int",
Expand Down Expand Up @@ -6238,7 +6307,6 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
collect_items(cx, crate);
collect_tag_ctors(cx, crate);
trans_constants(cx, crate);

trans_mod(cx, crate.node.module);
trans_vec_append_glue(cx);
if (!shared) {
Expand Down
30 changes: 19 additions & 11 deletions src/comp/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tag sty {
ty_nil;
ty_bool;
ty_int;
ty_float;
ty_uint;
ty_machine(util.common.ty_mach);
ty_char;
Expand Down Expand Up @@ -158,19 +159,20 @@ fn ty_to_str(&@t typ) -> str {

auto s = "";
alt (typ.struct) {
case (ty_native) { s += "native"; }
case (ty_nil) { s += "()"; }
case (ty_bool) { s += "bool"; }
case (ty_int) { s += "int"; }
case (ty_uint) { s += "uint"; }
case (ty_machine(?tm)) { s += common.ty_mach_to_str(tm); }
case (ty_char) { s += "char"; }
case (ty_str) { s += "str"; }
case (ty_native) { s = "native"; }
case (ty_nil) { s = "()"; }
case (ty_bool) { s = "bool"; }
case (ty_int) { s = "int"; }
case (ty_float) { s = "float"; }
case (ty_uint) { s = "uint"; }
case (ty_machine(?tm)) { s = common.ty_mach_to_str(tm); }
case (ty_char) { s = "char"; }
case (ty_str) { s = "str"; }
case (ty_box(?tm)) { s += "@" + mt_to_str(tm); }
case (ty_vec(?tm)) { s += "vec[" + mt_to_str(tm) + "]"; }
case (ty_port(?t)) { s += "port[" + ty_to_str(t) + "]"; }
case (ty_chan(?t)) { s += "chan[" + ty_to_str(t) + "]"; }
case (ty_type) { s += "type"; }
case (ty_port(?t)) { s = "port[" + ty_to_str(t) + "]"; }
case (ty_chan(?t)) { s = "chan[" + ty_to_str(t) + "]"; }
case (ty_type) { s = "type"; }

case (ty_tup(?elems)) {
auto f = mt_to_str;
Expand Down Expand Up @@ -243,6 +245,7 @@ fn fold_ty(ty_fold fld, @t ty) -> @t {
case (ty_bool) { ret fld.fold_simple_ty(ty); }
case (ty_int) { ret fld.fold_simple_ty(ty); }
case (ty_uint) { ret fld.fold_simple_ty(ty); }
case (ty_float) { ret fld.fold_simple_ty(ty); }
case (ty_machine(_)) { ret fld.fold_simple_ty(ty); }
case (ty_char) { ret fld.fold_simple_ty(ty); }
case (ty_str) { ret fld.fold_simple_ty(ty); }
Expand Down Expand Up @@ -418,6 +421,7 @@ fn type_is_scalar(@t ty) -> bool {
case (ty_nil) { ret true; }
case (ty_bool) { ret true; }
case (ty_int) { ret true; }
case (ty_float) { ret true; }
case (ty_uint) { ret true; }
case (ty_machine(_)) { ret true; }
case (ty_char) { ret true; }
Expand Down Expand Up @@ -500,6 +504,9 @@ fn type_is_fp(@t ty) -> bool {
case (_) { ret false; }
}
}
case (ty_float) {
ret true;
}
case (_) { ret false; }
}
fail;
Expand Down Expand Up @@ -1123,6 +1130,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
case (ty.ty_int) { ret struct_cmp(expected, actual); }
case (ty.ty_uint) { ret struct_cmp(expected, actual); }
case (ty.ty_machine(_)) { ret struct_cmp(expected, actual); }
case (ty.ty_float) { ret struct_cmp(expected, actual); }
case (ty.ty_char) { ret struct_cmp(expected, actual); }
case (ty.ty_str) { ret struct_cmp(expected, actual); }
case (ty.ty_type) { ret struct_cmp(expected, actual); }
Expand Down
1 change: 1 addition & 0 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,7 @@ fn check_lit(@ast.lit lit) -> @ty.t {
case (ast.lit_str(_)) { sty = ty.ty_str; }
case (ast.lit_char(_)) { sty = ty.ty_char; }
case (ast.lit_int(_)) { sty = ty.ty_int; }
case (ast.lit_float(_)) { sty = ty.ty_float; }
case (ast.lit_uint(_)) { sty = ty.ty_uint; }
case (ast.lit_mach_int(?tm, _)) { sty = ty.ty_machine(tm); }
case (ast.lit_nil) { sty = ty.ty_nil; }
Expand Down
3 changes: 3 additions & 0 deletions src/comp/pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ impure fn print_literal(ps s, @ast.lit lit) {
case (ast.lit_uint(?val)) { // TODO clipping? uistr?
wrd(s, util.common.istr(val as int) + "u");
}
case (ast.lit_float(?fstr)) {
wrd(s, fstr);
}
case (ast.lit_mach_int(?mach,?val)) {
wrd(s, util.common.istr(val as int));
wrd(s, util.common.ty_mach_to_str(mach));
Expand Down
1 change: 1 addition & 0 deletions src/comp/rustc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ auth middle.metadata = unsafe;
auth middle.trans = unsafe;
auth middle.trans.copy_args_to_allocas = impure;
auth middle.trans.trans_block = impure;
auth middle.trans.trans_log = impure;
auth lib.llvm = unsafe;
auth pretty.pprust = impure;

Expand Down
Loading