Do not allocate MemorySegments for memory.data(i32, i32?) by CountBleck · Pull Request #2831 · AssemblyScript/assemblyscript · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/builtins.ts
16 changes: 13 additions & 3 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,16 @@ export class Compiler extends DiagnosticEmitter {
return segment;
}

/** Adds a dummy memory segment. */
addZeroedMemorySegment(size: i32, alignment: i32 = 16): MemorySegment {
assert(isPowerOf2(alignment));
let memoryOffset = i64_align(this.memoryOffset, alignment);
let segment = new MemorySegment(null, memoryOffset);
this.memorySegments.push(segment);
this.memoryOffset = i64_add(memoryOffset, i64_new(size));
return segment;
}

/** Adds a static memory segment representing a runtime object. */
addRuntimeMemorySegment(buffer: Uint8Array): MemorySegment {
let memoryOffset = this.program.computeBlockStart64(this.memoryOffset);
Expand Down Expand Up @@ -2043,7 +2053,7 @@ export class Compiler extends DiagnosticEmitter {
if (!arrayInstance) {
arrayInstance = assert(this.resolver.resolveClass(this.program.arrayPrototype, [ elementType ]));
}
let bufferLength = readI32(bufferSegment.buffer, program.OBJECTInstance.offsetof("rtSize"));
let bufferLength = readI32(bufferSegment.buffer!, program.OBJECTInstance.offsetof("rtSize"));
let arrayLength = i32(bufferLength / elementType.byteSize);
let bufferAddress = i64_add(bufferSegment.offset, i64_new(program.totalOverhead));
let buf = arrayInstance.createBuffer();
Expand Down Expand Up @@ -8060,7 +8070,7 @@ export class Compiler extends DiagnosticEmitter {
}
let arrayInstance = assert(this.resolver.resolveClass(this.program.staticArrayPrototype, [ stringType ]));
let segment = this.addStaticBuffer(stringType, values, arrayInstance.id);
this.program.OBJECTInstance.writeField("gcInfo", 3, segment.buffer, 0); // use transparent gcinfo
this.program.OBJECTInstance.writeField("gcInfo", 3, segment.buffer!, 0); // use transparent gcinfo
let offset = i64_add(segment.offset, i64_new(this.program.totalOverhead));
let joinInstance = assert(arrayInstance.getMethod("join"));
let indexedSetInstance = assert(arrayInstance.lookupOverload(OperatorKind.IndexedSet, true));
Expand Down Expand Up @@ -8151,7 +8161,7 @@ export class Compiler extends DiagnosticEmitter {
);
arrayInstance.writeField("raw",
i64_add(rawHeaderSegment.offset, i64_new(this.program.totalOverhead)),
arraySegment.buffer
arraySegment.buffer!
);
} else {
arraySegment = this.addStaticArrayHeader(stringType,
Expand Down
27 changes: 18 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ export const enum ExpressionRunnerFlags {
export class MemorySegment {
constructor(
/** Segment data. */
public buffer: Uint8Array,
public buffer: Uint8Array | null,
/** Segment offset. */
public offset: i64
) {}
Expand Down Expand Up @@ -2409,22 +2409,31 @@ export class Module {
let cStr1 = this.allocStringCached(exportName);
let cStr2 = this.allocStringCached(name);
let k = segments.length;
let segs = new Array<usize>(k);
let empty = 0;

let segs = new Array<usize>();
let psvs = new Uint8Array(k);
let offs = new Array<ExpressionRef>(k);
let sizs = new Array<Index>(k);
for (let i = 0; i < k; ++i) {
let offs = new Array<ExpressionRef>();
let sizs = new Array<Index>();
for (let i = 0; i < k; i++) {
let segment = unchecked(segments[i]);
let buffer = segment.buffer;
if (!buffer) {
empty++;
continue;
}

let offset = segment.offset;
unchecked(segs[i] = allocU8Array(buffer));
unchecked(psvs[i] = 0); // no passive segments currently
unchecked(offs[i] = target == Target.Wasm64
segs.push(allocU8Array(buffer));
psvs[i - empty] = 0; // no passive segments currently
Comment thread
CountBleck marked this conversation as resolved.
offs.push(target == Target.Wasm64
? this.i64(i64_low(offset), i64_high(offset))
: this.i32(i64_low(offset))
);
unchecked(sizs[i] = buffer.length);
sizs.push(buffer.length);
}

k -= empty;
let cArr1 = allocPtrArray(segs);
let cArr2 = allocU8Array(psvs);
let cArr3 = allocPtrArray(offs);
Expand Down
13 changes: 5 additions & 8 deletions tests/compiler/assignment-chain.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@
(memory $0 1)
(data $0 (i32.const 12) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00(\00\00\00A\00l\00l\00o\00c\00a\00t\00i\00o\00n\00 \00t\00o\00o\00 \00l\00a\00r\00g\00e\00\00\00\00\00")
(data $1 (i32.const 76) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00 \00\00\00~\00l\00i\00b\00/\00r\00t\00/\00i\00t\00c\00m\00s\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $2 (i32.const 144) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $3 (i32.const 176) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $4 (i32.const 204) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00\00\00\00\00\00\00\00\00")
(data $5 (i32.const 268) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s\00\00\00\00\00\00\00\00\00")
(data $6 (i32.const 320) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $7 (i32.const 348) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $8 (i32.const 412) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00&\00\00\00a\00s\00s\00i\00g\00n\00m\00e\00n\00t\00-\00c\00h\00a\00i\00n\00.\00t\00s\00\00\00\00\00\00\00")
(data $9 (i32.const 480) "\06\00\00\00 \00\00\00 \00\00\00 \00\00\00\00\00\00\00 \00\00\00 \00\00\00")
(data $2 (i32.const 204) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00\00\00\00\00\00\00\00\00")
(data $3 (i32.const 268) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s\00\00\00\00\00\00\00\00\00")
(data $4 (i32.const 348) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $5 (i32.const 412) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00&\00\00\00a\00s\00s\00i\00g\00n\00m\00e\00n\00t\00-\00c\00h\00a\00i\00n\00.\00t\00s\00\00\00\00\00\00\00")
(data $6 (i32.const 480) "\06\00\00\00 \00\00\00 \00\00\00 \00\00\00\00\00\00\00 \00\00\00 \00\00\00")
(table $0 1 1 funcref)
(elem $0 (i32.const 1))
(export "normal_assignment_chain" (func $assignment-chain/normal_assignment_chain))
Expand Down
18 changes: 9 additions & 9 deletions tests/compiler/assignment-chain.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
(data $0.1 (i32.const 1048) "\02\00\00\00(\00\00\00A\00l\00l\00o\00c\00a\00t\00i\00o\00n\00 \00t\00o\00o\00 \00l\00a\00r\00g\00e")
(data $1 (i32.const 1100) "<")
(data $1.1 (i32.const 1112) "\02\00\00\00 \00\00\00~\00l\00i\00b\00/\00r\00t\00/\00i\00t\00c\00m\00s\00.\00t\00s")
(data $4 (i32.const 1228) "<")
(data $4.1 (i32.const 1240) "\02\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
(data $5 (i32.const 1292) ",")
(data $5.1 (i32.const 1304) "\02\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s")
(data $7 (i32.const 1372) "<")
(data $7.1 (i32.const 1384) "\02\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s")
(data $8 (i32.const 1436) "<")
(data $8.1 (i32.const 1448) "\02\00\00\00&\00\00\00a\00s\00s\00i\00g\00n\00m\00e\00n\00t\00-\00c\00h\00a\00i\00n\00.\00t\00s")
(data $9 (i32.const 1504) "\06\00\00\00 \00\00\00 \00\00\00 \00\00\00\00\00\00\00 \00\00\00 ")
(data $2 (i32.const 1228) "<")
(data $2.1 (i32.const 1240) "\02\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
(data $3 (i32.const 1292) ",")
(data $3.1 (i32.const 1304) "\02\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s")
(data $4 (i32.const 1372) "<")
(data $4.1 (i32.const 1384) "\02\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s")
(data $5 (i32.const 1436) "<")
(data $5.1 (i32.const 1448) "\02\00\00\00&\00\00\00a\00s\00s\00i\00g\00n\00m\00e\00n\00t\00-\00c\00h\00a\00i\00n\00.\00t\00s")
(data $6 (i32.const 1504) "\06\00\00\00 \00\00\00 \00\00\00 \00\00\00\00\00\00\00 \00\00\00 ")
(export "normal_assignment_chain" (func $assignment-chain/normal_assignment_chain))
(export "setter_assignment_chain" (func $assignment-chain/setter_assignment_chain))
(export "static_setter_assignment_chain" (func $assignment-chain/static_setter_assignment_chain))
Expand Down
25 changes: 11 additions & 14 deletions tests/compiler/bindings/esm.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,17 @@
(data $6 (i32.const 252) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00&\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00b\00u\00f\00f\00e\00r\00.\00t\00s\00\00\00\00\00\00\00")
(data $7 (i32.const 316) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00(\00\00\00A\00l\00l\00o\00c\00a\00t\00i\00o\00n\00 \00t\00o\00o\00 \00l\00a\00r\00g\00e\00\00\00\00\00")
(data $8 (i32.const 380) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00 \00\00\00~\00l\00i\00b\00/\00r\00t\00/\00i\00t\00c\00m\00s\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $9 (i32.const 448) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $10 (i32.const 480) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $11 (i32.const 508) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00\00\00\00\00\00\00\00\00")
(data $12 (i32.const 572) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s\00\00\00\00\00\00\00\00\00")
(data $13 (i32.const 624) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $14 (i32.const 652) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $15 (i32.const 716) "\1c\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $16 (i32.const 748) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00$\00\00\00~\00l\00i\00b\00/\00t\00y\00p\00e\00d\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00\00\00\00\00\00\00")
(data $17 (i32.const 812) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00&\00\00\00~\00l\00i\00b\00/\00s\00t\00a\00t\00i\00c\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00\00\00\00\00")
(data $18 (i32.const 876) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00")
(data $19 (i32.const 924) "|\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $20 (i32.const 1052) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00*\00\00\00O\00b\00j\00e\00c\00t\00 \00a\00l\00r\00e\00a\00d\00y\00 \00p\00i\00n\00n\00e\00d\00\00\00")
(data $21 (i32.const 1116) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00(\00\00\00O\00b\00j\00e\00c\00t\00 \00i\00s\00 \00n\00o\00t\00 \00p\00i\00n\00n\00e\00d\00\00\00\00\00")
(data $22 (i32.const 1184) "\10\00\00\00 \00\00\00 \00\00\00 \00\00\00\00\00\00\00\00\00\00\00\81\08\00\00\01\19\00\00\01\02\00\00$\t\00\00\a4\00\00\00$\n\00\00\02\t\00\00\02A\00\00\00\00\00\00A\00\00\00 \00\00\00")
(data $9 (i32.const 508) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00\00\00\00\00\00\00\00\00")
(data $10 (i32.const 572) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s\00\00\00\00\00\00\00\00\00")
(data $11 (i32.const 652) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $12 (i32.const 716) "\1c\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $13 (i32.const 748) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00$\00\00\00~\00l\00i\00b\00/\00t\00y\00p\00e\00d\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00\00\00\00\00\00\00")
(data $14 (i32.const 812) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00&\00\00\00~\00l\00i\00b\00/\00s\00t\00a\00t\00i\00c\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00\00\00\00\00")
(data $15 (i32.const 876) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00")
(data $16 (i32.const 924) "|\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data $17 (i32.const 1052) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00*\00\00\00O\00b\00j\00e\00c\00t\00 \00a\00l\00r\00e\00a\00d\00y\00 \00p\00i\00n\00n\00e\00d\00\00\00")
(data $18 (i32.const 1116) "<\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00(\00\00\00O\00b\00j\00e\00c\00t\00 \00i\00s\00 \00n\00o\00t\00 \00p\00i\00n\00n\00e\00d\00\00\00\00\00")
(data $19 (i32.const 1184) "\10\00\00\00 \00\00\00 \00\00\00 \00\00\00\00\00\00\00\00\00\00\00\81\08\00\00\01\19\00\00\01\02\00\00$\t\00\00\a4\00\00\00$\n\00\00\02\t\00\00\02A\00\00\00\00\00\00A\00\00\00 \00\00\00")
(table $0 2 2 funcref)
(elem $0 (i32.const 1) $start:bindings/esm~anonymous|0)
(export "plainGlobal" (global $bindings/esm/plainGlobal))
Expand Down
44 changes: 22 additions & 22 deletions tests/compiler/bindings/esm.release.wat
Loading