2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-14 19:02:30 -07:00
|
|
|
import std::bitv;
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::vec;
|
|
|
|
import std::vec::len;
|
|
|
|
import std::vec::slice;
|
2011-06-01 18:10:10 -07:00
|
|
|
import front::ast::*;
|
2011-05-14 19:02:30 -07:00
|
|
|
import aux::fn_ctxt;
|
|
|
|
import aux::fn_info;
|
|
|
|
import aux::log_bitv;
|
2011-06-01 18:10:10 -07:00
|
|
|
import aux::num_constraints;
|
|
|
|
import aux::cinit;
|
|
|
|
import aux::cpred;
|
2011-06-13 18:10:33 -07:00
|
|
|
import aux::ninit;
|
|
|
|
import aux::npred;
|
2011-06-01 18:10:10 -07:00
|
|
|
import aux::pred_desc;
|
|
|
|
import aux::match_args;
|
2011-06-13 18:10:33 -07:00
|
|
|
import aux::constr_;
|
2011-05-18 15:43:05 -07:00
|
|
|
import tstate::aux::ann_to_ts_ann;
|
2011-05-14 19:02:30 -07:00
|
|
|
import tstate::ann::pre_and_post;
|
|
|
|
import tstate::ann::precond;
|
|
|
|
import tstate::ann::postcond;
|
|
|
|
import tstate::ann::prestate;
|
|
|
|
import tstate::ann::poststate;
|
|
|
|
import tstate::ann::relax_prestate;
|
|
|
|
import tstate::ann::pps_len;
|
|
|
|
import tstate::ann::true_precond;
|
|
|
|
import tstate::ann::empty_prestate;
|
|
|
|
import tstate::ann::difference;
|
|
|
|
import tstate::ann::union;
|
|
|
|
import tstate::ann::intersect;
|
|
|
|
import tstate::ann::clone;
|
|
|
|
import tstate::ann::set_in_postcond;
|
|
|
|
import tstate::ann::set_in_poststate;
|
2011-05-20 19:50:29 -07:00
|
|
|
import tstate::ann::clear_in_poststate;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-13 18:10:33 -07:00
|
|
|
fn bit_num(&fn_ctxt fcx, &constr_ c) -> uint {
|
|
|
|
assert (fcx.enclosing.constrs.contains_key(c.id));
|
|
|
|
auto res = fcx.enclosing.constrs.get(c.id);
|
|
|
|
alt (c.c) {
|
|
|
|
case (ninit(_)) {
|
2011-06-01 18:10:10 -07:00
|
|
|
alt (res) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (cinit(?n, _, _)) { ret n; }
|
2011-06-01 18:10:10 -07:00
|
|
|
case (_) {
|
|
|
|
fcx.ccx.tcx.sess.bug("bit_num: asked for init constraint,"
|
2011-06-15 11:19:50 -07:00
|
|
|
+ " found a pred constraint");
|
2011-06-01 18:10:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-13 18:10:33 -07:00
|
|
|
case (npred(_, ?args)) {
|
2011-06-01 18:10:10 -07:00
|
|
|
alt (res) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (cpred(_, ?descs)) { ret match_args(fcx, *descs, args); }
|
2011-06-01 18:10:10 -07:00
|
|
|
case (_) {
|
|
|
|
fcx.ccx.tcx.sess.bug("bit_num: asked for pred constraint,"
|
2011-06-15 11:19:50 -07:00
|
|
|
+ " found an init constraint");
|
2011-06-01 18:10:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-13 18:10:33 -07:00
|
|
|
fn promises(&fn_ctxt fcx, &poststate p, &constr_ c) -> bool {
|
|
|
|
ret bitv::get(p, bit_num(fcx, c));
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-14 19:02:30 -07:00
|
|
|
// Given a list of pres and posts for exprs e0 ... en,
|
|
|
|
// return the precondition for evaluating each expr in order.
|
|
|
|
// So, if e0's post is {x} and e1's pre is {x, y, z}, the entire
|
|
|
|
// precondition shouldn't include x.
|
2011-05-26 16:02:25 -07:00
|
|
|
fn seq_preconds(fn_ctxt fcx, vec[pre_and_post] pps) -> precond {
|
2011-06-15 11:19:50 -07:00
|
|
|
let uint sz = len[pre_and_post](pps);
|
|
|
|
let uint num_vars = num_constraints(fcx.enclosing);
|
|
|
|
if (sz >= 1u) {
|
|
|
|
auto first = pps.(0);
|
|
|
|
assert (pps_len(first) == num_vars);
|
|
|
|
let precond rest =
|
|
|
|
seq_preconds(fcx, slice[pre_and_post](pps, 1u, sz));
|
|
|
|
difference(rest, first.postcondition);
|
|
|
|
auto res = clone(first.precondition);
|
|
|
|
union(res, rest);
|
|
|
|
log "seq_preconds:";
|
|
|
|
log "first.postcondition =";
|
|
|
|
log_bitv(fcx, first.postcondition);
|
|
|
|
log "rest =";
|
|
|
|
log_bitv(fcx, rest);
|
|
|
|
log "returning";
|
|
|
|
log_bitv(fcx, res);
|
|
|
|
ret res;
|
|
|
|
} else { ret true_precond(num_vars); }
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-14 19:02:30 -07:00
|
|
|
/* works on either postconds or preconds
|
|
|
|
should probably rethink the whole type synonym situation */
|
|
|
|
fn union_postconds_go(&postcond first, &vec[postcond] rest) -> postcond {
|
2011-06-15 11:19:50 -07:00
|
|
|
auto sz = vec::len[postcond](rest);
|
|
|
|
if (sz > 0u) {
|
|
|
|
auto other = rest.(0);
|
|
|
|
union(first, other);
|
|
|
|
union_postconds_go(first,
|
|
|
|
slice[postcond](rest, 1u, len[postcond](rest)));
|
|
|
|
}
|
|
|
|
ret first;
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn union_postconds(uint nv, &vec[postcond] pcs) -> postcond {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (len[postcond](pcs) > 0u) {
|
|
|
|
ret union_postconds_go(bitv::clone(pcs.(0)), pcs);
|
|
|
|
} else { ret empty_prestate(nv); }
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-14 19:02:30 -07:00
|
|
|
/* Gee, maybe we could use foldl or something */
|
|
|
|
fn intersect_postconds_go(&postcond first, &vec[postcond] rest) -> postcond {
|
2011-06-15 11:19:50 -07:00
|
|
|
auto sz = vec::len[postcond](rest);
|
|
|
|
if (sz > 0u) {
|
|
|
|
auto other = rest.(0);
|
|
|
|
intersect(first, other);
|
|
|
|
intersect_postconds_go(first,
|
|
|
|
slice[postcond](rest, 1u,
|
|
|
|
len[postcond](rest)));
|
|
|
|
}
|
|
|
|
ret first;
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn intersect_postconds(&vec[postcond] pcs) -> postcond {
|
2011-06-15 11:19:50 -07:00
|
|
|
assert (len[postcond](pcs) > 0u);
|
|
|
|
ret intersect_postconds_go(bitv::clone(pcs.(0)), pcs);
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-13 18:10:33 -07:00
|
|
|
fn gen(&fn_ctxt fcx, &ann a, &constr_ c) -> bool {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret set_in_postcond(bit_num(fcx, c),
|
|
|
|
ann_to_ts_ann(fcx.ccx, a).conditions);
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-13 18:10:33 -07:00
|
|
|
fn declare_var(&fn_ctxt fcx, &constr_ c, prestate pre) -> prestate {
|
2011-05-14 19:02:30 -07:00
|
|
|
auto res = clone(pre);
|
2011-06-13 18:10:33 -07:00
|
|
|
relax_prestate(bit_num(fcx, c), res);
|
2011-05-14 19:02:30 -07:00
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2011-06-13 18:10:33 -07:00
|
|
|
fn gen_poststate(&fn_ctxt fcx, &ann a, &constr_ c) -> bool {
|
2011-06-15 11:19:50 -07:00
|
|
|
log "gen_poststate";
|
|
|
|
ret set_in_poststate(bit_num(fcx, c), ann_to_ts_ann(fcx.ccx, a).states);
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-13 18:10:33 -07:00
|
|
|
fn kill_poststate(&fn_ctxt fcx, &ann a, &constr_ c) -> bool {
|
2011-06-15 11:19:50 -07:00
|
|
|
log "kill_poststate";
|
|
|
|
ret clear_in_poststate(bit_num(fcx, c), ann_to_ts_ann(fcx.ccx, a).states);
|
2011-05-20 19:50:29 -07:00
|
|
|
}
|
2011-06-01 18:10:10 -07:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// 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:
|
|
|
|
//
|