Skip to content

Commit

Permalink
Merge pull request lcompilers#525 from Smit-create/c_pow
Browse files Browse the repository at this point in the history
Fix pow in C backend
  • Loading branch information
Smit-create committed May 22, 2022
2 parents 2150288 + 6b61d64 commit fdeafac
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
public:

ASRToCVisitor(diag::Diagnostics &diag) : BaseCCPPVisitor(diag,
false, false) {}
false, false, true) {}

std::string convert_variable_decl(const ASR::Variable_t &v)
{
Expand Down Expand Up @@ -144,6 +144,7 @@ R"(#include <assert.h>
#include <complex.h>
#include <inttypes.h>
#include <stdio.h>
#include <math.h>
#include <lfortran_intrinsics.h>
Expand Down
9 changes: 6 additions & 3 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Derived>
bool gen_stdstring;
// Use std::complex<float/double> or float/double complex
bool gen_stdcomplex;
bool is_c;

BaseCCPPVisitor(diag::Diagnostics &diag,
bool gen_stdstring, bool gen_stdcomplex) : diag{diag},
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex} {}
bool gen_stdstring, bool gen_stdcomplex, bool is_c) : diag{diag},
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex},
is_c{is_c} {}

void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
// All loose statements must be converted to a function, so the items
Expand Down Expand Up @@ -674,7 +676,8 @@ R"(#include <stdio.h>
case (ASR::binopType::Mul) : { last_expr_precedence = 5; break; }
case (ASR::binopType::Div) : { last_expr_precedence = 5; break; }
case (ASR::binopType::Pow) : {
src = "std::pow(" + left + ", " + right + ")";
src = "pow(" + left + ", " + right + ")";
if (!is_c) src = "std::" + src;
return;
}
default: throw CodeGenError("BinOp: operator not implemented yet");
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
{
public:
ASRToCPPVisitor(diag::Diagnostics &diag) : BaseCCPPVisitor(diag,
true, true) {}
true, true, false) {}

std::string convert_variable_decl(const ASR::Variable_t &v)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/c-c_interop1-e215531.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "c-c_interop1-e215531.stdout",
"stdout_hash": "626931366920a203164bca08821af09d4e3401d5e62fc65cab4d9e76",
"stdout_hash": "ac7de87134ed10075c53ad60385310b20178165421d49aa124020905",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
1 change: 1 addition & 0 deletions tests/reference/c-c_interop1-e215531.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <complex.h>
#include <inttypes.h>
#include <stdio.h>
#include <math.h>

#include <lfortran_intrinsics.h>

Expand Down
13 changes: 13 additions & 0 deletions tests/reference/c-expr7-bb2692a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "c-expr7-bb2692a",
"cmd": "lpython --no-color --show-c {infile}",
"infile": "tests/expr7.py",
"infile_hash": "4a455e2279eb7dd269d81c1a257dea625b17b100d92e304e5ac34421",
"outfile": null,
"outfile_hash": null,
"stdout": "c-expr7-bb2692a.stdout",
"stdout_hash": "1300b234a8ba0b61ca9b134d106f05f2d23538aa3b7bcd4c308e6e47",
"stderr": "c-expr7-bb2692a.stderr",
"stderr_hash": "28509dd59a386eebd632340a550d14299cd2a921ef6dc3ac7dbe7fe9",
"returncode": 0
}
11 changes: 11 additions & 0 deletions tests/reference/c-expr7-bb2692a.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
style suggestion: Could have used '**' instead of 'pow'
--> tests/expr7.py:3:9
|
3 | a = pow(2, 2)
| ^^^^^^^^^ '**' could be used instead

style suggestion: Could have used '**' instead of 'pow'
--> tests/expr7.py:7:11
|
7 | res = pow(a, b)
| ^^^^^^^^^ '**' could be used instead
51 changes: 51 additions & 0 deletions tests/reference/c-expr7-bb2692a.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <assert.h>
#include <complex.h>
#include <inttypes.h>
#include <stdio.h>
#include <math.h>

#include <lfortran_intrinsics.h>

void _lpython_main_program()
{
main0();
}

void main0()
{
int32_t c;
test_pow();
c = test_pow_1(1, 2);
}

void test_pow()
{
int32_t a;
a = __lpython_overloaded_0__pow(2, 2);
}

int test_pow_1(int32_t a, int32_t b)
{
int32_t _lpython_return_variable;
int32_t res;
res = __lpython_overloaded_0__pow(a, b);
_lpython_return_variable = res;
return _lpython_return_variable;
}

int __lpython_overloaded_0__pow(int32_t x, int32_t y)
{
int32_t _lpython_return_variable;
_lpython_return_variable = pow(x, y);
return _lpython_return_variable;
}

float _lfortran_caimag(float complex x);

double _lfortran_zaimag(double complex x);

int main(int argc, char* argv[])
{
_lpython_main_program();
return 0;
}
2 changes: 1 addition & 1 deletion tests/reference/c-loop1-3e341c7.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "c-loop1-3e341c7.stdout",
"stdout_hash": "5c8eae32cbe4df80d617ce6e6c3141dce742fede8793f22fcd48df3a",
"stdout_hash": "a0890fe0052d3a3fe6e74eb71fa408a8883b1bd20d03f8bca0eac59d",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
1 change: 1 addition & 0 deletions tests/reference/c-loop1-3e341c7.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <complex.h>
#include <inttypes.h>
#include <stdio.h>
#include <math.h>

#include <lfortran_intrinsics.h>

Expand Down
1 change: 1 addition & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ filename = "expr7.py"
ast = true
asr = true
cpp = true
c = true

[[test]]
filename = "expr8.py"
Expand Down

0 comments on commit fdeafac

Please sign in to comment.