Skip to content

Commit 84bcf1c

Browse files
authored
Merge pull request #594 from czgdp1807/test_c_interop_03
Enabling c backend for test_c_interop_03.py
2 parents 35e9106 + a357944 commit 84bcf1c

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,5 @@ src/lpython/parser/parser.tab.hh
168168
integration_tests/test_c_interop_01.c
169169
integration_tests/test_c_interop_01
170170
integration_tests/test_c_interop_03
171+
integration_tests/test_c_interop_03.c
171172
integration_tests/test_c_interop_04

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ RUN(NAME test_math_02 LABELS cpython llvm)
154154
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
155155
RUN(NAME test_c_interop_02 LABELS cpython llvm c
156156
EXTRAFILES test_c_interop_02b.c)
157-
RUN(NAME test_c_interop_03 LABELS llvm
157+
RUN(NAME test_c_interop_03 LABELS llvm c
158158
EXTRAFILES test_c_interop_03b.c)
159159
RUN(NAME test_c_interop_04 LABELS llvm
160160
EXTRAFILES test_c_interop_04b.c)

integration_tests/test_c_interop_03.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ def test_c_callbacks():
2626

2727
xi64: i64
2828
xi64 = 3
29-
p_c_pointer(xi64, p)
29+
p_c_pointer(pointer(xi64), p)
30+
print(pointer(xi64), p)
3031
assert f_pi64_i32(p) == 4
3132

3233
xf32: f32
3334
xf32 = 3.3
34-
p_c_pointer(xf32, p)
35+
p_c_pointer(pointer(xf32), p)
36+
print(pointer(xf32), p)
3537
assert abs(f_pf32_i32(p)-4.3) < 1e-6
3638

3739
xf64: f64
3840
xf64 = 3.3
39-
p_c_pointer(xf64, p)
41+
p_c_pointer(pointer(xf64), p)
42+
print(pointer(xf64), p)
4043
assert abs(f_pf64_i32(p)-4.3) < 1e-12
4144

4245
test_c_callbacks()

src/libasr/asr_utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t)
197197
ASR::List_t *l = (ASR::List_t *)t;
198198
return "list[" + type_to_str_python(l->m_type) + "]";
199199
}
200-
default : throw LFortranException("Not implemented");
200+
case ASR::ttypeType::CPtr: {
201+
return "CPtr";
202+
}
203+
default : throw LFortranException("Not implemented " + std::to_string(t->type));
201204
}
202205
}
203206

src/libasr/codegen/asr_to_c.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
114114
ASR::Derived_t *t = ASR::down_cast<ASR::Derived_t>(v.m_type);
115115
std::string dims = convert_dims_c(t->n_dims, t->m_dims);
116116
sub = format_type_c(dims, "struct", v.m_name, use_ref, dummy);
117+
} else if (ASR::is_a<ASR::CPtr_t>(*v.m_type)) {
118+
sub = format_type_c("", "void*", v.m_name, false, false);
117119
} else {
118120
diag.codegen_error_label("Type number '"
119121
+ std::to_string(v.m_type->type)
@@ -333,6 +335,12 @@ R"(
333335
case ASR::ttypeType::Character: {
334336
return "%s";
335337
}
338+
case ASR::ttypeType::CPtr: {
339+
return "%p";
340+
}
341+
case ASR::ttypeType::Pointer: {
342+
return "%p";
343+
}
336344
default : throw LFortranException("Not implemented");
337345
}
338346
}

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,42 @@ R"(#include <stdio.h>
685685
}
686686
}
687687

688+
std::string get_c_type_from_ttype_t(ASR::ttype_t* t) {
689+
std::string type_src = "";
690+
switch( t->type ) {
691+
case ASR::ttypeType::Integer: {
692+
type_src = "int";
693+
break;
694+
}
695+
case ASR::ttypeType::Pointer: {
696+
ASR::Pointer_t* ptr_type = ASR::down_cast<ASR::Pointer_t>(t);
697+
type_src = get_c_type_from_ttype_t(ptr_type->m_type) + "*";
698+
break;
699+
}
700+
case ASR::ttypeType::CPtr: {
701+
type_src = "void*";
702+
break;
703+
}
704+
default: {
705+
throw CodeGenError("Type " + ASRUtils::type_to_str_python(t) + " not supported yet.");
706+
}
707+
}
708+
return type_src;
709+
}
710+
711+
void visit_GetPointer(const ASR::GetPointer_t& x) {
712+
self().visit_expr(*x.m_arg);
713+
std::string arg_src = std::move(src);
714+
src = "&" + arg_src;
715+
}
716+
717+
void visit_PointerToCPtr(const ASR::PointerToCPtr_t& x) {
718+
self().visit_expr(*x.m_arg);
719+
std::string arg_src = std::move(src);
720+
std::string type_src = get_c_type_from_ttype_t(x.m_type);
721+
src = "(" + type_src + ") " + arg_src;
722+
}
723+
688724
void visit_BinOp(const ASR::BinOp_t &x) {
689725
self().visit_expr(*x.m_left);
690726
std::string left = std::move(src);

0 commit comments

Comments
 (0)