@@ -85,6 +85,24 @@ namespace LFortran {
85
85
};
86
86
return builder.CreateCall (fn, args);
87
87
}
88
+
89
+ llvm::Value* lfortran_free (llvm::LLVMContext &context, llvm::Module &module,
90
+ llvm::IRBuilder<> &builder, llvm::Value* ptr) {
91
+ std::string func_name = " _lfortran_free" ;
92
+ llvm::Function *fn = module.getFunction (func_name);
93
+ if (!fn) {
94
+ llvm::FunctionType *function_type = llvm::FunctionType::get (
95
+ llvm::Type::getVoidTy (context), {
96
+ llvm::Type::getInt8PtrTy (context)
97
+ }, true );
98
+ fn = llvm::Function::Create (function_type,
99
+ llvm::Function::ExternalLinkage, func_name, module);
100
+ }
101
+ std::vector<llvm::Value*> args = {
102
+ builder.CreateBitCast (ptr, llvm::Type::getInt8PtrTy (context)),
103
+ };
104
+ return builder.CreateCall (fn, args);
105
+ }
88
106
} // namespace LLVM
89
107
90
108
LLVMUtils::LLVMUtils (llvm::LLVMContext& context,
@@ -429,6 +447,9 @@ namespace LFortran {
429
447
// integration_tests/test_list_07.py.
430
448
if ( LLVM::is_llvm_struct (element_type) ) {
431
449
builder->CreateStore (copy_data, get_pointer_to_list_data (dest));
450
+ // TODO: Should be created outside the user loop and not here.
451
+ // LLVMList should treat them as data members and create them
452
+ // only if they are NULL
432
453
llvm::AllocaInst *pos_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context),
433
454
nullptr );
434
455
LLVM::CreateStore (*builder, llvm::ConstantInt::get (llvm::Type::getInt32Ty (context),
@@ -708,6 +729,9 @@ namespace LFortran {
708
729
new_key_mask = builder->CreateBitCast (new_key_mask, llvm::Type::getInt1PtrTy (context));
709
730
710
731
llvm::Value* current_capacity = LLVM::CreateLoad (*builder, get_pointer_to_capacity (dict));
732
+ // TODO: Should be created outside the user loop and not here.
733
+ // LLVMDict should treat them as data members and create them
734
+ // only if they are NULL
711
735
llvm::Value* idx_ptr = builder->CreateAlloca (llvm::Type::getInt32Ty (context), nullptr );
712
736
LLVM::CreateStore (*builder, llvm::ConstantInt::get (llvm::Type::getInt32Ty (context),
713
737
llvm::APInt (32 , 0 )), idx_ptr);
@@ -769,6 +793,9 @@ namespace LFortran {
769
793
reset_iterators ();
770
794
771
795
// TODO: Free key_list, value_list and key_mask
796
+ llvm_utils->list_api ->free_data (key_list, *module);
797
+ llvm_utils->list_api ->free_data (value_list, *module);
798
+ LLVM::lfortran_free (context, *module, *builder, key_mask);
772
799
LLVM::CreateStore (*builder, LLVM::CreateLoad (*builder, new_key_list), key_list);
773
800
LLVM::CreateStore (*builder, LLVM::CreateLoad (*builder, new_value_list), value_list);
774
801
LLVM::CreateStore (*builder, new_key_mask, get_pointer_to_keymask (dict));
@@ -925,10 +952,16 @@ namespace LFortran {
925
952
* list[pos] = item;
926
953
*/
927
954
955
+ // TODO: Should be created outside the user loop and not here.
956
+ // LLVMList should treat them as data members and create them
957
+ // only if they are NULL
928
958
llvm::AllocaInst *tmp_ptr = builder->CreateAlloca (el_type, nullptr );
929
959
LLVM::CreateStore (*builder, read_item (list, pos, false ), tmp_ptr);
930
960
llvm::Value* tmp = nullptr ;
931
961
962
+ // TODO: Should be created outside the user loop and not here.
963
+ // LLVMList should treat them as data members and create them
964
+ // only if they are NULL
932
965
llvm::AllocaInst *pos_ptr = builder->CreateAlloca (
933
966
llvm::Type::getInt32Ty (context), nullptr );
934
967
LLVM::CreateStore (*builder, pos, pos_ptr);
@@ -975,6 +1008,9 @@ namespace LFortran {
975
1008
llvm::Type* pos_type = llvm::Type::getInt32Ty (context);
976
1009
llvm::Value* current_end_point = LLVM::CreateLoad (*builder,
977
1010
get_pointer_to_current_end_point (list));
1011
+ // TODO: Should be created outside the user loop and not here.
1012
+ // LLVMList should treat them as data members and create them
1013
+ // only if they are NULL
978
1014
llvm::AllocaInst *i = builder->CreateAlloca (pos_type, nullptr );
979
1015
LLVM::CreateStore (*builder, llvm::ConstantInt::get (
980
1016
context, llvm::APInt (32 , 0 )), i);
@@ -1059,6 +1095,9 @@ namespace LFortran {
1059
1095
llvm::Type* pos_type = llvm::Type::getInt32Ty (context);
1060
1096
llvm::Value* current_end_point = LLVM::CreateLoad (*builder,
1061
1097
get_pointer_to_current_end_point (list));
1098
+ // TODO: Should be created outside the user loop and not here.
1099
+ // LLVMList should treat them as data members and create them
1100
+ // only if they are NULL
1062
1101
llvm::AllocaInst *item_pos = builder->CreateAlloca (pos_type, nullptr );
1063
1102
llvm::Value* tmp = LLVMList::find_item_position (list, item, item_type, module);
1064
1103
LLVM::CreateStore (*builder, tmp, item_pos);
@@ -1114,6 +1153,11 @@ namespace LFortran {
1114
1153
LLVM::CreateStore (*builder, zero, end_point_ptr);
1115
1154
}
1116
1155
1156
+ void LLVMList::free_data (llvm::Value* list, llvm::Module& module) {
1157
+ llvm::Value* data = LLVM::CreateLoad (*builder, get_pointer_to_list_data (list));
1158
+ LLVM::lfortran_free (context, module, *builder, data);
1159
+ }
1160
+
1117
1161
1118
1162
LLVMTuple::LLVMTuple (llvm::LLVMContext& context_,
1119
1163
LLVMUtils* llvm_utils_,
0 commit comments