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

WASM: Initial support for const type #2297

Merged
merged 2 commits into from
Aug 24, 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
4 changes: 2 additions & 2 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ RUN(NAME print_list_tuple_02 LABELS cpython llvm c NOFAST)
RUN(NAME print_list_tuple_03 LABELS cpython llvm c NOFAST)

# CPython and LLVM
RUN(NAME const_01 LABELS cpython llvm c)
RUN(NAME const_02 LABELS cpython llvm c)
RUN(NAME const_01 LABELS cpython llvm c wasm)
RUN(NAME const_02 LABELS cpython llvm c wasm)
RUN(NAME const_03 LABELS cpython llvm c
EXTRAFILES const_03b.c)
RUN(NAME const_04 LABELS cpython llvm c)
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/const_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def test_const_variables():
ycf: Const[f64] = 3.0
yf: f64 = 3.0

print(xci, xi)
print(yci, yi)
print(xcf, xf)
print(ycf, yf)

assert xci == xi
assert yci == yi
assert xcf == xf
Expand Down
34 changes: 20 additions & 14 deletions src/libasr/codegen/asr_to_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
}

using namespace wasm;
int kind = ASRUtils::extract_kind_from_ttype_t(v->m_type);
uint32_t global_var_idx = UINT_MAX;
ASR::ttype_t* v_m_type = ASRUtils::type_get_past_array(v->m_type);
ASR::ttype_t* ttype = ASRUtils::type_get_past_const(v->m_type);
ASR::ttype_t* v_m_type = ASRUtils::type_get_past_array(ttype);
int kind = ASRUtils::extract_kind_from_ttype_t(ttype);
switch (v_m_type->type){
case ASR::ttypeType::Integer: {
uint64_t init_val = 0;
Expand Down Expand Up @@ -877,9 +878,11 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
throw CodeGenAbort();
}
} else {
if (ASRUtils::is_integer(*v->m_type)) {
ASR::ttype_t* ttype = v->m_type;
ttype = ASRUtils::type_get_past_const(ttype);
if (ASRUtils::is_integer(*ttype)) {
ASR::Integer_t *v_int =
ASR::down_cast<ASR::Integer_t>(ASRUtils::type_get_past_array(v->m_type));
ASR::down_cast<ASR::Integer_t>(ASRUtils::type_get_past_array(ttype));
if (is_array) {
type_vec.push_back(i32);
} else {
Expand All @@ -892,9 +895,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
"Integers of kind 4 and 8 only supported");
}
}
} else if (ASRUtils::is_real(*v->m_type)) {
} else if (ASRUtils::is_real(*ttype)) {
ASR::Real_t *v_float = ASR::down_cast<ASR::Real_t>(
ASRUtils::type_get_past_array(v->m_type));
ASRUtils::type_get_past_array(ttype));

if (is_array) {
type_vec.push_back(i32);
Expand All @@ -908,10 +911,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
"Floating Points of kind 4 and 8 only supported");
}
}
} else if (ASRUtils::is_logical(*v->m_type)) {
} else if (ASRUtils::is_logical(*ttype)) {
ASR::Logical_t *v_logical =
ASR::down_cast<ASR::Logical_t>(
ASRUtils::type_get_past_array(v->m_type));
ASRUtils::type_get_past_array(ttype));

if (is_array) {
type_vec.push_back(i32);
Expand All @@ -923,10 +926,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
throw CodeGenError("Logicals of kind 4 only supported");
}
}
} else if (ASRUtils::is_character(*v->m_type)) {
} else if (ASRUtils::is_character(*ttype)) {
ASR::Character_t *v_int =
ASR::down_cast<ASR::Character_t>(
ASRUtils::type_get_past_array(v->m_type));
ASRUtils::type_get_past_array(ttype));

if (is_array) {
type_vec.push_back(i32);
Expand All @@ -941,10 +944,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
"Characters of kind 1 only supported");
}
}
} else if (ASRUtils::is_complex(*v->m_type)) {
} else if (ASRUtils::is_complex(*ttype)) {
ASR::Complex_t *v_comp =
ASR::down_cast<ASR::Complex_t>(
ASRUtils::type_get_past_array(v->m_type));
ASRUtils::type_get_past_array(ttype));

if (is_array) {
type_vec.push_back(i32);
Expand Down Expand Up @@ -2132,7 +2135,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
void visit_Var(const ASR::Var_t &x) {
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v);
auto v = ASR::down_cast<ASR::Variable_t>(s);
switch (ASRUtils::type_get_past_array(v->m_type)->type) {
ASR::ttype_t* ttype = ASRUtils::type_get_past_array(v->m_type);
ttype = ASRUtils::type_get_past_const(ttype);
switch (ttype->type) {
case ASR::ttypeType::Integer:
case ASR::ttypeType::Logical:
case ASR::ttypeType::Real:
Expand Down Expand Up @@ -2263,7 +2268,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {

void visit_IntegerConstant(const ASR::IntegerConstant_t &x) {
int64_t val = x.m_n;
int a_kind = ((ASR::Integer_t *)(&(x.m_type->base)))->m_kind;
int a_kind = ASRUtils::extract_kind_from_ttype_t(x.m_type);
switch (a_kind) {
case 4: {
m_wa.emit_i32_const(val);
Expand Down Expand Up @@ -2940,6 +2945,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
}
ASR::expr_t *v = x.m_values[i];
ASR::ttype_t *t = ASRUtils::expr_type(v);
t = ASRUtils::type_get_past_const(t);
int a_kind = ASRUtils::extract_kind_from_ttype_t(t);

if (ASRUtils::is_integer(*t) || ASRUtils::is_logical(*t)) {
Expand Down