Skip to content

Commit 10aca34

Browse files
committed
Added Tests and fixed offsets
1 parent 2ef003c commit 10aca34

File tree

11 files changed

+2742
-57
lines changed

11 files changed

+2742
-57
lines changed

src/builtins.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ export namespace BuiltinSymbols {
499499
export const visit_globals = "~lib/rt/__visit_globals";
500500
export const visit_members = "~lib/rt/__visit_members";
501501

502+
// // relocatable
503+
export const memory_base = "~lib/relocatable/__memory_base";
504+
502505
// std/diagnostics.ts
503506
export const ERROR = "~lib/diagnostics/ERROR";
504507
export const WARNING = "~lib/diagnostics/WARNING";
@@ -4579,9 +4582,9 @@ export function compileRTTI(compiler: Compiler): void {
45794582
var segment = compiler.addMemorySegment(data);
45804583
if (usizeType.size == 8) {
45814584
let offset = segment.offset;
4582-
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I64, false, module.i64(i64_low(offset), i64_high(offset)));
4585+
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I64, false, module.i64Ptr(i64_low(offset), i64_high(offset)));
45834586
} else {
4584-
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I32, false, module.i32(i64_low(segment.offset)));
4587+
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I32, false, module.i32Ptr(i64_low(segment.offset)));
45854588
}
45864589
}
45874590

src/compiler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ export class Compiler extends DiagnosticEmitter {
368368
// add relocation globals
369369
if (options.relocatable) {
370370
let nativeSizeType = options.nativeSizeType;
371-
module.addGlobalImport("__memory_base", "env", "memory_base", nativeSizeType, false);
372-
module.setMemoryBase("__memory_base");
371+
module.addGlobalImport(BuiltinSymbols.memory_base, "env", "memory_base", nativeSizeType, false);
372+
module.setMemoryBase(BuiltinSymbols.memory_base);
373373
module.addGlobalImport("__table_base", "env", "table_base", nativeSizeType, false);
374374
module.setTableBase("__table_base");
375375
}
@@ -389,7 +389,7 @@ export class Compiler extends DiagnosticEmitter {
389389
if (!startIsEmpty || explicitStart) {
390390
let signature = startFunctionInstance.signature;
391391
if (!startIsEmpty && explicitStart) {
392-
module.addGlobal(BuiltinSymbols.started, NativeType.I32, true, module.i32(0));
392+
module.addGlobal(BuiltinSymbols.started, NativeType.I32, true, module.i32Ptr(0));
393393
startFunctionBody.unshift(
394394
module.if(
395395
module.global_get(BuiltinSymbols.started, NativeType.I32),
@@ -1510,10 +1510,10 @@ export class Compiler extends DiagnosticEmitter {
15101510
var ref = i64_add(stringSegment.offset, i64_new(rtHeaderSize));
15111511
this.currentType = stringInstance.type;
15121512
if (this.options.isWasm64) {
1513-
return this.module.i64(i64_low(ref), i64_high(ref));
1513+
return this.module.i64Ptr(i64_low(ref), i64_high(ref));
15141514
} else {
15151515
assert(i64_is_u32(ref));
1516-
return this.module.i32(i64_low(ref));
1516+
return this.module.i32Ptr(i64_low(ref));
15171517
}
15181518
}
15191519

@@ -1645,7 +1645,7 @@ export class Compiler extends DiagnosticEmitter {
16451645
}
16461646

16471647
// === Statements ===============================================================================
1648-
1648+
16491649
compileTopLevelStatement(statement: Statement, body: ExpressionRef[]): void {
16501650
if (statement.kind == NodeKind.EXPORTDEFAULT) {
16511651
statement = (<ExportDefaultStatement>statement).declaration;
@@ -7607,8 +7607,8 @@ export class Compiler extends DiagnosticEmitter {
76077607
let arrayAddress = i64_add(arraySegment.offset, i64_new(runtimeHeaderSize));
76087608
this.currentType = arrayType;
76097609
return program.options.isWasm64
7610-
? this.module.i64(i64_low(arrayAddress), i64_high(arrayAddress))
7611-
: this.module.i32(i64_low(arrayAddress));
7610+
? this.module.i64Ptr(i64_low(arrayAddress), i64_high(arrayAddress))
7611+
: this.module.i32Ptr(i64_low(arrayAddress));
76127612

76137613
// otherwise allocate a new array header and make it wrap a copy of the static buffer
76147614
} else {

src/module.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,17 @@ export class Module {
525525

526526
// constants
527527

528+
i32Ptr(value: i32): ExpressionRef{
529+
var out = this.lit;
530+
_BinaryenLiteralInt32(out, value);
531+
return this.relocMem(_BinaryenConst(this.ref, out));
532+
}
533+
i64Ptr(valueLow: i32, valueHigh: i32 = 0): ExpressionRef{
534+
var out = this.lit;
535+
_BinaryenLiteralInt64(out, valueLow, valueHigh);
536+
return this.relocMem(_BinaryenConst(this.ref, out));
537+
}
538+
528539
i32(value: i32): ExpressionRef {
529540
var out = this.lit;
530541
_BinaryenLiteralInt32(out, value);
@@ -618,7 +629,7 @@ export class Module {
618629
offset: Index = 0,
619630
align: Index = bytes // naturally aligned by default
620631
): ExpressionRef {
621-
return _BinaryenLoad(this.ref, bytes, signed ? 1 : 0, offset, align, type, this.relocMem(ptr));
632+
return _BinaryenLoad(this.ref, bytes, signed ? 1 : 0, offset, align, type, ptr); //removed reloc
622633
}
623634

624635
store(
@@ -629,7 +640,8 @@ export class Module {
629640
offset: Index = 0,
630641
align: Index = bytes // naturally aligned by default
631642
): ExpressionRef {
632-
return _BinaryenStore(this.ref, bytes, offset, align, this.relocMem(ptr), value, type);
643+
644+
return _BinaryenStore(this.ref, bytes, offset, align, ptr, value, type);
633645
}
634646

635647
atomic_load(
@@ -638,6 +650,7 @@ export class Module {
638650
type: NativeType,
639651
offset: Index = 0
640652
): ExpressionRef {
653+
console.log("atomic_load")
641654
return _BinaryenAtomicLoad(this.ref, bytes, offset, type, this.relocMem(ptr));
642655
}
643656

@@ -648,6 +661,7 @@ export class Module {
648661
type: NativeType,
649662
offset: Index = 0
650663
): ExpressionRef {
664+
console.log("atomic_store")
651665
return _BinaryenAtomicStore(this.ref, bytes, offset, this.relocMem(ptr), value, type);
652666
}
653667

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ declare const NaN: f32 | f64;
116116
declare const Infinity: f32 | f64;
117117
/** Heap base offset. */
118118
declare const __heap_base: usize;
119+
/** Memory Base Offset when relocatable memory is used */
120+
declare const __memory_base: usize;
119121
/** Determines the byte size of the specified underlying core type. Compiles to a constant. */
120122
declare function sizeof<T>(): usize;
121123
/** Determines the alignment (log2) of the specified underlying core type. Compiles to a constant. */

std/assembly/relocatable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @ts-ignore: decorator
2+
@builtin
3+
export declare const __memory_base: usize;

std/assembly/rt/stub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AL_MASK, BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE, AL_SIZE, DEBUG } from "r
22

33
// @ts-ignore: decorator
44
@lazy
5-
var startOffset: usize = (__heap_base + AL_MASK) & ~AL_MASK;
5+
var startOffset: usize = (__heap_base + AL_MASK + __memory_base) & ~AL_MASK;
66

77
// @ts-ignore: decorator
88
@lazy

std/assembly/util/error.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
// and reusing them where possible ensures minimal static data in binaries.
33

44
// @ts-ignore: decorator
5-
@lazy @inline
5+
@lazy
66
export const E_INDEXOUTOFRANGE: string = "Index out of range";
77

88
// @ts-ignore: decorator
9-
@lazy @inline
9+
@lazy
1010
export const E_INVALIDLENGTH: string = "Invalid length";
1111

1212
// @ts-ignore: decorator
13-
@lazy @inline
13+
@lazy
1414
export const E_EMPTYARRAY: string = "Array is empty";
1515

1616
// @ts-ignore: decorator
17-
@lazy @inline
17+
@lazy
1818
export const E_HOLEYARRAY: string = "Element type must be nullable if array is holey";
1919

2020
// @ts-ignore: decorator
21-
@lazy @inline
21+
@lazy
2222
export const E_NOTIMPLEMENTED: string = "Not implemented";
2323

2424
// @ts-ignore: decorator
25-
@lazy @inline
25+
@lazy
2626
export const E_KEYNOTFOUND: string = "Key does not exist";

tests/compiler/relocatable.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
exports.preInstantiate = function(imports, exports) {
1+
exports.preInstantiate = function (imports, exports) {
22
// compiler generates initial = 1 because it doesn't know the imported value
33
// of env.memory_base yet, hence we need to import a suitable memory as well:
44
imports["env"] = {
5+
"abort": (mesg, file, line, colm) => {
6+
console.log("abort", mesg, file, line, colm)
7+
assert(false, "abort was called")
8+
9+
},
510
"memory": new WebAssembly.Memory({ initial: 2 }),
6-
"memory_base": 2,
11+
"memory_base": 1000,
712
"table_base": 100,
8-
"log":(offset,length)=>{
9-
let view=new Uint16Array(imports["env"].memory.buffer,offset,length)
10-
let str=String.fromCharCode.apply(null,view)
11-
assert(str==="relocatable",`expected relocatable got'${str}' at index ${offset}`)
13+
"log": (offset, length) => {
14+
imports["env"].checkPtr(offset)
15+
let view = new Uint16Array(imports["env"].memory.buffer, offset, length)
16+
let str = String.fromCharCode.apply(null, view)
17+
console.log("val",offset,length,str)
18+
assert(str === "relocatable", `expected relocatable got'${str}' at index ${offset} with length ${length}`)
19+
},
20+
"checkPtr":(offset)=>{
21+
assert(offset>imports["env"].memory_base, `offset ${offset} is less than memory_base ${imports["env"].memory_base}`)
1222
}
1323
};
1424
};
1525

16-
exports.postInstantiate=function(instance){
26+
exports.postInstantiate = function (instance) {
1727
instance.exports.main()
1828
}

0 commit comments

Comments
 (0)