TEST(Board, block_refCount) {
  int init_array[] = {
    3,3,3,3,3,3,3,3,3,3,3,
    3,0,1,1,1,0,0,0,0,0,3,
    3,1,2,2,1,0,0,0,2,0,3,
    3,0,1,2,2,1,0,0,0,0,3,
    3,0,1,2,0,0,0,0,0,0,3,
    3,0,0,1,0,1,0,0,0,0,3,
    3,0,0,0,0,0,0,0,0,0,3,
    3,2,2,2,0,0,0,0,0,0,3,
    3,1,1,1,2,0,0,0,1,0,3,
    3,1,2,0,0,0,0,0,0,0,3,
    3,3,3,3,3,3,3,3,3,3,3
  };
  Board board(9, init_array);

  EXPECT_EQ(1, board.getBelongBlock(0,1)->refCount());
  EXPECT_EQ(4, board.getBelongBlock(1,0)->refCount());
  EXPECT_EQ(1, board.getBelongBlock(2,4)->refCount());
  EXPECT_EQ(1, board.getBelongBlock(4,4)->refCount());

  board.put(3,4,1);
  EXPECT_EQ(4, board.getBelongBlock(3,4)->refCount());
  board.undo();
  EXPECT_EQ(1, board.getBelongBlock(2,4)->refCount());
  EXPECT_EQ(1, board.getBelongBlock(4,4)->refCount());

  BlockPtr oldBlock = board.getBelongBlock(1,1);
  EXPECT_EQ(5, oldBlock->refCount());
  board.put(3,3,1);
  EXPECT_EQ(1, oldBlock->refCount());
  board.undo();
  EXPECT_EQ(5, oldBlock->refCount());
}
Exemple #2
0
RegionDesc::Block* RegionDesc::addBlock(SrcKey      sk,
                                        int         length,
                                        FPInvOffset spOffset) {
  m_blocks.push_back(
    std::make_shared<Block>(sk.func(), sk.resumed(), sk.offset(), length,
                            spOffset));
  BlockPtr block = m_blocks.back();
  m_data[block->id()] = BlockData(block);
  return block.get();
}
Exemple #3
0
StatementPtr desugarForStatement(ForPtr x, CompilerState* cst) {
    IdentifierPtr exprVar = Identifier::get("%expr", x->location);
    IdentifierPtr iterVar = Identifier::get("%iter", x->location);
    IdentifierPtr valueVar = Identifier::get("%value", x->location);

    BlockPtr block = new Block();
    block->location = x->body->location;
    vector<StatementPtr> &bs = block->statements;
    BindingPtr exprBinding = new Binding(FORWARD, identV(exprVar), new ExprList(x->expr));
    exprBinding->location = x->body->location;
    bs.push_back(exprBinding.ptr());

    CallPtr iteratorCall = new Call(operator_expr_iterator(cst), new ExprList());
    iteratorCall->location = x->body->location;
    ExprPtr exprName = new NameRef(exprVar);
    exprName->location = x->location;
    iteratorCall->parenArgs->add(exprName);
    BindingPtr iteratorBinding = new Binding(FORWARD,
        identV(iterVar),
        new ExprList(iteratorCall.ptr()));
    iteratorBinding->location = x->body->location;
    bs.push_back(iteratorBinding.ptr());

    vector<StatementPtr> valueStatements;
    CallPtr nextValueCall = new Call(operator_expr_nextValue(cst), new ExprList());
    nextValueCall->location = x->body->location;
    ExprPtr iterName = new NameRef(iterVar);
    iterName->location = x->body->location;
    nextValueCall->parenArgs->add(iterName);
    BindingPtr nextValueBinding = new Binding(VAR,
        identV(valueVar),
        new ExprList(nextValueCall.ptr()));
    nextValueBinding->location = x->body->location;
    valueStatements.push_back(nextValueBinding.ptr());

    CallPtr hasValueCall = new Call(operator_expr_hasValueP(cst), new ExprList());
    hasValueCall->location = x->body->location;
    ExprPtr valueName = new NameRef(valueVar);
    valueName->location = x->body->location;
    hasValueCall->parenArgs->add(valueName);

    CallPtr getValueCall = new Call(operator_expr_getValue(cst), new ExprList());
    getValueCall->location = x->body->location;
    getValueCall->parenArgs->add(valueName);
    BlockPtr whileBody = new Block();
    whileBody->location = x->body->location;
    vector<StatementPtr> &ws = whileBody->statements;
    ws.push_back(new Binding(FORWARD, identVtoFormalV(x->variables), new ExprList(getValueCall.ptr())));
    ws.push_back(x->body);

    StatementPtr whileStmt = new While(valueStatements, hasValueCall.ptr(), whileBody.ptr());
    whileStmt->location = x->location;
    bs.push_back(whileStmt);
    return block.ptr();
}
Exemple #4
0
StatementPtr desugarSwitchStatement(SwitchPtr x, CompilerState* cst) {
    BlockPtr block = new Block();
    block->location = x->location;

    block->statements.insert(block->statements.end(),
        x->exprStatements.begin(), x->exprStatements.end());

    // %thing is the value being switched on
    IdentifierPtr thing = Identifier::get("%case", x->expr->location);
    NameRefPtr thingRef = new NameRef(thing);
    thingRef->location = x->expr->location;

    // initialize %case
    {
        BindingPtr b = new Binding(FORWARD, identV(thing), new ExprList(x->expr));
        b->location = x->expr->location;
        block->statements.push_back(b.ptr());
    }

    ExprPtr caseCallable = operator_expr_caseP(cst);

    StatementPtr root;
    StatementPtr *nextPtr = &root;

    // dispatch logic
    for (size_t i = 0; i < x->caseBlocks.size(); ++i) {
        CaseBlockPtr caseBlock = x->caseBlocks[i];

        ExprListPtr caseArgs = new ExprList(thingRef.ptr());
        ExprPtr caseCompareArgs = new Paren(caseBlock->caseLabels);
        caseCompareArgs->location = caseBlock->location;
        caseArgs->add(caseCompareArgs);

        ExprPtr condition = new Call(caseCallable, caseArgs);
        condition->location = caseBlock->location;

        IfPtr ifStmt = new If(condition, caseBlock->body);
        ifStmt->location = caseBlock->location;
        *nextPtr = ifStmt.ptr();
        nextPtr = &(ifStmt->elsePart);
    }

    if (x->defaultCase.ptr())
        *nextPtr = x->defaultCase;
    else
        *nextPtr = NULL;

    block->statements.push_back(root);

    return block.ptr();
}
Exemple #5
0
point_count_t OciReader::readPointMajor(PointView& view,
    BlockPtr block, point_count_t numPts)
{
    size_t numRemaining = block->numRemaining();
    PointId nextId = view.size();
    point_count_t numRead = 0;

    if (m_compression)
    {
#ifdef PDAL_HAVE_LAZPERF
        LazPerfBuf buf(block->chunk);
        LazPerfDecompressor<LazPerfBuf> decompressor(buf, dbDimTypes());

        std::vector<char> ptBuf(decompressor.pointSize());
        while (numRead < numPts && numRemaining > 0)
        {
            point_count_t numWritten =
                decompressor.decompress(ptBuf.data(), ptBuf.size());
            writePoint(view, nextId, ptBuf.data());
            if (m_cb)
                m_cb(view, nextId);
            numRemaining--;
            nextId++;
            numRead++;
        }
#else
        throw pdal_error("Can't decompress without LAZperf.");
#endif
    }
    else
    {
        char *pos = seekPointMajor(block);
        while (numRead < numPts && numRemaining > 0)
        {
            writePoint(view, nextId, pos);

            if (m_cb)
                m_cb(view, nextId);

            pos += packedPointSize();
            numRemaining--;
            nextId++;
            numRead++;
        }
    }
    block->setNumRemaining(numRemaining);
    return numRead;
}
Exemple #6
0
void Block::resolveTargets(FlowPtr f) {
	//iterate over the hard targets of this block
	std::set<uint64_t>::iterator	it = this->HardTargets.begin();
	std::set<uint64_t>::iterator	e = this->HardTargets.end();
	
	while( it != e ) {
		unsigned long long target = *it;
		//resolve each hard target to a block in the flow
		BlockPtr b = f->getBlockWithAddr(target);

		//insert this blocks ID into the list of targeted IDs
		this->TargetIDs.push_back(b->getBlockId());

		++it;
	}
	return;
}
Exemple #7
0
    static void jitStatements(llvm::ArrayRef<StatementPtr> statements)
    {
        if (statements.empty()) {
            return;
        }

        if (printAST) {
            for (size_t i = 0; i < statements.size(); ++i) {
                llvm::errs() << statements[i] << "\n";
            }
        }

        IdentifierPtr fun = Identifier::get(newFunctionName());

        BlockPtr funBody = new Block(statements);
        ExternalProcedurePtr entryProc =
                new ExternalProcedure(NULL,
                                      fun,
                                      PRIVATE,
                                      vector<ExternalArgPtr>(),
                                      false,
                                      NULL,
                                      funBody.ptr(),
                                      new ExprList());

        entryProc->env = module->env;

        codegenBeforeRepl(module);
        try {
            codegenExternalProcedure(entryProc, true);
        }
        catch (std::exception) {
            return;
        }

        llvm::Function* ctor;
        llvm::Function* dtor;
        codegenAfterRepl(ctor, dtor);

        engine->runFunction(ctor, std::vector<llvm::GenericValue>());

        void* dtorLlvmFun = engine->getPointerToFunction(dtor);
        typedef void (*PFN)();
        atexit((PFN)(uintptr_t)dtorLlvmFun);
        engine->runFunction(entryProc->llvmFunc, std::vector<llvm::GenericValue>());
    }
Exemple #8
0
BlockPtr Flow::getBlockWithAddr(unsigned long long addr) {
	BlockPtr b;
	std::set<BlockPtr>::iterator	it = this->blocks.begin();
	std::set<BlockPtr>::iterator	e = this->blocks.end();

	while( it != e ) {
		BlockPtr n = *it;

		if( n->getBlockBase() <= addr && n->getBlockEnd() > addr ) {
			b = n;
			break;
		}
		++it;
	}

	return b;
}
Exemple #9
0
point_count_t OciReader::readPointMajor(PointView& view,
    BlockPtr block, point_count_t numPts)
{
    PointId nextId = view.size();
    point_count_t numRead = 0;

    if (m_compression)
    {
#ifdef PDAL_HAVE_LAZPERF
        auto cb = [this, &view, &nextId, &numRead](char *buf, size_t bufsize)
        {
            writePoint(view, nextId, buf);
            if (m_cb)
                m_cb(view, nextId);
            nextId++;
            numRead++;
        };
        const char *src = reinterpret_cast<const char *>(block->chunk.data());
        size_t srcsize = block->chunk.size();
        LazPerfDecompressor(cb, dbDimTypes(), block->numRemaining()).
            decompress(src, srcsize);
#else
        throwError("Can't decompress without LAZperf.");
#endif
    }
    else
    {
        char *pos = seekPointMajor(block);

        size_t cnt = block->numRemaining();
        while (numRead < numPts && cnt--)
        {
            writePoint(view, nextId, pos);

            if (m_cb)
                m_cb(view, nextId);

            pos += packedPointSize();
            nextId++;
            numRead++;
        }
    }
    block->setNumRemaining(block->numRemaining() - numRead);
    return numRead;
}
Exemple #10
0
bool Flow::isAddrInFlowCode(unsigned long long addr) {
	bool found = false;
	std::set<BlockPtr>::iterator	it = this->blocks.begin();
	std::set<BlockPtr>::iterator e = this->blocks.end();

	while( it != e ) {
		BlockPtr    b = *it;

		//if( b->overlaps(addr) ) {
        if( b->getBlockBase() == addr ) {
			found = true;
			break;
		}

		++it;
	}

	return found;
}
Exemple #11
0
void eatBlob(unsigned char *blob, unsigned long blobLen, void *ctx) {
	vector<BlockPtr>	blocks;

    std::cout << "eating blob" << std::endl;
	if( convertBlobToBlocks(ctx, blob, blobLen, blocks) ) {
		//print out the block
		std::vector<BlockPtr>::iterator	it = blocks.begin();
		std::vector<BlockPtr>::iterator	e = blocks.end();

		std::cout << "Dumping blocks" << std::endl;
		while( it != e ) {
			BlockPtr    b = *it;
			//std::cout << "Block at base " << std::hex << b->getBlockBase() << std::dec << std::endl;
			std::cout << b->printBlock() << std::endl;

			++it;
		}
	}
	return;
}
Exemple #12
0
 void removeRedundantBlocks(BlockPtr b) {
     StatementList& children = b->getChildren();
     for (size_t i = 0; i < children.size(); ++i) {
         if (REN_DYNAMIC_CAST_PTR(ib, Block, children[i])) {
             removeRedundantBlocks(ib);
             StatementList sl = ib->getChildren();
             children.erase(children.begin() + i);
             children.insert(children.begin() + i, sl.begin(), sl.end());
             --i;
         }
     }
 }
Exemple #13
0
int BlockLuaMetaData::Index(lua_State * L, const BlockPtr & ptr,
									  const std::string & index)
{
	if (index == "Code")
	{
		lua_pushstring(L, ptr->GetCode().c_str());
		return 1;
	}
	else if (index == "Id")
	{
		lua_pushstring(L, ptr->GetId().c_str());
		return 1;
	}
	else if (index == "ImportedNodes")
	{
		luaL_error(L, "Not implemented currently.");
		// NodeListPtr list = ptr->GetImportedNodes();
		// NodeListLuaMetaData::PutInstanceOnStack(L, list);
		return 1;
	}
	return 0;
}
Exemple #14
0
// Read a block (set of points) from the database.
bool OciReader::readOci(Statement stmt, BlockPtr block)
{
    if (!block->fetched())
    {
        if (!stmt->Fetch())
        {
            m_atEnd = true;
            return false;
        }
        block->setFetched();
    }
    // Read the points from the blob in the row.
    readBlob(stmt, block);
    XMLSchema *s = findSchema(stmt, block);
    updateSchema(*s);
    MetadataNode comp = s->getMetadata().findChild("compression");
    m_compression = (comp.value() == "lazperf");

    block->reset();
    block->clearFetched();
    return true;
}
TEST(Board, refCount_test_for_snapshot) {
  int init_array[] = {
    3,3,3,3,3,3,3,3,3,3,3,
    3,0,1,1,1,0,0,0,0,0,3,
    3,1,2,2,1,0,0,0,2,0,3,
    3,0,1,2,2,1,1,0,0,0,3,
    3,0,1,2,0,0,0,0,0,0,3,
    3,0,0,1,1,1,0,0,0,0,3,
    3,0,0,0,0,0,0,0,0,0,3,
    3,0,0,0,0,0,0,0,0,0,3,
    3,0,0,1,0,0,0,0,1,0,3,
    3,0,0,0,0,0,0,0,0,0,3,
    3,3,3,3,3,3,3,3,3,3,3
  };
  Board board(9, init_array);

  board.put(5,3,2);
  EXPECT_EQ(2, board.getBelongBlock(5,3)->refCount());
  board.put(5,4,1);
  EXPECT_EQ(5, board.getBelongBlock(5,4)->refCount());
  board.put(3,3,2);
  EXPECT_EQ(7, board.getBelongBlock(3,3)->refCount());
  BlockPtr old = board.getBelongBlock(3,3);
  board.put(4,3,1);
  EXPECT_EQ(9, board.getBelongBlock(4,3)->refCount());
  EXPECT_EQ(2, old->refCount());
  board.takeSnapshot();
  board.put(3,3,2);
  board.put(3,2,1);
  board.restoreStateFromSnapshot();
  EXPECT_EQ(9, board.getBelongBlock(4,3)->refCount());
  board.undo();
  EXPECT_EQ(2, board.getBelongBlock(4,2)->refCount());
  EXPECT_EQ(5, board.getBelongBlock(4,4)->refCount());
  EXPECT_EQ(7, board.getBelongBlock(3,3)->refCount());
}
Exemple #16
0
point_count_t OciReader::readDimMajor(PointView& view, BlockPtr block,
    point_count_t numPts)
{
    using namespace Dimension;

    point_count_t numRemaining = block->numRemaining();
    PointId startId = view.size();
    point_count_t blockRemaining = numRemaining;
    point_count_t numRead = 0;

    DimTypeList dims = dbDimTypes();
    for (auto di = dims.begin(); di != dims.end(); ++di)
    {
        PointId nextId = startId;
        char *pos = seekDimMajor(*di, block);
        blockRemaining = numRemaining;
        numRead = 0;
        while (numRead < numPts && blockRemaining > 0)
        {
            writeField(view, pos, *di, nextId);
            pos += Dimension::size(di->m_type);

            if (di->m_id == Id::PointSourceId && m_updatePointSourceId)
                view.setField(Id::PointSourceId, nextId, block->obj_id);

            if (m_cb && di == dims.rbegin().base() - 1)
                m_cb(view, nextId);

            nextId++;
            numRead++;
            blockRemaining--;
        }
    }
    block->setNumRemaining(blockRemaining);
    return numRead;
}
Exemple #17
0
StatementPtr clone(StatementPtr x)
{
    StatementPtr out;

    switch (x->stmtKind) {

    case BLOCK : {
        Block *y = (Block *)x.ptr();
        BlockPtr z = new Block();
        clone(y->statements, z->statements);
        out = z.ptr();
        break;
    }

    case LABEL : {
        Label *y = (Label *)x.ptr();
        out = new Label(y->name);
        break;
    }

    case BINDING : {
        Binding *y = (Binding *)x.ptr();
        vector<FormalArgPtr> args;
        vector<PatternVar> patternVars;
        vector<ObjectPtr> patternTypes;
        clone(y->args, args);
        clone(y->patternVars, patternVars);
        clone(y->patternTypes, patternTypes);
        out = new Binding(y->bindingKind, patternVars, patternTypes, cloneOpt(y->predicate),
            args, clone(y->values), y->hasVarArg);
        break;
    }

    case ASSIGNMENT : {
        Assignment *y = (Assignment *)x.ptr();
        out = new Assignment(clone(y->left), clone(y->right));
        break;
    }

    case INIT_ASSIGNMENT : {
        InitAssignment *y = (InitAssignment *)x.ptr();
        out = new InitAssignment(clone(y->left), clone(y->right));
        break;
    }

    case VARIADIC_ASSIGNMENT : {
        VariadicAssignment *y = (VariadicAssignment *)x.ptr();
        out = new VariadicAssignment(y->op, clone(y->exprs));
        break;
    }

    case GOTO : {
        Goto *y = (Goto *)x.ptr();
        out = new Goto(y->labelName);
        break;
    }

    case RETURN : {
        Return *y = (Return *)x.ptr();
        out = new Return(y->returnKind, clone(y->values));
        break;
    }

    case IF : {
        If *y = (If *)x.ptr();
        vector<StatementPtr> conditionStatements;
        clone(y->conditionStatements, conditionStatements);
        out = new If(conditionStatements, clone(y->condition), clone(y->thenPart),
                     cloneOpt(y->elsePart));
        break;
    }

    case SWITCH : {
        Switch *y = (Switch *)x.ptr();
        vector<StatementPtr> exprStatements;
        clone(y->exprStatements, exprStatements);
        vector<CaseBlockPtr> caseBlocks;
        clone(y->caseBlocks, caseBlocks);
        out = new Switch(exprStatements,
                         clone(y->expr),
                         caseBlocks,
                         cloneOpt(y->defaultCase));
        break;
    }

    case EXPR_STATEMENT : {
        ExprStatement *y = (ExprStatement *)x.ptr();
        out = new ExprStatement(clone(y->expr));
        break;
    }

    case WHILE : {
        While *y = (While *)x.ptr();
        vector<StatementPtr> conditionStatements;
        clone(y->conditionStatements, conditionStatements);
        out = new While(conditionStatements, clone(y->condition), clone(y->body));
        break;
    }

    case BREAK : {
        out = new Break();
        break;
    }

    case CONTINUE : {
        out = new Continue();
        break;
    }

    case FOR : {
        For *y = (For *)x.ptr();
        vector<IdentifierPtr> variables;
        clone(y->variables, variables);
        out = new For(variables, clone(y->expr), clone(y->body));
        break;
    }

    case FOREIGN_STATEMENT : {
        out = x;
        break;
    }

    case TRY : {
        Try *y = (Try *)x.ptr();
        vector<CatchPtr> catchBlocks;
        clone(y->catchBlocks, catchBlocks);
        out = new Try(clone(y->tryBlock), catchBlocks);
        break;
    }

    case THROW : {
        Throw *y = (Throw *)x.ptr();
        Throw *outThrow = new Throw(cloneOpt(y->expr), cloneOpt(y->context));
        // this is weird part: in certain situations
        // clone is called after desugar, and desugaredExpr is modified after desugar
        outThrow->desugaredExpr = cloneOpt(y->desugaredExpr);
        outThrow->desugaredContext = cloneOpt(y->desugaredContext);
        out = outThrow;
        break;
    }

    case STATIC_FOR : {
        StaticFor *y = (StaticFor *)x.ptr();
        out = new StaticFor(y->variable, clone(y->values), clone(y->body));
        break;
    }

    case FINALLY : {
        Finally *y = (Finally *)x.ptr();
        out = new Finally(clone(y->body));
        break;
    }

    case ONERROR : {
        OnError *y = (OnError *)x.ptr();
        out = new OnError(clone(y->body));
        break;
    }

    case UNREACHABLE : {
        out = new Unreachable();
        break;
    }

    case EVAL_STATEMENT : {
        EvalStatement *eval = (EvalStatement *)x.ptr();
        out = new EvalStatement(eval->args);
        break;
    }

    case STATIC_ASSERT_STATEMENT : {
        StaticAssertStatement *staticAssert = (StaticAssertStatement *)x.ptr();
        out = new StaticAssertStatement(clone(staticAssert->cond), clone(staticAssert->message));
        break;
    }

    default :
        assert(false);

    }

    out->location = x->location;
    return out;
}
Exemple #18
0
	bool C2SPing::runBlock(BlockPtr& nBlock)
	{
		nBlock->runInt32(mSecond);
		return true;
	}
Exemple #19
0
StatementPtr desugarCatchBlocks(llvm::ArrayRef<CatchPtr> catchBlocks,
                                CompilerState* cst) {
    assert(!catchBlocks.empty());
    Location firstCatchLocation = catchBlocks.front()->location;
    IdentifierPtr expVar = Identifier::get("%exp", firstCatchLocation);

    CallPtr activeException = new Call(primitive_expr_activeException(cst), new ExprList());
    activeException->location = firstCatchLocation;

    BindingPtr expBinding =
        new Binding(VAR,
                    identVtoFormalV(vector<IdentifierPtr>(1, expVar)),
                    new ExprList(activeException.ptr()));
    expBinding->location = firstCatchLocation;

    bool lastWasAny = false;
    IfPtr lastIf;
    StatementPtr result;
    for (size_t i = 0; i < catchBlocks.size(); ++i) {
        CatchPtr x = catchBlocks[i];
        if (lastWasAny)
            error(x, "unreachable catch block");
        if (x->exceptionType.ptr()) {
            ExprListPtr asTypeArgs = new ExprList(x->exceptionType);
            ExprPtr expVarName = new NameRef(expVar);
            expVarName->location = x->exceptionVar->location;
            asTypeArgs->add(expVarName);
            CallPtr cond = new Call(operator_expr_exceptionIsP(cst), asTypeArgs);
            cond->location = x->exceptionType->location;

            BlockPtr block = new Block();
            block->location = x->location;

            vector<IdentifierPtr> identifiers;
            makeExceptionVars(identifiers, x);

            CallPtr getter = new Call(operator_expr_exceptionAs(cst), asTypeArgs);
            getter->location = x->exceptionVar->location;
            BindingPtr binding =
                new Binding(VAR,
                            identVtoFormalV(identifiers),
                            new ExprList(getter.ptr()));
            binding->location = x->exceptionVar->location;

            BindingPtr exceptionVarForRethrow =
                new Binding(REF,
                            identVtoFormalV(vector<IdentifierPtr>(1, Identifier::get("%exception", x->location))),
                            new ExprList(new NameRef(Identifier::get(x->exceptionVar->str, x->location))));
            exceptionVarForRethrow->location = x->exceptionVar->location;

            block->statements.push_back(binding.ptr());
            block->statements.push_back(exceptionVarForRethrow.ptr());
            block->statements.push_back(x->body);

            IfPtr ifStatement = new If(cond.ptr(), block.ptr());
            ifStatement->location = x->location;
            if (!result)
                result = ifStatement.ptr();
            if (lastIf.ptr())
                lastIf->elsePart = ifStatement.ptr();
            lastIf = ifStatement;
        }
        else {
            BlockPtr block = new Block();
            block->location = x->location;

            vector<IdentifierPtr> identifiers;
            makeExceptionVars(identifiers, x);

            ExprListPtr asAnyArgs = new ExprList(new NameRef(expVar));
            CallPtr getter = new Call(operator_expr_exceptionAsAny(cst),
                                      asAnyArgs);
            getter->location = x->exceptionVar->location;
            BindingPtr binding =
                new Binding(VAR,
                            identVtoFormalV(identifiers),
                            new ExprList(getter.ptr()));
            binding->location = x->exceptionVar->location;

            BindingPtr exceptionVarForRethrow =
                new Binding(REF,
                            identVtoFormalV(vector<IdentifierPtr>(1, Identifier::get("%exception", x->location))),
                            new ExprList(new NameRef(Identifier::get(x->exceptionVar->str, x->location))));
            exceptionVarForRethrow->location = x->exceptionVar->location;

            block->statements.push_back(binding.ptr());
            block->statements.push_back(exceptionVarForRethrow.ptr());
            block->statements.push_back(x->body);

            if (!result)
                result = block.ptr();
            if (lastIf.ptr())
                lastIf->elsePart = block.ptr();

            lastWasAny = true;
            lastIf = NULL;
        }
    }
    assert(result.ptr());
    if (!lastWasAny) {
        assert(lastIf.ptr());
        BlockPtr block = new Block();
        block->location = firstCatchLocation;
        ExprPtr expVarName = new NameRef(expVar);
        expVarName->location = firstCatchLocation;
        ExprListPtr continueArgs = new ExprList(expVarName);
        CallPtr continueException = new Call(operator_expr_continueException(cst),
                                             continueArgs);
        continueException->location = firstCatchLocation;
        StatementPtr stmt = new ExprStatement(continueException.ptr());
        stmt->location = firstCatchLocation;
        block->statements.push_back(stmt);
        block->statements.push_back(new Unreachable());
        lastIf->elsePart = block.ptr();
    }

    BlockPtr block = new Block();
    block->location = firstCatchLocation;
    block->statements.push_back(expBinding.ptr());
    block->statements.push_back(result.ptr());

    return block.ptr();
}