示例#1
0
文件: where.cpp 项目: wh5a/xgill
void RemoveValBit(FrameId frame, BlockMemory *mcfg,
                  const GuardBitVector &input, GuardBitVector *output)
{
  for (size_t iind = 0; iind < input.Size(); iind++) {
    const GuardBit &igb = input[iind];

    RemoveFrameMapper mapper(frame);
    Bit *nbit = igb.bit->DoMap(&mapper);

    GuardBitVector remove_res;
    mcfg->TranslateBit(TRK_RemoveVal, 0, nbit, &remove_res);
    nbit->DecRef();

    for (size_t rind = 0; rind < remove_res.Size(); rind++) {
      const GuardBit &rgb = remove_res[rind];
      rgb.IncRef();
      igb.guard->IncRef();

      Bit *new_guard = Bit::MakeAnd(igb.guard, rgb.guard);
      output->PushBack(GuardBit(rgb.bit, new_guard));
    }
  }

  output->SortCombine();
}
示例#2
0
 int dfs(int curr, vb& path, int len, int inv, Bit& bit) const
 {
     path[curr] = true;
     ++len;
     bit.add(V[curr], 1);
     inv += bit.rangeCount(V[curr] + 1, 1000);
     int ret = INT_MAX;
     do {
         if (K == len) {
             ret = inv;
             break;
         }
         if (K < len)
             break;
         for (int i = 0; i < G[curr].size(); ++i) {
             int node = G[curr][i];
             if (!path[node]) {
                 ret = min(ret, dfs(node, path, len, inv, bit));
             }
         }
     } while (false);
     path[curr] = false;
     bit.add(V[curr], -1);
     return ret;
 }
示例#3
0
文件: fort.cpp 项目: M4573R/pc-code
int solve()
{
    sort(sticks, sticks + N);
    int hi = sticks[N - 1];

    int ans = 0;
    tree.init(sticks[N - 1]);

    for (int i = N - 4; i >= 2; --i) {
        int a = i + 1;
        for (int b = a + 1; b < N; ++b)
            for (int c = b + 1; c < N; ++c) {
                int v = max(1, sticks[c] - (sticks[a] + sticks[b]));
                tree.add(v, 1);
            }

        for (int j = i - 1; j >= 1; --j)
            for (int k = j - 1; k >= 0; --k) {
                int v = min(hi, sticks[i] + sticks[j] + sticks[k]);
                ans += tree.query(v - 1);
            }
    }

    return ans;
}
示例#4
0
int main ()
{

    bit = Bit (20);

    bit.Set (1, 10);

    printf ("%d\n", bit.Sum (15));

    return 0;
}
示例#5
0
int HuffmanDecoder::decode(IOReader& reader, IOWriter& writer, BitCode codes[], size_t codeCnt, size_t excessBit) {
	const size_t fileSize = reader.size() - sizeof(HuffmanFileHeader);

	size_t firstCode = 0;
	while (firstCode < codeCnt && codes[firstCode].bit.len == 0)
		++firstCode;
	if (firstCode >= codeCnt)
		return 0;

	const size_t minCodeLength = codes[firstCode].bit.len;

	const __int64 availableBits = excessBit ? ((fileSize -1 ) * 8 + excessBit) : fileSize * 8;
	__int64 consumeBits = 0;

	while(consumeBits < availableBits) {
		size_t codeLength = minCodeLength;
		Bit data = reader.readBits(codeLength);
		if (data.len != codeLength)
			return 1;

		consumeBits += data.len;

		bool find = false;

		for (size_t i = firstCode; i< codeCnt; ++i ) {
			const Bit& bit = codes[i].bit;
			if (bit.len > codeLength) {
				Bit appendBits = reader.readBits( bit.len - codeLength);

				if (appendBits.len != bit.len - codeLength)
					return 1;

				consumeBits += appendBits.len;

				data.add(appendBits);

				codeLength = bit.len;
			}			

			if (bit.bits == data.bits) {
				writer.write(&(codes[i].value), 1);
				find = true;
				break;
			}
		}

		if (!find )
			return 2;
	};

	return 0;	
}
示例#6
0
Bit PermutationManager::InversePermutation(Bit bit, IPermutationTable* table) {
	int sz = bit.Size();
	int n = table->Size();
	Bit res(0, sz);
	for (int i = 0; i < n; i++) {
		if (bit[i]) res.Set(table->Get(i) - 1);
	}
	return res;
}
示例#7
0
/**
 * Función para modificar los bits de un arreglo de caracteres de acuerdo a los parámetros recibidos
 * @param salida Es un arreglo de caracteres al cual se le van a modificar los bits
 * @param salidaLength El tamaño en bits del arreglo
 * @param pointer La posición a partir de la cual se comenzar a modificar los
 * @param c El caracter a codificar
 * @param frecuencias El arreglo de las frecuencias
 * @param length El tamaño del arreglo de frecuencias
 */
int Codificacion::escribeBits(char * salida, int salidaLength, long pointer, char c, Frecuencia * frecuencias, int length){
	int bitsescritos = 0;
	int indiceFrecuencia = buscaFrecuencia(frecuencias, 0, length, c);
	Frecuencia f = frecuencias[indiceFrecuencia];
	Bit * bits = new Bit();
	if (indiceFrecuencia >= 0){
		for (bitsescritos = 0; f.codigo[bitsescritos] != '\0'; bitsescritos++)
		{
			if (f.codigo[bitsescritos] == '1')
			{
				bits->pon1(salida, pointer + bitsescritos);
			}
			else if (f.codigo[bitsescritos] == '0')
			{
				bits->pon0(salida, pointer + bitsescritos);
			}
		}
	}
	return bitsescritos;
}
int main(){
   // freopen("input.txt", "r", stdin);
    Node lis[SZ];
    int n;
    int test, val, pos,  tc = 1;
    scanf("%d", &test);
    while( test-- ){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            scanf("%d", &val);
            lis[i] = Node(val, i);
        }
        sort(lis+1, lis+n+1);

        Bit bt = Bit(n);
        for(int idx = 1; idx <= n; idx++){
            pos = lis[idx].pos;
            bt.add(pos, 1 + bt.query(pos-1));
        }
        printf("Case %d: %lld\n", tc++, bt.query(n));
    }

    return 0;
}
示例#9
0
int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &N, &M);

        bit.init(N + M);

        for (int i = 1; i <= N; i++) {
            bit.add(i, 1);
            no[i] = N + 1 - i;
        }

        for (int i = 1; i <= M; i++) {
            scanf("%d", &A[i]);
            printf("%d%c", N - bit.sum(no[A[i]]), i < M ? ' ' : '\n');

            bit.add(no[A[i]], -1);
            no[A[i]] = N + i;
            bit.add(no[A[i]], 1);
        }
    }

    return 0;
}
示例#10
0
文件: j.cpp 项目: M4573R/pc-code
int main()
{
	int T;
	scanf("%d", &T);

	int ncase = 0;
	while (T--) {
		scanf("%d", &N);
		tree.init(N);
		for (int i = 1; i <= N; ++i) {
			int x;
			scanf("%d", &x);
			tree.add(i, x);
		}

		printf("Case %d:\n", ++ncase);
		scanf("%d", &M);
		while (M--) {
			int cmd;
			scanf("%d", &cmd);
			if (cmd == 1) {
				int x, y;
				scanf("%d%d", &x, &y);
				printf("%d\n", tree.query(x, y));
			}
			else if (cmd == 2) {
				int x, y, p;
				scanf("%d%d%d", &x, &y, &p);
				tree.add(x, -p);
				tree.add(y, p);
			}
		}
	}

	return 0;
}
QVector<Bit> AVRStudioXMLParser::GetBits(QDomNode node)
{
    QVector<Bit> bits(0);

    while(!node.isNull())
    {
        QDomNamedNodeMap attributes = node.attributes();
        Bit bit;

        for(int i = 0; i < attributes.size(); i++)
        {
            QDomNode attribute = attributes.item(i);
            if(bit.GetMappingMap()[attribute.nodeName()])
                *bit.GetMappingMap()[attribute.nodeName()] = attribute.nodeValue();
        }

        bit.SetValues(GetValues(node.childNodes()));
        bits.append(bit);

        node = node.nextSibling();
    }

    return bits;
}
void GetImplySufficient(CheckerFrame *frame, Vector<Bit*> *imply_list)
{
  // only getting implications for loops for now.
  if (frame->Kind() != B_Loop)
    return;

  // look for assumed bits of the form 'A || B'.
  // then check if either !A or !B is a sufficient condition
  // (this will force the other half of the implication to hold).

  for (size_t bind = 0; bind < frame->m_assumed_bits.Size(); bind++) {
    Bit *bit = frame->m_assumed_bits[bind];

    if (bit->Kind() == BIT_Or) {
      for (size_t oind = 0; oind < bit->GetOperandCount(); oind++) {
        Bit *op = bit->GetOperand(oind);
        Bit *nop = Bit::MakeNot(op);

        if (!imply_list->Contains(nop))
          imply_list->PushBack(nop);
      }
    }
  }
}
示例#13
0
文件: checker.cpp 项目: wh5a/xgill
bool CheckSingleHeapWrite(CheckerState *state, CheckerFrame *frame,
                          CheckerFrame *heap_frame, WhereInvariant *invariant,
                          const HeapWriteInfo &write)
{
  Assert(invariant);

  if (checker_verbose.IsSpecified())
    logout << "CHECK: " << frame
           << ": Checking single heap write: " << write.mcfg->GetId()
           << ": " << write.lval << endl;

  if (frame->Memory()->GetId()->Kind() == B_Initializer) {
    // rule out cases where another function executed before a static
    // initializer. TODO: this case can occur in C++ constructors and the
    // functions they call, should revisit this behavior.
    if (write.mcfg->GetId()->Kind() != B_Initializer) {
      if (checker_verbose.IsSpecified())
        logout << "CHECK: " << frame
               << ": Write is not in a static initializer" << endl;

      return false;
    }
  }

  CheckerFrame *write_frame = state->MakeFrame(write.mcfg->GetId());
  Assert(write_frame && write_frame->Memory() == write.mcfg);

  PPoint exit_point = write.mcfg->GetCFG()->GetExitPoint();
  write_frame->AssertPointGuard(exit_point, true);

  // assert the lvalue is actually updated within this frame. these bits
  // are never explicitly popped, they get dropped when the frame is deleted.
  for (size_t ind = 0; ind < write.exclude.Size(); ind++) {
    Bit *bit = write.exclude[ind];

    bit->IncRef();
    Bit *not_bit = Bit::MakeNot(bit);

    write_frame->PushAssumedBit(not_bit);
    not_bit->DecRef();
  }

  // connect the heap read and write frames.
  write_frame->ConnectHeapRead(heap_frame);

  // for type invariants, try to reconstruct the CSU exp for the write frame.
  Exp *write_csu = NULL;
  Exp *base_csu = NULL;
  if (invariant->GetCSU()) {
    write_csu = invariant->GetWriteCSU(write.lval);
    if (write_csu == NULL) {
      CheckerPropagate propagate(write_frame, exit_point, false);
      state->m_stack.PushBack(&propagate);

      state->SetReport(RK_UnknownCSU);
      return true;
    }

    // OK if we couldn't figure out the point-relative CSU, will just be
    // a more confusing UI message.
    base_csu = invariant->GetWriteCSU(write.base_lval);
  }

  // get the safe bits for the write frame.
  Bit *write_safe_bit;
  GuardBitVector write_safe_list;
  invariant->GetHeapBits(write_frame, write_csu, base_csu,
                         &write_safe_bit, &write_safe_list);

  if (CheckFrameList(state, write_frame, exit_point, true, true,
                     write_safe_bit, write_safe_list)) {
    write_safe_bit->DecRef();
    return true;
  }

  write_safe_bit->DecRef();
  write_frame->DisconnectHeapRead(heap_frame);
  state->DeleteFrame(write_frame);

  return false;
}
示例#14
0
 /*
 * Return true if a flag is set, false otherwise.
 */
 void Modifier::set(Bit flag)
 {  flag.set(flags_); }
示例#15
0
 /*
 * Return true if a flag is set, false otherwise.
 */
 bool Modifier::isSet(Bit flag) const
 {  return flag.isSet(flags_); }
示例#16
0
文件: summary.cpp 项目: wh5a/xgill
void BlockSummary::GetAssumedBits(BlockMemory *mcfg, PPoint end_point,
                                  Vector<AssumeInfo> *assume_list)
{
  BlockId *id = mcfg->GetId();
  BlockCFG *cfg = mcfg->GetCFG();

  BlockSummary *sum = GetBlockSummary(id);

  const Vector<Bit*> *assumes = sum->GetAssumes();
  size_t assume_count = VectorSize<Bit*>(assumes);

  // pull in assumptions from the summary for mcfg. in some cases these
  // assumptions won't be useful, e.g. describing the state at exit
  // for functions. for now we're just adding all of them though. TODO: fix.
  for (size_t ind = 0; ind < assume_count; ind++) {
    Bit *bit = assumes->At(ind);
    bit->IncRef(assume_list);

    AssumeInfo info;
    info.bit = bit;
    assume_list->PushBack(info);
  }

  sum->DecRef();

  Vector<BlockCFG*> *annot_list = BodyAnnotCache.Lookup(id->Function());

  // add assumes at function entry for any preconditions.

  if (id->Kind() == B_Function) {
    for (size_t ind = 0; annot_list && ind < annot_list->Size(); ind++) {
      BlockCFG *annot_cfg = annot_list->At(ind);

      if (annot_cfg->GetAnnotationKind() != AK_Precondition &&
          annot_cfg->GetAnnotationKind() != AK_PreconditionAssume)
        continue;

      Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg);
      if (!bit) continue;

      annot_cfg->IncRef(assume_list);
      bit->IncRef(assume_list);

      AssumeInfo info;
      info.annot = annot_cfg;
      info.bit = bit;
      assume_list->PushBack(info);
    }
  }

  // add assumptions from points within the block.

  for (size_t pind = 0; pind < cfg->GetPointAnnotationCount(); pind++) {
    PointAnnotation pann = cfg->GetPointAnnotation(pind);
    if (end_point && pann.point >= end_point)
      continue;

    BlockCFG *annot_cfg = GetAnnotationCFG(pann.annot);
    if (!annot_cfg) continue;

    Assert(annot_cfg->GetAnnotationKind() != AK_AssertRuntime);

    if (Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg)) {
      // get the annotation bit in terms of block entry.
      Bit *point_bit = NULL;
      mcfg->TranslateBit(TRK_Point, pann.point, bit, &point_bit);
      point_bit->MoveRef(&point_bit, assume_list);

      annot_cfg->IncRef(assume_list);

      AssumeInfo info;
      info.annot = annot_cfg;
      info.point = pann.point;
      info.bit = point_bit;
      assume_list->PushBack(info);
    }

    annot_cfg->DecRef();
  }

  // add assumptions from annotation edges within the block, invariants
  // on values accessed by the block, and from the summaries of any callees.

  for (size_t eind = 0; eind < cfg->GetEdgeCount(); eind++) {
    PEdge *edge = cfg->GetEdge(eind);
    PPoint point = edge->GetSource();

    if (end_point && point >= end_point)
      continue;

    InvariantAssumeVisitor visitor(mcfg, point, assume_list);
    edge->DoVisit(&visitor);

    if (PEdgeAnnotation *nedge = edge->IfAnnotation()) {
      // add an assumption for this annotation.
      BlockCFG *annot_cfg = GetAnnotationCFG(nedge->GetAnnotationId());
      if (!annot_cfg) continue;

      Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg);

      // don't incorporate AssertRuntimes, these are not assumed.
      if (bit && annot_cfg->GetAnnotationKind() != AK_AssertRuntime) {
        // get the annotation bit in terms of block entry.
        Bit *point_bit = NULL;
        mcfg->TranslateBit(TRK_Point, point, bit, &point_bit);
        point_bit->MoveRef(&point_bit, assume_list);

        annot_cfg->IncRef(assume_list);

        AssumeInfo info;
        info.annot = annot_cfg;
        info.point = point;
        info.bit = point_bit;
        assume_list->PushBack(info);
      }

      annot_cfg->DecRef();
    }

    if (BlockId *callee = edge->GetDirectCallee()) {
      GetCallAssumedBits(mcfg, edge, callee, false, assume_list);
      callee->DecRef();
    }
    else if (edge->IsCall()) {
      // add conditional assumes for the indirect targets of the call.
      // this is most useful for baked information and annotations, where
      // we sometimes need to attach information at indirect calls.

      CallEdgeSet *callees = CalleeCache.Lookup(id->BaseVar());
      size_t old_count = assume_list->Size();

      if (callees) {
        for (size_t cind = 0; cind < callees->GetEdgeCount(); cind++) {
          const CallEdge &cedge = callees->GetEdge(cind);
          if (cedge.where.id == id && cedge.where.point == point) {
            cedge.callee->IncRef();
            BlockId *callee = BlockId::Make(B_Function, cedge.callee);

            GetCallAssumedBits(mcfg, edge, callee, true, assume_list);
            callee->DecRef();
          }
        }
      }

      if (assume_list->Size() != old_count) {
        // we managed to do something at this indirect call site.
        // add another assumption restricting the possible callees to
        // only those identified by our callgraph.

        GuardExpVector receiver_list;
        mcfg->TranslateReceiver(point, &receiver_list);

        for (size_t rind = 0; rind < receiver_list.Size(); rind++) {
          const GuardExp &gs = receiver_list[rind];
          gs.guard->IncRef();

          // make a bit: !when || rcv == callee0 || rcv == callee1 || ...
          Bit *extra_bit = Bit::MakeNot(gs.guard);

          for (size_t cind = 0; cind < callees->GetEdgeCount(); cind++) {
            const CallEdge &cedge = callees->GetEdge(cind);
            if (cedge.where.id == id && cedge.where.point == point) {
              Variable *callee_var = cedge.callee;
              callee_var->IncRef();
              Exp *callee_exp = Exp::MakeVar(callee_var);

              gs.exp->IncRef();
              Bit *equal = Exp::MakeCompareBit(B_Equal, callee_exp, gs.exp);

              extra_bit = Bit::MakeOr(extra_bit, equal);
            }
          }

          extra_bit->MoveRef(NULL, assume_list);

          AssumeInfo info;
          info.bit = extra_bit;
          assume_list->PushBack(info);
        }
      }

      CalleeCache.Release(id->BaseVar());
    }
  }

  BodyAnnotCache.Release(id->Function());

  // add assumptions from heap invariants describing values mentioned
  // in added assumptions. we could keep doing this transitively but don't,
  // to ensure termination.
  size_t count = assume_list->Size();
  for (size_t ind = 0; ind < count; ind++) {
    InvariantAssumeVisitor visitor(NULL, 0, assume_list);
    assume_list->At(ind).bit->DoVisit(&visitor);
  }

  CombineAssumeList(assume_list);
}
示例#17
0
文件: summary.cpp 项目: wh5a/xgill
  void Visit(Exp *exp)
  {
    if (ExpFld *nexp = exp->IfFld()) {
      // pick up any type invariants from the host type.
      String *csu_name = nexp->GetField()->GetCSUType()->GetCSUName();
      Vector<BlockCFG*> *annot_list = CompAnnotCache.Lookup(csu_name);

      for (size_t ind = 0; annot_list && ind < annot_list->Size(); ind++) {
        BlockCFG *annot_cfg = annot_list->At(ind);
        Assert(annot_cfg->GetAnnotationKind() == AK_Invariant ||
               annot_cfg->GetAnnotationKind() == AK_InvariantAssume);
        BlockId *id = annot_cfg->GetId();

        Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg);
        if (!bit) continue;

        // get the *this expression. we'll replace this with the actual CSU
        // lvalue to get the assumed bit.
        id->IncRef();
        Variable *this_var = Variable::Make(id, VK_This, NULL, 0, NULL);
        Exp *this_exp = Exp::MakeVar(this_var);
        Exp *this_drf = Exp::MakeDrf(this_exp);
        Exp *target = nexp->GetTarget();

        GuardExpVector lval_res;
        if (mcfg) {
          mcfg->TranslateExp(TRK_Point, point, target, &lval_res);
        }
        else {
          target->IncRef();
          lval_res.PushBack(GuardExp(target, Bit::MakeConstant(true)));
        }

        for (size_t lind = 0; lind < lval_res.Size(); lind++) {
          // ignore the guard component of the result here. this means that
          // accessing a field of a value means related invariants hold for
          // the value along all paths. which is normally right, except when
          // the value is the result of a cast, and could have a different type
          // along other paths. TODO: sort this out.
          const GuardExp &gs = lval_res[lind];
          Bit *new_bit = BitReplaceExp(bit, this_drf, gs.exp);

          new_bit->MoveRef(NULL, assume_list);
          annot_cfg->IncRef(assume_list);

          AssumeInfo info;
          info.annot = annot_cfg;
          info.point = 0;
          info.bit = new_bit;
          assume_list->PushBack(info);
        }

        this_drf->DecRef();
      }

      CompAnnotCache.Release(csu_name);
    }

    if (ExpVar *nexp = exp->IfVar()) {
      if (nexp->GetVariable()->Kind() == VK_Glob) {
        String *var_name = nexp->GetVariable()->GetName();
        Vector<BlockCFG*> *annot_list = InitAnnotCache.Lookup(var_name);

        for (size_t ind = 0; annot_list && ind < annot_list->Size(); ind++) {
          BlockCFG *annot_cfg = annot_list->At(ind);
          Assert(annot_cfg->GetAnnotationKind() == AK_Invariant ||
                 annot_cfg->GetAnnotationKind() == AK_InvariantAssume);

          Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg);
          if (!bit) continue;

          bit->IncRef(assume_list);
          annot_cfg->IncRef(assume_list);

          AssumeInfo info;
          info.annot = annot_cfg;
          info.point = 0;
          info.bit = bit;
          assume_list->PushBack(info);
        }

        InitAnnotCache.Release(var_name);
      }
    }
  }
示例#18
0
void InferSummaries(const Vector<BlockSummary*> &summary_list)
{
  static BaseTimer infer_timer("infer_summaries");
  Timer _timer(&infer_timer);

  if (summary_list.Empty())
    return;

  Variable *function = summary_list[0]->GetId()->BaseVar();
  Vector<BlockCFG*> *annot_list = BodyAnnotCache.Lookup(function->GetName());

  // all traces which might refer to the result of pointer arithmetic.
  Vector<Exp*> arithmetic_list;
  ArithmeticEscape escape(function, arithmetic_list);

  // initial pass over the CFGs to get traces used in pointer arithmetic.
  for (size_t ind = 0; ind < summary_list.Size(); ind++) {
    BlockSummary *sum = summary_list[ind];

    BlockCFG *cfg = sum->GetMemory()->GetCFG();
    for (size_t eind = 0; eind < cfg->GetEdgeCount(); eind++) {
      PEdge *edge = cfg->GetEdge(eind);

      if (PEdgeAssign *assign_edge = edge->IfAssign()) {
        Exp *left = assign_edge->GetLeftSide();
        Exp *right = assign_edge->GetRightSide();
        ProcessArithmeticAssign(&escape, cfg->GetId(), left, right);
      }
    }
  }

  for (size_t ind = 0; ind < summary_list.Size(); ind++) {
    BlockSummary *sum = summary_list[ind];
    BlockMemory *mcfg = sum->GetMemory();
    BlockCFG *cfg = mcfg->GetCFG();

    // accumulate all the assertions at points in the CFG.
    Vector<AssertInfo> asserts;

    // add assertions at function exit for any postconditions.
    if (cfg->GetId()->Kind() == B_Function) {
      for (size_t aind = 0; annot_list && aind < annot_list->Size(); aind++) {
        BlockCFG *annot_cfg = annot_list->At(aind);

        if (annot_cfg->GetAnnotationKind() != AK_Postcondition)
          continue;
        if (Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg)) {
          AssertInfo info;
          info.kind = ASK_Annotation;
          info.cls = ASC_Check;
          info.point = cfg->GetExitPoint();
          info.bit = bit;
          asserts.PushBack(info);
        }
      }
    }

    // add assertions for any point annotations within the CFG.
    for (size_t pind = 0; pind < cfg->GetPointAnnotationCount(); pind++) {
      PointAnnotation pann = cfg->GetPointAnnotation(pind);
      BlockCFG *annot_cfg = GetAnnotationCFG(pann.annot);
      if (!annot_cfg) continue;

      if (annot_cfg->GetAnnotationKind() != AK_Assert)
        continue;

      if (Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg)) {
        AssertInfo info;
        info.kind = ASK_Annotation;
        info.cls = ASC_Check;
        info.point = pann.point;
        info.bit = bit;
        asserts.PushBack(info);
      }
    }

    for (size_t eind = 0; eind < cfg->GetEdgeCount(); eind++) {
      PEdge *edge = cfg->GetEdge(eind);
      PPoint point = edge->GetSource();

      if (PEdgeAnnotation *nedge = edge->IfAnnotation()) {
        // add an assertion for this annotation if it not an assume.
        BlockCFG *annot_cfg = GetAnnotationCFG(nedge->GetAnnotationId());
        if (!annot_cfg) continue;

        if (annot_cfg->GetAnnotationKind() != AK_Assert &&
            annot_cfg->GetAnnotationKind() != AK_AssertRuntime) {
          continue;
        }

        if (Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg)) {
          AssertInfo info;
          info.kind = (annot_cfg->GetAnnotationKind() == AK_Assert)
            ? ASK_Annotation : ASK_AnnotationRuntime;
          info.cls = ASC_Check;
          info.point = point;
          info.bit = bit;
          asserts.PushBack(info);
        }
      }

      // add assertions for any invariants affected by a write.

      Exp *left = NULL;
      if (PEdgeAssign *nedge = edge->IfAssign())
        left = nedge->GetLeftSide();
      if (PEdgeCall *nedge = edge->IfCall())
        left = nedge->GetReturnValue();

      // for now our detection of affected invariants is pretty crude;
      // writes to fields can affect type invariants on the field's type
      // which use that field, and writes to global variables can affect
      // invariants on that global. TODO: pin this down once we draw a
      // precise line between which invariants can and can't be checked.

      if (left && left->IsFld()) {
        ExpFld *nleft = left->AsFld();
        String *csu_name = nleft->GetField()->GetCSUType()->GetCSUName();
        Vector<BlockCFG*> *comp_annot_list = CompAnnotCache.Lookup(csu_name);

        for (size_t aind = 0; comp_annot_list &&
                              aind < comp_annot_list->Size(); aind++) {
          BlockCFG *annot_cfg = comp_annot_list->At(aind);

          if (annot_cfg->GetAnnotationKind() != AK_Invariant)
            continue;
          Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg);
          if (!bit) continue;

          Vector<Exp*> lval_list;
          LvalListVisitor visitor(&lval_list);
          bit->DoVisit(&visitor);

          bool uses_field = false;
          for (size_t ind = 0; ind < lval_list.Size(); ind++) {
            if (ExpFld *lval = lval_list[ind]->IfFld()) {
              if (lval->GetField() == nleft->GetField())
                uses_field = true;
            }
          }

          if (uses_field) {
            // this is a type invariant which uses the field being written
            // as an lvalue. we need to assert this write preserves
            // the invariant.
            BlockId *id = annot_cfg->GetId();
            Variable *this_var = Variable::Make(id, VK_This, NULL, 0, NULL);
            Exp *this_exp = Exp::MakeVar(this_var);
            Exp *this_drf = Exp::MakeDrf(this_exp);

            Bit *new_bit = BitReplaceExp(bit, this_drf, nleft->GetTarget());

            AssertInfo info;
            info.kind = ASK_Invariant;
            info.cls = ASC_Check;
            info.point = point;
            info.bit = new_bit;
            asserts.PushBack(info);
          }
        }

        CompAnnotCache.Release(csu_name);
      }

      if (left && left->IsVar()) {
        Variable *var = left->AsVar()->GetVariable();
        if (var->Kind() == VK_Glob) {
          Vector<BlockCFG*> *glob_annot_list =
            InitAnnotCache.Lookup(var->GetName());

          for (size_t aind = 0; glob_annot_list &&
                                aind < glob_annot_list->Size(); aind++) {
            BlockCFG *annot_cfg = glob_annot_list->At(aind);

            Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg);
            if (!bit) continue;

            AssertInfo info;
            info.kind = ASK_Invariant;
            info.cls = ASC_Check;
            info.point = point;
            info.bit = bit;
            asserts.PushBack(info);
          }

          InitAnnotCache.Release(var->GetName());
        }
      }

      if (PEdgeCall *nedge = edge->IfCall()) {
        // add assertions for any callee preconditions.

        // pull preconditions from both direct and indirect calls.
        Vector<Variable*> callee_names;

        if (Variable *callee = nedge->GetDirectFunction()) {
          callee_names.PushBack(callee);
        }
        else {
          CallEdgeSet *callees = CalleeCache.Lookup(function);

          if (callees) {
            for (size_t cind = 0; cind < callees->GetEdgeCount(); cind++) {
              const CallEdge &edge = callees->GetEdge(cind);
              if (edge.where.id == cfg->GetId() && edge.where.point == point)
                callee_names.PushBack(edge.callee);
            }
          }

          // CalleeCache release is below.
        }

        for (size_t cind = 0; cind < callee_names.Size(); cind++) {
          String *callee = callee_names[cind]->GetName();
          Vector<BlockCFG*> *call_annot_list = BodyAnnotCache.Lookup(callee);

          for (size_t aind = 0;
               call_annot_list && aind < call_annot_list->Size(); aind++) {
            BlockCFG *annot_cfg = call_annot_list->At(aind);

            if (annot_cfg->GetAnnotationKind() != AK_Precondition)
              continue;
            if (Bit *bit = BlockMemory::GetAnnotationBit(annot_cfg)) {
              ConvertCallsiteMapper mapper(cfg, point, false);
              Bit *caller_bit = bit->DoMap(&mapper);
              if (!caller_bit)
                continue;

              AssertInfo info;
              info.kind = ASK_Annotation;
              info.cls = ASC_Check;
              info.point = point;
              info.bit = caller_bit;
              asserts.PushBack(info);
            }
          }

          BodyAnnotCache.Release(callee);
        }

        if (!nedge->GetDirectFunction())
          CalleeCache.Release(function);
      }

      BufferScanVisitor write_visitor(asserts, arithmetic_list, point, true);
      BufferScanVisitor read_visitor(asserts, arithmetic_list, point, false);
      IntegerScanVisitor integer_visitor(asserts, point);
      GCScanVisitor gcsafe_visitor(asserts, point);

      // only look at the written lvalues for the write visitor.
      if (PEdgeAssign *assign = edge->IfAssign())
        write_visitor.Visit(assign->GetLeftSide());
      if (PEdgeCall *call = edge->IfCall()) {
        if (Exp *returned = call->GetReturnValue())
          write_visitor.Visit(returned);
      }

      edge->DoVisit(&read_visitor);

      // disable integer overflow visitor for now.
      // edge->DoVisit(&integer_visitor);

      edge->DoVisit(&gcsafe_visitor);
    }

    if (cfg->GetId()->Kind() == B_Function) {
      BlockModset *modset = GetBlockModset(cfg->GetId());
      if (modset->CanGC()) {
        AssertInfo info;
        info.kind = ASK_CanGC;
        info.cls = ASC_Check;
        info.point = cfg->GetExitPoint();

        String *name = cfg->GetId()->BaseVar()->GetName();
        Variable *var = Variable::Make(NULL, VK_Glob, name, 0, name);
        Exp *varexp = Exp::MakeVar(var);
        Exp *gcsafe = Exp::MakeGCSafe(varexp, false);
        info.bit = Bit::MakeVar(gcsafe);
        asserts.PushBack(info);
      }
    }

    MarkRedundantAssertions(mcfg, asserts);

    // move the finished assertion list into the summary.
    for (size_t ind = 0; ind < asserts.Size(); ind++) {
      const AssertInfo &info = asserts[ind];
      sum->AddAssert(info.kind, info.cls, info.point, info.bit);
    }
  }

  // infer delta and termination invariants for all summaries.
  for (size_t ind = 0; ind < summary_list.Size(); ind++)
    InferInvariants(summary_list[ind], arithmetic_list);

  BodyAnnotCache.Release(function->GetName());
}
示例#19
0
文件: chart.cpp 项目: M4573R/pc-code
	void init(int n) {
		up.init(n);
		dn.init(n);
		nt.init(n);
	}
void TestClass::TestDecimalToBinary()
{
	Bit bit;
	bit.DecimalToBinary("32.72");
}
void TestClass::TestUpdateBits()
{
	Bit bit;
	int result = bit.UpdateBit(2048, 21, 2, 6);
	assert(result == 2132);
}
void TestClass::TestSwapBits()
{
	Bit bit;
	bit.SwapBits(14);
}
void TestClass::TestBitDistance()
{
	Bit bit;
	assert( 2 == bit.BitDistance(31, 14));
}