From a3bca7918e8b1d291ef3ff351b69a82a47d45720 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sat, 10 Aug 2024 18:44:56 +0530 Subject: [PATCH] changes according to code review --- src/bin/lpython.cpp | 3 ++- src/libasr/pass/python_bind.cpp | 47 +++++++++++++++------------------ src/libasr/utils.h | 2 +- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 971184baba..a3b9a7eb4b 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -321,7 +321,7 @@ int emit_c(const std::string &infile, pass_manager.use_default_passes(true); compiler_options.po.always_run = true; compiler_options.po.run_fun = "f"; - compiler_options.po.c_backend = true; + compiler_options.po.c_skip_bindpy_pass = true; pass_manager.apply_passes(al, asr, compiler_options.po, diagnostics); @@ -371,6 +371,7 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile, compiler_options.po.run_fun = "f"; compiler_options.po.always_run = true; + compiler_options.po.c_skip_bindpy_pass = true; pass_manager.use_default_passes(true); pass_manager.apply_passes(al, asr, compiler_options.po, diagnostics); diff --git a/src/libasr/pass/python_bind.cpp b/src/libasr/pass/python_bind.cpp index 4174657b9e..a9bf51601b 100644 --- a/src/libasr/pass/python_bind.cpp +++ b/src/libasr/pass/python_bind.cpp @@ -149,7 +149,7 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) Vec body; body.reserve(al, 1); Str s; - + /* if (!Py_IsInitialized()) { Py_Initialize(); @@ -182,11 +182,11 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) s.from_str(al, "pS"); ASR::asr_t *pS = ASR::make_Variable_t(al, f.base.base.loc, f.m_symtab, s.c_str(al), nullptr, 0, ASRUtils::intent_local, nullptr, nullptr, ASR::storage_typeType::Default, i4_type, nullptr, - ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, false); + ASR::abiType::Source, ASR::Public, ASR::presenceType::Required, false); ASR::expr_t *pS_ref = ASRUtils::EXPR(ASR::make_Var_t(al, f.base.base.loc, ASR::down_cast(pS))); f.m_symtab->add_symbol(std::string("pS"), ASR::down_cast(pS)); if_body.push_back(al, ASRUtils::STMT( - ASR::make_Assignment_t(al, f.base.base.loc, pS_ref, + ASR::make_Assignment_t(al, f.base.base.loc, pS_ref, ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f.base.base.loc, sym_PyRun_SimpleString, nullptr, args_PyRun_SimpleString.p, args_PyRun_SimpleString.n, i4_type, nullptr, nullptr)), nullptr))); @@ -211,15 +211,15 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) s.from_str(al, "pName"); ASR::asr_t *pName = ASR::make_Variable_t(al, f.base.base.loc, f.m_symtab, s.c_str(al), nullptr, 0, ASRUtils::intent_local, nullptr, nullptr, ASR::storage_typeType::Default, ptr_t, nullptr, - ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, false); + ASR::abiType::Source, ASR::Public, ASR::presenceType::Required, false); ASR::expr_t *pName_ref = ASRUtils::EXPR(ASR::make_Var_t(al, f.base.base.loc, ASR::down_cast(pName))); f.m_symtab->add_symbol(std::string("pName"), ASR::down_cast(pName)); body.push_back(al, ASRUtils::STMT( - ASR::make_Assignment_t(al, f.base.base.loc, pName_ref, + ASR::make_Assignment_t(al, f.base.base.loc, pName_ref, ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f.base.base.loc, sym_PyUnicode_FromString, nullptr, args_PyUnicode_FromString.p, args_PyUnicode_FromString.n, ptr_t, nullptr, nullptr)), nullptr))); - + ASR::symbol_t *sym_PyImport_Import = parent_scope.get_symbol("PyImport_Import"); Vec args_PyImport_Import; args_PyImport_Import.reserve(al, 1); @@ -227,7 +227,7 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) s.from_str(al, "pModule"); ASR::asr_t *pModule = ASR::make_Variable_t(al, f.base.base.loc, f.m_symtab, s.c_str(al), nullptr, 0, ASRUtils::intent_local, nullptr, nullptr, ASR::storage_typeType::Default, ptr_t, nullptr, - ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, false); + ASR::abiType::Source, ASR::Public, ASR::presenceType::Required, false); ASR::expr_t *pModule_ref = ASRUtils::EXPR(ASR::make_Var_t(al, f.base.base.loc, ASR::down_cast(pModule))); f.m_symtab->add_symbol(std::string("pModule"), ASR::down_cast(pModule)); @@ -236,7 +236,7 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f.base.base.loc, sym_PyImport_Import, nullptr, args_PyImport_Import.p, args_PyImport_Import.n, ptr_t, nullptr, nullptr)), nullptr))); - + ASR::symbol_t *sym_PyObject_GetAttrString = parent_scope.get_symbol("PyObject_GetAttrString"); Vec args_PyObject_GetAttrString; s.from_str(al, f.m_module_file); @@ -249,7 +249,7 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) s.from_str(al, "pFunc"); ASR::asr_t *pFunc = ASR::make_Variable_t(al, f.base.base.loc, f.m_symtab, s.c_str(al), nullptr, 0, ASRUtils::intent_local, nullptr, nullptr, ASR::storage_typeType::Default, ptr_t, nullptr, - ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, false); + ASR::abiType::Source, ASR::Public, ASR::presenceType::Required, false); ASR::expr_t *pFunc_ref = ASRUtils::EXPR(ASR::make_Var_t(al, f.base.base.loc, ASR::down_cast(pFunc))); f.m_symtab->add_symbol(std::string("pFunc"), ASR::down_cast(pFunc)); body.push_back(al, ASRUtils::STMT( @@ -267,7 +267,7 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) s.from_str(al, "pArgs"); ASR::asr_t *pArgs = ASR::make_Variable_t(al, f.base.base.loc, f.m_symtab, s.c_str(al), nullptr, 0, ASRUtils::intent_local, nullptr, nullptr, ASR::storage_typeType::Default, ptr_t, nullptr, - ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, false); + ASR::abiType::Source, ASR::Public, ASR::presenceType::Required, false); ASR::expr_t *pArgs_ref = ASRUtils::EXPR(ASR::make_Var_t(al, f.base.base.loc, ASR::down_cast(pArgs))); f.m_symtab->add_symbol(std::string("pArgs"), ASR::down_cast(pArgs)); body.push_back(al, ASRUtils::STMT( @@ -281,19 +281,19 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) Vec args_PyTuple_SetItem; args_PyTuple_SetItem.reserve(al, 3); args_PyTuple_SetItem.push_back(al, {f.base.base.loc, pArgs_ref}); - args_PyTuple_SetItem.push_back(al, {f.base.base.loc, ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, + args_PyTuple_SetItem.push_back(al, {f.base.base.loc, ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, f.base.base.loc, i, i4_type))}); args_PyTuple_SetItem.push_back(al, {f.base.base.loc, native_to_cpython(al, f.m_args[i], f, parent_scope)}); std::string p = "pA" + std::to_string(i); s.from_str(al, p); ASR::asr_t *pA = ASR::make_Variable_t(al, f.base.base.loc, f.m_symtab, s.c_str(al), nullptr, 0, ASRUtils::intent_local, nullptr, nullptr, ASR::storage_typeType::Default, i4_type, nullptr, - ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, false); + ASR::abiType::Source, ASR::Public, ASR::presenceType::Required, false); ASR::expr_t *pA_ref = ASRUtils::EXPR(ASR::make_Var_t(al, f.base.base.loc, ASR::down_cast(pA))); f.m_symtab->add_symbol(p, ASR::down_cast(pA)); body.push_back(al, ASRUtils::STMT(ASR::make_Assignment_t(al, f.base.base.loc, pA_ref, - ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f.base.base.loc, sym_PyTuple_SetItem, nullptr, + ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f.base.base.loc, sym_PyTuple_SetItem, nullptr, args_PyTuple_SetItem.p, args_PyTuple_SetItem.n, i4_type, nullptr, nullptr)), nullptr))); } @@ -306,12 +306,12 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) s.from_str(al, "pReturn"); ASR::asr_t *pReturn = ASR::make_Variable_t(al, f.base.base.loc, f.m_symtab, s.c_str(al), nullptr, 0, ASRUtils::intent_local, nullptr, nullptr, ASR::storage_typeType::Default, ptr_t, nullptr, - ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, false); + ASR::abiType::Source, ASR::Public, ASR::presenceType::Required, false); ASR::expr_t *pReturn_ref = ASRUtils::EXPR(ASR::make_Var_t(al, f.base.base.loc, ASR::down_cast(pReturn))); f.m_symtab->add_symbol(std::string("pReturn"), ASR::down_cast(pReturn)); body.push_back(al, ASRUtils::STMT( - ASR::make_Assignment_t(al, f.base.base.loc, pReturn_ref, + ASR::make_Assignment_t(al, f.base.base.loc, pReturn_ref, ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f.base.base.loc, sym_PyObject_CallObject, nullptr, args_PyObject_CallObject.p, args_PyObject_CallObject.n, ptr_t, nullptr, nullptr)), nullptr))); @@ -337,7 +337,7 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) ASR::symbol_t *sym_Py_DecRef = parent_scope.get_symbol("Py_DecRef"); Vec args_Py_DecRef; - args_Py_DecRef.reserve(al, 1); + args_Py_DecRef.reserve(al, 1); args_Py_DecRef.push_back(al, {f.base.base.loc, pName_ref}); body.push_back(al, ASRUtils::STMT(ASRUtils::make_SubroutineCall_t_util(al, f.base.base.loc, sym_Py_DecRef, nullptr, args_Py_DecRef.p, args_Py_DecRef.n, nullptr, nullptr, false, false))); @@ -357,12 +357,12 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope) // reassignment f.m_body = body.p; f.n_body = body.n; - ASRUtils::get_FunctionType(f)->m_abi = ASR::abiType::BindC; + ASRUtils::get_FunctionType(f)->m_abi = ASR::abiType::Source; ASRUtils::get_FunctionType(f)->m_deftype = ASR::deftypeType::Implementation; } void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOptions &pass_options) { - if (pass_options.c_backend) { + if (pass_options.c_skip_bindpy_pass) { // FIXME: C backend supports arrays, it is used in bindpy_02 to bindpy_04 tests. // This pass currently does not handle any aggregate types. // Once we include support for aggregate types, we should remove this check, and @@ -371,13 +371,12 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt // backend as it would be handled by this ASR pass. return; } - bool bindpython_used = false; + for (auto &item : unit.m_symtab->get_scope()) { if (ASR::is_a(*item.second)) { ASR::Function_t *f = ASR::down_cast(item.second); if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) { if (f->n_body == 0 && f->m_module_file) { - bindpython_used = true; generate_body(al, *f, *unit.m_symtab); } } @@ -389,7 +388,6 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt ASR::Function_t *f = ASR::down_cast(module_item.second); if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) { if (f->n_body == 0 && f->m_module_file) { - bindpython_used = true; generate_body(al, *f, *module->m_symtab); } } @@ -398,9 +396,8 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt } } - if (bindpython_used) { - PassUtils::UpdateDependenciesVisitor u(al); - u.visit_TranslationUnit(unit); - } + + PassUtils::UpdateDependenciesVisitor u(al); + u.visit_TranslationUnit(unit); } } diff --git a/src/libasr/utils.h b/src/libasr/utils.h index f035b1365d..018c0afefe 100644 --- a/src/libasr/utils.h +++ b/src/libasr/utils.h @@ -59,7 +59,7 @@ struct PassOptions { bool tree = false; bool with_intrinsic_mods = false; bool c_mangling = false; - bool c_backend = false; + bool c_skip_bindpy_pass = false; }; struct CompilerOptions {