Skip to content

Commit c15728a

Browse files
committed
Handle CPtr printing support when its intent is In
1 parent aa24f22 commit c15728a

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
12391239
return;
12401240
}
12411241
der_type_name = "";
1242+
uint64_t ptr_loads_copy = ptr_loads;
1243+
ptr_loads = ptr_loads_copy - ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_v));
12421244
this->visit_expr(*x.m_v);
1245+
ptr_loads = ptr_loads_copy;
12431246
ASR::Variable_t* member = down_cast<ASR::Variable_t>(symbol_get_past_external(x.m_m));
12441247
std::string member_name = std::string(member->m_name);
12451248
LFORTRAN_ASSERT(der_type_name.size() != 0);
@@ -1608,15 +1611,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16081611
}
16091612
case (ASR::ttypeType::Pointer) : {
16101613
ASR::ttype_t *t2 = ASR::down_cast<ASR::Pointer_t>(asr_type)->m_type;
1611-
switch (t2->type) {
1612-
case (ASR::ttypeType::Derived) : {
1613-
throw CodeGenError("Pointers for Derived type not implemented yet in conversion.");
1614-
}
1615-
default :
1616-
llvm_type = get_type_from_ttype_t(t2, m_storage, is_array_type,
1614+
llvm_type = get_type_from_ttype_t(t2, m_storage, is_array_type,
16171615
is_malloc_array_type, m_dims, n_dims, a_kind);
1618-
llvm_type = llvm_type->getPointerTo();
1619-
}
1616+
llvm_type = llvm_type->getPointerTo();
16201617
break;
16211618
}
16221619
case (ASR::ttypeType::List) : {
@@ -2654,9 +2651,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
26542651

26552652
void visit_CPtrToPointer(const ASR::CPtrToPointer_t& x) {
26562653
ASR::expr_t *cptr = x.m_cptr, *fptr = x.m_ptr, *shape = x.m_shape;
2654+
int reduce_loads = 0;
2655+
if( ASR::is_a<ASR::Var_t>(*cptr) ) {
2656+
ASR::Variable_t* cptr_var = ASRUtils::EXPR2VAR(cptr);
2657+
reduce_loads = cptr_var->m_intent == ASRUtils::intent_in;
2658+
}
26572659
if( ASRUtils::is_array(ASRUtils::expr_type(fptr)) ) {
26582660
uint64_t ptr_loads_copy = ptr_loads;
2659-
ptr_loads = 1;
2661+
ptr_loads = 1 - reduce_loads;
26602662
this->visit_expr(*cptr);
26612663
llvm::Value* llvm_cptr = tmp;
26622664
ptr_loads = 0;
@@ -2706,7 +2708,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
27062708
}
27072709
} else {
27082710
uint64_t ptr_loads_copy = ptr_loads;
2709-
ptr_loads = 1;
2711+
ptr_loads = 1 - reduce_loads;
27102712
this->visit_expr(*cptr);
27112713
llvm::Value* llvm_cptr = tmp;
27122714
ptr_loads = 0;
@@ -3689,13 +3691,17 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
36893691
switch (t2->type) {
36903692
case ASR::ttypeType::Integer:
36913693
case ASR::ttypeType::Real:
3692-
case ASR::ttypeType::Complex: {
3694+
case ASR::ttypeType::Complex:
3695+
case ASR::ttypeType::Derived: {
3696+
if( t2->type == ASR::ttypeType::Derived ) {
3697+
ASR::Derived_t* d = ASR::down_cast<ASR::Derived_t>(t2);
3698+
der_type_name = ASRUtils::symbol_name(d->m_derived_type);
3699+
}
36933700
fetch_ptr(x);
36943701
break;
36953702
}
36963703
case ASR::ttypeType::Character:
3697-
case ASR::ttypeType::Logical:
3698-
case ASR::ttypeType::Derived: {
3704+
case ASR::ttypeType::Logical: {
36993705
break;
37003706
}
37013707
default:
@@ -4080,7 +4086,12 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
40804086
std::vector<std::string> fmt;
40814087
for (size_t i=0; i<x.n_values; i++) {
40824088
uint64_t ptr_loads_copy = ptr_loads;
4083-
ptr_loads = 1;
4089+
int reduce_loads = 0;
4090+
if( ASR::is_a<ASR::Var_t>(*x.m_values[i]) ) {
4091+
ASR::Variable_t* cptr_var = ASRUtils::EXPR2VAR(x.m_values[i]);
4092+
reduce_loads = cptr_var->m_intent == ASRUtils::intent_in;
4093+
}
4094+
ptr_loads = 1 - reduce_loads;
40844095
this->visit_expr_wrapper(x.m_values[i], true);
40854096
ptr_loads = ptr_loads_copy;
40864097
ASR::expr_t *v = x.m_values[i];
@@ -4235,11 +4246,13 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
42354246
template <typename T>
42364247
inline void set_func_subrout_params(T* func_subrout, ASR::abiType& x_abi,
42374248
std::uint32_t& m_h, ASR::Variable_t*& orig_arg,
4238-
std::string& orig_arg_name, size_t arg_idx) {
4249+
std::string& orig_arg_name, ASR::intentType& arg_intent,
4250+
size_t arg_idx) {
42394251
m_h = get_hash((ASR::asr_t*)func_subrout);
42404252
orig_arg = EXPR2VAR(func_subrout->m_args[arg_idx]);
42414253
orig_arg_name = orig_arg->m_name;
42424254
x_abi = func_subrout->m_abi;
4255+
arg_intent = orig_arg->m_intent;
42434256
}
42444257

42454258

@@ -4255,6 +4268,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
42554268
ASR::Subroutine_t* sub = down_cast<ASR::Subroutine_t>(func_subrout);
42564269
x_abi = sub->m_abi;
42574270
}
4271+
// TODO: Below if check is dead. Remove.
42584272
if( x_abi == ASR::abiType::Intrinsic ) {
42594273
if( name == "lbound" || name == "ubound" ) {
42604274
ASR::Variable_t *arg = EXPR2VAR(x.m_args[0].m_value);
@@ -4283,23 +4297,24 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
42834297
tmp = llvm_symtab[h];
42844298
func_subrout = symbol_get_past_external(x.m_name);
42854299
x_abi = (ASR::abiType) 0;
4300+
ASR::intentType orig_arg_intent = ASR::intentType::Unspecified;
42864301
std::uint32_t m_h;
42874302
ASR::Variable_t *orig_arg = nullptr;
42884303
std::string orig_arg_name = "";
42894304
if( func_subrout->type == ASR::symbolType::Function ) {
42904305
ASR::Function_t* func = down_cast<ASR::Function_t>(func_subrout);
4291-
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, i);
4306+
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
42924307
} else if( func_subrout->type == ASR::symbolType::Subroutine ) {
42934308
ASR::Subroutine_t* sub = down_cast<ASR::Subroutine_t>(func_subrout);
4294-
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, i);
4309+
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
42954310
} else if( func_subrout->type == ASR::symbolType::ClassProcedure ) {
42964311
ASR::ClassProcedure_t* clss_proc = ASR::down_cast<ASR::ClassProcedure_t>(func_subrout);
42974312
if( clss_proc->m_proc->type == ASR::symbolType::Subroutine ) {
42984313
ASR::Subroutine_t* sub = down_cast<ASR::Subroutine_t>(clss_proc->m_proc);
4299-
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, i);
4314+
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
43004315
} else if( clss_proc->m_proc->type == ASR::symbolType::Function ) {
43014316
ASR::Function_t* func = down_cast<ASR::Function_t>(clss_proc->m_proc);
4302-
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, i);
4317+
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
43034318
}
43044319
} else {
43054320
LFORTRAN_ASSERT(false)

0 commit comments

Comments
 (0)