Skip to content

Enabling c backend testing for integration_tests/test_c_interop_01.py #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,6 @@ src/lpython/parser/parser.tab.cc
src/lpython/parser/parser.tab.hh

*.py[0-9A-Za-z]*

##Generated files in integration_tests
integration_tests/test_c_interop_01.c
2 changes: 1 addition & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ RUN(NAME test_builtin_str_02 LABELS cpython llvm)
RUN(NAME test_builtin_round LABELS cpython llvm)
RUN(NAME test_math1 LABELS cpython llvm)
RUN(NAME test_math_02 LABELS cpython llvm)
RUN(NAME test_c_interop_01 LABELS cpython llvm)
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
RUN(NAME test_generics_01 LABELS cpython llvm)
RUN(NAME test_cmath LABELS cpython llvm)
RUN(NAME test_complex LABELS cpython llvm)
Expand Down
11 changes: 6 additions & 5 deletions integration_tests/test_c_interop_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ def _lfortran_bgt32(i: i32, j: i32) -> i32:
pass

@ccall
def _lfortran_bgt64(i: i64, j: i64) -> i64:
def _lfortran_bgt64(i: i64, j: i64) -> i32:
pass

@ccall
def _lfortran_random_number(n: i64, v: f64[:]):
pass
#@ccall
#def _lfortran_random_number(n: i64, v: f64[:]):
# pass

def test_c_callbacks():
pi: f64 = 3.141592653589793238462643383279502884197
pi: f64
pi = 3.141592653589793238462643383279502884197
assert abs(_lfortran_dsin(pi) - 0) < 1e-12
assert abs(_lfortran_dsin(pi/2) - 1) < 1e-12
#assert abs(_lfortran_ssin(pi) - 0) < 1e-6
Expand Down
25 changes: 25 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,31 @@ static inline bool is_logical(ASR::ttype_t &x) {
return ASR::is_a<ASR::Logical_t>(*type_get_past_pointer(&x));
}

static inline int get_body_size(ASR::symbol_t* s) {
int n_body = 0;
switch (s->type) {
case ASR::symbolType::Function: {
ASR::Function_t* f = ASR::down_cast<ASR::Function_t>(s);
n_body = f->n_body;
break;
}
case ASR::symbolType::Subroutine: {
ASR::Subroutine_t* sub = ASR::down_cast<ASR::Subroutine_t>(s);
n_body = sub->n_body;
break;
}
case ASR::symbolType::Program: {
ASR::Program_t* p = ASR::down_cast<ASR::Program_t>(s);
n_body = p->n_body;
break;
}
default: {
n_body = -1;
}
}
return n_body;
}

inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
ASR::dimension_t*& m_dims) {
int n_dims = 0;
Expand Down
23 changes: 12 additions & 11 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ std::string format_type_c(const std::string &dims, const std::string &type,
const std::string &name, bool use_ref, bool /*dummy*/)
{
std::string fmt;
if (dims.size() == 0) {
std::string ref;
if (use_ref) ref = "&";
fmt = type + " " + ref + name;
} else {
throw CodeGenError("Dimensions is not supported yet.");
}
std::string ref = "", ptr = "";
if (dims.size() > 0) ptr = "*";
if (use_ref) ref = "&";
fmt = type + " " + ptr + ref + name;
return fmt;
}

Expand Down Expand Up @@ -200,8 +197,10 @@ R"(#include <assert.h>
!= x.m_global_scope->get_scope().end());
if (startswith(item, "lfortran_intrinsic")) {
ASR::symbol_t *mod = x.m_global_scope->get_symbol(item);
visit_symbol(*mod);
unit_src += src;
if( ASRUtils::get_body_size(mod) != 0 ) {
visit_symbol(*mod);
unit_src += src;
}
}
}
}
Expand All @@ -210,8 +209,10 @@ R"(#include <assert.h>
for (auto &item : x.m_global_scope->get_scope()) {
if (ASR::is_a<ASR::Function_t>(*item.second)
|| ASR::is_a<ASR::Subroutine_t>(*item.second)) {
visit_symbol(*item.second);
unit_src += src;
if( ASRUtils::get_body_size(item.second) != 0 ) {
visit_symbol(*item.second);
unit_src += src;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ R"(#include <stdio.h>
if (ASRUtils::is_integer(*return_var->m_type)) {
bool is_int = ASR::down_cast<ASR::Integer_t>(return_var->m_type)->m_kind == 4;
if (is_int) {
sub = "int ";
sub = "int32_t ";
} else {
sub = "long long ";
sub = "int64_t ";
}
} else if (ASRUtils::is_real(*return_var->m_type)) {
bool is_float = ASR::down_cast<ASR::Real_t>(return_var->m_type)->m_kind == 4;
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ std::string string_format(const std::string& format, Args && ...args)
}

static inline std::string double_to_scientific(double x) {
return string_format("%e", x);
return string_format("%25.17e", x);
}

} // namespace LFortran
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign2-8d1a2ee.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-assign2-8d1a2ee.stdout",
"stdout_hash": "8b2bfbe57a9cfaf48dad7d4bc047fcce2987de92e5633834baa01147",
"stdout_hash": "f5a09dae1cc60e65a6a81de3aeaac7e24e4b704c1d28fc5445e10858",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign2-8d1a2ee.stdout
Original file line number Diff line number Diff line change
@@ -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 [] [])}) [])
(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 [] [])}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-c_interop1-cf2e9b4.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-c_interop1-cf2e9b4.stdout",
"stdout_hash": "61f0d41571959b5f0377c18def80a7487b388339bfa78a1ce2b2cbab",
"stdout_hash": "e27cb11d3d55eb71ea1728e7c5e23462b04bd5b51b68fa8ec7879c24",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-c_interop1-cf2e9b4.stdout
Original file line number Diff line number Diff line change
@@ -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 [] [])}) [])
(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 [] [])}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-complex1-f26c460.stdout",
"stdout_hash": "f637b18b301537d199ac5e728b573eef2492f93f2364e19caf280df4",
"stdout_hash": "773103b451c4cc93c9d0f3a8796001b29daa0fcd54642bf79f30134b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading