Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve command line interface #179

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 asur/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ paused = _paused;`);
});

export const instantiate = async (binary, importImpls) => {
const _vm = process?.argv?.includes('--wasm-debug') ? await wasmDebugVm() : vm;
const _vm = Options.wasmDebug ? await wasmDebugVm() : vm;

const parsed = parse(binary);
const exports = {};
Expand Down
10 changes: 5 additions & 5 deletions compiler/2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct ReturnValue {

// all:
// immediates: ['align', 'offset']
const CMemFuncs = Prefs['2cMemcpy'] ? {
const CMemFuncs = Options['2cMemcpy'] ? {
[Opcodes.i32_store]: {
c: `memcpy(_memory + offset + pointer, &value, sizeof(value));`,
args: ['pointer', 'value'],
Expand Down Expand Up @@ -214,13 +214,13 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {

if (pages.size > 0) {
prepend.set('_memory', `char _memory[${pages.size * pageSize}];\n`);
if (Prefs['2cMemcpy']) includes.set('string.h', true);
if (Options['2cMemcpy']) includes.set('string.h', true);
}

const activeData = data.filter(x => x.page != null);
if (activeData.length > 0) {
const dataOffset = x => pages.get(x.page).ind * pageSize;
if (Prefs['2cMemcpy']) {
if (Options['2cMemcpy']) {
prependMain.set('_data', activeData.map(x => `memcpy(_memory + ${dataOffset(x)}, (unsigned char[]){${x.bytes.join(',')}}, ${x.bytes.length});`).join('\n '));
includes.set('string.h', true);
} else {
Expand Down Expand Up @@ -319,7 +319,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {

let localTmpId = 0;
const localGet = idx => {
if (Prefs['2cDirectLocalGet']) {
if (Options['2cDirectLocalGet']) {
// this just does local.get via the variable name
// nice but does not get the value at this moment
// so breaks some wasm principles :(
Expand Down Expand Up @@ -817,7 +817,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
break;
}

if (Prefs.d) log.warning('2c', `unimplemented op: ${invOpcodes[i[0]]} \x1b[90m(${f.name})`);
if (Options.debugInfo) log.warning('2c', `unimplemented op: ${invOpcodes[i[0]]} \x1b[90m(${f.name})`);
}

lastCond = false;
Expand Down
8 changes: 4 additions & 4 deletions compiler/allocators.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class StaticAllocator {
alloc({ scope, pages }, name, { itemType }) {
let scopeName = scope.name;
if (globalThis.precompile && scopeName === 'main') scopeName = globalThis.precompile;
const reason = `${this.allocType(itemType)}: ${Prefs.scopedPageNames ? (scopeName + '/') : ''}${name}`;
const reason = `${this.allocType(itemType)}: ${Options.scopedPageNames ? (scopeName + '/') : ''}${name}`;

this.lastName = reason;
if (pages.has(reason)) {
Expand All @@ -63,7 +63,7 @@ export class StaticAllocator {

export class GrowAllocator {
constructor() {
Prefs.rmUnusedTypes = false;
Options.rmUnusedTypes = false;
}

alloc() {
Expand All @@ -81,11 +81,11 @@ export class GrowAllocator {

export class ChunkAllocator {
constructor(chunkSize) {
Prefs.rmUnusedTypes = false;
Options.rmUnusedTypes = false;

// 64KiB * chunk size each growth
// 16: 1MiB chunks
this.chunkSize = chunkSize ?? Prefs.chunkAllocatorSize ?? 16;
this.chunkSize = chunkSize ?? Options.chunkAllocatorSize ?? 16;
}

alloc({ asmFunc, funcIndex }) {
Expand Down
22 changes: 11 additions & 11 deletions compiler/assemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ const encodeNames = funcs => {
export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) => {
const types = [], typeCache = {};

const optLevel = parseInt(process.argv.find(x => x.startsWith('-O'))?.[2] ?? 1);
const optLevel = Options.optLevel;

const compileHints = Prefs.compileHints;
const compileHints = Options.compileHints;
if (compileHints) log.warning('assemble', 'compile hints is V8 only w/ experimental arg! (you used -compile-hints)');

const getType = (params, returns) => {
const hash = `${params.join(',')}_${returns.join(',')}`;
if (Prefs.optLog) log('assemble', `getType(${JSON.stringify(params)}, ${JSON.stringify(returns)}) -> ${hash} | cache: ${typeCache[hash]}`);
if (Options.optLog) log('assemble', `getType(${JSON.stringify(params)}, ${JSON.stringify(returns)}) -> ${hash} | cache: ${typeCache[hash]}`);
if (optLevel >= 1 && typeCache[hash] !== undefined) return typeCache[hash];

const type = [ FuncType, ...encodeVector(params), ...encodeVector(returns) ];
Expand All @@ -70,14 +70,14 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =

let t = performance.now();
const time = msg => {
if (!Prefs.profileAssemble) return;
if (!Options.profileAssemble) return;

console.log(`${' '.repeat(50)}\r[${(performance.now() - t).toFixed(2)}ms] ${msg}`);
t = performance.now();
};

let importFuncs = [], importDelta = 0;
if (optLevel < 1 || !Prefs.treeshakeWasmImports || noTreeshake) {
if (optLevel < 1 || !Options.treeshakeWasmImports || noTreeshake) {
importFuncs = importedFuncs;
} else {
let imports = new Map();
Expand Down Expand Up @@ -105,7 +105,7 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =

time('treeshake import funcs');

if (Prefs.optLog) log('assemble', `treeshake: using ${importFuncs.length}/${importedFuncs.length} imports`);
if (Options.optLog) log('assemble', `treeshake: using ${importFuncs.length}/${importedFuncs.length} imports`);

const importSection = importFuncs.length === 0 ? [] : createSection(
Section.import,
Expand All @@ -119,7 +119,7 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
);
time('func section');

const nameSection = Prefs.d ? customSection('name', encodeNames(funcs)) : [];
const nameSection = Options.debugInfo ? customSection('name', encodeNames(funcs)) : [];

const tableSection = !funcs.table ? [] : createSection(
Section.table,
Expand Down Expand Up @@ -232,7 +232,7 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
}
time('global section');

if (Prefs.alwaysMemory && pages.size === 0) pages.set('--always-memory', 0);
if (Options.alwaysMemory && pages.size === 0) pages.set('--always-memory', 0);
if (optLevel === 0) pages.set('O0 precaution', 0);

const usesMemory = pages.size > 0;
Expand Down Expand Up @@ -285,7 +285,7 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
if (typeCount !== 0) localDecl.push(encodeLocal(typeCount, lastType));
// time(' localDecl gen');

const makeAssembled = Prefs.d;
const makeAssembled = Options.debugInfo;
let wasm = [], wasmNonFlat = [];
for (let i = 0; i < x.wasm.length; i++) {
let o = x.wasm[i];
Expand Down Expand Up @@ -369,7 +369,7 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
const dataSection = data.length === 0 ? [] : createSection(
Section.data,
encodeVector(data.map(x => {
if (Prefs.d && x.bytes.length > PageSize) log.warning('assemble', `data (${x.page}) has more bytes than Wasm page size! (${x.bytes.length})`);
if (Options.debugInfo && x.bytes.length > PageSize) log.warning('assemble', `data (${x.page}) has more bytes than Wasm page size! (${x.bytes.length})`);

const bytes = unsignedLEB128(x.bytes.length).concat(x.bytes);
if (x.page != null) {
Expand All @@ -393,7 +393,7 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
);
time('datacount section');

if (Prefs.sections) console.log({
if (Options.sections) console.log({
typeSection: typeSection.map(x => x.toString(16)),
importSection: importSection.map(x => x.toString(16)),
funcSection: funcSection.map(x => x.toString(16)),
Expand Down
6 changes: 3 additions & 3 deletions compiler/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const BuiltinVars = function(ctx) {
this.__Float32Array_BYTES_PER_ELEMENT = number(4);
this.__Float64Array_BYTES_PER_ELEMENT = number(8);

ObjectBuiltins.call(this, ctx, Prefs);
ObjectBuiltins.call(this, ctx, Options);
};

export const BuiltinFuncs = function() {
Expand Down Expand Up @@ -670,9 +670,9 @@ export const BuiltinFuncs = function() {
[ Opcodes.local_get, 0 ],
]
}
})[Prefs.prng ?? 'xorshift128+'];
})[Options.prng ?? 'xorshift128+'];

if (!prng) throw new Error(`unknown prng algo: ${Prefs.prng}`);
if (!prng) throw new Error(`unknown prng algo: ${Options.prng}`);

this.__Math_random = {
floatOnly: true,
Expand Down
4 changes: 2 additions & 2 deletions compiler/builtins_objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Blocktype, Opcodes, PageSize, Valtype } from './wasmSpec.js';
import { TYPES } from './types.js';
import { number } from './embedding.js';

export default function({ builtinFuncs }, Prefs) {
export default function({ builtinFuncs }, Options) {
const makePrefix = name => (name.startsWith('__') ? '' : '__') + name + '_';

const done = new Set();
Expand Down Expand Up @@ -289,7 +289,7 @@ export default function({ builtinFuncs }, Prefs) {
}, enumerableGlobals)
});

if (Prefs.logMissingObjects) for (const x of Object.keys(builtinFuncs).concat(Object.keys(this))) {
if (Options.logMissingObjects) for (const x of Object.keys(builtinFuncs).concat(Object.keys(this))) {
if (!x.startsWith('__')) continue;

const name = x.split('_').slice(2, -1).join('_');
Expand Down
Loading