Skip to content

Create "char buffer[...]" for "i8[...]" typed arrays #1346

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 7 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 32 additions & 0 deletions integration_tests/structs_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from ltypes import i8, dataclass, i32, ccallable
from numpy import empty, int8
from copy import deepcopy

@dataclass
class buffer_struct:
buffer: i8[32]

@ccallable
@dataclass
class buffer_struct_clink:
buffer: i8[32]

def f():
i: i32
buffer_var: i8[32] = empty(32, dtype=int8)
buffer_: buffer_struct = buffer_struct(deepcopy(buffer_var))
buffer_clink_: buffer_struct_clink = buffer_struct_clink(deepcopy(buffer_var))
print(buffer_.buffer[15])
print(buffer_clink_.buffer[15])

for i in range(32):
buffer_.buffer[i] = i8(i + 1)
buffer_clink_.buffer[i] = i8(i + 2)

for i in range(32):
print(i, buffer_.buffer[i], buffer_clink_.buffer[i])
assert buffer_.buffer[i] == i8(i + 1)
assert buffer_clink_.buffer[i] == i8(i + 2)
assert buffer_clink_.buffer[i] - buffer_.buffer[i] == i8(1)

f()
44 changes: 44 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ static inline ASR::ttype_t* symbol_type(const ASR::symbol_t *f)
return nullptr;
}

static inline ASR::abiType symbol_abi(const ASR::symbol_t *f)
{
switch( f->type ) {
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_abi;
}
case ASR::symbolType::EnumType: {
return ASR::down_cast<ASR::EnumType_t>(f)->m_abi;
}
default: {
throw LCompilersException("Cannot return ABI of, " +
std::to_string(f->type) + " symbol.");
}
}
return ASR::abiType::Source;
}

static inline ASR::ttype_t* get_contained_type(ASR::ttype_t* asr_type) {
switch( asr_type->type ) {
case ASR::ttypeType::List: {
Expand Down Expand Up @@ -138,6 +155,20 @@ static inline ASR::ttype_t* get_contained_type(ASR::ttype_t* asr_type) {
}
}

static inline ASR::abiType expr_abi(ASR::expr_t* e) {
switch( e->type ) {
case ASR::exprType::Var: {
return ASRUtils::symbol_abi(ASR::down_cast<ASR::Var_t>(e)->m_v);
}
case ASR::exprType::StructInstanceMember: {
return ASRUtils::symbol_abi(ASR::down_cast<ASR::StructInstanceMember_t>(e)->m_m);
}
default:
throw LCompilersException("Cannot extract the ABI of " +
std::to_string(e->type) + " expression.");
}
}

static inline std::string type_to_str(const ASR::ttype_t *t)
{
switch (t->type) {
Expand Down Expand Up @@ -1369,6 +1400,19 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
return n_dims;
}

static inline bool is_fixed_size_array(ASR::dimension_t* m_dims, size_t n_dims) {
if( n_dims == 0 ) {
return false;
}
for( size_t i = 0; i < n_dims; i++ ) {
int64_t dim_size = -1;
if( !ASRUtils::extract_value(ASRUtils::expr_value(m_dims[i].m_length), dim_size) ) {
return false;
}
}
return true;
}

inline int extract_n_dims_from_ttype(ASR::ttype_t *x) {
ASR::dimension_t* m_dims_temp = nullptr;
return extract_dimensions_from_ttype(x, m_dims_temp);
Expand Down
Loading