Skip to content

Commit dd6d546

Browse files
authored
Merge pull request #2719 from Vipul-Cariappa/small-int
Support to print `i8`, `u8`, `i16` and `u16` in Interactive mode
2 parents 56d5723 + 3792f27 commit dd6d546

File tree

6 files changed

+415
-10
lines changed

6 files changed

+415
-10
lines changed

src/bin/lpython.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -888,50 +888,74 @@ int interactive_python_repl(
888888
}
889889

890890
switch (r.type) {
891+
case (LCompilers::PythonCompiler::EvalResult::integer1) : {
892+
if (verbose) std::cout << "Return type: i8" << std::endl;
893+
if (verbose) section("Result:");
894+
std::cout << r.i32 << std::endl;
895+
break;
896+
}
897+
case (LCompilers::PythonCompiler::EvalResult::integer2) : {
898+
if (verbose) std::cout << "Return type: i16" << std::endl;
899+
if (verbose) section("Result:");
900+
std::cout << r.i64 << std::endl;
901+
break;
902+
}
891903
case (LCompilers::PythonCompiler::EvalResult::integer4) : {
892-
if (verbose) std::cout << "Return type: integer" << std::endl;
904+
if (verbose) std::cout << "Return type: i32" << std::endl;
893905
if (verbose) section("Result:");
894906
std::cout << r.i32 << std::endl;
895907
break;
896908
}
897909
case (LCompilers::PythonCompiler::EvalResult::integer8) : {
898-
if (verbose) std::cout << "Return type: integer(8)" << std::endl;
910+
if (verbose) std::cout << "Return type: i64" << std::endl;
899911
if (verbose) section("Result:");
900912
std::cout << r.i64 << std::endl;
901913
break;
902914
}
915+
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger1) : {
916+
if (verbose) std::cout << "Return type: u8" << std::endl;
917+
if (verbose) section("Result:");
918+
std::cout << r.u32 << std::endl;
919+
break;
920+
}
921+
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger2) : {
922+
if (verbose) std::cout << "Return type: u16" << std::endl;
923+
if (verbose) section("Result:");
924+
std::cout << r.u64 << std::endl;
925+
break;
926+
}
903927
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger4) : {
904-
if (verbose) std::cout << "Return type: unsigned integer" << std::endl;
928+
if (verbose) std::cout << "Return type: u32" << std::endl;
905929
if (verbose) section("Result:");
906930
std::cout << r.u32 << std::endl;
907931
break;
908932
}
909933
case (LCompilers::PythonCompiler::EvalResult::unsignedInteger8) : {
910-
if (verbose) std::cout << "Return type: unsigned integer(8)" << std::endl;
934+
if (verbose) std::cout << "Return type: u64" << std::endl;
911935
if (verbose) section("Result:");
912936
std::cout << r.u64 << std::endl;
913937
break;
914938
}
915939
case (LCompilers::PythonCompiler::EvalResult::real4) : {
916-
if (verbose) std::cout << "Return type: real" << std::endl;
940+
if (verbose) std::cout << "Return type: f32" << std::endl;
917941
if (verbose) section("Result:");
918942
std::cout << std::setprecision(8) << r.f32 << std::endl;
919943
break;
920944
}
921945
case (LCompilers::PythonCompiler::EvalResult::real8) : {
922-
if (verbose) std::cout << "Return type: real(8)" << std::endl;
946+
if (verbose) std::cout << "Return type: f64" << std::endl;
923947
if (verbose) section("Result:");
924948
std::cout << std::setprecision(17) << r.f64 << std::endl;
925949
break;
926950
}
927951
case (LCompilers::PythonCompiler::EvalResult::complex4) : {
928-
if (verbose) std::cout << "Return type: complex" << std::endl;
952+
if (verbose) std::cout << "Return type: c32" << std::endl;
929953
if (verbose) section("Result:");
930954
std::cout << std::setprecision(8) << "(" << r.c32.re << ", " << r.c32.im << ")" << std::endl;
931955
break;
932956
}
933957
case (LCompilers::PythonCompiler::EvalResult::complex8) : {
934-
if (verbose) std::cout << "Return type: complex(8)" << std::endl;
958+
if (verbose) std::cout << "Return type: c64" << std::endl;
935959
if (verbose) section("Result:");
936960
std::cout << std::setprecision(17) << "(" << r.c64.re << ", " << r.c64.im << ")" << std::endl;
937961
break;

src/libasr/codegen/evaluator.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ std::string LLVMModule::get_return_type(const std::string &fn_name)
102102
return "real4";
103103
} else if (type->isDoubleTy()) {
104104
return "real8";
105+
} else if (type->isIntegerTy(8)) {
106+
return "integer1";
107+
} else if (type->isIntegerTy(16)) {
108+
return "integer2";
105109
} else if (type->isIntegerTy(32)) {
106110
return "integer4";
107111
} else if (type->isIntegerTy(64)) {
@@ -269,6 +273,18 @@ intptr_t LLVMEvaluator::get_symbol_address(const std::string &name) {
269273
return (intptr_t)cantFail(std::move(addr0));
270274
}
271275

276+
int8_t LLVMEvaluator::int8fn(const std::string &name) {
277+
intptr_t addr = get_symbol_address(name);
278+
int8_t (*f)() = (int8_t (*)())addr;
279+
return f();
280+
}
281+
282+
int16_t LLVMEvaluator::int16fn(const std::string &name) {
283+
intptr_t addr = get_symbol_address(name);
284+
int16_t (*f)() = (int16_t (*)())addr;
285+
return f();
286+
}
287+
272288
int32_t LLVMEvaluator::int32fn(const std::string &name) {
273289
intptr_t addr = get_symbol_address(name);
274290
int32_t (*f)() = (int32_t (*)())addr;

src/libasr/codegen/evaluator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class LLVMEvaluator
5252
void add_module(std::unique_ptr<llvm::Module> mod);
5353
void add_module(std::unique_ptr<LLVMModule> m);
5454
intptr_t get_symbol_address(const std::string &name);
55+
int8_t int8fn(const std::string &name);
56+
int16_t int16fn(const std::string &name);
5557
int32_t int32fn(const std::string &name);
5658
int64_t int64fn(const std::string &name);
5759
bool boolfn(const std::string &name);

src/lpython/python_evaluator.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,33 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
127127

128128
e->add_module(std::move(m));
129129
if (call_run_fn) {
130-
if (return_type == "integer4") {
130+
if (return_type == "integer1") {
131+
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
132+
->m_symtab->get_symbol(run_fn);
133+
LCOMPILERS_ASSERT(fn)
134+
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
135+
uint8_t r = e->int8fn(run_fn);
136+
result.type = EvalResult::unsignedInteger1;
137+
result.u32 = r;
138+
} else {
139+
int8_t r = e->int8fn(run_fn);
140+
result.type = EvalResult::integer1;
141+
result.i32 = r;
142+
}
143+
} else if (return_type == "integer2") {
144+
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
145+
->m_symtab->get_symbol(run_fn);
146+
LCOMPILERS_ASSERT(fn)
147+
if (ASRUtils::get_FunctionType(fn)->m_return_var_type->type == ASR::ttypeType::UnsignedInteger) {
148+
uint16_t r = e->int16fn(run_fn);
149+
result.type = EvalResult::unsignedInteger2;
150+
result.u32 = r;
151+
} else {
152+
int16_t r = e->int16fn(run_fn);
153+
result.type = EvalResult::integer2;
154+
result.i32 = r;
155+
}
156+
} else if (return_type == "integer4") {
131157
ASR::symbol_t *fn = ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol(module_name))
132158
->m_symtab->get_symbol(run_fn);
133159
LCOMPILERS_ASSERT(fn)

src/lpython/python_evaluator.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@ class PythonCompiler
3737

3838
struct EvalResult {
3939
enum {
40-
integer4, integer8, unsignedInteger4, unsignedInteger8, real4, real8, complex4, complex8, statement, none
40+
integer1,
41+
integer2,
42+
unsignedInteger1,
43+
unsignedInteger2,
44+
integer4,
45+
integer8,
46+
unsignedInteger4,
47+
unsignedInteger8,
48+
real4,
49+
real8,
50+
complex4,
51+
complex8,
52+
statement,
53+
none
4154
} type;
4255
union {
4356
int32_t i32;

0 commit comments

Comments
 (0)