Skip to content

Commit e35af7d

Browse files
committed
Fix C backend for pointer to struct array
1 parent dd076fc commit e35af7d

File tree

7 files changed

+32
-16
lines changed

7 files changed

+32
-16
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ 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
316+
RUN(NAME structs_13 LABELS llvm c
317317
EXTRAFILES structs_13b.c)
318318
RUN(NAME sizeof_01 LABELS llvm c
319319
EXTRAFILES sizeof_01b.c)

src/libasr/codegen/asr_to_c.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,14 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
205205
ASR::ttype_t* element_type, bool& is_fixed_size,
206206
bool convert_to_1d=false)
207207
{
208-
std::string dims;
208+
std::string dims = "";
209209
size_t size = 1;
210210
std::string array_size = "";
211211
for (size_t i=0; i<n_dims; i++) {
212212
ASR::expr_t *length = m_dims[i].m_length;
213213
if (!length) {
214-
dims += "*";
214+
is_fixed_size = false;
215+
return dims;
215216
} else {
216217
visit_expr(*length);
217218
array_size += "*" + src;
@@ -285,7 +286,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
285286
if( !is_fixed_size ) {
286287
sub += indent + format_type_c("*", type_name_copy, std::string(v_m_name) + "_data",
287288
use_ref, dummy);
288-
sub += " = " + dims + ";\n";
289+
if( dims.size() > 0 ) {
290+
sub += " = " + dims + ";\n";
291+
} else {
292+
sub += ";\n";
293+
}
289294
} else {
290295
sub += indent + format_type_c(dims, type_name_copy, std::string(v_m_name) + "_data",
291296
use_ref, dummy) + ";\n";
@@ -360,14 +365,25 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
360365
} else if(ASR::is_a<ASR::Struct_t>(*t2)) {
361366
ASR::Struct_t *t = ASR::down_cast<ASR::Struct_t>(t2);
362367
std::string der_type_name = ASRUtils::symbol_name(t->m_derived_type);
363-
bool is_fixed_size = true;
364-
std::string dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size);
365-
std::string ptr_char = "*";
366-
if( !use_ptr_for_derived_type ) {
367-
ptr_char.clear();
368+
if( is_array ) {
369+
bool is_fixed_size = true;
370+
std::string dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size, true);
371+
std::string encoded_type_name = "x" + der_type_name;
372+
std::string type_name = std::string("struct ") + der_type_name;
373+
generate_array_decl(sub, std::string(v.m_name), type_name, dims,
374+
encoded_type_name, t->m_dims, t->n_dims,
375+
use_ref, dummy,
376+
v.m_intent != ASRUtils::intent_in &&
377+
v.m_intent != ASRUtils::intent_inout,
378+
is_fixed_size);
379+
} else {
380+
std::string ptr_char = "*";
381+
if( !use_ptr_for_derived_type ) {
382+
ptr_char.clear();
383+
}
384+
sub = format_type_c("", "struct " + der_type_name + ptr_char,
385+
v.m_name, use_ref, dummy);
368386
}
369-
sub = format_type_c(dims, "struct " + der_type_name + ptr_char,
370-
v.m_name, use_ref, dummy);
371387
} else if(ASR::is_a<ASR::CPtr_t>(*t2)) {
372388
sub = format_type_c("", "void**", v.m_name, false, false);
373389
} else {

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3082,7 +3082,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30823082
if( is_item ) {
30833083
Vec<ASR::dimension_t> empty_dims;
30843084
empty_dims.reserve(al, 1);
3085-
type = ASRUtils::duplicate_type(al, type, &empty_dims);
3085+
type = ASRUtils::duplicate_type(al, ASRUtils::type_get_past_pointer(type), &empty_dims);
30863086
tmp = ASR::make_ArrayItem_t(al, x.base.base.loc, v_Var, args.p,
30873087
args.size(), type, ASR::arraystorageType::RowMajor, nullptr);
30883088
} else {

tests/reference/asr-bindc_02-bc1a7ea.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_02-bc1a7ea.stdout",
9-
"stdout_hash": "cb68f1c64e50e326c38c102f0dc26a26274be2f4f9d1423658fa0ad4",
9+
"stdout_hash": "3165b2da8871ec50f1243662d58fe93d7b92c3166ef180eca07d2152",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 4 {}) _lpython_main_program [f] [] [(CPtrToPointer (Var 1 queries) (Var 1 x) ()) (Print () [(Var 1 queries) (Var 1 x)] () ()) (SubroutineCall 1 f () [] ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), f: (Function (SymbolTable 2 {y: (Variable 2 y Local () () Default (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))]) Source Public Required .false.), yptr1: (Variable 2 yptr1 Local () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.), yq: (Variable 2 yq Local () () Default (CPtr) Source Public Required .false.)}) f [] [] [(= (ArrayItem (Var 2 y) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) ()) (= (ArrayItem (Var 2 y) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) ()) (= (Var 2 yptr1) (GetPointer (Var 2 y) (Pointer (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))])) ()) ()) (Print () [(GetPointer (Var 2 y) (Pointer (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))])) ()) (Var 2 yptr1)] () ()) (Print () [(ArrayItem (Var 2 yptr1) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ()) (ArrayItem (Var 2 yptr1) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ())] () ()) (Assert (IntegerCompare (ArrayItem (Var 2 yptr1) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ()) Eq (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) (Logical 4 []) ()) ()) (Assert (IntegerCompare (ArrayItem (Var 2 yptr1) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ()) Eq (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) (Logical 4 []) ()) ()) (CPtrToPointer (Var 2 yq) (Var 2 yptr1) ()) (Print () [(Var 2 yq) (Var 2 yptr1)] () ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), main_program: (Program (SymbolTable 3 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())]), queries: (Variable 1 queries Local () () Default (CPtr) Source Public Required .false.), x: (Variable 1 x Local () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.)}) [])
1+
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 4 {}) _lpython_main_program [f] [] [(CPtrToPointer (Var 1 queries) (Var 1 x) ()) (Print () [(Var 1 queries) (Var 1 x)] () ()) (SubroutineCall 1 f () [] ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), f: (Function (SymbolTable 2 {y: (Variable 2 y Local () () Default (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))]) Source Public Required .false.), yptr1: (Variable 2 yptr1 Local () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.), yq: (Variable 2 yq Local () () Default (CPtr) Source Public Required .false.)}) f [] [] [(= (ArrayItem (Var 2 y) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) ()) (= (ArrayItem (Var 2 y) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) ()) (= (Var 2 yptr1) (GetPointer (Var 2 y) (Pointer (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))])) ()) ()) (Print () [(GetPointer (Var 2 y) (Pointer (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))])) ()) (Var 2 yptr1)] () ()) (Print () [(ArrayItem (Var 2 yptr1) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (ArrayItem (Var 2 yptr1) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ())] () ()) (Assert (IntegerCompare (ArrayItem (Var 2 yptr1) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) Eq (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) (Logical 4 []) ()) ()) (Assert (IntegerCompare (ArrayItem (Var 2 yptr1) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) Eq (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) (Logical 4 []) ()) ()) (CPtrToPointer (Var 2 yq) (Var 2 yptr1) ()) (Print () [(Var 2 yq) (Var 2 yptr1)] () ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), main_program: (Program (SymbolTable 3 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())]), queries: (Variable 1 queries Local () () Default (CPtr) Source Public Required .false.), x: (Variable 1 x Local () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.)}) [])

tests/reference/asr-expr_12-6769be0.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-expr_12-6769be0.stdout",
9-
"stdout_hash": "6a3e6336c777defb05cb3c6c244ed20468ea29c584a8bf5c9f311122",
9+
"stdout_hash": "90152a321955a2c86da3e2036530feea66c16f9c4057cfcd8c92113e",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 6 {}) _lpython_main_program [f] [] [(SubroutineCall 1 f () [] ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), check: (Function (SymbolTable 3 {ptr: (Variable 3 ptr InOut () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.)}) check [] [(Var 3 ptr)] [(Assert (IntegerCompare (ArrayItem (Var 3 ptr) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ()) Eq (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) (Logical 4 []) ()) ()) (Assert (IntegerCompare (ArrayItem (Var 3 ptr) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ()) Eq (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) (Logical 4 []) ()) ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), f: (Function (SymbolTable 4 {y: (Variable 4 y Local () () Default (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))]) Source Public Required .false.), yptr1: (Variable 4 yptr1 Local () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.)}) f [g check] [] [(SubroutineCall 1 g () [((Var 4 yptr1)) ((Var 4 y))] ()) (SubroutineCall 1 check () [((Var 4 yptr1))] ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), g: (Function (SymbolTable 2 {x: (Variable 2 x InOut () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.), y: (Variable 2 y InOut () () Default (Integer 2 [(() ())]) Source Public Required .false.)}) g [] [(Var 2 x) (Var 2 y)] [(= (ArrayItem (Var 2 y) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) ()) (= (ArrayItem (Var 2 y) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) ()) (= (Var 2 x) (GetPointer (Var 2 y) (Pointer (Integer 2 [(() ())])) ()) ()) (Print () [(ArrayItem (Var 2 x) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ()) (ArrayItem (Var 2 x) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Pointer (Integer 2 [])) RowMajor ())] () ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), main_program: (Program (SymbolTable 5 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])
1+
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 6 {}) _lpython_main_program [f] [] [(SubroutineCall 1 f () [] ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), check: (Function (SymbolTable 3 {ptr: (Variable 3 ptr InOut () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.)}) check [] [(Var 3 ptr)] [(Assert (IntegerCompare (ArrayItem (Var 3 ptr) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) Eq (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) (Logical 4 []) ()) ()) (Assert (IntegerCompare (ArrayItem (Var 3 ptr) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) Eq (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) (Logical 4 []) ()) ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), f: (Function (SymbolTable 4 {y: (Variable 4 y Local () () Default (Integer 2 [((IntegerConstant 0 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])))]) Source Public Required .false.), yptr1: (Variable 4 yptr1 Local () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.)}) f [g check] [] [(SubroutineCall 1 g () [((Var 4 yptr1)) ((Var 4 y))] ()) (SubroutineCall 1 check () [((Var 4 yptr1))] ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), g: (Function (SymbolTable 2 {x: (Variable 2 x InOut () () Default (Pointer (Integer 2 [(() ())])) Source Public Required .false.), y: (Variable 2 y InOut () () Default (Integer 2 [(() ())]) Source Public Required .false.)}) g [] [(Var 2 x) (Var 2 y)] [(= (ArrayItem (Var 2 y) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 1 (Integer 2 []))) ()) (= (ArrayItem (Var 2 y) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToInteger (Integer 2 []) (IntegerConstant 2 (Integer 2 []))) ()) (= (Var 2 x) (GetPointer (Var 2 y) (Pointer (Integer 2 [(() ())])) ()) ()) (Print () [(ArrayItem (Var 2 x) [(() (IntegerConstant 0 (Integer 4 [])) ())] (Integer 2 []) RowMajor ()) (ArrayItem (Var 2 x) [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 2 []) RowMajor ())] () ())] () Source Public Implementation () .false. .false. .false. .false. .false. [] [] .false.), main_program: (Program (SymbolTable 5 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])

0 commit comments

Comments
 (0)