Skip to content

Commit c8b1ba9

Browse files
authored
Merge pull request #2297 from Shaikh-Ubaid/wasm_support_const
WASM: Initial support for const type
2 parents b33af13 + e04d01b commit c8b1ba9

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,8 @@ RUN(NAME print_list_tuple_02 LABELS cpython llvm c NOFAST)
454454
RUN(NAME print_list_tuple_03 LABELS cpython llvm c NOFAST)
455455

456456
# CPython and LLVM
457-
RUN(NAME const_01 LABELS cpython llvm c)
458-
RUN(NAME const_02 LABELS cpython llvm c)
457+
RUN(NAME const_01 LABELS cpython llvm c wasm)
458+
RUN(NAME const_02 LABELS cpython llvm c wasm)
459459
RUN(NAME const_03 LABELS cpython llvm c
460460
EXTRAFILES const_03b.c)
461461
RUN(NAME const_04 LABELS cpython llvm c)

integration_tests/const_01.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def test_const_variables():
1313
ycf: Const[f64] = 3.0
1414
yf: f64 = 3.0
1515

16+
print(xci, xi)
17+
print(yci, yi)
18+
print(xcf, xf)
19+
print(ycf, yf)
20+
1621
assert xci == xi
1722
assert yci == yi
1823
assert xcf == xf

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
647647
}
648648

649649
using namespace wasm;
650-
int kind = ASRUtils::extract_kind_from_ttype_t(v->m_type);
651650
uint32_t global_var_idx = UINT_MAX;
652-
ASR::ttype_t* v_m_type = ASRUtils::type_get_past_array(v->m_type);
651+
ASR::ttype_t* ttype = ASRUtils::type_get_past_const(v->m_type);
652+
ASR::ttype_t* v_m_type = ASRUtils::type_get_past_array(ttype);
653+
int kind = ASRUtils::extract_kind_from_ttype_t(ttype);
653654
switch (v_m_type->type){
654655
case ASR::ttypeType::Integer: {
655656
uint64_t init_val = 0;
@@ -877,9 +878,11 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
877878
throw CodeGenAbort();
878879
}
879880
} else {
880-
if (ASRUtils::is_integer(*v->m_type)) {
881+
ASR::ttype_t* ttype = v->m_type;
882+
ttype = ASRUtils::type_get_past_const(ttype);
883+
if (ASRUtils::is_integer(*ttype)) {
881884
ASR::Integer_t *v_int =
882-
ASR::down_cast<ASR::Integer_t>(ASRUtils::type_get_past_array(v->m_type));
885+
ASR::down_cast<ASR::Integer_t>(ASRUtils::type_get_past_array(ttype));
883886
if (is_array) {
884887
type_vec.push_back(i32);
885888
} else {
@@ -892,9 +895,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
892895
"Integers of kind 4 and 8 only supported");
893896
}
894897
}
895-
} else if (ASRUtils::is_real(*v->m_type)) {
898+
} else if (ASRUtils::is_real(*ttype)) {
896899
ASR::Real_t *v_float = ASR::down_cast<ASR::Real_t>(
897-
ASRUtils::type_get_past_array(v->m_type));
900+
ASRUtils::type_get_past_array(ttype));
898901

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

916919
if (is_array) {
917920
type_vec.push_back(i32);
@@ -923,10 +926,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
923926
throw CodeGenError("Logicals of kind 4 only supported");
924927
}
925928
}
926-
} else if (ASRUtils::is_character(*v->m_type)) {
929+
} else if (ASRUtils::is_character(*ttype)) {
927930
ASR::Character_t *v_int =
928931
ASR::down_cast<ASR::Character_t>(
929-
ASRUtils::type_get_past_array(v->m_type));
932+
ASRUtils::type_get_past_array(ttype));
930933

931934
if (is_array) {
932935
type_vec.push_back(i32);
@@ -941,10 +944,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
941944
"Characters of kind 1 only supported");
942945
}
943946
}
944-
} else if (ASRUtils::is_complex(*v->m_type)) {
947+
} else if (ASRUtils::is_complex(*ttype)) {
945948
ASR::Complex_t *v_comp =
946949
ASR::down_cast<ASR::Complex_t>(
947-
ASRUtils::type_get_past_array(v->m_type));
950+
ASRUtils::type_get_past_array(ttype));
948951

949952
if (is_array) {
950953
type_vec.push_back(i32);
@@ -2132,7 +2135,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
21322135
void visit_Var(const ASR::Var_t &x) {
21332136
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v);
21342137
auto v = ASR::down_cast<ASR::Variable_t>(s);
2135-
switch (ASRUtils::type_get_past_array(v->m_type)->type) {
2138+
ASR::ttype_t* ttype = ASRUtils::type_get_past_array(v->m_type);
2139+
ttype = ASRUtils::type_get_past_const(ttype);
2140+
switch (ttype->type) {
21362141
case ASR::ttypeType::Integer:
21372142
case ASR::ttypeType::Logical:
21382143
case ASR::ttypeType::Real:
@@ -2263,7 +2268,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
22632268

22642269
void visit_IntegerConstant(const ASR::IntegerConstant_t &x) {
22652270
int64_t val = x.m_n;
2266-
int a_kind = ((ASR::Integer_t *)(&(x.m_type->base)))->m_kind;
2271+
int a_kind = ASRUtils::extract_kind_from_ttype_t(x.m_type);
22672272
switch (a_kind) {
22682273
case 4: {
22692274
m_wa.emit_i32_const(val);
@@ -2940,6 +2945,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
29402945
}
29412946
ASR::expr_t *v = x.m_values[i];
29422947
ASR::ttype_t *t = ASRUtils::expr_type(v);
2948+
t = ASRUtils::type_get_past_const(t);
29432949
int a_kind = ASRUtils::extract_kind_from_ttype_t(t);
29442950

29452951
if (ASRUtils::is_integer(*t) || ASRUtils::is_logical(*t)) {

0 commit comments

Comments
 (0)