Skip to content

Commit 6dbb04a

Browse files
authored
Merge pull request #574 from czgdp1807/test_c_interop
Enabling ``c`` backend testing for ``integration_tests/test_c_interop_01.py``
2 parents 32c5853 + 140aa7e commit 6dbb04a

File tree

87 files changed

+177
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+177
-151
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,6 @@ src/lpython/parser/parser.tab.cc
163163
src/lpython/parser/parser.tab.hh
164164

165165
*.py[0-9A-Za-z]*
166+
167+
##Generated files in integration_tests
168+
integration_tests/test_c_interop_01.c

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ RUN(NAME test_builtin_str_02 LABELS cpython llvm)
143143
RUN(NAME test_builtin_round LABELS cpython llvm)
144144
RUN(NAME test_math1 LABELS cpython llvm)
145145
RUN(NAME test_math_02 LABELS cpython llvm)
146-
RUN(NAME test_c_interop_01 LABELS cpython llvm)
146+
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
147147
RUN(NAME test_generics_01 LABELS cpython llvm)
148148
RUN(NAME test_cmath LABELS cpython llvm)
149149
RUN(NAME test_complex LABELS cpython llvm)

integration_tests/test_c_interop_01.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ def _lfortran_bgt32(i: i32, j: i32) -> i32:
1414
pass
1515

1616
@ccall
17-
def _lfortran_bgt64(i: i64, j: i64) -> i64:
17+
def _lfortran_bgt64(i: i64, j: i64) -> i32:
1818
pass
1919

20-
@ccall
21-
def _lfortran_random_number(n: i64, v: f64[:]):
22-
pass
20+
#@ccall
21+
#def _lfortran_random_number(n: i64, v: f64[:]):
22+
# pass
2323

2424
def test_c_callbacks():
25-
pi: f64 = 3.141592653589793238462643383279502884197
25+
pi: f64
26+
pi = 3.141592653589793238462643383279502884197
2627
assert abs(_lfortran_dsin(pi) - 0) < 1e-12
2728
assert abs(_lfortran_dsin(pi/2) - 1) < 1e-12
2829
#assert abs(_lfortran_ssin(pi) - 0) < 1e-6

src/libasr/asr_utils.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,31 @@ static inline bool is_logical(ASR::ttype_t &x) {
820820
return ASR::is_a<ASR::Logical_t>(*type_get_past_pointer(&x));
821821
}
822822

823+
static inline int get_body_size(ASR::symbol_t* s) {
824+
int n_body = 0;
825+
switch (s->type) {
826+
case ASR::symbolType::Function: {
827+
ASR::Function_t* f = ASR::down_cast<ASR::Function_t>(s);
828+
n_body = f->n_body;
829+
break;
830+
}
831+
case ASR::symbolType::Subroutine: {
832+
ASR::Subroutine_t* sub = ASR::down_cast<ASR::Subroutine_t>(s);
833+
n_body = sub->n_body;
834+
break;
835+
}
836+
case ASR::symbolType::Program: {
837+
ASR::Program_t* p = ASR::down_cast<ASR::Program_t>(s);
838+
n_body = p->n_body;
839+
break;
840+
}
841+
default: {
842+
n_body = -1;
843+
}
844+
}
845+
return n_body;
846+
}
847+
823848
inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
824849
ASR::dimension_t*& m_dims) {
825850
int n_dims = 0;

src/libasr/codegen/asr_to_c.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ std::string format_type_c(const std::string &dims, const std::string &type,
4444
const std::string &name, bool use_ref, bool /*dummy*/)
4545
{
4646
std::string fmt;
47-
if (dims.size() == 0) {
48-
std::string ref;
49-
if (use_ref) ref = "&";
50-
fmt = type + " " + ref + name;
51-
} else {
52-
throw CodeGenError("Dimensions is not supported yet.");
53-
}
47+
std::string ref = "", ptr = "";
48+
if (dims.size() > 0) ptr = "*";
49+
if (use_ref) ref = "&";
50+
fmt = type + " " + ptr + ref + name;
5451
return fmt;
5552
}
5653

@@ -200,8 +197,10 @@ R"(#include <assert.h>
200197
!= x.m_global_scope->get_scope().end());
201198
if (startswith(item, "lfortran_intrinsic")) {
202199
ASR::symbol_t *mod = x.m_global_scope->get_symbol(item);
203-
visit_symbol(*mod);
204-
unit_src += src;
200+
if( ASRUtils::get_body_size(mod) != 0 ) {
201+
visit_symbol(*mod);
202+
unit_src += src;
203+
}
205204
}
206205
}
207206
}
@@ -210,8 +209,10 @@ R"(#include <assert.h>
210209
for (auto &item : x.m_global_scope->get_scope()) {
211210
if (ASR::is_a<ASR::Function_t>(*item.second)
212211
|| ASR::is_a<ASR::Subroutine_t>(*item.second)) {
213-
visit_symbol(*item.second);
214-
unit_src += src;
212+
if( ASRUtils::get_body_size(item.second) != 0 ) {
213+
visit_symbol(*item.second);
214+
unit_src += src;
215+
}
215216
}
216217
}
217218

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ R"(#include <stdio.h>
243243
if (ASRUtils::is_integer(*return_var->m_type)) {
244244
bool is_int = ASR::down_cast<ASR::Integer_t>(return_var->m_type)->m_kind == 4;
245245
if (is_int) {
246-
sub = "int ";
246+
sub = "int32_t ";
247247
} else {
248-
sub = "long long ";
248+
sub = "int64_t ";
249249
}
250250
} else if (ASRUtils::is_real(*return_var->m_type)) {
251251
bool is_float = ASR::down_cast<ASR::Real_t>(return_var->m_type)->m_kind == 4;

src/libasr/containers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ std::string string_format(const std::string& format, Args && ...args)
190190
}
191191

192192
static inline std::string double_to_scientific(double x) {
193-
return string_format("%e", x);
193+
return string_format("%25.17e", x);
194194
}
195195

196196
} // namespace LFortran

tests/reference/asr-assign2-8d1a2ee.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-assign2-8d1a2ee.stdout",
9-
"stdout_hash": "8b2bfbe57a9cfaf48dad7d4bc047fcce2987de92e5633834baa01147",
9+
"stdout_hash": "f5a09dae1cc60e65a6a81de3aeaac7e24e4b704c1d28fc5445e10858",
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 {f: (Variable 1 f Local () (Cast (RealConstant 1.234568e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.234568e+00 (Real 4 []))) Default (Real 4 []) Source Public Required .false.), f2: (Variable 1 f2 Local () (RealConstant 1.234568e+00 (Real 8 [])) Default (Real 8 []) Source Public Required .false.), i: (Variable 1 i Local () (IntegerConstant 5 (Integer 4 [])) Default (Integer 4 []) Source Public Required .false.), i2: (Variable 1 i2 Local () (Cast (IntegerConstant 53430903434 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) Default (Integer 8 []) Source Public Required .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [])}) [])
1+
(TranslationUnit (SymbolTable 1 {f: (Variable 1 f Local () (Cast (RealConstant 1.23456788999999989e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.23456788999999989e+00 (Real 4 []))) Default (Real 4 []) Source Public Required .false.), f2: (Variable 1 f2 Local () (RealConstant 1.23456789012340007e+00 (Real 8 [])) Default (Real 8 []) Source Public Required .false.), i: (Variable 1 i Local () (IntegerConstant 5 (Integer 4 [])) Default (Integer 4 []) Source Public Required .false.), i2: (Variable 1 i2 Local () (Cast (IntegerConstant 53430903434 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) Default (Integer 8 []) Source Public Required .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [])}) [])

tests/reference/asr-c_interop1-cf2e9b4.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-c_interop1-cf2e9b4.stdout",
9-
"stdout_hash": "61f0d41571959b5f0377c18def80a7487b388339bfa78a1ce2b2cbab",
9+
"stdout_hash": "e27cb11d3d55eb71ea1728e7c5e23462b04bd5b51b68fa8ec7879c24",
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 {f: (Function (SymbolTable 2 {_lpython_return_variable: (Variable 2 _lpython_return_variable ReturnVar () () Default (Real 8 []) BindC Public Required .false.), x: (Variable 2 x In () () Default (Real 8 []) BindC Public Required .true.)}) f [(Var 2 x)] [] (Var 2 _lpython_return_variable) BindC Public Interface ()), g: (Subroutine (SymbolTable 3 {a: (Variable 3 a In () () Default (Real 8 []) BindC Public Required .true.), b: (Variable 3 b In () () Default (Real 4 []) BindC Public Required .true.), c: (Variable 3 c In () () Default (Integer 8 []) BindC Public Required .true.), d: (Variable 3 d In () () Default (Integer 4 []) BindC Public Required .true.)}) g [(Var 3 a) (Var 3 b) (Var 3 c) (Var 3 d)] [] BindC Public Interface () .false. .false.), h: (Function (SymbolTable 4 {_lpython_return_variable: (Variable 4 _lpython_return_variable ReturnVar () () Default (Real 8 []) BindC Public Required .false.), x: (Variable 4 x In () () Default (Real 8 []) BindC Public Required .true.)}) h [(Var 4 x)] [(= (Var 4 _lpython_return_variable) (BinOp (Var 4 x) Add (RealConstant 1.000000e+00 (Real 8 [])) (Real 8 []) () ()) ()) (Return)] (Var 4 _lpython_return_variable) BindC Public Implementation ()), l: (Subroutine (SymbolTable 5 {a: (Variable 5 a In () () Default (Real 8 []) BindC Public Required .true.), b: (Variable 5 b In () () Default (Real 4 []) BindC Public Required .true.), c: (Variable 5 c In () () Default (Integer 8 []) BindC Public Required .true.), d: (Variable 5 d In () () Default (Integer 4 []) BindC Public Required .true.)}) l [(Var 5 a) (Var 5 b) (Var 5 c) (Var 5 d)] [(Print () [(StringConstant "OK" (Character 1 2 () []))])] BindC Public Implementation () .false. .false.), main0: (Subroutine (SymbolTable 6 {i: (Variable 6 i Local () () Default (Real 8 []) Source Public Required .false.), x: (Variable 6 x Local () () Default (Real 8 []) Source Public Required .false.), y: (Variable 6 y Local () () Default (Real 4 []) Source Public Required .false.), z: (Variable 6 z Local () () Default (Integer 8 []) Source Public Required .false.), zz: (Variable 6 zz Local () () Default (Integer 4 []) Source Public Required .false.)}) main0 [] [(= (Var 6 x) (RealConstant 5.000000e+00 (Real 8 [])) ()) (= (Var 6 i) (FunctionCall 1 f () [((Var 6 x))] (Real 8 []) () ()) ()) (= (Var 6 y) (Cast (RealConstant 5.400000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 5.400000e+00 (Real 4 []))) ()) (= (Var 6 z) (Cast (IntegerConstant 3 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 6 zz) (IntegerConstant 2 (Integer 4 [])) ()) (SubroutineCall 1 g () [((Var 6 x)) ((Var 6 y)) ((Var 6 z)) ((Var 6 zz))] ()) (= (Var 6 i) (FunctionCall 1 h () [((Var 6 x))] (Real 8 []) () ()) ()) (SubroutineCall 1 l () [((Var 6 x)) ((Var 6 y)) ((Var 6 z)) ((Var 6 zz))] ())] Source Public Implementation () .false. .false.), main_program: (Program (SymbolTable 7 {}) main_program [] [])}) [])
1+
(TranslationUnit (SymbolTable 1 {f: (Function (SymbolTable 2 {_lpython_return_variable: (Variable 2 _lpython_return_variable ReturnVar () () Default (Real 8 []) BindC Public Required .false.), x: (Variable 2 x In () () Default (Real 8 []) BindC Public Required .true.)}) f [(Var 2 x)] [] (Var 2 _lpython_return_variable) BindC Public Interface ()), g: (Subroutine (SymbolTable 3 {a: (Variable 3 a In () () Default (Real 8 []) BindC Public Required .true.), b: (Variable 3 b In () () Default (Real 4 []) BindC Public Required .true.), c: (Variable 3 c In () () Default (Integer 8 []) BindC Public Required .true.), d: (Variable 3 d In () () Default (Integer 4 []) BindC Public Required .true.)}) g [(Var 3 a) (Var 3 b) (Var 3 c) (Var 3 d)] [] BindC Public Interface () .false. .false.), h: (Function (SymbolTable 4 {_lpython_return_variable: (Variable 4 _lpython_return_variable ReturnVar () () Default (Real 8 []) BindC Public Required .false.), x: (Variable 4 x In () () Default (Real 8 []) BindC Public Required .true.)}) h [(Var 4 x)] [(= (Var 4 _lpython_return_variable) (BinOp (Var 4 x) Add (RealConstant 1.00000000000000000e+00 (Real 8 [])) (Real 8 []) () ()) ()) (Return)] (Var 4 _lpython_return_variable) BindC Public Implementation ()), l: (Subroutine (SymbolTable 5 {a: (Variable 5 a In () () Default (Real 8 []) BindC Public Required .true.), b: (Variable 5 b In () () Default (Real 4 []) BindC Public Required .true.), c: (Variable 5 c In () () Default (Integer 8 []) BindC Public Required .true.), d: (Variable 5 d In () () Default (Integer 4 []) BindC Public Required .true.)}) l [(Var 5 a) (Var 5 b) (Var 5 c) (Var 5 d)] [(Print () [(StringConstant "OK" (Character 1 2 () []))])] BindC Public Implementation () .false. .false.), main0: (Subroutine (SymbolTable 6 {i: (Variable 6 i Local () () Default (Real 8 []) Source Public Required .false.), x: (Variable 6 x Local () () Default (Real 8 []) Source Public Required .false.), y: (Variable 6 y Local () () Default (Real 4 []) Source Public Required .false.), z: (Variable 6 z Local () () Default (Integer 8 []) Source Public Required .false.), zz: (Variable 6 zz Local () () Default (Integer 4 []) Source Public Required .false.)}) main0 [] [(= (Var 6 x) (RealConstant 5.00000000000000000e+00 (Real 8 [])) ()) (= (Var 6 i) (FunctionCall 1 f () [((Var 6 x))] (Real 8 []) () ()) ()) (= (Var 6 y) (Cast (RealConstant 5.40000000000000036e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 5.40000000000000036e+00 (Real 4 []))) ()) (= (Var 6 z) (Cast (IntegerConstant 3 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 6 zz) (IntegerConstant 2 (Integer 4 [])) ()) (SubroutineCall 1 g () [((Var 6 x)) ((Var 6 y)) ((Var 6 z)) ((Var 6 zz))] ()) (= (Var 6 i) (FunctionCall 1 h () [((Var 6 x))] (Real 8 []) () ()) ()) (SubroutineCall 1 l () [((Var 6 x)) ((Var 6 y)) ((Var 6 z)) ((Var 6 zz))] ())] Source Public Implementation () .false. .false.), main_program: (Program (SymbolTable 7 {}) main_program [] [])}) [])

tests/reference/asr-complex1-f26c460.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-complex1-f26c460.stdout",
9-
"stdout_hash": "f637b18b301537d199ac5e728b573eef2492f93f2364e19caf280df4",
9+
"stdout_hash": "773103b451c4cc93c9d0f3a8796001b29daa0fcd54642bf79f30134b",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)