rt hooks all over the place

still some work to do on optimizing away retain/release calls, but this looks promising
This commit is contained in:
dcode
2019-05-15 21:17:41 +02:00
parent cb09edf677
commit f73d807d5a
99 changed files with 8237 additions and 18460 deletions

View File

@ -157,7 +157,7 @@ export declare function offsetof<T>(fieldName?: string): usize; // | u32 / u64
export declare function select<T>(ifTrue: T, ifFalse: T, condition: bool): T;
// @ts-ignore: decorator
@builtin
@unsafe @builtin
export declare function unreachable(): void;
// @ts-ignore: decorator

View File

@ -988,14 +988,8 @@ declare namespace memory {
export function init(segmentIndex: u32, srcOffset: usize, dstOffset: usize, n: usize): void;
/** Prevents further use of a passive element segment. */
export function drop(segmentIndex: u32): void;
/** Copies elements from one region of a table to another region. */
export function allocate(size: usize): usize;
/** Disposes a chunk of memory by its pointer. */
export function free(ptr: usize): void;
/** Compares two chunks of memory. Returns `0` if equal, otherwise the difference of the first differing bytes. */
export function compare(vl: usize, vr: usize, n: usize): i32;
/** Resets the allocator to its initial state, if supported. */
export function reset(): void;
}
/** Garbage collector operations. */

View File

@ -1,3 +1,3 @@
export { __alloc, __realloc, __free } from "./tlsf";
export { __retain, __release, __collect } from "./purerc";
export { __instanceof, __typeinfo } from "./common";
export { __alloc, __realloc, __free } from "rt/tlsf";
export { __retain, __release, __collect } from "rt/purerc";
export { __instanceof, __typeinfo } from "rt/common";

View File

@ -154,7 +154,7 @@ function growRoots(): void {
var oldRoots = ROOTS;
var oldSize = CUR - oldRoots;
var newSize = max(oldSize * 2, 64 << alignof<usize>());
var newRoots = memory.allocate(newSize);
var newRoots = __alloc(newSize, 0);
memory.copy(newRoots, oldRoots, oldSize);
ROOTS = newRoots;
CUR = newRoots + oldSize;

View File

@ -476,8 +476,8 @@ export function allocateBlock(root: Root, size: usize): Block {
if (DEBUG) assert(block); // must be found now
}
if (DEBUG) assert((block.mmInfo & ~TAGS_MASK) >= payloadSize); // must fit
block.gcInfo = 0;
block.rtId = 0; // not determined yet
block.gcInfo = 1; // RC=1
// block.rtId = 0; // set by the caller (__alloc)
block.rtSize = size;
removeBlock(root, <Block>block);
prepareBlock(root, <Block>block, payloadSize);

View File

@ -439,7 +439,7 @@ import { idof } from "./builtins";
static fromUTF8(ptr: usize, len: usize): String {
if (len < 1) return changetype<String>("");
var ptrPos = <usize>0;
var buf = memory.allocate(<usize>len << 1);
var buf = __alloc(<usize>len << 1, 0);
var bufPos = <usize>0;
while (ptrPos < len) {
let cp = <u32>load<u8>(ptr + ptrPos++);
@ -475,12 +475,12 @@ import { idof } from "./builtins";
assert(ptrPos == len);
var out = __alloc(bufPos, idof<String>());
memory.copy(out, buf, bufPos);
memory.free(buf);
__free(buf);
return changetype<String>(out); // retains
}
toUTF8(): usize {
var buf = memory.allocate(<usize>this.lengthUTF8);
var buf = __alloc(<usize>this.lengthUTF8, 0);
var pos: usize = 0;
var end = <usize>this.length;
var off: usize = 0;

View File

@ -267,7 +267,7 @@ export function utoa32(value: u32): String {
var decimals = decimalCount32(value);
var out = __alloc(decimals << 1, idof<String>());
utoa32_core(changetype<usize>(out), value, decimals);
utoa32_core(out, value, decimals);
return changetype<String>(out); // retains
}
@ -280,9 +280,8 @@ export function itoa32(value: i32): String {
var decimals = decimalCount32(value) + u32(sign);
var out = __alloc(decimals << 1, idof<String>());
utoa32_core(changetype<usize>(out), value, decimals);
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
utoa32_core(out, value, decimals);
if (sign) store<u16>(out, CharCode.MINUS);
return changetype<String>(out); // retains
}
@ -298,7 +297,7 @@ export function utoa64(value: u64): String {
} else {
let decimals = decimalCount64(value);
out = __alloc(decimals << 1, idof<String>());
utoa64_core(changetype<usize>(out), value, decimals);
utoa64_core(out, value, decimals);
}
return changetype<String>(out); // retains
}
@ -314,13 +313,13 @@ export function itoa64(value: i64): String {
let val32 = <u32>value;
let decimals = decimalCount32(val32) + u32(sign);
out = __alloc(decimals << 1, idof<String>());
utoa32_core(changetype<usize>(out), val32, decimals);
utoa32_core(out, val32, decimals);
} else {
let decimals = decimalCount64(value) + u32(sign);
out = __alloc(decimals << 1, idof<String>());
utoa64_core(changetype<usize>(out), value, decimals);
utoa64_core(out, value, decimals);
}
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
if (sign) store<u16>(out, CharCode.MINUS);
return changetype<String>(out); // retains
}
@ -627,12 +626,12 @@ export function dtoa(value: f64): String {
if (isNaN<f64>(value)) return "NaN";
return select<String>("-Infinity", "Infinity", value < 0);
}
var temp = __alloc(MAX_DOUBLE_LENGTH << 1, idof<String>());
var length = dtoa_core(temp, value);
if (length < MAX_DOUBLE_LENGTH) {
return changetype<String>(temp).substring(0, length); // retains/releases `temp`, retains return
}
return changetype<String>(temp); // retains
var buffer = __alloc(MAX_DOUBLE_LENGTH << 1, idof<String>());
var length = dtoa_core(buffer, value);
if (length == MAX_DOUBLE_LENGTH) return changetype<String>(buffer);
var result = changetype<String>(buffer).substring(0, length);
__free(buffer);
return result;
}
export function itoa_stream<T extends number>(buffer: usize, offset: usize, value: T): u32 {

View File

@ -87,7 +87,7 @@ function weakHeapSort<T>(
const shift32 = alignof<u32>();
var bitsetSize = (length + 31) >> 5 << shift32;
var bitset = memory.allocate(bitsetSize); // indexed in 32-bit chunks below
var bitset = __alloc(bitsetSize, 0); // indexed in 32-bit chunks below
memory.fill(bitset, 0, bitsetSize);
// see: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.21.1863&rep=rep1&type=pdf
@ -133,7 +133,7 @@ function weakHeapSort<T>(
}
}
memory.free(bitset);
__free(bitset);
var t: T = load<T>(dataStart, sizeof<T>()); // t = arr[1]
store<T>(dataStart, load<T>(dataStart), sizeof<T>()); // arr[1] = arr[0]