Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed May 10, 2024
1 parent 824d34e commit a36d692
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 102 deletions.
70 changes: 40 additions & 30 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,11 @@ void print_time_report(std::vector<std::pair<std::string, double>> &times, bool

#ifdef HAVE_LFORTRAN_LLVM

void section(const std::string &s)
{
std::cout << color(LCompilers::style::bold) << color(LCompilers::fg::blue) << s << color(LCompilers::style::reset) << color(LCompilers::fg::reset) << std::endl;
}

int emit_llvm(const std::string &infile,
const std::string &runtime_library_dir,
LCompilers::PassManager& pass_manager,
Expand Down Expand Up @@ -794,9 +799,9 @@ int emit_llvm(const std::string &infile,
}

int interactive_python_repl(
// LCompilers::PassManager& pass_manager,
LCompilers::PassManager& pass_manager,
CompilerOptions &compiler_options,
bool /*time_report*/)
bool verbose)
{
Allocator al(4*1024);
compiler_options.interactive = true;
Expand All @@ -805,8 +810,6 @@ int interactive_python_repl(
LCompilers::LocationManager lm;
std::vector<std::pair<std::string, double>> times;
LCompilers::PythonCompiler::EvalResult r;
LCompilers::PassManager pass_manager;
pass_manager.use_default_passes();

std::string code_string;
std::cout << ">>> ";
Expand All @@ -831,8 +834,6 @@ int interactive_python_repl(
}
code_string += input + "\n";

// std::cout << "code block: \n" << code_string;

{
cell_count++;
LCompilers::LocationManager::FileLocations fl;
Expand All @@ -847,7 +848,7 @@ int interactive_python_repl(
try {
auto evaluation_start_time = std::chrono::high_resolution_clock::now();
LCompilers::Result<LCompilers::PythonCompiler::EvalResult>
res = fe.evaluate(code_string, false, lm, pass_manager, diagnostics);
res = fe.evaluate(code_string, verbose, lm, pass_manager, diagnostics);
if (res.ok) {
r = res.result;
} else {
Expand Down Expand Up @@ -876,57 +877,66 @@ int interactive_python_repl(
continue;
}

if (verbose) {
section("AST:");
std::cout << r.ast << std::endl;
section("ASR:");
std::cout << r.asr << std::endl;
section("LLVM IR:");
std::cout << r.llvm_ir << std::endl;
}

switch (r.type) {
case (LCompilers::PythonCompiler::EvalResult::integer4) : {
// if (verbose) std::cout << "Return type: integer" << std::endl;
// if (verbose) section("Result:");
if (verbose) std::cout << "Return type: integer" << 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) section("Result:");
if (verbose) std::cout << "Return type: integer(8)" << std::endl;
if (verbose) section("Result:");
std::cout << r.i64 << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::real4) : {
// if (verbose) std::cout << "Return type: real" << std::endl;
// if (verbose) section("Result:");
if (verbose) std::cout << "Return type: real" << 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) section("Result:");
if (verbose) std::cout << "Return type: real(8)" << 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) section("Result:");
if (verbose) std::cout << "Return type: complex" << 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) section("Result:");
if (verbose) std::cout << "Return type: complex(8)" << std::endl;
if (verbose) section("Result:");
std::cout << std::setprecision(17) << "(" << r.c64.re << ", " << r.c64.im << ")" << std::endl;
break;
}
case (LCompilers::PythonCompiler::EvalResult::statement) : {
// if (verbose) {
// std::cout << "Return type: none" << std::endl;
// section("Result:");
// std::cout << "(statement)" << std::endl;
// }
if (verbose) {
std::cout << "Return type: none" << std::endl;
section("Result:");
std::cout << "(statement)" << std::endl;
}
break;
}
case (LCompilers::PythonCompiler::EvalResult::none) : {
// if (verbose) {
// std::cout << "Return type: none" << std::endl;
// section("Result:");
// std::cout << "(nothing to execute)" << std::endl;
// }
if (verbose) {
std::cout << "Return type: none" << std::endl;
section("Result:");
std::cout << "(nothing to execute)" << std::endl;
}
break;
}
default : throw LCompilers::LCompilersException("Return type not supported");
Expand Down Expand Up @@ -1983,7 +1993,7 @@ int main(int argc, char *argv[])
compiler_options.po.disable_main = true;
compiler_options.emit_debug_line_column = false;
compiler_options.generate_object_code = false;
return interactive_python_repl(compiler_options, time_report);
return interactive_python_repl(lpython_pass_manager, compiler_options, arg_v);
}

// TODO: for now we ignore the other filenames, only handle
Expand Down
75 changes: 10 additions & 65 deletions src/lpython/python_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PythonCompiler::~PythonCompiler() = default;

Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
#ifdef HAVE_LFORTRAN_LLVM
const std::string &code_orig, bool /*verbose*/, LocationManager &lm,
const std::string &code_orig, bool verbose, LocationManager &lm,
LCompilers::PassManager& pass_manager, diag::Diagnostics &diagnostics
#else
const std::string &/*code_orig*/, bool /*verbose*/,
Expand All @@ -65,11 +65,9 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
return res.error;
}

// if (verbose) {
// result.ast = LCompilers::LPython::pickle_python(*ast, true, true);
// }

// std::cout << result.ast << std::endl;
if (verbose) {
result.ast = LCompilers::LPython::pickle_python(*ast, true, true);
}

// AST -> ASR
Result<ASR::TranslationUnit_t*> res2 = get_asr3(*ast, diagnostics, lm);
Expand All @@ -81,11 +79,9 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
return res2.error;
}

// if (verbose) {
// result.asr = pickle(*asr, true, true, true);
// }

// std::cout << result.asr << std::endl;
if (verbose) {
result.asr = pickle(*asr, true, true, true);
}

// ASR -> LLVM
Result<std::unique_ptr<LLVMModule>> res3 = get_llvm3(*asr,
Expand All @@ -98,11 +94,9 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
return res3.error;
}

// if (verbose) {
// result.llvm_ir = m->str();
// }

// std::cout << m->str() << std::endl;
if (verbose) {
result.llvm_ir = m->str();
}

bool call_init = false;
bool call_stmts = false;
Expand All @@ -123,7 +117,6 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
e->voidfn(stmts_fn);
}

// TODO: remove init_fn and stmts_fn from asr
if (call_init) {
ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol("__main__"))->m_symtab
->erase_symbol("__main____lpython_interactive_init_" + std::to_string(eval_count) + "__");
Expand All @@ -134,45 +127,6 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
}

eval_count++;

// std::string return_type = m->get_return_type(run_fn);

// // LLVM -> Machine code -> Execution
// e->add_module(std::move(m));
// if (return_type == "integer4") {
// int32_t r = e->int32fn(run_fn);
// result.type = EvalResult::integer4;
// result.i32 = r;
// } else if (return_type == "integer8") {
// int64_t r = e->int64fn(run_fn);
// result.type = EvalResult::integer8;
// result.i64 = r;
// } else if (return_type == "real4") {
// float r = e->floatfn(run_fn);
// result.type = EvalResult::real4;
// result.f32 = r;
// } else if (return_type == "real8") {
// double r = e->doublefn(run_fn);
// result.type = EvalResult::real8;
// result.f64 = r;
// } else if (return_type == "complex4") {
// std::complex<float> r = e->complex4fn(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);
// result.type = EvalResult::complex8;
// result.c64.re = r.real();
// result.c64.im = r.imag();
// } else if (return_type == "void") {
// e->voidfn(run_fn);
// result.type = EvalResult::statement;
// } else if (return_type == "none") {
// result.type = EvalResult::none;
// } else {
// throw LCompilersException("PythonCompiler::evaluate(): Return type not supported");
// }
return result;
#else
throw LCompilersException("LLVM is not enabled");
Expand All @@ -187,8 +141,6 @@ Result<LCompilers::LPython::AST::ast_t*> PythonCompiler::get_ast2(
std::string tmp;
Result<LCompilers::LPython::AST::ast_t*>
res = LCompilers::LPython::parse_to_ast(al, *code, 0, diagnostics);
// Result<LCompilers::LPython::AST::Module_t*>
// res = LCompilers::LPython::parse(al, *code, 0, diagnostics);
if (res.ok) {
return (LCompilers::LPython::AST::ast_t*)res.result;
} else {
Expand All @@ -203,11 +155,7 @@ Result<ASR::TranslationUnit_t*> PythonCompiler::get_asr3(
{
ASR::TranslationUnit_t* asr;
// AST -> ASR
// Remove the old execution function if it exists
if (symbol_table) {
if (symbol_table->get_symbol(run_fn) != nullptr) {
symbol_table->erase_symbol(run_fn);
}
symbol_table->mark_all_variables_external(al);
}
auto res = LCompilers::LPython::python_ast_to_asr(al, lm, symbol_table, ast, diagnostics,
Expand All @@ -234,9 +182,6 @@ Result<std::unique_ptr<LLVMModule>> PythonCompiler::get_llvm3(
)
{
#ifdef HAVE_LFORTRAN_LLVM
// eval_count++;
// run_fn = "__lfortran_evaluate_" + std::to_string(eval_count);

if (compiler_options.emit_debug_info) {
if (!compiler_options.emit_debug_line_column) {
diagnostics.add(LCompilers::diag::Diagnostic(
Expand Down
10 changes: 3 additions & 7 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4175,16 +4175,11 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
} else {
ASR::Module_t* module_sym =
ASR::down_cast<ASR::Module_t>(parent_scope->resolve_symbol(module_name));
current_scope = module_sym->m_symtab;
LCOMPILERS_ASSERT(module_sym != nullptr);
current_scope = module_sym->m_symtab;
for (size_t i=0; i<x.n_body; i++) {
visit_stmt(*x.m_body[i]);
}
module_sym->m_dependencies = current_module_dependencies.p;
module_sym->n_dependencies = current_module_dependencies.size();
if (!overload_defs.empty()) {
create_GenericProcedure(x.base.base.loc);
}
}

global_scope = nullptr;
Expand Down Expand Up @@ -5040,7 +5035,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}

void visit_Interactive(const AST::Interactive_t &x) {
static size_t interactive_execution_count = 0;
static size_t interactive_execution_count = 0; // ???: should this be a class member variable
// interactive_execution_count should be the same as eval_count in PythonCompiler

ASR::TranslationUnit_t *unit = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
current_scope = unit->m_symtab;
Expand Down

0 comments on commit a36d692

Please sign in to comment.