porffor 0.0.0-bddcdc3 → 0.0.0-d650361
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 +45 -12
- package/c +0 -0
- package/c.exe +0 -0
- package/compiler/2c.js +350 -0
- package/compiler/builtins.js +6 -1
- package/compiler/codeGen.js +397 -128
- package/compiler/decompile.js +3 -3
- package/compiler/encoding.js +4 -2
- package/compiler/index.js +53 -2
- package/compiler/opt.js +35 -5
- package/compiler/parse.js +1 -0
- package/compiler/prototype.js +92 -33
- package/compiler/sections.js +28 -2
- package/compiler/wrap.js +21 -3
- package/cool.exe +0 -0
- package/g +0 -0
- package/g.exe +0 -0
- package/hi.c +37 -0
- package/out +0 -0
- package/out.exe +0 -0
- package/package.json +1 -1
- package/r.js +39 -0
- package/rhemyn/README.md +37 -0
- package/rhemyn/compile.js +214 -0
- package/rhemyn/parse.js +321 -0
- package/rhemyn/test/parse.js +59 -0
- package/runner/index.js +54 -34
- package/runner/repl.js +3 -10
- package/runner/transform.js +2 -1
- package/runner/version.js +10 -0
- package/tmp.c +58 -0
package/runner/index.js
CHANGED
@@ -1,35 +1,55 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
|
3
|
-
import compile from '../compiler/wrap.js';
|
4
|
-
import fs from 'node:fs';
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
//
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
const
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
}
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
import compile from '../compiler/wrap.js';
|
4
|
+
import fs from 'node:fs';
|
5
|
+
|
6
|
+
if (process.argv.includes('-compile-hints')) {
|
7
|
+
const v8 = await import('node:v8');
|
8
|
+
v8.setFlagsFromString(`--experimental-wasm-compilation-hints`);
|
9
|
+
|
10
|
+
// see also these flags:
|
11
|
+
// --experimental-wasm-branch-hinting
|
12
|
+
// --experimental-wasm-extended-const
|
13
|
+
// --experimental-wasm-inlining (?)
|
14
|
+
// --experimental-wasm-js-inlining (?)
|
15
|
+
// --experimental-wasm-return-call (on by default)
|
16
|
+
}
|
17
|
+
|
18
|
+
const file = process.argv.slice(2).find(x => x[0] !== '-');
|
19
|
+
if (!file) {
|
20
|
+
if (process.argv.includes('-v')) {
|
21
|
+
// just print version
|
22
|
+
console.log((await import('./version.js')).default);
|
23
|
+
process.exit(0);
|
24
|
+
}
|
25
|
+
|
26
|
+
// run repl if no file given
|
27
|
+
await import('./repl.js');
|
28
|
+
|
29
|
+
// do nothing for the rest of this file
|
30
|
+
await new Promise(() => {});
|
31
|
+
}
|
32
|
+
|
33
|
+
const source = fs.readFileSync(file, 'utf8');
|
34
|
+
|
35
|
+
let cache = '';
|
36
|
+
const print = str => {
|
37
|
+
/* cache += str;
|
38
|
+
|
39
|
+
if (str === '\n') {
|
40
|
+
process.stdout.write(cache);
|
41
|
+
cache = '';
|
42
|
+
} */
|
43
|
+
|
44
|
+
process.stdout.write(str);
|
45
|
+
};
|
46
|
+
|
47
|
+
try {
|
48
|
+
const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
|
49
|
+
|
50
|
+
exports.main();
|
51
|
+
if (cache) process.stdout.write(cache);
|
52
|
+
} catch (e) {
|
53
|
+
if (cache) process.stdout.write(cache);
|
54
|
+
console.error(process.argv.includes('-i') ? e : `${e.constructor.name}: ${e.message}`);
|
35
55
|
}
|
package/runner/repl.js
CHANGED
@@ -1,14 +1,7 @@
|
|
1
1
|
import compile from '../compiler/wrap.js';
|
2
|
+
import rev from './version.js';
|
2
3
|
|
3
4
|
import repl from 'node:repl';
|
4
|
-
import fs from 'node:fs';
|
5
|
-
|
6
|
-
let rev = 'unknown';
|
7
|
-
try {
|
8
|
-
rev = fs.readFileSync(new URL('../.git/refs/heads/main', import.meta.url), 'utf8').trim().slice(0, 7);
|
9
|
-
} catch {
|
10
|
-
rev = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version.split('-')[1];
|
11
|
-
}
|
12
5
|
|
13
6
|
// process.argv.push('-O0'); // disable opts
|
14
7
|
|
@@ -48,7 +41,7 @@ const memoryToString = mem => {
|
|
48
41
|
return out;
|
49
42
|
};
|
50
43
|
|
51
|
-
const alwaysPrev = process.argv.includes('-
|
44
|
+
const alwaysPrev = process.argv.includes('-prev');
|
52
45
|
|
53
46
|
let prev = '';
|
54
47
|
const run = async (source, _context, _filename, callback, run = true) => {
|
@@ -56,7 +49,7 @@ const run = async (source, _context, _filename, callback, run = true) => {
|
|
56
49
|
if (alwaysPrev) prev = toRun + ';\n';
|
57
50
|
|
58
51
|
const { exports, wasm, pages } = await compile(toRun, []);
|
59
|
-
fs.writeFileSync('out.wasm', Buffer.from(wasm));
|
52
|
+
// fs.writeFileSync('out.wasm', Buffer.from(wasm));
|
60
53
|
|
61
54
|
if (run && exports.$) {
|
62
55
|
lastMemory = exports.$;
|
package/runner/transform.js
CHANGED
@@ -8,7 +8,8 @@ const source = fs.readFileSync(file, 'utf8');
|
|
8
8
|
const { wasm } = await compile(source);
|
9
9
|
|
10
10
|
// const out = `(async () => { const print = str => process.stdout.write(str); (await WebAssembly.instantiate(Uint8Array.from([${wasm.toString()}]), {'': { p: i => print(i.toString()), c: i => print(String.fromCharCode(i))}})).instance.exports.m()})()`;
|
11
|
-
const out = `new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([${wasm.toString()}])),{'':{p:i=>process.stdout.write(i.toString())}}).exports.m()`;
|
11
|
+
// const out = `new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([${wasm.toString()}])),{'':{p:i=>process.stdout.write(i.toString())}}).exports.m()`;
|
12
|
+
const out = `const a=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([${wasm.toString()}])));const b=a.exports.m();console.log(Array.from(new Uint16Array(a.exports.$.buffer,b+4,new Int32Array(a.exports.$.buffer,b,1))).map(x=>String.fromCharCode(x)).join(''))`;
|
12
13
|
|
13
14
|
console.log(out);
|
14
15
|
eval(out);
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import fs from 'node:fs';
|
2
|
+
|
3
|
+
let rev = 'unknown';
|
4
|
+
try {
|
5
|
+
rev = fs.readFileSync(new URL('../.git/refs/heads/main', import.meta.url), 'utf8').trim().slice(0, 7);
|
6
|
+
} catch {
|
7
|
+
rev = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version.split('-')[1].slice(0, 7);
|
8
|
+
}
|
9
|
+
|
10
|
+
export default rev;
|
package/tmp.c
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#ifdef _WIN32
|
2
|
+
#include <windows.h>
|
3
|
+
#else
|
4
|
+
#include <time.h>
|
5
|
+
#endif
|
6
|
+
|
7
|
+
#include <stdio.h>
|
8
|
+
|
9
|
+
double aux(double n, double acc1, double acc2) {
|
10
|
+
if (n == 0e+0) {
|
11
|
+
return acc1;
|
12
|
+
}
|
13
|
+
if (n == 1e+0) {
|
14
|
+
return acc2;
|
15
|
+
}
|
16
|
+
return aux(n - 1e+0, acc2, acc1 + acc2);
|
17
|
+
}
|
18
|
+
|
19
|
+
double fib(double n) {
|
20
|
+
return aux(n, 0e+0, 1e+0);
|
21
|
+
}
|
22
|
+
|
23
|
+
double test(double n, double count) {
|
24
|
+
double res = 0;
|
25
|
+
double i = 0;
|
26
|
+
|
27
|
+
i = 0e+0;
|
28
|
+
while (i < count) {
|
29
|
+
res = fib(n);
|
30
|
+
i = i + 1e+0;
|
31
|
+
}
|
32
|
+
return res;
|
33
|
+
}
|
34
|
+
|
35
|
+
double inline __performance_now() {
|
36
|
+
double _time_out;
|
37
|
+
#ifdef _WIN32
|
38
|
+
LARGE_INTEGER _time_freq, _time_t;
|
39
|
+
QueryPerformanceFrequency(&_time_freq);
|
40
|
+
QueryPerformanceCounter(&_time_t);
|
41
|
+
_time_out = ((double)_time_t.QuadPart / _time_freq.QuadPart) * 1000.;
|
42
|
+
#else
|
43
|
+
struct timespec _time;
|
44
|
+
clock_gettime(CLOCK_MONOTONIC, &_time);
|
45
|
+
_time_out = _time.tv_nsec / 1000000.;
|
46
|
+
#endif
|
47
|
+
return _time_out;
|
48
|
+
}
|
49
|
+
|
50
|
+
int main() {
|
51
|
+
double t = 0;
|
52
|
+
|
53
|
+
t = __performance_now();
|
54
|
+
// Sleep(1000);
|
55
|
+
printf("%f\n", test(4.6e+1, 1e+7));
|
56
|
+
printf("%f\n", (__performance_now() - t));
|
57
|
+
}
|
58
|
+
|