1414#include < iostream>
1515#include < memory>
1616#include < set>
17- #include < unordered_set>
1817
1918#include < libasr/asr.h>
2019#include < libasr/containers.h>
@@ -84,36 +83,6 @@ struct CPPDeclarationOptions: public DeclarationOptions {
8483 }
8584};
8685
87- class SymEngineQueue {
88- public:
89- std::vector<std::string> queue;
90- int queue_front = -1 ;
91- std::string& symengine_src;
92- std::unordered_set<std::string> variables_to_free;
93-
94- SymEngineQueue (std::string& symengine_src) : symengine_src(symengine_src) {}
95-
96- std::string push () {
97- std::string indent (4 , ' ' );
98- std::string var;
99- if (queue_front == -1 || queue_front >= static_cast <int >(queue.size ())) {
100- var = " queue" + std::to_string (queue.size ());
101- queue.push_back (var);
102- if (queue_front == -1 ) queue_front++;
103- symengine_src = indent + " basic " + var + " ;\n " ;
104- symengine_src += indent + " basic_new_stack(" + var + " );\n " ;
105- }
106- variables_to_free.insert (queue[queue_front]);
107- return queue[queue_front++];
108- }
109-
110- void pop () {
111- LCOMPILERS_ASSERT (queue_front != -1 && queue_front < static_cast <int >(queue.size ()));
112- variables_to_free.insert (queue[queue_front]);
113- queue_front++;
114- }
115- };
116-
11786template <class Struct >
11887class BaseCCPPVisitor : public ASR ::BaseVisitor<Struct>
11988{
@@ -147,8 +116,6 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
147116 bool is_c;
148117 std::set<std::string> headers, user_headers, user_defines;
149118 std::vector<std::string> tmp_buffer_src;
150- std::string symengine_src;
151- SymEngineQueue symengine_queue{symengine_src};
152119
153120 SymbolTable* global_scope;
154121 int64_t lower_bound;
@@ -512,8 +479,6 @@ R"(#include <stdio.h>
512479 }
513480 }
514481 }
515- } else if (ASR::is_a<ASR::SymbolicExpression_t>(*return_var->m_type )) {
516- sub = " basic " ;
517482 } else if (ASR::is_a<ASR::CPtr_t>(*return_var->m_type )) {
518483 sub = " void* " ;
519484 } else if (ASR::is_a<ASR::List_t>(*return_var->m_type )) {
@@ -841,10 +806,6 @@ R"(#include <stdio.h>
841806 if (v->m_intent == ASRUtils::intent_local ||
842807 v->m_intent == ASRUtils::intent_return_var) {
843808 std::string d = indent + self ().convert_variable_decl (*v) + " ;\n " ;
844- if (ASR::is_a<ASR::SymbolicExpression_t>(*v->m_type )) {
845- std::string v_m_name = v->m_name ;
846- d += indent + " basic_new_stack(" + v_m_name + " );\n " ;
847- }
848809 decl += check_tmp_buffer () + d;
849810 }
850811 if (ASR::is_a<ASR::TypeParameter_t>(*v->m_type )) {
@@ -879,10 +840,6 @@ R"(#include <stdio.h>
879840 + " ;\n " ;
880841 }
881842
882- for (const auto & var : symengine_queue.variables_to_free ) {
883- current_body += indent + " basic_free_stack(" + var + " );\n " ;
884- }
885- symengine_queue.variables_to_free .clear ();
886843 if (decl.size () > 0 || current_body.size () > 0 ) {
887844 sub += " {\n " + decl + current_body + " }\n " ;
888845 } else {
@@ -1337,17 +1294,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
13371294 target = " &" + target;
13381295 }
13391296 }
1340- if ( ASR::is_a<ASR::SymbolicExpression_t>(*value_type) ) {
1341- if (ASR::is_a<ASR::Var_t>(*x.m_value )){
1342- src = indent + " basic_assign(" + target + " , " + value + " );\n " ;
1343- symengine_queue.pop ();
1344- symengine_queue.pop ();
1345- return ;
1346- }
1347- src = symengine_src;
1348- symengine_src = " " ;
1349- return ;
1350- }
13511297 if ( !from_std_vector_helper.empty () ) {
13521298 src = from_std_vector_helper;
13531299 } else {
@@ -1825,15 +1771,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
18251771 src = std::string (ASR::down_cast<ASR::Variable_t>(s)->m_name );
18261772 }
18271773 last_expr_precedence = 2 ;
1828- ASR::ttype_t * var_type = sv->m_type ;
1829- if ( ASR::is_a<ASR::SymbolicExpression_t>(*var_type)) {
1830- std::string var_name = std::string (ASR::down_cast<ASR::Variable_t>(s)->m_name );
1831- symengine_queue.queue .push_back (var_name);
1832- if (symengine_queue.queue_front == -1 ) {
1833- symengine_queue.queue_front = 0 ;
1834- }
1835- symengine_src = " " ;
1836- }
18371774 }
18381775
18391776 void visit_StructInstanceMember (const ASR::StructInstanceMember_t& x) {
@@ -2048,11 +1985,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
20481985 last_expr_precedence = 2 ;
20491986 break ;
20501987 }
2051- case (ASR::cast_kindType::IntegerToSymbolicExpression): {
2052- self ().visit_expr (*x.m_value );
2053- last_expr_precedence = 2 ;
2054- break ;
2055- }
20561988 default : throw CodeGenError (" Cast kind " + std::to_string (x.m_kind ) + " not implemented" ,
20571989 x.base .base .loc );
20581990 }
@@ -2100,40 +2032,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
21002032 handle_Compare (x);
21012033 }
21022034
2103- void visit_SymbolicCompare (const ASR::SymbolicCompare_t &x) {
2104- CHECK_FAST_C_CPP (compiler_options, x)
2105- self ().visit_expr (*x.m_left );
2106- std::string left_src = symengine_src;
2107- if (ASR::is_a<ASR::Var_t>(*x.m_left )){
2108- symengine_queue.pop ();
2109- }
2110- std::string left = std::move (src);
2111-
2112- self ().visit_expr (*x.m_right );
2113- std::string right_src = symengine_src;
2114- if (ASR::is_a<ASR::Var_t>(*x.m_right )){
2115- symengine_queue.pop ();
2116- }
2117- std::string right = std::move (src);
2118- std::string op_str = ASRUtils::cmpop_to_str (x.m_op );
2119- switch (x.m_op ) {
2120- case (ASR::cmpopType::Eq) : {
2121- src = " basic_eq(" + left + " , " + right + " ) " + op_str + " 1" ;
2122- break ;
2123- }
2124- case (ASR::cmpopType::NotEq) : {
2125- src = " basic_neq(" + left + " , " + right + " ) " + op_str + " 0" ;
2126- break ;
2127- }
2128- default : {
2129- throw LCompilersException (" Symbolic comparison operator: '"
2130- + op_str
2131- + " ' is not implemented" );
2132- }
2133- }
2134- symengine_src = left_src + right_src;
2135- }
2136-
21372035 template <typename T>
21382036 void handle_Compare (const T &x) {
21392037 CHECK_FAST_C_CPP (compiler_options, x)
@@ -2846,48 +2744,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
28462744 out += func_name; break ; \
28472745 }
28482746
2849- std::string performBinarySymbolicOperation (const std::string& functionName, const ASR::IntrinsicFunction_t& x) {
2850- headers.insert (" symengine/cwrapper.h" );
2851- std::string indent (4 , ' ' );
2852- LCOMPILERS_ASSERT (x.n_args == 2 );
2853- std::string target = symengine_queue.push ();
2854- std::string target_src = symengine_src;
2855- this ->visit_expr (*x.m_args [0 ]);
2856- std::string arg1 = src;
2857- std::string arg1_src = symengine_src;
2858- // Check if x.m_args[0] is a Var
2859- if (ASR::is_a<ASR::Var_t>(*x.m_args [0 ])) {
2860- symengine_queue.pop ();
2861- }
2862- this ->visit_expr (*x.m_args [1 ]);
2863- std::string arg2 = src;
2864- std::string arg2_src = symengine_src;
2865- // Check if x.m_args[0] is a Var
2866- if (ASR::is_a<ASR::Var_t>(*x.m_args [1 ])) {
2867- symengine_queue.pop ();
2868- }
2869- symengine_src = target_src + arg1_src + arg2_src;
2870- symengine_src += indent + functionName + " (" + target + " , " + arg1 + " , " + arg2 + " );\n " ;
2871- return target;
2872- }
2873-
2874- std::string performUnarySymbolicOperation (const std::string& functionName, const ASR::IntrinsicFunction_t& x) {
2875- headers.insert (" symengine/cwrapper.h" );
2876- std::string indent (4 , ' ' );
2877- LCOMPILERS_ASSERT (x.n_args == 1 );
2878- std::string target = symengine_queue.push ();
2879- std::string target_src = symengine_src;
2880- this ->visit_expr (*x.m_args [0 ]);
2881- std::string arg1 = src;
2882- std::string arg1_src = symengine_src;
2883- if (ASR::is_a<ASR::Var_t>(*x.m_args [0 ])) {
2884- symengine_queue.pop ();
2885- }
2886- symengine_src = target_src + arg1_src;
2887- symengine_src += indent + functionName + " (" + target + " , " + arg1 + " );\n " ;
2888- return target;
2889- }
2890-
28912747 void visit_IntrinsicFunction (const ASR::IntrinsicFunction_t &x) {
28922748 std::string out;
28932749 std::string indent (4 , ' ' );
@@ -2905,80 +2761,6 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
29052761 SET_INTRINSIC_NAME (Exp, " exp" );
29062762 SET_INTRINSIC_NAME (Exp2, " exp2" );
29072763 SET_INTRINSIC_NAME (Expm1, " expm1" );
2908- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicAdd)): {
2909- src = performBinarySymbolicOperation (" basic_add" , x);
2910- return ;
2911- }
2912- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicSub)): {
2913- src = performBinarySymbolicOperation (" basic_sub" , x);
2914- return ;
2915- }
2916- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicMul)): {
2917- src = performBinarySymbolicOperation (" basic_mul" , x);
2918- return ;
2919- }
2920- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicDiv)): {
2921- src = performBinarySymbolicOperation (" basic_div" , x);
2922- return ;
2923- }
2924- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicPow)): {
2925- src = performBinarySymbolicOperation (" basic_pow" , x);
2926- return ;
2927- }
2928- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicDiff)): {
2929- src = performBinarySymbolicOperation (" basic_diff" , x);
2930- return ;
2931- }
2932- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicSin)): {
2933- src = performUnarySymbolicOperation (" basic_sin" , x);
2934- return ;
2935- }
2936- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicCos)): {
2937- src = performUnarySymbolicOperation (" basic_cos" , x);
2938- return ;
2939- }
2940- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicLog)): {
2941- src = performUnarySymbolicOperation (" basic_log" , x);
2942- return ;
2943- }
2944- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicExp)): {
2945- src = performUnarySymbolicOperation (" basic_exp" , x);
2946- return ;
2947- }
2948- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicAbs)): {
2949- src = performUnarySymbolicOperation (" basic_abs" , x);
2950- return ;
2951- }
2952- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicExpand)): {
2953- src = performUnarySymbolicOperation (" basic_expand" , x);
2954- return ;
2955- }
2956- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicPi)): {
2957- headers.insert (" symengine/cwrapper.h" );
2958- LCOMPILERS_ASSERT (x.n_args == 0 );
2959- std::string target = symengine_queue.push ();
2960- symengine_src += indent + " basic_const_pi(" + target + " );\n " ;
2961- src = target;
2962- return ;
2963- }
2964- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicSymbol)): {
2965- headers.insert (" symengine/cwrapper.h" );
2966- LCOMPILERS_ASSERT (x.n_args == 1 );
2967- this ->visit_expr (*x.m_args [0 ]);
2968- std::string target = symengine_queue.push ();
2969- symengine_src += indent + " symbol_set(" + target + " , " + src + " );\n " ;
2970- src = target;
2971- return ;
2972- }
2973- case (static_cast <int64_t >(ASRUtils::IntrinsicFunctions::SymbolicInteger)): {
2974- headers.insert (" symengine/cwrapper.h" );
2975- LCOMPILERS_ASSERT (x.n_args == 1 );
2976- this ->visit_expr (*x.m_args [0 ]);
2977- std::string target = symengine_queue.push ();
2978- symengine_src += indent + " integer_set_si(" + target + " , " + src + " );\n " ;
2979- src = target;
2980- return ;
2981- }
29822764 default : {
29832765 throw LCompilersException (" IntrinsicFunction: `"
29842766 + ASRUtils::get_intrinsic_name (x.m_intrinsic_id )
0 commit comments