Skip to content

Commit 614e981

Browse files
committed
Fix "Terminator found in the middle of a basic block!"
1 parent a8738a7 commit 614e981

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

src/backend/irgen-decl.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ void IRGenerator::emitFunctionBody(const FunctionDecl& decl, Function& function)
5959
}
6060
}
6161

62-
for (auto& stmt : *decl.body) {
63-
emitStmt(*stmt);
64-
}
65-
62+
emitStmts(*decl.body);
6663
endScope();
6764

6865
if (insertBlock->body.empty() || !llvm::isa<ReturnInst>(insertBlock->body.back())) {

src/backend/irgen-stmt.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ void IRGenerator::emitReturnStmt(const ReturnStmt& stmt) {
1717

1818
void IRGenerator::emitBlock(llvm::ArrayRef<Stmt*> stmts, BasicBlock* continuation) {
1919
beginScope();
20-
21-
for (const auto& stmt : stmts) {
22-
emitStmt(*stmt);
23-
if (stmt->isReturnStmt() || stmt->isBreakStmt() || stmt->isContinueStmt()) break;
24-
}
25-
20+
emitStmts(stmts);
2621
endScope();
2722

2823
if (insertBlock->body.empty() || !insertBlock->body.back()->isTerminator()) {
@@ -148,11 +143,7 @@ void IRGenerator::emitContinueStmt(const ContinueStmt&) {
148143

149144
void IRGenerator::emitCompoundStmt(const CompoundStmt& compoundStmt) {
150145
beginScope();
151-
152-
for (auto& stmt : compoundStmt.body) {
153-
emitStmt(*stmt);
154-
}
155-
146+
emitStmts(compoundStmt.body);
156147
endScope();
157148
}
158149

@@ -196,3 +187,10 @@ void IRGenerator::emitStmt(const Stmt& stmt) {
196187
break;
197188
}
198189
}
190+
191+
void IRGenerator::emitStmts(llvm::ArrayRef<Stmt*> stmts) {
192+
for (Stmt* stmt : stmts) {
193+
emitStmt(*stmt);
194+
if (stmt->isReturnStmt() || stmt->isBreakStmt() || stmt->isContinueStmt()) break;
195+
}
196+
}

src/backend/irgen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct IRGenerator {
9898
Value* emitAssignmentLHS(const Expr& lhs);
9999
void emitCompoundStmt(const CompoundStmt& stmt);
100100
void emitStmt(const Stmt& stmt);
101+
void emitStmts(llvm::ArrayRef<Stmt*> stmts);
101102
void emitDecl(const Decl& decl);
102103
void emitFunctionDecl(const FunctionDecl& decl);
103104
Value* emitVarDecl(const VarDecl& decl);

src/backend/llvm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ void LLVMGenerator::codegenFunctionBody(const Function* function, llvm::Function
141141
}
142142

143143
for (auto* inst : block->body) {
144-
getValue(inst);
144+
auto llvmValue = codegenInst(inst);
145+
generatedValues.emplace(inst, llvmValue);
145146
}
146147
}
147148

0 commit comments

Comments
 (0)