Skip to content

Commit e79155b

Browse files
authored
Add a tableBase CLI option (AssemblyScript#1087)
1 parent e5a768b commit e79155b

18 files changed

+91
-13
lines changed

cli/asc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ exports.main = function main(argv, options, callback) {
227227
assemblyscript.setExportTable(compilerOptions, args.exportTable);
228228
assemblyscript.setExplicitStart(compilerOptions, args.explicitStart);
229229
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
230+
assemblyscript.setTableBase(compilerOptions, args.tableBase >>> 0);
230231
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);
231232
assemblyscript.setNoUnsafe(compilerOptions, args.noUnsafe);
232233
assemblyscript.setPedantic(compilerOptions, args.pedantic);

cli/asc.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,16 @@
191191
"type": "S",
192192
"alias": "u"
193193
},
194+
194195
"memoryBase": {
195-
"category": "Features",
196-
"description": "Sets the start offset of compiler-generated static memory.",
196+
"category": "Linking",
197+
"description": "Sets the start offset of emitted memory segments.",
198+
"type": "i",
199+
"default": 0
200+
},
201+
"tableBase": {
202+
"category": "Linking",
203+
"description": "Sets the start offset of emitted table elements.",
197204
"type": "i",
198205
"default": 0
199206
},

src/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export namespace CommonNames {
151151
export const ASC_NO_TREESHAKING = "ASC_NO_TREESHAKING";
152152
export const ASC_NO_ASSERT = "ASC_NO_ASSERT";
153153
export const ASC_MEMORY_BASE = "ASC_MEMORY_BASE";
154+
export const ASC_TABLE_BASE = "ASC_TABLE_BASE";
154155
export const ASC_OPTIMIZE_LEVEL = "ASC_OPTIMIZE_LEVEL";
155156
export const ASC_SHRINK_LEVEL = "ASC_SHRINK_LEVEL";
156157
export const ASC_FEATURE_SIGN_EXTENSION = "ASC_FEATURE_SIGN_EXTENSION";

src/compiler.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ export class Options {
204204
explicitStart: bool = false;
205205
/** Static memory start offset. */
206206
memoryBase: i32 = 0;
207+
/** Static table start offset. */
208+
tableBase: i32 = 0;
207209
/** Global aliases, mapping alias names as the key to internal names to be aliased as the value. */
208210
globalAliases: Map<string,string> | null = null;
209211
/** Features to activate by default. These are the finished proposals. */
@@ -500,7 +502,9 @@ export class Compiler extends DiagnosticEmitter {
500502

501503
// set up function table (first elem is blank)
502504
var functionTable = this.functionTable;
503-
module.setFunctionTable(1 + functionTable.length, Module.UNLIMITED_TABLE, functionTable, module.i32(1));
505+
var tableBase = this.options.tableBase;
506+
if (!tableBase) tableBase = 1; // leave first elem blank
507+
module.setFunctionTable(tableBase + functionTable.length, Module.UNLIMITED_TABLE, functionTable, module.i32(tableBase));
504508

505509
// import and/or export table if requested (default table is named '0' by Binaryen)
506510
if (options.importTable) {
@@ -992,7 +996,11 @@ export class Compiler extends DiagnosticEmitter {
992996

993997
// Initialize to zero if there's no initializer
994998
} else {
995-
initExpr = this.makeZero(type);
999+
if (global.is(CommonFlags.INLINED)) {
1000+
initExpr = this.compileInlineConstant(global, global.type, Constraints.PREFER_STATIC | Constraints.WILL_RETAIN);
1001+
} else {
1002+
initExpr = this.makeZero(type);
1003+
}
9961004
}
9971005

9981006
var internalName = global.internalName;
@@ -1644,12 +1652,11 @@ export class Compiler extends DiagnosticEmitter {
16441652
ensureFunctionTableEntry(instance: Function): i32 {
16451653
assert(instance.is(CommonFlags.COMPILED));
16461654
var index = instance.functionTableIndex;
1647-
if (index >= 0) {
1648-
assert(index != 0); // first elem must be blank
1649-
return index;
1650-
}
1655+
if (index >= 0) return index;
16511656
var functionTable = this.functionTable;
1652-
index = 1 + functionTable.length; // first elem is blank
1657+
var tableBase = this.options.tableBase;
1658+
if (!tableBase) tableBase = 1; // leave first elem blank
1659+
index = tableBase + functionTable.length;
16531660
if (!instance.is(CommonFlags.TRAMPOLINE) && instance.signature.requiredParameters < instance.signature.parameterTypes.length) {
16541661
// insert the trampoline if the function has optional parameters
16551662
instance = this.ensureTrampoline(instance);
@@ -2990,7 +2997,7 @@ export class Compiler extends DiagnosticEmitter {
29902997
contextualType: Type,
29912998
constraints: Constraints
29922999
): ExpressionRef {
2993-
assert(element.is(CommonFlags.INLINED));
3000+
assert(element.is(CommonFlags.INLINED | CommonFlags.RESOLVED));
29943001
var type = element.type;
29953002
switch (
29963003
!(constraints & (Constraints.CONV_IMPLICIT | Constraints.CONV_EXPLICIT)) &&

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export function setMemoryBase(options: Options, memoryBase: u32): void {
5757
options.memoryBase = memoryBase;
5858
}
5959

60+
/** Sets the `tableBase` option. */
61+
export function setTableBase(options: Options, tableBase: u32): void {
62+
options.tableBase = tableBase;
63+
}
64+
6065
/** Sets a 'globalAliases' value. */
6166
export function setGlobalAlias(options: Options, alias: string, name: string): void {
6267
var globalAliases = options.globalAliases;

src/program.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,8 @@ export class Program extends DiagnosticEmitter {
679679
i64_new(options.noAssert ? 1 : 0, 0));
680680
this.registerConstantInteger(CommonNames.ASC_MEMORY_BASE, Type.i32,
681681
i64_new(options.memoryBase, 0));
682+
this.registerConstantInteger(CommonNames.ASC_TABLE_BASE, Type.i32,
683+
i64_new(options.tableBase, 0));
682684
this.registerConstantInteger(CommonNames.ASC_OPTIMIZE_LEVEL, Type.i32,
683685
i64_new(options.optimizeLevelHint, 0));
684686
this.registerConstantInteger(CommonNames.ASC_SHRINK_LEVEL, Type.i32,

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ declare const ASC_TARGET: i32;
4646
declare const ASC_NO_ASSERT: bool;
4747
/** Provided memoryBase option. */
4848
declare const ASC_MEMORY_BASE: i32;
49+
/** Provided tableBase option. */
50+
declare const ASC_TABLE_BASE: i32;
4951
/** Provided optimizeLevel option. */
5052
declare const ASC_OPTIMIZE_LEVEL: i32;
5153
/** Provided shrinkLevel option. */

tests/compiler/asc-constants.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(global $~lib/ASC_OPTIMIZE_LEVEL i32 (i32.const 0))
99
(global $~lib/ASC_SHRINK_LEVEL i32 (i32.const 0))
1010
(global $~lib/ASC_FEATURE_SIGN_EXTENSION i32 (i32.const 0))
11-
(global $~lib/ASC_FEATURE_MUTABLE_GLOBALS i32 (i32.const 0))
11+
(global $~lib/ASC_FEATURE_MUTABLE_GLOBALS i32 (i32.const 1))
1212
(global $~lib/ASC_FEATURE_NONTRAPPING_F2I i32 (i32.const 0))
1313
(global $~lib/ASC_FEATURE_BULK_MEMORY i32 (i32.const 0))
1414
(global $~lib/ASC_FEATURE_SIMD i32 (i32.const 0))

tests/compiler/features/simd.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(data (i32.const 16) "\1e\00\00\00\01\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00s\00t\00u\00b\00.\00t\00s\00")
99
(data (i32.const 64) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00f\00e\00a\00t\00u\00r\00e\00s\00/\00s\00i\00m\00d\00.\00t\00s\00")
1010
(table $0 1 funcref)
11-
(global $~lib/ASC_FEATURE_SIMD i32 (i32.const 0))
11+
(global $~lib/ASC_FEATURE_SIMD i32 (i32.const 1))
1212
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
1313
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
1414
(global $~lib/builtins/i16.MAX_VALUE i32 (i32.const 32767))

tests/compiler/features/threads.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $none_=>_none (func))
33
(memory $0 (shared 1 1))
44
(table $0 1 funcref)
5-
(global $~lib/ASC_FEATURE_THREADS i32 (i32.const 0))
5+
(global $~lib/ASC_FEATURE_THREADS i32 (i32.const 1))
66
(export "memory" (memory $0))
77
(start $~start)
88
(func $features/threads/testAtomic (; 0 ;)

tests/compiler/memorybase.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none",
4+
"--memoryBase 1024"
5+
]
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(memory $0 1)
3+
(data (i32.const 1024) "\16\00\00\00\01\00\00\00\01\00\00\00\16\00\00\00h\00e\00l\00l\00o\00 \00w\00o\00r\00l\00d")
4+
(export "memory" (memory $0))
5+
)

tests/compiler/memorybase.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { BLOCK_OVERHEAD } from "rt/common";
2+
3+
const staticString = "hello world";
4+
5+
assert(ASC_MEMORY_BASE == 1024);
6+
assert(changetype<usize>(staticString) - BLOCK_OVERHEAD == 1024);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(memory $0 1)
3+
(data (i32.const 1024) "\16\00\00\00\01\00\00\00\01\00\00\00\16\00\00\00h\00e\00l\00l\00o\00 \00w\00o\00r\00l\00d\00")
4+
(table $0 1 funcref)
5+
(global $memorybase/staticString i32 (i32.const 1040))
6+
(global $~lib/ASC_MEMORY_BASE i32 (i32.const 1024))
7+
(export "memory" (memory $0))
8+
)

tests/compiler/tablebase.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none",
4+
"--tableBase 32"
5+
]
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module
2+
(memory $0 0)
3+
(export "memory" (memory $0))
4+
)

tests/compiler/tablebase.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function foo(): void {}
2+
3+
const staticFunction = foo;
4+
5+
assert(ASC_TABLE_BASE == 32);
6+
assert(changetype<usize>(staticFunction) == 32);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(module
2+
(type $none_=>_none (func))
3+
(memory $0 0)
4+
(table $0 33 funcref)
5+
(elem (i32.const 32) $tablebase/foo)
6+
(global $tablebase/staticFunction i32 (i32.const 32))
7+
(global $~lib/ASC_TABLE_BASE i32 (i32.const 32))
8+
(export "memory" (memory $0))
9+
(func $tablebase/foo (; 0 ;)
10+
nop
11+
)
12+
)

0 commit comments

Comments
 (0)