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, WASM_X64: Support global variables #1645

Merged
merged 11 commits into from
Mar 30, 2023
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,4 @@ RUN(NAME test_argv_01 LABELS llvm) # TODO: Test using CPython
RUN(NAME global_syms_01 LABELS cpython llvm c)
RUN(NAME global_syms_02 LABELS cpython llvm c)
RUN(NAME global_syms_03_b LABELS cpython llvm c)
RUN(NAME global_syms_04 LABELS cpython llvm c wasm wasm_x64)
37 changes: 37 additions & 0 deletions integration_tests/global_syms_04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from lpython import i32, i64, f64

a: str = "hi"
b: i32 = -24
c: i64 = i64(151)
d: f64 = -68.512

def print_global_symbols():
print(a)
print(b)
print(c)
print(d)

def test_global_symbols():
assert b == -24
assert c == i64(151)
assert abs(d - (-68.512)) <= 1e-12

def update_global_symbols():
global b, c, d
x: f64 = f64(c) * d
b = i32(int(x))
y: f64 = f64(b) / 12.0
c = i64(int(y))
z: i64 = i64(b) * c
d = f64(z)

def test_global_symbols_post_update():
assert b == -10345
assert c == i64(-862)
assert abs(d - 8917390.0) <= 1e-12

print_global_symbols()
test_global_symbols()
update_global_symbols()
print_global_symbols()
test_global_symbols_post_update()
7 changes: 4 additions & 3 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
} else if (ASRUtils::is_character(*v_m_type)) {
ASR::Character_t *t = ASR::down_cast<ASR::Character_t>(v_m_type);
bool is_fixed_size = true;
std::string dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size);
dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size);
sub = format_type_c(dims, "char *", v.m_name, use_ref, dummy);
if( v.m_intent == ASRUtils::intent_local &&
if(v.m_intent == ASRUtils::intent_local &&
!(ASR::is_a<ASR::symbol_t>(*v.m_parent_symtab->asr_owner) &&
ASR::is_a<ASR::StructType_t>(
*ASR::down_cast<ASR::symbol_t>(v.m_parent_symtab->asr_owner))) ) {
*ASR::down_cast<ASR::symbol_t>(v.m_parent_symtab->asr_owner))) &&
!(dims.size() == 0 && v.m_symbolic_value)) {
sub += " = NULL";
return sub;
}
Expand Down
541 changes: 301 additions & 240 deletions src/libasr/codegen/asr_to_wasm.cpp

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/libasr/codegen/wasm_assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,25 +225,25 @@ void encode_section(Vec<uint8_t> &des, Vec<uint8_t> &section_content,
void emit_drop(Vec<uint8_t> &code, Allocator &al) { code.push_back(al, 0x1A); }

// function to emit get local variable at given index
void emit_get_local(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
void emit_local_get(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
code.push_back(al, 0x20);
emit_u32(code, al, idx);
}

// function to emit set local variable at given index
void emit_set_local(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
void emit_local_set(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
code.push_back(al, 0x21);
emit_u32(code, al, idx);
}

// function to emit get global variable at given index
void emit_get_global(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
void emit_global_get(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
code.push_back(al, 0x23);
emit_u32(code, al, idx);
}

// function to emit set global variable at given index
void emit_set_global(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
void emit_global_set(Vec<uint8_t> &code, Allocator &al, uint32_t idx) {
code.push_back(al, 0x24);
emit_u32(code, al, idx);
}
Expand Down
8 changes: 4 additions & 4 deletions src/libasr/codegen/wasm_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ class WASMDecoder {
wasm::read_b8(wasm_bytes, offset);
switch (globals[i].type)
{
case 0x7F: wasm::read_i32(wasm_bytes, offset); break;
case 0x7E: wasm::read_i64(wasm_bytes, offset); break;
case 0x7D: wasm::read_f32(wasm_bytes, offset); break;
case 0x7C: wasm::read_f64(wasm_bytes, offset); break;
case 0x7F: globals.p[i].n32 = wasm::read_i32(wasm_bytes, offset); break;
case 0x7E: globals.p[i].n64 = wasm::read_i64(wasm_bytes, offset); break;
case 0x7D: globals.p[i].r32 = wasm::read_f32(wasm_bytes, offset); break;
case 0x7C: globals.p[i].r64 = wasm::read_f64(wasm_bytes, offset); break;
default: throw CodeGenError("decode_global_section: Unsupport global type"); break;
}

Expand Down
15 changes: 4 additions & 11 deletions src/libasr/codegen/wasm_to_x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,29 +623,22 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
}

for (size_t i = 0; i < globals.size(); i++) {
uint32_t tmp_offset = globals[i].insts_start_idx;
wasm::read_b8(wasm_bytes, tmp_offset); // read byte for i32/i64/f32/f64.const

std::string global_loc = "global_" + std::to_string(i);
switch (globals[i].type) {
case 0x7F: {
int32_t val = wasm::read_i32(wasm_bytes, offset);
emit_i64_const(m_a, global_loc, val);
emit_i64_const(m_a, global_loc, globals[i].n32);
break;
}
case 0x7E: {
int64_t val = wasm::read_i64(wasm_bytes, offset);
emit_i64_const(m_a, global_loc, val);
emit_i64_const(m_a, global_loc, globals[i].n64);
break;
}
case 0x7D: {
float val = wasm::read_f32(wasm_bytes, offset);
emit_double_const(m_a, global_loc, val);
emit_double_const(m_a, global_loc, globals[i].r32);
break;
}
case 0x7C: {
double val = wasm::read_f64(wasm_bytes, offset);
emit_double_const(m_a, global_loc, val);
emit_double_const(m_a, global_loc, globals[i].r64);
break;
}
default: throw CodeGenError("decode_global_section: Unsupport global type"); break;
Expand Down
6 changes: 6 additions & 0 deletions src/libasr/codegen/wasm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ struct Global {
uint8_t type;
uint8_t mut;
uint32_t insts_start_idx;
union {
int32_t n32;
int64_t n64;
float r32;
double r64;
};
};

struct Export {
Expand Down
4 changes: 4 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6315,6 +6315,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// Handled by SymbolTableVisitor already
tmp = nullptr;
}

void visit_Global(const AST::Global_t &/*x*/) {
tmp = nullptr;
}
};

Result<ASR::TranslationUnit_t*> body_visitor(Allocator &al, LocationManager &lm,
Expand Down