-
Notifications
You must be signed in to change notification settings - Fork 42
/
unpack.cpp
2444 lines (2224 loc) · 64.2 KB
/
unpack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vim: set ts=2 sw=2 tw=99 et:
#include "unpack.h"
#include "shared.h"
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#ifdef __EMSCRIPTEN__
# include <emscripten.h>
#endif
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
using namespace std;
// TODO:
// - Use pool allocator for vector et al to avoid malloc (or at least inline allocation).
// - include ascii magic number (to ensure we got the right file)
// - add a built-with-version to easily catch tool mismatches
namespace asmjs {
const uint8_t iden_chars[] = {
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'_', '$',
'0','1','2','3','4','5','6','7','8','9'
};
static const unsigned FirstCharRange = 26 * 2 + 2;
static const unsigned FirstCharRangeMinusDollar = 26 * 2 + 1;
static const unsigned NextCharRange = sizeof(iden_chars);
enum class HotStdLib : unsigned
{
HeapS8,
HeapU8,
HeapS16,
HeapU16,
HeapS32,
HeapU32,
HeapF32,
HeapF64,
IMul,
FRound,
Count
};
enum class StdLib : unsigned
{
stdlib,
foreign,
buffer,
acos,
asin,
atan,
cos,
sin,
tan,
exp,
log,
ceil,
floor,
sqrt,
abs,
min,
max,
atan2,
pow,
clz32,
NaN,
Infinity,
Count
};
class Utf8Writer
{
uint8_t* cur_;
#ifdef CHECKED_OUTPUT_SIZE
uint8_t* limit_;
vector<uint8_t> bytes_;
void check_write(int32_t bytes_to_write)
{
if (limit_ - cur_ >= bytes_to_write)
return;
size_t cur_off = cur_ - bytes_.data();
bytes_.resize(cur_off + bytes_to_write + 16*1024);
cur_ = bytes_.data() + cur_off;
limit_ = bytes_.data() + bytes_.size();
}
#else
# ifndef NDEBUG
uint8_t* const begin_;
const uint32_t unpacked_size_;
# endif
void inline check_write(size_t bytes_to_write)
{
assert((cur_ - begin_) + bytes_to_write <= unpacked_size_);
}
#endif
// In the encoded format, variables are named by dense indices. A straight ASCII-decimal encoding
// would only use 10 of the 64 possible single-byte UTF8 JS identifiers and thus generate longer
// variable names than necessary. (Since the goal is speed and non-ASCII identifiers often trigger
// slow paths in JS tokenizers, only consider the ASCII subset of UTF-8.) This function maps
// indices surjectively onto the enumeration of valid ASCII JS identifiers.
template <uint32_t FirstCharRange>
void indexed_name(uint32_t i)
{
// Take advantage of power-of-two length of iden_chars to replace slow integer division and
// modulus operations with fast bitwise operations.
static_assert(sizeof(iden_chars) == 64, "assumed below");
if (i < FirstCharRange) {
check_write(1);
*cur_++ = iden_chars[i];
return;
}
i -= FirstCharRange;
if (i < FirstCharRange * 64) {
check_write(2);
*cur_++ = iden_chars[i >> 6];
*cur_++ = iden_chars[i & 0x3f];
// Append _ if we would otherwise generate one of the two-letter keywords.
assert(iden_chars[0x205>>6] == 'i' && iden_chars[0x205&0x3f] == 'f');
assert(iden_chars[0x20d>>6] == 'i' && iden_chars[0x20d&0x3f] == 'n');
assert(iden_chars[0x0ce>>6] == 'd' && iden_chars[0x0ce&0x3f] == 'o');
if (FirstCharRange < NextCharRange && (i == 0x205 || i == 0x20d || i == 0x0ce)) {
check_write(1);
*cur_++ = '_';
}
return;
}
i -= FirstCharRange * 64;
// Instead of trying to catch every >2 letter keyword, just inject a _.
if (FirstCharRange < NextCharRange) {
check_write(1);
*cur_++ = '_';
}
uint32_t len = 0;
do {
check_write(1);
*cur_++ = iden_chars[i & 0x3f];
len++;
i = i >> 6;
} while (i >= FirstCharRange * 64);
check_write(2);
*cur_++ = iden_chars[i & 0x3f];
*cur_++ = iden_chars[i >> 6];
len += 2;
reverse(cur_ - len, cur_);
}
public:
#ifdef CHECKED_OUTPUT_SIZE
Utf8Writer() : cur_(nullptr), limit_(nullptr) {}
uint32_t finish() { return cur_ - bytes_.data(); }
#else
Utf8Writer(uint32_t sz, uint8_t* begin)
: cur_(begin)
#ifndef NDEBUG
, begin_(begin)
, unpacked_size_(sz)
#endif
{}
void finish()
{
assert(unpacked_size_ == cur_ - begin_);
}
#endif
bool has_token_ambiguity(char c)
{
return c == '+' || c == '-';
}
template <unsigned N>
void ascii(const char (&str)[N])
{
if (N > 0 && has_token_ambiguity(str[0]) && cur_[-1] == str[0]) {
check_write(1);
*cur_++ = ' ';
}
for (unsigned i = 0; i < N-1; i++) {
check_write(1);
*cur_++ = str[i];
}
}
void ascii(unsigned char c)
{
assert(c < 0x80);
if (has_token_ambiguity(c) && cur_[-1] == c) {
check_write(1);
*cur_++ = ' ';
}
check_write(1);
*cur_++ = c;
}
void dynamic_ascii(const char *str)
{
while (*str)
ascii(*str++);
}
void uint32(uint32_t u32)
{
if (u32 <= 9) {
check_write(1);
*cur_++ = '0' + u32;
return;
}
uint32_t len = 0;
do {
check_write(1);
*cur_++ = '0' + u32 % 10;
len++;
u32 /= 10;
} while (u32 > 0);
reverse(cur_ - len, cur_);
}
void int32(int32_t i32)
{
if (i32 >= 0) {
uint32(i32);
} else {
ascii('-');
uint32(-i32);
}
}
void float32(double f)
{
name(HotStdLib::FRound);
ascii('(');
float64((double)f, false); // no need for decimal point when going into fround() call
ascii(')');
}
void float64(double d, bool needDecimalPoint = true)
{
if (std::isnan(d))
return name(StdLib::NaN);
if (std::isinf(d))
return name(StdLib::Infinity);
// handle negative numbers. to detect -0 (which has -0 == 0), we must
// inspect the sign bit.
if (d < 0 || (d == 0 && std::signbit((float)d) == 1)) {
ascii('-');
d = -d;
}
static const unsigned N = 100;
char buf[N];
uint32_t len;
#if __EMSCRIPTEN__
len = emscripten_print_double(d, buf, N);
assert(len < N-1);
buf[len] = 0;
#else
len = snprintf(buf, N, "%g", d);
#endif
check_write(len);
memcpy(cur_, buf, len);
uint8_t *beg = cur_;
cur_ += len;
if (!needDecimalPoint)
return;
for (uint8_t* p = beg; p != cur_; ++p) {
if (*p == '.')
return;
}
for (uint8_t* p = beg; p != cur_; ++p) {
if (*p == 'e' || *p == 'E') {
check_write(1);
for (uint8_t* q = cur_++; q != p; q--)
*q = *(q - 1);
*p = '.';
return;
}
}
check_write(1);
*cur_++ = '.';
}
void name(HotStdLib i)
{
assert(i < HotStdLib::Count);
check_write(1);
*cur_++ = 'a' + unsigned(i);
}
void local_name(uint32_t i)
{
indexed_name<FirstCharRangeMinusDollar>(i + uint32_t(HotStdLib::Count));
}
void name(StdLib i)
{
assert(i < StdLib::Count);
check_write(2);
*cur_++ = '$';
*cur_++ = 'a' + unsigned(i);
}
void global_name(uint32_t i)
{
check_write(1);
*cur_++ = '$';
indexed_name<NextCharRange>(i + uint32_t(StdLib::Count));
}
void label_name(uint32_t i)
{
indexed_name<FirstCharRange>(i);
}
};
template <>
void
Utf8Writer::ascii(const char (&str)[1])
{
assert(!str[0]);
}
template <>
void
Utf8Writer::ascii(const char (&str)[2])
{
if (has_token_ambiguity(str[0]) && cur_[-1] == str[0]) {
check_write(1);
*cur_++ = ' ';
}
check_write(1);
*cur_++ = str[0];
assert(!str[1]);
}
template <>
void
Utf8Writer::ascii(const char (&str)[3])
{
if (has_token_ambiguity(str[0]) && cur_[-1] == str[0]) {
check_write(1);
*cur_++ = ' ';
}
check_write(2);
*cur_++ = str[0];
*cur_++ = str[1];
assert(!str[2]);
}
template <>
void
Utf8Writer::ascii(const char (&str)[4])
{
if (has_token_ambiguity(str[0]) && cur_[-1] == str[0]) {
check_write(1);
*cur_++ = ' ';
}
check_write(3);
*cur_++ = str[0];
*cur_++ = str[1];
*cur_++ = str[2];
assert(!str[3]);
}
struct FuncImportSignature
{
uint32_t sig_index;
uint32_t func_imp_index;
};
struct FuncPtrTable
{
uint32_t sig_index;
vector<uint32_t> elems;
};
uint32_t
cb_name_len(const char* cb_name)
{
if (!cb_name)
return 0;
return strlen(cb_name) + 2; // cb_name(...)
}
class State
{
vector<Signature> sigs_;
vector<uint32_t> i32s_;
vector<float> f32s_;
vector<double> f64s_;
uint32_t num_func_imps_;
vector<FuncImportSignature> func_imp_sigs_;
vector<Type> global_types_;
uint32_t func_name_base_;
vector<uint32_t> func_sigs_;
uint32_t func_ptr_table_name_base_;
vector<FuncPtrTable> func_ptr_tables_;
uint32_t num_labels_;
RType cur_ret_;
vector<Type> cur_local_types_;
public:
In read;
Utf8Writer write;
#ifdef CHECKED_OUTPUT_SIZE
State(const uint8_t* in)
: num_labels_(0)
, read(in)
{
if (read.fixed_width<uint32_t>() != MagicNumber)
abort();
(void)read.fixed_width<uint32_t>();
}
#else
State(const uint8_t* in, const char* cb_name, uint32_t out_size, uint8_t* out)
: num_labels_(0)
, read(in)
, write(out_size, out)
{
if (read.fixed_width<uint32_t>() != MagicNumber)
abort();
if (read.fixed_width<uint32_t>() != out_size - cb_name_len(cb_name))
abort();
}
#endif
void set_sigs(vector<Signature>&& sigs)
{
sigs_ = move(sigs);
}
void set_pools(vector<uint32_t>&& i32s, vector<float>&& f32s, vector<double>&& f64s)
{
i32s_ = move(i32s);
f32s_ = move(f32s);
f64s_ = move(f64s);
}
void set_func_imps(uint32_t num_func_imps, vector<FuncImportSignature>&& func_imp_sigs)
{
num_func_imps_ = num_func_imps;
func_imp_sigs_ = move(func_imp_sigs);
}
void set_global_types(vector<Type>&& global_types)
{
global_types_ = move(global_types);
func_name_base_ = num_func_imps_ + global_types_.size();
}
void set_funcs(vector<uint32_t>&& func_sigs)
{
func_sigs_ = move(func_sigs);
func_ptr_table_name_base_ = func_name_base_ + func_sigs_.size();
}
void set_func_ptr_tables(vector<FuncPtrTable>&& f)
{
func_ptr_tables_ = move(f);
}
void write_func_imp_name(uint32_t i)
{
write.global_name(i);
}
void write_global_name(uint32_t i)
{
write.global_name(num_func_imps_ + i);
}
void write_func_name(uint32_t i)
{
write.global_name(func_name_base_ + i);
}
void write_func_ptr_table_name(uint32_t i)
{
write.global_name(func_ptr_table_name_base_ + i);
}
const vector<Signature>& sigs() const { return sigs_; }
const Signature& sig(size_t i) const { return sigs_[i]; }
Type global_type(size_t i) { return global_types_[i]; }
const FuncImportSignature& func_imp_sig(size_t i) const { return func_imp_sigs_[i]; }
size_t num_funcs() const { return func_sigs_.size(); }
const Signature& func_sig(size_t i) { return sigs_[func_sigs_[i]]; }
size_t num_func_ptr_tables() const { return func_ptr_tables_.size(); }
const FuncPtrTable& func_ptr_table(size_t i) { return func_ptr_tables_[i]; }
const vector<uint32_t>& i32s() const { return i32s_; }
const vector<float>& f32s() const { return f32s_; }
const vector<double>& f64s() const { return f64s_; }
void set_cur_ret(RType ret) { cur_ret_ = ret; }
RType cur_ret() const { return cur_ret_; }
vector<Type>& cur_local_types() { return cur_local_types_; }
Type cur_local_type(size_t i) const { return cur_local_types_[i]; }
uint32_t push_label() { return num_labels_++; }
void pop_label() { num_labels_--; }
};
void
signature_section(State& s)
{
uint32_t num_sigs = s.read.imm_u32();
vector<Signature> sigs(num_sigs);
for (uint32_t sig_i = 0; sig_i != num_sigs; sig_i++) {
RType ret = s.read.rtype();
uint32_t num_args = s.read.imm_u32();
Signature sig(ret, num_args);
for (uint32_t arg_i = 0; arg_i < num_args; arg_i++)
sig.args[arg_i] = s.read.type();
sigs[sig_i] = move(sig);
}
s.set_sigs(move(sigs));
}
void
function_import_section(State& s)
{
uint32_t num_func_imps = s.read.imm_u32();
uint32_t num_func_imp_sigs = s.read.imm_u32();
vector<FuncImportSignature> func_imp_sigs(num_func_imp_sigs);
FuncImportSignature* func_imp_sig = func_imp_sigs.data();
for (uint32_t func_imp_i = 0; func_imp_i < num_func_imps; func_imp_i++) {
s.write.ascii("var ");
s.write_func_imp_name(func_imp_i);
s.write.ascii('=');
s.write.name(StdLib::foreign);
s.write.ascii('.');
while (char c = s.read.single_char())
s.write.ascii(c);
s.write.ascii(";\n");
uint32_t num_sigs = s.read.imm_u32();
for (uint32_t i = 0; i < num_sigs; i++, func_imp_sig++) {
func_imp_sig->sig_index = s.read.imm_u32();
func_imp_sig->func_imp_index = func_imp_i;
}
}
assert(func_imp_sig == func_imp_sigs.data() + num_func_imp_sigs);
s.set_func_imps(num_func_imps, move(func_imp_sigs));
}
void
global_section(State& s)
{
uint32_t num_i32_zero = s.read.imm_u32();
uint32_t num_f32_zero = s.read.imm_u32();
uint32_t num_f64_zero = s.read.imm_u32();
uint32_t num_i32_import = s.read.imm_u32();
uint32_t num_f32_import = s.read.imm_u32();
uint32_t num_f64_import = s.read.imm_u32();
uint32_t num_global_vars = num_i32_zero +
num_f32_zero +
num_f64_zero +
num_i32_import +
num_f32_import +
num_f64_import;
vector<Type> global_types;
global_types.reserve(num_global_vars);
for (uint32_t i = 0; i < num_i32_zero; i++) {
s.write.ascii("var ");
s.write_global_name(global_types.size());
s.write.ascii("=0;\n");
global_types.push_back(Type::I32);
}
for (uint32_t i = 0; i < num_f32_zero; i++) {
s.write.ascii("var ");
s.write_global_name(global_types.size());
s.write.ascii('=');
s.write.name(HotStdLib::FRound);
s.write.ascii("(0);\n");
global_types.push_back(Type::F32);
}
for (uint32_t i = 0; i < num_f64_zero; i++) {
s.write.ascii("var ");
s.write_global_name(global_types.size());
s.write.ascii("=0.;\n");
global_types.push_back(Type::F64);
}
for (uint32_t i = 0; i < num_i32_import; i++) {
s.write.ascii("var ");
s.write_global_name(global_types.size());
s.write.ascii('=');
s.write.name(StdLib::foreign);
s.write.ascii('.');
while (char c = s.read.single_char())
s.write.ascii(c);
s.write.ascii("|0;\n");
global_types.push_back(Type::I32);
}
for (uint32_t i = 0; i < num_f32_import; i++) {
s.write.ascii("var ");
s.write_global_name(global_types.size());
s.write.ascii("=");
s.write.name(HotStdLib::FRound);
s.write.ascii('(');
s.write.name(StdLib::foreign);
s.write.ascii('.');
while (char c = s.read.single_char())
s.write.ascii(c);
s.write.ascii(");\n");
global_types.push_back(Type::F32);
}
for (uint32_t i = 0; i < num_f64_import; i++) {
s.write.ascii("var ");
s.write_global_name(global_types.size());
s.write.ascii("=+");
s.write.name(StdLib::foreign);
s.write.ascii('.');
while (char c = s.read.single_char())
s.write.ascii(c);
s.write.ascii(";\n");
global_types.push_back(Type::F64);
}
assert(global_types.size() == num_global_vars);
s.set_global_types(move(global_types));
}
void
constant_pool_section(State& s)
{
uint32_t num_i32s = s.read.imm_u32();
uint32_t num_f32s = s.read.imm_u32();
uint32_t num_f64s = s.read.imm_u32();
vector<uint32_t> i32s(num_i32s);
for (uint32_t i = 0; i < num_i32s; i++)
i32s[i] = s.read.imm_u32();
vector<float> f32s(num_f32s);
for (uint32_t i = 0; i < num_f32s; i++)
f32s[i] = s.read.fixed_width<float>();
vector<double> f64s(num_f64s);
for (uint32_t i = 0; i < num_f64s; i++)
f64s[i] = s.read.fixed_width<double>();
s.set_pools(move(i32s), move(f32s), move(f64s));
}
void
function_declaration_section(State& s)
{
uint32_t num_funcs = s.read.imm_u32();
vector<uint32_t> func_sigs(num_funcs);
for (uint32_t i = 0; i != num_funcs; i++)
func_sigs[i] = s.read.imm_u32();
s.set_funcs(move(func_sigs));
}
void
read_function_pointer_tables(State& s)
{
uint32_t num_func_ptr_tables = s.read.imm_u32();
vector<FuncPtrTable> func_ptr_tables(num_func_ptr_tables);
for (uint32_t func_ptr_tbl_i = 0; func_ptr_tbl_i != num_func_ptr_tables; func_ptr_tbl_i++) {
func_ptr_tables[func_ptr_tbl_i].sig_index = s.read.imm_u32();
uint32_t num_elems = s.read.imm_u32();
vector<uint32_t> elems(num_elems);
for (uint32_t elem_i = 0; elem_i != num_elems; elem_i++)
elems[elem_i] = s.read.imm_u32();
func_ptr_tables[func_ptr_tbl_i].elems = move(elems);
}
s.set_func_ptr_tables(move(func_ptr_tables));
}
enum class Prec : unsigned {
Lowest = 0,
Comma = 2,
Assign = 4,
Cond = 6,
BitOr = 8,
BitXor = 10,
BitAnd = 12,
Eq = 14,
Comp = 16,
Shifts = 18,
AddSub = 20,
MulDivMod = 22,
Unary = 24,
Call = 26,
Index = 28,
Highest = 30
};
Prec reject_same(Prec prec) { return prec; }
Prec accept_same(Prec prec) { return Prec(unsigned(prec) - 1); }
bool need_paren(Prec parent, Prec child) { return unsigned(child) <= unsigned(parent); }
enum class Ctx
{
Default,
AddSub,
ToI32,
FRound,
ToNumber
};
template <RType T> void expr(State& s, Prec prec, Ctx ctx = Ctx::Default);
void expr_i32(State&, Prec, Ctx);
void expr_f32(State&, Prec, Ctx);
void expr_f64(State&, Prec, Ctx);
void expr_void(State&, Prec, Ctx);
template <> void expr<RType::I32>(State& s, Prec prec, Ctx ctx) { expr_i32(s, prec, ctx); }
template <> void expr<RType::F32>(State& s, Prec prec, Ctx ctx) { expr_f32(s, prec, ctx); }
template <> void expr<RType::F64>(State& s, Prec prec, Ctx ctx) { expr_f64(s, prec, ctx); }
template <> void expr<RType::Void>(State& s, Prec prec, Ctx ctx) { expr_void(s, prec, ctx); }
void
signed_expr(State& s, Prec prec, Signedness si)
{
uint32_t u32;
if (s.read.if_i32_lit(s.i32s(), &u32)) {
if (si == Signed)
s.write.int32(u32);
else
s.write.uint32(u32);
} else if (si == Signed) {
if (need_paren(prec, Prec::BitOr)) {
s.write.ascii('(');
expr<RType::I32>(s, accept_same(Prec::BitOr), Ctx::ToI32);
s.write.ascii("|0");
s.write.ascii(')');
} else {
expr<RType::I32>(s, accept_same(Prec::BitOr), Ctx::ToI32);
s.write.ascii("|0");
}
} else {
if (need_paren(prec, Prec::Shifts)) {
s.write.ascii('(');
expr<RType::I32>(s, accept_same(Prec::Shifts), Ctx::ToI32);
s.write.ascii(">>>0");
s.write.ascii(')');
} else {
expr<RType::I32>(s, accept_same(Prec::Shifts), Ctx::ToI32);
s.write.ascii(">>>0");
}
}
}
void
get_local(State& s)
{
s.write.local_name(s.read.imm_u32());
}
template <Type T>
void
set_local(State& s, uint32_t imm)
{
s.write.local_name(imm);
s.write.ascii('=');
expr<RType(T)>(s, accept_same(Prec::Assign));
}
template <Type T>
void
set_local(State& s, Prec prec, uint32_t imm)
{
if (need_paren(prec, Prec::Assign)) {
s.write.ascii('(');
set_local<T>(s, imm);
s.write.ascii(')');
} else {
set_local<T>(s, imm);
}
}
template <Type T>
void
set_local(State& s, Prec prec)
{
set_local<T>(s, prec, s.read.imm_u32());
}
void
set_local(State& s, Prec prec, uint32_t imm)
{
switch (s.cur_local_type(imm)) {
case Type::I32: set_local<Type::I32>(s, prec, imm); break;
case Type::F32: set_local<Type::F32>(s, prec, imm); break;
case Type::F64: set_local<Type::F64>(s, prec, imm); break;
default: unreachable<void>();
}
}
void
set_local(State& s, Prec prec)
{
set_local(s, prec, s.read.imm_u32());
}
void
get_global(State& s)
{
s.write_global_name(s.read.imm_u32());
}
template <Type T>
void
set_global(State& s, uint32_t imm)
{
s.write_global_name(imm);
s.write.ascii('=');
expr<RType(T)>(s, accept_same(Prec::Assign));
}
template <Type T>
void
set_global(State& s, Prec prec, uint32_t imm)
{
if (need_paren(prec, Prec::Assign)) {
s.write.ascii('(');
set_global<T>(s, imm);
s.write.ascii(')');
} else {
set_global<T>(s, imm);
}
}
template <Type T>
void
set_global(State& s, Prec prec)
{
set_global<T>(s, prec, s.read.imm_u32());
}
void
set_global(State& s, Prec prec, uint32_t imm)
{
switch (s.global_type(imm)) {
case Type::I32: set_global<Type::I32>(s, prec, imm); break;
case Type::F32: set_global<Type::F32>(s, prec, imm); break;
case Type::F64: set_global<Type::F64>(s, prec, imm); break;
default: unreachable<void>();
}
}
void
set_global(State& s, Prec prec)
{
set_global(s, prec, s.read.imm_u32());
}
void
index(State& s, HotStdLib name, unsigned shift, bool has_offset)
{
s.write.name(name);
s.write.ascii('[');
uint32_t offset = has_offset ? s.read.imm_u32() : 0;
uint32_t u32;
if (s.read.if_i32_lit(s.i32s(), &u32)) {
u32 += offset;
if (u32 < 16*1024*1024) {
s.write.uint32(u32 >> shift);
} else {
s.write.uint32(u32);
s.write.ascii(">>");
s.write.uint32(shift);
}
} else {
if (offset) {
expr<RType::I32>(s, accept_same(Prec::AddSub), Ctx::AddSub);
s.write.ascii('+');
s.write.uint32(offset);
} else {
expr<RType::I32>(s, accept_same(Prec::Shifts), Ctx::ToI32);
}
s.write.ascii(">>");
s.write.uint32(shift);
}
s.write.ascii("]");
}
void
load_i32(State& s, Prec prec, Ctx ctx, HotStdLib name, unsigned shift, bool offset)
{
if (ctx != Ctx::ToI32) {
if (need_paren(prec, Prec::BitOr)) {
s.write.ascii('(');
index(s, name, shift, offset);
s.write.ascii("|0)");
} else {
index(s, name, shift, offset);
s.write.ascii("|0");
}
} else {
index(s, name, shift, offset);
}
}
void
load_f32(State& s, Prec prec, Ctx ctx, bool offset)
{
if (ctx != Ctx::FRound && ctx != Ctx::ToNumber) {
if (need_paren(prec, Prec::Unary)) {
s.write.ascii('(');
s.write.name(HotStdLib::FRound);
s.write.ascii('(');
index(s, HotStdLib::HeapF32, 2, offset);
s.write.ascii("))");
} else {
s.write.name(HotStdLib::FRound);
s.write.ascii('(');
index(s, HotStdLib::HeapF32, 2, offset);
s.write.ascii(')');
}
} else {
index(s, HotStdLib::HeapF32, 2, offset);
}
}
void
load_f64(State& s, Prec prec, Ctx ctx, bool offset)
{
if (ctx != Ctx::ToNumber) {
if (need_paren(prec, Prec::Unary)) {
s.write.ascii("(+");
index(s, HotStdLib::HeapF64, 3, offset);
s.write.ascii(')');
} else {
s.write.ascii('+');
index(s, HotStdLib::HeapF64, 3, offset);
}
} else {
index(s, HotStdLib::HeapF64, 3, offset);
}
}
template <RType T>
void
store(State& s, Prec prec, HotStdLib name, unsigned shift, Ctx child_ctx, bool offset)
{
if (need_paren(prec, Prec::Assign)) {
s.write.ascii('(');
index(s, name, shift, offset);
s.write.ascii('=');
expr<T>(s, accept_same(Prec::Assign), child_ctx);
s.write.ascii(')');
} else {
index(s, name, shift, offset);
s.write.ascii('=');
expr<T>(s, accept_same(Prec::Assign), child_ctx);
}
}
void
store_i8(State& s, Prec prec, bool offset)
{
store<RType::I32>(s, prec, HotStdLib::HeapS8, 0, Ctx::ToI32, offset);
}
void
store_i16(State& s, Prec prec, bool offset)
{
store<RType::I32>(s, prec, HotStdLib::HeapS16, 1, Ctx::ToI32, offset);
}
void
store_i32(State& s, Prec prec, bool offset)
{
store<RType::I32>(s, prec, HotStdLib::HeapS32, 2, Ctx::ToI32, offset);
}