Heap fill/compare; Std string experiments

This commit is contained in:
dcodeIO
2017-12-21 10:14:53 +01:00
parent dd5c3e7a4e
commit 666ba54e42
17 changed files with 5108 additions and 222 deletions

24
std/assembly.d.ts vendored
View File

@ -191,10 +191,25 @@ declare class Array<T> {
/** Class representing a sequence of characters. */
declare class String {
static fromCharCode(ls: i32, hs?: i32): string;
static fromCharCodes(arr: u16[]): string;
static fromCodePoint(cp: i32): string;
static fromCodePoints(arr: i32[]): string;
readonly length: u32;
charAt(index: u32): string;
charCodeAt(index: u32): u16;
concat(other: string): string;
endsWith(other: string): bool;
indexOf(other: string): u32;
startsWith(other: string): bool;
substr(start: u32, length?: u32): string;
substring(start: u32, end?: u32): string;
trim(): string;
trimLeft(): string;
trimRight(): string;
}
/** Class for representing a runtime error. Base class of all errors. */
@ -237,6 +252,12 @@ declare class Heap {
/** Copies a chunk of memory from one location to another. */
static copy(dest: usize, src: usize, n: usize): usize;
/** Fills a chunk of memory with the specified byte value. */
static fill(dest: usize, c: u8, n: usize): usize;
/** Compares two chunks of memory. Returns `0` if equal, otherwise the difference of the first differing bytes. */
static compare(vl: usize, vr: usize, n: usize): i32;
private constructor();
}
@ -251,3 +272,6 @@ interface RegExp {}
/** Annotates an element being part of the global namespace. */
declare function global(): any;
/** Annotates a method being an operator overload. */
declare function operator(token: string): any;

View File

@ -29,7 +29,7 @@ export class Heap {
// just a big chunk of non-disposable memory for now
}
static copy(dest: usize, src: usize, n: usize): usize {
static copy(dest: usize, src: usize, n: usize): usize { // TODO: use move_memory op once available
assert(dest >= HEAP_BASE);
// the following is based on musl's implementation of memcpy
@ -174,5 +174,76 @@ export class Heap {
return dest;
}
static fill(dest: usize, c: u8, n: usize): usize { // TODO: use set_memory op once available
assert(dest >= HEAP_BASE);
// the following is based on musl's implementation of memset
if (!n) return dest;
let s: usize = dest;
// Fill head and tail with minimal branching
store<u8>(s, c); store<u8>(s + n - 1, c);
if (n <= 2) return dest;
store<u8>(s + 1, c); store<u8>(s + n - 2, c);
store<u8>(s + 2, c); store<u8>(s + n - 3, c);
if (n <= 6) return dest;
store<u8>(s + 3, c); store<u8>(s + n - 4, c);
if (n <= 8) return dest;
// Align to 4 bytes
let k: usize = -s & 3;
s += k;
n -= k;
n &= -4;
let c32: u32 = -1 / 255 * c;
// Fill head and tail in preparation of setting 32 bytes at a time
store<u32>(s, c32);
store<u32>(s + n - 4, c32);
if (n <= 8) return dest;
store<u32>(s + 4, c32);
store<u32>(s + 8, c32);
store<u32>(s + n - 12, c32);
store<u32>(s + n - 8, c32);
if (n <= 24) return dest;
store<u32>(s + 12, c32);
store<u32>(s + 16, c32);
store<u32>(s + 20, c32);
store<u32>(s + 24, c32);
store<u32>(s + n - 28, c32);
store<u32>(s + n - 24, c32);
store<u32>(s + n - 20, c32);
store<u32>(s + n - 16, c32);
// Align to 8 bytes
k = 24 + (s & 4);
s += k;
n -= k;
// Set 32 bytes at a time
let c64: u64 = <u64>c32 | (<u64>c32 << 32);
while (n >= 32) {
store<u64>(s, c64);
store<u64>(s + 8, c64);
store<u64>(s + 16, c64);
store<u64>(s + 24, c64);
n -= 32; s += 32;
}
return dest;
}
static compare(vl: usize, vr: usize, n: usize): i32 {
if (vl == vr) return 0;
// the following is based on musl's implementation of memcmp
while (n && load<u8>(vl) == load<u8>(vr)) {
n--; vl++; vr++;
}
return n ? <i32>load<u8>(vl) - <i32>load<u8>(vr) : 0;
}
private constructor() {}
}

View File

@ -1,4 +1,98 @@
@global()
export class String {
// TODO
private ptr: usize;
readonly length: u32;
constructor(ptr: usize, len: u32) {
this.ptr = ptr;
this.length = len;
}
charAt(index: u32): String {
assert(this != null && index < this.length);
return new String(this.ptr + (index << 1), 1);
}
charCodeAt(index: u32): u16 {
assert(this != null && index < this.length);
return load<u16>(this.ptr + (index << 1));
}
@operator("+")
concat(other: String): String {
assert(this != null && other != null);
const len: u32 = this.length + other.length;
const ptr: usize = Heap.allocate(len << 1);
Heap.copy(ptr, this.ptr, this.length << 1);
Heap.copy(ptr, this.ptr + (len << 1), other.length << 1);
return new String(ptr, len);
}
endsWith(other: String): bool {
assert(this != null && other != null);
if (other.length > this.length)
return false;
for (let i: u32 = this.length - other.length, j: u32 = 0, k: u32 = this.length; i < k;)
if (this.charCodeAt(i++) != other.charCodeAt(j++))
return false;
return true;
}
@operator("==")
equals(other: String): bool {
assert(this != null && other != null);
if (this.length != other.length)
return false;
for (let i: u32 = 0, k: u32 = this.length; i < k; ++i)
if (this.charCodeAt(i) != other.charCodeAt(i))
return false;
return true;
}
indexOf(other: String): u32 {
assert(this != null && other != null);
throw new Error("not implemented");
}
startsWith(other: String): bool {
assert(this != null && other != null);
if (other.length > this.length)
return false;
for (let i: u32 = 0, k: u32 = other.length; i < k; ++i)
if (this.charCodeAt(i) != other.charCodeAt(i))
return false;
return true;
}
substr(start: u32, length: u32 = <u32>-1): String {
assert(this != null);
if (start >= this.length)
return changetype<string,String>("");
const len: u32 = min<u32>(length, this.length - start);
return new String(this.ptr + (start << 1), len);
}
substring(start: u32, end: u32 = <u32>-1): String {
assert(this != null);
if (start >= this.length || end <= start)
return changetype<string,String>("");
const len: u32 = min<u32>(end - start, this.length - start);
return new String(this.ptr + (start << 1), len);
}
trim(): string {
assert(this != null);
throw new Error("not implemented");
}
trimLeft(): string {
assert(this != null);
throw new Error("not implemented");
}
trimRight(): string {
assert(this != null);
throw new Error("not implemented");
}
}