Exemplo n.º 1
0
void IRGenerator::accept(MatchStmt& stmt)
{
    FNTRACE();

    Value* cond = codegen(stmt.condition());
    BasicBlock* contBlock = createBlock("match.cont");
    MatchInstr* matchInstr = createMatch(stmt.op(), cond);

    for (const MatchCase& one: stmt.cases()) {
        BasicBlock* bb = createBlock("match.case");
        setInsertPoint(bb);
        codegen(one.second.get());
        createBr(contBlock);

        for (auto& labelNode: one.first) {
            Constant* label = getConstant(labelNode.get());
            matchInstr->addCase(label, bb);
        }
    }

    if (stmt.elseStmt()) {
        BasicBlock* elseBlock = createBlock("match.else");
        setInsertPoint(elseBlock);
        codegen(stmt.elseStmt());
        createBr(contBlock);

        matchInstr->setElseBlock(elseBlock);
    }

    setInsertPoint(contBlock);
}
Exemplo n.º 2
0
void IRGenerator::accept(MatchStmt& stmt)
{
    FNTRACE();

    // TODO

    BasicBlock* contBlock = createBlock("match.cont");
    MatchInstr* matchInstr = new MatchInstr(stmt.op());

    Value* cond = codegen(stmt.condition());
    matchInstr->setCondition(cond);

    for (const MatchCase& one: stmt.cases()) {
        Value* label;
        if (auto e = dynamic_cast<StringExpr*>(one.first.get()))
            label = get(e->value());
        else if (auto e = dynamic_cast<RegExpExpr*>(one.first.get()))
            label = get(e->value());
        else {
            reportError("FIXME: Invalid (unsupported) literal type <%s> in match case.",
                    tos(one.first->getType()).c_str());
            result_ = nullptr;
            return;
        }

        BasicBlock* bb = createBlock("match.case");
        setInsertPoint(bb);
        codegen(one.second.get());
        createBr(contBlock);

        matchInstr->addCase(label, bb);
    }

    if (stmt.elseStmt()) {
        BasicBlock* elseBlock = createBlock("match.else");
        setInsertPoint(elseBlock);
        codegen(stmt.elseStmt());
        createBr(contBlock);

        matchInstr->setElseBlock(elseBlock);
    }

    setInsertPoint(contBlock);
}