Skip to content

Commit 502761a

Browse files
committed
Add WASM changes from LFortran
1 parent 7850aa4 commit 502761a

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
667667
if (ASR::is_a<ASR::Function_t>(*item.second)) {
668668
ASR::Function_t *s =
669669
ASR::down_cast<ASR::Function_t>(item.second);
670-
this->visit_Function(*s);
670+
if (ASRUtils::get_FunctionType(s)->n_type_params == 0) {
671+
this->visit_Function(*s);
672+
}
671673
}
672674
}
673675
}
@@ -2444,6 +2446,49 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
24442446
wasm::emit_unreachable(m_code_section, m_al); // raise trap/exception
24452447
}
24462448

2449+
void visit_ArrayBound(const ASR::ArrayBound_t& x) {
2450+
ASR::ttype_t *ttype = ASRUtils::expr_type(x.m_v);
2451+
uint32_t kind = ASRUtils::extract_kind_from_ttype_t(ttype);
2452+
ASR::dimension_t *m_dims;
2453+
int n_dims = ASRUtils::extract_dimensions_from_ttype(ttype, m_dims);
2454+
if (kind != 4) {
2455+
throw CodeGenError("ArrayBound: Kind 4 only supported currently");
2456+
}
2457+
2458+
if (x.m_dim) {
2459+
ASR::expr_t *val = ASRUtils::expr_value(x.m_dim);
2460+
2461+
if (!ASR::is_a<ASR::IntegerConstant_t>(*val)) {
2462+
throw CodeGenError("ArrayBound: Only constant dim values supported currently");
2463+
}
2464+
ASR::IntegerConstant_t *dimDir = ASR::down_cast<ASR::IntegerConstant_t>(val);
2465+
if (x.m_bound == ASR::arrayboundType::LBound) {
2466+
this->visit_expr(*m_dims[dimDir->m_n - 1].m_start);
2467+
} else {
2468+
this->visit_expr(*m_dims[dimDir->m_n - 1].m_start);
2469+
this->visit_expr(*m_dims[dimDir->m_n - 1].m_length);
2470+
wasm::emit_i32_add(m_code_section, m_al);
2471+
wasm::emit_i32_const(m_code_section, m_al, 1);
2472+
wasm::emit_i32_sub(m_code_section, m_al);
2473+
}
2474+
} else {
2475+
if (x.m_bound == ASR::arrayboundType::LBound) {
2476+
wasm::emit_i32_const(m_code_section, m_al, 1);
2477+
} else {
2478+
// emit the whole array size
2479+
if (!m_dims[0].m_length) {
2480+
throw CodeGenError(
2481+
"ArrayBound: Dimension length for index 0 does not exist");
2482+
}
2483+
this->visit_expr(*(m_dims[0].m_length));
2484+
for (int i = 1; i < n_dims; i++) {
2485+
this->visit_expr(*m_dims[i].m_length);
2486+
wasm::emit_i32_mul(m_code_section, m_al);
2487+
}
2488+
}
2489+
}
2490+
}
2491+
24472492
void visit_Stop(const ASR::Stop_t &x) {
24482493
print_msg("STOP");
24492494
if (x.m_code &&

0 commit comments

Comments
 (0)