MikeInnes / Charlotte.jl

Charlotte's Web Scale
Other
113 stars 13 forks source link

Implementing structs #25

Open sjorn3 opened 6 years ago

sjorn3 commented 6 years ago

If we assume that structs are always going to be stored in memory (which for the time being, it's fine if they are), a relatively simple implementation for them presents itself in that we can just replace getfield and setfield! with calls to load/store memory operations at the offset given by fieldoffset.

(I don't really know what alignment is and I can't seem to find out how to set it to the default value that the wasm compiler uses, so for the time being it's just set to 1, it seems to only effect efficiency and not correctness.)

Here's the decompiled output:

(func $#addTwo_AddTwo (type 0) (param i32) (result i32)
    get_local 0
    i32.load align=1
    get_local 0
    i32.load offset=4 align=1
    i32.add
    return)
(func $#altertwo_AddTwo (type 0) (param i32) (result i32)
    get_local 0
    get_local 0
    i32.load offset=4 align=1
    i32.store align=1
    get_local 0
    return)

addTwo Adds the two values in the struct together and returns the result and altertwo sets the first value to the second and returns the struct.