porffor 0.0.0-d650361 → 0.0.0-e975d7a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/compiler/2c.js +349 -349
- package/compiler/codeGen.js +180 -47
- package/compiler/embedding.js +9 -5
- package/compiler/index.js +3 -3
- package/compiler/opt.js +14 -18
- package/compiler/prototype.js +83 -4
- package/compiler/sections.js +19 -4
- package/package.json +1 -1
- package/runner/info.js +37 -2
package/compiler/sections.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { Valtype, FuncType, Empty, ExportDesc, Section, Magic, ModuleVersion, Opcodes, PageSize } from './wasmSpec.js';
|
2
|
-
import { encodeVector, encodeString, encodeLocal } from './encoding.js';
|
2
|
+
import { encodeVector, encodeString, encodeLocal, unsignedLEB128, signedLEB128 } from './encoding.js';
|
3
3
|
import { number } from './embedding.js';
|
4
4
|
import { importedFuncs } from './builtins.js';
|
5
5
|
|
@@ -20,7 +20,7 @@ const chHint = (topTier, baselineTier, strategy) => {
|
|
20
20
|
return (strategy | (baselineTier << 2) | (topTier << 4));
|
21
21
|
};
|
22
22
|
|
23
|
-
export default (funcs, globals, tags, pages, flags) => {
|
23
|
+
export default (funcs, globals, tags, pages, data, flags) => {
|
24
24
|
const types = [], typeCache = {};
|
25
25
|
|
26
26
|
const optLevel = parseInt(process.argv.find(x => x.startsWith('-O'))?.[2] ?? 1);
|
@@ -105,6 +105,8 @@ export default (funcs, globals, tags, pages, flags) => {
|
|
105
105
|
|
106
106
|
const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === 'main' ? 'm' : x.name), ExportDesc.func, x.index ]);
|
107
107
|
|
108
|
+
if (process.argv.includes('-always-memory') && pages.size === 0) pages.set('-always-memory', 0);
|
109
|
+
|
108
110
|
const usesMemory = pages.size > 0;
|
109
111
|
const memorySection = !usesMemory ? [] : createSection(
|
110
112
|
Section.memory,
|
@@ -155,13 +157,24 @@ export default (funcs, globals, tags, pages, flags) => {
|
|
155
157
|
encodeVector(types)
|
156
158
|
);
|
157
159
|
|
160
|
+
const dataSection = data.length === 0 ? [] : createSection(
|
161
|
+
Section.data,
|
162
|
+
encodeVector(data.map(x => [ 0x00, Opcodes.i32_const, ...signedLEB128(x.offset), Opcodes.end, ...encodeVector(x.bytes) ]))
|
163
|
+
);
|
164
|
+
|
165
|
+
const dataCountSection = data.length === 0 ? [] : createSection(
|
166
|
+
Section.data_count,
|
167
|
+
unsignedLEB128(data.length)
|
168
|
+
);
|
169
|
+
|
158
170
|
if (process.argv.includes('-sections')) console.log({
|
159
171
|
typeSection: typeSection.map(x => x.toString(16)),
|
160
172
|
importSection: importSection.map(x => x.toString(16)),
|
161
173
|
funcSection: funcSection.map(x => x.toString(16)),
|
162
174
|
globalSection: globalSection.map(x => x.toString(16)),
|
163
175
|
exportSection: exportSection.map(x => x.toString(16)),
|
164
|
-
codeSection: codeSection.map(x => x.toString(16))
|
176
|
+
codeSection: codeSection.map(x => x.toString(16)),
|
177
|
+
dataSection: dataSection.map(x => x.toString(16)),
|
165
178
|
});
|
166
179
|
|
167
180
|
return Uint8Array.from([
|
@@ -175,6 +188,8 @@ export default (funcs, globals, tags, pages, flags) => {
|
|
175
188
|
...tagSection,
|
176
189
|
...globalSection,
|
177
190
|
...exportSection,
|
178
|
-
...
|
191
|
+
...dataCountSection,
|
192
|
+
...codeSection,
|
193
|
+
...dataSection
|
179
194
|
]);
|
180
195
|
};
|
package/package.json
CHANGED
package/runner/info.js
CHANGED
@@ -36,7 +36,7 @@ const print = str => {
|
|
36
36
|
};
|
37
37
|
|
38
38
|
const t0 = performance.now();
|
39
|
-
const { wasm, exports } = await compile(source, raw ? [ 'module' ] : [ 'module', 'info' ], {}, print);
|
39
|
+
const { wasm, exports, pages } = await compile(source, raw ? [ 'module' ] : [ 'module', 'info' ], {}, print);
|
40
40
|
|
41
41
|
if (!raw && typeof Deno === 'undefined') fs.writeFileSync('out.wasm', Buffer.from(wasm));
|
42
42
|
|
@@ -51,4 +51,39 @@ if (!process.argv.includes('-no-run')) {
|
|
51
51
|
}
|
52
52
|
|
53
53
|
if (!raw) console.log(bold(`wasm binary is ${wasm.byteLength} bytes`));
|
54
|
-
if (!raw) console.log(`total: ${(performance.now() - t0).toFixed(2)}ms`);
|
54
|
+
if (!raw) console.log(`total: ${(performance.now() - t0).toFixed(2)}ms`);
|
55
|
+
|
56
|
+
if (!raw && process.argv.includes('-mem') && exports.$) {
|
57
|
+
console.log();
|
58
|
+
|
59
|
+
let lastMemory, lastPages;
|
60
|
+
const PageSize = 65536;
|
61
|
+
const memoryToString = mem => {
|
62
|
+
let out = '';
|
63
|
+
const pages = lastPages.length;
|
64
|
+
const wasmPages = mem.buffer.byteLength / PageSize;
|
65
|
+
|
66
|
+
out += `\x1B[1mallocated ${mem.buffer.byteLength / 1024}KiB\x1B[0m for ${pages} things using ${wasmPages} Wasm page${wasmPages === 1 ? '' : 's'}\n`;
|
67
|
+
|
68
|
+
const buf = new Uint8Array(mem.buffer);
|
69
|
+
|
70
|
+
for (let i = 0; i < pages; i++) {
|
71
|
+
out += `\x1B[36m${lastPages[i]}\x1B[2m | \x1B[0m`;
|
72
|
+
|
73
|
+
for (let j = 0; j < 50; j++) {
|
74
|
+
const val = buf[i * pageSize + j];
|
75
|
+
if (val === 0) out += '\x1B[2m';
|
76
|
+
out += val.toString(16).padStart(2, '0');
|
77
|
+
if (val === 0) out += '\x1B[0m';
|
78
|
+
out += ' ';
|
79
|
+
}
|
80
|
+
out += '\n';
|
81
|
+
}
|
82
|
+
|
83
|
+
return out;
|
84
|
+
};
|
85
|
+
|
86
|
+
lastPages = [...pages.keys()];
|
87
|
+
lastMemory = exports.$;
|
88
|
+
console.log(memoryToString(lastMemory));
|
89
|
+
}
|