Skip to content

Incoming #2516

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

Merged
merged 6 commits into from
Jun 6, 2012
Merged
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
10 changes: 10 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[doc="Interfaces used for comparison."]

iface ord {
fn lt(&&other: self) -> bool;
}

iface eq {
fn eq(&&other: self) -> bool;
}

2 changes: 2 additions & 0 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export extfmt;
export tuple;
export to_str;
export dvec, dvec_iter;
export cmp;

// NDM seems to be necessary for resolve to work
export option_iter;
Expand Down Expand Up @@ -152,6 +153,7 @@ mod tuple;

// Ubiquitous-utility-type modules

mod cmp;
mod either;
mod iter;
mod logging;
Expand Down
14 changes: 14 additions & 0 deletions src/libcore/int-template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import T = inst::T;
import cmp::{eq, ord};

export min_value, max_value;
export min, max;
Expand All @@ -10,6 +11,7 @@ export range;
export compl;
export abs;
export parse_buf, from_str, to_str, to_str_bytes, str;
export ord, eq;

const min_value: T = -1 as T << (inst::bits - 1 as T);
const max_value: T = min_value - 1 as T;
Expand Down Expand Up @@ -108,6 +110,18 @@ fn to_str_bytes<U>(n: T, radix: uint, f: fn([u8]/&) -> U) -> U {
#[doc = "Convert to a string"]
fn str(i: T) -> str { ret to_str(i, 10u); }

impl ord of ord for T {
fn lt(&&other: T) -> bool {
ret self < other;
}
}

impl eq of eq for T {
fn eq(&&other: T) -> bool {
ret self == other;
}
}


// FIXME: Has alignment issues on windows and 32-bit linux
#[test]
Expand Down
14 changes: 14 additions & 0 deletions src/libcore/uint-template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import T = inst::T;
import cmp::{eq, ord};

export min_value, max_value;
export min, max;
Expand All @@ -10,6 +11,7 @@ export range;
export compl;
export to_str, to_str_bytes;
export from_str, from_str_radix, str, parse_buf;
export ord, eq;

const min_value: T = 0 as T;
const max_value: T = 0 as T - 1 as T;
Expand Down Expand Up @@ -49,6 +51,18 @@ pure fn compl(i: T) -> T {
max_value ^ i
}

impl ord of ord for T {
fn lt(&&other: T) -> bool {
ret self < other;
}
}

impl eq of eq for T {
fn eq(&&other: T) -> bool {
ret self == other;
}
}

#[doc = "
Parse a buffer of bytes

Expand Down
13 changes: 4 additions & 9 deletions src/libstd/sort.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[doc = "Sorting methods"];
import vec::len;
import int::{eq, ord};

export le;
export merge_sort;
Expand Down Expand Up @@ -141,7 +142,6 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
qsort3::<T>(compare_func_lt, compare_func_eq, arr, i, right);
}

// FIXME: This should take lt and eq types (#2348)
#[doc = "
Fancy quicksort. Sorts a mut vector in place.

Expand All @@ -152,22 +152,17 @@ According to these slides this is the algorithm of choice for

This is an unstable sort.
"]
fn quick_sort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
arr: [mut T]) {
fn quick_sort3<T: copy ord eq>(arr: [mut T]) {
if len::<T>(arr) == 0u { ret; }
qsort3::<T>(compare_func_lt, compare_func_eq, arr, 0,
qsort3::<T>({ |x, y| x.lt(y) }, { |x, y| x.eq(y) }, arr, 0,
(len::<T>(arr) as int) - 1);
}

#[cfg(test)]
mod test_qsort3 {
fn check_sort(v1: [mut int], v2: [mut int]) {
let len = vec::len::<int>(v1);
fn lt(&&a: int, &&b: int) -> bool { ret a < b; }
fn equal(&&a: int, &&b: int) -> bool { ret a == b; }
let f1 = lt;
let f2 = equal;
quick_sort3::<int>(f1, f2, v1);
quick_sort3::<int>(v1);
let mut i = 0u;
while i < len {
log(debug, v2[i]);
Expand Down
4 changes: 2 additions & 2 deletions src/rustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
bind middle::check_loop::check_crate(ty_cx, crate));
time(time_passes, "alt checking",
bind middle::check_alt::check_crate(ty_cx, crate));
let (last_use_map, spill_map) =
let last_use_map =
time(time_passes, "liveness checking",
bind middle::liveness::check_crate(ty_cx, method_map, crate));
time(time_passes, "typestate checking",
Expand All @@ -216,7 +216,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
let maps = {mutbl_map: mutbl_map, root_map: root_map,
copy_map: copy_map, last_use_map: last_use_map,
impl_map: impl_map, method_map: method_map,
vtable_map: vtable_map, spill_map: spill_map};
vtable_map: vtable_map};

let (llmod, link_meta) =
time(time_passes, "translation",
Expand Down
3 changes: 2 additions & 1 deletion src/rustc/driver/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import std::map::hashmap;
import getopts::{opt_present};
import rustc::driver::driver::*;
import syntax::codemap;
import rustc::driver::{diagnostic, session};
import syntax::diagnostic;
import rustc::driver::session;
import rustc::middle::lint;
import io::reader_util;

Expand Down
2 changes: 1 addition & 1 deletion src/rustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import syntax::{ast, ast_util};
import syntax::attr;
import middle::ty;
import syntax::ast_map;
import common::*;
import tydecode::{parse_ty_data, parse_def_id, parse_bounds_data,
parse_ident};
import syntax::print::pprust;
import cmd=cstore::crate_metadata;
import util::ppaux::ty_to_str;
import ebml::deserializer;
import syntax::diagnostic::span_handler;
import common::*;

export class_dtor;
export get_class_fields;
Expand Down
9 changes: 0 additions & 9 deletions src/rustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type maps = {
impl_map: middle::resolve::impl_map,
method_map: middle::typeck::method_map,
vtable_map: middle::typeck::vtable_map,
spill_map: middle::liveness::spill_map
};

type decode_ctxt = @{
Expand Down Expand Up @@ -839,12 +838,6 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
}
}

option::iter(maps.spill_map.find(id)) {|_m|
ebml_w.tag(c::tag_table_spill) {||
ebml_w.id(id);
}
}

option::iter(maps.last_use_map.find(id)) {|m|
ebml_w.tag(c::tag_table_last_use) {||
ebml_w.id(id);
Expand Down Expand Up @@ -953,8 +946,6 @@ fn decode_side_tables(xcx: extended_decode_ctxt,
dcx.maps.mutbl_map.insert(id, ());
} else if tag == (c::tag_table_copy as uint) {
dcx.maps.copy_map.insert(id, ());
} else if tag == (c::tag_table_spill as uint) {
dcx.maps.spill_map.insert(id, ());
} else {
let val_doc = entry_doc[c::tag_table_val];
let val_dsr = ebml::ebml_deserializer(val_doc);
Expand Down
61 changes: 9 additions & 52 deletions src/rustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import capture::{cap_move, cap_drop, cap_copy, cap_ref};

export check_crate;
export last_use_map;
export spill_map;

// Maps from an expr id to a list of variable ids for which this expr
// is the last use. Typically, the expr is a path and the node id is
Expand All @@ -66,13 +65,6 @@ export spill_map;
// list of closed over variables that can be moved into the closure.
type last_use_map = hashmap<node_id, @dvec<node_id>>;

// A set of variable ids which must be spilled (stored on the stack).
// We add in any variables or arguments where:
// (1) the variables are moved;
// (2) the address of the variable/argument is taken;
// or (3) we find a last use (as they may be moved).
type spill_map = hashmap<node_id, ()>;

enum variable = uint;
enum live_node = uint;

Expand All @@ -85,7 +77,7 @@ enum live_node_kind {

fn check_crate(tcx: ty::ctxt,
method_map: typeck::method_map,
crate: @crate) -> (last_use_map, spill_map) {
crate: @crate) -> last_use_map {
let visitor = visit::mk_vt(@{
visit_fn: visit_fn,
visit_local: visit_local,
Expand All @@ -94,12 +86,11 @@ fn check_crate(tcx: ty::ctxt,
});

let last_use_map = int_hash();
let spill_map = int_hash();
let initial_maps = @ir_maps(tcx, method_map,
last_use_map, spill_map);
last_use_map);
visit::visit_crate(*crate, initial_maps, visitor);
tcx.sess.abort_if_errors();
ret (last_use_map, spill_map);
ret last_use_map;
}

impl of to_str::to_str for live_node {
Expand Down Expand Up @@ -162,7 +153,6 @@ class ir_maps {
let tcx: ty::ctxt;
let method_map: typeck::method_map;
let last_use_map: last_use_map;
let spill_map: spill_map;

let mut num_live_nodes: uint;
let mut num_vars: uint;
Expand All @@ -174,11 +164,10 @@ class ir_maps {
let mut lnks: [live_node_kind];

new(tcx: ty::ctxt, method_map: typeck::method_map,
last_use_map: last_use_map, spill_map: spill_map) {
last_use_map: last_use_map) {
self.tcx = tcx;
self.method_map = method_map;
self.last_use_map = last_use_map;
self.spill_map = spill_map;

self.num_live_nodes = 0u;
self.num_vars = 0u;
Expand Down Expand Up @@ -264,17 +253,6 @@ class ir_maps {
self.lnks[*ln]
}

fn add_spill(var: variable) {
let vk = self.var_kinds[*var];
alt vk {
vk_local(id, _) | vk_arg(id, _, by_val) {
#debug["adding spill for %?", vk];
self.spill_map.insert(id, ());
}
vk_arg(*) | vk_field(_) | vk_self | vk_implicit_ret {}
}
}

fn add_last_use(expr_id: node_id, var: variable) {
let vk = self.var_kinds[*var];
#debug["Node %d is a last use of variable %?", expr_id, vk];
Expand Down Expand Up @@ -308,7 +286,7 @@ fn visit_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,

// swap in a new set of IR maps for this function body:
let fn_maps = @ir_maps(self.tcx, self.method_map,
self.last_use_map, self.spill_map);
self.last_use_map);

#debug["creating fn_maps: %x", ptr::addr_of(*fn_maps) as uint];

Expand Down Expand Up @@ -1407,11 +1385,7 @@ fn check_expr(expr: @expr, &&self: @liveness, vt: vt<@liveness>) {
vt.visit_expr(f, self, vt);
vec::iter2(args, targs) { |arg_expr, arg_ty|
alt ty::resolved_mode(self.tcx, arg_ty.mode) {
by_val | by_copy {
vt.visit_expr(arg_expr, self, vt);
}
by_ref | by_mutbl_ref {
self.spill_expr(arg_expr);
by_val | by_copy | by_ref | by_mutbl_ref{
vt.visit_expr(arg_expr, self, vt);
}
by_move {
Expand All @@ -1421,10 +1395,6 @@ fn check_expr(expr: @expr, &&self: @liveness, vt: vt<@liveness>) {
}
}

expr_addr_of(_, arg_expr) {
self.spill_expr(arg_expr);
}

// no correctness conditions related to liveness
expr_if_check(*) | expr_if(*) | expr_alt(*) |
expr_while(*) | expr_loop(*) |
Expand All @@ -1434,7 +1404,7 @@ fn check_expr(expr: @expr, &&self: @liveness, vt: vt<@liveness>) {
expr_assert(*) | expr_check(*) | expr_copy(*) |
expr_loop_body(*) | expr_cast(*) | expr_unary(*) | expr_fail(*) |
expr_ret(*) | expr_break | expr_cont | expr_lit(_) |
expr_block(*) | expr_swap(*) | expr_mac(*) {
expr_block(*) | expr_swap(*) | expr_mac(*) | expr_addr_of(*) {
visit::visit_expr(expr, self, vt);
}
}
Expand Down Expand Up @@ -1501,10 +1471,7 @@ impl check_methods for @liveness {
ln.to_str(), var.to_str()];

alt (*self).live_on_exit(ln, var) {
none {
// update spill map to include this variable, as it is moved:
(*self.ir).add_spill(var);
}
none { }
some(lnk) {
self.report_illegal_move(span, lnk, var);
}
Expand All @@ -1516,20 +1483,10 @@ impl check_methods for @liveness {
some(_) {}
none {
(*self.ir).add_last_use(expr.id, var);

// update spill map to include this variable, as it may be moved:
(*self.ir).add_spill(var);
}
}
}

fn spill_expr(expr: @expr) {
alt (*self).variable_from_path(expr) {
some(var) {(*self.ir).add_spill(var)}
none {}
}
}

fn check_move_from_expr(expr: @expr, vt: vt<@liveness>) {
#debug["check_move_from_expr(node %d: %s)",
expr.id, expr_to_str(expr)];
Expand Down Expand Up @@ -1775,4 +1732,4 @@ impl check_methods for @liveness {
}
}
}
}
}
Loading