Skip to content

Commit

Permalink
compiler: fix functions named main breaking things
Browse files Browse the repository at this point in the history
oops! fixes #204.
  • Loading branch information
CanadaHonk committed Oct 19, 2024
1 parent 3e5d768 commit d991c7c
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 22 deletions.
10 changes: 5 additions & 5 deletions compiler/2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
const typedReturns = f.returnType == null;

const shouldInline = false; // f.internal;
if (f.name === 'main') out += `int main(${prependMain.has('argv') ? 'int argc, char* argv[]' : ''}) {\n`;
if (f.name === '#main') out += `int main(${prependMain.has('argv') ? 'int argc, char* argv[]' : ''}) {\n`;
else out += `${!typedReturns ? (returns ? CValtype[f.returns[0]] : 'void') : 'struct ReturnValue'} ${shouldInline ? 'inline ' : ''}${sanitize(f.name)}(${f.params.map((x, i) => `${CValtype[x]} ${invLocals[i]}`).join(', ')}) {\n`;

if (f.name === '__Porffor_promise_runJobs') {
Expand All @@ -352,7 +352,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
return;
}

if (f.name === 'main') {
if (f.name === '#main') {
out += ' ' + [...prependMain.values()].join('\n ');
if (prependMain.size > 0) out += '\n\n';
}
Expand Down Expand Up @@ -881,7 +881,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
line(`return ${vals.pop()}`);
}

if (f.name === 'main') {
if (f.name === '#main') {
out += '\n';
line(`return 0`);
}
Expand All @@ -891,14 +891,14 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
globalThis.out = globalThis.out + out;
};

cify(funcs.find(x => x.name === 'main'));
cify(funcs.find(x => x.name === '#main'));

const rawParams = f => {
if (ffiFuncs[f.name]) return ffiFuncs[f.name].parameters;
return f.params;
};

prepend.set('func decls', funcs.filter(x => x.name !== 'main' && cified.has(x.name)).map(f => {
prepend.set('func decls', funcs.filter(x => x.name !== '#main' && cified.has(x.name)).map(f => {
const returns = f.returns.length > 0;
const typedReturns = f.returnType == null;

Expand Down
2 changes: 1 addition & 1 deletion compiler/allocator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const pagePtr = ind => {

export const nameToReason = (scope, name) => {
let scopeName = scope.name;
if (globalThis.precompile && scopeName === 'main') scopeName = globalThis.precompile;
if (globalThis.precompile && scopeName === '#main') scopeName = globalThis.precompile;

return `${Prefs.scopedPageNames ? (scopeName + '/') : ''}${name}`;
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/assemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export default (funcs, globals, tags, pages, data, noTreeshake = false) => {
);
time('memory section');

const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === 'main' ? 'm' : x.name), ExportDesc.func, ...unsignedLEB128(x.asmIndex) ]);
const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === '#main' ? 'm' : x.name), ExportDesc.func, ...unsignedLEB128(x.asmIndex) ]);

// export memory if used
if (usesMemory) exports.unshift([ ...encodeString('$'), ExportDesc.mem, 0x00 ]);
Expand Down
19 changes: 9 additions & 10 deletions compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ const generateIdent = (scope, decl) => {
}

if (local?.idx === undefined) {
if (name === 'arguments' && scope.name !== 'main' && !scope.arrow) {
if (name === 'arguments' && scope.name !== '#main' && !scope.arrow) {
// todo: not compliant
let len = countLength(scope);
const names = new Array(len);
Expand Down Expand Up @@ -1352,7 +1352,7 @@ const getType = (scope, name, failEarly = false) => {
return fallback;
}

if (name === 'arguments' && scope.name !== 'main' && !scope.arrow) {
if (name === 'arguments' && scope.name !== '#main' && !scope.arrow) {
return number(TYPES.array, Valtype.i32);
}

Expand Down Expand Up @@ -3075,14 +3075,13 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
}
}

const topLevel = scope.name === 'main';

if (typeof pattern === 'string') {
pattern = { type: 'Identifier', name: pattern };
}

// todo: handle globalThis.foo = ...

const topLevel = scope.name === '#main';
if (pattern.type === 'Identifier') {
let out = [];
const name = pattern.name;
Expand Down Expand Up @@ -3314,7 +3313,7 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
const generateVar = (scope, decl) => {
let out = [];

const topLevel = scope.name === 'main';
const topLevel = scope.name === '#main';

// global variable if in top scope (main) or if internally wanted
const global = decl._global ?? (topLevel || decl._bare);
Expand Down Expand Up @@ -4175,7 +4174,7 @@ const generateForOf = (scope, decl) => {
setVar = generateVarDstr(scope, 'var', decl.left, { type: 'Identifier', name: tmpName }, undefined, true);
} else {
// todo: verify this is correct
const global = scope.name === 'main' && decl.left.kind === 'var';
const global = scope.name === '#main' && decl.left.kind === 'var';
setVar = generateVarDstr(scope, decl.left.kind, decl.left?.declarations?.[0]?.id ?? decl.left, { type: 'Identifier', name: tmpName }, undefined, global);
}

Expand Down Expand Up @@ -4537,7 +4536,7 @@ const generateForIn = (scope, decl) => {
setVar = generateVarDstr(scope, 'var', decl.left, { type: 'Identifier', name: tmpName }, undefined, true);
} else {
// todo: verify this is correct
const global = scope.name === 'main' && decl.left.kind === 'var';
const global = scope.name === '#main' && decl.left.kind === 'var';
setVar = generateVarDstr(scope, decl.left.kind, decl.left?.declarations?.[0]?.id ?? decl.left, { type: 'Identifier', name: tmpName }, undefined, global);
}

Expand Down Expand Up @@ -6245,7 +6244,7 @@ const generateFunc = (scope, decl, forceNoExpr = false) => {
ensureTag();
}

if (name === 'main') {
if (name === '#main') {
func.gotLastType = true;
func.export = true;

Expand Down Expand Up @@ -6358,7 +6357,7 @@ const generateFunc = (scope, decl, forceNoExpr = false) => {
func.jsLength = jsLength;

// force generate for main
if (name === 'main') func.generate();
if (name === '#main') func.generate();

// force generate all for precompile
if (globalThis.precompile) func.generate();
Expand Down Expand Up @@ -6537,7 +6536,7 @@ export default program => {
const getObjectName = x => x.startsWith('__') && x.slice(2, x.indexOf('_', 2));
objectHackers = ['assert', 'compareArray', 'Test262Error', ...new Set(Object.keys(builtinFuncs).map(getObjectName).concat(Object.keys(builtinVars).map(getObjectName)).filter(x => x))];

program.id = { name: 'main' };
program.id = { name: '#main' };

program.body = {
type: 'BlockStatement',
Expand Down
2 changes: 1 addition & 1 deletion compiler/pgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export const run = obj => {
log = '';
for (const x of funcs) {
// skip pgo opt for main()
if (x.name === 'main') continue;
if (x.name === '#main') continue;

const wasmFunc = wasmFuncs.find(y => y.name === x.name);

Expand Down
4 changes: 2 additions & 2 deletions compiler/precompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const compile = async (file, _funcs) => {
return acc;
}, {});

const main = funcs.find(x => x.name === 'main');
const exports = funcs.filter(x => x.export && x.name !== 'main');
const main = funcs.find(x => x.name === '#main');
const exports = funcs.filter(x => x.export && x.name !== '#main');
for (const x of exports) {
if (x.data) {
x.data = x.data.reduce((acc, x) => { acc[data[x].page] = data[x].bytes; return acc; }, {});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "porffor",
"description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
"version": "0.49.2",
"version": "0.49.3",
"author": "CanadaHonk",
"license": "MIT",
"scripts": {},
Expand Down
2 changes: 1 addition & 1 deletion runner/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import fs from 'node:fs';
globalThis.version = '0.49.2';
globalThis.version = '0.49.3';

// deno compat
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
Expand Down

0 comments on commit d991c7c

Please sign in to comment.