mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-13 06:51:34 +00:00
Initial element access compilation; Carefully approaching std array
This commit is contained in:
@ -10,7 +10,7 @@ class Animal<T> {
|
||||
instanceSub<T>(a: T, b: T): T { return a - b + <T>Animal.ONE; } // tsc does not allow this
|
||||
}
|
||||
|
||||
assert(sizeof<Animal<f64>>() == 7);
|
||||
assert(sizeof<Animal<f64>>() == sizeof<usize>());
|
||||
|
||||
Animal.ONE;
|
||||
Animal.add(1,2);
|
||||
|
@ -145,8 +145,8 @@
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(i32.const 7)
|
||||
(i32.const 7)
|
||||
(i32.const 4)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
|
2918
tests/compiler/std/array.optimized-inlined.wast
Normal file
2918
tests/compiler/std/array.optimized-inlined.wast
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1 +1,75 @@
|
||||
// Array.fromPtr<i32>(1);
|
||||
var arr = changetype<i32[]>(Heap.allocate(sizeof<usize>() + 2 * sizeof<i32>()));
|
||||
|
||||
assert(arr.length == 0);
|
||||
assert(arr.__capacity == 0);
|
||||
|
||||
arr.push(42);
|
||||
|
||||
assert(arr[0] == 42);
|
||||
assert(arr.length == 1);
|
||||
assert(arr.__capacity == 1);
|
||||
|
||||
var i = arr.pop();
|
||||
|
||||
assert(i == 42);
|
||||
assert(arr.length == 0);
|
||||
assert(arr.__capacity == 1);
|
||||
|
||||
arr.push(43);
|
||||
|
||||
assert(arr.length == 1);
|
||||
assert(arr.__capacity == 1);
|
||||
assert(arr[0] == 43);
|
||||
|
||||
arr.push(44);
|
||||
|
||||
assert(arr.length == 2);
|
||||
assert(arr.__capacity == 2);
|
||||
assert(arr[0] == 43);
|
||||
assert(arr[1] == 44);
|
||||
|
||||
arr.push(45);
|
||||
|
||||
assert(arr.length == 3);
|
||||
assert(arr.__capacity == 4);
|
||||
assert(arr[0] == 43);
|
||||
assert(arr[1] == 44);
|
||||
assert(arr[2] == 45);
|
||||
|
||||
arr.unshift(42); // see FIXME in std:array
|
||||
|
||||
assert(arr.length == 4);
|
||||
assert(arr.__capacity == 4);
|
||||
assert(arr[0] == 42);
|
||||
assert(arr[1] == 43);
|
||||
assert(arr[2] == 44);
|
||||
assert(arr[3] == 45);
|
||||
|
||||
arr.unshift(41);
|
||||
|
||||
assert(arr.length == 5);
|
||||
assert(arr.__capacity == 8);
|
||||
assert(arr[0] == 41);
|
||||
assert(arr[1] == 42);
|
||||
assert(arr[2] == 43);
|
||||
assert(arr[3] == 44);
|
||||
assert(arr[4] == 45);
|
||||
|
||||
i = arr.shift();
|
||||
|
||||
assert(i == 41);
|
||||
assert(arr.length == 4);
|
||||
assert(arr.__capacity == 8);
|
||||
assert(arr[0] == 42);
|
||||
assert(arr[1] == 43);
|
||||
assert(arr[2] == 44);
|
||||
assert(arr[3] == 45);
|
||||
|
||||
i = arr.pop();
|
||||
|
||||
assert(i == 45);
|
||||
assert(arr.length == 3);
|
||||
assert(arr.__capacity == 8);
|
||||
assert(arr[0] == 42);
|
||||
assert(arr[1] == 43);
|
||||
assert(arr[2] == 44);
|
||||
|
File diff suppressed because it is too large
Load Diff
157
tests/compiler/std/carray.optimized.wast
Normal file
157
tests/compiler/std/carray.optimized.wast
Normal file
@ -0,0 +1,157 @@
|
||||
(module
|
||||
(type $iii (func (param i32 i32) (result i32)))
|
||||
(type $iiiv (func (param i32 i32 i32)))
|
||||
(type $v (func))
|
||||
(global $std/carray/arr (mut i32) (i32.const 0))
|
||||
(global $HEAP_BASE i32 (i32.const 4))
|
||||
(memory $0 1)
|
||||
(export "memory" (memory $0))
|
||||
(start $start)
|
||||
(func $std:array/CArray#__get (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(i32.mul
|
||||
(get_local $1)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $std:array/CArray#__set (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
|
||||
(i32.store
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(i32.mul
|
||||
(get_local $1)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(get_local $2)
|
||||
)
|
||||
)
|
||||
(func $start (; 2 ;) (type $v)
|
||||
(local $0 i32)
|
||||
(set_global $std/carray/arr
|
||||
(get_global $HEAP_BASE)
|
||||
)
|
||||
(if
|
||||
(i32.load
|
||||
(get_global $HEAP_BASE)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_global $HEAP_BASE)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 1)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(call $std:array/CArray#__set
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 0)
|
||||
(i32.const 42)
|
||||
)
|
||||
(call $std:array/CArray#__set
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 1)
|
||||
(i32.const 24)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(i32.load
|
||||
(get_global $HEAP_BASE)
|
||||
)
|
||||
(i32.const 42)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_global $HEAP_BASE)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 42)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(block (result i32)
|
||||
(call $std:array/CArray#__set
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 3)
|
||||
(tee_local $0
|
||||
(i32.const 9000)
|
||||
)
|
||||
)
|
||||
(i32.ne
|
||||
(get_local $0)
|
||||
(i32.const 9000)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_global $HEAP_BASE)
|
||||
(i32.const 12)
|
||||
)
|
||||
)
|
||||
(i32.const 9000)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 3)
|
||||
)
|
||||
(i32.const 9000)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
)
|
26
tests/compiler/std/carray.ts
Normal file
26
tests/compiler/std/carray.ts
Normal file
@ -0,0 +1,26 @@
|
||||
// TBD: While this is useful as long as the pointer is just a local or global,
|
||||
// things go haywire, compared to C, as soon as the CArray is a member of a
|
||||
// class. Also, multi dimensional arrays cannot be implemented C-like because
|
||||
// their length isn't known at compile time.
|
||||
|
||||
var arr: CArray<i32> = changetype<CArray<i32>>(HEAP_BASE);
|
||||
|
||||
assert(load<i32>(HEAP_BASE) == 0);
|
||||
assert(load<i32>(HEAP_BASE + 4) == 0);
|
||||
|
||||
assert(arr[0] == 0);
|
||||
assert(arr[1] == 0);
|
||||
|
||||
arr[0] = 42;
|
||||
arr[1] = 24;
|
||||
|
||||
assert(load<i32>(HEAP_BASE) == 42);
|
||||
assert(load<i32>(HEAP_BASE + 4) == 24);
|
||||
|
||||
assert(arr[0] == 42);
|
||||
assert(arr[1] == 24);
|
||||
|
||||
assert((arr[3] = 9000) == 9000);
|
||||
|
||||
assert(load<i32>(HEAP_BASE + 12) == 9000);
|
||||
assert(arr[3] == 9000);
|
287
tests/compiler/std/carray.wast
Normal file
287
tests/compiler/std/carray.wast
Normal file
@ -0,0 +1,287 @@
|
||||
(module
|
||||
(type $iii (func (param i32 i32) (result i32)))
|
||||
(type $iiiv (func (param i32 i32 i32)))
|
||||
(type $v (func))
|
||||
(global $std/carray/arr (mut i32) (i32.const 0))
|
||||
(global $HEAP_BASE i32 (i32.const 4))
|
||||
(memory $0 1)
|
||||
(export "memory" (memory $0))
|
||||
(start $start)
|
||||
(func $std:array/CArray#__get (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(return
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(i32.mul
|
||||
(get_local $1)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $std:array/CArray#__set (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
|
||||
(i32.store
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(i32.mul
|
||||
(get_local $1)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(get_local $2)
|
||||
)
|
||||
)
|
||||
(func $start (; 2 ;) (type $v)
|
||||
(local $0 i32)
|
||||
(set_global $std/carray/arr
|
||||
(get_global $HEAP_BASE)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(i32.load
|
||||
(get_global $HEAP_BASE)
|
||||
)
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_global $HEAP_BASE)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(call $std:array/CArray#__set
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 0)
|
||||
(i32.const 42)
|
||||
)
|
||||
(call $std:array/CArray#__set
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 1)
|
||||
(i32.const 24)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(i32.load
|
||||
(get_global $HEAP_BASE)
|
||||
)
|
||||
(i32.const 42)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_global $HEAP_BASE)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 42)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(block (result i32)
|
||||
(call $std:array/CArray#__set
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 3)
|
||||
(tee_local $0
|
||||
(i32.const 9000)
|
||||
)
|
||||
)
|
||||
(get_local $0)
|
||||
)
|
||||
(i32.const 9000)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(i32.load
|
||||
(i32.add
|
||||
(get_global $HEAP_BASE)
|
||||
(i32.const 12)
|
||||
)
|
||||
)
|
||||
(i32.const 9000)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call $std:array/CArray#__get
|
||||
(get_global $std/carray/arr)
|
||||
(i32.const 3)
|
||||
)
|
||||
(i32.const 9000)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
)
|
||||
(;
|
||||
[program.elements]
|
||||
GLOBAL: NaN
|
||||
GLOBAL: Infinity
|
||||
FUNCTION_PROTOTYPE: isNaN
|
||||
FUNCTION_PROTOTYPE: isFinite
|
||||
FUNCTION_PROTOTYPE: clz
|
||||
FUNCTION_PROTOTYPE: ctz
|
||||
FUNCTION_PROTOTYPE: popcnt
|
||||
FUNCTION_PROTOTYPE: rotl
|
||||
FUNCTION_PROTOTYPE: rotr
|
||||
FUNCTION_PROTOTYPE: abs
|
||||
FUNCTION_PROTOTYPE: max
|
||||
FUNCTION_PROTOTYPE: min
|
||||
FUNCTION_PROTOTYPE: ceil
|
||||
FUNCTION_PROTOTYPE: floor
|
||||
FUNCTION_PROTOTYPE: copysign
|
||||
FUNCTION_PROTOTYPE: nearest
|
||||
FUNCTION_PROTOTYPE: reinterpret
|
||||
FUNCTION_PROTOTYPE: sqrt
|
||||
FUNCTION_PROTOTYPE: trunc
|
||||
FUNCTION_PROTOTYPE: load
|
||||
FUNCTION_PROTOTYPE: store
|
||||
FUNCTION_PROTOTYPE: sizeof
|
||||
FUNCTION_PROTOTYPE: select
|
||||
FUNCTION_PROTOTYPE: unreachable
|
||||
FUNCTION_PROTOTYPE: current_memory
|
||||
FUNCTION_PROTOTYPE: grow_memory
|
||||
FUNCTION_PROTOTYPE: changetype
|
||||
FUNCTION_PROTOTYPE: assert
|
||||
FUNCTION_PROTOTYPE: i8
|
||||
FUNCTION_PROTOTYPE: i16
|
||||
FUNCTION_PROTOTYPE: i32
|
||||
FUNCTION_PROTOTYPE: i64
|
||||
FUNCTION_PROTOTYPE: u8
|
||||
FUNCTION_PROTOTYPE: u16
|
||||
FUNCTION_PROTOTYPE: u32
|
||||
FUNCTION_PROTOTYPE: u64
|
||||
FUNCTION_PROTOTYPE: bool
|
||||
FUNCTION_PROTOTYPE: f32
|
||||
FUNCTION_PROTOTYPE: f64
|
||||
FUNCTION_PROTOTYPE: isize
|
||||
FUNCTION_PROTOTYPE: usize
|
||||
GLOBAL: HEAP_BASE
|
||||
CLASS_PROTOTYPE: std:array/Array
|
||||
CLASS_PROTOTYPE: Array
|
||||
CLASS_PROTOTYPE: std:array/CArray
|
||||
CLASS_PROTOTYPE: CArray
|
||||
CLASS_PROTOTYPE: std:error/Error
|
||||
CLASS_PROTOTYPE: Error
|
||||
CLASS_PROTOTYPE: std:error/RangeError
|
||||
CLASS_PROTOTYPE: RangeError
|
||||
GLOBAL: std:heap/ALIGN_LOG2
|
||||
GLOBAL: std:heap/ALIGN_SIZE
|
||||
GLOBAL: std:heap/ALIGN_MASK
|
||||
GLOBAL: std:heap/HEAP_OFFSET
|
||||
CLASS_PROTOTYPE: std:heap/Heap
|
||||
CLASS_PROTOTYPE: Heap
|
||||
PROPERTY: std:heap/Heap.used
|
||||
PROPERTY: std:heap/Heap.free
|
||||
PROPERTY: std:heap/Heap.size
|
||||
FUNCTION_PROTOTYPE: std:heap/Heap.allocate
|
||||
FUNCTION_PROTOTYPE: std:heap/Heap.dispose
|
||||
FUNCTION_PROTOTYPE: std:heap/Heap.copy
|
||||
FUNCTION_PROTOTYPE: std:heap/Heap.fill
|
||||
FUNCTION_PROTOTYPE: std:heap/Heap.compare
|
||||
CLASS_PROTOTYPE: std:map/Map
|
||||
CLASS_PROTOTYPE: Map
|
||||
CLASS_PROTOTYPE: std:regexp/RegExp
|
||||
CLASS_PROTOTYPE: RegExp
|
||||
CLASS_PROTOTYPE: std:set/Set
|
||||
CLASS_PROTOTYPE: Set
|
||||
GLOBAL: std:string/EMPTY
|
||||
CLASS_PROTOTYPE: std:string/String
|
||||
CLASS_PROTOTYPE: String
|
||||
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
|
||||
FUNCTION_PROTOTYPE: std:string/parseInt
|
||||
FUNCTION_PROTOTYPE: parseInt
|
||||
FUNCTION_PROTOTYPE: std:string/parseFloat
|
||||
FUNCTION_PROTOTYPE: parseFloat
|
||||
GLOBAL: std/carray/arr
|
||||
[program.exports]
|
||||
CLASS_PROTOTYPE: std:array/Array
|
||||
CLASS_PROTOTYPE: std:array/CArray
|
||||
CLASS_PROTOTYPE: std:error/Error
|
||||
CLASS_PROTOTYPE: std:error/RangeError
|
||||
CLASS_PROTOTYPE: std:heap/Heap
|
||||
CLASS_PROTOTYPE: std:map/Map
|
||||
CLASS_PROTOTYPE: std:regexp/RegExp
|
||||
CLASS_PROTOTYPE: std:set/Set
|
||||
CLASS_PROTOTYPE: std:string/String
|
||||
FUNCTION_PROTOTYPE: std:string/parseInt
|
||||
FUNCTION_PROTOTYPE: std:string/parseFloat
|
||||
;)
|
Reference in New Issue
Block a user