Skip to content

Commit

Permalink
Change literal representation to not truncate
Browse files Browse the repository at this point in the history
Also shuffles around the organization of numeric literals and types,
separating by int/uint/float instead of machine-vs-non-machine types.
This simplifies some code.

Closes #974
Closes #1252
  • Loading branch information
marijnh committed Dec 7, 2011
1 parent 6daa233 commit e3eca91
Show file tree
Hide file tree
Showing 24 changed files with 479 additions and 731 deletions.
8 changes: 4 additions & 4 deletions src/comp/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import syntax::{ast, codemap};
import syntax::ast::node_id;
import codemap::span;
import syntax::ast::ty_mach;
import syntax::ast::{int_ty, uint_ty, float_ty};
import std::{option};
import std::option::{some, none};
import syntax::parse::parser::parse_sess;
Expand All @@ -17,9 +17,9 @@ type config =
{os: os,
arch: arch,
target_strs: target_strs::t,
int_type: ty_mach,
uint_type: ty_mach,
float_type: ty_mach};
int_type: int_ty,
uint_type: uint_ty,
float_type: float_ty};

type options =
// The crate config requested for the session, which may be combined
Expand Down
20 changes: 10 additions & 10 deletions src/comp/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,16 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
'l' { ret ty::mk_float(st.tcx); }
'M' {
alt next(st) as char {
'b' { ret ty::mk_mach(st.tcx, ast::ty_u8); }
'w' { ret ty::mk_mach(st.tcx, ast::ty_u16); }
'l' { ret ty::mk_mach(st.tcx, ast::ty_u32); }
'd' { ret ty::mk_mach(st.tcx, ast::ty_u64); }
'B' { ret ty::mk_mach(st.tcx, ast::ty_i8); }
'W' { ret ty::mk_mach(st.tcx, ast::ty_i16); }
'L' { ret ty::mk_mach(st.tcx, ast::ty_i32); }
'D' { ret ty::mk_mach(st.tcx, ast::ty_i64); }
'f' { ret ty::mk_mach(st.tcx, ast::ty_f32); }
'F' { ret ty::mk_mach(st.tcx, ast::ty_f64); }
'b' { ret ty::mk_mach_uint(st.tcx, ast::ty_u8); }
'w' { ret ty::mk_mach_uint(st.tcx, ast::ty_u16); }
'l' { ret ty::mk_mach_uint(st.tcx, ast::ty_u32); }
'd' { ret ty::mk_mach_uint(st.tcx, ast::ty_u64); }
'B' { ret ty::mk_mach_int(st.tcx, ast::ty_i8); }
'W' { ret ty::mk_mach_int(st.tcx, ast::ty_i16); }
'L' { ret ty::mk_mach_int(st.tcx, ast::ty_i32); }
'D' { ret ty::mk_mach_int(st.tcx, ast::ty_i64); }
'f' { ret ty::mk_mach_float(st.tcx, ast::ty_f32); }
'F' { ret ty::mk_mach_float(st.tcx, ast::ty_f64); }
}
}
'c' { ret ty::mk_char(st.tcx); }
Expand Down
28 changes: 18 additions & 10 deletions src/comp/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,32 @@ fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
ty::ty_nil. { w.write_char('n'); }
ty::ty_bot. { w.write_char('z'); }
ty::ty_bool. { w.write_char('b'); }
ty::ty_int. { w.write_char('i'); }
ty::ty_uint. { w.write_char('u'); }
ty::ty_float. { w.write_char('l'); }
ty::ty_machine(mach) {
alt mach {
ty_u8. { w.write_str("Mb"); }
ty_u16. { w.write_str("Mw"); }
ty_u32. { w.write_str("Ml"); }
ty_u64. { w.write_str("Md"); }
ty::ty_int(t) {
alt t {
ty_i. { w.write_char('i'); }
ty_char. { w.write_char('c'); }
ty_i8. { w.write_str("MB"); }
ty_i16. { w.write_str("MW"); }
ty_i32. { w.write_str("ML"); }
ty_i64. { w.write_str("MD"); }
}
}
ty::ty_uint(t) {
alt t {
ty_u. { w.write_char('u'); }
ty_u8. { w.write_str("Mb"); }
ty_u16. { w.write_str("Mw"); }
ty_u32. { w.write_str("Ml"); }
ty_u64. { w.write_str("Md"); }
}
}
ty::ty_float(t) {
alt t {
ty_f. { w.write_char('l'); }
ty_f32. { w.write_str("Mf"); }
ty_f64. { w.write_str("MF"); }
}
}
ty::ty_char. { w.write_char('c'); }
ty::ty_str. { w.write_char('S'); }
ty::ty_tag(def, tys) {
w.write_str("t[");
Expand Down
5 changes: 2 additions & 3 deletions src/comp/middle/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,8 @@ fn local_id_of_node(cx: ctx, id: node_id) -> uint {
fn copy_is_expensive(tcx: ty::ctxt, ty: ty::t) -> bool {
fn score_ty(tcx: ty::ctxt, ty: ty::t) -> uint {
ret alt ty::struct(tcx, ty) {
ty::ty_nil. | ty::ty_bot. | ty::ty_bool. | ty::ty_int. |
ty::ty_uint. | ty::ty_float. | ty::ty_machine(_) |
ty::ty_char. | ty::ty_type. | ty::ty_native(_) |
ty::ty_nil. | ty::ty_bot. | ty::ty_bool. | ty::ty_int(_) |
ty::ty_uint(_) | ty::ty_float(_) | ty::ty_type. | ty::ty_native(_) |
ty::ty_ptr(_) { 1u }
ty::ty_box(_) { 3u }
ty::ty_constr(t, _) | ty::ty_res(_, t, _) { score_ty(tcx, t) }
Expand Down
4 changes: 2 additions & 2 deletions src/comp/middle/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ fn add_gc_root(cx: @block_ctxt, llval: ValueRef, ty: ty::t) -> @block_ctxt {

fn type_is_gc_relevant(cx: ty::ctxt, ty: ty::t) -> bool {
alt ty::struct(cx, ty) {
ty::ty_nil. | ty::ty_bot. | ty::ty_bool. | ty::ty_int. | ty::ty_float. |
ty::ty_uint. | ty::ty_machine(_) | ty::ty_char. | ty::ty_str. |
ty::ty_nil. | ty::ty_bot. | ty::ty_bool. | ty::ty_int(_) |
ty::ty_float(_) | ty::ty_uint(_) | ty::ty_str. |
ty::ty_type. | ty::ty_native(_) | ty::ty_ptr(_) | ty::ty_type. |
ty::ty_native(_) {
ret false;
Expand Down
82 changes: 16 additions & 66 deletions src/comp/middle/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,54 +299,27 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint],
let s = [];

alt ty::struct(ccx.tcx, t) {
ty::ty_nil. | ty::ty_bool. | ty::ty_machine(ast::ty_u8.) | ty::ty_bot. {
s += [shape_u8];
}





ty::ty_int. {
s += [s_int(ccx.tcx)];
}
ty::ty_float. { s += [s_float(ccx.tcx)]; }





ty::ty_uint. | ty::ty_ptr(_) | ty::ty_type. | ty::ty_native(_) {
s += [s_uint(ccx.tcx)];
}





ty::ty_machine(ast::ty_i8.) {
s += [shape_i8];
}
ty::ty_machine(ast::ty_u16.) { s += [shape_u16]; }
ty::ty_machine(ast::ty_i16.) { s += [shape_i16]; }
ty::ty_machine(ast::ty_u32.) | ty::ty_char. { s += [shape_u32]; }
ty::ty_machine(ast::ty_i32.) { s += [shape_i32]; }
ty::ty_machine(ast::ty_u64.) { s += [shape_u64]; }
ty::ty_machine(ast::ty_i64.) { s += [shape_i64]; }

ty::ty_machine(ast::ty_f32.) { s += [shape_f32]; }
ty::ty_machine(ast::ty_f64.) { s += [shape_f64]; }

ty::ty_nil. | ty::ty_bool. | ty::ty_uint(ast::ty_u8.) |
ty::ty_bot. { s += [shape_u8]; }
ty::ty_int(ast::ty_i.) { s += [s_int(ccx.tcx)]; }
ty::ty_float(ast::ty_f.) { s += [s_float(ccx.tcx)]; }
ty::ty_uint(ast::ty_u.) | ty::ty_ptr(_) | ty::ty_type. |
ty::ty_native(_) { s += [s_uint(ccx.tcx)]; }
ty::ty_int(ast::ty_i8.) { s += [shape_i8]; }
ty::ty_uint(ast::ty_u16.) { s += [shape_u16]; }
ty::ty_int(ast::ty_i16.) { s += [shape_i16]; }
ty::ty_uint(ast::ty_u32.) { s += [shape_u32]; }
ty::ty_int(ast::ty_i32.) | ty::ty_int(ast::ty_char.) {s += [shape_i32];}
ty::ty_uint(ast::ty_u64.) { s += [shape_u64]; }
ty::ty_int(ast::ty_i64.) { s += [shape_i64]; }
ty::ty_float(ast::ty_f32.) { s += [shape_f32]; }
ty::ty_float(ast::ty_f64.) { s += [shape_f64]; }
ty::ty_str. {
s += [shape_vec];
add_bool(s, true); // type is POD
let unit_ty = ty::mk_mach(ccx.tcx, ast::ty_u8);
let unit_ty = ty::mk_mach_uint(ccx.tcx, ast::ty_u8);
add_substr(s, shape_of(ccx, unit_ty, ty_param_map, is_obj_body));
}




ty::ty_tag(did, tps) {
alt tag_kind(ccx, did) {
tk_unit. {
Expand Down Expand Up @@ -382,11 +355,6 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint],
}
}
}





ty::ty_box(mt) {
s += [shape_box];
add_substr(s, shape_of(ccx, mt.ty, ty_param_map, is_obj_body));
Expand Down Expand Up @@ -416,21 +384,11 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint],
}
add_substr(s, sub);
}





ty::ty_fn(_, _, _, _, _) {
s += [shape_fn];
}
ty::ty_native_fn(_, _) { s += [shape_u32]; }
ty::ty_obj(_) { s += [shape_obj]; }





ty::ty_res(did, raw_subt, tps) {
let subt = ty::substitute_type_params(ccx.tcx, tps, raw_subt);
let ri = {did: did, t: subt};
Expand All @@ -445,17 +403,9 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint],
add_substr(s, shape_of(ccx, subt, ty_param_map, is_obj_body));

}




ty::ty_var(n) {
fail "shape_of ty_var";
}




ty::ty_param(n, _) {
if is_obj_body {
// Just write in the parameter number.
Expand Down
75 changes: 13 additions & 62 deletions src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,9 @@ fn type_of_inner(cx: @crate_ctxt, sp: span, t: ty::t)
T_nil() /* ...I guess? */
}
ty::ty_bool. { T_bool() }
ty::ty_int. { cx.int_type }
ty::ty_float. { cx.float_type }
ty::ty_uint. { cx.int_type }
ty::ty_machine(tm) {
alt tm {
ast::ty_i8. | ast::ty_u8. { T_i8() }
ast::ty_i16. | ast::ty_u16. { T_i16() }
ast::ty_i32. | ast::ty_u32. { T_i32() }
ast::ty_i64. | ast::ty_u64. { T_i64() }
ast::ty_f32. { T_f32() }
ast::ty_f64. { T_f64() }
}
}
ty::ty_char. { T_char() }
ty::ty_int(t) { T_int_ty(cx, t) }
ty::ty_uint(t) { T_uint_ty(cx, t) }
ty::ty_float(t) { T_float_ty(cx, t) }
ty::ty_str. { T_ptr(T_vec(cx, T_i8())) }
ty::ty_tag(did, _) { type_of_tag(cx, sp, did, t) }
ty::ty_box(mt) {
Expand Down Expand Up @@ -1516,23 +1505,10 @@ fn compare_scalar_types(cx: @block_ctxt, lhs: ValueRef, rhs: ValueRef,

alt ty::struct(bcx_tcx(cx), t) {
ty::ty_nil. { ret rslt(cx, f(nil_type)); }
ty::ty_bool. | ty::ty_uint. | ty::ty_ptr(_) | ty::ty_char. {
ret rslt(cx, f(unsigned_int));
}
ty::ty_int. { ret rslt(cx, f(signed_int)); }
ty::ty_float. { ret rslt(cx, f(floating_point)); }
ty::ty_machine(_) {
if ty::type_is_fp(bcx_tcx(cx), t) {
// Floating point machine types
ret rslt(cx, f(floating_point));
} else if ty::type_is_signed(bcx_tcx(cx), t) {
// Signed, integral machine types
ret rslt(cx, f(signed_int));
} else {
// Unsigned, integral machine types
ret rslt(cx, f(unsigned_int));
}
}
ty::ty_bool. | ty::ty_ptr(_) { ret rslt(cx, f(unsigned_int)); }
ty::ty_int(_) { ret rslt(cx, f(signed_int)); }
ty::ty_uint(_) { ret rslt(cx, f(unsigned_int)); }
ty::ty_float(_) { ret rslt(cx, f(floating_point)); }
ty::ty_type. {
ret rslt(trans_fail(cx, none,
"attempt to compare values of type type"),
Expand Down Expand Up @@ -2120,36 +2096,11 @@ fn store_temp_expr(cx: @block_ctxt, action: copy_action, dst: ValueRef,

fn trans_crate_lit(cx: @crate_ctxt, lit: ast::lit) -> ValueRef {
alt lit.node {
ast::lit_int(i) { ret C_int(cx, i); }
ast::lit_uint(u) { ret C_uint(cx, u); }
ast::lit_mach_int(tm, i) {
// FIXME: the entire handling of mach types falls apart
// if target int width is larger than host, at the moment;
// re-do the mach-int types using 'big' when that works.

let t = cx.int_type;
let s = True;
alt tm {
ast::ty_u8. { t = T_i8(); s = False; }
ast::ty_u16. { t = T_i16(); s = False; }
ast::ty_u32. { t = T_i32(); s = False; }
ast::ty_u64. { t = T_i64(); s = False; }
ast::ty_i8. { t = T_i8(); }
ast::ty_i16. { t = T_i16(); }
ast::ty_i32. { t = T_i32(); }
ast::ty_i64. { t = T_i64(); }
}
ret C_integral(t, i as u64, s);
}
ast::lit_float(fs) { ret C_float(cx, fs); }
ast::lit_mach_float(tm, s) {
let t = cx.float_type;
alt tm { ast::ty_f32. { t = T_f32(); } ast::ty_f64. { t = T_f64(); } }
ret C_floating(s, t);
}
ast::lit_char(c) { ret C_integral(T_char(), c as u64, False); }
ast::lit_bool(b) { ret C_bool(b); }
ast::lit_nil. { ret C_nil(); }
ast::lit_int(i, t) { C_integral(T_int_ty(cx, t), i as u64, True) }
ast::lit_uint(u, t) { C_integral(T_uint_ty(cx, t), u, False) }
ast::lit_float(fs, t) { C_floating(fs, T_float_ty(cx, t)) }
ast::lit_bool(b) { C_bool(b) }
ast::lit_nil. { C_nil() }
ast::lit_str(s) {
cx.sess.span_unimpl(lit.span, "unique string in this context");
}
Expand Down Expand Up @@ -4359,7 +4310,7 @@ fn trans_fail_expr(bcx: @block_ctxt, sp_opt: option::t<span>,
if ty::type_is_str(tcx, e_ty) {
let data = tvec::get_dataptr(
bcx, expr_res.val, type_of_or_i8(
bcx, ty::mk_mach(tcx, ast::ty_u8)));
bcx, ty::mk_mach_uint(tcx, ast::ty_u8)));
ret trans_fail_value(bcx, sp_opt, data);
} else if bcx.unreachable {
ret bcx;
Expand Down
35 changes: 29 additions & 6 deletions src/comp/middle/trans_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,35 @@ fn T_int(targ_cfg: @session::config) -> TypeRef {
};
}

fn T_int_ty(cx: @crate_ctxt, t: ast::int_ty) -> TypeRef {
alt t {
ast::ty_i. { cx.int_type }
ast::ty_char. { T_char() }
ast::ty_i8. { T_i8() }
ast::ty_i16. { T_i16() }
ast::ty_i32. { T_i32() }
ast::ty_i64. { T_i64() }
}
}

fn T_uint_ty(cx: @crate_ctxt, t: ast::uint_ty) -> TypeRef {
alt t {
ast::ty_u. { cx.int_type }
ast::ty_u8. { T_i8() }
ast::ty_u16. { T_i16() }
ast::ty_u32. { T_i32() }
ast::ty_u64. { T_i64() }
}
}

fn T_float_ty(cx: @crate_ctxt, t: ast::float_ty) -> TypeRef {
alt t {
ast::ty_f. { cx.float_type }
ast::ty_f32. { T_f32() }
ast::ty_f64. { T_f64() }
}
}

fn T_float(targ_cfg: @session::config) -> TypeRef {
ret alt targ_cfg.arch {
session::arch_x86. { T_f64() }
Expand Down Expand Up @@ -734,12 +763,6 @@ fn C_integral(t: TypeRef, u: u64, sign_extend: Bool) -> ValueRef {
ret llvm::LLVMRustConstInt(t, u_hi, u_lo, sign_extend);
}

fn C_float(cx: @crate_ctxt, s: str) -> ValueRef {
ret str::as_buf(s, {|buf|
llvm::LLVMConstRealOfString(cx.float_type, buf)
});
}

fn C_floating(s: str, t: TypeRef) -> ValueRef {
ret str::as_buf(s, {|buf| llvm::LLVMConstRealOfString(t, buf) });
}
Expand Down
Loading

0 comments on commit e3eca91

Please sign in to comment.