diff --git a/src/ast.ts b/src/ast.ts index b06912c1..ca184092 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -687,6 +687,9 @@ export abstract class Node { range.source.normalizedPath ); } else { // absolute + if (!normalizedPath.startsWith(LIBRARY_PREFIX)) { + normalizedPath = LIBRARY_PREFIX + normalizedPath; + } stmt.normalizedPath = normalizedPath; } stmt.internalPath = mangleInternalPath(stmt.normalizedPath); @@ -782,10 +785,18 @@ export abstract class Node { stmt.declarations = null; stmt.namespaceName = identifier; stmt.path = path; - stmt.normalizedPath = resolvePath( - normalizePath(path.value), - range.source.normalizedPath - ); + var normalizedPath = normalizePath(path.value); + if (path.value.startsWith(".")) { + stmt.normalizedPath = resolvePath( + normalizedPath, + range.source.normalizedPath + ); + } else { + if (!normalizedPath.startsWith(LIBRARY_PREFIX)) { + normalizedPath = LIBRARY_PREFIX + normalizedPath; + } + stmt.normalizedPath = normalizedPath; + } stmt.internalPath = mangleInternalPath(stmt.normalizedPath); return stmt; } diff --git a/src/compiler.ts b/src/compiler.ts index 7d2b0f59..81a74e77 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -716,7 +716,12 @@ export class Compiler extends DiagnosticEmitter { var exports = file.exports; if (exports) for (let element of exports.values()) this.compileElement(element); var exportsStar = file.exportsStar; - if (exportsStar) for (let exportStar of exportsStar) this.compileFile(exportStar); + if (exportsStar) { + for (let exportStar of exportsStar) { + this.compileFile(exportStar); + this.compileExports(exportStar); + } + } } // files diff --git a/std/assembly/rt/index.ts b/std/assembly/rt/index.ts index a37cf887..f9baadc7 100644 --- a/std/assembly/rt/index.ts +++ b/std/assembly/rt/index.ts @@ -6,7 +6,7 @@ import { ROOT, Block, BLOCK_OVERHEAD, initializeRoot, allocateBlock, reallocateB // @ts-ignore: decorator @global @unsafe -function __rt_allocate(size: usize, id: u32): usize { +export function __rt_allocate(size: usize, id: u32): usize { var root = ROOT; if (!root) { initializeRoot(); @@ -19,7 +19,7 @@ function __rt_allocate(size: usize, id: u32): usize { // @ts-ignore: decorator @global @unsafe -function __rt_reallocate(ref: usize, size: usize): usize { +export function __rt_reallocate(ref: usize, size: usize): usize { if (DEBUG) assert(ROOT); // must be initialized assert(ref != 0 && !(ref & AL_MASK)); // must exist and be aligned return changetype(reallocateBlock(ROOT, changetype(ref - BLOCK_OVERHEAD), size)) + BLOCK_OVERHEAD; @@ -27,7 +27,7 @@ function __rt_reallocate(ref: usize, size: usize): usize { // @ts-ignore: decorator @global @unsafe -function __rt_free(ref: usize): void { +export function __rt_free(ref: usize): void { if (DEBUG) assert(ROOT); // must be initialized assert(ref != 0 && !(ref & AL_MASK)); // must exist and be aligned freeBlock(ROOT, changetype(ref - BLOCK_OVERHEAD)); @@ -39,18 +39,20 @@ import { increment, decrement, collectCycles } from "./pure"; // @ts-ignore: decorator @global @unsafe -function __rt_retain(ref: usize): void { +export function __rt_retain(ref: usize): void { if (ref) increment(changetype(ref - BLOCK_OVERHEAD)); } // @ts-ignore: decorator @global @unsafe -function __rt_release(ref: usize): void { +export function __rt_release(ref: usize): void { if (ref) decrement(changetype(ref - BLOCK_OVERHEAD)); } // @ts-ignore: decorator @global @unsafe -function __rt_collect(): void { +export function __rt_collect(): void { collectCycles(); } + +export { __rt_typeinfo }; diff --git a/std/assembly/rt/stub.ts b/std/assembly/rt/stub.ts index 386a202d..0244d6cd 100644 --- a/std/assembly/rt/stub.ts +++ b/std/assembly/rt/stub.ts @@ -20,7 +20,7 @@ var offset: usize = startOffset; // @ts-ignore: decorator @unsafe @global -function __rt_allocate(size: usize, id: u32): usize { +export function __rt_allocate(size: usize, id: u32): usize { if (size > BLOCK_MAXSIZE) unreachable(); var ptr = offset + BLOCK_OVERHEAD; var newPtr = (ptr + max(size, 1) + AL_MASK) & ~AL_MASK; @@ -41,7 +41,7 @@ function __rt_allocate(size: usize, id: u32): usize { // @ts-ignore: decorator @unsafe @global -function __rt_reallocate(ref: usize, size: usize): usize { +export function __rt_reallocate(ref: usize, size: usize): usize { var block = changetype(ref - BLOCK_OVERHEAD); var oldSize = block.rtSize; if (size > oldSize) { @@ -56,12 +56,12 @@ function __rt_reallocate(ref: usize, size: usize): usize { // @ts-ignore: decorator @unsafe @global -function __rt_free(ref: usize): void { +export function __rt_free(ref: usize): void { } // @ts-ignore: decorator @unsafe @global -function __rt_reset(): void { // special +export function __rt_reset(): void { // special offset = startOffset; } @@ -69,15 +69,17 @@ function __rt_reset(): void { // special // @ts-ignore: decorator @global @unsafe -function __rt_retain(ref: usize): void { +export function __rt_retain(ref: usize): void { } // @ts-ignore: decorator @global @unsafe -function __rt_release(ref: usize): void { +export function __rt_release(ref: usize): void { } // @ts-ignore: decorator @global @unsafe -function __rt_collect(): void { +export function __rt_collect(): void { } + +export { __rt_typeinfo }; diff --git a/std/assembly/rt/tlsf.ts b/std/assembly/rt/tlsf.ts index 518908a2..6fecf19d 100644 --- a/std/assembly/rt/tlsf.ts +++ b/std/assembly/rt/tlsf.ts @@ -146,37 +146,60 @@ import { AL_BITS, AL_SIZE, AL_MASK, DEBUG, CommonBlock } from "./common"; /** Gets the second level map of the specified first level. */ // @ts-ignore: decorator @inline function GETSL(root: Root, fl: usize): u32 { - return load(changetype(root) + (fl << alignof()), SL_START); + return load( + changetype(root) + (fl << alignof()), + SL_START + ); } /** Sets the second level map of the specified first level. */ // @ts-ignore: decorator @inline function SETSL(root: Root, fl: usize, slMap: u32): void { - store(changetype(root) + (fl << alignof()), slMap, SL_START); + store( + changetype(root) + (fl << alignof()), + slMap, + SL_START + ); } /** Gets the head of the free list for the specified combination of first and second level. */ // @ts-ignore: decorator @inline function GETHEAD(root: Root, fl: usize, sl: u32): Block | null { - return changetype(load(changetype(root) + (fl * SL_SIZE + sl) * sizeof(), HL_START)); + return changetype( + load( + changetype(root) + (((fl << SL_BITS) + sl) << alignof()), + HL_START + ) + ); } /** Sets the head of the free list for the specified combination of first and second level. */ // @ts-ignore: decorator @inline function SETHEAD(root: Root, fl: usize, sl: u32, head: Block | null): void { - store(changetype(root) + (fl * SL_SIZE + sl) * sizeof() , changetype(head), HL_START); + store( + changetype(root) + (((fl << SL_BITS) + sl) << alignof()), + changetype(head), + HL_START + ); } /** Gets the tail block.. */ // @ts-ignore: decorator @inline function GETTAIL(root: Root): Block { - return load(changetype(root), HL_END); + return load( + changetype(root), + HL_END + ); } /** Sets the tail block. */ // @ts-ignore: decorator @inline function SETTAIL(root: Root, tail: Block): void { - store(changetype(root), tail, HL_END); + store( + changetype(root), + tail, + HL_END + ); } /** Inserts a previously used block back into the free list. */ @@ -229,7 +252,7 @@ function insertBlock(root: Root, block: Block): void { var fl: usize, sl: u32; if (size < SB_SIZE) { fl = 0; - sl = (size / AL_SIZE); + sl = (size >> AL_BITS); } else { const inv: usize = sizeof() * 8 - 1; fl = inv - clz(size); @@ -261,7 +284,7 @@ function removeBlock(root: Root, block: Block): void { var fl: usize, sl: u32; if (size < SB_SIZE) { fl = 0; - sl = (size / AL_SIZE); + sl = (size >> AL_BITS); } else { const inv: usize = sizeof() * 8 - 1; fl = inv - clz(size); @@ -302,7 +325,7 @@ function searchBlock(root: Root, size: usize): Block | null { var fl: usize, sl: u32; if (size < SB_SIZE) { fl = 0; - sl = (size / AL_SIZE); + sl = (size >> AL_BITS); } else { const halfMaxSize = BLOCK_MAXSIZE >> 1; // don't round last fl const inv: usize = sizeof() * 8 - 1; diff --git a/tests/allocators/rt/optimized.wat b/tests/allocators/rt/optimized.wat index 7b5eb5ce..be09955c 100644 --- a/tests/allocators/rt/optimized.wat +++ b/tests/allocators/rt/optimized.wat @@ -29,8 +29,8 @@ i32.lt_u if (result i32) local.get $2 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $4 i32.const 0 else @@ -248,8 +248,8 @@ i32.lt_u if (result i32) local.get $2 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $2 i32.const 0 else @@ -497,7 +497,7 @@ if i32.const 0 i32.const 24 - i32.const 427 + i32.const 450 i32.const 29 call $~lib/builtins/abort unreachable @@ -523,8 +523,8 @@ i32.lt_u if (result i32) local.get $1 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $1 i32.const 0 else diff --git a/tests/allocators/rt/untouched.wat b/tests/allocators/rt/untouched.wat index 1667756b..8e5acb2d 100644 --- a/tests/allocators/rt/untouched.wat +++ b/tests/allocators/rt/untouched.wat @@ -41,7 +41,7 @@ if i32.const 0 i32.const 24 - i32.const 256 + i32.const 279 i32.const 13 call $~lib/builtins/abort unreachable @@ -66,7 +66,7 @@ if i32.const 0 i32.const 24 - i32.const 258 + i32.const 281 i32.const 13 call $~lib/builtins/abort unreachable @@ -78,8 +78,8 @@ i32.const 0 local.set $4 local.get $3 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $5 else i32.const 31 @@ -118,7 +118,7 @@ if i32.const 0 i32.const 24 - i32.const 271 + i32.const 294 i32.const 13 call $~lib/builtins/abort unreachable @@ -151,12 +151,12 @@ local.set $8 local.get $10 local.get $9 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $8 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -173,12 +173,12 @@ local.set $8 local.get $11 local.get $10 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $9 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add local.get $8 i32.store offset=96 @@ -256,7 +256,7 @@ if i32.const 0 i32.const 24 - i32.const 184 + i32.const 207 i32.const 13 call $~lib/builtins/abort unreachable @@ -271,7 +271,7 @@ if i32.const 0 i32.const 24 - i32.const 186 + i32.const 209 i32.const 13 call $~lib/builtins/abort unreachable @@ -370,7 +370,7 @@ if i32.const 0 i32.const 24 - i32.const 207 + i32.const 230 i32.const 15 call $~lib/builtins/abort unreachable @@ -433,7 +433,7 @@ if i32.const 0 i32.const 24 - i32.const 222 + i32.const 245 i32.const 13 call $~lib/builtins/abort unreachable @@ -449,7 +449,7 @@ if i32.const 0 i32.const 24 - i32.const 223 + i32.const 246 i32.const 13 call $~lib/builtins/abort unreachable @@ -466,8 +466,8 @@ i32.const 0 local.set $9 local.get $8 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $10 else i32.const 31 @@ -506,7 +506,7 @@ if i32.const 0 i32.const 24 - i32.const 239 + i32.const 262 i32.const 13 call $~lib/builtins/abort unreachable @@ -520,12 +520,12 @@ local.set $7 local.get $3 local.get $6 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $7 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -553,12 +553,12 @@ local.set $7 local.get $12 local.get $3 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $6 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add local.get $7 i32.store offset=96 @@ -629,7 +629,7 @@ if i32.const 0 i32.const 24 - i32.const 365 + i32.const 388 i32.const 4 call $~lib/builtins/abort unreachable @@ -654,7 +654,7 @@ if i32.const 0 i32.const 24 - i32.const 375 + i32.const 398 i32.const 15 call $~lib/builtins/abort unreachable @@ -685,7 +685,7 @@ if i32.const 0 i32.const 24 - i32.const 387 + i32.const 410 i32.const 4 call $~lib/builtins/abort unreachable @@ -862,12 +862,12 @@ local.set $6 local.get $9 local.get $8 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $7 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add local.get $6 i32.store offset=96 @@ -918,7 +918,7 @@ if i32.const 0 i32.const 24 - i32.const 427 + i32.const 450 i32.const 29 call $~lib/builtins/abort unreachable @@ -954,8 +954,8 @@ i32.const 0 local.set $2 local.get $1 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $3 else local.get $1 @@ -1012,7 +1012,7 @@ if i32.const 0 i32.const 24 - i32.const 317 + i32.const 340 i32.const 13 call $~lib/builtins/abort unreachable @@ -1077,7 +1077,7 @@ if i32.const 0 i32.const 24 - i32.const 330 + i32.const 353 i32.const 17 call $~lib/builtins/abort unreachable @@ -1092,12 +1092,12 @@ local.set $5 local.get $9 local.get $8 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $5 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -1114,12 +1114,12 @@ local.set $4 local.get $8 local.get $5 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $4 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -1195,7 +1195,7 @@ if i32.const 0 i32.const 24 - i32.const 344 + i32.const 367 i32.const 13 call $~lib/builtins/abort unreachable @@ -1306,7 +1306,7 @@ if i32.const 0 i32.const 24 - i32.const 457 + i32.const 480 i32.const 15 call $~lib/builtins/abort unreachable @@ -1324,7 +1324,7 @@ if i32.const 0 i32.const 24 - i32.const 459 + i32.const 482 i32.const 13 call $~lib/builtins/abort unreachable @@ -1388,7 +1388,7 @@ if i32.const 0 i32.const 24 - i32.const 510 + i32.const 533 i32.const 2 call $~lib/builtins/abort unreachable diff --git a/tests/runtime/assembly/index.ts b/tests/runtime/assembly/index.ts index 18f2db53..018c10d9 100644 --- a/tests/runtime/assembly/index.ts +++ b/tests/runtime/assembly/index.ts @@ -1,13 +1,3 @@ -import "rt"; - -export { - __rt_allocate, - __rt_reallocate, - __rt_free, - __rt_retain, - __rt_release, - __rt_collect, - __rt_typeinfo -}; +export * from "rt"; @start export function main(): void {} diff --git a/tests/runtime/optimized.wat b/tests/runtime/optimized.wat index fb233542..2c4599d2 100644 --- a/tests/runtime/optimized.wat +++ b/tests/runtime/optimized.wat @@ -49,8 +49,8 @@ i32.lt_u if (result i32) local.get $2 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $4 i32.const 0 else @@ -268,8 +268,8 @@ i32.lt_u if (result i32) local.get $2 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $2 i32.const 0 else @@ -517,7 +517,7 @@ if i32.const 0 i32.const 24 - i32.const 427 + i32.const 450 i32.const 29 call $~lib/builtins/abort unreachable @@ -543,8 +543,8 @@ i32.lt_u if (result i32) local.get $1 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $1 i32.const 0 else diff --git a/tests/runtime/untouched.wasm b/tests/runtime/untouched.wasm index e6bc7cdd..0272a2a4 100644 Binary files a/tests/runtime/untouched.wasm and b/tests/runtime/untouched.wasm differ diff --git a/tests/runtime/untouched.wat b/tests/runtime/untouched.wat index 267a1056..0ca5d74d 100644 --- a/tests/runtime/untouched.wat +++ b/tests/runtime/untouched.wat @@ -63,7 +63,7 @@ if i32.const 0 i32.const 24 - i32.const 256 + i32.const 279 i32.const 13 call $~lib/builtins/abort unreachable @@ -88,7 +88,7 @@ if i32.const 0 i32.const 24 - i32.const 258 + i32.const 281 i32.const 13 call $~lib/builtins/abort unreachable @@ -100,8 +100,8 @@ i32.const 0 local.set $4 local.get $3 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $5 else i32.const 31 @@ -140,7 +140,7 @@ if i32.const 0 i32.const 24 - i32.const 271 + i32.const 294 i32.const 13 call $~lib/builtins/abort unreachable @@ -173,12 +173,12 @@ local.set $8 local.get $10 local.get $9 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $8 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -195,12 +195,12 @@ local.set $8 local.get $11 local.get $10 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $9 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add local.get $8 i32.store offset=96 @@ -278,7 +278,7 @@ if i32.const 0 i32.const 24 - i32.const 184 + i32.const 207 i32.const 13 call $~lib/builtins/abort unreachable @@ -293,7 +293,7 @@ if i32.const 0 i32.const 24 - i32.const 186 + i32.const 209 i32.const 13 call $~lib/builtins/abort unreachable @@ -392,7 +392,7 @@ if i32.const 0 i32.const 24 - i32.const 207 + i32.const 230 i32.const 15 call $~lib/builtins/abort unreachable @@ -455,7 +455,7 @@ if i32.const 0 i32.const 24 - i32.const 222 + i32.const 245 i32.const 13 call $~lib/builtins/abort unreachable @@ -471,7 +471,7 @@ if i32.const 0 i32.const 24 - i32.const 223 + i32.const 246 i32.const 13 call $~lib/builtins/abort unreachable @@ -488,8 +488,8 @@ i32.const 0 local.set $9 local.get $8 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $10 else i32.const 31 @@ -528,7 +528,7 @@ if i32.const 0 i32.const 24 - i32.const 239 + i32.const 262 i32.const 13 call $~lib/builtins/abort unreachable @@ -542,12 +542,12 @@ local.set $7 local.get $3 local.get $6 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $7 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -575,12 +575,12 @@ local.set $7 local.get $12 local.get $3 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $6 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add local.get $7 i32.store offset=96 @@ -651,7 +651,7 @@ if i32.const 0 i32.const 24 - i32.const 365 + i32.const 388 i32.const 4 call $~lib/builtins/abort unreachable @@ -676,7 +676,7 @@ if i32.const 0 i32.const 24 - i32.const 375 + i32.const 398 i32.const 15 call $~lib/builtins/abort unreachable @@ -707,7 +707,7 @@ if i32.const 0 i32.const 24 - i32.const 387 + i32.const 410 i32.const 4 call $~lib/builtins/abort unreachable @@ -884,12 +884,12 @@ local.set $6 local.get $9 local.get $8 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $7 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add local.get $6 i32.store offset=96 @@ -940,7 +940,7 @@ if i32.const 0 i32.const 24 - i32.const 427 + i32.const 450 i32.const 29 call $~lib/builtins/abort unreachable @@ -976,8 +976,8 @@ i32.const 0 local.set $2 local.get $1 - i32.const 16 - i32.div_u + i32.const 4 + i32.shr_u local.set $3 else local.get $1 @@ -1034,7 +1034,7 @@ if i32.const 0 i32.const 24 - i32.const 317 + i32.const 340 i32.const 13 call $~lib/builtins/abort unreachable @@ -1099,7 +1099,7 @@ if i32.const 0 i32.const 24 - i32.const 330 + i32.const 353 i32.const 17 call $~lib/builtins/abort unreachable @@ -1114,12 +1114,12 @@ local.set $5 local.get $9 local.get $8 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $5 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -1136,12 +1136,12 @@ local.set $4 local.get $8 local.get $5 - i32.const 16 - i32.mul + i32.const 4 + i32.shl local.get $4 i32.add - i32.const 4 - i32.mul + i32.const 2 + i32.shl i32.add i32.load offset=96 end @@ -1217,7 +1217,7 @@ if i32.const 0 i32.const 24 - i32.const 344 + i32.const 367 i32.const 13 call $~lib/builtins/abort unreachable @@ -1328,7 +1328,7 @@ if i32.const 0 i32.const 24 - i32.const 457 + i32.const 480 i32.const 15 call $~lib/builtins/abort unreachable @@ -1346,7 +1346,7 @@ if i32.const 0 i32.const 24 - i32.const 459 + i32.const 482 i32.const 13 call $~lib/builtins/abort unreachable @@ -1622,7 +1622,7 @@ if i32.const 0 i32.const 24 - i32.const 472 + i32.const 495 i32.const 13 call $~lib/builtins/abort unreachable @@ -1789,7 +1789,7 @@ if i32.const 0 i32.const 24 - i32.const 510 + i32.const 533 i32.const 2 call $~lib/builtins/abort unreachable