Skip to content

Commit dd076fc

Browse files
committed
Fix LLVM for pointer to structs
1 parent 42ff991 commit dd076fc

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ RUN(NAME structs_08 LABELS cpython llvm c)
313313
RUN(NAME structs_09 LABELS cpython llvm c)
314314
RUN(NAME structs_10 LABELS cpython llvm c)
315315
RUN(NAME structs_12 LABELS cpython llvm c)
316+
RUN(NAME structs_13 LABELS llvm
317+
EXTRAFILES structs_13b.c)
316318
RUN(NAME sizeof_01 LABELS llvm c
317319
EXTRAFILES sizeof_01b.c)
318320
RUN(NAME enum_01 LABELS cpython llvm c)

integration_tests/structs_13.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from ltypes import i32, i16, i64, CPtr, dataclass, ccall, Pointer, c_p_pointer
2+
3+
@dataclass
4+
class A:
5+
x: i32
6+
y: i16
7+
8+
@ccall
9+
def cmalloc(size: i64) -> CPtr:
10+
pass
11+
12+
@ccall
13+
def is_null(ptr: CPtr) -> i32:
14+
pass
15+
16+
def add_A_members(Ax: i32, Ay: i16) -> i32:
17+
return Ax + i32(Ay)
18+
19+
def test_A_member_passing():
20+
array_cptr: CPtr = cmalloc(sizeof(A) * i64(10))
21+
assert not bool(is_null(array_cptr)), "Failed to allocate array on memory"
22+
array_ptr: Pointer[A[:]]
23+
c_p_pointer(array_cptr, array_ptr)
24+
i: i32; sum_A_members: i32
25+
for i in range(10):
26+
array_ptr[i] = A(i, i16(i + 1))
27+
28+
for i in range(5):
29+
a: A = array_ptr[i]
30+
sum_A_members = add_A_members(a.x, a.y)
31+
assert a.x == i
32+
assert a.y == i16(i + 1)
33+
print(sum_A_members)
34+
assert sum_A_members == 2*i + 1
35+
36+
for i in range(6, 10):
37+
sum_A_members = add_A_members(array_ptr[i].x, array_ptr[i].y)
38+
assert array_ptr[i].x == i
39+
assert array_ptr[i].y == i16(i + 1)
40+
print(sum_A_members)
41+
assert sum_A_members == 2*i + 1
42+
43+
test_A_member_passing()

integration_tests/structs_13b.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdlib.h>
2+
3+
void* cmalloc(int64_t size) {
4+
return malloc(size);
5+
}
6+
7+
int32_t is_null(void* ptr) {
8+
return ptr == NULL;
9+
}

integration_tests/structs_13b.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <inttypes.h>
2+
3+
void* cmalloc(int64_t size);
4+
int32_t is_null(void* ptr);

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,8 +1685,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16851685
llvm::Value* array = nullptr;
16861686
if( ASR::is_a<ASR::Var_t>(*x.m_v) ) {
16871687
ASR::Variable_t *v = ASRUtils::EXPR2VAR(x.m_v);
1688-
if( ASR::is_a<ASR::Struct_t>(*v->m_type) ) {
1689-
ASR::Struct_t* der_type = ASR::down_cast<ASR::Struct_t>(v->m_type);
1688+
if( ASR::is_a<ASR::Struct_t>(*ASRUtils::get_contained_type(v->m_type)) ) {
1689+
ASR::Struct_t* der_type = ASR::down_cast<ASR::Struct_t>(
1690+
ASRUtils::get_contained_type(v->m_type));
16901691
der_type_name = ASRUtils::symbol_name(ASRUtils::symbol_get_past_external(der_type->m_derived_type));
16911692
}
16921693
uint32_t v_h = get_hash((ASR::asr_t*)v);

src/libasr/pass/pass_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ namespace LFortran {
180180
}
181181
}
182182

183+
void visit_DoLoop(const ASR::DoLoop_t& x) {
184+
self().visit_do_loop_head(x.m_head);
185+
ASR::DoLoop_t& xx = const_cast<ASR::DoLoop_t&>(x);
186+
transform_stmts(xx.m_body, xx.n_body);
187+
}
188+
183189
};
184190

185191
template <class Struct>

0 commit comments

Comments
 (0)