@@ -1505,7 +1505,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
15051505 this ->visit_expr (*x.m_v );
15061506 ptr_loads = ptr_loads_copy;
15071507 llvm::Value* union_llvm = tmp;
1508- ASR::Variable_t* member_var = ASR::down_cast<ASR::Variable_t>(x.m_m );
1508+ ASR::Variable_t* member_var = ASR::down_cast<ASR::Variable_t>(
1509+ ASRUtils::symbol_get_past_external (x.m_m ));
15091510 ASR::ttype_t * member_type_asr = ASRUtils::get_contained_type (member_var->m_type );
15101511 if ( ASR::is_a<ASR::Struct_t>(*member_type_asr) ) {
15111512 ASR::Struct_t* d = ASR::down_cast<ASR::Struct_t>(member_type_asr);
@@ -2424,7 +2425,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
24242425 this ->visit_expr (*x.m_shape );
24252426 llvm::Value* shape = tmp;
24262427 ASR::ttype_t * x_m_array_type = ASRUtils::expr_type (x.m_array );
2427- ASR::array_physical_typeType array_physical_type = ASRUtils::extract_physical_type (x_m_array_type);
2428+ ASR::array_physical_typeType array_physical_type = ASRUtils::extract_physical_type (x_m_array_type);
24282429 switch ( array_physical_type ) {
24292430 case ASR::array_physical_typeType::DescriptorArray: {
24302431 ASR::ttype_t * asr_data_type = ASRUtils::duplicate_type_without_dims (al,
@@ -4549,6 +4550,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
45494550 bool is_value_set = ASR::is_a<ASR::Set_t>(*asr_value_type);
45504551 bool is_target_struct = ASR::is_a<ASR::Struct_t>(*asr_target_type);
45514552 bool is_value_struct = ASR::is_a<ASR::Struct_t>(*asr_value_type);
4553+ bool is_value_list_to_array = (ASR::is_a<ASR::Cast_t>(*x.m_value ) &&
4554+ ASR::down_cast<ASR::Cast_t>(x.m_value )->m_kind == ASR::cast_kindType::ListToArray);
45524555 if (ASR::is_a<ASR::StringSection_t>(*x.m_target )) {
45534556 handle_StringSection_Assignment (x.m_target , x.m_value );
45544557 if (tmp == strings_to_be_deallocated.back ()) {
@@ -4767,7 +4770,37 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
47674770 target = CreateLoad (target);
47684771 }
47694772 ASR::ttype_t *cont_type = ASRUtils::get_contained_type (asr_target_type);
4770- if (ASRUtils::is_array (cont_type) && ASRUtils::is_array (cont_type) ) {
4773+ if ( ASRUtils::is_array (cont_type) ) {
4774+ if ( is_value_list_to_array ) {
4775+ this ->visit_expr_wrapper (x.m_value , true );
4776+ llvm::Value* list_data = tmp;
4777+ int64_t ptr_loads_copy = ptr_loads;
4778+ ptr_loads = 0 ;
4779+ this ->visit_expr (*ASR::down_cast<ASR::Cast_t>(x.m_value )->m_arg );
4780+ llvm::Value* plist = tmp;
4781+ ptr_loads = ptr_loads_copy;
4782+ llvm::Value* array_data = nullptr ;
4783+ if ( ASRUtils::extract_physical_type (asr_target_type) ==
4784+ ASR::array_physical_typeType::DescriptorArray ) {
4785+ array_data = LLVM::CreateLoad (*builder,
4786+ arr_descr->get_pointer_to_data (LLVM::CreateLoad (*builder, target)));
4787+ } else if ( ASRUtils::extract_physical_type (asr_target_type) ==
4788+ ASR::array_physical_typeType::FixedSizeArray ) {
4789+ array_data = llvm_utils->create_gep (target, 0 );
4790+ } else {
4791+ LCOMPILERS_ASSERT (false );
4792+ }
4793+ llvm::Value* size = list_api->len (plist);
4794+ llvm::Type* el_type = llvm_utils->get_type_from_ttype_t_util (
4795+ ASRUtils::extract_type (ASRUtils::expr_type (x.m_value )), module .get ());
4796+ llvm::DataLayout data_layout (module .get ());
4797+ uint64_t size_ = data_layout.getTypeAllocSize (el_type);
4798+ size = builder->CreateMul (size, llvm::ConstantInt::get (
4799+ llvm::Type::getInt32Ty (context), llvm::APInt (32 , size_)));
4800+ builder->CreateMemCpy (array_data, llvm::MaybeAlign (),
4801+ list_data, llvm::MaybeAlign (), size);
4802+ return ;
4803+ }
47714804 if ( asr_target->m_type ->type == ASR::ttypeType::Character) {
47724805 target = CreateLoad (arr_descr->get_pointer_to_data (target));
47734806 }
@@ -5030,17 +5063,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
50305063 } else if (
50315064 m_new == ASR::array_physical_typeType::PointerToDataArray &&
50325065 m_old == ASR::array_physical_typeType::FixedSizeArray) {
5033- if ( (ASRUtils::expr_value (m_arg) &&
5066+ if ( (( ASRUtils::expr_value (m_arg) &&
50345067 !ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value (m_arg))) ||
5035- ASRUtils::expr_value (m_arg) == nullptr ) {
5068+ ASRUtils::expr_value (m_arg) == nullptr ) &&
5069+ !ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
50365070 tmp = llvm_utils->create_gep (tmp, 0 );
50375071 }
50385072 } else if (
50395073 m_new == ASR::array_physical_typeType::UnboundedPointerToDataArray &&
50405074 m_old == ASR::array_physical_typeType::FixedSizeArray) {
5041- if ( (ASRUtils::expr_value (m_arg) &&
5075+ if ( (( ASRUtils::expr_value (m_arg) &&
50425076 !ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value (m_arg))) ||
5043- ASRUtils::expr_value (m_arg) == nullptr ) {
5077+ ASRUtils::expr_value (m_arg) == nullptr ) &&
5078+ !ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
50445079 tmp = llvm_utils->create_gep (tmp, 0 );
50455080 }
50465081 } else if (
@@ -5054,9 +5089,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
50545089 } else if (
50555090 m_new == ASR::array_physical_typeType::DescriptorArray &&
50565091 m_old == ASR::array_physical_typeType::FixedSizeArray) {
5057- if ( (ASRUtils::expr_value (m_arg) &&
5092+ if ( (( ASRUtils::expr_value (m_arg) &&
50585093 !ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value (m_arg))) ||
5059- ASRUtils::expr_value (m_arg) == nullptr ) {
5094+ ASRUtils::expr_value (m_arg) == nullptr ) &&
5095+ !ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
50605096 tmp = llvm_utils->create_gep (tmp, 0 );
50615097 }
50625098 PointerToData_to_Descriptor (m_type, m_type_for_dimensions);
@@ -5094,9 +5130,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
50945130 m_old == ASR::array_physical_typeType::CharacterArraySinglePointer) {
50955131 //
50965132 if (ASRUtils::is_fixed_size_array (m_type)) {
5097- if ( (ASRUtils::expr_value (m_arg) &&
5133+ if ( (( ASRUtils::expr_value (m_arg) &&
50985134 !ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value (m_arg))) ||
5099- ASRUtils::expr_value (m_arg) == nullptr ) {
5135+ ASRUtils::expr_value (m_arg) == nullptr ) &&
5136+ !ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
51005137 tmp = llvm_utils->create_gep (tmp, 0 );
51015138 }
51025139 } else {
@@ -6396,7 +6433,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
63966433
63976434 }
63986435
6399- void visit_ArrayConstructor (const ASR::ArrayConstructor_t &x) {
6436+ template <typename T>
6437+ void visit_ArrayConstructorUtil (const T& x) {
64006438 llvm::Type* el_type = nullptr ;
64016439 ASR::ttype_t * x_m_type = ASRUtils::type_get_past_array (x.m_type );
64026440 if (ASR::is_a<ASR::Integer_t>(*x_m_type)) {
@@ -6444,52 +6482,12 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
64446482 tmp = llvm_utils->create_gep (p_fxn, 0 );
64456483 }
64466484
6485+ void visit_ArrayConstructor (const ASR::ArrayConstructor_t &x) {
6486+ visit_ArrayConstructorUtil (x);
6487+ }
6488+
64476489 void visit_ArrayConstant (const ASR::ArrayConstant_t &x) {
6448- llvm::Type* el_type = nullptr ;
6449- ASR::ttype_t * x_m_type = ASRUtils::type_get_past_array (x.m_type );
6450- if (ASR::is_a<ASR::Integer_t>(*x_m_type)) {
6451- el_type = llvm_utils->getIntType (ASR::down_cast<ASR::Integer_t>(x_m_type)->m_kind );
6452- } else if (ASR::is_a<ASR::Real_t>(*x_m_type)) {
6453- switch (ASR::down_cast<ASR::Real_t>(x_m_type)->m_kind ) {
6454- case (4 ) :
6455- el_type = llvm::Type::getFloatTy (context); break ;
6456- case (8 ) :
6457- el_type = llvm::Type::getDoubleTy (context); break ;
6458- default :
6459- throw CodeGenError (" ConstArray real kind not supported yet" );
6460- }
6461- } else if (ASR::is_a<ASR::Logical_t>(*x_m_type)) {
6462- el_type = llvm::Type::getInt1Ty (context);
6463- } else if (ASR::is_a<ASR::Character_t>(*x_m_type)) {
6464- el_type = character_type;
6465- } else if (ASR::is_a<ASR::Complex_t>(*x_m_type)) {
6466- int complex_kind = ASR::down_cast<ASR::Complex_t>(x_m_type)->m_kind ;
6467- if ( complex_kind == 4 ) {
6468- el_type = llvm_utils->complex_type_4 ;
6469- } else if ( complex_kind == 8 ) {
6470- el_type = llvm_utils->complex_type_8 ;
6471- } else {
6472- LCOMPILERS_ASSERT (false );
6473- }
6474- } else {
6475- throw CodeGenError (" ConstArray type not supported yet" );
6476- }
6477- // Create <n x float> type, where `n` is the length of the `x` constant array
6478- llvm::Type* type_fxn = FIXED_VECTOR_TYPE::get (el_type, x.n_args );
6479- // Create a pointer <n x float>* to a stack allocated <n x float>
6480- llvm::AllocaInst *p_fxn = builder->CreateAlloca (type_fxn, nullptr );
6481- // Assign the array elements to `p_fxn`.
6482- for (size_t i=0 ; i < x.n_args ; i++) {
6483- llvm::Value *llvm_el = llvm_utils->create_gep (p_fxn, i);
6484- ASR::expr_t *el = x.m_args [i];
6485- int64_t ptr_loads_copy = ptr_loads;
6486- ptr_loads = 2 ;
6487- this ->visit_expr_wrapper (el, true );
6488- ptr_loads = ptr_loads_copy;
6489- builder->CreateStore (tmp, llvm_el);
6490- }
6491- // Return the vector as float* type:
6492- tmp = llvm_utils->create_gep (p_fxn, 0 );
6490+ visit_ArrayConstructorUtil (x);
64936491 }
64946492
64956493 void visit_Assert (const ASR::Assert_t &x) {
@@ -6654,7 +6652,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
66546652 this ->visit_expr_wrapper (x->m_value , true );
66556653 return ;
66566654 }
6657- ASR::ttype_t *t2_ = ASRUtils::type_get_past_array (x->m_type );
6655+ ASR::ttype_t *t2_ = ASRUtils::type_get_past_const (
6656+ ASRUtils::type_get_past_array (x->m_type ));
66586657 switch ( t2_->type ) {
66596658 case ASR::ttypeType::Pointer:
66606659 case ASR::ttypeType::Allocatable: {
@@ -7248,6 +7247,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
72487247 tmp = builder->CreateSelect (cmp, zero_str, one_str);
72497248 break ;
72507249 }
7250+ case (ASR::cast_kindType::ListToArray) : {
7251+ if ( !ASR::is_a<ASR::List_t>(*ASRUtils::expr_type (x.m_arg )) ) {
7252+ throw CodeGenError (" The argument of ListToArray cast should "
7253+ " be a list/std::vector, found, " + ASRUtils::type_to_str (
7254+ ASRUtils::expr_type (x.m_arg )));
7255+ }
7256+ int64_t ptr_loads_copy = ptr_loads;
7257+ ptr_loads = 0 ;
7258+ this ->visit_expr (*x.m_arg );
7259+ ptr_loads = ptr_loads_copy;
7260+ tmp = LLVM::CreateLoad (*builder, list_api->get_pointer_to_list_data (tmp));
7261+ break ;
7262+ }
72517263 default : throw CodeGenError (" Cast kind not implemented" );
72527264 }
72537265 }
@@ -9792,6 +9804,7 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
97929804 v.module ->print (os, nullptr );
97939805 std::cout << os.str ();
97949806 msg = " asr_to_llvm: module failed verification. Error:\n " + err.str ();
9807+ std::cout << msg << std::endl;
97959808 diagnostics.diagnostics .push_back (diag::Diagnostic (msg,
97969809 diag::Level::Error, diag::Stage::CodeGen));
97979810 Error error;
0 commit comments