Skip to content

Support to print i8, u8, i16 and u16 in Interactive mode #2719

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 3, 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
40 changes: 32 additions & 8 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,50 +888,74 @@ int interactive_python_repl(
}

switch (r.type) {
case (LCompilers::PythonCompiler::EvalResult::integer1) : {
if (verbose) std::cout << "Return type: i8" << std::endl;
if (verbose) section("Result:");
std::cout << r.i32 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::integer2) : {
if (verbose) std::cout << "Return type: i16" << std::endl;
if (verbose) section("Result:");
std::cout << r.i64 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::integer4) : {
if (verbose) std::cout << "Return type: integer" << std::endl;
if (verbose) std::cout << "Return type: i32" << std::endl;
if (verbose) section("Result:");
std::cout << r.i32 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::integer8) : {
if (verbose) std::cout << "Return type: integer(8)" << std::endl;
if (verbose) std::cout << "Return type: i64" << std::endl;
if (verbose) section("Result:");
std::cout << r.i64 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger1) : {
if (verbose) std::cout << "Return type: u8" << std::endl;
if (verbose) section("Result:");
std::cout << r.u32 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger2) : {
if (verbose) std::cout << "Return type: u16" << std::endl;
if (verbose) section("Result:");
std::cout << r.u64 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger4) : {
if (verbose) std::cout << "Return type: unsigned integer" << std::endl;
if (verbose) std::cout << "Return type: u32" << std::endl;
if (verbose) section("Result:");
std::cout << r.u32 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger8) : {
if (verbose) std::cout << "Return type: unsigned integer(8)" << std::endl;
if (verbose) std::cout << "Return type: u64" << std::endl;
if (verbose) section("Result:");
std::cout << r.u64 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::real4) : {
if (verbose) std::cout << "Return type: real" << std::endl;
if (verbose) std::cout << "Return type: f32" << std::endl;
if (verbose) section("Result:");
std::cout << std::setprecision(8) << r.f32 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::real8) : {
if (verbose) std::cout << "Return type: real(8)" << std::endl;
if (verbose) std::cout << "Return type: f64" << std::endl;
if (verbose) section("Result:");
std::cout << std::setprecision(17) << r.f64 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::complex4) : {
if (verbose) std::cout << "Return type: complex" << std::endl;
if (verbose) std::cout << "Return type: c32" << std::endl;
if (verbose) section("Result:");
std::cout << std::setprecision(8) << "(" << r.c32.re << ", " << r.c32.im << ")" << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::complex8) : {
if (verbose) std::cout << "Return type: complex(8)" << std::endl;
if (verbose) std::cout << "Return type: c64" << std::endl;
if (verbose) section("Result:");
std::cout << std::setprecision(17) << "(" << r.c64.re << ", " << r.c64.im << ")" << std::endl;
break;
Expand Down
16 changes: 16 additions & 0 deletions src/libasr/codegen/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ std::string LLVMModule::get_return_type(const std::string &fn_name)
return "real4";
} else if (type->isDoubleTy()) {
return "real8";
} else if (type->isIntegerTy(8)) {
return "integer1";
} else if (type->isIntegerTy(16)) {
return "integer2";
} else if (type->isIntegerTy(32)) {
return "integer4";
} else if (type->isIntegerTy(64)) {
Expand Down Expand Up @@ -269,6 +273,18 @@ intptr_t LLVMEvaluator::get_symbol_address(const std::string &name) {
return (intptr_t)cantFail(std::move(addr0));
}

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;
Expand Down
2 changes: 2 additions & 0 deletions src/libasr/codegen/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ 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);
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);
Expand Down
28 changes: 27 additions & 1 deletion src/lpython/python_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,33 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(

e->add_module(std::move(m));
if (call_run_fn) {
if (return_type == "integer4") {
if (return_type == "integer1") {
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
->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);
result.type = EvalResult::unsignedInteger1;
result.u32 = r;
} else {
int8_t r = e->int8fn(run_fn);
result.type = EvalResult::integer1;
result.i32 = r;
}
} else if (return_type == "integer2") {
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
->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);
result.type = EvalResult::unsignedInteger2;
result.u32 = r;
} else {
int16_t r = e->int16fn(run_fn);
result.type = EvalResult::integer2;
result.i32 = r;
}
} else if (return_type == "integer4") {
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
->m_symtab->get_symbol(run_fn);
LCOMPILERS_ASSERT(fn)
Expand Down
15 changes: 14 additions & 1 deletion src/lpython/python_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@ class PythonCompiler

struct EvalResult {
enum {
integer4, integer8, unsignedInteger4, unsignedInteger8, real4, real8, complex4, complex8, statement, none
integer1,
integer2,
unsignedInteger1,
unsignedInteger2,
integer4,
integer8,
unsignedInteger4,
unsignedInteger8,
real4,
real8,
complex4,
complex8,
statement,
none
} type;
union {
int32_t i32;
Expand Down
Loading
Loading