Skip to content

Commit

Permalink
refactored declare_functions to asr_utils.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Aug 11, 2024
1 parent 8278e43 commit 644a453
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 138 deletions.
91 changes: 91 additions & 0 deletions src/libasr/asr_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,97 @@ void append_error(diag::Diagnostics& diag, const std::string& msg,
//Initialize pointer to zero so that it can be initialized in first call to get_instance
ASRUtils::LabelGenerator* ASRUtils::LabelGenerator::label_generator = nullptr;

ASR::expr_t *type_enum_to_asr_expr(Allocator &al, enum TTYPE_T t, const Location &l, std::string n,
SymbolTable *current_scope, ASR::intentType intent) {
ASR::ttype_t *type = nullptr;

Str s;
s.from_str(al, n);

switch (t) {
case VOID:
return nullptr;
case I1:
type = ASRUtils::TYPE(ASR::make_Logical_t(al, l, 4));
break;
case I8:
type = ASRUtils::TYPE(ASR::make_Integer_t(al, l, 1));
break;
case I32:
type = ASRUtils::TYPE(ASR::make_Integer_t(al, l, 4));
break;
case I64:
type = ASRUtils::TYPE(ASR::make_Integer_t(al, l, 8));
break;
case U8:
type = ASRUtils::TYPE(ASR::make_UnsignedInteger_t(al, l, 1));
break;
case U32:
type = ASRUtils::TYPE(ASR::make_UnsignedInteger_t(al, l, 4));
break;
case U64:
type = ASRUtils::TYPE(ASR::make_UnsignedInteger_t(al, l, 8));
break;
case F32:
type = ASRUtils::TYPE(ASR::make_Real_t(al, l, 4));
break;
case F64:
type = ASRUtils::TYPE(ASR::make_Real_t(al, l, 8));
break;
case STR:
type = ASRUtils::TYPE(ASR::make_Character_t(al, l, 1, -2, nullptr));
break;
case PTR:
type = ASRUtils::TYPE(ASR::make_CPtr_t(al, l));
break;
case PTR_TO_PTR:
type = ASRUtils::TYPE(ASR::make_Pointer_t(al, l, ASRUtils::TYPE(ASR::make_CPtr_t(al, l))));
break;
}
LCOMPILERS_ASSERT(type);
ASR::symbol_t *v = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(al, l, current_scope, s.c_str(al), nullptr,
0, intent, nullptr, nullptr, ASR::storage_typeType::Default,
type, nullptr, ASR::abiType::BindC, ASR::Public,
ASR::presenceType::Required, true));
current_scope->add_symbol(n, v);
return ASRUtils::EXPR(ASR::make_Var_t(al, l, v));
}

void declare_function(Allocator &al, ASRFunc fn, const Location &l, SymbolTable *parent_scope,
std::string header_name) {
Str s;
char *c_header = nullptr;
if (header_name != "") {
s.from_str(al, header_name);
c_header = s.c_str(al);
}
s.from_str(al, fn.m_name);
Vec<ASR::expr_t*> args;
args.reserve(al, fn.args.size());
SymbolTable *current_scope = al.make_new<SymbolTable>(parent_scope);
int c = 0;
for (auto j: fn.args) {
args.push_back(al, type_enum_to_asr_expr(al, j, l, fn.m_name + std::to_string(++c), current_scope,
ASRUtils::intent_in));
}
ASR::expr_t *retval = type_enum_to_asr_expr(al, fn.retvar, l, "_lpython_return_variable", current_scope,
ASRUtils::intent_return_var);
char *fn_name = s.c_str(al);
ASR::asr_t *f = ASRUtils::make_Function_t_util(al, l, current_scope, fn_name, nullptr, 0, args.p, args.n,
nullptr, 0, retval, ASR::abiType::BindC, ASR::accessType::Public,
ASR::deftypeType::Interface, nullptr, false, false, false, false, false, nullptr, 0,
false, false, false, c_header);

parent_scope->add_symbol(fn.m_name, ASR::down_cast<ASR::symbol_t>(f));
}

void declare_functions(Allocator &al, std::vector<ASRFunc> fns, const Location &l, SymbolTable *parent_scope,
std::string header_name) {
for (auto i: fns) {
declare_function(al, i, l, parent_scope, header_name);
}
}

} // namespace ASRUtils


Expand Down
35 changes: 35 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5415,6 +5415,41 @@ static inline bool is_argument_of_type_CPtr(ASR::expr_t *var) {
return is_argument;
}

enum TTYPE_T {
VOID,
I1,
I8,
STR,
I32,
I64,
U8,
U32,
U64,
F32,
F64,
PTR,
PTR_TO_PTR,
};

typedef struct {
std::string m_name;
std::vector<enum TTYPE_T> args;
enum TTYPE_T retvar;
} ASRFunc;

// Create a variable with name `n` and type `t` and add the symbol into `currect_scope`
// Returns the variable
ASR::expr_t *type_enum_to_asr_expr(Allocator &al, enum TTYPE_T t, const Location &l, std::string n,
SymbolTable *current_scope, ASR::intentType intent);

// created a BindC Interface function decleration in the `parent_scope`
void declare_function(Allocator &al, ASRFunc fn, const Location &l, SymbolTable *parent_scope,
std::string header_name="");

// created a BindC Interface functions decleration in the `parent_scope`
void declare_functions(Allocator &al, std::vector<ASRFunc> fns, const Location &l, SymbolTable *parent_scope,
std::string header_name="");

} // namespace ASRUtils

} // namespace LCompilers
Expand Down
168 changes: 30 additions & 138 deletions src/libasr/pass/python_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,118 +376,6 @@ void generate_body(Allocator &al, ASR::Function_t &f, SymbolTable &parent_scope)
ASRUtils::get_FunctionType(f)->m_deftype = ASR::deftypeType::Implementation;
}

enum T {
VOID,
I1,
I8,
STR,
I32,
I64,
U8,
U32,
U64,
F32,
F64,
PTR,
PTR_TO_PTR,
};

struct F {
std::string m_name;
std::vector<enum T> args;
enum T retvar;
};

ASR::expr_t *type_enum_to_asr_expr(Allocator &al, enum T t, Location &l, std::string n, SymbolTable *current_scope, ASR::intentType intent) {
ASR::ttype_t *type = nullptr;

Str s;
s.from_str(al, n);

switch (t) {
case VOID:
return nullptr;
case I1:
type = ASRUtils::TYPE(ASR::make_Logical_t(al, l, 4));
break;
case I8:
type = ASRUtils::TYPE(ASR::make_Integer_t(al, l, 1));
break;
case I32:
type = ASRUtils::TYPE(ASR::make_Integer_t(al, l, 4));
break;
case I64:
type = ASRUtils::TYPE(ASR::make_Integer_t(al, l, 8));
break;
case U8:
type = ASRUtils::TYPE(ASR::make_UnsignedInteger_t(al, l, 1));
break;
case U32:
type = ASRUtils::TYPE(ASR::make_UnsignedInteger_t(al, l, 4));
break;
case U64:
type = ASRUtils::TYPE(ASR::make_UnsignedInteger_t(al, l, 8));
break;
case F32:
type = ASRUtils::TYPE(ASR::make_Real_t(al, l, 4));
break;
case F64:
type = ASRUtils::TYPE(ASR::make_Real_t(al, l, 8));
break;
case STR:
type = ASRUtils::TYPE(ASR::make_Character_t(al, l, 1, -2, nullptr));
break;
case PTR:
type = ASRUtils::TYPE(ASR::make_CPtr_t(al, l));
break;
case PTR_TO_PTR:
type = ASRUtils::TYPE(ASR::make_Pointer_t(al, l, ASRUtils::TYPE(ASR::make_CPtr_t(al, l))));
break;
}
LCOMPILERS_ASSERT(type);
ASR::symbol_t *v = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(al, l, current_scope, s.c_str(al), nullptr,
0, intent, nullptr, nullptr, ASR::storage_typeType::Default,
type, nullptr, ASR::abiType::BindC, ASR::Public,
ASR::presenceType::Required, true));
current_scope->add_symbol(n, v);
return ASRUtils::EXPR(ASR::make_Var_t(al, l, v));
}

void declare_functions(Allocator &al, std::vector<struct F> fns, SymbolTable *parent_scope,
std::string header_name="") {
Location *l = al.make_new<Location>();
l->first = 0;
l->last = 0;

Str s;
char *c_header = nullptr;
if (header_name != "") {
s.from_str(al, header_name);
c_header = s.c_str(al);
}

for (auto i: fns) {
Vec<ASR::expr_t*> args;
args.reserve(al, i.args.size());
s.from_str(al, i.m_name);
SymbolTable *current_scope = al.make_new<SymbolTable>(parent_scope);
int c = 0;
for (auto j: i.args) {
args.push_back(al, type_enum_to_asr_expr(al, j, *l, i.m_name + std::to_string(++c), current_scope,
ASRUtils::intent_in));
}
ASR::expr_t *retval = type_enum_to_asr_expr(al, i.retvar, *l, "_lpython_return_variable", current_scope,
ASRUtils::intent_return_var);
char *fn_name = s.c_str(al);
ASR::asr_t *f = ASRUtils::make_Function_t_util(al, *l, current_scope, fn_name, nullptr, 0, args.p, args.n,
nullptr, 0, retval, ASR::abiType::BindC, ASR::accessType::Public,
ASR::deftypeType::Interface, nullptr, false, false, false, false, false, nullptr, 0,
false, false, false, c_header);

parent_scope->add_symbol(i.m_name, ASR::down_cast<ASR::symbol_t>(f));
}
}

void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOptions &pass_options) {
if (pass_options.c_skip_bindpy_pass) {
// FIXME: C backend supports arrays, it is used in bindpy_02 to bindpy_04 tests.
Expand All @@ -499,40 +387,44 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
return;
}

std::vector<struct F> fns;
fns.push_back({"Py_Initialize", {}, VOID});
fns.push_back({"Py_IsInitialized", {}, I32});
// fns.push_back({"PyRun_SimpleString", {STR}, I32});
fns.push_back({"Py_DecodeLocale", {STR, PTR}, PTR});
fns.push_back({"PySys_SetArgv", {I32, PTR_TO_PTR}, VOID});
fns.push_back({"Py_FinalizeEx", {}, I32});
fns.push_back({"PyUnicode_FromString", {STR}, PTR});
fns.push_back({"PyUnicode_AsUTF8AndSize", {PTR, PTR}, STR});
fns.push_back({"PyImport_Import", {PTR}, PTR});
fns.push_back({"Py_DecRef", {PTR}, VOID});
fns.push_back({"Py_IncRef", {PTR}, VOID});
fns.push_back({"PyObject_GetAttrString", {PTR, STR}, PTR});
fns.push_back({"PyTuple_New", {I32}, PTR});
fns.push_back({"PyTuple_SetItem", {PTR, I32, PTR}, I32});
fns.push_back({"PyObject_CallObject", {PTR, PTR}, PTR});
fns.push_back({"PyLong_AsLongLong", {PTR}, I64});
fns.push_back({"PyLong_AsUnsignedLongLong", {PTR}, U64});
fns.push_back({"PyLong_FromLongLong", {I64}, PTR});
fns.push_back({"PyLong_FromUnsignedLongLong", {U64}, PTR});
fns.push_back({"PyFloat_FromDouble", {F64}, PTR});
fns.push_back({"PyFloat_AsDouble", {PTR}, F64});
fns.push_back({"PyBool_FromLong", {I32}, PTR});
fns.push_back({"PyObject_IsTrue", {PTR}, I32});
std::vector<ASRUtils::ASRFunc> fns;
fns.push_back({"Py_Initialize", {}, ASRUtils::VOID});
fns.push_back({"Py_IsInitialized", {}, ASRUtils::I32});
// fns.push_back({"PyRun_SimpleString", {STR}, ASRUtils::I32});
fns.push_back({"Py_DecodeLocale", {ASRUtils::STR, ASRUtils::PTR}, ASRUtils::PTR});
fns.push_back({"PySys_SetArgv", {ASRUtils::I32, ASRUtils::PTR_TO_PTR}, ASRUtils::VOID});
fns.push_back({"Py_FinalizeEx", {}, ASRUtils::I32});
fns.push_back({"PyUnicode_FromString", {ASRUtils::STR}, ASRUtils::PTR});
fns.push_back({"PyUnicode_AsUTF8AndSize", {ASRUtils::PTR, ASRUtils::PTR}, ASRUtils::STR});
fns.push_back({"PyImport_Import", {ASRUtils::PTR}, ASRUtils::PTR});
fns.push_back({"Py_DecRef", {ASRUtils::PTR}, ASRUtils::VOID});
fns.push_back({"Py_IncRef", {ASRUtils::PTR}, ASRUtils::VOID});
fns.push_back({"PyObject_GetAttrString", {ASRUtils::PTR, ASRUtils::STR}, ASRUtils::PTR});
fns.push_back({"PyTuple_New", {ASRUtils::I32}, ASRUtils::PTR});
fns.push_back({"PyTuple_SetItem", {ASRUtils::PTR, ASRUtils::I32, ASRUtils::PTR}, ASRUtils::I32});
fns.push_back({"PyObject_CallObject", {ASRUtils::PTR, ASRUtils::PTR}, ASRUtils::PTR});
fns.push_back({"PyLong_AsLongLong", {ASRUtils::PTR}, ASRUtils::I64});
fns.push_back({"PyLong_AsUnsignedLongLong", {ASRUtils::PTR}, ASRUtils::U64});
fns.push_back({"PyLong_FromLongLong", {ASRUtils::I64}, ASRUtils::PTR});
fns.push_back({"PyLong_FromUnsignedLongLong", {ASRUtils::U64}, ASRUtils::PTR});
fns.push_back({"PyFloat_FromDouble", {ASRUtils::F64}, ASRUtils::PTR});
fns.push_back({"PyFloat_AsDouble", {ASRUtils::PTR}, ASRUtils::F64});
fns.push_back({"PyBool_FromLong", {ASRUtils::I32}, ASRUtils::PTR});
fns.push_back({"PyObject_IsTrue", {ASRUtils::PTR}, ASRUtils::I32});

bool included_cpython_funcs = false;

Location *l = al.make_new<Location>();
l->first = 0;
l->last = 0;

for (auto &item : unit.m_symtab->get_scope()) {
if (ASR::is_a<ASR::Function_t>(*item.second)) {
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(item.second);
if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) {
if (f->n_body == 0 && f->m_module_file) {
if (!included_cpython_funcs) {
declare_functions(al, fns, unit.m_symtab, "Python.h");
ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");
included_cpython_funcs = true;
}
generate_body(al, *f, *unit.m_symtab);
Expand All @@ -547,7 +439,7 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt
if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) {
if (f->n_body == 0 && f->m_module_file) {
if (!included_cpython_funcs) {
declare_functions(al, fns, unit.m_symtab, "Python.h");
ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h");
included_cpython_funcs = true;
}
generate_body(al, *f, *module->m_symtab);
Expand Down

0 comments on commit 644a453

Please sign in to comment.