Skip to content

C: Implement allocatable #1829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/array_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 38 additions & 5 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1811,23 +1811,56 @@ R"(#include <stdio.h>

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<x.n_args; i++) {
ASR::symbol_t* tmp_sym = nullptr;
ASR::ttype_t* type = nullptr;
ASR::expr_t* tmp_expr = x.m_args[i].m_a;
if( ASR::is_a<ASR::Var_t>(*tmp_expr) ) {
const ASR::Var_t* tmp_var = ASR::down_cast<ASR::Var_t>(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; j<x.m_args[i].n_dims; j++) {
std::string st, l;
if (x.m_args[i].m_dims[j].m_start) {
self().visit_expr(*x.m_args[i].m_dims[j].m_start);
st = src;
} else {
st = "0";
}
if (x.m_args[i].m_dims[j].m_length) {
self().visit_expr(*x.m_args[i].m_dims[j].m_length);
l = src;
} else {
l = "1";
}
size_str += "*" + l;
out += indent + sym + "->dims[" + 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;
}

Expand Down