Skip to content

Instantly share code, notes, and snippets.

@jonathanvdc
Created March 1, 2016 17:54
Show Gist options
  • Save jonathanvdc/1ee676182c4d29777735 to your computer and use it in GitHub Desktop.
Save jonathanvdc/1ee676182c4d29777735 to your computer and use it in GitHub Desktop.
Wasm Structs
// Compile with `dsc Struct.ds -platform wasm -Wno-build`
public struct Vector2
{
public int X;
public int Y;
public int LengthSquared()
{
return X * X + Y * Y;
}
}
public module StructTest
{
public int Allocate()
{
Vector2 val;
val.X = 3;
val.Y = 4;
Vector2 copy = val;
return copy.LengthSquared();
}
}
(module
(func
$Vector2.LengthSquared
(param $stacktop i32)
(param $this i32)
(result i32)
(local $frameptr i32)
(local $stackptr i32)
(block
(set_local $frameptr (get_local $stacktop))
(set_local $stackptr (get_local $frameptr))
(return
(i32.add
(i32.mul
(i32.load (i32.add (get_local $this) (i32.const 0)))
(i32.load (i32.add (get_local $this) (i32.const 0))))
(i32.mul
(i32.load (i32.add (get_local $this) (i32.const 4)))
(i32.load (i32.add (get_local $this) (i32.const 4))))))))
(func
$StructTest.Allocate
(param $stacktop i32)
(result i32)
(local $frameptr i32)
(local $stackptr i32)
(local $tmp0 i32)
(local $tmp1 i32)
(block
(set_local $frameptr (get_local $stacktop))
(set_local $stackptr (get_local $frameptr))
(set_local $stackptr (i32.add (get_local $stackptr) (i32.const 16)))
(i32.store (i32.add (get_local $frameptr) (i32.const 0)) (i32.const 3))
(i32.store (i32.add (get_local $frameptr) (i32.const 4)) (i32.const 4))
(set_local $tmp0 (i32.add (get_local $frameptr) (i32.const 8)))
(set_local $tmp1 (i32.add (get_local $frameptr) (i32.const 0)))
(i32.store
(i32.add (get_local $tmp1) (i32.const 0))
(i32.load (i32.add (get_local $tmp0) (i32.const 0))))
(i32.store
(i32.add (get_local $tmp1) (i32.const 4))
(i32.load (i32.add (get_local $tmp0) (i32.const 4))))
(return
(call
$Vector2.LengthSquared
(get_local $stackptr)
(i32.add (get_local $frameptr) (i32.const 8))))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment