@@ -270,6 +270,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
270
270
llvm_utils->tuple_api = tuple_api.get ();
271
271
llvm_utils->list_api = list_api.get ();
272
272
llvm_utils->dict_api = nullptr ;
273
+ llvm_utils->arr_api = arr_descr.get ();
273
274
}
274
275
275
276
llvm::Value* CreateLoad (llvm::Value *x) {
@@ -1383,7 +1384,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1383
1384
this ->visit_expr (*x.m_args [i]);
1384
1385
llvm::Value* item = tmp;
1385
1386
llvm::Value* pos = llvm::ConstantInt::get (context, llvm::APInt (32 , i));
1386
- list_api->write_item (const_list, pos, item, list_type->m_type , false , *module);
1387
+ list_api->write_item (const_list, pos, item, list_type->m_type ,
1388
+ false , module.get (), name2memidx);
1387
1389
}
1388
1390
ptr_loads = ptr_loads_copy;
1389
1391
tmp = const_list;
@@ -1416,7 +1418,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1416
1418
visit_expr (*x.m_values [i]);
1417
1419
llvm::Value* value = tmp;
1418
1420
llvm_utils->dict_api ->write_item (const_dict, key, value, module.get (),
1419
- x_dict->m_key_type , x_dict->m_value_type );
1421
+ x_dict->m_key_type , x_dict->m_value_type , name2memidx );
1420
1422
}
1421
1423
ptr_loads = ptr_loads_copy;
1422
1424
tmp = const_dict;
@@ -1484,7 +1486,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1484
1486
llvm::Value *item = tmp;
1485
1487
ptr_loads = ptr_loads_copy;
1486
1488
1487
- list_api->append (plist, item, asr_list->m_type , * module);
1489
+ list_api->append (plist, item, asr_list->m_type , module. get (), name2memidx );
1488
1490
}
1489
1491
1490
1492
void visit_UnionRef (const ASR::UnionRef_t& x) {
@@ -1609,7 +1611,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1609
1611
llvm::Value *item = tmp;
1610
1612
ptr_loads = ptr_loads_copy;
1611
1613
1612
- list_api->insert_item (plist, pos, item, asr_list->m_type , * module);
1614
+ list_api->insert_item (plist, pos, item, asr_list->m_type , module. get (), name2memidx );
1613
1615
}
1614
1616
1615
1617
void visit_DictInsert (const ASR::DictInsert_t& x) {
@@ -1631,7 +1633,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1631
1633
set_dict_api (dict_type);
1632
1634
llvm_utils->dict_api ->write_item (pdict, key, value, module.get (),
1633
1635
dict_type->m_key_type ,
1634
- dict_type->m_value_type );
1636
+ dict_type->m_value_type , name2memidx );
1635
1637
}
1636
1638
1637
1639
void visit_ListRemove (const ASR::ListRemove_t& x) {
@@ -2571,6 +2573,49 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
2571
2573
m_dims_local, n_dims_local, a_kind_local);
2572
2574
}
2573
2575
2576
+ void fill_array_details_ (llvm::Value* ptr, ASR::dimension_t * m_dims,
2577
+ size_t n_dims, bool is_malloc_array_type, bool is_array_type,
2578
+ bool is_list, ASR::ttype_t * m_type) {
2579
+ if ( is_malloc_array_type &&
2580
+ m_type->type != ASR::ttypeType::Pointer &&
2581
+ !is_list ) {
2582
+ arr_descr->fill_dimension_descriptor (ptr, n_dims);
2583
+ }
2584
+ if ( is_array_type && !is_malloc_array_type &&
2585
+ m_type->type != ASR::ttypeType::Pointer &&
2586
+ !is_list ) {
2587
+ ASR::ttype_t * asr_data_type = ASRUtils::duplicate_type_without_dims (al, m_type, m_type->base .loc );
2588
+ llvm::Type* llvm_data_type = get_type_from_ttype_t_util (asr_data_type);
2589
+ fill_array_details (ptr, llvm_data_type, m_dims, n_dims);
2590
+ }
2591
+ if ( is_array_type && is_malloc_array_type &&
2592
+ m_type->type != ASR::ttypeType::Pointer &&
2593
+ !is_list ) {
2594
+ // Set allocatable arrays as unallocated
2595
+ arr_descr->set_is_allocated_flag (ptr, 0 );
2596
+ }
2597
+ }
2598
+
2599
+ void allocate_array_members_of_struct (llvm::Value* ptr, ASR::ttype_t * asr_type) {
2600
+ LFORTRAN_ASSERT (ASR::is_a<ASR::Struct_t>(*asr_type));
2601
+ ASR::Struct_t* struct_t = ASR::down_cast<ASR::Struct_t>(asr_type);
2602
+ ASR::StructType_t* struct_type_t = ASR::down_cast<ASR::StructType_t>(struct_t ->m_derived_type );
2603
+ std::string struct_type_name = struct_type_t ->m_name ;
2604
+ for ( auto item: struct_type_t ->m_symtab ->get_scope () ) {
2605
+ ASR::ttype_t * symbol_type = ASRUtils::symbol_type (item.second );
2606
+ int idx = name2memidx[struct_type_name][item.first ];
2607
+ llvm::Value* ptr_member = llvm_utils->create_gep (ptr, idx);
2608
+ if ( ASRUtils::is_array (symbol_type) ) {
2609
+ // Assume that struct member array is not allocatable
2610
+ ASR::dimension_t * m_dims = nullptr ;
2611
+ size_t n_dims = ASRUtils::extract_dimensions_from_ttype (symbol_type, m_dims);
2612
+ fill_array_details_ (ptr_member, m_dims, n_dims, false , true , false , symbol_type);
2613
+ } else if ( ASR::is_a<ASR::Struct_t>(*symbol_type) ) {
2614
+ allocate_array_members_of_struct (ptr_member, symbol_type);
2615
+ }
2616
+ }
2617
+ }
2618
+
2574
2619
template <typename T>
2575
2620
void declare_vars (const T &x) {
2576
2621
llvm::Value *target_var;
@@ -2626,6 +2671,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
2626
2671
}
2627
2672
}
2628
2673
llvm::AllocaInst *ptr = builder->CreateAlloca (type, nullptr , v->m_name );
2674
+ if ( ASR::is_a<ASR::Struct_t>(*v->m_type ) ) {
2675
+ allocate_array_members_of_struct (ptr, v->m_type );
2676
+ }
2629
2677
if (emit_debug_info) {
2630
2678
// Reset the debug location
2631
2679
builder->SetCurrentDebugLocation (nullptr );
@@ -2659,24 +2707,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
2659
2707
}
2660
2708
}
2661
2709
llvm_symtab[h] = ptr;
2662
- if ( is_malloc_array_type &&
2663
- v->m_type ->type != ASR::ttypeType::Pointer &&
2664
- !is_list ) {
2665
- arr_descr->fill_dimension_descriptor (ptr, n_dims);
2666
- }
2667
- if ( is_array_type && !is_malloc_array_type &&
2668
- v->m_type ->type != ASR::ttypeType::Pointer &&
2669
- !is_list ) {
2670
- ASR::ttype_t * asr_data_type = ASRUtils::duplicate_type_without_dims (al, v->m_type , v->m_type ->base .loc );
2671
- llvm::Type* llvm_data_type = get_type_from_ttype_t_util (asr_data_type);
2672
- fill_array_details (ptr, llvm_data_type, m_dims, n_dims);
2673
- }
2674
- if ( is_array_type && is_malloc_array_type &&
2675
- v->m_type ->type != ASR::ttypeType::Pointer &&
2676
- !is_list ) {
2677
- // Set allocatable arrays as unallocated
2678
- arr_descr->set_is_allocated_flag (ptr, 0 );
2679
- }
2710
+ fill_array_details_ (ptr, m_dims, n_dims,
2711
+ is_malloc_array_type,
2712
+ is_array_type, is_list, v->m_type );
2680
2713
if ( v->m_symbolic_value != nullptr &&
2681
2714
!ASR::is_a<ASR::List_t>(*v->m_type )) {
2682
2715
target_var = ptr;
@@ -3776,7 +3809,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3776
3809
ASRUtils::expr_type (x.m_value ));
3777
3810
std::string value_type_code = ASRUtils::get_type_code (value_asr_list->m_type );
3778
3811
list_api->list_deepcopy (value_list, target_list,
3779
- value_asr_list, *module);
3812
+ value_asr_list, module.get (),
3813
+ name2memidx);
3780
3814
return ;
3781
3815
} else if ( is_target_tuple && is_value_tuple ) {
3782
3816
uint64_t ptr_loads_copy = ptr_loads;
@@ -3805,7 +3839,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3805
3839
llvm::Value* llvm_tuple_i = builder->CreateAlloca (llvm_tuple_i_type, nullptr );
3806
3840
ptr_loads = !LLVM::is_llvm_struct (asr_tuple_i_type);
3807
3841
visit_expr (*asr_value_tuple->m_elements [i]);
3808
- llvm_utils->deepcopy (tmp, llvm_tuple_i, asr_tuple_i_type, * module);
3842
+ llvm_utils->deepcopy (tmp, llvm_tuple_i, asr_tuple_i_type, module. get (), name2memidx );
3809
3843
src_deepcopies.push_back (al, llvm_tuple_i);
3810
3844
}
3811
3845
ASR::TupleConstant_t* asr_target_tuple = ASR::down_cast<ASR::TupleConstant_t>(x.m_target );
@@ -3829,7 +3863,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3829
3863
std::string type_code = ASRUtils::get_type_code (value_tuple_type->m_type ,
3830
3864
value_tuple_type->n_type );
3831
3865
tuple_api->tuple_deepcopy (value_tuple, target_tuple,
3832
- value_tuple_type, *module);
3866
+ value_tuple_type, module.get (),
3867
+ name2memidx);
3833
3868
}
3834
3869
return ;
3835
3870
} else if ( is_target_dict && is_value_dict ) {
@@ -3843,7 +3878,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3843
3878
ASR::Dict_t* value_dict_type = ASR::down_cast<ASR::Dict_t>(asr_value_type);
3844
3879
set_dict_api (value_dict_type);
3845
3880
llvm_utils->dict_api ->dict_deepcopy (value_dict, target_dict,
3846
- value_dict_type, module.get ());
3881
+ value_dict_type, module.get (), name2memidx );
3847
3882
return ;
3848
3883
} else if ( is_target_struct && is_value_struct ) {
3849
3884
uint64_t ptr_loads_copy = ptr_loads;
@@ -3856,10 +3891,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3856
3891
is_assignment_target = is_assignment_target_copy;
3857
3892
llvm::Value* target_struct = tmp;
3858
3893
ptr_loads = ptr_loads_copy;
3859
- LLVM::CreateStore (*builder,
3860
- LLVM::CreateLoad (*builder, value_struct),
3861
- target_struct
3862
- );
3894
+ llvm_utils->deepcopy (value_struct, target_struct,
3895
+ asr_target_type, module.get (), name2memidx);
3863
3896
return ;
3864
3897
}
3865
3898
@@ -3973,9 +4006,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
3973
4006
if ( ASRUtils::is_array (target_type) &&
3974
4007
ASRUtils::is_array (value_type) &&
3975
4008
ASRUtils::check_equal_type (target_type, value_type) ) {
3976
- bool create_dim_des_array = !ASR::is_a<ASR::Var_t>(*x.m_target );
3977
4009
arr_descr->copy_array (value, target, module.get (),
3978
- target_type, create_dim_des_array );
4010
+ target_type, false , false );
3979
4011
} else {
3980
4012
builder->CreateStore (value, target);
3981
4013
}
@@ -5975,7 +6007,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
5975
6007
}
5976
6008
if ( ASR::is_a<ASR::Tuple_t>(*arg_type) ||
5977
6009
ASR::is_a<ASR::List_t>(*arg_type) ) {
5978
- llvm_utils->deepcopy (value, target, arg_type, * module);
6010
+ llvm_utils->deepcopy (value, target, arg_type, module. get (), name2memidx );
5979
6011
} else {
5980
6012
builder->CreateStore (value, target);
5981
6013
}
0 commit comments