From b8fe33e09257706280c8bafb71bafb9de9dfe944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 13:55:02 -0700 Subject: [PATCH 1/9] Add compile time evaluation struct --- src/lpython/semantics/python_comptime_eval.h | 108 +++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/lpython/semantics/python_comptime_eval.h diff --git a/src/lpython/semantics/python_comptime_eval.h b/src/lpython/semantics/python_comptime_eval.h new file mode 100644 index 0000000000..8576ddd3cc --- /dev/null +++ b/src/lpython/semantics/python_comptime_eval.h @@ -0,0 +1,108 @@ +#ifndef LPYTHON_SEMANTICS_COMPTIME_EVAL_H +#define LPYTHON_SEMANTICS_COMPTIME_EVAL_H + +#include + +#include +#include +#include +#include +#include +#include + +namespace LFortran { + +struct PythonIntrinsicProcedures { + + const std::string m_builtin = "lpython_builtin"; + + typedef ASR::expr_t* (*comptime_eval_callback)(Allocator &, const Location &, Vec &); + // Table of intrinsics + // The callback is only called if all arguments have compile time `value` + // which is always one of the `Constant*` expression ASR nodes, so inside + // the callback one can assume that. + std::map> comptime_eval_map; + + PythonIntrinsicProcedures() { + comptime_eval_map = { + {"abs", {m_builtin, &eval_abs}}, + }; + } + + // Return `true` if `name` is in the table of intrinsics + bool is_intrinsic(std::string name) const { + auto search = comptime_eval_map.find(name); + if (search != comptime_eval_map.end()) { + return true; + } else { + return false; + } + } + + // Looks up `name` in the table of intrinsics and returns the corresponding + // module name; Otherwise rises an exception + std::string get_module(std::string name, const Location &loc) const { + auto search = comptime_eval_map.find(name); + if (search != comptime_eval_map.end()) { + std::string module_name = std::get<0>(search->second); + return module_name; + } else { + throw SemanticError("Function '" + name + + "' not found among intrinsic procedures", + loc); + } + } + + // Evaluates the intrinsic function `name` at compile time + ASR::expr_t *comptime_eval(std::string name, Allocator &al, const Location &loc, Vec &args) const { + auto search = comptime_eval_map.find(name); + if (search != comptime_eval_map.end()) { + comptime_eval_callback cb = std::get<1>(search->second); + Vec arg_values = ASRUtils::get_arg_values(al, args); + if (arg_values.size() != args.size()) { + throw SemanticError("Intrinsic function '" + name + + "' compile time evaluation expects different number of arguments", + loc); + } + return cb(al, loc, arg_values); + } else { + throw SemanticError("Intrinsic function '" + name + + "' compile time evaluation is not implemented yet", + loc); + } + } + + + static ASR::expr_t *eval_abs(Allocator &al, const Location &loc, + Vec &args + ) { + LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args)); + if (args.size() != 1) { + throw SemanticError("Intrinsic abs function accepts exactly 1 argument", loc); + } + ASR::expr_t* trig_arg = args[0]; + ASR::ttype_t* t = ASRUtils::expr_type(args[0]); + if (ASR::is_a(*t)) { + double rv = ASR::down_cast(trig_arg)->m_r; + double val = std::abs(rv); + return ASR::down_cast(ASR::make_ConstantReal_t(al, loc, val, t)); + } else if (ASR::is_a(*t)) { + int64_t rv = ASR::down_cast(trig_arg)->m_n; + int64_t val = std::abs(rv); + return ASR::down_cast(ASR::make_ConstantInteger_t(al, loc, val, t)); + } else if (ASR::is_a(*t)) { + double re = ASR::down_cast(trig_arg)->m_re; + double im = ASR::down_cast(trig_arg)->m_im; + std::complex x(re, im); + double result = std::abs(x); + return ASR::down_cast(ASR::make_ConstantReal_t(al, loc, result, t)); + } else { + throw SemanticError("Argument of the abs function must be Integer, Real or Complex", loc); + } + } + +}; // ComptimeEval + +} // namespace LFortran + +#endif /* LPYTHON_SEMANTICS_COMPTIME_EVAL_H */ From ef576126151c564beb0e96c58e7b210b3fdd7ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:15:53 -0700 Subject: [PATCH 2/9] Use new intrinsic design in ast to asr --- src/lpython/semantics/python_ast_to_asr.cpp | 78 +++++++++++++++++++-- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 55f2c3bd32..991d2dd523 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace LFortran::Python { @@ -152,6 +153,7 @@ class CommonVisitor : public AST::BaseVisitor { // True for the main module, false for every other one // The main module is stored directly in TranslationUnit, other modules are Modules bool main_module; + PythonIntrinsicProcedures intrinsic_procedures; CommonVisitor(Allocator &al, SymbolTable *symbol_table, diag::Diagnostics &diagnostics, bool main_module) @@ -178,6 +180,69 @@ class CommonVisitor : public AST::BaseVisitor { return ASR::make_Var_t(al, loc, v); } + ASR::symbol_t* resolve_intrinsic_function(const Location &loc, const std::string &remote_sym) { + LFORTRAN_ASSERT(intrinsic_procedures.is_intrinsic(remote_sym)) + std::string module_name = intrinsic_procedures.get_module(remote_sym, loc); + + SymbolTable *tu_symtab = ASRUtils::get_tu_symtab(current_scope); + std::string rl_path = get_runtime_library_dir(); + ASR::Module_t *m = ASRUtils::load_module(al, tu_symtab, module_name, + loc, true, rl_path, + [&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); } + ); + + ASR::symbol_t *t = m->m_symtab->resolve_symbol(remote_sym); + if (!t) { + throw SemanticError("The symbol '" + remote_sym + + "' not found in the module '" + module_name + "'", + loc); + } else if (! (ASR::is_a(*t) + || ASR::is_a(*t) + || ASR::is_a(*t))) { + throw SemanticError("The symbol '" + remote_sym + + "' found in the module '" + module_name + "', " + + "but it is not a function, subroutine or a generic procedure.", + loc); + } + char *fn_name = ASRUtils::symbol_name(t); + ASR::asr_t *fn = ASR::make_ExternalSymbol_t( + al, t->base.loc, + /* a_symtab */ current_scope, + /* a_name */ fn_name, + t, + m->m_name, nullptr, 0, fn_name, + ASR::accessType::Private + ); + std::string sym = fn_name; + + current_scope->scope[sym] = ASR::down_cast(fn); + ASR::symbol_t *v = ASR::down_cast(fn); + + // Now we need to add the module `m` with the intrinsic function + // into the current module dependencies + if (current_module) { + // We are in body visitor, the module is already constructed + // and available as current_module. + // Add the module `m` to current module dependencies + Vec vec; + vec.from_pointer_n_copy(al, current_module->m_dependencies, + current_module->n_dependencies); + if (!present(vec, m->m_name)) { + vec.push_back(al, m->m_name); + current_module->m_dependencies = vec.p; + current_module->n_dependencies = vec.size(); + } + } else { + // We are in the symtab visitor or body visitor and we are + // constructing a module, so current_module is not available yet + // (the current_module_dependencies is not used in body visitor) + if (!present(current_module_dependencies, m->m_name)) { + current_module_dependencies.push_back(al, m->m_name); + } + } + return v; + } + // Convert Python AST type annotation to an ASR type // Examples: // i32, i64, f32, f64 @@ -1981,6 +2046,14 @@ class BodyVisitor : public CommonVisitor { if (!s) { + if (intrinsic_procedures.is_intrinsic(call_name)) { + s = resolve_intrinsic_function(x.base.base.loc, call_name); + } else { + throw SemanticError("The function '" + call_name + "' is not declared and not intrinsic", + x.base.base.loc); + } + if (false) { + // This will all be removed once we port it to intrinsic functions // Intrinsic functions if (call_name == "size") { // TODO: size should be part of ASR. That way @@ -2481,12 +2554,9 @@ class BodyVisitor : public CommonVisitor { throw SemanticError("Function '" + call_name + "' is not declared and not intrinsic", x.base.base.loc); } + } // end of "comment" } - if (!s) { - throw SemanticError("Function '" + call_name + "' is not declared", - x.base.base.loc); - } // handling ExternalSymbol ASR::symbol_t *stemp = s; s = ASRUtils::symbol_get_past_external(s); From 9dab83651cb47a811969df03f2f0bed503464f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:18:42 -0700 Subject: [PATCH 3/9] Add a comment --- src/lpython/semantics/python_ast_to_asr.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 991d2dd523..5381ecb75c 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -148,6 +148,8 @@ class CommonVisitor : public AST::BaseVisitor { ASR::asr_t *tmp; Allocator &al; SymbolTable *current_scope; + // The current_module contains the current module that is being visited; + // this is used to append to the module dependencies if needed ASR::Module_t *current_module = nullptr; Vec current_module_dependencies; // True for the main module, false for every other one From 59e129e88a848918a18a4911e43791e8ac979f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:28:54 -0700 Subject: [PATCH 4/9] Call the correct load_module --- src/lpython/semantics/python_ast_to_asr.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 5381ecb75c..6d32358482 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -188,10 +188,13 @@ class CommonVisitor : public AST::BaseVisitor { SymbolTable *tu_symtab = ASRUtils::get_tu_symtab(current_scope); std::string rl_path = get_runtime_library_dir(); - ASR::Module_t *m = ASRUtils::load_module(al, tu_symtab, module_name, + bool ltypes; + ASR::Module_t *m = load_module(al, tu_symtab, module_name, loc, true, rl_path, + ltypes, [&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); } ); + LFORTRAN_ASSERT(!ltypes) ASR::symbol_t *t = m->m_symtab->resolve_symbol(remote_sym); if (!t) { From ed7caec88f71fcb4687d55b159f1a3034158f373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:32:17 -0700 Subject: [PATCH 5/9] Add a test for compile time value --- integration_tests/test_builtin_abs.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integration_tests/test_builtin_abs.py b/integration_tests/test_builtin_abs.py index 43379ff1c6..3c1e7b3a3f 100644 --- a/integration_tests/test_builtin_abs.py +++ b/integration_tests/test_builtin_abs.py @@ -6,6 +6,8 @@ def test_abs(): assert abs(x) == 5.5 x = -5.5 assert abs(x) == 5.5 + assert abs(5.5) == 5.5 + assert abs(-5.5) == 5.5 test_abs() From 7480f14c5c5afed69465e046cfc9c0b40e755ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:51:15 -0700 Subject: [PATCH 6/9] Implement robust is_intrinsic_function() --- src/libasr/asr_utils.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 95293fe47e..6076ea77e8 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -368,6 +368,18 @@ static inline bool is_intrinsic_function(const ASR::Function_t *fn) { return false; } +// Returns true if the Function is intrinsic, otherwise false +// This version uses the `intrinsic` member of `Module`, so it +// should be used instead of is_intrinsic_function +static inline bool is_intrinsic_function2(const ASR::Function_t *fn) { + ASR::symbol_t *sym = (ASR::symbol_t*)fn; + ASR::Module_t *m = get_sym_module0(sym); + if (m != nullptr) { + if (m->m_intrinsic) return true; + } + return false; +} + // Returns true if all arguments have a `value` static inline bool all_args_have_value(const Vec &args) { for (auto &a : args) { From 6ebd3799992f1fe8122f87b860a386597d8ea9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:51:56 -0700 Subject: [PATCH 7/9] Evaluate intrinsic functions at compile time --- src/lpython/semantics/python_ast_to_asr.cpp | 6 +++++- src/lpython/semantics/python_comptime_eval.h | 5 ++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 6d32358482..538b22ddae 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2569,8 +2569,12 @@ class BodyVisitor : public CommonVisitor { if(ASR::is_a(*s)) { ASR::Function_t *func = ASR::down_cast(s); ASR::ttype_t *a_type = ASRUtils::expr_type(func->m_return_var); + ASR::expr_t *value = nullptr; + if (ASRUtils::is_intrinsic_function2(func)) { + value = intrinsic_procedures.comptime_eval(call_name, al, x.base.base.loc, args); + } tmp = ASR::make_FunctionCall_t(al, x.base.base.loc, stemp, - nullptr, args.p, args.size(), nullptr, 0, a_type, nullptr, nullptr); + nullptr, args.p, args.size(), nullptr, 0, a_type, value, nullptr); } else if(ASR::is_a(*s)) { tmp = ASR::make_SubroutineCall_t(al, x.base.base.loc, stemp, nullptr, args.p, args.size(), nullptr); diff --git a/src/lpython/semantics/python_comptime_eval.h b/src/lpython/semantics/python_comptime_eval.h index 8576ddd3cc..7a10f24981 100644 --- a/src/lpython/semantics/python_comptime_eval.h +++ b/src/lpython/semantics/python_comptime_eval.h @@ -60,9 +60,8 @@ struct PythonIntrinsicProcedures { comptime_eval_callback cb = std::get<1>(search->second); Vec arg_values = ASRUtils::get_arg_values(al, args); if (arg_values.size() != args.size()) { - throw SemanticError("Intrinsic function '" + name - + "' compile time evaluation expects different number of arguments", - loc); + // Not all arguments have compile time values; we do not call the callback + return nullptr; } return cb(al, loc, arg_values); } else { From 14fa90b6bb44a8cf47ca164c4fbb0b455ab33c2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:55:19 -0700 Subject: [PATCH 8/9] Temporarily enable the old functions --- src/lpython/semantics/python_ast_to_asr.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 538b22ddae..49ccd201a5 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2054,10 +2054,14 @@ class BodyVisitor : public CommonVisitor { if (intrinsic_procedures.is_intrinsic(call_name)) { s = resolve_intrinsic_function(x.base.base.loc, call_name); } else { + // TODO: We need to port all functions below to the intrinsic functions file + // Then we can uncomment this error message: + /* throw SemanticError("The function '" + call_name + "' is not declared and not intrinsic", x.base.base.loc); } if (false) { + */ // This will all be removed once we port it to intrinsic functions // Intrinsic functions if (call_name == "size") { From 83141ce7df14d063f5e59b6991d63450992b78d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Wed, 23 Feb 2022 14:55:33 -0700 Subject: [PATCH 9/9] Update tests --- tests/reference/asr-constants1-5828e8a.json | 2 +- tests/reference/asr-constants1-5828e8a.stdout | 2 +- tests/reference/asr-test_builtin_abs-c74d2c9.json | 4 ++-- tests/reference/asr-test_builtin_abs-c74d2c9.stdout | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/reference/asr-constants1-5828e8a.json b/tests/reference/asr-constants1-5828e8a.json index 1a4c4ea745..8eb9f87993 100644 --- a/tests/reference/asr-constants1-5828e8a.json +++ b/tests/reference/asr-constants1-5828e8a.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-constants1-5828e8a.stdout", - "stdout_hash": "82599c6a0114feef56f5e9198dd5cfbd06d87638efc14126af92a89e", + "stdout_hash": "0a0d5acbd9b14c908f856d9d95d36e49e4cb2f5db735109a008af200", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-constants1-5828e8a.stdout b/tests/reference/asr-constants1-5828e8a.stdout index d53f6e2e4a..f9450621d6 100644 --- a/tests/reference/asr-constants1-5828e8a.stdout +++ b/tests/reference/asr-constants1-5828e8a.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 17 {}) main_program [] []), test_abs: (Subroutine (SymbolTable 4 {abs: (ExternalSymbol 4 abs 13 abs lpython_builtin [] abs Public), b: (Variable 4 b Local () () Default (Real 4 []) Source Public Required .false.)}) test_abs [] [(= (Var 4 b) (FunctionCall 4 abs () [(ConstantReal 3.450000 (Real 8 []))] [] (Real 8 []) () ()) ()) (= (Var 4 b) (FunctionCall 4 abs () [(UnaryOp USub (ConstantReal 5346.340000 (Real 8 [])) (Real 8 []) (ConstantReal -5346.340000 (Real 8 [])))] [] (Real 8 []) () ()) ())] Source Public Implementation () .false. .false.), test_bool: (Subroutine (SymbolTable 6 {a: (Variable 6 a Local () () Default (Logical 1 []) Source Public Required .false.)}) test_bool [] [(= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_boz: (Subroutine (SymbolTable 2 {b: (Variable 2 b Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_boz [] [(= (Var 2 b) (ConstantString "0b101" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0b1000000" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0b1000010110" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o10" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o70" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0o1026" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0x2a" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0xc0ffee" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0x216" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_callable: (Subroutine (SymbolTable 8 {a: (Variable 8 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 8 b Local () () Default (Integer 4 []) Source Public Required .false.)}) test_callable [] [(= (Var 8 b) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 8 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_divmod: (Subroutine (SymbolTable 11 {a: (Variable 11 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_divmod [] [(= (Var 11 a) (ConstantTuple [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger -3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ())] Source Public Implementation () .false. .false.), test_float: (Subroutine (SymbolTable 10 {a: (Variable 10 a Local () () Default (Real 8 []) Source Public Required .false.)}) test_float [] [(= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 4.560000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal -1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5346.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 423.533997 (Real 8 [])) ())] Source Public Implementation () .false. .false.), test_int: (Subroutine (SymbolTable 9 {a: (Variable 9 a Local () () Default (Integer 8 []) Source Public Required .false.)}) test_int [] [(= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 4 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger -5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 1 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5346 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ())] Source Public Implementation () .false. .false.), test_len: (Subroutine (SymbolTable 5 {a: (Variable 5 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_len [] [(= (Var 5 a) (ConstantInteger 0 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 4 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 14 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ())] Source Public Implementation () .false. .false.), test_ord_chr: (Subroutine (SymbolTable 3 {a: (Variable 3 a Local () () Default (Integer 4 []) Source Public Required .false.), ord: (ExternalSymbol 3 ord 13 ord lpython_builtin [] ord Public), s: (Variable 3 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_ord_chr [] [(= (Var 3 a) (FunctionCall 3 ord () [(ConstantString "5" (Character 1 1 () []))] [] (Integer 4 []) () ()) ()) (= (Var 3 s) (ConstantString "+" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_str: (Subroutine (SymbolTable 7 {s: (Variable 7 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_str [] [(= (Var 7 s) (ConstantString "" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "-4" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5.600000" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "True" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "False" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5346" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 17 {}) main_program [] []), test_abs: (Subroutine (SymbolTable 4 {abs: (ExternalSymbol 4 abs 13 abs lpython_builtin [] abs Private), b: (Variable 4 b Local () () Default (Real 4 []) Source Public Required .false.)}) test_abs [] [(= (Var 4 b) (FunctionCall 4 abs () [(ConstantReal 3.450000 (Real 8 []))] [] (Real 8 []) (ConstantReal 3.450000 (Real 8 [])) ()) ()) (= (Var 4 b) (FunctionCall 4 abs () [(UnaryOp USub (ConstantReal 5346.340000 (Real 8 [])) (Real 8 []) (ConstantReal -5346.340000 (Real 8 [])))] [] (Real 8 []) (ConstantReal 5346.340000 (Real 8 [])) ()) ())] Source Public Implementation () .false. .false.), test_bool: (Subroutine (SymbolTable 6 {a: (Variable 6 a Local () () Default (Logical 1 []) Source Public Required .false.)}) test_bool [] [(= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_boz: (Subroutine (SymbolTable 2 {b: (Variable 2 b Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_boz [] [(= (Var 2 b) (ConstantString "0b101" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0b1000000" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0b1000010110" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o10" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o70" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0o1026" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0x2a" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0xc0ffee" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0x216" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_callable: (Subroutine (SymbolTable 8 {a: (Variable 8 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 8 b Local () () Default (Integer 4 []) Source Public Required .false.)}) test_callable [] [(= (Var 8 b) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 8 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_divmod: (Subroutine (SymbolTable 11 {a: (Variable 11 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_divmod [] [(= (Var 11 a) (ConstantTuple [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger -3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ())] Source Public Implementation () .false. .false.), test_float: (Subroutine (SymbolTable 10 {a: (Variable 10 a Local () () Default (Real 8 []) Source Public Required .false.)}) test_float [] [(= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 4.560000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal -1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5346.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 423.533997 (Real 8 [])) ())] Source Public Implementation () .false. .false.), test_int: (Subroutine (SymbolTable 9 {a: (Variable 9 a Local () () Default (Integer 8 []) Source Public Required .false.)}) test_int [] [(= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 4 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger -5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 1 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5346 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ())] Source Public Implementation () .false. .false.), test_len: (Subroutine (SymbolTable 5 {a: (Variable 5 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_len [] [(= (Var 5 a) (ConstantInteger 0 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 4 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 14 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ())] Source Public Implementation () .false. .false.), test_ord_chr: (Subroutine (SymbolTable 3 {a: (Variable 3 a Local () () Default (Integer 4 []) Source Public Required .false.), ord: (ExternalSymbol 3 ord 13 ord lpython_builtin [] ord Public), s: (Variable 3 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_ord_chr [] [(= (Var 3 a) (FunctionCall 3 ord () [(ConstantString "5" (Character 1 1 () []))] [] (Integer 4 []) () ()) ()) (= (Var 3 s) (ConstantString "+" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_str: (Subroutine (SymbolTable 7 {s: (Variable 7 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_str [] [(= (Var 7 s) (ConstantString "" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "-4" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5.600000" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "True" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "False" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5346" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.)}) []) diff --git a/tests/reference/asr-test_builtin_abs-c74d2c9.json b/tests/reference/asr-test_builtin_abs-c74d2c9.json index f8d50d9419..ce80265555 100644 --- a/tests/reference/asr-test_builtin_abs-c74d2c9.json +++ b/tests/reference/asr-test_builtin_abs-c74d2c9.json @@ -2,11 +2,11 @@ "basename": "asr-test_builtin_abs-c74d2c9", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/../integration_tests/test_builtin_abs.py", - "infile_hash": "6469a8f5b27cd216944f1dcde21c25cce6aa28435399b0b862cead11", + "infile_hash": "196e1cd7d641336a4bb317f947ec4e3369a0e62a4da29af1a9c20946", "outfile": null, "outfile_hash": null, "stdout": "asr-test_builtin_abs-c74d2c9.stdout", - "stdout_hash": "7bd2db29560606cab8ff10b4a418637743bc3e8083dead814e8f9c0a", + "stdout_hash": "db838618bb7ad2900d537a296ac699b1fcd846d088528fb22fba502a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-test_builtin_abs-c74d2c9.stdout b/tests/reference/asr-test_builtin_abs-c74d2c9.stdout index d6c082242c..57091561ff 100644 --- a/tests/reference/asr-test_builtin_abs-c74d2c9.stdout +++ b/tests/reference/asr-test_builtin_abs-c74d2c9.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {_lfortran_main_program: (Subroutine (SymbolTable 9 {}) _lfortran_main_program [] [(SubroutineCall 1 test_abs () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 8 {}) main_program [] [(SubroutineCall 1 _lfortran_main_program () [] ())]), test_abs: (Subroutine (SymbolTable 2 {abs: (ExternalSymbol 2 abs 4 abs lpython_builtin [] abs Public), x: (Variable 2 x Local () () Default (Real 8 []) Source Public Required .false.)}) test_abs [] [(= (Var 2 x) (ConstantReal 5.500000 (Real 8 [])) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ()) (= (Var 2 x) (UnaryOp USub (ConstantReal 5.500000 (Real 8 [])) (Real 8 []) (ConstantReal -5.500000 (Real 8 []))) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {_lfortran_main_program: (Subroutine (SymbolTable 9 {}) _lfortran_main_program [] [(SubroutineCall 1 test_abs () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 8 {}) main_program [] [(SubroutineCall 1 _lfortran_main_program () [] ())]), test_abs: (Subroutine (SymbolTable 2 {abs: (ExternalSymbol 2 abs 4 abs lpython_builtin [] abs Private), x: (Variable 2 x Local () () Default (Real 8 []) Source Public Required .false.)}) test_abs [] [(= (Var 2 x) (ConstantReal 5.500000 (Real 8 [])) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ()) (= (Var 2 x) (UnaryOp USub (ConstantReal 5.500000 (Real 8 [])) (Real 8 []) (ConstantReal -5.500000 (Real 8 []))) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ()) (Assert (Compare (FunctionCall 2 abs () [(ConstantReal 5.500000 (Real 8 []))] [] (Real 8 []) (ConstantReal 5.500000 (Real 8 [])) ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 abs () [(UnaryOp USub (ConstantReal 5.500000 (Real 8 [])) (Real 8 []) (ConstantReal -5.500000 (Real 8 [])))] [] (Real 8 []) (ConstantReal 5.500000 (Real 8 [])) ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ())] Source Public Implementation () .false. .false.)}) [])