From c8da5aa0d9800325b63fad95929e558584fcd8eb Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 18 May 2023 10:15:44 +0530 Subject: [PATCH 1/2] C: Implement allocatable --- src/libasr/codegen/asr_to_c_cpp.h | 43 +++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index ce7ff2ca13..57a3f7fbdd 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -1811,23 +1811,56 @@ R"(#include void visit_Allocate(const ASR::Allocate_t &x) { std::string indent(indentation_level*indentation_spaces, ' '); - std::string out = indent + "// FIXME: allocate("; + std::string out = ""; for (size_t i=0; i(*tmp_expr) ) { const ASR::Var_t* tmp_var = ASR::down_cast(tmp_expr); tmp_sym = tmp_var->m_v; + type = ASRUtils::expr_type(tmp_expr); } else { throw CodeGenError("Cannot deallocate variables in expression " + std::to_string(tmp_expr->type), tmp_expr->base.loc); } - //ASR::dimension_t* dims = x.m_args[i].m_dims; - //size_t n_dims = x.m_args[i].n_dims; - out += std::string(ASRUtils::symbol_name(tmp_sym)) + ", "; + std::string sym = ASRUtils::symbol_name(tmp_sym); + if (ASRUtils::is_array(type)) { + std::string size_str = "1"; + out += indent + sym + "->n_dims = " + std::to_string(x.m_args[i].n_dims) + ";\n"; + for (size_t j=0; jdims[" + std::to_string(j) + "].lower_bound = "; + out += st + ";\n"; + out += indent + sym + "->dims[" + std::to_string(j) + "].length = "; + out += l + ";\n"; + } + std::string ty = CUtils::get_c_type_from_ttype_t(type); + size_str += "*sizeof(" + ty + ")"; + out += indent + sym + "->data = (" + ty + "*) _lfortran_malloc(" + size_str + ")"; + out += ";\n"; + out += indent + sym + "->is_allocated = true;\n"; + } else { + std::string ty = CUtils::get_c_type_from_ttype_t(type), size_str; + size_str = "sizeof(" + ty + ")"; + out += indent + sym + " = (" + ty + "*) _lfortran_malloc(" + size_str + ")"; + out += ";\n"; + } } - out += ");\n"; src = out; } From 842bb88ca1b890359925f8bd66e1fdba94bbce12 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 18 May 2023 10:15:59 +0530 Subject: [PATCH 2/2] Enable tests --- integration_tests/CMakeLists.txt | 2 +- integration_tests/array_03.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 0ce4739c9e..5ec5176406 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -284,7 +284,7 @@ RUN(NAME array_size_01 LABELS cpython llvm c) RUN(NAME array_size_02 LABELS cpython llvm c) RUN(NAME array_01 LABELS cpython llvm wasm c) RUN(NAME array_02 LABELS cpython wasm c) -RUN(NAME array_03 LABELS cpython llvm) +RUN(NAME array_03 LABELS cpython llvm c) RUN(NAME bindc_01 LABELS cpython llvm c) RUN(NAME bindc_02 LABELS cpython llvm c) RUN(NAME bindc_04 LABELS llvm c) diff --git a/integration_tests/array_03.py b/integration_tests/array_03.py index 51f7c2f8f9..975fe4aea6 100644 --- a/integration_tests/array_03.py +++ b/integration_tests/array_03.py @@ -7,12 +7,13 @@ def f(): i: i32 for i in range(n): a[i] = f64(i+1) + for i in range(n): + assert abs(a[i] - f64(i + 1)) < 1e-12 b: Allocatable[i32[:]] n = 10 b = empty((n,), dtype=int32) for i in range(n): b[i] = i+1 - print(a) for i in range(n): assert b[i] == i+1