porffor 0.17.0-30b9c5fa5 → 0.17.0-3c2d70d66

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/compiler/opt.js CHANGED
@@ -203,6 +203,9 @@ export default (funcs, globals, pages, tags, exceptions) => {
203
203
  if (type === 'Array') missing = !pages.hasArray;
204
204
  if (type === 'String') missing = !pages.hasString;
205
205
  if (type === 'ByteString') missing = !pages.hasByteString;
206
+ if (['Set', 'Uint8Array', 'Int8Array', 'Uint8ClampedArray', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type)) {
207
+ missing = funcs.find(x => x.name === type) == null;
208
+ }
206
209
 
207
210
  if (missing) {
208
211
  let j = i, depth = 0;
@@ -6,6 +6,8 @@ import { join } from 'node:path';
6
6
 
7
7
  import { fileURLToPath } from 'node:url';
8
8
  const __dirname = fileURLToPath(new URL('.', import.meta.url));
9
+ globalThis.precompileCompilerPath = __dirname;
10
+ globalThis.precompile = true;
9
11
 
10
12
  const argv = process.argv.slice();
11
13
 
@@ -14,7 +16,7 @@ const compile = async (file, [ _funcs, _globals ]) => {
14
16
  let first = source.slice(0, source.indexOf('\n'));
15
17
 
16
18
  if (first.startsWith('export default')) {
17
- source = (await import(file)).default();
19
+ source = await (await import(file)).default();
18
20
  first = source.slice(0, source.indexOf('\n'));
19
21
  }
20
22
 
@@ -117,7 +119,7 @@ ${funcs.map(x => {
117
119
  locals: ${JSON.stringify(Object.values(x.locals).slice(x.params.length).map(x => x.type))},
118
120
  localNames: ${JSON.stringify(Object.keys(x.locals))},
119
121
  ${x.data && x.data.length > 0 ? ` data: ${JSON.stringify(x.data)},` : ''}
120
- ${x.table ? ` table: true` : ''}
122
+ ${x.table ? ` table: true,` : ''}${x.constr ? ` constr: true,` : ''}
121
123
  };`.replaceAll('\n\n', '\n').replaceAll('\n\n', '\n').replaceAll('\n\n', '\n');
122
124
  }).join('\n')}
123
125
  };`;
@@ -208,72 +208,6 @@ export const PrototypeFuncs = function() {
208
208
  // ...number(1, Valtype.i32),
209
209
  // [ Opcodes.i32_sub ]
210
210
  // ]),
211
- ],
212
-
213
- fill: (pointer, length, wElement, wType, iTmp, iTmp2) => [
214
- ...wElement,
215
- [ Opcodes.local_set, iTmp ],
216
-
217
- // use cached length i32 as pointer
218
- ...length.getCachedI32(),
219
-
220
- // length - 1 for indexes
221
- ...number(1, Valtype.i32),
222
- [ Opcodes.i32_sub ],
223
-
224
- // * sizeof value
225
- ...number(ValtypeSize[valtype] + 1, Valtype.i32),
226
- [ Opcodes.i32_mul ],
227
-
228
- ...length.setCachedI32(),
229
-
230
- ...(noUnlikelyChecks ? [] : [
231
- ...length.getCachedI32(),
232
- ...number(0, Valtype.i32),
233
- [ Opcodes.i32_lt_s ],
234
- [ Opcodes.if, Blocktype.void ],
235
- ...pointer,
236
- Opcodes.i32_from_u,
237
- [ Opcodes.br, 1 ],
238
- [ Opcodes.end ]
239
- ]),
240
-
241
- [ Opcodes.loop, Blocktype.void ],
242
-
243
- // calculate offset
244
- ...length.getCachedI32(),
245
- ...pointer,
246
- [ Opcodes.i32_add ],
247
- [ Opcodes.local_set, iTmp2 ],
248
-
249
- // store value
250
- [ Opcodes.local_get, iTmp2 ],
251
- [ Opcodes.local_get, iTmp ],
252
- [ Opcodes.store, 0, ValtypeSize.i32 ],
253
-
254
- // store type
255
- [ Opcodes.local_get, iTmp2 ],
256
- ...wType,
257
- [ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
258
-
259
- // pointer - sizeof value
260
- ...length.getCachedI32(),
261
- ...number(ValtypeSize[valtype] + 1, Valtype.i32),
262
- [ Opcodes.i32_sub ],
263
-
264
- ...length.setCachedI32(),
265
-
266
- // if pointer >= 0, loop
267
- ...length.getCachedI32(),
268
- ...number(0, Valtype.i32),
269
- [ Opcodes.i32_ge_s ],
270
- [ Opcodes.br_if, 0 ],
271
-
272
- [ Opcodes.end ],
273
-
274
- // return this array
275
- ...pointer,
276
- Opcodes.i32_from_u,
277
211
  ]
278
212
  };
279
213
 
@@ -282,9 +216,6 @@ export const PrototypeFuncs = function() {
282
216
  this[TYPES.array].push.noArgRetLength = true;
283
217
  this[TYPES.array].push.local = Valtype.i32;
284
218
  this[TYPES.array].pop.local = Valtype.i32;
285
- this[TYPES.array].fill.local = valtypeBinary;
286
- this[TYPES.array].fill.local2 = Valtype.i32;
287
- this[TYPES.array].fill.returnType = TYPES.array;
288
219
 
289
220
  this[TYPES.string] = {
290
221
  at: (pointer, length, wIndex, wType, iTmp, _, arrayShell) => {
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.17.0-30b9c5fa5",
4
+ "version": "0.17.0-3c2d70d66",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/rhemyn/compile.js CHANGED
@@ -12,15 +12,36 @@ const Length = 4;
12
12
  const Tmp = 5;
13
13
  const QuantifierTmp = 6; // the temporary variable used for quanitifers
14
14
 
15
+ const doesSucceedZero = (node) => {
16
+ for (const n of node.body) {
17
+ if (n.type === "Group") {
18
+ if (!doesSucceedZero(node)) return false;
19
+ }
20
+ if (!n.quantifier || n.quantifier[0] > 0) {
21
+ return false;
22
+ }
23
+ }
24
+ return true;
25
+ }
26
+
15
27
  const generate = (node, negated = false, get = true, stringSize = 2, func = 'test') => {
16
28
  let out = [];
17
29
  switch (node.type) {
18
30
  case 'Expression':
31
+ let succeedsZero = doesSucceedZero(node);
32
+
19
33
  out = [
20
34
  // set length local
21
35
  [ Opcodes.local_get, BasePointer ],
22
36
  [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
23
- [ Opcodes.local_set, Length ],
37
+ [ Opcodes.local_tee, Length ],
38
+
39
+ ...number(0, Valtype.i32),
40
+ [ Opcodes.i32_eq ],
41
+ [ Opcodes.if, Blocktype.void ],
42
+ ...number(succeedsZero ? 1 : 0, Valtype.i32),
43
+ [ Opcodes.return ],
44
+ [ Opcodes.end ],
24
45
 
25
46
  // pointer = base + sizeof i32
26
47
  [ Opcodes.local_get, BasePointer ],
@@ -43,14 +64,15 @@ const generate = (node, negated = false, get = true, stringSize = 2, func = 'tes
43
64
  [ Opcodes.return ],
44
65
  [ Opcodes.end ],
45
66
 
46
- // increment counter by 1, check if eq length, if not loop
67
+ // counter++, if length > counter, loop
68
+ [ Opcodes.local_get, Length ],
69
+
47
70
  [ Opcodes.local_get, Counter ],
48
71
  ...number(1, Valtype.i32),
49
72
  [ Opcodes.i32_add ],
50
73
  [ Opcodes.local_tee, Counter ],
51
74
 
52
- [ Opcodes.local_get, Length ],
53
- [ Opcodes.i32_ne ],
75
+ [ Opcodes.i32_gt_s ],
54
76
 
55
77
  [ Opcodes.br_if, 0 ],
56
78
  [ Opcodes.end ],
@@ -122,6 +144,18 @@ const wrapQuantifier = (node, method, get, stringSize) => {
122
144
  ...number(0, Valtype.i32),
123
145
  [Opcodes.local_set, QuantifierTmp],
124
146
 
147
+ // if len - counter == 0, if min == 0, succeed, else fail
148
+ [ Opcodes.local_get, Length ],
149
+ [ Opcodes.local_get, Counter ],
150
+ [ Opcodes.i32_sub ],
151
+ ...number(0, Valtype.i32),
152
+ [ Opcodes.i32_eq ],
153
+ ...(min == 0 ? [
154
+ [ Opcodes.if, Blocktype.void ],
155
+ ] : [
156
+ [ Opcodes.br_if, 0 ],
157
+ ]),
158
+
125
159
  // start loop
126
160
  [Opcodes.loop, Blocktype.void],
127
161
  [ Opcodes.block, Blocktype.void ],
@@ -166,13 +200,7 @@ const wrapQuantifier = (node, method, get, stringSize) => {
166
200
  [Opcodes.i32_lt_s],
167
201
  ...(get ? checkFailure(): []),
168
202
 
169
- // counter += tmp - 1
170
- [ Opcodes.local_get, QuantifierTmp ],
171
- ...number(1, Valtype.i32),
172
- [ Opcodes.i32_sub ],
173
- [ Opcodes.local_get, Counter ],
174
- [ Opcodes.i32_add ],
175
- [ Opcodes.local_set, Counter ]
203
+ ...(min == 0 ? [ [ Opcodes.end ] ] : []),
176
204
  ];
177
205
  }
178
206
 
@@ -190,7 +218,7 @@ const generateChar = (node, negated, get, stringSize) => {
190
218
 
191
219
  return [
192
220
  ...out,
193
- ...(get ? checkFailure(): [])
221
+ ...(get ? checkFailure(): []),
194
222
  ];
195
223
  };
196
224
 
@@ -225,7 +253,7 @@ const generateSet = (node, negated, get, stringSize) => {
225
253
 
226
254
  return [
227
255
  ...out,
228
- ...checkFailure()
256
+ ...checkFailure(),
229
257
  ];
230
258
  };
231
259
 
@@ -267,21 +295,26 @@ const wrapFunc = (regex, func, name, index) => {
267
295
  // bytestring
268
296
  ...generate(parsed, false, true, 1, func),
269
297
  [ Opcodes.end ]
270
- ], name, index);
298
+ ], name, index, types[func]);
271
299
  };
272
300
 
273
301
  export const test = (regex, index = 0, name = 'regex_test_' + regex) => wrapFunc(regex, 'test', name, index);
274
302
  export const search = (regex, index = 0, name = 'regex_search_' + regex) => wrapFunc(regex, 'search', name, index);
275
303
 
276
- const outputFunc = (wasm, name, index) => ({
304
+ export const types = {
305
+ test: TYPES.boolean,
306
+ search: TYPES.number
307
+ };
308
+
309
+ const outputFunc = (wasm, name, index, returnType) => ({
277
310
  name,
278
311
  index,
279
312
  wasm,
313
+ returnType,
280
314
 
281
315
  export: true,
282
316
  params: [ Valtype.i32, Valtype.i32 ],
283
317
  returns: [ Valtype.i32 ],
284
- returnType: TYPES.boolean,
285
318
  locals: {
286
319
  basePointer: { idx: 0, type: Valtype.i32 },
287
320
  inputType: { idx: 1, type: Valtype.i32 },