Skip to content

Commit

Permalink
Added Tests and fixed offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaioletti committed Oct 10, 2019
1 parent 2ef003c commit 10aca34
Show file tree
Hide file tree
Showing 11 changed files with 2,742 additions and 57 deletions.
7 changes: 5 additions & 2 deletions src/builtins.ts
Expand Up @@ -499,6 +499,9 @@ export namespace BuiltinSymbols {
export const visit_globals = "~lib/rt/__visit_globals";
export const visit_members = "~lib/rt/__visit_members";

// // relocatable
export const memory_base = "~lib/relocatable/__memory_base";

// std/diagnostics.ts
export const ERROR = "~lib/diagnostics/ERROR";
export const WARNING = "~lib/diagnostics/WARNING";
Expand Down Expand Up @@ -4579,9 +4582,9 @@ export function compileRTTI(compiler: Compiler): void {
var segment = compiler.addMemorySegment(data);
if (usizeType.size == 8) {
let offset = segment.offset;
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I64, false, module.i64(i64_low(offset), i64_high(offset)));
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I64, false, module.i64Ptr(i64_low(offset), i64_high(offset)));
} else {
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I32, false, module.i32(i64_low(segment.offset)));
module.addGlobal(BuiltinSymbols.rtti_base, NativeType.I32, false, module.i32Ptr(i64_low(segment.offset)));
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/compiler.ts
Expand Up @@ -368,8 +368,8 @@ export class Compiler extends DiagnosticEmitter {
// add relocation globals
if (options.relocatable) {
let nativeSizeType = options.nativeSizeType;
module.addGlobalImport("__memory_base", "env", "memory_base", nativeSizeType, false);
module.setMemoryBase("__memory_base");
module.addGlobalImport(BuiltinSymbols.memory_base, "env", "memory_base", nativeSizeType, false);
module.setMemoryBase(BuiltinSymbols.memory_base);
module.addGlobalImport("__table_base", "env", "table_base", nativeSizeType, false);
module.setTableBase("__table_base");
}
Expand All @@ -389,7 +389,7 @@ export class Compiler extends DiagnosticEmitter {
if (!startIsEmpty || explicitStart) {
let signature = startFunctionInstance.signature;
if (!startIsEmpty && explicitStart) {
module.addGlobal(BuiltinSymbols.started, NativeType.I32, true, module.i32(0));
module.addGlobal(BuiltinSymbols.started, NativeType.I32, true, module.i32Ptr(0));
startFunctionBody.unshift(
module.if(
module.global_get(BuiltinSymbols.started, NativeType.I32),
Expand Down Expand Up @@ -1510,10 +1510,10 @@ export class Compiler extends DiagnosticEmitter {
var ref = i64_add(stringSegment.offset, i64_new(rtHeaderSize));
this.currentType = stringInstance.type;
if (this.options.isWasm64) {
return this.module.i64(i64_low(ref), i64_high(ref));
return this.module.i64Ptr(i64_low(ref), i64_high(ref));
} else {
assert(i64_is_u32(ref));
return this.module.i32(i64_low(ref));
return this.module.i32Ptr(i64_low(ref));
}
}

Expand Down Expand Up @@ -1645,7 +1645,7 @@ export class Compiler extends DiagnosticEmitter {
}

// === Statements ===============================================================================

compileTopLevelStatement(statement: Statement, body: ExpressionRef[]): void {
if (statement.kind == NodeKind.EXPORTDEFAULT) {
statement = (<ExportDefaultStatement>statement).declaration;
Expand Down Expand Up @@ -7607,8 +7607,8 @@ export class Compiler extends DiagnosticEmitter {
let arrayAddress = i64_add(arraySegment.offset, i64_new(runtimeHeaderSize));
this.currentType = arrayType;
return program.options.isWasm64
? this.module.i64(i64_low(arrayAddress), i64_high(arrayAddress))
: this.module.i32(i64_low(arrayAddress));
? this.module.i64Ptr(i64_low(arrayAddress), i64_high(arrayAddress))
: this.module.i32Ptr(i64_low(arrayAddress));

// otherwise allocate a new array header and make it wrap a copy of the static buffer
} else {
Expand Down
18 changes: 16 additions & 2 deletions src/module.ts
Expand Up @@ -525,6 +525,17 @@ export class Module {

// constants

i32Ptr(value: i32): ExpressionRef{
var out = this.lit;
_BinaryenLiteralInt32(out, value);
return this.relocMem(_BinaryenConst(this.ref, out));
}
i64Ptr(valueLow: i32, valueHigh: i32 = 0): ExpressionRef{
var out = this.lit;
_BinaryenLiteralInt64(out, valueLow, valueHigh);
return this.relocMem(_BinaryenConst(this.ref, out));
}

i32(value: i32): ExpressionRef {
var out = this.lit;
_BinaryenLiteralInt32(out, value);
Expand Down Expand Up @@ -618,7 +629,7 @@ export class Module {
offset: Index = 0,
align: Index = bytes // naturally aligned by default
): ExpressionRef {
return _BinaryenLoad(this.ref, bytes, signed ? 1 : 0, offset, align, type, this.relocMem(ptr));
return _BinaryenLoad(this.ref, bytes, signed ? 1 : 0, offset, align, type, ptr); //removed reloc
}

store(
Expand All @@ -629,7 +640,8 @@ export class Module {
offset: Index = 0,
align: Index = bytes // naturally aligned by default
): ExpressionRef {
return _BinaryenStore(this.ref, bytes, offset, align, this.relocMem(ptr), value, type);

return _BinaryenStore(this.ref, bytes, offset, align, ptr, value, type);
}

atomic_load(
Expand All @@ -638,6 +650,7 @@ export class Module {
type: NativeType,
offset: Index = 0
): ExpressionRef {
console.log("atomic_load")
return _BinaryenAtomicLoad(this.ref, bytes, offset, type, this.relocMem(ptr));
}

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

Expand Down
2 changes: 2 additions & 0 deletions std/assembly/index.d.ts
Expand Up @@ -116,6 +116,8 @@ declare const NaN: f32 | f64;
declare const Infinity: f32 | f64;
/** Heap base offset. */
declare const __heap_base: usize;
/** Memory Base Offset when relocatable memory is used */
declare const __memory_base: usize;
/** Determines the byte size of the specified underlying core type. Compiles to a constant. */
declare function sizeof<T>(): usize;
/** Determines the alignment (log2) of the specified underlying core type. Compiles to a constant. */
Expand Down
3 changes: 3 additions & 0 deletions std/assembly/relocatable.ts
@@ -0,0 +1,3 @@
// @ts-ignore: decorator
@builtin
export declare const __memory_base: usize;
2 changes: 1 addition & 1 deletion std/assembly/rt/stub.ts
Expand Up @@ -2,7 +2,7 @@ import { AL_MASK, BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE, AL_SIZE, DEBUG } from "r

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

// @ts-ignore: decorator
@lazy
Expand Down
12 changes: 6 additions & 6 deletions std/assembly/util/error.ts
Expand Up @@ -2,25 +2,25 @@
// and reusing them where possible ensures minimal static data in binaries.

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

// @ts-ignore: decorator
@lazy @inline
@lazy
export const E_INVALIDLENGTH: string = "Invalid length";

// @ts-ignore: decorator
@lazy @inline
@lazy
export const E_EMPTYARRAY: string = "Array is empty";

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

// @ts-ignore: decorator
@lazy @inline
@lazy
export const E_NOTIMPLEMENTED: string = "Not implemented";

// @ts-ignore: decorator
@lazy @inline
@lazy
export const E_KEYNOTFOUND: string = "Key does not exist";
24 changes: 17 additions & 7 deletions tests/compiler/relocatable.js
@@ -1,18 +1,28 @@
exports.preInstantiate = function(imports, exports) {
exports.preInstantiate = function (imports, exports) {
// compiler generates initial = 1 because it doesn't know the imported value
// of env.memory_base yet, hence we need to import a suitable memory as well:
imports["env"] = {
"abort": (mesg, file, line, colm) => {
console.log("abort", mesg, file, line, colm)
assert(false, "abort was called")

},
"memory": new WebAssembly.Memory({ initial: 2 }),
"memory_base": 2,
"memory_base": 1000,
"table_base": 100,
"log":(offset,length)=>{
let view=new Uint16Array(imports["env"].memory.buffer,offset,length)
let str=String.fromCharCode.apply(null,view)
assert(str==="relocatable",`expected relocatable got'${str}' at index ${offset}`)
"log": (offset, length) => {
imports["env"].checkPtr(offset)
let view = new Uint16Array(imports["env"].memory.buffer, offset, length)
let str = String.fromCharCode.apply(null, view)
console.log("val",offset,length,str)
assert(str === "relocatable", `expected relocatable got'${str}' at index ${offset} with length ${length}`)
},
"checkPtr":(offset)=>{
assert(offset>imports["env"].memory_base, `offset ${offset} is less than memory_base ${imports["env"].memory_base}`)
}
};
};

exports.postInstantiate=function(instance){
exports.postInstantiate = function (instance) {
instance.exports.main()
}

0 comments on commit 10aca34

Please sign in to comment.