Skip to content
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

Add unsigned integer support to C backend #1769

Merged
merged 1 commit into from
May 10, 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 @@ -311,7 +311,7 @@ RUN(NAME const_03 LABELS cpython llvm c
EXTRAFILES const_03b.c)
RUN(NAME const_04 LABELS cpython llvm c)
RUN(NAME expr_01 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_01u LABELS cpython llvm)
RUN(NAME expr_01u LABELS cpython llvm c)
RUN(NAME expr_02 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_03 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_04 LABELS cpython llvm c wasm)
Expand Down
38 changes: 38 additions & 0 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,44 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size);
sub = format_type_c(dims, type_name, v_m_name, use_ref, dummy);
}
} else if (ASRUtils::is_unsigned_integer(*v_m_type)) {
headers.insert("inttypes.h");
ASR::UnsignedInteger_t *t = ASR::down_cast<ASR::UnsignedInteger_t>(v_m_type);
std::string type_name = "uint" + std::to_string(t->m_kind * 8) + "_t";
if( is_array ) {
bool is_fixed_size = true;
dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size, true);
bool is_struct_type_member = ASR::is_a<ASR::StructType_t>(
*ASR::down_cast<ASR::symbol_t>(v.m_parent_symtab->asr_owner));
if( is_fixed_size && is_struct_type_member ) {
if( !force_declare ) {
force_declare_name = std::string(v.m_name);
}
sub = type_name + " " + force_declare_name + dims;
} else {
std::string encoded_type_name = "u" + std::to_string(t->m_kind * 8);
if( !force_declare ) {
force_declare_name = std::string(v.m_name);
}
generate_array_decl(sub, force_declare_name, type_name, dims,
encoded_type_name, t->m_dims, t->n_dims,
use_ref, dummy,
(v.m_intent != ASRUtils::intent_in &&
v.m_intent != ASRUtils::intent_inout &&
v.m_intent != ASRUtils::intent_out &&
!is_struct_type_member) || force_declare,
is_fixed_size, false, v.m_abi);
}
} else {
bool is_fixed_size = true;
std::string v_m_name = v.m_name;
if( declare_as_constant ) {
type_name = "const " + type_name;
v_m_name = const_name;
}
dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size);
sub = format_type_c(dims, type_name, v_m_name, use_ref, dummy);
}
} else if (ASRUtils::is_real(*v_m_type)) {
ASR::Real_t *t = ASR::down_cast<ASR::Real_t>(v_m_type);
std::string type_name = "float";
Expand Down
22 changes: 22 additions & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@ R"(#include <stdio.h>
case (4) : sub = "int32_t "; break;
case (8) : sub = "int64_t "; break;
}
} else if (ASRUtils::is_unsigned_integer(*return_var->m_type)) {
int kind = ASR::down_cast<ASR::UnsignedInteger_t>(return_var->m_type)->m_kind;
switch (kind) {
case (1) : sub = "uint8_t "; break;
case (2) : sub = "uint16_t "; break;
case (4) : sub = "uint32_t "; break;
case (8) : sub = "uint64_t "; break;
}
} else if (ASRUtils::is_real(*return_var->m_type)) {
bool is_float = ASR::down_cast<ASR::Real_t>(return_var->m_type)->m_kind == 4;
if (is_float) {
Expand Down Expand Up @@ -1382,6 +1390,12 @@ R"(#include <stdio.h>
// src = src;
break;
}
case (ASR::cast_kindType::IntegerToUnsignedInteger) : {
int dest_kind = ASRUtils::extract_kind_from_ttype_t(x.m_type);
src = "(uint" + std::to_string(dest_kind * 8) + "_t)(" + src + ")";
last_expr_precedence = 2;
break;
}
case (ASR::cast_kindType::ComplexToComplex) : {
break;
}
Expand Down Expand Up @@ -1522,6 +1536,10 @@ R"(#include <stdio.h>
handle_Compare(x);
}

void visit_UnsignedIntegerCompare(const ASR::UnsignedIntegerCompare_t &x) {
handle_Compare(x);
}

void visit_RealCompare(const ASR::RealCompare_t &x) {
handle_Compare(x);
}
Expand Down Expand Up @@ -1672,6 +1690,10 @@ R"(#include <stdio.h>
handle_BinOp(x);
}

void visit_UnsignedIntegerBinOp(const ASR::UnsignedIntegerBinOp_t &x) {
handle_BinOp(x);
}

void visit_RealBinOp(const ASR::RealBinOp_t &x) {
handle_BinOp(x);
}
Expand Down
Loading