Skip to content

Commit 9823eab

Browse files
committed
ready
1 parent a949e7d commit 9823eab

File tree

5 files changed

+88
-54
lines changed

5 files changed

+88
-54
lines changed

integration_tests/test_list_02.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
from ltypes import i32
22

3-
def test_list_01():
3+
def fill_list_str(size: i32) -> list[str]:
44
a: list[str] = ["0_str", "1_str"]
5+
i: i32
6+
for i in range(size):
7+
a.append(str(i + 2) + "_str")
8+
return a
9+
10+
def test_list_01():
11+
a: list[str] = []
512
b: list[str]
613
string: str = "string_"
714
b = [string, string]
815
i: i32
916

17+
a = fill_list_str(10)
18+
1019
for i in range(10):
11-
a.append(str(i + 2) + "_str")
1220
b.append(string + str(i + 2))
1321

1422

integration_tests/test_list_03.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ def test_list_01(n: i32) -> i32:
1010
sum += a[i]
1111
return sum
1212

13-
def test_list_02(n: i32) -> i32:
14-
x: list[i32] = [50, 1]
15-
13+
def test_list_insert_02(x: list[i32], n: i32) -> list[i32]:
1614
i: i32
1715
imod: i32
1816
for i in range(n):
@@ -24,7 +22,15 @@ def test_list_02(n: i32) -> i32:
2422
elif imod == 2:
2523
x.insert(len(x)//2, i + n + 2)
2624

25+
return x
26+
27+
def test_list_02(n: i32) -> i32:
28+
x: list[i32] = [50, 1]
2729
acc: i32 = 0
30+
i: i32
31+
32+
x = test_list_insert_02(x, n)
33+
2834
for i in range(n):
2935
acc += x[i]
3036
return acc
@@ -50,28 +56,9 @@ def test_list_02_string():
5056
assert x[i] == y[i]
5157

5258

53-
def foo(x: i32) -> list[i32]:
54-
y: list[i32] = []
55-
i: i32
56-
for i in range(x):
57-
y.append(i+1)
58-
return y
59-
60-
61-
def test_list_return():
62-
x: list[i32]
63-
x = foo(2)
64-
assert x[0] == 1
65-
assert x[1] == 2
66-
x = foo(5)
67-
assert x[3] == 4
68-
assert x[4] == 5
69-
70-
7159
def verify():
7260
assert test_list_01(11) == 55
7361
assert test_list_02(50) == 3628
7462
test_list_02_string()
75-
test_list_return()
7663

7764
verify()

integration_tests/test_list_05.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,38 @@ def check_list_of_tuples(l: list[tuple[i32, f64, str]], sign: i32):
1818
assert t[2] == string
1919
assert l[i][2] == string
2020

21-
def test_list_of_tuples():
21+
def fill_list_of_tuples(size: i32) -> list[tuple[i32, f64, str]]:
2222
l1: list[tuple[i32, f64, str]] = []
2323
t: tuple[i32, f64, str]
24-
size: i32 = 20
2524
i: i32
26-
string: str
2725

2826
for i in range(size):
2927
t = (i, float(i), str(i) + "_str")
3028
l1.append(t)
3129

30+
return l1
31+
32+
def insert_tuples_into_list(l: list[tuple[i32, f64, str]], size: i32) -> list[tuple[i32, f64, str]]:
33+
i: i32
34+
string: str
35+
t: tuple[i32, f64, str]
36+
37+
for i in range(size//2, size):
38+
string = str(i) + "_str"
39+
t = (i, float(i), string)
40+
l.insert(i, t)
41+
42+
return l
43+
44+
def test_list_of_tuples():
45+
l1: list[tuple[i32, f64, str]] = []
46+
t: tuple[i32, f64, str]
47+
size: i32 = 20
48+
i: i32
49+
string: str
50+
51+
l1 = fill_list_of_tuples(size)
52+
3253
check_list_of_tuples(l1, 1)
3354

3455
for i in range(size//2):
@@ -39,10 +60,7 @@ def test_list_of_tuples():
3960
assert t[1] == size//2 - 1
4061
assert t[2] == str(size//2 - 1) + "_str"
4162

42-
for i in range(size//2, size):
43-
string = str(i) + "_str"
44-
t = (i, float(i), string)
45-
l1.insert(i, t)
63+
l1 = insert_tuples_into_list(l1, size)
4664

4765
check_list_of_tuples(l1, 1)
4866

integration_tests/test_list_07.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
from ltypes import c64, i32
22
from copy import deepcopy
33

4+
def generate_complex_tensors(mat: list[list[c64]], vec: list[c64]) -> list[tuple[list[list[c64]], list[c64]]]:
5+
tensor: tuple[list[list[c64]], list[c64]]
6+
tensors: list[tuple[list[list[c64]], list[c64]]] = []
7+
rows: i32 = len(mat)
8+
cols: i32 = len(vec)
9+
i: i32; j: i32; k: i32
10+
11+
tensor = (deepcopy(mat), deepcopy(vec))
12+
13+
for k in range(2 * rows):
14+
tensors.append(deepcopy(tensor))
15+
for i in range(rows):
16+
for j in range(cols):
17+
mat[i][j] += complex(1.0, 2.0)
18+
19+
for i in range(cols):
20+
vec[i] += complex(1.0, 2.0)
21+
22+
tensor = (deepcopy(mat), deepcopy(vec))
23+
24+
return tensors
25+
426
def test_tuple_with_lists():
527
mat: list[list[c64]] = []
628
vec: list[c64] = []
@@ -42,18 +64,7 @@ def test_tuple_with_lists():
4264
for i in range(cols):
4365
assert tensor[1][i] - vec[i] == -complex(0, 2.0)
4466

45-
tensor = (deepcopy(mat), deepcopy(vec))
46-
47-
for k in range(2 * rows):
48-
tensors.append(deepcopy(tensor))
49-
for i in range(rows):
50-
for j in range(cols):
51-
mat[i][j] += complex(1.0, 2.0)
52-
53-
for i in range(cols):
54-
vec[i] += complex(1.0, 2.0)
55-
56-
tensor = (deepcopy(mat), deepcopy(vec))
67+
tensors = generate_complex_tensors(mat, vec)
5768

5869
for k in range(2 * rows):
5970
for i in range(rows):

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,10 +1131,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
11311131

11321132
void visit_ListConstant(const ASR::ListConstant_t& x) {
11331133
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(x.m_type);
1134-
llvm::Type* llvm_el_type = get_el_type(list_type->m_type);
1134+
bool is_array_type_local = false, is_malloc_array_type_local = false;
1135+
bool is_list_local = false;
1136+
ASR::dimension_t* m_dims_local = nullptr;
1137+
int n_dims_local = -1, a_kind_local = -1;
1138+
llvm::Type* llvm_el_type = get_type_from_ttype_t(list_type->m_type,
1139+
ASR::storage_typeType::Default, is_array_type_local,
1140+
is_malloc_array_type_local, is_list_local, m_dims_local,
1141+
n_dims_local, a_kind_local);
11351142
std::string type_code = ASRUtils::get_type_code(list_type->m_type);
11361143
int32_t type_size = -1;
1137-
if( ASR::is_a<ASR::Character_t>(*list_type->m_type) ) {
1144+
if( ASR::is_a<ASR::Character_t>(*list_type->m_type) ||
1145+
LLVM::is_llvm_struct(list_type->m_type) ||
1146+
ASR::is_a<ASR::Complex_t>(*list_type->m_type) ) {
11381147
llvm::DataLayout data_layout(module.get());
11391148
type_size = data_layout.getTypeAllocSize(llvm_el_type);
11401149
} else {
@@ -2615,8 +2624,17 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
26152624
is_malloc_array_type,
26162625
is_list, m_dims, n_dims,
26172626
a_kind);
2627+
int32_t type_size = -1;
2628+
if( LLVM::is_llvm_struct(asr_list->m_type) ||
2629+
ASR::is_a<ASR::Character_t>(*asr_list->m_type) ||
2630+
ASR::is_a<ASR::Complex_t>(*asr_list->m_type) ) {
2631+
llvm::DataLayout data_layout(module.get());
2632+
type_size = data_layout.getTypeAllocSize(el_llvm_type);
2633+
} else {
2634+
type_size = a_kind;
2635+
}
26182636
std::string el_type_code = ASRUtils::get_type_code(asr_list->m_type);
2619-
return_type = list_api->get_list_type(el_llvm_type, el_type_code, a_kind);
2637+
return_type = list_api->get_list_type(el_llvm_type, el_type_code, type_size);
26202638
break;
26212639
}
26222640
default :
@@ -5228,14 +5246,6 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
52285246
return pointer_to_struct;
52295247
}
52305248

5231-
llvm::Value* CreateCallUtil(llvm::FunctionType* fnty, llvm::Value* fn,
5232-
std::vector<llvm::Value*>& args,
5233-
ASR::ttype_t* asr_return_type) {
5234-
llvm::Value* return_value = builder->CreateCall(fnty, fn, args);
5235-
return CreatePointerToStructReturnValue(fnty, return_value,
5236-
asr_return_type);
5237-
}
5238-
52395249
llvm::Value* CreateCallUtil(llvm::FunctionType* fnty, llvm::Function* fn,
52405250
std::vector<llvm::Value*>& args,
52415251
ASR::ttype_t* asr_return_type) {

0 commit comments

Comments
 (0)