porffor 0.2.0-77e30e8 → 0.2.0-964b4c2

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  # Porffor &nbsp;<sup><sub>/ˈpɔrfɔr/ &nbsp;*(poor-for)*</sup></sub>
2
- A from-scratch experimental *AOT* optimizing JS -> Wasm/C engine/compiler/runtime in JS. Not serious/intended for (real) use. (this is a straight forward, honest readme)<br>
2
+ A from-scratch experimental **AOT** optimizing JS -> Wasm/C engine/compiler/runtime in JS. Not serious/intended for (real) use. (this is a straight forward, honest readme)<br>
3
3
  Age: ~6 months (very on and off)
4
4
 
5
5
  ## Design
package/compiler/2c.js CHANGED
@@ -35,7 +35,10 @@ const todo = msg => {
35
35
  throw new TodoError(`todo: ${msg}`);
36
36
  };
37
37
 
38
- const removeBrackets = str => str.startsWith('(') && str.endsWith(')') ? str.slice(1, -1) : str;
38
+ const removeBrackets = str => {
39
+ if (str.startsWith('(long)(unsigned long)')) return '(long)(unsigned long)(' + removeBrackets(str.slice(22, -1)) + ')';
40
+ return str.startsWith('(') && str.endsWith(')') ? str.slice(1, -1) : str;
41
+ };
39
42
 
40
43
  export default ({ funcs, globals, tags, exceptions, pages }) => {
41
44
  const invOperatorOpcode = Object.values(operatorOpcode).reduce((acc, x) => {
@@ -116,13 +116,12 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
116
116
  return highlightAsm(out);
117
117
  };
118
118
 
119
- export const highlightAsm = asm =>
120
- asm
121
- .replace(/(local|global|memory)\.[^\s]*/g, _ => `\x1B[31m${_}\x1B[0m`)
122
- .replace(/(i(8|16|32|64)x[0-9]+|v128)(\.[^\s]*)?/g, _ => `\x1B[34m${_}\x1B[0m`)
123
- .replace(/[^m](i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `${_[0]}\x1B[36m${_.slice(1)}\x1B[0m`)
124
- .replace(/(return_call|call|br_if|br|return|throw|rethrow)/g, _ => `\x1B[35m${_}\x1B[0m`)
125
- .replace(/(block|loop|if|end|else|try|catch_all|catch|delegate)/g, _ => `\x1B[95m${_}\x1B[0m`)
126
- .replace(/unreachable/g, _ => `\x1B[91m${_}\x1B[0m`)
127
- .replace(/ \-?[0-9\.]+/g, _ => ` \x1B[33m${_.slice(1)}\x1B[0m`)
128
- .replace(/ ;;.*$/gm, _ => `\x1B[90m${_.replaceAll(/\x1B\[[0-9]+m/g, '')}\x1B[0m`);
119
+ export const highlightAsm = asm => asm
120
+ .replace(/(local|global|memory)\.[^\s]*/g, _ => `\x1B[31m${_}\x1B[0m`)
121
+ .replace(/(i(8|16|32|64)x[0-9]+|v128)(\.[^\s]*)?/g, _ => `\x1B[34m${_}\x1B[0m`)
122
+ .replace(/[^m](i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `${_[0]}\x1B[36m${_.slice(1)}\x1B[0m`)
123
+ .replace(/(return_call|call|br_if|br|return|rethrow|throw)/g, _ => `\x1B[35m${_}\x1B[0m`)
124
+ .replace(/(block|loop|if|end|else|try|catch_all|catch|delegate)/g, _ => `\x1B[95m${_}\x1B[0m`)
125
+ .replace(/unreachable/g, _ => `\x1B[91m${_}\x1B[0m`)
126
+ .replace(/ \-?[0-9\.]+/g, _ => ` \x1B[33m${_.slice(1)}\x1B[0m`)
127
+ .replace(/ ;;.*$/gm, _ => `\x1B[90m${_.replaceAll(/\x1B\[[0-9]+m/g, '')}\x1B[0m`);
@@ -105,119 +105,5 @@ export const read_unsignedLEB128 = _input => {
105
105
  };
106
106
 
107
107
  // ieee 754 binary64
108
-
109
- // from https://github.com/feross/ieee754
110
- // BSD 3-Clause. Copyright 2008 Fair Oaks Labs, Inc. (https://github.com/feross/ieee754/blob/master/LICENSE)
111
- export const ieee754_binary64 = value => {
112
- return [...new Uint8Array(new Float64Array([ value ]).buffer)];
113
-
114
- let isLE = true, mLen = 52, nBytes = 8, offset = 0;
115
- let buffer = new Array(nBytes).fill(0);
116
-
117
- let e, m, c
118
- let eLen = (nBytes * 8) - mLen - 1
119
- const eMax = (1 << eLen) - 1
120
- const eBias = eMax >> 1
121
- const rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
122
- let i = isLE ? 0 : (nBytes - 1)
123
- const d = isLE ? 1 : -1
124
- const s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
125
-
126
- value = Math.abs(value)
127
-
128
- if (isNaN(value) || value === Infinity) {
129
- m = isNaN(value) ? 1 : 0
130
- e = eMax
131
- } else {
132
- e = Math.floor(Math.log(value) / Math.LN2)
133
- if (value * (c = Math.pow(2, -e)) < 1) {
134
- e--
135
- c *= 2
136
- }
137
- if (e + eBias >= 1) {
138
- value += rt / c
139
- } else {
140
- value += rt * Math.pow(2, 1 - eBias)
141
- }
142
- if (value * c >= 2) {
143
- e++
144
- c /= 2
145
- }
146
-
147
- if (e + eBias >= eMax) {
148
- m = 0
149
- e = eMax
150
- } else if (e + eBias >= 1) {
151
- m = ((value * c) - 1) * Math.pow(2, mLen)
152
- e = e + eBias
153
- } else {
154
- m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
155
- e = 0
156
- }
157
- }
158
-
159
- while (mLen >= 8) {
160
- buffer[offset + i] = m & 0xff
161
- i += d
162
- m /= 256
163
- mLen -= 8
164
- }
165
-
166
- e = (e << mLen) | m
167
- eLen += mLen
168
- while (eLen > 0) {
169
- buffer[offset + i] = e & 0xff
170
- i += d
171
- e /= 256
172
- eLen -= 8
173
- }
174
-
175
- buffer[offset + i - d] |= s * 128
176
-
177
- return buffer;
178
- };
179
-
180
- export const read_ieee754_binary64 = buffer => {
181
- return new Float64Array(new Uint8Array(buffer).buffer)[0];
182
-
183
- let isLE = true, mLen = 52, nBytes = 8, offset = 0;
184
-
185
- let e, m
186
- const eLen = (nBytes * 8) - mLen - 1
187
- const eMax = (1 << eLen) - 1
188
- const eBias = eMax >> 1
189
- let nBits = -7
190
- let i = isLE ? (nBytes - 1) : 0
191
- const d = isLE ? -1 : 1
192
- let s = buffer[offset + i]
193
-
194
- i += d
195
-
196
- e = s & ((1 << (-nBits)) - 1)
197
- s >>= (-nBits)
198
- nBits += eLen
199
- while (nBits > 0) {
200
- e = (e * 256) + buffer[offset + i]
201
- i += d
202
- nBits -= 8
203
- }
204
-
205
- m = e & ((1 << (-nBits)) - 1)
206
- e >>= (-nBits)
207
- nBits += mLen
208
- while (nBits > 0) {
209
- m = (m * 256) + buffer[offset + i]
210
- i += d
211
- nBits -= 8
212
- }
213
-
214
- if (e === 0) {
215
- e = 1 - eBias
216
- } else if (e === eMax) {
217
- return m ? NaN : ((s ? -1 : 1) * Infinity)
218
- } else {
219
- m = m + Math.pow(2, mLen)
220
- e = e - eBias
221
- }
222
- return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
223
- };
108
+ export const ieee754_binary64 = value => [...new Uint8Array(new Float64Array([ value ]).buffer)];
109
+ export const read_ieee754_binary64 = buffer => new Float64Array(new Uint8Array(buffer).buffer)[0];
package/compiler/index.js CHANGED
@@ -28,8 +28,8 @@ const logFuncs = (funcs, globals, exceptions) => {
28
28
 
29
29
  const getArg = name => process.argv.find(x => x.startsWith(`-${name}=`))?.slice(name.length + 2);
30
30
 
31
- const writeFileSync = (typeof process !== 'undefined' ? (await import('node:fs')).writeFileSync : undefined);
32
- const execSync = (typeof process !== 'undefined' ? (await import('node:child_process')).execSync : undefined);
31
+ const writeFileSync = (typeof process?.version !== 'undefined' ? (await import('node:fs')).writeFileSync : undefined);
32
+ const execSync = (typeof process?.version !== 'undefined' ? (await import('node:child_process')).execSync : undefined);
33
33
 
34
34
  export default (code, flags) => {
35
35
  globalThis.optLog = process.argv.includes('-opt-log');
@@ -75,6 +75,7 @@ export default (code, flags) => {
75
75
 
76
76
  if (target === 'c') {
77
77
  const c = toc(out);
78
+ out.c = c;
78
79
 
79
80
  if (outFile) {
80
81
  writeFileSync(outFile, c);
@@ -82,7 +83,7 @@ export default (code, flags) => {
82
83
  console.log(c);
83
84
  }
84
85
 
85
- process.exit();
86
+ if (process.version) process.exit();
86
87
  }
87
88
 
88
89
  if (target === 'native') {
@@ -98,7 +99,7 @@ export default (code, flags) => {
98
99
  // obvious command escape is obvious
99
100
  execSync(args.join(' '), { stdio: 'inherit' });
100
101
 
101
- process.exit();
102
+ if (process.version) process.exit();
102
103
  }
103
104
 
104
105
  return out;
package/compiler/parse.js CHANGED
@@ -8,10 +8,16 @@ if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
8
8
 
9
9
  // import { parse } from 'acorn';
10
10
 
11
- // todo: review which to use by default
12
- const parser = process.argv.find(x => x.startsWith('-parser='))?.split('=')?.[1] ?? 'acorn';
13
- const { parse } = (await import((globalThis.document ? 'https://esm.sh/' : '') + parser));
11
+ let parser, parse;
12
+
13
+ const loadParser = async () => {
14
+ parser = process.argv.find(x => x.startsWith('-parser='))?.split('=')?.[1] ?? 'acorn';
15
+ 0, { parse } = (await import((globalThis.document ? 'https://esm.sh/' : '') + parser));
16
+ };
17
+ globalThis._porf_loadParser = loadParser;
18
+ await loadParser();
14
19
 
20
+ // todo: review which to use by default
15
21
  // supported parsers:
16
22
  // - acorn
17
23
  // - meriyah
@@ -24,8 +30,26 @@ const types = process.argv.includes('-types');
24
30
  if (types && !['@babel/parser', 'hermes-parser'].includes(parser)) log.warning('parser', `passed -types with a parser (${parser}) which does not support`);
25
31
 
26
32
  export default (input, flags) => {
27
- return parse(input, {
33
+ const ast = parse(input, {
34
+ // acorn
28
35
  ecmaVersion: 'latest',
29
- sourceType: flags.includes('module') ? 'module' : 'script'
36
+
37
+ // meriyah
38
+ next: true,
39
+ module: flags.includes('module'),
40
+ webcompat: true,
41
+
42
+ // babel
43
+ plugins: types ? ['estree', 'typescript'] : ['estree'],
44
+
45
+ // multiple
46
+ sourceType: flags.includes('module') ? 'module' : 'script',
47
+ ranges: false,
48
+ tokens: false,
49
+ comments: false,
30
50
  });
51
+
52
+ if (ast.type === 'File') return ast.program;
53
+
54
+ return ast;
31
55
  };
package/compiler/wrap.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import compile from './index.js';
2
2
  import decompile from './decompile.js';
3
- import fs from 'node:fs';
3
+ // import fs from 'node:fs';
4
4
 
5
5
  const bold = x => `\u001b[1m${x}\u001b[0m`;
6
6
 
@@ -25,7 +25,7 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
25
25
  const times = [];
26
26
 
27
27
  const t1 = performance.now();
28
- const { wasm, funcs, globals, tags, exceptions, pages } = compile(source, flags);
28
+ const { wasm, funcs, globals, tags, exceptions, pages, c } = compile(source, flags);
29
29
 
30
30
  if (source.includes('export function')) flags.push('module');
31
31
 
@@ -126,8 +126,8 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
126
126
  }
127
127
 
128
128
  if (flags.includes('decomp')) {
129
- return { exports, wasm, times, decomps: funcs.map(x => decompile(x.wasm, x.name, x.index, x.locals, x.params, x.returns, funcs, globals, exceptions)) };
129
+ return { exports, wasm, times, decomps: funcs.map(x => decompile(x.wasm, x.name, x.index, x.locals, x.params, x.returns, funcs, globals, exceptions)), c };
130
130
  }
131
131
 
132
- return { exports, wasm, times, pages };
132
+ return { exports, wasm, times, pages, c };
133
133
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.2.0-77e30e8",
4
+ "version": "0.2.0-964b4c2",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/tmp.c ADDED
@@ -0,0 +1,69 @@
1
+
2
+ #include <stdio.h>
3
+
4
+ struct ReturnValue {
5
+ double value;
6
+ long type;
7
+ };
8
+
9
+ double sum = 0;
10
+ long sumdtype = 0;
11
+ double counter = 0;
12
+ long counterdtype = 0;
13
+
14
+ double inline f64_f(double x, double y) {
15
+ return (x - ((int)(x / y) * y));
16
+ }
17
+
18
+ struct ReturnValue isPrime(double number, long numberdtype) {
19
+ double i = 0;
20
+ long idtype = 0;
21
+ double __tmpop_left = 0;
22
+ double __tmpop_right = 0;
23
+ long compare_left_pointer = 0;
24
+ long compare_left_length = 0;
25
+ long compare_right_pointer = 0;
26
+ long compare_right_length = 0;
27
+ long compare_index = 0;
28
+ long compare_index_end = 0;
29
+
30
+ if (number < 2e+0) {
31
+ return (struct ReturnValue){ 1, 0e+0 };
32
+ }
33
+ i = 2e+0;
34
+ idtype = 0;
35
+ while (i < number) {
36
+ if (f64_f(number, i) == 0e+0) {
37
+ return (struct ReturnValue){ 1, 0e+0 };
38
+ }
39
+ i = i + 1e+0;
40
+ }
41
+ return (struct ReturnValue){ 1, 1e+0 };
42
+ }
43
+
44
+ double inline __console_log(double x) {
45
+ printf("%f\n", x);
46
+ printf("%c", (int)(1e+1));
47
+ }
48
+
49
+ int main() {
50
+ long dlast_type = 0;
51
+ double elogicinner_tmp = 0;
52
+ long dtypeswitch_tmp = 0;
53
+
54
+ sum = 0e+0;
55
+ sumdtype = 0;
56
+ counter = 0e+0;
57
+ counterdtype = 0;
58
+ while (counter <= 1e+5) {
59
+ const struct ReturnValue _ = isPrime(counter, counterdtype);
60
+ dlast_type = _.type;
61
+ if ((unsigned long)(elogicinner_tmp = _.value) == 1e+0) {
62
+ sum = sum + counter;
63
+ sumdtype = 0;
64
+ }
65
+ counter = counter + 1e+0;
66
+ }
67
+ __console_log(sum);
68
+ }
69
+