2011-07-11 09:47:24 -05:00
|
|
|
import std::str;
|
2011-08-15 18:38:23 -05:00
|
|
|
import std::vec;
|
2011-07-11 09:47:24 -05:00
|
|
|
import std::option;
|
|
|
|
import option::some;
|
|
|
|
import option::none;
|
|
|
|
import std::map::hashmap;
|
|
|
|
|
|
|
|
import lib::llvm::llvm;
|
|
|
|
import lib::llvm::llvm::ValueRef;
|
|
|
|
import lib::llvm::llvm::TypeRef;
|
|
|
|
import lib::llvm::llvm::BasicBlockRef;
|
|
|
|
import trans::new_sub_block_ctxt;
|
|
|
|
import trans::new_scope_block_ctxt;
|
|
|
|
import trans::load_if_immediate;
|
|
|
|
import ty::pat_ty;
|
|
|
|
import syntax::ast;
|
2011-08-12 10:57:21 -05:00
|
|
|
import syntax::ast::dummy_sp;
|
2011-07-11 09:47:24 -05:00
|
|
|
import syntax::ast::def_id;
|
|
|
|
import syntax::codemap::span;
|
|
|
|
import util::common::lit_eq;
|
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
import trans_common::*;
|
|
|
|
|
2011-07-11 09:47:24 -05:00
|
|
|
// An option identifying a branch (either a literal or a tag variant)
|
|
|
|
tag opt {
|
|
|
|
lit(@ast::lit);
|
2011-07-27 07:19:39 -05:00
|
|
|
var(/* variant id */uint, /* variant dids */{tg: def_id, var: def_id});
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn opt_eq(a: &opt, b: &opt) -> bool {
|
|
|
|
alt a {
|
|
|
|
lit(la) {
|
|
|
|
ret alt b { lit(lb) { lit_eq(la, lb) } var(_, _) { false } };
|
|
|
|
}
|
|
|
|
var(ida, _) {
|
|
|
|
ret alt b { lit(_) { false } var(idb, _) { ida == idb } };
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn trans_opt(bcx: &@block_ctxt, o: &opt) -> result {
|
|
|
|
alt o {
|
|
|
|
lit(l) { ret trans::trans_lit(bcx, *l); }
|
|
|
|
var(id, _) { ret rslt(bcx, C_int(id as int)); }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn variant_opt(ccx: &@crate_ctxt, pat_id: ast::node_id) -> opt {
|
|
|
|
let vdef = ast::variant_def_ids(ccx.tcx.def_map.get(pat_id));
|
|
|
|
let variants = ty::tag_variants(ccx.tcx, vdef.tg);
|
|
|
|
let i = 0u;
|
2011-08-15 23:54:52 -05:00
|
|
|
for v: ty::variant_info in variants {
|
2011-07-27 07:19:39 -05:00
|
|
|
if vdef.var == v.id { ret var(i, vdef); }
|
2011-07-11 09:47:24 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
type bind_map = [{ident: ast::ident, val: ValueRef}];
|
2011-07-27 07:19:39 -05:00
|
|
|
type match_branch =
|
2011-08-04 18:20:09 -05:00
|
|
|
@{pats: [@ast::pat], body: BasicBlockRef, mutable bound: bind_map};
|
|
|
|
type match = [match_branch];
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn matches_always(p: &@ast::pat) -> bool {
|
2011-07-11 09:47:24 -05:00
|
|
|
ret alt p.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::pat_wild. { true }
|
|
|
|
ast::pat_bind(_) { true }
|
|
|
|
ast::pat_rec(_, _) { true }
|
2011-08-15 06:15:19 -05:00
|
|
|
ast::pat_tup(_) { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
_ { false }
|
|
|
|
};
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn bind_for_pat(p: &@ast::pat, br: &match_branch, val: ValueRef) {
|
2011-07-11 09:47:24 -05:00
|
|
|
alt p.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::pat_bind(name) { br.bound += ~[{ident: name, val: val}]; }
|
|
|
|
_ { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 09:15:18 -05:00
|
|
|
type enter_pat = fn(&@ast::pat) -> option::t<[@ast::pat]> ;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
fn enter_match(m: &match, col: uint, val: ValueRef, e: &enter_pat) -> match {
|
|
|
|
let result = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt e(br.pats.(col)) {
|
|
|
|
some(sub) {
|
|
|
|
let pats =
|
2011-08-15 18:38:23 -05:00
|
|
|
vec::slice(br.pats, 0u, col) + sub +
|
|
|
|
vec::slice(br.pats, col + 1u, vec::len(br.pats));
|
2011-07-27 07:19:39 -05:00
|
|
|
let new_br = @{pats: pats with *br};
|
|
|
|
result += ~[new_br];
|
|
|
|
bind_for_pat(br.pats.(col), new_br, val);
|
|
|
|
}
|
|
|
|
none. { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn enter_default(m: &match, col: uint, val: ValueRef) -> match {
|
2011-08-12 09:15:18 -05:00
|
|
|
fn e(p: &@ast::pat) -> option::t<[@ast::pat]> {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret if matches_always(p) { some(~[]) } else { none };
|
2011-07-13 04:33:46 -05:00
|
|
|
}
|
|
|
|
ret enter_match(m, col, val, e);
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn enter_opt(ccx: &@crate_ctxt, m: &match, opt: &opt, col: uint,
|
|
|
|
tag_size: uint, val: ValueRef) -> match {
|
2011-08-12 10:57:21 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-07-27 07:19:39 -05:00
|
|
|
fn e(ccx: &@crate_ctxt, dummy: &@ast::pat, opt: &opt, size: uint,
|
2011-08-12 09:15:18 -05:00
|
|
|
p: &@ast::pat) -> option::t<[@ast::pat]> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_tag(ctor, subpats) {
|
|
|
|
ret if opt_eq(variant_opt(ccx, p.id), opt) {
|
|
|
|
some(subpats)
|
|
|
|
} else { none };
|
|
|
|
}
|
|
|
|
ast::pat_lit(l) {
|
|
|
|
ret if opt_eq(lit(l), opt) { some(~[]) } else { none };
|
|
|
|
}
|
2011-08-15 18:38:23 -05:00
|
|
|
_ { ret some(vec::init_elt(dummy, size)); }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
ret enter_match(m, col, val, bind e(ccx, dummy, opt, tag_size, _));
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn enter_rec(m: &match, col: uint, fields: &[ast::ident], val: ValueRef) ->
|
2011-07-27 07:19:39 -05:00
|
|
|
match {
|
2011-08-15 15:33:12 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-08-04 18:20:09 -05:00
|
|
|
fn e(dummy: &@ast::pat, fields: &[ast::ident], p: &@ast::pat) ->
|
2011-08-12 09:15:18 -05:00
|
|
|
option::t<[@ast::pat]> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_rec(fpats, _) {
|
|
|
|
let pats = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for fname: ast::ident in fields {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pat = dummy;
|
2011-08-15 23:54:52 -05:00
|
|
|
for fpat: ast::field_pat in fpats {
|
2011-07-27 07:19:39 -05:00
|
|
|
if str::eq(fpat.ident, fname) { pat = fpat.pat; break; }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
pats += ~[pat];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
ret some(pats);
|
|
|
|
}
|
2011-08-15 18:38:23 -05:00
|
|
|
_ { ret some(vec::init_elt(dummy, vec::len(fields))); }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
ret enter_match(m, col, val, bind e(dummy, fields, _));
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-08-15 06:15:19 -05:00
|
|
|
fn enter_tup(m: &match, col: uint, val: ValueRef, n_elts: uint) -> match {
|
2011-08-15 15:33:12 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-08-15 06:15:19 -05:00
|
|
|
fn e(dummy: &@ast::pat, n_elts: uint, p: &@ast::pat)
|
2011-08-12 09:15:18 -05:00
|
|
|
-> option::t<[@ast::pat]> {
|
2011-08-15 06:15:19 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_tup(elts) { ret some(elts); }
|
2011-08-15 18:38:23 -05:00
|
|
|
_ { ret some(vec::init_elt(dummy, n_elts)); }
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret enter_match(m, col, val, bind e(dummy, n_elts, _));
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn enter_box(m: &match, col: uint, val: ValueRef) -> match {
|
2011-08-12 10:57:21 -05:00
|
|
|
let dummy = @{id: 0, node: ast::pat_wild, span: dummy_sp()};
|
2011-08-12 09:15:18 -05:00
|
|
|
fn e(dummy: &@ast::pat, p: &@ast::pat) -> option::t<[@ast::pat]> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt p.node {
|
|
|
|
ast::pat_box(sub) { ret some(~[sub]); }
|
|
|
|
_ { ret some(~[dummy]); }
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
ret enter_match(m, col, val, bind e(dummy, _));
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn get_options(ccx: &@crate_ctxt, m: &match, col: uint) -> [opt] {
|
|
|
|
fn add_to_set(set: &mutable [opt], val: &opt) {
|
2011-08-15 23:54:52 -05:00
|
|
|
for l: opt in set { if opt_eq(l, val) { ret; } }
|
2011-07-11 09:47:24 -05:00
|
|
|
set += ~[val];
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let found = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt br.pats.(col).node {
|
|
|
|
ast::pat_lit(l) { add_to_set(found, lit(l)); }
|
|
|
|
ast::pat_tag(_, _) {
|
|
|
|
add_to_set(found, variant_opt(ccx, br.pats.(col).id));
|
|
|
|
}
|
|
|
|
_ { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret found;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn extract_variant_args(bcx: @block_ctxt, pat_id: ast::node_id,
|
2011-07-28 05:01:45 -05:00
|
|
|
vdefs: &{tg: def_id, var: def_id}, val: ValueRef)
|
2011-08-04 18:20:09 -05:00
|
|
|
-> {vals: [ValueRef], bcx: @block_ctxt} {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ccx = bcx.fcx.lcx.ccx;
|
|
|
|
let ty_param_substs = ty::node_id_to_type_params(ccx.tcx, pat_id);
|
|
|
|
let blobptr = val;
|
|
|
|
let variants = ty::tag_variants(ccx.tcx, vdefs.tg);
|
|
|
|
let args = ~[];
|
|
|
|
let size =
|
2011-08-15 18:38:23 -05:00
|
|
|
vec::len(ty::tag_variant_with_id(ccx.tcx, vdefs.tg, vdefs.var).args);
|
|
|
|
if size > 0u && vec::len(variants) != 1u {
|
2011-07-27 07:19:39 -05:00
|
|
|
let tagptr =
|
|
|
|
bcx.build.PointerCast(val,
|
|
|
|
trans_common::T_opaque_tag_ptr(ccx.tn));
|
2011-07-11 09:47:24 -05:00
|
|
|
blobptr = bcx.build.GEP(tagptr, ~[C_int(0), C_int(1)]);
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
|
|
|
while i < size {
|
|
|
|
let r =
|
|
|
|
trans::GEP_tag(bcx, blobptr, vdefs.tg, vdefs.var, ty_param_substs,
|
|
|
|
i as int);
|
2011-07-11 09:47:24 -05:00
|
|
|
bcx = r.bcx;
|
|
|
|
args += ~[r.val];
|
|
|
|
i += 1u;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {vals: args, bcx: bcx};
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn collect_record_fields(m: &match, col: uint) -> [ast::ident] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let fields = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt br.pats.(col).node {
|
|
|
|
ast::pat_rec(fs, _) {
|
2011-08-15 23:54:52 -05:00
|
|
|
for f: ast::field_pat in fs {
|
2011-08-15 18:38:23 -05:00
|
|
|
if !vec::any(bind str::eq(f.ident, _), fields) {
|
2011-07-27 07:19:39 -05:00
|
|
|
fields += ~[f.ident];
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret fields;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn any_box_pat(m: &match, col: uint) -> bool {
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt br.pats.(col).node { ast::pat_box(_) { ret true; } _ { } }
|
2011-07-13 04:11:43 -05:00
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
2011-08-15 06:15:19 -05:00
|
|
|
fn any_tup_pat(m: &match, col: uint) -> bool {
|
|
|
|
for br: match_branch in m {
|
|
|
|
alt br.pats.(col).node { ast::pat_tup(_) { ret true; } _ { } }
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type exit_node = {bound: bind_map, from: BasicBlockRef, to: BasicBlockRef};
|
2011-08-02 05:57:27 -05:00
|
|
|
type mk_fail = fn() -> BasicBlockRef;
|
|
|
|
|
|
|
|
fn pick_col(m: &match) -> uint {
|
2011-08-15 18:38:23 -05:00
|
|
|
let scores = vec::init_elt_mut(0u, vec::len(m.(0).pats));
|
2011-08-02 05:57:27 -05:00
|
|
|
for br: match_branch in m {
|
|
|
|
let i = 0u;
|
|
|
|
for p: @ast::pat in br.pats {
|
|
|
|
alt p.node {
|
|
|
|
ast::pat_lit(_) | ast::pat_tag(_, _) { scores.(i) += 1u; }
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let max_score = 0u;
|
|
|
|
let best_col = 0u;
|
|
|
|
let i = 0u;
|
|
|
|
for score: uint in scores {
|
|
|
|
// Irrefutable columns always go first, they'd only be duplicated in
|
|
|
|
// the branches.
|
|
|
|
if score == 0u { ret i; }
|
|
|
|
// If no irrefutable ones are found, we pick the one with the biggest
|
|
|
|
// branching factor.
|
|
|
|
if score > max_score {
|
|
|
|
max_score = score;
|
|
|
|
best_col = i;
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret best_col;
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn compile_submatch(bcx: @block_ctxt, m: &match, vals: [ValueRef],
|
|
|
|
f: &mk_fail, exits: &mutable [exit_node]) {
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(m) == 0u { bcx.build.Br(f()); ret; }
|
|
|
|
if vec::len(m.(0).pats) == 0u {
|
2011-07-27 07:19:39 -05:00
|
|
|
exits += ~[{bound: m.(0).bound, from: bcx.llbb, to: m.(0).body}];
|
2011-07-11 09:47:24 -05:00
|
|
|
bcx.build.Br(m.(0).body);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-08-02 05:57:27 -05:00
|
|
|
let col = pick_col(m);
|
2011-07-27 07:19:39 -05:00
|
|
|
let val = vals.(col);
|
2011-08-15 18:38:23 -05:00
|
|
|
let vals_left = vec::slice(vals, 0u, col) +
|
|
|
|
vec::slice(vals, col + 1u, vec::len(vals));
|
2011-07-27 07:19:39 -05:00
|
|
|
let ccx = bcx.fcx.lcx.ccx;
|
|
|
|
let pat_id = 0;
|
2011-08-15 23:54:52 -05:00
|
|
|
for br: match_branch in m {
|
2011-07-11 09:47:24 -05:00
|
|
|
// Find a real id (we're adding placeholder wildcard patterns, but
|
|
|
|
// each column is guaranteed to have at least one real pattern)
|
2011-07-27 07:19:39 -05:00
|
|
|
if pat_id == 0 { pat_id = br.pats.(col).id; }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-13 04:11:43 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let rec_fields = collect_record_fields(m, col);
|
2011-07-11 09:47:24 -05:00
|
|
|
// Separate path for extracting and binding record fields
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(rec_fields) > 0u {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rec_ty = ty::node_id_to_monotype(ccx.tcx, pat_id);
|
|
|
|
let fields =
|
|
|
|
alt ty::struct(ccx.tcx, rec_ty) { ty::ty_rec(fields) { fields } };
|
|
|
|
let rec_vals = ~[];
|
2011-07-28 05:01:45 -05:00
|
|
|
for field_name: ast::ident in rec_fields {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ix: uint =
|
2011-08-12 10:57:21 -05:00
|
|
|
ty::field_idx(ccx.sess, dummy_sp(), field_name, fields);
|
2011-07-27 07:19:39 -05:00
|
|
|
let r = trans::GEP_tup_like(bcx, rec_ty, val, ~[0, ix as int]);
|
2011-07-11 09:47:24 -05:00
|
|
|
rec_vals += ~[r.val];
|
|
|
|
bcx = r.bcx;
|
|
|
|
}
|
2011-07-13 04:33:46 -05:00
|
|
|
compile_submatch(bcx, enter_rec(m, col, rec_fields, val),
|
2011-07-11 09:47:24 -05:00
|
|
|
rec_vals + vals_left, f, exits);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-08-15 06:15:19 -05:00
|
|
|
if any_tup_pat(m, col) {
|
|
|
|
let tup_ty = ty::node_id_to_monotype(ccx.tcx, pat_id);
|
|
|
|
let n_tup_elts = alt ty::struct(ccx.tcx, tup_ty) {
|
2011-08-15 18:38:23 -05:00
|
|
|
ty::ty_tup(elts) { vec::len(elts) }
|
2011-08-15 06:15:19 -05:00
|
|
|
};
|
|
|
|
let tup_vals = ~[], i = 0u;
|
|
|
|
while i < n_tup_elts {
|
|
|
|
let r = trans::GEP_tup_like(bcx, tup_ty, val, ~[0, i as int]);
|
|
|
|
tup_vals += ~[r.val];
|
|
|
|
bcx = r.bcx;
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
compile_submatch(bcx, enter_tup(m, col, val, n_tup_elts),
|
|
|
|
tup_vals + vals_left, f, exits);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-07-13 04:11:43 -05:00
|
|
|
// Unbox in case of a box field
|
2011-07-27 07:19:39 -05:00
|
|
|
if any_box_pat(m, col) {
|
|
|
|
let box = bcx.build.Load(val);
|
|
|
|
let unboxed =
|
|
|
|
bcx.build.InBoundsGEP(box,
|
|
|
|
~[C_int(0),
|
|
|
|
C_int(back::abi::box_rc_field_body)]);
|
|
|
|
compile_submatch(bcx, enter_box(m, col, val), ~[unboxed] + vals_left,
|
|
|
|
f, exits);
|
2011-07-13 04:11:43 -05:00
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2011-07-11 09:47:24 -05:00
|
|
|
// Decide what kind of branch we need
|
2011-07-27 07:19:39 -05:00
|
|
|
let opts = get_options(ccx, m, col);
|
2011-07-11 09:47:24 -05:00
|
|
|
tag branch_kind { no_branch; single; switch; compare; }
|
2011-07-27 07:19:39 -05:00
|
|
|
let kind = no_branch;
|
|
|
|
let test_val = val;
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(opts) > 0u {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt opts.(0) {
|
|
|
|
var(_, vdef) {
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(ty::tag_variants(ccx.tcx, vdef.tg)) == 1u {
|
2011-07-27 07:19:39 -05:00
|
|
|
kind = single;
|
|
|
|
} else {
|
2011-07-27 07:48:34 -05:00
|
|
|
let tagptr = bcx.build.PointerCast
|
|
|
|
(val, trans_common::T_opaque_tag_ptr(ccx.tn));
|
2011-07-27 07:19:39 -05:00
|
|
|
let discrimptr = bcx.build.GEP(tagptr, ~[C_int(0), C_int(0)]);
|
|
|
|
test_val = bcx.build.Load(discrimptr);
|
|
|
|
kind = switch;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
lit(l) {
|
|
|
|
test_val = bcx.build.Load(val);
|
|
|
|
kind = alt l.node { ast::lit_str(_, _) { compare } _ { switch } };
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let else_cx =
|
|
|
|
alt kind {
|
|
|
|
no_branch. | single. { bcx }
|
|
|
|
_ { new_sub_block_ctxt(bcx, "match_else") }
|
|
|
|
};
|
|
|
|
let sw =
|
|
|
|
if kind == switch {
|
2011-08-15 18:38:23 -05:00
|
|
|
bcx.build.Switch(test_val, else_cx.llbb, vec::len(opts))
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { C_int(0) }; // Placeholder for when not using a switch
|
|
|
|
|
|
|
|
// Compile subtrees for each option
|
2011-08-15 23:54:52 -05:00
|
|
|
for opt: opt in opts {
|
2011-07-27 07:19:39 -05:00
|
|
|
let opt_cx = new_sub_block_ctxt(bcx, "match_case");
|
|
|
|
alt kind {
|
|
|
|
single. { bcx.build.Br(opt_cx.llbb); }
|
|
|
|
switch. {
|
|
|
|
let r = trans_opt(bcx, opt);
|
|
|
|
bcx = r.bcx;
|
|
|
|
llvm::LLVMAddCase(sw, r.val, opt_cx.llbb);
|
|
|
|
}
|
|
|
|
compare. {
|
|
|
|
let r = trans_opt(bcx, opt);
|
|
|
|
bcx = r.bcx;
|
|
|
|
let t = ty::node_id_to_type(ccx.tcx, pat_id);
|
2011-08-02 19:21:28 -05:00
|
|
|
let eq = trans::trans_compare(bcx, ast::eq, test_val, t,
|
|
|
|
r.val, t);
|
2011-07-27 07:19:39 -05:00
|
|
|
bcx = new_sub_block_ctxt(bcx, "next");
|
|
|
|
eq.bcx.build.CondBr(eq.val, opt_cx.llbb, bcx.llbb);
|
|
|
|
}
|
|
|
|
_ { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let size = 0u;
|
|
|
|
let unpacked = ~[];
|
2011-07-11 09:47:24 -05:00
|
|
|
alt opt {
|
2011-07-27 07:19:39 -05:00
|
|
|
var(_, vdef) {
|
|
|
|
let args = extract_variant_args(opt_cx, pat_id, vdef, val);
|
2011-08-15 18:38:23 -05:00
|
|
|
size = vec::len(args.vals);
|
2011-07-27 07:19:39 -05:00
|
|
|
unpacked = args.vals;
|
|
|
|
opt_cx = args.bcx;
|
|
|
|
}
|
|
|
|
lit(_) { }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
compile_submatch(opt_cx, enter_opt(ccx, m, opt, col, size, val),
|
|
|
|
unpacked + vals_left, f, exits);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the fall-through case
|
2011-07-27 07:19:39 -05:00
|
|
|
if kind == compare { bcx.build.Br(else_cx.llbb); }
|
|
|
|
if kind != single {
|
|
|
|
compile_submatch(else_cx, enter_default(m, col, val), vals_left, f,
|
|
|
|
exits);
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-19 10:07:51 -05:00
|
|
|
// Returns false for unreachable blocks
|
2011-08-04 18:20:09 -05:00
|
|
|
fn make_phi_bindings(bcx: &@block_ctxt, map: &[exit_node],
|
2011-07-27 07:19:39 -05:00
|
|
|
ids: &ast::pat_id_map) -> bool {
|
2011-08-12 09:15:18 -05:00
|
|
|
fn assoc(key: str, list: &bind_map) -> option::t<ValueRef> {
|
2011-08-15 23:54:52 -05:00
|
|
|
for elt: {ident: ast::ident, val: ValueRef} in list {
|
2011-07-27 07:19:39 -05:00
|
|
|
if str::eq(elt.ident, key) { ret some(elt.val); }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-19 10:07:51 -05:00
|
|
|
ret none;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let our_block = bcx.llbb as uint;
|
|
|
|
let success = true;
|
2011-08-15 23:54:52 -05:00
|
|
|
for each item: @{key: ast::ident, val: ast::node_id} in ids.items() {
|
2011-07-27 07:19:39 -05:00
|
|
|
let llbbs = ~[];
|
|
|
|
let vals = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for ex: exit_node in map {
|
2011-07-27 07:19:39 -05:00
|
|
|
if ex.to as uint == our_block {
|
|
|
|
alt assoc(item.key, ex.bound) {
|
|
|
|
some(val) { llbbs += ~[ex.from]; vals += ~[val]; }
|
|
|
|
none. { }
|
2011-07-19 10:07:51 -05:00
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(vals) > 0u {
|
2011-07-27 07:19:39 -05:00
|
|
|
let phi = bcx.build.Phi(val_ty(vals.(0)), vals, llbbs);
|
2011-07-26 07:06:02 -05:00
|
|
|
bcx.fcx.lllocals.insert(item.val, phi);
|
2011-07-19 10:07:51 -05:00
|
|
|
} else { success = false; }
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
2011-07-19 10:07:51 -05:00
|
|
|
ret success;
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn trans_alt(cx: &@block_ctxt, expr: &@ast::expr, arms: &[ast::arm],
|
2011-08-18 03:01:39 -05:00
|
|
|
output: &trans::out_method) -> result {
|
2011-07-27 07:19:39 -05:00
|
|
|
let bodies = ~[];
|
|
|
|
let match: match = ~[];
|
2011-08-02 21:02:38 -05:00
|
|
|
let er = trans::trans_expr(cx, expr);
|
|
|
|
if (ty::type_is_bot(bcx_tcx(cx), ty::expr_ty(bcx_tcx(cx), expr))) {
|
|
|
|
// No need to generate code for alt,
|
|
|
|
// since the disc diverges.
|
|
|
|
if (!cx.build.is_terminated()) {
|
|
|
|
ret rslt(cx, cx.build.Unreachable());
|
|
|
|
}
|
|
|
|
else {
|
2011-08-04 18:07:26 -05:00
|
|
|
ret er;
|
2011-08-02 21:02:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-15 23:54:52 -05:00
|
|
|
for a: ast::arm in arms {
|
2011-07-27 07:19:39 -05:00
|
|
|
let body = new_scope_block_ctxt(cx, "case_body");
|
2011-07-11 09:47:24 -05:00
|
|
|
bodies += ~[body];
|
2011-08-15 23:54:52 -05:00
|
|
|
for p: @ast::pat in a.pats {
|
2011-07-27 07:19:39 -05:00
|
|
|
match += ~[@{pats: ~[p], body: body.llbb, mutable bound: ~[]}];
|
2011-07-11 09:47:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cached fail-on-fallthrough block
|
2011-07-27 07:19:39 -05:00
|
|
|
let fail_cx = @mutable none;
|
|
|
|
fn mk_fail(cx: &@block_ctxt, sp: &span,
|
2011-08-12 09:15:18 -05:00
|
|
|
done: @mutable option::t<BasicBlockRef>) -> BasicBlockRef {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt *done { some(bb) { ret bb; } _ { } }
|
|
|
|
let fail_cx = new_sub_block_ctxt(cx, "case_fallthrough");
|
2011-07-11 09:47:24 -05:00
|
|
|
trans::trans_fail(fail_cx, some(sp), "non-exhaustive match failure");
|
|
|
|
*done = some(fail_cx.llbb);
|
|
|
|
ret fail_cx.llbb;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let exit_map = ~[];
|
|
|
|
let t = trans::node_id_type(cx.fcx.lcx.ccx, expr.id);
|
|
|
|
let v = trans::spill_if_immediate(er.bcx, er.val, t);
|
2011-07-11 09:47:24 -05:00
|
|
|
compile_submatch(er.bcx, match, ~[v],
|
|
|
|
bind mk_fail(cx, expr.span, fail_cx), exit_map);
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
|
|
|
let arm_results = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for a: ast::arm in arms {
|
2011-07-27 07:19:39 -05:00
|
|
|
let body_cx = bodies.(i);
|
|
|
|
if make_phi_bindings(body_cx, exit_map, ast::pat_id_map(a.pats.(0))) {
|
2011-08-10 17:38:41 -05:00
|
|
|
let block_res = trans::trans_block(body_cx, a.body, output);
|
2011-07-19 10:07:51 -05:00
|
|
|
arm_results += ~[block_res];
|
|
|
|
} else { // Unreachable
|
|
|
|
arm_results += ~[rslt(body_cx, C_nil())];
|
|
|
|
}
|
2011-07-11 09:47:24 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret rslt(trans::join_branches(cx, arm_results), C_nil());
|
|
|
|
}
|
|
|
|
|
2011-07-28 05:01:45 -05:00
|
|
|
// Not alt-related, but similar to the pattern-munging code above
|
|
|
|
fn bind_irrefutable_pat(bcx: @block_ctxt, pat: &@ast::pat, val: ValueRef,
|
2011-08-12 09:15:18 -05:00
|
|
|
table: hashmap<ast::node_id, ValueRef>,
|
2011-08-15 17:35:40 -05:00
|
|
|
make_copy: bool)
|
2011-07-28 05:01:45 -05:00
|
|
|
-> @block_ctxt {
|
|
|
|
let ccx = bcx.fcx.lcx.ccx;
|
|
|
|
alt pat.node {
|
|
|
|
ast::pat_bind(_) {
|
2011-08-15 17:35:40 -05:00
|
|
|
if make_copy {
|
2011-08-02 04:44:00 -05:00
|
|
|
let ty = ty::node_id_to_monotype(ccx.tcx, pat.id);
|
|
|
|
let llty = trans::type_of(ccx, pat.span, ty);
|
|
|
|
let alloc = trans::alloca(bcx, llty);
|
|
|
|
bcx = trans::memmove_ty(bcx, alloc, val, ty).bcx;
|
|
|
|
let loaded = trans::load_if_immediate(bcx, alloc, ty);
|
|
|
|
bcx = trans::copy_ty(bcx, loaded, ty).bcx;
|
|
|
|
table.insert(pat.id, alloc);
|
|
|
|
trans_common::add_clean(bcx, alloc, ty);
|
|
|
|
} else {
|
|
|
|
table.insert(pat.id, val);
|
|
|
|
}
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
ast::pat_tag(_, sub) {
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(sub) == 0u { ret bcx; }
|
2011-07-28 05:01:45 -05:00
|
|
|
let vdefs = ast::variant_def_ids(ccx.tcx.def_map.get(pat.id));
|
|
|
|
let args = extract_variant_args(bcx, pat.id, vdefs, val);
|
|
|
|
let i = 0;
|
|
|
|
for argval: ValueRef in args.vals {
|
2011-08-15 17:35:40 -05:00
|
|
|
bcx = bind_irrefutable_pat(bcx, sub.(i), argval, table,
|
|
|
|
make_copy);
|
2011-07-28 05:01:45 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::pat_rec(fields, _) {
|
|
|
|
let rec_ty = ty::node_id_to_monotype(ccx.tcx, pat.id);
|
|
|
|
let rec_fields =
|
|
|
|
alt ty::struct(ccx.tcx, rec_ty) { ty::ty_rec(fields) { fields } };
|
|
|
|
for f: ast::field_pat in fields {
|
|
|
|
let ix: uint =
|
|
|
|
ty::field_idx(ccx.sess, pat.span, f.ident, rec_fields);
|
|
|
|
let r = trans::GEP_tup_like(bcx, rec_ty, val, ~[0, ix as int]);
|
2011-08-15 17:35:40 -05:00
|
|
|
bcx = bind_irrefutable_pat(r.bcx, f.pat, r.val, table, make_copy);
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 06:15:19 -05:00
|
|
|
ast::pat_tup(elems) {
|
|
|
|
let tup_ty = ty::node_id_to_monotype(ccx.tcx, pat.id);
|
|
|
|
let i = 0u;
|
|
|
|
for elem in elems {
|
|
|
|
let r = trans::GEP_tup_like(bcx, tup_ty, val, ~[0, i as int]);
|
2011-08-15 17:35:40 -05:00
|
|
|
bcx = bind_irrefutable_pat(r.bcx, elem, r.val, table, make_copy);
|
2011-08-15 06:15:19 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
2011-07-28 05:01:45 -05:00
|
|
|
ast::pat_box(inner) {
|
|
|
|
let box = bcx.build.Load(val);
|
|
|
|
let unboxed = bcx.build.InBoundsGEP
|
|
|
|
(box, ~[C_int(0), C_int(back::abi::box_rc_field_body)]);
|
2011-08-02 04:44:00 -05:00
|
|
|
bcx = bind_irrefutable_pat(bcx, inner, unboxed, table, true);
|
2011-07-28 05:01:45 -05:00
|
|
|
}
|
|
|
|
ast::pat_wild. | ast::pat_lit(_) {}
|
|
|
|
}
|
|
|
|
ret bcx;
|
|
|
|
}
|
|
|
|
|
2011-07-11 09:47:24 -05:00
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|