Skip to content

Commit 98f5c67

Browse files
authored
Merge pull request #1137 from Smit-create/complex1
Implement C backend for complex numbers
2 parents 2ee6de7 + 4f9cc9c commit 98f5c67

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ RUN(NAME bindc_03 LABELS llvm
235235
EXTRAFILES bindc_03b.c)
236236
RUN(NAME test_generics_01 LABELS cpython llvm)
237237
RUN(NAME test_cmath LABELS cpython llvm)
238-
RUN(NAME test_complex LABELS cpython llvm)
238+
RUN(NAME test_complex LABELS cpython llvm c)
239239
RUN(NAME test_max_min LABELS cpython llvm)
240240
RUN(NAME test_global LABELS cpython llvm)
241241
RUN(NAME test_integer_bitnot LABELS cpython llvm)

src/libasr/codegen/asr_to_c.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,9 @@ R"(
590590

591591
void visit_ComplexConstant(const ASR::ComplexConstant_t &x) {
592592
headers.insert("complex");
593-
594-
ASR::Complex_t *t = ASR::down_cast<ASR::Complex_t>(x.m_type);
595-
596593
std::string re = std::to_string(x.m_re);
597594
std::string im = std::to_string(x.m_im);
598-
599-
std::string type_name = "float complex";
600-
if (t->m_kind == 8) type_name = "double complex";
601-
602-
src = "(" + type_name + ") (" + re + ", " + im + ")";
595+
src = "CMPLX(" + re + ", " + im + ")";
603596

604597
last_expr_precedence = 2;
605598
}

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,10 @@ R"(#include <stdio.h>
748748
handle_UnaryMinus(x);
749749
}
750750

751+
void visit_ComplexUnaryMinus(const ASR::ComplexUnaryMinus_t &x) {
752+
handle_UnaryMinus(x);
753+
}
754+
751755
template <typename T>
752756
void handle_UnaryMinus(const T &x) {
753757
self().visit_expr(*x.m_arg);
@@ -760,6 +764,26 @@ R"(#include <stdio.h>
760764
}
761765
}
762766

767+
void visit_ComplexRe(const ASR::ComplexRe_t &x) {
768+
headers.insert("complex");
769+
self().visit_expr(*x.m_arg);
770+
if (is_c) {
771+
src = "creal(" + src + ")";
772+
} else {
773+
src = src + ".real()";
774+
}
775+
}
776+
777+
void visit_ComplexIm(const ASR::ComplexIm_t &x) {
778+
headers.insert("complex");
779+
self().visit_expr(*x.m_arg);
780+
if (is_c) {
781+
src = "cimag(" + src + ")";
782+
} else {
783+
src = src + ".imag()";
784+
}
785+
}
786+
763787
void visit_LogicalNot(const ASR::LogicalNot_t &x) {
764788
self().visit_expr(*x.m_arg);
765789
int expr_precedence = last_expr_precedence;

0 commit comments

Comments
 (0)