Add pointer arithmetic experiment to tests

While questionable, it highlights some of the things that currently do not inline well.
This commit is contained in:
dcodeIO 2018-06-21 03:09:58 +02:00
parent c44cbec2c7
commit 02e0a91070
3 changed files with 792 additions and 0 deletions

View File

@ -0,0 +1,319 @@
(module
(type $iii (func (param i32 i32) (result i32)))
(type $iv (func (param i32)))
(type $ii (func (param i32) (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $~argc (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/add (mut i32) (i32.const 0))
(global $std/pointer/sub (mut i32) (i32.const 0))
(memory $0 1)
(data (i32.const 8) "\0e\00\00\00s\00t\00d\00/\00p\00o\00i\00n\00t\00e\00r\00.\00t\00s")
(export "_setargc" (func $~setargc))
(export "Pointer<Entry>#constructor" (func $std/pointer/Pointer<Entry>#constructor|trampoline))
(export "Pointer<Entry>#get:offset" (func $std/pointer/Pointer<Entry>#get:offset))
(export "Pointer<Entry>#get:value" (func $std/pointer/Pointer<Entry>#get:offset))
(export "memory" (memory $0))
(start $start)
(func $std/pointer/Pointer<Entry>#constructor (; 1 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(get_local $1)
)
(func $std/pointer/Pointer<Entry>#constructor|trampoline (; 2 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(block $1of1
(block $0of1
(block $oob
(br_table $0of1 $1of1 $oob
(get_global $~argc)
)
)
(unreachable)
)
(set_local $1
(i32.const 0)
)
)
(call $std/pointer/Pointer<Entry>#constructor
(get_local $0)
(get_local $1)
)
)
(func $~setargc (; 3 ;) (type $iv) (param $0 i32)
(set_global $~argc
(get_local $0)
)
)
(func $std/pointer/Pointer<Entry>#get:offset (; 4 ;) (type $ii) (param $0 i32) (result i32)
(get_local $0)
)
(func $start (; 5 ;) (type $v)
(set_global $std/pointer/one
(call $std/pointer/Pointer<Entry>#constructor
(i32.const 0)
(i32.const 8)
)
)
(set_global $std/pointer/two
(call $std/pointer/Pointer<Entry>#constructor
(i32.const 0)
(i32.const 24)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 8)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 54)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
(i32.const 24)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 55)
(i32.const 0)
)
(unreachable)
)
)
(i32.store
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 1)
)
(i32.store offset=4
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 2)
)
(if
(i32.ne
(i32.load
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
)
(i32.const 1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 59)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=4
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
)
(i32.const 2)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 60)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/add
(i32.add
(get_global $std/pointer/one)
(get_global $std/pointer/two)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/add)
)
(i32.const 32)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 63)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/sub
(i32.sub
(get_global $std/pointer/two)
(get_global $std/pointer/one)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/sub)
)
(i32.const 16)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 66)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 8)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 68)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/one
(i32.add
(get_global $std/pointer/one)
(i32.const 8)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 16)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 70)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
(i32.const 24)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 72)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/two
(i32.sub
(get_global $std/pointer/two)
(i32.const 8)
)
)
(set_global $std/pointer/two
(i32.sub
(get_global $std/pointer/two)
(i32.const 8)
)
)
(if
(i32.ne
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
(i32.const 8)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 75)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
)
(i32.const 1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 76)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=4
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
)
(i32.const 2)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 77)
(i32.const 0)
)
(unreachable)
)
)
)
)

View File

@ -0,0 +1,77 @@
// A pointer arithmetic experiment
export class Pointer<T> {
// FIXME: does not inline, always yields a trampoline
@inline constructor(offset: usize = 0) {
return changetype<Pointer<T>>(offset);
}
// FIXME: does not inline
@inline get offset(): usize {
return changetype<usize>(this);
}
// FIXME: does not inline
@inline get value(): T {
return changetype<T>(changetype<usize>(this));
}
// FIXME: in general, inlining any of the following always yields a block. one could argue that
// this helps debuggability, or that it is unnecessary overhead due to the simplicity of the
// functions. a compromise could be to inline a block consisting of a single 'return' as is,
// where possible.
@inline @operator("+") add(other: Pointer<T>): Pointer<T> {
return changetype<Pointer<T>>(changetype<usize>(this) + changetype<usize>(other));
}
@inline @operator("-") sub(other: Pointer<T>): Pointer<T> {
return changetype<Pointer<T>>(changetype<usize>(this) - changetype<usize>(other));
}
@inline @operator.prefix("++") inc(): Pointer<T> {
// FIXME: this should take alignment into account, but then would require a new builtin to
// determine the minimal alignment of a struct by evaluating its field layout.
const size = isReference<T>() ? offsetof<T>() : sizeof<T>();
return changetype<Pointer<T>>(changetype<usize>(this) + size);
}
@inline @operator.prefix("--") dec(): Pointer<T> {
const size = isReference<T>() ? offsetof<T>() : sizeof<T>();
return changetype<Pointer<T>>(changetype<usize>(this) - size);
}
}
@unmanaged
class Entry {
key: i32;
val: i32;
}
var one = new Pointer<Entry>(8);
var two = new Pointer<Entry>(24);
assert(one.offset == 8);
assert(two.offset == 24);
one.value.key = 1;
one.value.val = 2;
assert(one.value.key == 1);
assert(one.value.val == 2);
var add = one + two;
assert(add.offset == 32);
var sub = two - one;
assert(sub.offset == 16);
assert(one.offset == 8);
++one; // FIXME: assigning to a var yields an 'auto to void' error
assert(one.offset == 16);
assert(two.offset == 24);
--two;
--two;
assert(two.offset == 8);
assert(two.value.key == 1);
assert(two.value.val == 2);

View File

@ -0,0 +1,396 @@
(module
(type $iii (func (param i32 i32) (result i32)))
(type $iv (func (param i32)))
(type $ii (func (param i32) (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $~argc (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/add (mut i32) (i32.const 0))
(global $std/pointer/sub (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 40))
(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")
(export "_setargc" (func $~setargc))
(export "Pointer<Entry>#constructor" (func $std/pointer/Pointer<Entry>#constructor|trampoline))
(export "Pointer<Entry>#get:offset" (func $std/pointer/Pointer<Entry>#get:offset))
(export "Pointer<Entry>#get:value" (func $std/pointer/Pointer<Entry>#get:value))
(export "memory" (memory $0))
(start $start)
(func $std/pointer/Pointer<Entry>#constructor (; 1 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(return
(get_local $1)
)
)
(func $std/pointer/Pointer<Entry>#constructor|trampoline (; 2 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(block $1of1
(block $0of1
(block $oob
(br_table $0of1 $1of1 $oob
(get_global $~argc)
)
)
(unreachable)
)
(set_local $1
(i32.const 0)
)
)
(call $std/pointer/Pointer<Entry>#constructor
(get_local $0)
(get_local $1)
)
)
(func $~setargc (; 3 ;) (type $iv) (param $0 i32)
(set_global $~argc
(get_local $0)
)
)
(func $std/pointer/Pointer<Entry>#get:offset (; 4 ;) (type $ii) (param $0 i32) (result i32)
(return
(get_local $0)
)
)
(func $std/pointer/Pointer<Entry>#get:value (; 5 ;) (type $ii) (param $0 i32) (result i32)
(return
(get_local $0)
)
)
(func $start (; 6 ;) (type $v)
(local $0 i32)
(local $1 i32)
(set_global $std/pointer/one
(call $std/pointer/Pointer<Entry>#constructor
(i32.const 0)
(i32.const 8)
)
)
(set_global $std/pointer/two
(call $std/pointer/Pointer<Entry>#constructor
(i32.const 0)
(i32.const 24)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 8)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 54)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
(i32.const 24)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 55)
(i32.const 0)
)
(unreachable)
)
)
(i32.store
(call $std/pointer/Pointer<Entry>#get:value
(get_global $std/pointer/one)
)
(i32.const 1)
)
(i32.store offset=4
(call $std/pointer/Pointer<Entry>#get:value
(get_global $std/pointer/one)
)
(i32.const 2)
)
(if
(i32.eqz
(i32.eq
(i32.load
(call $std/pointer/Pointer<Entry>#get:value
(get_global $std/pointer/one)
)
)
(i32.const 1)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 59)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eq
(i32.load offset=4
(call $std/pointer/Pointer<Entry>#get:value
(get_global $std/pointer/one)
)
)
(i32.const 2)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 60)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/add
(block $std/pointer/Pointer<Entry>#add|inlined.0 (result i32)
(set_local $0
(get_global $std/pointer/one)
)
(set_local $1
(get_global $std/pointer/two)
)
(br $std/pointer/Pointer<Entry>#add|inlined.0
(i32.add
(get_local $0)
(get_local $1)
)
)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/add)
)
(i32.const 32)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 63)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/sub
(block $std/pointer/Pointer<Entry>#sub|inlined.0 (result i32)
(set_local $1
(get_global $std/pointer/two)
)
(set_local $0
(get_global $std/pointer/one)
)
(br $std/pointer/Pointer<Entry>#sub|inlined.0
(i32.sub
(get_local $1)
(get_local $0)
)
)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/sub)
)
(i32.const 16)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 66)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 8)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 68)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/one
(block $std/pointer/Pointer<Entry>#inc|inlined.0 (result i32)
(set_local $0
(get_global $std/pointer/one)
)
(br $std/pointer/Pointer<Entry>#inc|inlined.0
(i32.add
(get_local $0)
(i32.const 8)
)
)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/one)
)
(i32.const 16)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 70)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
(i32.const 24)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 72)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/pointer/two
(block $std/pointer/Pointer<Entry>#dec|inlined.0 (result i32)
(set_local $0
(get_global $std/pointer/two)
)
(br $std/pointer/Pointer<Entry>#dec|inlined.0
(i32.sub
(get_local $0)
(i32.const 8)
)
)
)
)
(set_global $std/pointer/two
(block $std/pointer/Pointer<Entry>#dec|inlined.1 (result i32)
(set_local $0
(get_global $std/pointer/two)
)
(br $std/pointer/Pointer<Entry>#dec|inlined.1
(i32.sub
(get_local $0)
(i32.const 8)
)
)
)
)
(if
(i32.eqz
(i32.eq
(call $std/pointer/Pointer<Entry>#get:offset
(get_global $std/pointer/two)
)
(i32.const 8)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 75)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eq
(i32.load
(call $std/pointer/Pointer<Entry>#get:value
(get_global $std/pointer/two)
)
)
(i32.const 1)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 76)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eq
(i32.load offset=4
(call $std/pointer/Pointer<Entry>#get:value
(get_global $std/pointer/two)
)
)
(i32.const 2)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 77)
(i32.const 0)
)
(unreachable)
)
)
)
)