Skip to content

Commit 593602a

Browse files
authored
Merge pull request #622 from czgdp1807/arr_idx
Supporting C-interop with array indices
2 parents 8ad9e74 + b6eab22 commit 593602a

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ integration_tests/bindc_01
177177
integration_tests/bindc_01.c
178178
integration_tests/bindc_02
179179
integration_tests/bindc_02.c
180+
integration_tests/bindc_03
180181
integration_tests/structs_01
181182
integration_tests/structs_01.c
182183
integration_tests/structs_02

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ RUN(NAME test_c_interop_04 LABELS cpython llvm c
160160
EXTRAFILES test_c_interop_04b.c)
161161
RUN(NAME test_c_interop_05 LABELS llvm c
162162
EXTRAFILES test_c_interop_05b.c)
163+
RUN(NAME bindc_03 LABELS llvm
164+
EXTRAFILES bindc_03b.c)
163165
RUN(NAME test_generics_01 LABELS cpython llvm)
164166
RUN(NAME test_cmath LABELS cpython llvm)
165167
RUN(NAME test_complex LABELS cpython llvm)

integration_tests/bindc_03.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from ltypes import c_p_pointer, CPtr, pointer, i16, i32, Pointer, ccall, p_c_pointer
2+
3+
@ccall
4+
def g(a: CPtr, value: i32) -> None:
5+
pass
6+
7+
@ccall
8+
def get_array(size: i32) -> CPtr:
9+
pass
10+
11+
@ccallable
12+
def f(q_void: CPtr) -> None:
13+
i: i32
14+
el: i32
15+
q: Pointer[i32[:]]
16+
c_p_pointer(q_void, q)
17+
for i in range(10):
18+
q2: CPtr
19+
p_c_pointer(pointer(q[i]), q2)
20+
g(q2, i * i)
21+
# TODO: Use q[i] directly in the assert.
22+
el = q[i]
23+
print(el)
24+
assert el == i * i
25+
26+
def run():
27+
a: CPtr
28+
size: i32
29+
size = 10
30+
a = get_array(size)
31+
f(a)
32+
33+
run()

integration_tests/bindc_03b.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "bindc_03b.h"
2+
#include <stdlib.h>
3+
4+
void g(int32_t* x, int32_t value) {
5+
*x = value;
6+
}
7+
8+
void* get_array(int32_t size) {
9+
return malloc(size * sizeof(int32_t));
10+
}

integration_tests/bindc_03b.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef BINDC_03B
2+
#define BINDC_03B
3+
4+
#include <stdint.h>
5+
6+
void g(int32_t* x, int32_t value);
7+
8+
void* get_array(int32_t size);
9+
10+
#endif // BINDC_03B

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
12231223
std::vector<llvm::Value*> indices;
12241224
for( size_t r = 0; r < x.n_args; r++ ) {
12251225
ASR::array_index_t curr_idx = x.m_args[r];
1226+
uint64_t ptr_loads_copy = ptr_loads;
1227+
ptr_loads = 2;
12261228
this->visit_expr_wrapper(curr_idx.m_right, true);
1229+
ptr_loads = ptr_loads_copy;
12271230
indices.push_back(tmp);
12281231
}
12291232
if (v->m_type->type == ASR::ttypeType::Pointer) {
@@ -2376,6 +2379,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
23762379
case (ASR::ttypeType::Logical) :
23772380
return_type = llvm::Type::getInt1Ty(context);
23782381
break;
2382+
case (ASR::ttypeType::CPtr) :
2383+
return_type = llvm::Type::getVoidTy(context)->getPointerTo();
2384+
break;
23792385
case (ASR::ttypeType::Derived) :
23802386
throw CodeGenError("Derived return type not implemented yet");
23812387
break;

src/runtime/ltypes/ltypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
# TODO: this does not seem to restrict other imports
99
__slots__ = ["i8", "i16", "i32", "i64", "f32", "f64", "c32", "c64", "CPtr",
10-
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer"]
10+
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer",
11+
"p_c_pointer"]
1112

1213
# data-types
1314

0 commit comments

Comments
 (0)