Exemple #1
0
int run_vector()
{
	std::vector<int> z(100);
	std::generate(z.begin(), z.end(), igen());

	std::printf("vector test: %p, %p\n", &*z.begin(), &*z.end());
	for (std::vector<int>::iterator it = z.begin(); it != z.end(); it++)
	{
		std::printf("%d\n", *it);
	}

	std::vector<std::string> y;
	std::back_insert_iterator<std::vector<std::string> > back(y);
	back++ = "hello";
	back++ = "mcfly";

	std::insert_iterator<std::vector<std::string> > ins(y, y.begin() + 1);
	ins++ = "(stupid)";

	for (std::vector<std::string>::iterator it = y.begin(); it != y.end(); it++)
	{
		std::printf("%s\n", it->c_str());
	}

	std::vector<std::string>::iterator eit = std::find(y.begin(), y.end(), "(stupid)");
	if (eit != y.end())
		y.erase(eit);
	eit = std::find(y.begin(), y.end(), "blah");
	if (eit != y.end())
		y.erase(eit);

	for (std::vector<std::string>::iterator it = y.begin(); it != y.end(); it++)
	{
		std::printf("%s\n", it->c_str());
	}

	return 0;
}
Exemple #2
0
void
complexgen(Node *n, Node *res)
{
	Node *nl, *nr;
	Node tnl, tnr;
	Node n1, n2, tmp;
	int tl, tr;

	if(debug['g']) {
		dump("\ncomplexgen-n", n);
		dump("complexgen-res", res);
	}

	// pick off float/complex opcodes
	switch(n->op) {
	case OCMPLX:
		subnode(&n1, &n2, res);
		tempname(&tmp, n1.type);
		cgen(n->left, &tmp);
		cgen(n->right, &n2);
		cgen(&tmp, &n1);
		return;

	case OREAL:
		subnode(&n1, &n2, n->left);
		cgen(&n1, res);
		return;

	case OIMAG:
		subnode(&n1, &n2, n->left);
		cgen(&n2, res);
		return;
	}

	// perform conversion from n to res
	tl = simsimtype(res->type);
	tl = cplxsubtype(tl);
	tr = simsimtype(n->type);
	tr = cplxsubtype(tr);
	if(tl != tr) {
		if(!n->addable) {
			tempname(&n1, n->type);
			complexmove(n, &n1);
			n = &n1;
		}
		complexmove(n, res);
		return;
	}

	if(!res->addable) {
		igen(res, &n1, N);
		cgen(n, &n1);
		regfree(&n1);
		return;
	}
	if(n->addable) {
		complexmove(n, res);
		return;
	}

	switch(n->op) {
	default:
		dump("complexgen: unknown op", n);
		fatal("complexgen: unknown op %O", n->op);

	case ODOT:
	case ODOTPTR:
	case OINDEX:
	case OIND:
	case ONAME:	// PHEAP or PPARAMREF var
		igen(n, &n1, res);
		complexmove(&n1, res);
		regfree(&n1);
		return;

	case OCONV:
	case OADD:
	case OSUB:
	case OMUL:
	case OMINUS:
	case OCMPLX:
	case OREAL:
	case OIMAG:
		break;
	}

	nl = n->left;
	if(nl == N)
		return;
	nr = n->right;

	// make both sides addable in ullman order
	if(nr != N) {
		if(nl->ullman > nr->ullman && !nl->addable) {
			tempname(&tnl, nl->type);
			cgen(nl, &tnl);
			nl = &tnl;
		}
		if(!nr->addable) {
			tempname(&tnr, nr->type);
			cgen(nr, &tnr);
			nr = &tnr;
		}
	}
	if(!nl->addable) {
		tempname(&tnl, nl->type);
		cgen(nl, &tnl);
		nl = &tnl;
	}

	switch(n->op) {
	default:
		fatal("complexgen: unknown op %O", n->op);
		break;

	case OCONV:
		complexmove(nl, res);
		break;

	case OMINUS:
		complexminus(nl, res);
		break;

	case OADD:
	case OSUB:
		complexadd(n->op, nl, nr, res);
		break;

	case OMUL:
		complexmul(nl, nr, res);
		break;
	// ODIV call a runtime function
	}
}
void SmackModuleGenerator::generateProgram(llvm::Module& M) {

  Naming naming;
  SmackRep rep(M.getDataLayout(), naming, program, getAnalysis<Regions>());
  std::list<Decl*>& decls = program.getDeclarations();

  DEBUG(errs() << "Analyzing globals...\n");

  for (auto& G : M.globals()) {
    auto ds = rep.globalDecl(&G);
    decls.insert(decls.end(), ds.begin(), ds.end());
  }

  DEBUG(errs() << "Analyzing functions...\n");

  for (auto& F : M) {

    // Reset the counters for per-function names
    naming.reset();

    DEBUG(errs() << "Analyzing function: " << naming.get(F) << "\n");

    auto ds = rep.globalDecl(&F);
    decls.insert(decls.end(), ds.begin(), ds.end());

    auto procs = rep.procedure(&F);
    assert(procs.size() > 0);

    if (naming.get(F) != Naming::DECLARATIONS_PROC)
      decls.insert(decls.end(), procs.begin(), procs.end());

    if (F.isDeclaration())
      continue;

    if (!F.empty() && !F.getEntryBlock().empty()) {
      DEBUG(errs() << "Analyzing function body: " << naming.get(F) << "\n");

      for (auto P : procs) {
        SmackInstGenerator igen(getAnalysis<LoopInfo>(F), rep, *P, naming);
        DEBUG(errs() << "Generating body for " << naming.get(F) << "\n");
        igen.visit(F);
        DEBUG(errs() << "\n");

        // First execute static initializers, in the main procedure.
        if (F.hasName() && SmackOptions::isEntryPoint(F.getName())) {
          P->insert(Stmt::call(Naming::INITIALIZE_PROC));

        } else if (naming.get(F).find(Naming::INIT_FUNC_PREFIX) == 0)
          rep.addInitFunc(&F);
      }
      DEBUG(errs() << "Finished analyzing function: " << naming.get(F) << "\n\n");
    }

    // MODIFIES
    // ... to do below, after memory splitting is determined.
  }

  auto ds = rep.auxiliaryDeclarations();
  decls.insert(decls.end(), ds.begin(), ds.end());
  decls.insert(decls.end(), rep.getInitFuncs());

  // NOTE we must do this after instruction generation, since we would not
  // otherwise know how many regions to declare.
  program.appendPrelude(rep.getPrelude());

  std::list<Decl*> kill_list;
  for (auto D : program) {
    if (auto P = dyn_cast<ProcDecl>(D)) {
      if (D->getName().find(Naming::CONTRACT_EXPR) != std::string::npos) {
        decls.insert(decls.end(), Decl::code(P));
        kill_list.push_back(P);
      }
    }
  }
  for (auto D : kill_list)
    decls.erase(std::remove(decls.begin(), decls.end(), D), decls.end());
}
Exemple #4
0
/*
 * generate:
 *	res = n;
 * simplifies and calls gmove.
 */
void
cgen(Node *n, Node *res)
{
	Node *nl, *nr, *r;
	Node n1, n2;
	int a, f;
	Prog *p1, *p2, *p3;
	Addr addr;

	if(debug['g']) {
		dump("\ncgen-n", n);
		dump("cgen-res", res);
	}
	if(n == N || n->type == T)
		goto ret;

	if(res == N || res->type == T)
		fatal("cgen: res nil");

	while(n->op == OCONVNOP)
		n = n->left;

	// inline slices
	if(cgen_inline(n, res))
		goto ret;

	if(n->ullman >= UINF) {
		if(n->op == OINDREG)
			fatal("cgen: this is going to misscompile");
		if(res->ullman >= UINF) {
			tempname(&n1, n->type);
			cgen(n, &n1);
			cgen(&n1, res);
			goto ret;
		}
	}

	if(isfat(n->type)) {
		sgen(n, res, n->type->width);
		goto ret;
	}

	if(!res->addable) {
		if(n->ullman > res->ullman) {
			regalloc(&n1, n->type, res);
			cgen(n, &n1);
			if(n1.ullman > res->ullman) {
				dump("n1", &n1);
				dump("res", res);
				fatal("loop in cgen");
			}
			cgen(&n1, res);
			regfree(&n1);
			goto ret;
		}

		if(res->ullman >= UINF)
			goto gen;

		if(complexop(n, res)) {
			complexgen(n, res);
			goto ret;
		}

		f = 1;	// gen thru register
		switch(n->op) {
		case OLITERAL:
			if(smallintconst(n))
				f = 0;
			break;
		case OREGISTER:
			f = 0;
			break;
		}

		if(!iscomplex[n->type->etype]) {
			a = optoas(OAS, res->type);
			if(sudoaddable(a, res, &addr)) {
				if(f) {
					regalloc(&n2, res->type, N);
					cgen(n, &n2);
					p1 = gins(a, &n2, N);
					regfree(&n2);
				} else
					p1 = gins(a, n, N);
				p1->to = addr;
				if(debug['g'])
					print("%P [ignore previous line]\n", p1);
				sudoclean();
				goto ret;
			}
		}

	gen:
		igen(res, &n1, N);
		cgen(n, &n1);
		regfree(&n1);
		goto ret;
	}

	// update addressability for string, slice
	// can't do in walk because n->left->addable
	// changes if n->left is an escaping local variable.
	switch(n->op) {
	case OLEN:
		if(isslice(n->left->type) || istype(n->left->type, TSTRING))
			n->addable = n->left->addable;
		break;
	case OCAP:
		if(isslice(n->left->type))
			n->addable = n->left->addable;
		break;
	}

	if(complexop(n, res)) {
		complexgen(n, res);
		goto ret;
	}

	if(n->addable) {
		gmove(n, res);
		goto ret;
	}

	nl = n->left;
	nr = n->right;

	if(nl != N && nl->ullman >= UINF)
	if(nr != N && nr->ullman >= UINF) {
		tempname(&n1, nl->type);
		cgen(nl, &n1);
		n2 = *n;
		n2.left = &n1;
		cgen(&n2, res);
		goto ret;
	}

	if(!iscomplex[n->type->etype]) {
		a = optoas(OAS, n->type);
		if(sudoaddable(a, n, &addr)) {
			if(res->op == OREGISTER) {
				p1 = gins(a, N, res);
				p1->from = addr;
			} else {
				regalloc(&n2, n->type, N);
				p1 = gins(a, N, &n2);
				p1->from = addr;
				gins(a, &n2, res);
				regfree(&n2);
			}
			sudoclean();
			goto ret;
		}
	}

	switch(n->op) {
	default:
		dump("cgen", n);
		fatal("cgen: unknown op %N", n);
		break;

	// these call bgen to get a bool value
	case OOROR:
	case OANDAND:
	case OEQ:
	case ONE:
	case OLT:
	case OLE:
	case OGE:
	case OGT:
	case ONOT:
		p1 = gbranch(AJMP, T);
		p2 = pc;
		gmove(nodbool(1), res);
		p3 = gbranch(AJMP, T);
		patch(p1, pc);
		bgen(n, 1, p2);
		gmove(nodbool(0), res);
		patch(p3, pc);
		goto ret;

	case OPLUS:
		cgen(nl, res);
		goto ret;

	// unary
	case OCOM:
		a = optoas(OXOR, nl->type);
		regalloc(&n1, nl->type, N);
		cgen(nl, &n1);
		nodconst(&n2, nl->type, -1);
		gins(a, &n2, &n1);
		gmove(&n1, res);
		regfree(&n1);
		goto ret;

	case OMINUS:
		if(isfloat[nl->type->etype]) {
			nr = nodintconst(-1);
			convlit(&nr, n->type);
			a = optoas(OMUL, nl->type);
			goto sbop;
		}
		a = optoas(n->op, nl->type);
		goto uop;

	// symmetric binary
	case OAND:
	case OOR:
	case OXOR:
	case OADD:
	case OMUL:
		a = optoas(n->op, nl->type);
		if(a != AIMULB)
			goto sbop;
		cgen_bmul(n->op, nl, nr, res);
		break;

	// asymmetric binary
	case OSUB:
		a = optoas(n->op, nl->type);
		goto abop;

	case OCONV:
		regalloc(&n1, nl->type, res);
		regalloc(&n2, n->type, &n1);
		cgen(nl, &n1);

		// if we do the conversion n1 -> n2 here
		// reusing the register, then gmove won't
		// have to allocate its own register.
		gmove(&n1, &n2);
		gmove(&n2, res);
		regfree(&n2);
		regfree(&n1);
		break;

	case ODOT:
	case ODOTPTR:
	case OINDEX:
	case OIND:
	case ONAME:	// PHEAP or PPARAMREF var
		igen(n, &n1, res);
		gmove(&n1, res);
		regfree(&n1);
		break;

	case OLEN:
		if(istype(nl->type, TMAP) || istype(nl->type, TCHAN)) {
			// map and chan have len in the first 32-bit word.
			// a zero pointer means zero length
			regalloc(&n1, types[tptr], res);
			cgen(nl, &n1);

			nodconst(&n2, types[tptr], 0);
			gins(optoas(OCMP, types[tptr]), &n1, &n2);
			p1 = gbranch(optoas(OEQ, types[tptr]), T);

			n2 = n1;
			n2.op = OINDREG;
			n2.type = types[TINT32];
			gmove(&n2, &n1);

			patch(p1, pc);

			gmove(&n1, res);
			regfree(&n1);
			break;
		}
		if(istype(nl->type, TSTRING) || isslice(nl->type)) {
			// both slice and string have len one pointer into the struct.
			// a zero pointer means zero length
			regalloc(&n1, types[tptr], res);
			agen(nl, &n1);
			n1.op = OINDREG;
			n1.type = types[TUINT32];
			n1.xoffset = Array_nel;
			gmove(&n1, res);
			regfree(&n1);
			break;
		}
		fatal("cgen: OLEN: unknown type %lT", nl->type);
		break;

	case OCAP:
		if(istype(nl->type, TCHAN)) {
			// chan has cap in the second 32-bit word.
			// a zero pointer means zero length
			regalloc(&n1, types[tptr], res);
			cgen(nl, &n1);

			nodconst(&n2, types[tptr], 0);
			gins(optoas(OCMP, types[tptr]), &n1, &n2);
			p1 = gbranch(optoas(OEQ, types[tptr]), T);

			n2 = n1;
			n2.op = OINDREG;
			n2.xoffset = 4;
			n2.type = types[TINT32];
			gmove(&n2, &n1);

			patch(p1, pc);

			gmove(&n1, res);
			regfree(&n1);
			break;
		}
		if(isslice(nl->type)) {
			regalloc(&n1, types[tptr], res);
			agen(nl, &n1);
			n1.op = OINDREG;
			n1.type = types[TUINT32];
			n1.xoffset = Array_cap;
			gmove(&n1, res);
			regfree(&n1);
			break;
		}
		fatal("cgen: OCAP: unknown type %lT", nl->type);
		break;

	case OADDR:
		agen(nl, res);
		break;

	case OCALLMETH:
		cgen_callmeth(n, 0);
		cgen_callret(n, res);
		break;

	case OCALLINTER:
		cgen_callinter(n, res, 0);
		cgen_callret(n, res);
		break;

	case OCALLFUNC:
		cgen_call(n, 0);
		cgen_callret(n, res);
		break;

	case OMOD:
	case ODIV:
		if(isfloat[n->type->etype]) {
			a = optoas(n->op, nl->type);
			goto abop;
		}
		cgen_div(n->op, nl, nr, res);
		break;

	case OLSH:
	case ORSH:
		cgen_shift(n->op, nl, nr, res);
		break;
	}
	goto ret;

sbop:	// symmetric binary
	if(nl->ullman < nr->ullman) {
		r = nl;
		nl = nr;
		nr = r;
	}

abop:	// asymmetric binary
	if(nl->ullman >= nr->ullman) {
		regalloc(&n1, nl->type, res);
		cgen(nl, &n1);

		if(sudoaddable(a, nr, &addr)) {
			p1 = gins(a, N, &n1);
			p1->from = addr;
			gmove(&n1, res);
			sudoclean();
			regfree(&n1);
			goto ret;
		}
		regalloc(&n2, nr->type, N);
		cgen(nr, &n2);
	} else {
		regalloc(&n2, nr->type, N);
		cgen(nr, &n2);
		regalloc(&n1, nl->type, res);
		cgen(nl, &n1);
	}
	gins(a, &n2, &n1);
	gmove(&n1, res);
	regfree(&n1);
	regfree(&n2);
	goto ret;

uop:	// unary
	regalloc(&n1, nl->type, res);
	cgen(nl, &n1);
	gins(a, N, &n1);
	gmove(&n1, res);
	regfree(&n1);
	goto ret;

ret:
	;
}
Exemple #5
0
void DoExecuteCommand(struct Kommando *cmd) {
  switch(cmd->nummer) {
  case 201: Cmd_GoConf(); break; // ga(argument);
  case 210: Cmd_NextText(); break;
  case 211: Cmd_NextReply(); break;
  case 221: Cmd_NextConf(); break;
  case 222: GoConf(MAILBOX_CONFID); break;
  case 301: Cmd_Logout(); break;
  case 101: listmot(argument); break;
  case 102: Cmd_ListUsers(); break;
  case 103: listmed(); break;
  case 104: sendfile("NiKom:Texter/ListaKommandon.txt"); break;
  case 105: listratt(); break;
  case 106: listasenaste(); break;
  case 107: listnyheter(); break;
  case 108: listaarende(); break;
  case 109: listflagg(); break;
  case 111: listarea(); break;
  case 112: listnyckel(); break;
  case 113: listfiler(); break;
  case 114: listagrupper(); break;
  case 115: listgruppmed(); break;
  case 116: listabrev(); break;
  case 202: skriv(); break;
  case 203: Cmd_Reply(); break;
  case 204: personlig(); break;
  case 205: skickabrev(); break;
  case 206: igen(); break;
  case 207: atersekom(); break;
  case 208: medlem(argument); break;
  case 209: uttrad(argument); break;
  case 212: Cmd_Read(); break;
  case 213: endast(); break;
  case 214: Cmd_SkipReplies(); break;
  case 215: addratt(); break;
  case 216: subratt(); break;
  case 217: radtext(); break;
  case 218: skapmot(); break;
  case 219: radmot(); break;
  case 220: var(mote2); break;
  case 223: andmot(); break;
  case 224: radbrev(); break;
  case 225: rensatexter(); break;
  case 226: rensabrev(); break;
  case 227: gamlatexter(); break;
  case 228: gamlabrev(); break;
  case 229: dumpatext(); break;
  case 231: movetext(); break;
  case 232: motesstatus(); break;
  case 233: hoppaarende(); break;
  case 234: flyttagren(); break;
  case 235: Cmd_FootNote(); break;
  case 236: Cmd_Search(); break;
  case 237: Cmd_Like(); break;
  case 238: Cmd_Dislike(); break;
  case 302: sendfile("NiKom:Texter/Help.txt"); break;
  case 303: Cmd_ChangeUser(); break;
  case 304: slaav(); break;
  case 305: slapa(); break;
  case 306: tiden(); break;
  case 307: ropa(); break;
  case 308: Cmd_Status(); break;
  case 309: Cmd_DeleteUser(); break;
  case 310: vilka(); break;
  case 311: visainfo(); break;
  case 312: getconfig(); break;
  case 313: writeinfo(); break;
  case 314: sag(); break;
  case 315: skrivlapp(); break;
  case 316: radlapp(); break;
  case 317: grab(); break;
  case 318: skapagrupp(); break;
  case 319: andragrupp(); break;
  case 320: raderagrupp(); break;
  case 321: adderagruppmedlem(); break;
  case 322: subtraheragruppmedlem(); break;
  case 323: DisplayVersionInfo(); break;
  case 324: alias(); break;
  case 325: Cmd_ReLogin(); break;
  case 326: bytnodtyp(); break;
  case 327: bytteckenset(); break;
  case 328: SaveCurrentUser(inloggad, nodnr); break;
  case 329: Cmd_ChangeLanguage(); break;
  case 401: bytarea(); break;
  case 402: filinfo(); break;
  case 403: upload(); break;
  case 404: download(); break;
  case 405: Cmd_CreateArea(); break;
  case 406: radarea(); break;
  case 407: andraarea(); break;
  case 408: skapafil(); break;
  case 409: radfil(); break;
  case 410: andrafil(); break;
  case 411: lagrafil(); break;
  case 412: flyttafil(); break;
  case 413: sokfil(); break;
  case 414: filstatus(); break;
  case 415: typefil(); break;
  case 416: nyafiler(); break;
  case 417: validerafil(); break;
  default:
    if(cmd->nummer >= 500) {
      sendrexx(cmd->nummer);
    } else {
      LogEvent(SYSTEM_LOG, ERROR,
               "Trying to execute undefined command %d", cmd->nummer);
      DisplayInternalError();
    }
  }
}