Skip to content

Commit

Permalink
Merge pull request lcompilers#2719 from Vipul-Cariappa/small-int
Browse files Browse the repository at this point in the history
Support to print `i8`, `u8`, `i16` and `u16` in Interactive mode
  • Loading branch information
Shaikh-Ubaid committed Jun 3, 2024
2 parents 56d5723 + 3792f27 commit dd6d546
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 10 deletions.
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

0 comments on commit dd6d546

Please sign in to comment.