@@ -281,34 +281,6 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
281
281
builder->SetInsertPoint (bb);
282
282
}
283
283
284
- // Note: `create_if_else` and `create_loop` are optional APIs
285
- // that do not have to be used. Many times, for more complicated
286
- // things, it might be more readable to just use the LLVM API
287
- // without any extra layer on top. In some other cases, it might
288
- // be more readable to use this abstraction.
289
- // The `if_block` and `else_block` must generate one or more blocks. In
290
- // addition, the `if_block` must not be terminated, we terminate it
291
- // ourselves. The `else_block` can be either terminated or not.
292
- template <typename IF, typename ELSE>
293
- void create_if_else (llvm::Value * cond, IF if_block, ELSE else_block) {
294
- llvm::Function *fn = builder->GetInsertBlock ()->getParent ();
295
-
296
- llvm::BasicBlock *thenBB = llvm::BasicBlock::Create (context, " then" , fn);
297
- llvm::BasicBlock *elseBB = llvm::BasicBlock::Create (context, " else" );
298
- llvm::BasicBlock *mergeBB = llvm::BasicBlock::Create (context, " ifcont" );
299
-
300
- builder->CreateCondBr (cond, thenBB, elseBB);
301
- builder->SetInsertPoint (thenBB); {
302
- if_block ();
303
- }
304
- builder->CreateBr (mergeBB);
305
-
306
- start_new_block (elseBB); {
307
- else_block ();
308
- }
309
- start_new_block (mergeBB);
310
- }
311
-
312
284
template <typename Cond, typename Body>
313
285
void create_loop (char *name, Cond condition, Body loop_body) {
314
286
dict_api_lp->set_iterators ();
@@ -1487,7 +1459,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1487
1459
}
1488
1460
}
1489
1461
llvm::Value *cond = arr_descr->get_is_allocated_flag (tmp);
1490
- create_if_else (cond, [=]() {
1462
+ llvm_utils-> create_if_else (cond, [=]() {
1491
1463
call_lfortran_free (free_fn);
1492
1464
}, [](){});
1493
1465
}
@@ -1793,10 +1765,22 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1793
1765
this ->visit_expr_wrapper (x.m_key , true );
1794
1766
ptr_loads = ptr_loads_copy;
1795
1767
llvm::Value *key = tmp;
1796
-
1797
- set_dict_api (dict_type);
1798
- tmp = llvm_utils->dict_api ->read_item (pdict, key, *module, dict_type,
1768
+ if (x.m_default ) {
1769
+ llvm::Type *val_type = get_type_from_ttype_t_util (dict_type->m_value_type );
1770
+ llvm::Value *def_value_ptr = builder->CreateAlloca (val_type, nullptr );
1771
+ ptr_loads = !LLVM::is_llvm_struct (dict_type->m_value_type );
1772
+ this ->visit_expr_wrapper (x.m_default , true );
1773
+ ptr_loads = ptr_loads_copy;
1774
+ builder->CreateStore (tmp, def_value_ptr);
1775
+ set_dict_api (dict_type);
1776
+ tmp = llvm_utils->dict_api ->get_item (pdict, key, *module, dict_type, def_value_ptr,
1799
1777
LLVM::is_llvm_struct (dict_type->m_value_type ));
1778
+ } else {
1779
+ set_dict_api (dict_type);
1780
+ tmp = llvm_utils->dict_api ->read_item (pdict, key, *module, dict_type,
1781
+ compiler_options.enable_bounds_checking ,
1782
+ LLVM::is_llvm_struct (dict_type->m_value_type ));
1783
+ }
1800
1784
}
1801
1785
1802
1786
void visit_DictPop (const ASR::DictPop_t& x) {
@@ -5140,7 +5124,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
5140
5124
5141
5125
void visit_If (const ASR::If_t &x) {
5142
5126
this ->visit_expr_wrapper (x.m_test , true );
5143
- create_if_else (tmp, [=]() {
5127
+ llvm_utils-> create_if_else (tmp, [=]() {
5144
5128
for (size_t i=0 ; i<x.n_body ; i++) {
5145
5129
this ->visit_stmt (*x.m_body [i]);
5146
5130
}
@@ -5157,7 +5141,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
5157
5141
llvm::Value *cond = tmp;
5158
5142
llvm::Value *then_val = nullptr ;
5159
5143
llvm::Value *else_val = nullptr ;
5160
- create_if_else (cond, [=, &then_val]() {
5144
+ llvm_utils-> create_if_else (cond, [=, &then_val]() {
5161
5145
this ->visit_expr_wrapper (x.m_body , true );
5162
5146
then_val = tmp;
5163
5147
}, [=, &else_val]() {
@@ -5313,7 +5297,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
5313
5297
}
5314
5298
switch (x.m_op ) {
5315
5299
case ASR::logicalbinopType::And: {
5316
- create_if_else (cond, [&, result, left_val]() {
5300
+ llvm_utils-> create_if_else (cond, [&, result, left_val]() {
5317
5301
LLVM::CreateStore (*builder, left_val, result);
5318
5302
}, [&, result, right_val]() {
5319
5303
LLVM::CreateStore (*builder, right_val, result);
@@ -5322,7 +5306,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
5322
5306
break ;
5323
5307
};
5324
5308
case ASR::logicalbinopType::Or: {
5325
- create_if_else (cond, [&, result, right_val]() {
5309
+ llvm_utils-> create_if_else (cond, [&, result, right_val]() {
5326
5310
LLVM::CreateStore (*builder, right_val, result);
5327
5311
5328
5312
}, [&, result, left_val]() {
@@ -5854,7 +5838,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
5854
5838
void visit_Assert (const ASR::Assert_t &x) {
5855
5839
if (compiler_options.emit_debug_info ) debug_emit_loc (x);
5856
5840
this ->visit_expr_wrapper (x.m_test , true );
5857
- create_if_else (tmp, []() {}, [=]() {
5841
+ llvm_utils-> create_if_else (tmp, []() {}, [=]() {
5858
5842
if (compiler_options.emit_debug_info ) {
5859
5843
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr (infile);
5860
5844
llvm::Value *fmt_ptr1 = llvm::ConstantInt::get (context, llvm::APInt (
0 commit comments