コード例 #1
0
ファイル: memoize.c プロジェクト: KarlHegbloom/guile
static SCM
lookup (SCM x, SCM env)
{
  int d = 0;
  for (; scm_is_pair (env); env = CDR (env), d++)
    {
      SCM link = CAR (env);
      if (env_link_is_flat (link))
        {
          int w;
          SCM vars;

          for (vars = env_link_vars (link), w = scm_ilength (vars) - 1;
               scm_is_pair (vars);
               vars = CDR (vars), w--)
            if (scm_is_eq (x, (CAAR (vars))))
              return make_pos (d, w);

          env_link_add_flat_var (link, x, lookup (x, CDR (env)));
          return make_pos (d, scm_ilength (env_link_vars (link)) - 1);
        }
      else
        {
          int w = try_lookup_rib (x, env_link_vars (link));
          if (w < 0)
            continue;
          return make_pos (d, w);
        }
    }
  abort ();
}
コード例 #2
0
repv
sys_get_mouse_pos(Lisp_Window *w)
{
    int x, y;
    /* XXX track mouse pointer position in gtk_jade.c.. */
    gtk_widget_get_pointer (GTK_WIDGET (w->w_Window), &x, &y);
    return make_pos((x - w->pixel_left) / w->font_width,
		    (y - w->pixel_top) / w->font_height);
}
コード例 #3
0
ファイル: iterparse.c プロジェクト: askielboe/astropy
/*
 * Handle the XML declaration so that we can determine its encoding.
 */
static void
xmlDecl(IterParser *self, const XML_Char *version,
        const XML_Char *encoding, int standalone)
{
    PyObject* tuple        = NULL;
    PyObject* xml_str      = NULL;
    PyObject* attrs        = NULL;
    PyObject* encoding_str = NULL;
    PyObject* version_str = NULL;

    if (self->queue_write_idx < self->queue_size) {
        tuple = PyTuple_New(4);
        if (tuple == NULL) {
            XML_StopParser(self->parser, 0);
            return;
        }

        Py_INCREF(Py_True);
        PyTuple_SET_ITEM(tuple, 0, Py_True);

        xml_str = PyUnicode_FromString("xml");
        PyTuple_SET_ITEM(tuple, 1, xml_str);

        attrs = PyDict_New();

        if (encoding) {
            encoding_str = PyUnicode_FromString(encoding);
        } else {
            encoding_str = PyUnicode_FromString("");
        }
        PyDict_SetItemString(attrs, "encoding", encoding_str);
        Py_DECREF(encoding_str);

        if (version) {
            version_str = PyUnicode_FromString(version);
        } else {
            version_str = PyUnicode_FromString("");
        }
        PyDict_SetItemString(attrs, "version", version_str);
        Py_DECREF(version_str);

        PyTuple_SET_ITEM(tuple, 2, attrs);

        self->last_line = (unsigned long)XML_GetCurrentLineNumber(
            self->parser);
        self->last_col = (unsigned long)XML_GetCurrentColumnNumber(
            self->parser);
        PyTuple_SET_ITEM(tuple, 3, make_pos(self));

        self->queue[self->queue_write_idx++] = tuple;
    } else {
        PyErr_SetString(
            PyExc_RuntimeError,
            "XML queue overflow in xmlDecl.  This most likely indicates an internal bug.");
    }
}
コード例 #4
0
ファイル: gpuirr.sse.cpp プロジェクト: scamicha/NBODY6--GPU
	Particle(
		const double _pos [3],
		const double _vel [3],
		const double _acc2[3],
		const double _jrk6[3],
		const double _mass,
		const double _time)
		: time(_time)
	{
		make_pos(_pos, &_mass);
		vel  = make_v4sf(_vel);
		acc2 = make_v4sf(_acc2);
		jrk6 = make_v4sf(_jrk6);
	}
コード例 #5
0
ファイル: irr.sse.cpp プロジェクト: nbodyx/Nbody6ppGPU
    Particle(
		const double _pos [3],
		const double _vel [3],
		const double _acc2[3],
		const double _jrk6[3],
		const double _mass,
		const double _time)
	{
		make_pos(_pos, &_mass);
		vel  = make_v4sf(_vel);
		acc2 = make_v4sf(_acc2);
		jrk6 = make_v4sf(_jrk6);
		time = (v2df){_time, _time};
	}
コード例 #6
0
ファイル: predicate_image_satqe.cpp プロジェクト: olivo/BP
void predicate_image_satqe(
    message_handlert &message_handler,
    const std::vector<exprt> &deref_curr_predicates,
    const std::vector<exprt> &deref_next_predicates,
    const std::list<exprt> &constraints,
    symex_target_equationt &equation,
    const namespacet &ns,
    abstract_transition_relationt &
    abstract_transition_relation)
{
  const std::set<unsigned> &from_predicates=
    abstract_transition_relation.from_predicates;

  const std::set<unsigned> &to_predicates=
    abstract_transition_relation.to_predicates;

  assert(to_predicates.size()!=0);

  // create SAT solver object
  satqe_satcheckt satqe;
  bv_pointerst solver(satqe);

  solver.unbounded_array=boolbvt::U_ALL;
  solver.set_message_handler(message_handler);

  // turn equation into CNF
  equation.convert(solver);

  for(std::set<unsigned>::const_iterator
      it=from_predicates.begin();
      it!=from_predicates.end();
      it++)
  {
    unsigned i=*it;

    literalt li=make_pos(ns, solver, deref_curr_predicates[i]);
    satqe.quantify(li);
  }

  for(std::set<unsigned>::const_iterator
      it=to_predicates.begin();
      it!=to_predicates.end();
      it++)
  {
    unsigned i=*it;

    literalt lo=make_pos(ns, solver, deref_next_predicates[i]);
    satqe.quantify(lo);
  }

  // we want cubes
  cube_sett cube_set;
  satqe.set_cube_set(cube_set);

  // solve it
  switch(solver.dec_solve())
  {
    case decision_proceduret::D_UNSATISFIABLE:
      // OK, this is what we expect
      break;

    default:
      throw "unexpected result from satqe.solve()";
  }

  message_handler.print(9, "Generated "+
      i2string(cube_set.no_insertions())+" cube(s)");

#if 0
  std::cout << cube_set;
#endif

  exprt constraint_cubes_disj = true_exprt();

  // convert the cubes into constraints
  for(cubest::star_mapt::const_iterator
      it=cube_set.star_map.begin();
      it!=cube_set.star_map.end();
      it++)
  {
    for(cubest::bitssett::const_iterator
        it2=it->second.begin();
        it2!=it->second.end();
        it2++)
    {
      exprt constraint_cube_conj;

      unsigned bit=0;
      unsigned i=0;

      /* Assume it->first[i] is true iff the i-th predicate is in the
         cube. Assume predicates are stored in it->first in the order of
         from_predicates followed by to_predicates. Scan it->first and
         from/to_predicates in parallel to get the cube. */

      for(std::set<unsigned>::const_iterator
          it3=from_predicates.begin();
          it3!=from_predicates.end();
          it3++)
      {
        unsigned id=*it3;

        if(!it->first[i])
        {
          constraint_cube_conj.operands().push_back(exprt());
          exprt &e=constraint_cube_conj.operands().back();
          e=exprt(ID_predicate_symbol, typet(ID_bool));
          e.set(ID_identifier, id);

          if(!(*it2)[bit]) 
            e.make_not();

          bit++;
#if 0
          std::cout << "C: " << from_expr(ns, "", e) << std::endl;
#endif
        }

        i++;
      }

      for(std::set<unsigned>::const_iterator
          it3=to_predicates.begin();
          it3!=to_predicates.end();
          it3++)
      {
        unsigned id=*it3;

        if(!it->first[i])
        {
          constraint_cube_conj.operands().push_back(exprt());
          exprt &e=constraint_cube_conj.operands().back();
          e=exprt(ID_predicate_next_symbol, typet(ID_bool));
          e.set(ID_identifier, id);

          if(!(*it2)[bit]) 
            e.make_not();

          bit++;
#if 0
          std::cout << "C: " << from_expr(ns, "", e) << std::endl;
#endif
        }

        i++;
      }

      assert(i==it->first.size());

      /* Convert the cube into a conjunct. */
      gen_and(constraint_cube_conj);

      /* Add the conjunct in disjunction to previous cubes. */
      if(constraint_cubes_disj.is_true())
        constraint_cubes_disj=constraint_cube_conj;
      else
        constraint_cubes_disj=gen_or(
            constraint_cubes_disj,
            constraint_cube_conj);      
    }
  }

  /* Add the cubes to the contraints of the abstract transition
     relation. Warning: we may want to remove old constrains. */

  abstract_transition_relation.constraints.push_back(constraint_cubes_disj);
}
コード例 #7
0
ファイル: refiner_wp.cpp プロジェクト: olivo/BP
bool refiner_wpt::refine_prefix(
  predicatest &predicates, 
  abstract_modelt &abstract_model,
  const fail_infot &fail_info)
{
  status("Refining set of predicates according to counterexample (WP)");

  reset_num_predicates_added();

  bool found_new=false;

  // keep track of the loops that we're in (may be nested)
  std::list<fail_infot::induction_infot> loops;
  exprt invariant;
  if(fail_info.use_invariants)
    status("Using recurrence predicates detected by loop detection.");
  
  print(10, "refiner_wpt::refine_prefix_async 1");
  
  print(10, "Inconsistent prefix:");

  for(abstract_counterexamplet::stepst::const_reverse_iterator 
      r_it=fail_info.steps.rbegin();
      r_it!=fail_info.steps.rend();
      r_it++)
  {
    std::stringstream str;

    abstract_programt::targett abstract_pc=r_it->pc;

    goto_programt::const_targett concrete_pc=
      abstract_pc->code.concrete_pc;

    if(concrete_pc->is_goto())
      str << "GUARD: " << (r_it->branch_taken?"(":"!(")
                << from_expr(concrete_model.ns, "", concrete_pc->guard) << ")";
    else if(concrete_pc->is_assert())
      str << "ASSERT: "
                << from_expr(concrete_model.ns, "", concrete_pc->guard);
    else if(concrete_pc->is_location())
      str << "LOC" << std::endl;
    else if(concrete_pc->is_other() || concrete_pc->is_assign() || concrete_pc->is_decl())
      str << from_expr(concrete_model.ns, "", concrete_pc->code);
    else
    {
      str << concrete_pc->type;
    }

    str << "  // " << (concrete_pc->location);

    str << std::endl << "**********";

    print(10, str.str());
  }

  
  {
    // get the constraint causing the failure

    exprt predicate=fail_info.guard;

    #ifdef DEBUG
    std::cout << "P start0: " 
              << from_expr(concrete_model.ns, "", predicate) << std::endl;
    #endif

    simplify(predicate, concrete_model.ns);

    abstract_counterexamplet::stepst::const_iterator 
      it=--fail_info.steps.end();

    // there must be at least two steps, or it's odd
    assert(it!=fail_info.steps.begin());

    {
      abstract_programt::targett abstract_pc=it->pc;

      #ifdef DEBUG
      std::cout << "P start1: " 
                << from_expr(concrete_model.ns, "", predicate) << std::endl;
      #endif

      add_predicates(
        abstract_pc, predicates, predicate, found_new, FROM);
    }
      
    // now do the WPs
    goto_symex_statet renaming_state;
    renaming_state.source.thread_nr=it->thread_nr;
    renaming_state.rename(predicate, concrete_model.ns, goto_symex_statet::L0);

    for(it--; // skip last instruction
        it!=fail_info.steps.begin();
        it--)
    {
      #ifdef DEBUG
      std::cout << "refiner_wpt::refine_prefix_async 2\n";
      #endif

      // handle loops 
      if(fail_info.use_invariants)
      {
        if(it->is_loop_begin())
        {
          loops.pop_back(); // pop induction_info if we leave loop
          
#ifdef DEBUG
          std::cout << "INV: "
                    << from_expr(concrete_model.ns, "", invariant) << std::endl;
#endif           
          
          exprt wp(ID_and, typet(ID_bool));
          wp.operands().resize(2);
          wp.op0().swap(invariant);
          wp.op1().swap(predicate);
          predicate.swap(wp);
        }
        else if (it->is_loop_end())
        {
          push_induction_info(fail_info, it, loops);
          invariant.make_true();
        }
      }

      if(!it->is_state())
        continue;

      if(predicate.is_true() && found_new)
      {
        // ok, refuted it, done
        break;
      }
        
      // add the predicate

      goto_programt::const_targett concrete_pc=
        it->pc->code.concrete_pc;
        
      abstract_programt::targett abstract_pc=it->pc;
        
      #ifdef DEBUG
      std::cout << from_expr(concrete_model.ns, "", predicate) << std::endl;
      #endif
      
      exprt no_tid_predicate=predicate;
      renaming_state.get_original_name(no_tid_predicate);
      add_predicates(abstract_pc, predicates, no_tid_predicate, found_new, TO);

      // skip irrelevant instructions
      if(!it->relevant) continue;

      exprt pred_bak=predicate;
#ifdef DEBUG
      goto_programt tmp;
      tmp.output_instruction(concrete_model.ns, "", std::cerr, concrete_pc);
#endif

      // compute weakest precondition
      switch(it->pc->type)
      {
      case ASSUME:
        // we only do this for assumptions
        // if we haven't found a new predicate so far
        if(1/*!found_new*/)
        {
          exprt tid_guard=concrete_pc->guard;
          renaming_state.source.thread_nr=it->thread_nr;
          renaming_state.rename(tid_guard, concrete_model.ns, goto_symex_statet::L0);
          predicate=implies_exprt(tid_guard, predicate);
          simplify(predicate, concrete_model.ns);
        }
        break;

      case GOTO:
        {
          exprt tid_guard=concrete_pc->guard;
          if(!it->branch_taken) tid_guard.make_not();
          renaming_state.source.thread_nr=it->thread_nr;
          renaming_state.rename(tid_guard, concrete_model.ns, goto_symex_statet::L0);
          predicate=implies_exprt(tid_guard, predicate);
          simplify(predicate, concrete_model.ns);
        }
        break;

      case OTHER:
    	/* Ignore if user-specified predicate, otherwise treat like assign */
    	if(it->pc->code.concrete_pc->code.get_statement()==ID_user_specified_predicate || it->pc->code.concrete_pc->code.get_statement()==ID_user_specified_parameter_predicates || it->pc->code.concrete_pc->code.get_statement()==ID_user_specified_return_predicates)
    		break;
      case DECL:
      case ASSIGN:
        #ifdef DEBUG
        std::cout << "OTHER/ASSIGN/DECL\n";
        #endif

        {
          codet tid_tmp_code;
          if(!fail_info.use_invariants ||
             !get_instruction(concrete_pc, loops, tid_tmp_code, invariant))
            tid_tmp_code=to_code(concrete_pc->code);

#ifdef DEBUG
          std::cout << "A P before: " << from_expr(concrete_model.ns, "", predicate) << std::endl;
          std::cout << "Code:     " << from_expr(concrete_model.ns, "", tid_tmp_code) << std::endl;
#endif
          
          // compute weakest precondition
          if(tid_tmp_code.get_statement()==ID_assign)
            approximate_nondet(to_code_assign(tid_tmp_code).rhs());
          renaming_state.source.thread_nr=it->thread_nr;
          renaming_state.rename(tid_tmp_code, concrete_model.ns, goto_symex_statet::L0);
          exprt predicate_wp=wp(tid_tmp_code, predicate, concrete_model.ns);
      
          simplify(predicate_wp, concrete_model.ns);
          predicate=predicate_wp;

#ifdef DEBUG
          std::cout << "A P after:  " << from_expr(concrete_model.ns, "", predicate) << std::endl;
#endif
        }
        break;

      default:
        // ignore
        break;
      }

#ifdef DEBUG
          std::cout << "B P to-check:  " << from_expr(concrete_model.ns, "", predicate) << std::endl;
#endif
          
      if(pred_bak != predicate)
      {
        satcheckt satcheck;
        bv_pointerst solver(concrete_model.ns, satcheck);
        solver.unbounded_array=boolbvt::U_NONE;
        literalt li=make_pos(concrete_model.ns, solver, predicate);
        satcheck.set_assumptions(bvt(1, li));
        propt::resultt result=satcheck.prop_solve();
        assert(propt::P_SATISFIABLE==result || propt::P_UNSATISFIABLE==result);
        if(propt::P_UNSATISFIABLE==result)
          predicate.make_false();
        else
        {
          satcheck.set_assumptions(bvt(1, li.negation()));
          propt::resultt result=satcheck.prop_solve();
          assert(propt::P_SATISFIABLE==result || propt::P_UNSATISFIABLE==result);
          if(propt::P_UNSATISFIABLE==result)
          {
            predicate.make_true();
            if(it->pc->type==ASSIGN)
            {
              const codet &code=concrete_pc->code;
              if(code.get_statement()==ID_assign)
              {
                equal_exprt pred_new(to_code_assign(code).lhs(),
                    to_code_assign(code).rhs());
                simplify(pred_new, concrete_model.ns);
#ifdef DEBUG
                std::cout << "Adding new predicate as we arrived at TRUE: "
                  << from_expr(concrete_model.ns, "", pred_new) << std::endl;
#endif
                no_tid_predicate=pred_new;
                renaming_state.get_original_name(no_tid_predicate);
                add_predicates(abstract_pc, predicates, no_tid_predicate, found_new, FROM);
              }
            }
            else if(it->pc->type==ASSUME || it->pc->type==GOTO)
            {
              exprt pred_new=concrete_pc->guard;
              simplify(pred_new, concrete_model.ns);
#ifdef DEBUG
              std::cout << "Adding new predicate as we arrived at TRUE: "
                << from_expr(concrete_model.ns, "", pred_new) << std::endl;
#endif
              no_tid_predicate=pred_new;
              renaming_state.get_original_name(no_tid_predicate);
              add_predicates(abstract_pc, predicates, no_tid_predicate, found_new, FROM);
            }
          }
        }
      }
      
      #ifdef DEBUG
      std::cout << "B P after:   " << from_expr(concrete_model.ns, "", predicate) << std::endl;
      #endif

      no_tid_predicate=predicate;
      renaming_state.get_original_name(no_tid_predicate);
      add_predicates(abstract_pc, predicates, no_tid_predicate, found_new, FROM);
    }

    if(!predicate.is_true() && fail_info.warn_on_failure)
    {
      warning("Failed to refute spurious trace with WPs (got "+
              from_expr(concrete_model.ns, "", predicate)+")");
    }
  }

  if(found_new && fail_info.use_invariants)
  {
    add_induction_predicates(
      fail_info,
      abstract_model,
      predicates);
  }
  
  // make sure we have progress
  return !found_new;
}
コード例 #8
0
ファイル: iterparse.c プロジェクト: Juanlu001/astropy
/*
 * Handle the expat endElement event.
 */
static void
endElement(IterParser *self, const XML_Char *name)
{
    PyObject*       pyname     = NULL;
    PyObject*       tuple      = NULL;
    PyObject*       pytext     = NULL;
    const XML_Char* name_start = NULL;
    XML_Char*       end;
    PyObject*       pos        = NULL;

    /* If we've already had an error in a previous call, don't make
       things worse. */
    if (PyErr_Occurred() != NULL) {
        XML_StopParser(self->parser, 0);
        return;
    }

    /* Don't overflow the queue -- in practice this should *never* happen */
    if (self->queue_write_idx < self->queue_size) {
        tuple = PyTuple_New(4);
        if (tuple == NULL) {
            goto fail;
        }

        Py_INCREF(Py_False);
        PyTuple_SET_ITEM(tuple, 0, Py_False);

        /* This is an egregious but effective optimization.  Since by
           far the most frequently occurring element name in a large
           VOTABLE file is TD, we explicitly check for it here with
           integer comparison to avoid the lookup in the interned
           string table in PyString_InternFromString, and return a
           singleton string for "TD" */
        if ((*(int*)name & TD_AS_INT_MASK) == TD_AS_INT) {
            Py_INCREF(self->td_singleton);
            PyTuple_SetItem(tuple, 1, self->td_singleton);
        } else {
            name_start = remove_namespace(name);

            pyname = PyUnicode_FromString(name_start);
            if (pyname == NULL) {
                goto fail;
            }
            PyTuple_SetItem(tuple, 1, pyname);
            pyname = NULL;
        }

        /* Cut whitespace off the end of the string */
        end = self->text + self->text_size - 1;
        while (end >= self->text && IS_WHITESPACE(*end)) {
            --end;
            --self->text_size;
        }

        pytext = PyUnicode_FromStringAndSize(self->text, self->text_size);
        if (pytext == NULL) {
            goto fail;
        }
        PyTuple_SetItem(tuple, 2, pytext);
        pytext = NULL;

        pos = make_pos(self);
        if (pos == NULL) {
            goto fail;
        }
        PyTuple_SetItem(tuple, 3, pos);
        pos = NULL;

        self->keep_text = 0;

        self->queue[self->queue_write_idx++] = tuple;
    } else {
        PyErr_SetString(
            PyExc_RuntimeError,
            "XML queue overflow in endElement.  This most likely indicates an internal bug.");
        goto fail;
    }

    return;

 fail:
    Py_XDECREF(tuple);
    XML_StopParser(self->parser, 0);
}
コード例 #9
0
ファイル: iterparse.c プロジェクト: Juanlu001/astropy
/*
 * Handle the expat startElement event.
 */
static void
startElement(IterParser *self, const XML_Char *name, const XML_Char **atts)
{
    PyObject*        pyname = NULL;
    PyObject*        pyatts = NULL;
    const XML_Char** att_ptr = atts;
    const XML_Char*  name_start = NULL;
    PyObject*        tuple = NULL;
    PyObject*        key = NULL;
    PyObject*        val = NULL;
    PyObject*        pos = NULL;

    /* If we've already had an error in a previous call, don't make
       things worse. */
    if (PyErr_Occurred() != NULL) {
        XML_StopParser(self->parser, 0);
        return;
    }

    /* Don't overflow the queue -- in practice this should *never* happen */
    if (self->queue_write_idx < self->queue_size) {
        tuple = PyTuple_New(4);
        if (tuple == NULL) {
            goto fail;
        }

        Py_INCREF(Py_True);
        PyTuple_SET_ITEM(tuple, 0, Py_True);

        /* This is an egregious but effective optimization.  Since by
           far the most frequently occurring element name in a large
           VOTABLE file is TD, we explicitly check for it here with
           integer comparison to avoid the lookup in the interned
           string table in PyString_InternFromString, and return a
           singleton string for "TD" */
        if ((*(int*)name & TD_AS_INT_MASK) == TD_AS_INT) {
            Py_INCREF(self->td_singleton);
            PyTuple_SetItem(tuple, 1, self->td_singleton);
        } else {
            name_start = remove_namespace(name);

            pyname = PyUnicode_FromString(name_start);
            if (pyname == NULL) {
                goto fail;
            }
            PyTuple_SetItem(tuple, 1, pyname);
            pyname = NULL;
        }

        if (*att_ptr) {
            pyatts = PyDict_New();
            if (pyatts == NULL) {
                goto fail;
            }
            do {
                if (*(*(att_ptr + 1)) != 0) {
                    key = PyUnicode_FromString(*att_ptr);
                    if (key == NULL) {
                        goto fail;
                    }
                    val = PyUnicode_FromString(*(att_ptr + 1));
                    if (val == NULL) {
                        Py_DECREF(key);
                        goto fail;
                    }
                    if (PyDict_SetItem(pyatts, key, val)) {
                        Py_DECREF(key);
                        Py_DECREF(val);
                        goto fail;
                    }
                    Py_DECREF(key);
                    Py_DECREF(val);
                    key = val = NULL;
                }
                att_ptr += 2;
            } while (*att_ptr);
        } else {
            Py_INCREF(self->dict_singleton);
            pyatts = self->dict_singleton;
        }

        PyTuple_SetItem(tuple, 2, pyatts);
        pyatts = NULL;

        self->last_line = (unsigned long)XML_GetCurrentLineNumber(
            self->parser);
        self->last_col = (unsigned long)XML_GetCurrentColumnNumber(
            self->parser);

        pos = make_pos(self);
        if (pos == NULL) {
            goto fail;
        }
        PyTuple_SetItem(tuple, 3, pos);
        pos = NULL;

        text_clear(self);

        self->keep_text = 1;

        self->queue[self->queue_write_idx++] = tuple;
    } else {
        PyErr_SetString(
            PyExc_RuntimeError,
            "XML queue overflow in startElement.  This most likely indicates an internal bug.");
        goto fail;
    }

    return;

 fail:
    Py_XDECREF(tuple);
    Py_XDECREF(pyatts);
    XML_StopParser(self->parser, 0);
}
コード例 #10
0
ファイル: transition_refiner.cpp プロジェクト: olivo/BP
bool transition_refinert::check_guarded_transition(
  const predicatest &predicates,
  const abstract_stept &abstract_state_from,
  unsigned passive_id,
  bool &inconsistent_initial_state)
{
  // get the concrete basic block
  const goto_programt::instructiont &c_instruction=
    *abstract_state_from.pc->code.concrete_pc;

  if (!c_instruction.is_goto() &&
      !c_instruction.is_assume())
    return false; // whatever

  // this is the original guard
  exprt guard=c_instruction.guard; 

  if(guard.is_true()) // boring
    return false;

  // we might need to negate it if the branch was not taken
  if (c_instruction.is_goto() && 
      !abstract_state_from.branch_taken)
    guard.make_not();

  abstract_transition_relationt &abstract_transition_relation=
    abstract_state_from.pc->code.get_transition_relation();

  #ifdef SATCHECK_MINISAT2
  satcheck_minisat_no_simplifiert satcheck;
  #else
  satcheckt satcheck;
  #endif
  bv_pointerst solver(concrete_model.ns, satcheck);
  solver.unbounded_array=boolbvt::U_NONE;

  // Note that we take "thread_nr" from "abstract_state_from", not from "abstract_state_to", as the "from" state determines which thread is executing
  const unsigned active_id=abstract_state_from.thread_nr;
  const unsigned num_threads=abstract_state_from.thread_states.size();

  std::vector<predicatest> active_passive_preds(num_threads, predicatest());

  for(unsigned int t=0; t < num_threads; ++t)
  {
    if(active_id!=t &&
        passive_id < num_threads &&
        t!=passive_id)
      continue;

    for(unsigned int i = 0; i < predicates.size(); i++)
    {
      active_passive_preds[t].lookup(active_id==t?
          predicates[i] :
          predicatest::make_expr_passive(predicates[i], concrete_model.ns, t));
    }
    assert(active_passive_preds[t].size() == predicates.size());
  }

  std::vector<std::vector<literalt> >
    predicate_variables_from(num_threads, std::vector<literalt>(predicates.size(), literalt()));
    
  bvt assumptions;

  std::vector<abstract_stept::thread_to_predicate_valuest::const_iterator>
    from_predicates(num_threads, abstract_state_from.thread_states.end());
  for(unsigned int t=0; t < num_threads; ++t)
  {
    from_predicates[t]=abstract_state_from.thread_states.find(t);
    assert(abstract_state_from.thread_states.end() != from_predicates[t]);
  }

  // we use all predicates in order to also find constraints over invalid
  // from states
  for(unsigned i=0; i < predicates.size(); ++i)
  {
    for(unsigned int t=0; t < num_threads; ++t)
    {
      if(active_id!=t &&
          passive_id < num_threads &&
          t!=passive_id)
        continue;
      // not sure whether we really want the following check
      if(active_id!=t &&
        active_passive_preds[t][i]==predicates[i])
        continue;

      literalt li=make_pos(concrete_model.ns, solver, active_passive_preds[t][i]);
      predicate_variables_from[t][i]=li;

      assumptions.push_back(li.cond_negation(
            !from_predicates[t]->second[i]));

      #ifdef DEBUG
      const std::string predname=
        (active_id==t?"P#":"PP"+i2string(t)+"#")+i2string(i);
      std::cerr
        << "G-F: " << predname << ": "
        << (from_predicates[t]->second[i]?"":"!") << "("
        << from_expr(concrete_model.ns, "", active_passive_preds[t][i]) << ")" << std::endl;
      #endif
    }
  }
  
  satcheck.set_assumptions(assumptions);

  if(!is_satisfiable(solver))
  {
    if(passive_id >= num_threads)
    {
      const std::string opt="Invalid states requiring more than 1 passive thread";
      assert(stats.find(opt)!=stats.end());
      ++(stats[opt].val);
    }
    inconsistent_initial_state = true;
    print(9, "Guarded transition spurious due to invalid abstract state");
    return false; // this has to be fixed in the respective assignment
  }

  // now add the guard
  solver.set_to_true(guard);

  // solve it incrementally
  if(is_satisfiable(solver))
  {
    print(9, "Transition is OK");
    #ifdef DEBUG
    std::cout << "********\n";
    solver.print_assignment(std::cout);
    std::cout << "********\n";
    #endif
    return false; // ok
  }

  if(passive_id >= num_threads)
  {
    const std::string opt="Spurious guard transitions requiring more than 1 passive thread";
    assert(stats.find(opt)!=stats.end());
    ++(stats[opt].val);
    return false; // can't do anything
  }

  print(9, "Guarded transition is spurious, refining");

  exprt condition;

  for(unsigned i=0; i < predicates.size(); ++i)
  {
    if(satcheck.is_in_conflict(predicate_variables_from[active_id][i]))
    {
      condition.operands().push_back(exprt());
      exprt &e=condition.operands().back();
      e=exprt(ID_predicate_symbol, bool_typet());
      e.set(ID_identifier, i);
      if(!from_predicates[active_id]->second[i]) e.make_not();
      #ifdef DEBUG
      std::cout << "G-C: " << from_expr(concrete_model.ns, "", e) << std::endl;
      #endif
    }

    if(passive_id!=active_id &&
        satcheck.is_in_conflict(predicate_variables_from[passive_id][i]))
    {
      condition.operands().push_back(exprt());
      exprt &e=condition.operands().back();
      e=exprt(ID_predicate_passive_symbol, bool_typet());
      e.set(ID_identifier, i);
      if(!from_predicates[passive_id]->second[i]) e.make_not();
#ifdef DEBUG
      std::cout << "G-C-F: " << from_expr(concrete_model.ns, "", e) << std::endl;
#endif
    }
  }

  gen_and(condition);

  if(c_instruction.is_goto())
  {  
    bool neg=abstract_state_from.branch_taken;
    constrain_goto_transition(abstract_transition_relation, condition, neg);
  }
  else
  {
    assert(c_instruction.is_assume());
    constrain_assume_transition(abstract_transition_relation, condition);
  }

  // spurious
  return true;
}
コード例 #11
0
ファイル: transition_refiner.cpp プロジェクト: olivo/BP
bool transition_refinert::check_assignment_transition(
  const predicatest &predicates,
  const abstract_stept &abstract_state_from,
  const abstract_stept &abstract_state_to,
  unsigned passive_id)
{
  // Note that we take "thread_nr" from "abstract_state_from", not from "abstract_state_to", as the "from" state determines which thread is executing
  const unsigned active_id=abstract_state_from.thread_nr;
  const unsigned num_threads=abstract_state_from.thread_states.size();

  std::vector<predicatest> active_passive_preds(num_threads, predicatest());
  std::vector<std::vector<exprt> > predicates_wp(num_threads,
      std::vector<exprt>());
  std::list<exprt> constraints;

  for(unsigned int t=0; t < num_threads; ++t)
  {
    if(active_id!=t &&
        passive_id < num_threads &&
        t!=passive_id)
      continue;

    for(unsigned int i = 0; i < predicates.size(); i++)
    {
      active_passive_preds[t].lookup(active_id==t?
          predicates[i] :
          predicatest::make_expr_passive(predicates[i], concrete_model.ns, t));
    }
    assert(active_passive_preds[t].size() == predicates.size());

    std::list<exprt> constraints_t;
    build_equation(
      concrete_model.ns,
      active_passive_preds[t],
      abstract_state_from.pc->code.concrete_pc,
      constraints_t,
      predicates_wp[t]);

    constraints.splice(constraints.end(), constraints_t);
  }

  // create SAT solver object
  satcheckt satcheck;
  bv_pointerst solver(concrete_model.ns, satcheck);
  solver.unbounded_array=boolbvt::U_NONE;
  
  // convert constraints
  for(std::list<exprt>::const_iterator
      it=constraints.begin();
      it!=constraints.end();
      it++)
  {
    exprt tmp(*it);
    solver.set_to_true(tmp);
  }

  abstract_transition_relationt &abstract_transition_relation=
    abstract_state_from.pc->code.get_transition_relation();

  std::vector<std::vector<literalt> >
    predicate_variables_from(num_threads, std::vector<literalt>(predicates.size(), literalt())),
    predicate_variables_to(num_threads, std::vector<literalt>(predicates.size(), literalt()));
    
  bvt assumptions;

  std::vector<abstract_stept::thread_to_predicate_valuest::const_iterator>
    from_predicates(num_threads, abstract_state_from.thread_states.end()),
    to_predicates(num_threads, abstract_state_to.thread_states.end());
  for(unsigned int t=0; t < num_threads; ++t)
  {
    from_predicates[t]=abstract_state_from.thread_states.find(t);
    assert(abstract_state_from.thread_states.end() != from_predicates[t]);
    to_predicates[t]=abstract_state_to.thread_states.find(t);
    assert(abstract_state_to.thread_states.end() != to_predicates[t]);
  }

  // we use all predicates in order to also find constraints over invalid
  // from/to states
  for(unsigned i=0; i < predicates.size(); ++i)
  {
    assert(abstract_transition_relation.to_predicates.find(i) !=
        abstract_transition_relation.to_predicates.end() ||
        abstract_transition_relation.from_predicates.find(i) !=
        abstract_transition_relation.from_predicates.end() ||
        (from_predicates[active_id]->second[i] ==
         to_predicates[active_id]->second[i] &&
         (passive_id >= num_threads ||
          from_predicates[passive_id]->second[i] ==
          to_predicates[passive_id]->second[i])));

    for(unsigned int t=0; t < num_threads; ++t)
    {
      if(active_id!=t &&
          passive_id < num_threads &&
          t!=passive_id)
        continue;
      // not sure whether we really want the following check
      if(active_id!=t &&
        active_passive_preds[t][i]==predicates[i])
        continue;

      literalt li=make_pos(concrete_model.ns, solver, active_passive_preds[t][i]);
      predicate_variables_from[t][i]=li;

      assumptions.push_back(li.cond_negation(
            !from_predicates[t]->second[i]));

      #ifdef DEBUG
      const std::string predname=
        (active_id==t?"P#":"PP"+i2string(t)+"#")+i2string(i);
      std::cerr
        << "F: " << predname << ": "
        << (from_predicates[t]->second[i]?"":"!") << "("
        << from_expr(concrete_model.ns, "", active_passive_preds[t][i]) << ")" << std::endl;
      #endif

      literalt lo=make_pos(concrete_model.ns, solver, predicates_wp[t][i]);
      predicate_variables_to[t][i]=lo;

      assumptions.push_back(lo.cond_negation(
            !to_predicates[t]->second[i]));

      #ifdef DEBUG
      std::cerr
        << "T: " << predname << ": "
        << (to_predicates[t]->second[i]?"":"!") << "("
        << from_expr(concrete_model.ns, "", predicates_wp[t][i]) << ")" << std::endl;
      #endif
    }
  }
  
  satcheck.set_assumptions(assumptions);

  // solve it
  if(is_satisfiable(solver))
  {
    print(9, "Transition is OK");
    #ifdef DEBUG
    std::cout << "********\n";
    solver.print_assignment(std::cout);
    std::cout << "********\n";
    #endif
    return false; // ok
  }
  
  if(passive_id >= num_threads)
  {
    const std::string opt="Spurious assignment transitions requiring more than 1 passive thread";
    assert(stats.find(opt)!=stats.end());
    ++(stats[opt].val);
    return false; // can't do anything
  }

  print(9, "Assignment transition is spurious, refining");

  exprt constraint;

  for(unsigned i=0; i < predicates.size(); ++i)
  {
    if(satcheck.is_in_conflict(predicate_variables_from[active_id][i]))
    {
      constraint.operands().push_back(exprt());
      exprt &e=constraint.operands().back();
      e=exprt(ID_predicate_symbol, bool_typet());
      e.set(ID_identifier, i);
      if(from_predicates[active_id]->second[i]) e.make_not();
#ifdef DEBUG
      std::cout << "C-F: " << from_expr(concrete_model.ns, "", e) << std::endl;
#endif
    }

    if(passive_id!=active_id &&
        satcheck.is_in_conflict(predicate_variables_from[passive_id][i]))
    {
      constraint.operands().push_back(exprt());
      exprt &e=constraint.operands().back();
      e=exprt(ID_predicate_passive_symbol, bool_typet());
      e.set(ID_identifier, i);
      if(from_predicates[passive_id]->second[i]) e.make_not();
#ifdef DEBUG
      std::cout << "C-F: " << from_expr(concrete_model.ns, "", e) << std::endl;
#endif
    }

    if(satcheck.is_in_conflict(predicate_variables_to[active_id][i]))
    {
      constraint.operands().push_back(exprt());
      exprt &e=constraint.operands().back();
      e=exprt(ID_predicate_next_symbol, bool_typet());
      e.set(ID_identifier, i);
      if(to_predicates[active_id]->second[i]) e.make_not();
#ifdef DEBUG
      std::cout << "C-T: " << from_expr(concrete_model.ns, "", e) << std::endl;
#endif
    }

    if(passive_id!=active_id &&
        satcheck.is_in_conflict(predicate_variables_to[passive_id][i]))
    {
      constraint.operands().push_back(exprt());
      exprt &e=constraint.operands().back();
      e=exprt(ID_next_symbol, bool_typet());
      e.operands().resize(1);
      e.op0()=exprt(ID_predicate_passive_symbol, bool_typet());
      e.op0().set(ID_identifier, i);
      if(to_predicates[passive_id]->second[i]) e.make_not();
#ifdef DEBUG
      std::cout << "C-T: " << from_expr(concrete_model.ns, "", e) << std::endl;
#endif
    }
  }
#ifdef DEBUG
  ::std::cerr << "Spurious in thread " << passive_id << " (active: " << abstract_state_from.thread_nr << "): " << ::std::endl;
  ::std::cerr << abstract_state_from << ::std::endl;
  goto_programt tmp;
  tmp.output_instruction(concrete_model.ns, "", std::cerr, abstract_state_from.pc->code.concrete_pc);
  ::std::cerr << abstract_state_to << ::std::endl;
#endif

  if(constraint.operands().empty())
    constraint.make_false(); // this can happen if
  else                       // the invariants are inconsistent
    gen_or(constraint);
  
  abstract_transition_relation.constraints.push_back(constraint);

  // spurious!
  return true;
}
コード例 #12
0
ファイル: iterparse.c プロジェクト: askielboe/astropy
/*
 * Handle the expat startElement event.
 */
static void
startElement(IterParser *self, const XML_Char *name, const XML_Char **atts)
{
    PyObject*        pyname = NULL;
    PyObject*        pyatts = NULL;
    const XML_Char** att_ptr = atts;
    const XML_Char*  name_start = NULL;
    PyObject*        tuple = NULL;
    PyObject*        key = NULL;
    PyObject*        val = NULL;

    /* If we've already had an error in a previous call, don't make
       things worse. */
    if (PyErr_Occurred() != NULL) {
        XML_StopParser(self->parser, 0);
        return;
    }

    /* Don't overflow the queue -- in practice this should *never* happen */
    if (self->queue_write_idx < self->queue_size) {
        tuple = PyTuple_New(4);
        if (tuple == NULL) {
            XML_StopParser(self->parser, 0);
            return;
        }

        Py_INCREF(Py_True);
        PyTuple_SET_ITEM(tuple, 0, Py_True);

        /* This is an egregious but effective optimization.  Since by
           far the most frequently occurring element name in a large
           VOTABLE file is TD, we explicitly check for it here with
           integer comparison to avoid the lookup in the interned
           string table in PyString_InternFromString, and return a
           singleton string for "TD" */
        if ((*(int*)name & TD_AS_INT_MASK) == TD_AS_INT) {
            Py_INCREF(self->td_singleton);
            PyTuple_SetItem(tuple, 1, self->td_singleton);
        } else {
            name_start = remove_namespace(name);

            pyname = PyUnicode_FromString(name_start);
            if (pyname == NULL) {
                Py_DECREF(tuple);
                XML_StopParser(self->parser, 0);
                return;
            }
            PyTuple_SetItem(tuple, 1, pyname);
        }

        if (*att_ptr) {
            pyatts = PyDict_New();
            if (pyatts == NULL) {
                Py_DECREF(tuple);
                XML_StopParser(self->parser, 0);
                return;
            }
            do {
                if (*(*(att_ptr + 1)) != 0) {
                    /* Python < 2.6.5 can't handle unicode keyword
                       arguments.  Since those were coming from here
                       (the dictionary of attributes), we use byte
                       strings for the keys instead.  Should be fine
                       for VOTable, since it has ascii attribute
                       names, but that's not true of XML in
                       general. */
                    #if PY_VERSION_HEX < 0x02060500
                    /* Due to Python issue #4978 */
                    key = PyBytes_FromString(*att_ptr);
                    #else
                    key = PyUnicode_FromString(*att_ptr);
                    #endif
                    if (key == NULL) {
                        Py_DECREF(tuple);
                        XML_StopParser(self->parser, 0);
                        return;
                    }
                    val = PyUnicode_FromString(*(att_ptr + 1));
                    if (val == NULL) {
                        Py_DECREF(key);
                        Py_DECREF(tuple);
                        XML_StopParser(self->parser, 0);
                        return;
                    }
                    if (PyDict_SetItem(pyatts, key, val)) {
                        Py_DECREF(pyatts);
                        Py_DECREF(tuple);
                        Py_DECREF(key);
                        Py_DECREF(val);
                        XML_StopParser(self->parser, 0);
                        return;
                    }
                    Py_DECREF(key);
                    Py_DECREF(val);
                }
                att_ptr += 2;
            } while (*att_ptr);
        } else {
            Py_INCREF(self->dict_singleton);
            pyatts = self->dict_singleton;
        }

        PyTuple_SetItem(tuple, 2, pyatts);

        self->last_line = (unsigned long)XML_GetCurrentLineNumber(
            self->parser);
        self->last_col = (unsigned long)XML_GetCurrentColumnNumber(
            self->parser);
        PyTuple_SetItem(tuple, 3, make_pos(self));

        text_clear(self);

        self->keep_text = 1;

        self->queue[self->queue_write_idx++] = tuple;
    } else {
        PyErr_SetString(
            PyExc_RuntimeError,
            "XML queue overflow in startElement.  This most likely indicates an internal bug.");
    }
}