Handle new throw expressions

This commit is contained in:
Martin Kinkelin 2022-02-19 19:28:08 +01:00
parent 78681e12e1
commit 5c9f1e5fd6
4 changed files with 32 additions and 14 deletions

View file

@ -270,7 +270,7 @@ LLValue *DtoAllocaDump(LLValue *val, LLType *asType, int alignment,
} }
/****************************************************************************** /******************************************************************************
* ASSERT HELPER * ASSERT HELPERS
******************************************************************************/ ******************************************************************************/
void DtoAssert(Module *M, const Loc &loc, DValue *msg) { void DtoAssert(Module *M, const Loc &loc, DValue *msg) {
@ -339,6 +339,21 @@ void DtoCAssert(Module *M, const Loc &loc, LLValue *msg) {
gIR->ir->CreateUnreachable(); gIR->ir->CreateUnreachable();
} }
/******************************************************************************
* THROW HELPER
******************************************************************************/
void DtoThrow(const Loc &loc, DValue *e) {
LLFunction *fn = getRuntimeFunction(loc, gIR->module, "_d_throw_exception");
LLValue *arg = DtoBitCast(DtoRVal(e), fn->getFunctionType()->getParamType(0));
gIR->CreateCallOrInvoke(fn, arg);
gIR->ir->CreateUnreachable();
llvm::BasicBlock *bb = gIR->insertBB("afterthrow");
gIR->ir->SetInsertPoint(bb);
}
/****************************************************************************** /******************************************************************************
* MODULE FILE NAME * MODULE FILE NAME
******************************************************************************/ ******************************************************************************/

View file

@ -63,6 +63,8 @@ LLValue *DtoAllocaDump(LLValue *val, LLType *asType, int alignment = 0,
void DtoAssert(Module *M, const Loc &loc, DValue *msg); void DtoAssert(Module *M, const Loc &loc, DValue *msg);
void DtoCAssert(Module *M, const Loc &loc, LLValue *msg); void DtoCAssert(Module *M, const Loc &loc, LLValue *msg);
void DtoThrow(const Loc &loc, DValue *e);
// returns module file name // returns module file name
LLConstant *DtoModuleFileName(Module *M, const Loc &loc); LLConstant *DtoModuleFileName(Module *M, const Loc &loc);

View file

@ -934,19 +934,7 @@ public:
emitCoverageLinecountInc(stmt->loc); emitCoverageLinecountInc(stmt->loc);
assert(stmt->exp); assert(stmt->exp);
DValue *e = toElemDtor(stmt->exp); DtoThrow(stmt->loc, toElemDtor(stmt->exp));
llvm::Function *fn =
getRuntimeFunction(stmt->loc, irs->module, "_d_throw_exception");
LLValue *arg =
DtoBitCast(DtoRVal(e), fn->getFunctionType()->getParamType(0));
irs->CreateCallOrInvoke(fn, arg);
irs->ir->CreateUnreachable();
// TODO: Should not be needed.
llvm::BasicBlock *bb = irs->insertBB("afterthrow");
irs->ir->SetInsertPoint(bb);
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////

View file

@ -1587,6 +1587,19 @@ public:
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
void visit(ThrowExp *e) override {
IF_LOG Logger::print("ThrowExp::toElem: %s\n", e->toChars());
LOG_SCOPE;
auto &PGO = gIR->funcGen().pgo;
PGO.setCurrentStmt(e);
DtoThrow(e->loc, toElem(e->e1));
result = new DNullValue(e->type, llvm::UndefValue::get(DtoType(e->type)));
}
//////////////////////////////////////////////////////////////////////////////
void visit(AssertExp *e) override { void visit(AssertExp *e) override {
IF_LOG Logger::print("AssertExp::toElem: %s\n", e->toChars()); IF_LOG Logger::print("AssertExp::toElem: %s\n", e->toChars());
LOG_SCOPE; LOG_SCOPE;