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
15 changes: 9 additions & 6 deletions src/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,15 @@ function loadStructInfo(filename) {
Object.assign(defines, temp.defines);
}

if (!BOOTSTRAPPING_STRUCT_INFO) {
// Load struct and define information.
if (MEMORY64) {
loadStructInfo(localFile('struct_info_generated_wasm64.json'));
} else {
loadStructInfo(localFile('struct_info_generated.json'));
// Delayed initialization that depends on settings loaded via `applySettings`.
export function initModules() {
if (!BOOTSTRAPPING_STRUCT_INFO) {
// Load struct and define information.
if (MEMORY64) {
loadStructInfo(localFile('struct_info_generated_wasm64.json'));
} else {
loadStructInfo(localFile('struct_info_generated.json'));
}
}
}

Expand Down
60 changes: 34 additions & 26 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,41 @@ function isNiceIdent(ident) {
return /^\(?[$_]+[\w$_\d ]*\)?$/.test(ident);
}

export const POINTER_SIZE = MEMORY64 ? 8 : 4;
const POINTER_MAX = MEMORY64 ? 'Number.MAX_SAFE_INTEGER' : '0xFFFFFFFF';
export let POINTER_SIZE;
const STACK_ALIGN = 16;
const POINTER_BITS = POINTER_SIZE * 8;
const POINTER_TYPE = `u${POINTER_BITS}`;
const POINTER_JS_TYPE = MEMORY64 ? "'bigint'" : "'number'";
const POINTER_SHIFT = MEMORY64 ? '3' : '2';
const POINTER_HEAP = MEMORY64 ? 'HEAP64' : 'HEAP32';
const LONG_TYPE = `i${POINTER_BITS}`;

const SIZE_TYPE = POINTER_TYPE;

// Similar to POINTER_TYPE, but this is the actual wasm type that is
// used in practice, while POINTER_TYPE is the more refined internal
// type (that is unsigned, where as core wasm does not have unsigned
// types).
const POINTER_WASM_TYPE = `i${POINTER_BITS}`;
let POINTER_TYPE;
Comment thread
sbc100 marked this conversation as resolved.
Resolved

// Delayed initialization that depends on settings loaded via `applySettings`.
export function initParseTools() {
POINTER_SIZE = MEMORY64 ? 8 : 4;
const POINTER_MAX = MEMORY64 ? 'Number.MAX_SAFE_INTEGER' : '0xFFFFFFFF';
const POINTER_BITS = POINTER_SIZE * 8;
POINTER_TYPE = `u${POINTER_BITS}`;
const POINTER_JS_TYPE = MEMORY64 ? "'bigint'" : "'number'";
const POINTER_SHIFT = MEMORY64 ? '3' : '2';
const POINTER_HEAP = MEMORY64 ? 'HEAP64' : 'HEAP32';
const LONG_TYPE = `i${POINTER_BITS}`;
const SIZE_TYPE = POINTER_TYPE;

// Similar to POINTER_TYPE, but this is the actual wasm type that is
// used in practice, while POINTER_TYPE is the more refined internal
// type (that is unsigned, where as core wasm does not have unsigned
// types).
const POINTER_WASM_TYPE = `i${POINTER_BITS}`;

addToCompileTimeContext({
LONG_TYPE,
POINTER_HEAP,
POINTER_BITS,
POINTER_JS_TYPE,
POINTER_MAX,
POINTER_SHIFT,
POINTER_SIZE,
POINTER_TYPE,
POINTER_WASM_TYPE,
SIZE_TYPE,
});
}

function isPointerType(type) {
return type.endsWith('*');
Expand Down Expand Up @@ -1235,16 +1253,6 @@ addToCompileTimeContext({
ATMAINS,
ATPOSTRUNS,
FOUR_GB,
LONG_TYPE,
POINTER_HEAP,
POINTER_BITS,
POINTER_JS_TYPE,
POINTER_MAX,
POINTER_SHIFT,
POINTER_SIZE,
POINTER_TYPE,
POINTER_WASM_TYPE,
SIZE_TYPE,
STACK_ALIGN,
TARGET_NOT_SUPPORTED,
WASM_PAGE_SIZE,
Expand Down
27 changes: 15 additions & 12 deletions src/parseTools_legacy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ function makeMalloc(source, param) {
return `_malloc(${param})`;
}

const Runtime = {
POINTER_SIZE,
QUANTUM_SIZE: POINTER_SIZE,
};

// Legacy name for runIfMainThread.
const runOnMainThread = runIfMainThread;

addToCompileTimeContext({
Runtime,
makeMalloc,
receiveI64ParamAsDouble,
receiveI64ParamAsI32s,
runOnMainThread,
});
// Delayed initialization that depends on `POINTER_SIZE` from `initParseTools`.
export function initLegacyParseTools() {
const Runtime = {
POINTER_SIZE,
QUANTUM_SIZE: POINTER_SIZE,
};

addToCompileTimeContext({
Runtime,
makeMalloc,
receiveI64ParamAsDouble,
receiveI64ParamAsI32s,
runOnMainThread,
});
}
22 changes: 8 additions & 14 deletions tools/compiler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
readFile,
timer,
} from '../src/utility.mjs';
import {initModules} from '../src/modules.mjs';
import {initParseTools} from '../src/parseTools.mjs';
import {initLegacyParseTools} from '../src/parseTools_legacy.mjs';
import {runJSify} from '../src/jsifier.mjs';

timer.start('startup')

Expand Down Expand Up @@ -73,21 +77,11 @@ assert(
'JS compiler should only run on side modules if asyncify is used.',
);

// Load compiler code

timer.start('dynamic imports')

// We can't use static import statements here because several of these
// file depend on having the settings defined in the global scope (which
// we do dynamically above.
await import('../src/modules.mjs');
await import('../src/parseTools.mjs');
initModules();
initParseTools();
if (!STRICT) {
await import('../src/parseTools_legacy.mjs');
initLegacyParseTools();
}
const jsifier = await import('../src/jsifier.mjs');

timer.stop('dynamic imports')

timer.stop('startup')

Expand All @@ -97,7 +91,7 @@ timer.stop('startup')

try {
timer.start('runJSify')
await jsifier.runJSify(values.output, symbolsOnly);
await runJSify(values.output, symbolsOnly);
timer.stop('runJSify')
} catch (err) {
if (err.toString().includes('Aborting compilation due to previous errors')) {
Expand Down
15 changes: 7 additions & 8 deletions tools/preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import assert from 'node:assert';
import {parseArgs} from 'node:util';

import {readFile, loadDefaultSettings, applySettings} from '../src/utility.mjs';
import {initParseTools, preprocess, processMacros} from '../src/parseTools.mjs';
import {initModules} from '../src/modules.mjs';

const options = {
'expand-macros': {type: 'boolean'},
Expand Down Expand Up @@ -45,16 +47,13 @@ if (settingsFile == '-') {
const userSettings = JSON.parse(readFile(settingsFile));
applySettings(userSettings);

const inputFile = positionals[1];
initModules();
initParseTools();

// We can't use static import statements here because several of these
// file depend on having the settings defined in the global scope (which
// we do dynamically above.
const parseTools = await import('../src/parseTools.mjs');
await import('../src/modules.mjs');
const inputFile = positionals[1];

let output = parseTools.preprocess(inputFile);
let output = preprocess(inputFile);
if (values['expand-macros']) {
output = parseTools.processMacros(output, inputFile);
output = processMacros(output, inputFile);
}
process.stdout.write(output);
Loading