Skip to content

combining duplicated function into a single templated function #2727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ int compile_python_using_llvm(

e.add_module(std::move(m));
if (call_stmts) {
e.voidfn("__module___main_____main__global_stmts");
e.execfn<void>("__module___main_____main__global_stmts");
}

if (compiler_options.enable_cpython) {
Expand Down
66 changes: 0 additions & 66 deletions src/libasr/codegen/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,72 +275,6 @@ intptr_t LLVMEvaluator::get_symbol_address(const std::string &name) {
return (intptr_t)cantFail(std::move(addr0));
}

char *LLVMEvaluator::strfn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
char *(*f)() = (char *(*)())addr;
return f();
}

int8_t LLVMEvaluator::int8fn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
int8_t (*f)() = (int8_t (*)())addr;
return f();
}

int16_t LLVMEvaluator::int16fn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
int16_t (*f)() = (int16_t (*)())addr;
return f();
}

int32_t LLVMEvaluator::int32fn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
int32_t (*f)() = (int32_t (*)())addr;
return f();
}

int64_t LLVMEvaluator::int64fn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
int64_t (*f)() = (int64_t (*)())addr;
return f();
}

bool LLVMEvaluator::boolfn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
bool (*f)() = (bool (*)())addr;
return f();
}

float LLVMEvaluator::floatfn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
float (*f)() = (float (*)())addr;
return f();
}

double LLVMEvaluator::doublefn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
double (*f)() = (double (*)())addr;
return f();
}

std::complex<float> LLVMEvaluator::complex4fn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
std::complex<float> (*f)() = (std::complex<float> (*)())addr;
return f();
}

std::complex<double> LLVMEvaluator::complex8fn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
std::complex<double> (*f)() = (std::complex<double> (*)())addr;
return f();
}

void LLVMEvaluator::voidfn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
void (*f)() = (void (*)())addr;
f();
}

void write_file(const std::string &filename, const std::string &contents)
{
std::ofstream out;
Expand Down
18 changes: 7 additions & 11 deletions src/libasr/codegen/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ class LLVMEvaluator
void add_module(std::unique_ptr<llvm::Module> mod);
void add_module(std::unique_ptr<LLVMModule> m);
intptr_t get_symbol_address(const std::string &name);
char *strfn(const std::string &name);
int8_t int8fn(const std::string &name);
int16_t int16fn(const std::string &name);
int32_t int32fn(const std::string &name);
int64_t int64fn(const std::string &name);
bool boolfn(const std::string &name);
float floatfn(const std::string &name);
double doublefn(const std::string &name);
std::complex<float> complex4fn(const std::string &name);
std::complex<double> complex8fn(const std::string &name);
void voidfn(const std::string &name);
std::string get_asm(llvm::Module &m);
void save_asm_file(llvm::Module &m, const std::string &filename);
void save_object_file(llvm::Module &m, const std::string &filename);
Expand All @@ -73,6 +62,13 @@ class LLVMEvaluator
llvm::LLVMContext &get_context();
static void print_targets();
static std::string get_default_target_triple();

template<class T>
T execfn(const std::string &name) {
intptr_t addr = get_symbol_address(name);
T (*f)() = (T (*)())addr;
return f();
}
};


Expand Down
28 changes: 14 additions & 14 deletions src/lpython/python_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::Character) {
char *r = e->strfn(run_fn);
char *r = e->execfn<char*>(run_fn);
result.type = EvalResult::string;
result.str = r;
} else {
Expand All @@ -143,11 +143,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
uint8_t r = e->int8fn(run_fn);
uint8_t r = e->execfn<uint8_t>(run_fn);
result.type = EvalResult::unsignedInteger1;
result.u32 = r;
} else {
int8_t r = e->int8fn(run_fn);
int8_t r = e->execfn<int8_t>(run_fn);
result.type = EvalResult::integer1;
result.i32 = r;
}
Expand All @@ -156,11 +156,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
uint16_t r = e->int16fn(run_fn);
uint16_t r = e->execfn<uint16_t>(run_fn);
result.type = EvalResult::unsignedInteger2;
result.u32 = r;
} else {
int16_t r = e->int16fn(run_fn);
int16_t r = e->execfn<int16_t>(run_fn);
result.type = EvalResult::integer2;
result.i32 = r;
}
Expand All @@ -169,11 +169,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
uint32_t r = e->int32fn(run_fn);
uint32_t r = e->execfn<uint32_t>(run_fn);
result.type = EvalResult::unsignedInteger4;
result.u32 = r;
} else {
int32_t r = e->int32fn(run_fn);
int32_t r = e->execfn<int32_t>(run_fn);
result.type = EvalResult::integer4;
result.i32 = r;
}
Expand All @@ -182,34 +182,34 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
uint64_t r = e->int64fn(run_fn);
uint64_t r = e->execfn<uint64_t>(run_fn);
result.type = EvalResult::unsignedInteger8;
result.u64 = r;
} else {
int64_t r = e->int64fn(run_fn);
int64_t r = e->execfn<int64_t>(run_fn);
result.type = EvalResult::integer8;
result.i64 = r;
}
} else if (return_type == "real4") {
float r = e->floatfn(run_fn);
float r = e->execfn<float>(run_fn);
result.type = EvalResult::real4;
result.f32 = r;
} else if (return_type == "real8") {
double r = e->doublefn(run_fn);
double r = e->execfn<double>(run_fn);
result.type = EvalResult::real8;
result.f64 = r;
} else if (return_type == "complex4") {
std::complex<float> r = e->complex4fn(run_fn);
std::complex<float> r = e->execfn<std::complex<float>>(run_fn);
result.type = EvalResult::complex4;
result.c32.re = r.real();
result.c32.im = r.imag();
} else if (return_type == "complex8") {
std::complex<double> r = e->complex8fn(run_fn);
std::complex<double> r = e->execfn<std::complex<double>>(run_fn);
result.type = EvalResult::complex8;
result.c64.re = r.real();
result.c64.im = r.imag();
} else if (return_type == "void") {
e->voidfn(run_fn);
e->execfn<void>(run_fn);
result.type = EvalResult::statement;
} else if (return_type == "none") {
result.type = EvalResult::none;
Expand Down
Loading
Loading