Integrate Buffer<T> experiment into Pointer<T>; Remove CArray

This commit is contained in:
dcodeIO 2018-09-15 02:54:30 +02:00
parent 029dde7c3c
commit 3f93808914
7 changed files with 573 additions and 712 deletions

View File

@ -457,12 +457,6 @@ declare class Array<T> {
sort(comparator?: (a: T, b: T) => i32): this; sort(comparator?: (a: T, b: T) => i32): this;
} }
/** Class representing a C-like array of values of type `T` with limited capabilities. */
declare class CArray<T> {
[key: number]: T;
private constructor();
}
/** Class representing a sequence of characters. */ /** Class representing a sequence of characters. */
declare class String { declare class String {

View File

@ -1,247 +0,0 @@
(module
(type $iifv (func (param i32 i32 f32)))
(type $iif (func (param i32 i32) (result f32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $v (func))
(type $FUNCSIG$i (func (result i32)))
(type $FUNCSIG$ii (func (param i32) (result i32)))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $std/buffer/buf (mut i32) (i32.const 0))
(memory $0 1)
(data (i32.const 8) "\0d\00\00\00s\00t\00d\00/\00b\00u\00f\00f\00e\00r\00.\00t\00s")
(export "memory" (memory $0))
(start $start)
(func $std/buffer/Buffer.from<f32> (; 1 ;) (; has Stack IR ;) (type $FUNCSIG$i) (result i32)
(i32.const 0)
)
(func $std/buffer/Buffer<f32>#set (; 2 ;) (; has Stack IR ;) (type $iifv) (param $0 i32) (param $1 i32) (param $2 f32)
(f32.store
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
(get_local $2)
)
)
(func $std/buffer/Buffer<f32>#get (; 3 ;) (; has Stack IR ;) (type $iif) (param $0 i32) (param $1 i32) (result f32)
(f32.load
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
)
)
(func $std/buffer/Buffer<f32>#slice (; 4 ;) (; has Stack IR ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
(i32.add
(get_local $0)
(i32.const 4)
)
)
(func $start (; 5 ;) (; has Stack IR ;) (type $v)
(set_global $std/buffer/buf
(call $std/buffer/Buffer.from<f32>)
)
(call $std/buffer/Buffer<f32>#set
(get_global $std/buffer/buf)
(i32.const 0)
(f32.const 1.100000023841858)
)
(call $std/buffer/Buffer<f32>#set
(get_global $std/buffer/buf)
(i32.const 1)
(f32.const 1.2000000476837158)
)
(if
(f32.ne
(call $std/buffer/Buffer<f32>#get
(get_global $std/buffer/buf)
(i32.const 0)
)
(f32.const 1.100000023841858)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 26)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(call $std/buffer/Buffer<f32>#get
(get_global $std/buffer/buf)
(i32.const 1)
)
(f32.const 1.2000000476837158)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 27)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(get_global $std/buffer/buf)
)
(f32.const 1.100000023841858)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 29)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.add
(get_global $std/buffer/buf)
(i32.const 4)
)
)
(f32.const 1.2000000476837158)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 30)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.const 0)
)
(f32.const 1.100000023841858)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 32)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.const 4)
)
(f32.const 1.2000000476837158)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 33)
(i32.const 0)
)
(unreachable)
)
)
(f32.store
(i32.add
(get_global $std/buffer/buf)
(i32.const 8)
)
(f32.const 1.2999999523162842)
)
(if
(f32.ne
(call $std/buffer/Buffer<f32>#get
(get_global $std/buffer/buf)
(i32.const 2)
)
(f32.const 1.2999999523162842)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 36)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.add
(get_global $std/buffer/buf)
(i32.const 8)
)
)
(f32.const 1.2999999523162842)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 37)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.const 8)
)
(f32.const 1.2999999523162842)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 38)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(call $std/buffer/Buffer<f32>#slice
(get_global $std/buffer/buf)
)
(i32.const 4)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 40)
(i32.const 0)
)
(unreachable)
)
)
)
)

View File

@ -1,40 +0,0 @@
class Buffer<T> {
static from<T>(ptr: usize): Buffer<T> {
return changetype<Buffer<T>>(ptr);
}
@operator("[]") @inline get(index: i32): T {
return load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
}
@operator("[]=") @inline set(index: i32, value: T): void {
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
}
slice(startIndex: i32): Buffer<T> {
return changetype<Buffer<T>>(changetype<usize>(this) + (<usize>startIndex << alignof<T>()));
}
// ...
}
var buf = Buffer.from<f32>(0);
buf[0] = 1.1;
buf[1] = 1.2;
assert(buf[0] == 1.1);
assert(buf[1] == 1.2);
assert(buf.get(0) == 1.1);
assert(buf.get(1) == 1.2);
assert(load<f32>(0) == 1.1);
assert(load<f32>(4) == 1.2);
buf.set(2, 1.3);
assert(buf[2] == 1.3);
assert(buf.get(2) == 1.3);
assert(load<f32>(8) == 1.3);
assert(buf.slice(1) === changetype<Buffer<f32>>(4));

View File

@ -1,327 +0,0 @@
(module
(type $ii (func (param i32) (result i32)))
(type $iifv (func (param i32 i32 f32)))
(type $iif (func (param i32 i32) (result f32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $iii (func (param i32 i32) (result i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $std/buffer/buf (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 40))
(memory $0 1)
(data (i32.const 8) "\0d\00\00\00s\00t\00d\00/\00b\00u\00f\00f\00e\00r\00.\00t\00s\00")
(export "memory" (memory $0))
(start $start)
(func $std/buffer/Buffer.from<f32> (; 1 ;) (type $ii) (param $0 i32) (result i32)
(get_local $0)
)
(func $std/buffer/Buffer<f32>#set (; 2 ;) (type $iifv) (param $0 i32) (param $1 i32) (param $2 f32)
(f32.store
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
(get_local $2)
)
)
(func $std/buffer/Buffer<f32>#get (; 3 ;) (type $iif) (param $0 i32) (param $1 i32) (result f32)
(f32.load
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
)
)
(func $std/buffer/Buffer<f32>#slice (; 4 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
)
(func $start (; 5 ;) (type $v)
(local $0 i32)
(local $1 i32)
(local $2 f32)
(set_global $std/buffer/buf
(call $std/buffer/Buffer.from<f32>
(i32.const 0)
)
)
(call $std/buffer/Buffer<f32>#set
(get_global $std/buffer/buf)
(i32.const 0)
(f32.const 1.100000023841858)
)
(call $std/buffer/Buffer<f32>#set
(get_global $std/buffer/buf)
(i32.const 1)
(f32.const 1.2000000476837158)
)
(if
(i32.eqz
(f32.eq
(call $std/buffer/Buffer<f32>#get
(get_global $std/buffer/buf)
(i32.const 0)
)
(f32.const 1.100000023841858)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 26)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(call $std/buffer/Buffer<f32>#get
(get_global $std/buffer/buf)
(i32.const 1)
)
(f32.const 1.2000000476837158)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 27)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(block $std/buffer/Buffer<f32>#get|inlined.0 (result f32)
(set_local $0
(get_global $std/buffer/buf)
)
(set_local $1
(i32.const 0)
)
(f32.load
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
)
)
(f32.const 1.100000023841858)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 29)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(block $std/buffer/Buffer<f32>#get|inlined.1 (result f32)
(set_local $1
(get_global $std/buffer/buf)
)
(set_local $0
(i32.const 1)
)
(f32.load
(i32.add
(get_local $1)
(i32.shl
(get_local $0)
(i32.const 2)
)
)
)
)
(f32.const 1.2000000476837158)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 30)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(f32.load
(i32.const 0)
)
(f32.const 1.100000023841858)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 32)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(f32.load
(i32.const 4)
)
(f32.const 1.2000000476837158)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 33)
(i32.const 0)
)
(unreachable)
)
)
(block $std/buffer/Buffer<f32>#set|inlined.0
(set_local $0
(get_global $std/buffer/buf)
)
(set_local $1
(i32.const 2)
)
(set_local $2
(f32.const 1.2999999523162842)
)
(f32.store
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
(get_local $2)
)
)
(if
(i32.eqz
(f32.eq
(call $std/buffer/Buffer<f32>#get
(get_global $std/buffer/buf)
(i32.const 2)
)
(f32.const 1.2999999523162842)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 36)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(block $std/buffer/Buffer<f32>#get|inlined.2 (result f32)
(set_local $1
(get_global $std/buffer/buf)
)
(set_local $0
(i32.const 2)
)
(f32.load
(i32.add
(get_local $1)
(i32.shl
(get_local $0)
(i32.const 2)
)
)
)
)
(f32.const 1.2999999523162842)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 37)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(f32.load
(i32.const 8)
)
(f32.const 1.2999999523162842)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 38)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eq
(call $std/buffer/Buffer<f32>#slice
(get_global $std/buffer/buf)
(i32.const 1)
)
(i32.const 4)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 40)
(i32.const 0)
)
(unreachable)
)
)
)
)

View File

@ -1,8 +1,8 @@
(module (module
(type $iii (func (param i32 i32) (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32))) (type $iiiiv (func (param i32 i32 i32 i32)))
(type $iifv (func (param i32 i32 f32)))
(type $iif (func (param i32 i32) (result f32)))
(type $v (func)) (type $v (func))
(type $iv (func (param i32)))
(type $FUNCSIG$ii (func (param i32) (result i32))) (type $FUNCSIG$ii (func (param i32) (result i32)))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32))) (import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $std/pointer/one (mut i32) (i32.const 0)) (global $std/pointer/one (mut i32) (i32.const 0))
@ -10,17 +10,38 @@
(global $std/pointer/add (mut i32) (i32.const 0)) (global $std/pointer/add (mut i32) (i32.const 0))
(global $std/pointer/sub (mut i32) (i32.const 0)) (global $std/pointer/sub (mut i32) (i32.const 0))
(global $std/pointer/nextOne (mut i32) (i32.const 0)) (global $std/pointer/nextOne (mut i32) (i32.const 0))
(global $~argc (mut i32) (i32.const 0)) (global $std/pointer/buf (mut i32) (i32.const 0))
(memory $0 1) (memory $0 1)
(data (i32.const 8) "\0e\00\00\00s\00t\00d\00/\00p\00o\00i\00n\00t\00e\00r\00.\00t\00s") (data (i32.const 8) "\0e\00\00\00s\00t\00d\00/\00p\00o\00i\00n\00t\00e\00r\00.\00t\00s")
(export "memory" (memory $0)) (export "memory" (memory $0))
(export "_setargc" (func $~setargc))
(export "Pointer<Entry>#constructor" (func $std/pointer/Pointer<Entry>#constructor|trampoline))
(start $start) (start $start)
(func $std/pointer/Pointer<Entry>#constructor (; 1 ;) (; has Stack IR ;) (type $FUNCSIG$ii) (param $0 i32) (result i32) (func $std/pointer/Pointer<Entry>#constructor (; 1 ;) (; has Stack IR ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
(get_local $0) (get_local $0)
) )
(func $start (; 2 ;) (; has Stack IR ;) (type $v) (func $std/pointer/Pointer<f32>#set (; 2 ;) (; has Stack IR ;) (type $iifv) (param $0 i32) (param $1 i32) (param $2 f32)
(f32.store
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
(get_local $2)
)
)
(func $std/pointer/Pointer<f32>#get (; 3 ;) (; has Stack IR ;) (type $iif) (param $0 i32) (param $1 i32) (result f32)
(f32.load
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 2)
)
)
)
)
(func $start (; 4 ;) (; has Stack IR ;) (type $v)
(set_global $std/pointer/one (set_global $std/pointer/one
(call $std/pointer/Pointer<Entry>#constructor (call $std/pointer/Pointer<Entry>#constructor
(i32.const 8) (i32.const 8)
@ -40,7 +61,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 52) (i32.const 66)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -55,7 +76,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 53) (i32.const 67)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -80,7 +101,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 57) (i32.const 71)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -97,7 +118,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 58) (i32.const 72)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -118,7 +139,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 61) (i32.const 75)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -139,7 +160,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 64) (i32.const 78)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -154,7 +175,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 66) (i32.const 80)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -178,7 +199,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 68) (i32.const 82)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -193,7 +214,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 69) (i32.const 83)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -208,7 +229,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 71) (i32.const 85)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -235,7 +256,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 74) (i32.const 88)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -252,7 +273,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 75) (i32.const 89)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -269,34 +290,195 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 76) (i32.const 90)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/buf
(call $std/pointer/Pointer<Entry>#constructor
(i32.const 0)
)
)
(call $std/pointer/Pointer<f32>#set
(get_global $std/pointer/buf)
(i32.const 0)
(f32.const 1.100000023841858)
)
(call $std/pointer/Pointer<f32>#set
(get_global $std/pointer/buf)
(i32.const 1)
(f32.const 1.2000000476837158)
)
(if
(f32.ne
(call $std/pointer/Pointer<f32>#get
(get_global $std/pointer/buf)
(i32.const 0)
)
(f32.const 1.100000023841858)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 96)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(call $std/pointer/Pointer<f32>#get
(get_global $std/pointer/buf)
(i32.const 1)
)
(f32.const 1.2000000476837158)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 97)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(get_global $std/pointer/buf)
)
(f32.const 1.100000023841858)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 99)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.add
(get_global $std/pointer/buf)
(i32.const 4)
)
)
(f32.const 1.2000000476837158)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 100)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.const 0)
)
(f32.const 1.100000023841858)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 102)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.const 4)
)
(f32.const 1.2000000476837158)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 103)
(i32.const 0)
)
(unreachable)
)
)
(f32.store
(i32.add
(get_global $std/pointer/buf)
(i32.const 8)
)
(f32.const 1.2999999523162842)
)
(if
(f32.ne
(call $std/pointer/Pointer<f32>#get
(get_global $std/pointer/buf)
(i32.const 2)
)
(f32.const 1.2999999523162842)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 106)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.add
(get_global $std/pointer/buf)
(i32.const 8)
)
)
(f32.const 1.2999999523162842)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 107)
(i32.const 0)
)
(unreachable)
)
)
(if
(f32.ne
(f32.load
(i32.const 8)
)
(f32.const 1.2999999523162842)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 108)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
) )
) )
) )
(func $std/pointer/Pointer<Entry>#constructor|trampoline (; 3 ;) (; has Stack IR ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(block $1of1
(block $0of1
(block $outOfRange
(br_table $0of1 $1of1 $outOfRange
(get_global $~argc)
)
)
(unreachable)
)
(set_local $1
(i32.const 0)
)
)
(call $std/pointer/Pointer<Entry>#constructor
(get_local $1)
)
)
(func $~setargc (; 4 ;) (; has Stack IR ;) (type $iv) (param $0 i32)
(set_global $~argc
(get_local $0)
)
)
) )

View File

@ -1,6 +1,6 @@
// A pointer arithmetic experiment // A pointer arithmetic experiment
export class Pointer<T> { class Pointer<T> {
// FIXME: does not inline, always yields a trampoline // FIXME: does not inline, always yields a trampoline
@inline constructor(offset: usize = 0) { @inline constructor(offset: usize = 0) {
@ -12,7 +12,11 @@ export class Pointer<T> {
} }
@inline get value(): T { @inline get value(): T {
return changetype<T>(changetype<usize>(this)); if (isReference<T>()) {
return changetype<T>(changetype<usize>(this));
} else {
return load<T>(changetype<usize>(this));
}
} }
// FIXME: in general, inlining any of the following always yields a block. one could argue that // FIXME: in general, inlining any of the following always yields a block. one could argue that
@ -39,6 +43,16 @@ export class Pointer<T> {
const size = isReference<T>() ? offsetof<T>() : sizeof<T>(); const size = isReference<T>() ? offsetof<T>() : sizeof<T>();
return changetype<Pointer<T>>(changetype<usize>(this) - size); return changetype<Pointer<T>>(changetype<usize>(this) - size);
} }
@inline @operator("[]") get(index: i32): T {
const size = isReference<T>() ? offsetof<T>() : sizeof<T>();
return load<T>(changetype<usize>(this) + (<usize>index * size));
}
@inline @operator("[]=") set(index: i32, value: T): void {
const size = isReference<T>() ? offsetof<T>() : sizeof<T>();
store<T>(changetype<usize>(this) + (<usize>index * size), value);
}
} }
@unmanaged @unmanaged
@ -74,3 +88,21 @@ assert(two.offset == 24);
assert(two.offset == 8); assert(two.offset == 8);
assert(two.value.key == 1); assert(two.value.key == 1);
assert(two.value.val == 2); assert(two.value.val == 2);
var buf = new Pointer<f32>(0);
buf[0] = 1.1;
buf[1] = 1.2;
assert(buf[0] == 1.1);
assert(buf[1] == 1.2);
assert(buf.get(0) == 1.1);
assert(buf.get(1) == 1.2);
assert(load<f32>(0) == 1.1);
assert(load<f32>(4) == 1.2);
buf.set(2, 1.3);
assert(buf[2] == 1.3);
assert(buf.get(2) == 1.3);
assert(load<f32>(8) == 1.3);

View File

@ -1,28 +1,54 @@
(module (module
(type $iii (func (param i32 i32) (result i32))) (type $iii (func (param i32 i32) (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32))) (type $iiiiv (func (param i32 i32 i32 i32)))
(type $iifv (func (param i32 i32 f32)))
(type $iif (func (param i32 i32) (result f32)))
(type $v (func)) (type $v (func))
(type $iv (func (param i32)))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32))) (import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $std/pointer/one (mut i32) (i32.const 0)) (global $std/pointer/one (mut i32) (i32.const 0))
(global $std/pointer/two (mut i32) (i32.const 0)) (global $std/pointer/two (mut i32) (i32.const 0))
(global $std/pointer/add (mut i32) (i32.const 0)) (global $std/pointer/add (mut i32) (i32.const 0))
(global $std/pointer/sub (mut i32) (i32.const 0)) (global $std/pointer/sub (mut i32) (i32.const 0))
(global $std/pointer/nextOne (mut i32) (i32.const 0)) (global $std/pointer/nextOne (mut i32) (i32.const 0))
(global $std/pointer/buf (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 40)) (global $HEAP_BASE i32 (i32.const 40))
(global $~argc (mut i32) (i32.const 0))
(memory $0 1) (memory $0 1)
(data (i32.const 8) "\0e\00\00\00s\00t\00d\00/\00p\00o\00i\00n\00t\00e\00r\00.\00t\00s\00") (data (i32.const 8) "\0e\00\00\00s\00t\00d\00/\00p\00o\00i\00n\00t\00e\00r\00.\00t\00s\00")
(export "memory" (memory $0)) (export "memory" (memory $0))
(export "_setargc" (func $~setargc))
(export "Pointer<Entry>#constructor" (func $std/pointer/Pointer<Entry>#constructor|trampoline))
(start $start) (start $start)
(func $std/pointer/Pointer<Entry>#constructor (; 1 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (func $std/pointer/Pointer<Entry>#constructor (; 1 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(get_local $1) (get_local $1)
) )
(func $start (; 2 ;) (type $v) (func $std/pointer/Pointer<f32>#constructor (; 2 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(get_local $1)
)
(func $std/pointer/Pointer<f32>#set (; 3 ;) (type $iifv) (param $0 i32) (param $1 i32) (param $2 f32)
(f32.store
(i32.add
(get_local $0)
(i32.mul
(get_local $1)
(i32.const 4)
)
)
(get_local $2)
)
)
(func $std/pointer/Pointer<f32>#get (; 4 ;) (type $iif) (param $0 i32) (param $1 i32) (result f32)
(f32.load
(i32.add
(get_local $0)
(i32.mul
(get_local $1)
(i32.const 4)
)
)
)
)
(func $start (; 5 ;) (type $v)
(local $0 i32) (local $0 i32)
(local $1 i32) (local $1 i32)
(local $2 f32)
(set_global $std/pointer/one (set_global $std/pointer/one
(call $std/pointer/Pointer<Entry>#constructor (call $std/pointer/Pointer<Entry>#constructor
(i32.const 0) (i32.const 0)
@ -51,7 +77,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 52) (i32.const 66)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -73,7 +99,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 53) (i32.const 67)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -84,7 +110,9 @@
(set_local $0 (set_local $0
(get_global $std/pointer/one) (get_global $std/pointer/one)
) )
(get_local $0) (br $std/pointer/Pointer<Entry>#get:value|inlined.0
(get_local $0)
)
) )
(i32.const 1) (i32.const 1)
) )
@ -93,7 +121,9 @@
(set_local $0 (set_local $0
(get_global $std/pointer/one) (get_global $std/pointer/one)
) )
(get_local $0) (br $std/pointer/Pointer<Entry>#get:value|inlined.1
(get_local $0)
)
) )
(i32.const 2) (i32.const 2)
) )
@ -105,7 +135,9 @@
(set_local $0 (set_local $0
(get_global $std/pointer/one) (get_global $std/pointer/one)
) )
(get_local $0) (br $std/pointer/Pointer<Entry>#get:value|inlined.2
(get_local $0)
)
) )
) )
(i32.const 1) (i32.const 1)
@ -115,7 +147,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 57) (i32.const 71)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -129,7 +161,9 @@
(set_local $0 (set_local $0
(get_global $std/pointer/one) (get_global $std/pointer/one)
) )
(get_local $0) (br $std/pointer/Pointer<Entry>#get:value|inlined.3
(get_local $0)
)
) )
) )
(i32.const 2) (i32.const 2)
@ -139,7 +173,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 58) (i32.const 72)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -175,7 +209,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 61) (i32.const 75)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -211,7 +245,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 64) (i32.const 78)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -233,7 +267,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 66) (i32.const 80)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -266,7 +300,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 68) (i32.const 82)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -288,7 +322,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 69) (i32.const 83)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -310,7 +344,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 71) (i32.const 85)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -354,7 +388,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 74) (i32.const 88)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -368,7 +402,9 @@
(set_local $0 (set_local $0
(get_global $std/pointer/two) (get_global $std/pointer/two)
) )
(get_local $0) (br $std/pointer/Pointer<Entry>#get:value|inlined.4
(get_local $0)
)
) )
) )
(i32.const 1) (i32.const 1)
@ -378,7 +414,7 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 75) (i32.const 89)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
@ -392,7 +428,9 @@
(set_local $0 (set_local $0
(get_global $std/pointer/two) (get_global $std/pointer/two)
) )
(get_local $0) (br $std/pointer/Pointer<Entry>#get:value|inlined.5
(get_local $0)
)
) )
) )
(i32.const 2) (i32.const 2)
@ -402,35 +440,264 @@
(call $~lib/env/abort (call $~lib/env/abort
(i32.const 0) (i32.const 0)
(i32.const 8) (i32.const 8)
(i32.const 76) (i32.const 90)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/buf
(call $std/pointer/Pointer<f32>#constructor
(i32.const 0)
(i32.const 0)
)
)
(call $std/pointer/Pointer<f32>#set
(get_global $std/pointer/buf)
(i32.const 0)
(f32.const 1.100000023841858)
)
(call $std/pointer/Pointer<f32>#set
(get_global $std/pointer/buf)
(i32.const 1)
(f32.const 1.2000000476837158)
)
(if
(i32.eqz
(f32.eq
(call $std/pointer/Pointer<f32>#get
(get_global $std/pointer/buf)
(i32.const 0)
)
(f32.const 1.100000023841858)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 96)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(call $std/pointer/Pointer<f32>#get
(get_global $std/pointer/buf)
(i32.const 1)
)
(f32.const 1.2000000476837158)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 97)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(block $std/pointer/Pointer<f32>#get|inlined.0 (result f32)
(set_local $0
(get_global $std/pointer/buf)
)
(set_local $1
(i32.const 0)
)
(f32.load
(i32.add
(get_local $0)
(i32.mul
(get_local $1)
(i32.const 4)
)
)
)
)
(f32.const 1.100000023841858)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 99)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(block $std/pointer/Pointer<f32>#get|inlined.1 (result f32)
(set_local $1
(get_global $std/pointer/buf)
)
(set_local $0
(i32.const 1)
)
(f32.load
(i32.add
(get_local $1)
(i32.mul
(get_local $0)
(i32.const 4)
)
)
)
)
(f32.const 1.2000000476837158)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 100)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(f32.load
(i32.const 0)
)
(f32.const 1.100000023841858)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 102)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(f32.load
(i32.const 4)
)
(f32.const 1.2000000476837158)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 103)
(i32.const 0)
)
(unreachable)
)
)
(block $std/pointer/Pointer<f32>#set|inlined.0
(set_local $0
(get_global $std/pointer/buf)
)
(set_local $1
(i32.const 2)
)
(set_local $2
(f32.const 1.2999999523162842)
)
(f32.store
(i32.add
(get_local $0)
(i32.mul
(get_local $1)
(i32.const 4)
)
)
(get_local $2)
)
)
(if
(i32.eqz
(f32.eq
(call $std/pointer/Pointer<f32>#get
(get_global $std/pointer/buf)
(i32.const 2)
)
(f32.const 1.2999999523162842)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 106)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(block $std/pointer/Pointer<f32>#get|inlined.2 (result f32)
(set_local $1
(get_global $std/pointer/buf)
)
(set_local $0
(i32.const 2)
)
(f32.load
(i32.add
(get_local $1)
(i32.mul
(get_local $0)
(i32.const 4)
)
)
)
)
(f32.const 1.2999999523162842)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 107)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(f32.eq
(f32.load
(i32.const 8)
)
(f32.const 1.2999999523162842)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 108)
(i32.const 0) (i32.const 0)
) )
(unreachable) (unreachable)
) )
) )
) )
(func $std/pointer/Pointer<Entry>#constructor|trampoline (; 3 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(block $1of1
(block $0of1
(block $outOfRange
(br_table $0of1 $1of1 $outOfRange
(get_global $~argc)
)
)
(unreachable)
)
(set_local $1
(i32.const 0)
)
)
(call $std/pointer/Pointer<Entry>#constructor
(get_local $0)
(get_local $1)
)
)
(func $~setargc (; 4 ;) (type $iv) (param $0 i32)
(set_global $~argc
(get_local $0)
)
)
) )