Skip to content

Commit

Permalink
WASM: Support visit_StringLen()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaikh-Ubaid committed Aug 1, 2023
1 parent 1180f99 commit e582946
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/libasr/codegen/asr_to_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ enum RT_FUNCS {
abs_c64 = 10,
equal_c32 = 11,
equal_c64 = 12,
NO_OF_RT_FUNCS = 13,
str_len = 13,
NO_OF_RT_FUNCS = 14,
};

enum GLOBAL_VAR {
Expand Down Expand Up @@ -552,6 +553,14 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
});
}

void emit_str_len() {
using namespace wasm;
m_wa.define_func({i32}, {i32}, {}, "str_len", [&](){
m_wa.emit_local_get(0);
m_wa.emit_i32_load(wasm::mem_align::b8, 4);
});
}

void declare_global_var(ASR::Variable_t* v) {
if (v->m_type->type == ASR::ttypeType::TypeParameter) {
// Ignore type variables
Expand Down Expand Up @@ -688,6 +697,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
m_rt_funcs_map[abs_c64] = &ASRToWASMVisitor::emit_complex_abs_64;
m_rt_funcs_map[equal_c32] = &ASRToWASMVisitor::emit_complex_equal_32;
m_rt_funcs_map[equal_c64] = &ASRToWASMVisitor::emit_complex_equal_64;
m_rt_funcs_map[str_len] = &ASRToWASMVisitor::emit_str_len;

{
// Pre-declare all functions first, then generate code
Expand Down Expand Up @@ -1935,6 +1945,16 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
throw CodeGenError("String Types not yet supported");
}

void visit_StringLen(const ASR::StringLen_t & x) {
if (x.m_value) {
visit_expr(*x.m_value);
return;
}
INCLUDE_RUNTIME_FUNC(str_len);
this->visit_expr(*x.m_arg);
m_wa.emit_call(m_rt_func_used_idx[str_len]);
}

void visit_LogicalBinOp(const ASR::LogicalBinOp_t &x) {
if (x.m_value) {
visit_expr(*x.m_value);
Expand Down

0 comments on commit e582946

Please sign in to comment.