porffor 0.2.0-b9abe0d → 0.2.0-bae0c5b

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.
@@ -216,7 +216,7 @@ export const BuiltinFuncs = function() {
216
216
  };
217
217
 
218
218
 
219
- this.__console_log = {
219
+ this.__Porffor_print = {
220
220
  params: [ valtypeBinary, Valtype.i32 ],
221
221
  typedParams: true,
222
222
  locals: [ Valtype.i32, Valtype.i32 ],
@@ -368,10 +368,7 @@ export const BuiltinFuncs = function() {
368
368
  [ Opcodes.local_get, 0 ],
369
369
  [ Opcodes.call, importedFuncs.print ],
370
370
  ]
371
- }, Blocktype.void),
372
-
373
- ...char('\n'),
374
- [ Opcodes.call, importedFuncs.printChar ]
371
+ }, Blocktype.void)
375
372
  ]
376
373
  };
377
374
 
@@ -1058,19 +1055,6 @@ export const BuiltinFuncs = function() {
1058
1055
  };
1059
1056
 
1060
1057
 
1061
- // this.__Date_now = {
1062
- // params: [],
1063
- // locals: [],
1064
- // returns: [ valtypeBinary ],
1065
- // wasm: [
1066
- // [ Opcodes.call, importedFuncs.timeOrigin ],
1067
- // [ Opcodes.call, importedFuncs.time ],
1068
- // [ Opcodes.f64_add ],
1069
- // [ Opcodes.f64_trunc ]
1070
- // ]
1071
- // };
1072
-
1073
-
1074
1058
  this.__Porffor_type = {
1075
1059
  params: [ valtypeBinary, Valtype.i32 ],
1076
1060
  typedParams: true,
@@ -210,7 +210,7 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
210
210
  return int;
211
211
  });
212
212
 
213
- out.push([ ...inst, ...immediates ]);
213
+ out.push([ ...inst, ...immediates.flatMap(x => signedLEB128(x)) ]);
214
214
  }
215
215
 
216
216
  return out;
@@ -1472,7 +1472,7 @@ const countLeftover = wasm => {
1472
1472
  if (depth === 0)
1473
1473
  if ([Opcodes.throw, Opcodes.drop, Opcodes.local_set, Opcodes.global_set].includes(inst[0])) count--;
1474
1474
  else if ([null, Opcodes.i32_eqz, Opcodes.i64_eqz, Opcodes.f64_ceil, Opcodes.f64_floor, Opcodes.f64_trunc, Opcodes.f64_nearest, Opcodes.f64_sqrt, Opcodes.local_tee, Opcodes.i32_wrap_i64, Opcodes.i64_extend_i32_s, Opcodes.i64_extend_i32_u, Opcodes.f32_demote_f64, Opcodes.f64_promote_f32, Opcodes.f64_convert_i32_s, Opcodes.f64_convert_i32_u, Opcodes.i32_clz, Opcodes.i32_ctz, Opcodes.i32_popcnt, Opcodes.f64_neg, Opcodes.end, Opcodes.i32_trunc_sat_f64_s[0], Opcodes.i32x4_extract_lane, Opcodes.i16x8_extract_lane, Opcodes.i32_load, Opcodes.i64_load, Opcodes.f64_load, Opcodes.v128_load, Opcodes.i32_load16_u, Opcodes.i32_load16_s, Opcodes.i32_load8_u, Opcodes.i32_load8_s, Opcodes.memory_grow].includes(inst[0]) && (inst[0] !== 0xfc || inst[1] < 0x0a)) {}
1475
- else if ([Opcodes.local_get, Opcodes.global_get, Opcodes.f64_const, Opcodes.i32_const, Opcodes.i64_const, Opcodes.v128_const].includes(inst[0])) count++;
1475
+ else if ([Opcodes.local_get, Opcodes.global_get, Opcodes.f64_const, Opcodes.i32_const, Opcodes.i64_const, Opcodes.v128_const, Opcodes.memory_size].includes(inst[0])) count++;
1476
1476
  else if ([Opcodes.i32_store, Opcodes.i64_store, Opcodes.f64_store, Opcodes.i32_store16, Opcodes.i32_store8].includes(inst[0])) count -= 2;
1477
1477
  else if (Opcodes.memory_copy[0] === inst[0] && Opcodes.memory_copy[1] === inst[1]) count -= 3;
1478
1478
  else if (inst[0] === Opcodes.return) count = 0;
@@ -1646,18 +1646,25 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1646
1646
  // megahack for /regex/.func()
1647
1647
  const funcName = decl.callee.property.name;
1648
1648
  if (decl.callee.object.regex && Object.hasOwn(Rhemyn, funcName)) {
1649
- const func = Rhemyn[funcName](decl.callee.object.regex.pattern, currentFuncIndex++);
1649
+ const regex = decl.callee.object.regex.pattern;
1650
+ const rhemynName = `regex_${funcName}_${regex}`;
1650
1651
 
1651
- funcIndex[func.name] = func.index;
1652
- funcs.push(func);
1652
+ if (!funcIndex[rhemynName]) {
1653
+ const func = Rhemyn[funcName](regex, currentFuncIndex++, rhemynName);
1653
1654
 
1655
+ funcIndex[func.name] = func.index;
1656
+ funcs.push(func);
1657
+ }
1658
+
1659
+ const idx = funcIndex[rhemynName];
1654
1660
  return [
1655
1661
  // make string arg
1656
1662
  ...generate(scope, decl.arguments[0]),
1663
+ Opcodes.i32_to_u,
1664
+ ...getNodeType(scope, decl.arguments[0]),
1657
1665
 
1658
1666
  // call regex func
1659
- Opcodes.i32_to_u,
1660
- [ Opcodes.call, func.index ],
1667
+ [ Opcodes.call, idx ],
1661
1668
  Opcodes.i32_from_u,
1662
1669
 
1663
1670
  ...number(TYPES.boolean, Valtype.i32),
@@ -1697,6 +1704,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1697
1704
 
1698
1705
  protoBC[type] = generateCall(scope, {
1699
1706
  callee: {
1707
+ type: 'Identifier',
1700
1708
  name: x
1701
1709
  },
1702
1710
  arguments: [ target, ...decl.arguments ],
@@ -1845,31 +1853,28 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1845
1853
  if (idx === undefined && name.startsWith('__Porffor_wasm_')) {
1846
1854
  const wasmOps = {
1847
1855
  // pointer, align, offset
1848
- i32_load: { imms: 2, args: 1, returns: 1 },
1856
+ i32_load: { imms: 2, args: [ true ], returns: 1 },
1849
1857
  // pointer, value, align, offset
1850
- i32_store: { imms: 2, args: 2, returns: 0 },
1858
+ i32_store: { imms: 2, args: [ true, true ], returns: 0 },
1851
1859
  // pointer, align, offset
1852
- i32_load8_u: { imms: 2, args: 1, returns: 1 },
1860
+ i32_load8_u: { imms: 2, args: [ true ], returns: 1 },
1853
1861
  // pointer, value, align, offset
1854
- i32_store8: { imms: 2, args: 2, returns: 0 },
1862
+ i32_store8: { imms: 2, args: [ true, true ], returns: 0 },
1855
1863
  // pointer, align, offset
1856
- i32_load16_u: { imms: 2, args: 1, returns: 1 },
1864
+ i32_load16_u: { imms: 2, args: [ true ], returns: 1 },
1857
1865
  // pointer, value, align, offset
1858
- i32_store16: { imms: 2, args: 2, returns: 0 },
1866
+ i32_store16: { imms: 2, args: [ true, true ], returns: 0 },
1859
1867
 
1860
1868
  // pointer, align, offset
1861
- f64_load: { imms: 2, args: 1, returns: 1 },
1869
+ f64_load: { imms: 2, args: [ true ], returns: 0 }, // 0 due to not i32
1862
1870
  // pointer, value, align, offset
1863
- f64_store: { imms: 2, args: 2, returns: 0 },
1871
+ f64_store: { imms: 2, args: [ true, false ], returns: 0 },
1864
1872
 
1865
1873
  // value
1866
- i32_const: { imms: 1, args: 0, returns: 1 },
1874
+ i32_const: { imms: 1, args: [], returns: 1 },
1867
1875
 
1868
1876
  // a, b
1869
- i32_or: { imms: 0, args: 2, returns: 1 },
1870
-
1871
- add: { imms: 0, args: 2, returns: 1 },
1872
- i32_to_u: { imms: 0, args: 1, returns: 1 }
1877
+ i32_or: { imms: 0, args: [ true, true ], returns: 1 },
1873
1878
  };
1874
1879
 
1875
1880
  const opName = name.slice('__Porffor_wasm_'.length);
@@ -1878,13 +1883,13 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1878
1883
  const op = wasmOps[opName];
1879
1884
 
1880
1885
  const argOut = [];
1881
- for (let i = 0; i < op.args; i++) argOut.push(
1886
+ for (let i = 0; i < op.args.length; i++) argOut.push(
1882
1887
  ...generate(scope, decl.arguments[i]),
1883
- Opcodes.i32_to
1888
+ ...(op.args[i] ? [ Opcodes.i32_to ] : [])
1884
1889
  );
1885
1890
 
1886
1891
  // literals only
1887
- const imms = decl.arguments.slice(op.args).map(x => x.value);
1892
+ const imms = decl.arguments.slice(op.args.length).map(x => x.value);
1888
1893
 
1889
1894
  return [
1890
1895
  ...argOut,
@@ -2229,7 +2234,7 @@ const generateVar = (scope, decl) => {
2229
2234
  }
2230
2235
 
2231
2236
  const typed = typedInput && x.id.typeAnnotation;
2232
- let idx = allocVar(scope, name, global, !typed);
2237
+ let idx = allocVar(scope, name, global, !(typed && extractTypeAnnotation(x.id).type != null));
2233
2238
 
2234
2239
  if (typed) {
2235
2240
  addVarMetadata(scope, name, global, extractTypeAnnotation(x.id));
@@ -3508,6 +3513,8 @@ const internalConstrs = {
3508
3513
  );
3509
3514
  }
3510
3515
 
3516
+ out.push(Opcodes.i32_from_u);
3517
+
3511
3518
  return out;
3512
3519
  },
3513
3520
  type: TYPES.boolean,
@@ -3526,6 +3533,8 @@ const internalConstrs = {
3526
3533
  );
3527
3534
  }
3528
3535
 
3536
+ out.push(Opcodes.i32_from_u);
3537
+
3529
3538
  return out;
3530
3539
  },
3531
3540
  type: TYPES.boolean,
@@ -3580,6 +3589,39 @@ const internalConstrs = {
3580
3589
  type: TYPES.number,
3581
3590
  notConstr: true,
3582
3591
  length: 2
3592
+ },
3593
+
3594
+ __console_log: {
3595
+ generate: (scope, decl) => {
3596
+ const out = [];
3597
+
3598
+ for (let i = 0; i < decl.arguments.length; i++) {
3599
+ out.push(
3600
+ ...generateCall(scope, {
3601
+ callee: {
3602
+ type: 'Identifier',
3603
+ name: '__Porffor_print'
3604
+ },
3605
+ arguments: [ decl.arguments[i] ]
3606
+ }),
3607
+
3608
+ // print space
3609
+ ...number(32),
3610
+ [ Opcodes.call, importedFuncs.printChar ]
3611
+ );
3612
+ }
3613
+
3614
+ // print newline
3615
+ out.push(
3616
+ ...number(10),
3617
+ [ Opcodes.call, importedFuncs.printChar ]
3618
+ );
3619
+
3620
+ return out;
3621
+ },
3622
+ type: TYPES.undefined,
3623
+ notConstr: true,
3624
+ length: 0
3583
3625
  }
3584
3626
  };
3585
3627
 
@@ -3614,7 +3656,7 @@ export default program => {
3614
3656
 
3615
3657
  globalThis.valtype = 'f64';
3616
3658
 
3617
- const valtypeOpt = process.argv.find(x => x.startsWith('-valtype='));
3659
+ const valtypeOpt = process.argv.find(x => x.startsWith('--valtype='));
3618
3660
  if (valtypeOpt) valtype = valtypeOpt.split('=')[1];
3619
3661
 
3620
3662
  globalThis.valtypeBinary = Valtype[valtype];
@@ -3622,7 +3664,7 @@ export default program => {
3622
3664
  const valtypeInd = ['i32', 'i64', 'f64'].indexOf(valtype);
3623
3665
 
3624
3666
  globalThis.pageSize = PageSize;
3625
- const pageSizeOpt = process.argv.find(x => x.startsWith('-page-size='));
3667
+ const pageSizeOpt = process.argv.find(x => x.startsWith('--page-size='));
3626
3668
  if (pageSizeOpt) pageSize = parseInt(pageSizeOpt.split('=')[1]) * 1024;
3627
3669
 
3628
3670
  // set generic opcodes for current valtype