porffor 0.2.0-623cdf0 → 0.2.0-6aff0fa

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/2c.js CHANGED
@@ -17,12 +17,12 @@ const CValtype = {
17
17
  undefined: 'void'
18
18
  };
19
19
 
20
- const alwaysPreface = `typedef unsigned char i8;
21
- typedef unsigned short i16;
22
- typedef long i32;
23
- typedef unsigned long u32;
24
- typedef long long i64;
25
- typedef unsigned long long u64;
20
+ const alwaysPreface = `typedef uint8_t i8;
21
+ typedef uint16_t i16;
22
+ typedef int32_t i32;
23
+ typedef uint32_t u32;
24
+ typedef int64_t i64;
25
+ typedef uint64_t u64;
26
26
  typedef float f32;
27
27
  typedef double f64;
28
28
 
@@ -31,8 +31,7 @@ f64 NAN = 0e+0/0e+0;
31
31
  struct ReturnValue {
32
32
  ${CValtype.f64} value;
33
33
  ${CValtype.i32} type;
34
- };
35
- \n`;
34
+ };\n\n`;
36
35
 
37
36
  // todo: is memcpy/etc safe with host endianness?
38
37
 
@@ -148,7 +147,8 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
148
147
  const includes = new Map(), unixIncludes = new Map(), winIncludes = new Map();
149
148
  const prepend = new Map(), prependMain = new Map();
150
149
 
151
- // presume all <i32 work is unsigned
150
+ includes.set('stdint.h', true);
151
+
152
152
  let out = ``;
153
153
 
154
154
  for (const x in globals) {
@@ -164,7 +164,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
164
164
  }
165
165
 
166
166
  if (data.length > 0) {
167
- prependMain.set('_data', data.map(x => `memcpy(_memory + ${x.offset}, (char[]){${x.bytes.join(',')}}, ${x.bytes.length});`).join('\n'));
167
+ prependMain.set('_data', data.map(x => `memcpy(_memory + ${x.offset}, (unsigned char[]){${x.bytes.join(',')}}, ${x.bytes.length});`).join('\n'));
168
168
  }
169
169
 
170
170
  // for (const [ x, p ] of pages) {
@@ -624,7 +624,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
624
624
 
625
625
  const makeIncludes = includes => [...includes.keys()].map(x => `#include <${x}>\n`).join('');
626
626
 
627
- out = alwaysPreface + platformSpecific(makeIncludes(winIncludes), makeIncludes(unixIncludes), false) + '\n' + makeIncludes(includes) + '\n' + [...prepend.values()].join('\n') + '\n\n' + out;
627
+ out = platformSpecific(makeIncludes(winIncludes), makeIncludes(unixIncludes), false) + '\n' + makeIncludes(includes) + '\n' + alwaysPreface + [...prepend.values()].join('\n') + '\n\n' + out;
628
628
 
629
629
  return out.trim();
630
630
  };
@@ -0,0 +1,153 @@
1
+ // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
2
+
3
+ import type { i32, bytestring } from './porffor.d.ts';
4
+
5
+ // while (len >= 8) {
6
+ // Porffor.wasm`
7
+ // local tmp i64
8
+ // local.get ${i}
9
+ // i64.load 0 4
10
+ // local.set tmp
11
+
12
+ // local k i64
13
+ // i64.const 0
14
+ // local.set k
15
+
16
+ // loop 64
17
+ // local.get ${j}
18
+
19
+ // local.get ${keyStrPtr}
20
+
21
+ // local.get tmp
22
+
23
+ // ;; k * 6
24
+ // i64.const 58
25
+
26
+ // local.get k
27
+ // i64.const 6
28
+ // i64.mul
29
+
30
+ // i64.sub
31
+
32
+ // ;; tmp >> (58 - (k * 6))
33
+ // i64.shr_u
34
+
35
+ // ;; (tmp >> (58 - (k * 6))) & 0x3f
36
+ // i64.const 63
37
+ // i64.and
38
+
39
+ // i32.wrap_i64
40
+
41
+ // ;; keyStrPtr + ...
42
+ // i32.add
43
+
44
+ // ;; load character from keyStr
45
+ // i32.load8_u 0 4
46
+
47
+ // ;; store in output at j
48
+ // i32.store8 0 4
49
+
50
+ // local.get ${j}
51
+ // i32.const 1
52
+ // i32.add
53
+ // local.set ${j}
54
+
55
+ // local.get k
56
+ // i64.const 1
57
+ // i64.add
58
+ // local.tee k
59
+
60
+ // i64.const 8
61
+ // i64.lt_s
62
+ // br_if 0
63
+ // end
64
+
65
+ // `;
66
+
67
+ // // len -= 6;
68
+ // i += 6;
69
+ // }
70
+
71
+ // // while (k < 8) {
72
+ // // Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + Porffor.wasm.i32.wrap_i64(Porffor.wasm.i64.and(
73
+ // // Porffor.wasm.i64.shr_u(tmp, Porffor.wasm.i64.extend_i32_u(58 - k * 6)),
74
+ // // Porffor.wasm.i64.const(0x3f)
75
+ // // )), 0, 4), 0, 4);
76
+ // // k += 1;
77
+ // // }
78
+
79
+ // i += 6;
80
+ // len -= 6;
81
+ // }
82
+
83
+ export const btoa = (input: bytestring): bytestring => {
84
+ const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
85
+ const keyStrPtr: i32 = Porffor.i32.ptrUnsafe(keyStr);
86
+
87
+ let len: i32 = input.length;
88
+ let output: bytestring = '';
89
+ output.length = 4 * (len / 3 + !!(len % 3));
90
+
91
+ let i: i32 = Porffor.i32.ptrUnsafe(input),
92
+ j: i32 = Porffor.i32.ptrUnsafe(output);
93
+
94
+ const endPtr = i + len;
95
+ while (i < endPtr) {
96
+ const chr1: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
97
+ const chr2: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
98
+ const chr3: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
99
+
100
+ const enc1: i32 = chr1 >> 2;
101
+ const enc2: i32 = ((chr1 & 3) << 4) | (chr2 == -1 ? 0 : (chr2 >> 4));
102
+ let enc3: i32 = ((chr2 & 15) << 2) | (chr3 == -1 ? 0 : (chr3 >> 6));
103
+ let enc4: i32 = chr3 & 63;
104
+
105
+ if (chr2 == -1) {
106
+ enc3 = 64;
107
+ enc4 = 64;
108
+ } else if (chr3 == -1) {
109
+ enc4 = 64;
110
+ }
111
+
112
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc1, 0, 4), 0, 4);
113
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc2, 0, 4), 0, 4);
114
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc3, 0, 4), 0, 4);
115
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc4, 0, 4), 0, 4);
116
+ }
117
+
118
+ return output;
119
+ };
120
+
121
+ /* var atob = function (input) {
122
+ const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
123
+
124
+ let output = "";
125
+ let chr1, chr2, chr3;
126
+ let enc1, enc2, enc3, enc4;
127
+ let i = 0;
128
+
129
+ while (i < input.length) {
130
+ enc1 = keyStr.indexOf(input.charAt(i++));
131
+ enc2 = keyStr.indexOf(input.charAt(i++));
132
+ enc3 = keyStr.indexOf(input.charAt(i++));
133
+ enc4 = keyStr.indexOf(input.charAt(i++));
134
+
135
+ chr1 = (enc1 << 2) | (enc2 >> 4);
136
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
137
+ chr3 = ((enc3 & 3) << 6) | enc4;
138
+
139
+ // output += String.fromCharCode(chr1);
140
+ Porffor.bytestring.appendCharCode(output, chr1);
141
+
142
+ if (enc3 != 64) {
143
+ // output += String.fromCharCode(chr2);
144
+ Porffor.bytestring.appendCharCode(output, chr2);
145
+ }
146
+ if (enc4 != 64) {
147
+ // output += String.fromCharCode(chr3);
148
+ Porffor.bytestring.appendCharCode(output, chr3);
149
+ }
150
+ }
151
+
152
+ return output;
153
+ }; */
@@ -0,0 +1,23 @@
1
+ export type i32 = number;
2
+ export type i64 = number;
3
+ export type bytestring = string;
4
+
5
+ type PorfforGlobal = {
6
+ wasm: {
7
+ (...args: any[]): void;
8
+ i32: {
9
+ load8_u: (pointer: i32, align: i32, offset: i32) => i32;
10
+ store8: (pointer: i32, value: i32, align: i32, offset: i32) => i32;
11
+ }
12
+ }
13
+ ptr: (obj: any) => i32;
14
+
15
+ i32: {
16
+ ptr: (obj: any) => i32;
17
+ ptrUnsafe: (obj: any) => i32;
18
+ }
19
+ };
20
+
21
+ declare global {
22
+ const Porffor: PorfforGlobal;
23
+ }
@@ -1,5 +1,7 @@
1
1
  import { Blocktype, Opcodes, Valtype, ValtypeSize } from "./wasmSpec.js";
2
2
  import { number, i32x4 } from "./embedding.js";
3
+ import Prefs from './prefs.js';
4
+ import * as GeneratedBuiltins from './generated_builtins.js';
3
5
 
4
6
  export const importedFuncs = [
5
7
  {
@@ -19,8 +21,14 @@ export const importedFuncs = [
19
21
  import: 't',
20
22
  params: 0,
21
23
  returns: 1
24
+ },
25
+ {
26
+ name: 'profile',
27
+ import: 'z',
28
+ params: 1,
29
+ returns: 0
22
30
  }
23
- ];
31
+ ].filter(x => x);
24
32
 
25
33
  for (let i = 0; i < importedFuncs.length; i++) {
26
34
  const f = importedFuncs[i];
@@ -117,6 +125,10 @@ export const BuiltinVars = function() {
117
125
 
118
126
  // stubs just so that parent objects exist
119
127
  this.Math = number(1);
128
+
129
+ // wintercg(tm)
130
+ this.__navigator_userAgent = (scope, { makeString }) => makeString(scope, `Porffor/0.2.0`, false, '__navigator_userAgent');
131
+ this.__navigator_userAgent.type = Prefs.bytestring ? '_bytestring' : 'string';
120
132
  };
121
133
 
122
134
  export const BuiltinFuncs = function() {
@@ -239,6 +251,40 @@ export const BuiltinFuncs = function() {
239
251
 
240
252
  [ Opcodes.end ]
241
253
  ],
254
+ [TYPES._bytestring]: [
255
+ // simply print a (byte)string :))
256
+ // cache input pointer as i32
257
+ [ Opcodes.local_get, 0 ],
258
+ Opcodes.i32_to_u,
259
+ [ Opcodes.local_tee, 2 ],
260
+
261
+ // make end pointer
262
+ [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
263
+ [ Opcodes.local_get, 2 ],
264
+ [ Opcodes.i32_add ],
265
+ [ Opcodes.local_set, 3 ],
266
+
267
+ [ Opcodes.loop, Blocktype.void ],
268
+
269
+ // print current char
270
+ [ Opcodes.local_get, 2 ],
271
+ [ Opcodes.i32_load8_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
272
+ Opcodes.i32_from_u,
273
+ [ Opcodes.call, importedFuncs.printChar ],
274
+
275
+ // increment pointer
276
+ [ Opcodes.local_get, 2 ],
277
+ [ Opcodes.i32_const, 1 ],
278
+ [ Opcodes.i32_add ],
279
+ [ Opcodes.local_tee, 2 ],
280
+
281
+ // if pointer != end pointer, loop
282
+ [ Opcodes.local_get, 3 ],
283
+ [ Opcodes.i32_ne ],
284
+ [ Opcodes.br_if, 0 ],
285
+
286
+ [ Opcodes.end ]
287
+ ],
242
288
  [TYPES._array]: [
243
289
  ...printStaticStr('[ '),
244
290
 
@@ -685,7 +731,7 @@ export const BuiltinFuncs = function() {
685
731
  typedParams: true,
686
732
  locals: [ Valtype.i32, Valtype.i32 ],
687
733
  returns: [ valtypeBinary ],
688
- returnType: process.argv.includes('-bytestring') ? '_bytestring' : 'string',
734
+ returnType: Prefs.bytestring ? '_bytestring' : 'string',
689
735
  wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
690
736
  const bc = {};
691
737
  for (const x in TYPE_NAMES) {
@@ -707,7 +753,7 @@ export const BuiltinFuncs = function() {
707
753
  return out;
708
754
  };
709
755
 
710
- this.__Porffor_pointer = {
756
+ this.__Porffor_ptr = {
711
757
  params: [ valtypeBinary, Valtype.i32 ],
712
758
  typedParams: true,
713
759
  locals: [ Valtype.i32, Valtype.i32 ],
@@ -722,207 +768,34 @@ export const BuiltinFuncs = function() {
722
768
  ]
723
769
  };
724
770
 
725
-
726
- this.__SIMD_i32x4_load = {
727
- params: [ Valtype.i32 ],
728
- locals: [],
729
- returns: [ Valtype.v128 ],
730
- wasm: [
771
+ this.__Porffor_i32_ptr = {
772
+ params: [ Valtype.i32, Valtype.i32 ],
773
+ typedParams: true,
774
+ locals: [ Valtype.i32, Valtype.i32 ],
775
+ returns: [ Valtype.i32 ],
776
+ wasm: (scope, { TYPES }) => [
777
+ ...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
778
+ [ Opcodes.if, Valtype.i32 ],
731
779
  [ Opcodes.local_get, 0 ],
732
- [ ...Opcodes.v128_load, 0, 0 ]
780
+ [ Opcodes.else ],
781
+ ...number(-1, Valtype.i32),
782
+ [ Opcodes.end ]
733
783
  ]
734
784
  };
735
785
 
736
- this.__SIMD_i32x4_splat = {
786
+ // unsafe: does not check type just ~casts to number
787
+ this.__Porffor_i32_ptrUnsafe = {
737
788
  params: [ Valtype.i32 ],
738
789
  locals: [],
739
- returns: [ Valtype.v128 ],
740
- wasm: [
741
- [ Opcodes.local_get, 0 ],
742
- [ ...Opcodes.i32x4_splat ],
743
- ]
744
- };
745
-
746
- this.__SIMD_i16x8_create = {
747
- params: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32 ],
748
- locals: [],
749
- returns: [ Valtype.v128 ],
750
- wasm: [
751
- ...i32x4(0, 0, 0, 0),
752
- [ Opcodes.local_get, 0 ],
753
- [ ...Opcodes.i16x8_replace_lane, 0 ],
754
- [ Opcodes.local_get, 1 ],
755
- [ ...Opcodes.i16x8_replace_lane, 1 ],
756
- [ Opcodes.local_get, 2 ],
757
- [ ...Opcodes.i16x8_replace_lane, 2 ],
758
- [ Opcodes.local_get, 3 ],
759
- [ ...Opcodes.i16x8_replace_lane, 3 ],
760
- [ Opcodes.local_get, 4 ],
761
- [ ...Opcodes.i16x8_replace_lane, 4 ],
762
- [ Opcodes.local_get, 5 ],
763
- [ ...Opcodes.i16x8_replace_lane, 5 ],
764
- [ Opcodes.local_get, 6 ],
765
- [ ...Opcodes.i16x8_replace_lane, 6 ],
766
- [ Opcodes.local_get, 7 ],
767
- [ ...Opcodes.i16x8_replace_lane, 7 ],
768
- ]
769
- };
770
-
771
- this.__SIMD_i32x4_dot_i16x8 = {
772
- params: [ Valtype.v128, Valtype.v128 ],
773
- locals: [],
774
- returns: [ Valtype.v128 ],
775
- wasm: [
776
- [ Opcodes.local_get, 0 ],
777
- [ Opcodes.local_get, 1 ],
778
- [ ...Opcodes.i32x4_dot_i16x8_s ]
779
- ]
780
- };
781
-
782
- this.__SIMD_i32x4_create = {
783
- params: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32 ],
784
- locals: [],
785
- returns: [ Valtype.v128 ],
786
- wasm: [
787
- ...i32x4(0, 0, 0, 0),
788
- [ Opcodes.local_get, 0 ],
789
- [ ...Opcodes.i32x4_replace_lane, 0 ],
790
- [ Opcodes.local_get, 1 ],
791
- [ ...Opcodes.i32x4_replace_lane, 1 ],
792
- [ Opcodes.local_get, 2 ],
793
- [ ...Opcodes.i32x4_replace_lane, 2 ],
794
- [ Opcodes.local_get, 3 ],
795
- [ ...Opcodes.i32x4_replace_lane, 3 ],
796
- ]
797
- };
798
-
799
- this.__SIMD_i32x4_add = {
800
- params: [ Valtype.v128, Valtype.v128 ],
801
- locals: [],
802
- returns: [ Valtype.v128 ],
803
- wasm: [
804
- [ Opcodes.local_get, 0 ],
805
- [ Opcodes.local_get, 1 ],
806
- [ ...Opcodes.i32x4_add ]
807
- ]
808
- };
809
-
810
- this.__SIMD_i32x4_sub = {
811
- params: [ Valtype.v128, Valtype.v128 ],
812
- locals: [],
813
- returns: [ Valtype.v128 ],
814
- wasm: [
815
- [ Opcodes.local_get, 0 ],
816
- [ Opcodes.local_get, 1 ],
817
- [ ...Opcodes.i32x4_sub ]
818
- ]
819
- };
820
-
821
- this.__SIMD_i32x4_mul = {
822
- params: [ Valtype.v128, Valtype.v128 ],
823
- locals: [],
824
- returns: [ Valtype.v128 ],
825
- wasm: [
826
- [ Opcodes.local_get, 0 ],
827
- [ Opcodes.local_get, 1 ],
828
- [ ...Opcodes.i32x4_mul ]
829
- ]
830
- };
831
-
832
- this.__SIMD_i32x4_get0 = {
833
- params: [ Valtype.v128 ],
834
- locals: [],
835
790
  returns: [ Valtype.i32 ],
836
791
  wasm: [
837
792
  [ Opcodes.local_get, 0 ],
838
- [ ...Opcodes.i32x4_extract_lane, 0 ],
839
- ],
840
- },
841
-
842
- this.__SIMD_i32x4_get1 = {
843
- params: [ Valtype.v128 ],
844
- locals: [],
845
- returns: [ Valtype.i32 ],
846
- wasm: [
847
- [ Opcodes.local_get, 0 ],
848
- [ ...Opcodes.i32x4_extract_lane, 1 ],
849
- ],
850
- };
851
-
852
- this.__SIMD_i32x4_get2 = {
853
- params: [ Valtype.v128 ],
854
- locals: [],
855
- returns: [ Valtype.i32 ],
856
- wasm: [
857
- [ Opcodes.local_get, 0 ],
858
- [ ...Opcodes.i32x4_extract_lane, 2 ],
859
- ],
860
- };
861
-
862
- this.__SIMD_i32x4_get3 = {
863
- params: [ Valtype.v128 ],
864
- locals: [],
865
- returns: [ Valtype.i32 ],
866
- wasm: [
867
- [ Opcodes.local_get, 0 ],
868
- [ ...Opcodes.i32x4_extract_lane, 3 ],
869
- ],
870
- };
871
-
872
- this.__SIMD_i32x4_shuffle_000c = {
873
- params: [ Valtype.v128 ],
874
- locals: [],
875
- returns: [ Valtype.v128 ],
876
- wasm: [
877
- [ Opcodes.local_get, 0 ],
878
- ...i32x4(0, 0, 0, 0),
879
- [ ...Opcodes.i8x16_shuffle, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 9, 10, 11 ], // i32x4 (a, b, c, d) -> i32x4 (0, 0, 0, c)
880
- ]
881
- };
882
-
883
- this.__SIMD_i32x4_shuffle_00ab = {
884
- params: [ Valtype.v128 ],
885
- locals: [],
886
- returns: [ Valtype.v128 ],
887
- wasm: [
888
- [ Opcodes.local_get, 0 ],
889
- ...i32x4(0, 0, 0, 0),
890
- [ ...Opcodes.i8x16_shuffle, 16, 16, 16, 16, 16, 16, 16, 16, 0, 1, 2, 3, 4, 5, 6, 7 ], // i32x4 (a, b, c, d) -> i32x4 (0, 0, a, b)
891
793
  ]
892
794
  };
893
- };
894
795
 
895
- export const BuiltinPreludes = {
896
- btoa: `var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
897
- var btoa = function (input) {
898
- // todo: throw invalid character for unicode
899
-
900
- let output = "";
901
- let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
902
- let i = 0;
903
-
904
- while (i < input.length) {
905
- chr1 = input.charCodeAt(i++);
906
- chr2 = input.charCodeAt(i++);
907
- chr3 = input.charCodeAt(i++);
908
-
909
- enc1 = chr1 >> 2;
910
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
911
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
912
- enc4 = chr3 & 63;
913
-
914
- if (isNaN(chr2)) {
915
- enc3 = enc4 = 64;
916
- } else if (isNaN(chr3)) {
917
- enc4 = 64;
918
- }
919
796
 
920
- output += keyStr.charAt(enc1);
921
- output += keyStr.charAt(enc2);
922
- output += keyStr.charAt(enc3);
923
- output += keyStr.charAt(enc4);
797
+ const generated = new GeneratedBuiltins.BuiltinFuncs();
798
+ for (const x in generated) {
799
+ this[x] = generated[x];
924
800
  }
925
-
926
- return output;
927
- };`
928
801
  };