From 896810859e229e38f00ecb4ace2d0f9628b9f62a Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sun, 28 Jan 2018 19:33:05 +0100 Subject: [PATCH] Make memory allocators pluggable --- bin/asc.js | 2 +- src/util/path.ts | 2 ++ std/assembly/memory.ts | 2 -- tests/compiler/std/array.ts | 2 ++ tests/compiler/std/array.wast | 2 +- tests/compiler/std/carray.wast | 16 ---------------- tests/compiler/std/heap.ts | 3 ++- tests/compiler/std/heap.wast | 4 ++-- tests/compiler/std/new.ts | 2 ++ tests/compiler/std/new.wast | 2 +- tests/compiler/std/set.ts | 2 ++ tests/compiler/std/set.wast | 2 +- tests/compiler/std/string.wast | 16 ---------------- 13 files changed, 16 insertions(+), 41 deletions(-) diff --git a/bin/asc.js b/bin/asc.js index 226e4010..d1a3136a 100644 --- a/bin/asc.js +++ b/bin/asc.js @@ -156,7 +156,7 @@ args._.forEach(filename => { while ((nextPath = parser.nextFile()) != null) { try { readTime += measure(() => { - if (nextPath.starsWith("std:")) + if (nextPath.startsWith("std:")) nextText = fs.readFileSync(stdlibDir + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" }); else nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" }); diff --git a/src/util/path.ts b/src/util/path.ts index 3e150b1b..47438f6e 100644 --- a/src/util/path.ts +++ b/src/util/path.ts @@ -82,6 +82,8 @@ export function normalize(path: string, trimExtension: bool = false, separator: /** Resolves the specified path to a normalized path relative to the specified origin. */ export function resolve(normalizedPath: string, normalizedOrigin: string, separator: CharCode = CharCode.SLASH): string { + if (normalizedPath.startsWith("std:")) + return normalizedPath; return normalize(dirname(normalizedOrigin, separator) + String.fromCharCode(separator) + normalizedPath); } diff --git a/std/assembly/memory.ts b/std/assembly/memory.ts index 662bba2b..53743ce6 100644 --- a/std/assembly/memory.ts +++ b/std/assembly/memory.ts @@ -1,5 +1,3 @@ -import "std:memory/arena"; - function copy_memory(dest: usize, src: usize, n: usize): void { // based on musl's implementation of memcpy // not a future instruction and sufficiently covered by the upcoming move_memory intrinsic diff --git a/tests/compiler/std/array.ts b/tests/compiler/std/array.ts index 4af7f747..60ed6e09 100644 --- a/tests/compiler/std/array.ts +++ b/tests/compiler/std/array.ts @@ -1,3 +1,5 @@ +import "std:memory/arena"; + var arr = changetype(allocate_memory(sizeof() + 2 * sizeof())); assert(arr.length == 0); diff --git a/tests/compiler/std/array.wast b/tests/compiler/std/array.wast index 2bc348a5..9cabb1e2 100644 --- a/tests/compiler/std/array.wast +++ b/tests/compiler/std/array.wast @@ -7,10 +7,10 @@ (type $iii (func (param i32 i32) (result i32))) (type $iiii (func (param i32 i32 i32) (result i32))) (type $v (func)) - (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) (global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3)) (global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8)) (global $std:memory/arena/ALIGN_MASK i32 (i32.const 7)) + (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) (global $std/array/arr (mut i32) (i32.const 0)) (global $std/array/i (mut i32) (i32.const 0)) (global $HEAP_BASE i32 (i32.const 4)) diff --git a/tests/compiler/std/carray.wast b/tests/compiler/std/carray.wast index c58f2cdd..654d1ab7 100644 --- a/tests/compiler/std/carray.wast +++ b/tests/compiler/std/carray.wast @@ -285,16 +285,6 @@ FUNCTION_PROTOTYPE: std:string/parseFloat FUNCTION_PROTOTYPE: parseFloat GLOBAL: std/carray/arr - GLOBAL: std:memory/arena/ALIGN_LOG2 - GLOBAL: std:memory/arena/ALIGN_SIZE - GLOBAL: std:memory/arena/ALIGN_MASK - GLOBAL: std:memory/arena/HEAP_OFFSET - FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory - FUNCTION_PROTOTYPE: allocate_memory - FUNCTION_PROTOTYPE: std:memory/arena/free_memory - FUNCTION_PROTOTYPE: free_memory - FUNCTION_PROTOTYPE: std:memory/arena/clear_memory - FUNCTION_PROTOTYPE: clear_memory [program.exports] CLASS_PROTOTYPE: std:array/Array CLASS_PROTOTYPE: Array @@ -322,10 +312,4 @@ FUNCTION_PROTOTYPE: std:string/parseInt FUNCTION_PROTOTYPE: parseFloat FUNCTION_PROTOTYPE: std:string/parseFloat - FUNCTION_PROTOTYPE: allocate_memory - FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory - FUNCTION_PROTOTYPE: free_memory - FUNCTION_PROTOTYPE: std:memory/arena/free_memory - FUNCTION_PROTOTYPE: clear_memory - FUNCTION_PROTOTYPE: std:memory/arena/clear_memory ;) diff --git a/tests/compiler/std/heap.ts b/tests/compiler/std/heap.ts index 1bc48fb6..3a1b7f00 100644 --- a/tests/compiler/std/heap.ts +++ b/tests/compiler/std/heap.ts @@ -1,3 +1,5 @@ +import "std:memory/arena"; + const size: usize = 42; let ptr1: usize = allocate_memory(size); let ptr2: usize = allocate_memory(size); @@ -20,7 +22,6 @@ assert(compare_memory(ptr1, ptr2, size) == 0); free_memory(ptr1); free_memory(ptr2); -// arena clear_memory(); ptr1 = allocate_memory(size); assert(ptr1 == HEAP_BASE); diff --git a/tests/compiler/std/heap.wast b/tests/compiler/std/heap.wast index 2b37a614..421634fc 100644 --- a/tests/compiler/std/heap.wast +++ b/tests/compiler/std/heap.wast @@ -5,11 +5,11 @@ (type $iiii (func (param i32 i32 i32) (result i32))) (type $iv (func (param i32))) (type $v (func)) - (global $std/heap/size i32 (i32.const 42)) - (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) (global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3)) (global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8)) (global $std:memory/arena/ALIGN_MASK i32 (i32.const 7)) + (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) + (global $std/heap/size i32 (i32.const 42)) (global $std/heap/ptr1 (mut i32) (i32.const 0)) (global $std/heap/ptr2 (mut i32) (i32.const 0)) (global $std/heap/i (mut i32) (i32.const 0)) diff --git a/tests/compiler/std/new.ts b/tests/compiler/std/new.ts index 2eecae5e..f7b04596 100644 --- a/tests/compiler/std/new.ts +++ b/tests/compiler/std/new.ts @@ -1,3 +1,5 @@ +import "std:memory/arena"; + class AClass { static aStaticField: i32 = 0; aField: i32 = 1; diff --git a/tests/compiler/std/new.wast b/tests/compiler/std/new.wast index c41f8fdb..0992331f 100644 --- a/tests/compiler/std/new.wast +++ b/tests/compiler/std/new.wast @@ -3,10 +3,10 @@ (type $ii (func (param i32) (result i32))) (type $ifv (func (param i32 f32))) (type $v (func)) - (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) (global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3)) (global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8)) (global $std:memory/arena/ALIGN_MASK i32 (i32.const 7)) + (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) (global $std/new/aClass (mut i32) (i32.const 0)) (global $HEAP_BASE i32 (i32.const 4)) (memory $0 1) diff --git a/tests/compiler/std/set.ts b/tests/compiler/std/set.ts index 4062f369..91b54f4e 100644 --- a/tests/compiler/std/set.ts +++ b/tests/compiler/std/set.ts @@ -1,3 +1,5 @@ +import "std:memory/arena"; + // note that this doesn't test a real set implementation yet, see std/assembly/set.ts var set = changetype>(allocate_memory(sizeof() + 2 * sizeof())); diff --git a/tests/compiler/std/set.wast b/tests/compiler/std/set.wast index 3208097e..9f130343 100644 --- a/tests/compiler/std/set.wast +++ b/tests/compiler/std/set.wast @@ -5,10 +5,10 @@ (type $iv (func (param i32))) (type $iii (func (param i32 i32) (result i32))) (type $v (func)) - (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) (global $std:memory/arena/ALIGN_LOG2 i32 (i32.const 3)) (global $std:memory/arena/ALIGN_SIZE i32 (i32.const 8)) (global $std:memory/arena/ALIGN_MASK i32 (i32.const 7)) + (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) (global $std/set/set (mut i32) (i32.const 0)) (global $HEAP_BASE i32 (i32.const 4)) (memory $0 1) diff --git a/tests/compiler/std/string.wast b/tests/compiler/std/string.wast index 79eae22b..197c235f 100644 --- a/tests/compiler/std/string.wast +++ b/tests/compiler/std/string.wast @@ -636,16 +636,6 @@ FUNCTION_PROTOTYPE: parseFloat GLOBAL: std/string/str FUNCTION_PROTOTYPE: std/string/getString - GLOBAL: std:memory/arena/ALIGN_LOG2 - GLOBAL: std:memory/arena/ALIGN_SIZE - GLOBAL: std:memory/arena/ALIGN_MASK - GLOBAL: std:memory/arena/HEAP_OFFSET - FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory - FUNCTION_PROTOTYPE: allocate_memory - FUNCTION_PROTOTYPE: std:memory/arena/free_memory - FUNCTION_PROTOTYPE: free_memory - FUNCTION_PROTOTYPE: std:memory/arena/clear_memory - FUNCTION_PROTOTYPE: clear_memory [program.exports] CLASS_PROTOTYPE: std:array/Array CLASS_PROTOTYPE: Array @@ -674,10 +664,4 @@ FUNCTION_PROTOTYPE: parseFloat FUNCTION_PROTOTYPE: std:string/parseFloat FUNCTION_PROTOTYPE: std/string/getString - FUNCTION_PROTOTYPE: allocate_memory - FUNCTION_PROTOTYPE: std:memory/arena/allocate_memory - FUNCTION_PROTOTYPE: free_memory - FUNCTION_PROTOTYPE: std:memory/arena/free_memory - FUNCTION_PROTOTYPE: clear_memory - FUNCTION_PROTOTYPE: std:memory/arena/clear_memory ;)