From 951b6f9f45e77901959befdc2c3f6e91abf34ac0 Mon Sep 17 00:00:00 2001 From: Nidin Vinayakan Date: Thu, 21 Feb 2019 00:22:25 +0100 Subject: [PATCH] Add shared memory support to compiler (#494) --- cli/asc.js | 1 + cli/asc.json | 5 +++++ src/compiler.ts | 7 +++++-- src/index.ts | 5 +++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cli/asc.js b/cli/asc.js index 0bbe4abf..840bebbb 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -421,6 +421,7 @@ exports.main = function main(argv, options, callback) { assemblyscript.setTarget(compilerOptions, 0); assemblyscript.setNoAssert(compilerOptions, args.noAssert); assemblyscript.setImportMemory(compilerOptions, args.importMemory); + assemblyscript.setSharedMemory(compilerOptions, args.sharedMemory); assemblyscript.setImportTable(compilerOptions, args.importTable); assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0); assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null); diff --git a/cli/asc.json b/cli/asc.json index adbddf51..74f7734c 100644 --- a/cli/asc.json +++ b/cli/asc.json @@ -101,6 +101,11 @@ "type": "b", "default": false }, + "sharedMemory": { + "description": "Declare memory as shared by settings the max shared memory.", + "type": "i", + "default": 0 + }, "memoryBase": { "description": "Sets the start offset of compiler-generated static memory.", "type": "i", diff --git a/src/compiler.ts b/src/compiler.ts index 27c84db7..d95f7f05 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -186,6 +186,8 @@ export class Options { noAssert: bool = false; /** If true, imports the memory provided by the embedder. */ importMemory: bool = false; + /** If greater than zero, declare memory as shared by setting max memory to sharedMemory. */ + sharedMemory: i32 = 0; /** If true, imports the function table provided by the embedder. */ importTable: bool = false; /** If true, generates information necessary for source maps. */ @@ -400,18 +402,19 @@ export class Compiler extends DiagnosticEmitter { } // set up memory + var isSharedMemory = options.hasFeature(Feature.THREADS) && options.sharedMemory > 0; module.setMemory( this.options.memoryBase /* is specified */ || this.memorySegments.length ? i64_low(i64_shr_u(i64_align(memoryOffset, 0x10000), i64_new(16, 0))) : 0, - Module.UNLIMITED_MEMORY, + isSharedMemory ? options.sharedMemory : Module.UNLIMITED_MEMORY, this.memorySegments, options.target, "memory" ); // import memory if requested (default memory is named '0' by Binaryen) - if (options.importMemory) module.addMemoryImport("0", "env", "memory"); + if (options.importMemory) module.addMemoryImport("0", "env", "memory", isSharedMemory); // set up function table var functionTable = this.functionTable; diff --git a/src/index.ts b/src/index.ts index c1d5e0a0..9de73751 100644 --- a/src/index.ts +++ b/src/index.ts @@ -97,6 +97,11 @@ export function setImportMemory(options: Options, importMemory: bool): void { options.importMemory = importMemory; } +/** Sets the `sharedMemory` option. */ +export function setSharedMemory(options: Options, sharedMemory: i32): void { + options.sharedMemory = sharedMemory; +} + /** Sets the `importTable` option. */ export function setImportTable(options: Options, importTable: bool): void { options.importTable = importTable;