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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
|
/*
* Copyright 2016 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm-binary-reader-ast.h"
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include "wasm-allocator.h"
#include "wasm-ast.h"
#include "wasm-binary-reader.h"
#include "wasm-common.h"
#define CHECK_ALLOC_(ctx, cond) \
do { \
if (!(cond)) { \
print_error((ctx), "%s:%d: allocation failed", __FILE__, __LINE__); \
return WASM_ERROR; \
} \
} while (0)
#define CHECK_ALLOC(ctx, e) CHECK_ALLOC_((ctx), (e) == WASM_OK)
#define CHECK_ALLOC_NULL(ctx, v) CHECK_ALLOC_((ctx), (v))
#define CHECK_ALLOC_NULL_STR(ctx, v) CHECK_ALLOC_((ctx), (v).start)
#define CHECK_RESULT(expr) \
if ((expr) != WASM_OK) \
return WASM_ERROR; \
else \
(void)0
#define CHECK_DEPTH(ctx, depth) \
if ((depth) >= (ctx)->depth_stack.size) { \
print_error((ctx), "invalid depth: %d (max %d)", (depth), \
(int)((ctx)->depth_stack.size)); \
return WASM_ERROR; \
} else \
(void)0
#define CHECK_LOCAL(ctx, local_index) \
do { \
assert((ctx)->current_func->flags& WASM_FUNC_FLAG_HAS_FUNC_TYPE); \
uint32_t max_local_index = \
get_num_func_params_and_locals((ctx)->module, (ctx)->current_func); \
if ((local_index) >= max_local_index) { \
print_error((ctx), "invalid local_index: %d (max %d)", (local_index), \
max_local_index); \
return WASM_ERROR; \
} \
} while (0)
#define LOG 0
#define UNKNOWN_OFFSET ((uint32_t)~0)
typedef struct WasmExprNode {
WasmExpr* expr;
uint32_t index;
uint32_t total;
} WasmExprNode;
WASM_DEFINE_VECTOR(expr_node, WasmExprNode);
typedef uint32_t WasmUint32;
WASM_DEFINE_VECTOR(uint32, WasmUint32);
typedef struct WasmReadAstContext {
WasmAllocator* allocator;
WasmModule* module;
WasmFunc* current_func;
WasmExprNodeVector expr_stack;
/* TODO(binji): remove all this if-block stuff when we switch to postorder */
WasmUint32Vector depth_stack;
uint32_t depth;
/* currently there is a discrepancy between the wast format and the binary
* format w.r.t. whether the if branches introduce new blocks. As a result,
* if we see an if or if_else operator in the binary format that does not
* have a block as the branch expression, we'll have to introduce a fake
* block so that the break depths are correct for any nested br operators.
*
* |just_pushed_fake_depth| works as follows: we pre-emptively assume that an
* expression does not have a nested block, and introduce a fake block. If
* the next expression we see is a block, then we reverse that and add the
* real block instead. */
int just_pushed_fake_depth;
} WasmReadAstContext;
static void on_error(uint32_t offset, const char* message, void* user_data);
static void print_error(WasmReadAstContext* ctx, const char* format, ...) {
va_list args;
va_list args_copy;
va_start(args, format);
va_copy(args_copy, args);
char buffer[128];
int len = wasm_vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
if (len + 1 > sizeof(buffer)) {
char* buffer2 = alloca(len + 1);
len = wasm_vsnprintf(buffer2, len + 1, format, args_copy);
va_end(args_copy);
}
on_error(UNKNOWN_OFFSET, buffer, ctx);
}
static WasmFuncSignature* get_func_sig(WasmModule* module, WasmFunc* func) {
return &module->func_types.data[func->type_var.index]->sig;
}
static uint32_t get_num_func_params(WasmModule* module, WasmFunc* func) {
WasmFuncSignature* sig = get_func_sig(module, func);
return sig->param_types.size;
}
static uint32_t get_num_func_params_and_locals(WasmModule* module,
WasmFunc* func) {
uint32_t num_params = get_num_func_params(module, func);
uint32_t num_locals = func->locals.types.size;
return num_params + num_locals;
}
static WasmStringSlice dup_string_slice(WasmAllocator* allocator,
WasmStringSlice str) {
WasmStringSlice result;
result.start = wasm_strndup(allocator, str.start, str.length);
result.length = str.length;
return result;
}
/* TODO(binji): remove all this if-block stuff when we switch to postorder */
static WasmResult push_depth(WasmReadAstContext* ctx) {
uint32_t* depth = wasm_append_uint32(ctx->allocator, &ctx->depth_stack);
CHECK_ALLOC_NULL(ctx, depth);
*depth = ctx->depth;
ctx->depth++;
return WASM_OK;
}
static void pop_depth(WasmReadAstContext* ctx) {
ctx->depth--;
ctx->depth_stack.size--;
}
static void push_fake_depth(WasmReadAstContext* ctx) {
ctx->depth++;
ctx->just_pushed_fake_depth = 1;
}
static void pop_fake_depth(WasmReadAstContext* ctx) {
ctx->depth--;
}
static void pop_fake_depth_unless_block(WasmReadAstContext* ctx,
WasmExpr* expr) {
if (expr->type != WASM_EXPR_TYPE_BLOCK)
pop_fake_depth(ctx);
}
static uint32_t translate_depth(WasmReadAstContext* ctx, uint32_t depth) {
uint32_t index = ctx->depth_stack.size - depth - 1;
assert(index < ctx->depth_stack.size);
return ctx->depth - ctx->depth_stack.data[index] - 1;
}
void on_error(uint32_t offset, const char* message, void* user_data) {
if (offset == UNKNOWN_OFFSET)
fprintf(stderr, "error: %s\n", message);
else
fprintf(stderr, "error: @0x%08x: %s\n", offset, message);
}
static WasmResult begin_memory_section(void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_MEMORY;
assert(ctx->module->memory == NULL);
ctx->module->memory = &field->memory;
return WASM_OK;
}
static WasmResult on_memory_initial_size_pages(uint32_t pages,
void* user_data) {
WasmReadAstContext* ctx = user_data;
ctx->module->memory->initial_pages = pages;
return WASM_OK;
}
static WasmResult on_memory_max_size_pages(uint32_t pages, void* user_data) {
WasmReadAstContext* ctx = user_data;
ctx->module->memory->max_pages = pages;
return WASM_OK;
}
static WasmResult on_memory_exported(int exported, void* user_data) {
WasmReadAstContext* ctx = user_data;
if (exported) {
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_EXPORT_MEMORY;
/* TODO(binji): refactor */
field->export_memory.name.start = wasm_strndup(ctx->allocator, "memory", 6);
field->export_memory.name.length = 6;
assert(ctx->module->export_memory == NULL);
ctx->module->export_memory = &field->export_memory;
}
return WASM_OK;
}
static WasmResult on_data_segment_count(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
CHECK_ALLOC(ctx, wasm_reserve_segments(
ctx->allocator, &ctx->module->memory->segments, count));
return WASM_OK;
}
static WasmResult on_data_segment(uint32_t index,
uint32_t address,
const void* data,
uint32_t size,
void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(index < ctx->module->memory->segments.capacity);
WasmSegment* segment =
wasm_append_segment(ctx->allocator, &ctx->module->memory->segments);
CHECK_ALLOC_NULL(ctx, segment);
segment->addr = address;
segment->data = wasm_alloc(ctx->allocator, size, WASM_DEFAULT_ALIGN);
segment->size = size;
memcpy(segment->data, data, size);
return WASM_OK;
}
static WasmResult on_signature_count(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
CHECK_ALLOC(ctx, wasm_reserve_func_type_ptrs(
ctx->allocator, &ctx->module->func_types, count));
return WASM_OK;
}
static WasmResult on_signature(uint32_t index,
WasmType result_type,
uint32_t param_count,
WasmType* param_types,
void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_FUNC_TYPE;
WasmFuncType* func_type = &field->func_type;
WASM_ZERO_MEMORY(*func_type);
func_type->sig.result_type = result_type;
CHECK_ALLOC(ctx,
wasm_reserve_types(ctx->allocator, &func_type->sig.param_types,
param_count));
func_type->sig.param_types.size = param_count;
memcpy(func_type->sig.param_types.data, param_types,
param_count * sizeof(WasmType));
assert(index < ctx->module->func_types.capacity);
WasmFuncTypePtr* func_type_ptr =
wasm_append_func_type_ptr(ctx->allocator, &ctx->module->func_types);
*func_type_ptr = func_type;
return WASM_OK;
}
static WasmResult on_import_count(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
CHECK_ALLOC(ctx, wasm_reserve_import_ptrs(ctx->allocator,
&ctx->module->imports, count));
return WASM_OK;
}
static WasmResult on_import(uint32_t index,
uint32_t sig_index,
WasmStringSlice module_name,
WasmStringSlice function_name,
void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_IMPORT;
WasmImport* import = &field->import;
WASM_ZERO_MEMORY(*import);
import->import_type = WASM_IMPORT_HAS_TYPE;
CHECK_ALLOC_NULL_STR(
ctx, import->module_name = dup_string_slice(ctx->allocator, module_name));
CHECK_ALLOC_NULL_STR(
ctx, import->func_name = dup_string_slice(ctx->allocator, function_name));
import->type_var.type = WASM_VAR_TYPE_INDEX;
assert(sig_index < ctx->module->func_types.size);
import->type_var.index = sig_index;
assert(index < ctx->module->imports.capacity);
WasmImportPtr* import_ptr =
wasm_append_import_ptr(ctx->allocator, &ctx->module->imports);
*import_ptr = import;
return WASM_OK;
}
static WasmResult on_function_signatures_count(uint32_t count,
void* user_data) {
WasmReadAstContext* ctx = user_data;
CHECK_ALLOC(
ctx, wasm_reserve_func_ptrs(ctx->allocator, &ctx->module->funcs, count));
return WASM_OK;
}
static WasmResult on_function_signature(uint32_t index,
uint32_t sig_index,
void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_FUNC;
WasmFunc* func = &field->func;
WASM_ZERO_MEMORY(*func);
func->flags = WASM_FUNC_FLAG_HAS_FUNC_TYPE;
func->type_var.type = WASM_VAR_TYPE_INDEX;
assert(sig_index < ctx->module->func_types.size);
func->type_var.index = sig_index;
assert(index < ctx->module->funcs.capacity);
WasmFuncPtr* func_ptr =
wasm_append_func_ptr(ctx->allocator, &ctx->module->funcs);
*func_ptr = func;
return WASM_OK;
}
static WasmResult on_function_bodies_count(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(count == ctx->module->funcs.size);
(void)ctx;
return WASM_OK;
}
static WasmResult begin_function_body(uint32_t index, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(index < ctx->module->funcs.size);
ctx->current_func = ctx->module->funcs.data[index];
#if LOG
fprintf(stderr, "func %d\n", index);
#endif
return WASM_OK;
}
static WasmResult end_function_body(uint32_t index, void* user_data) {
WasmReadAstContext* ctx = user_data;
if (ctx->expr_stack.size != 0) {
print_error(ctx, "expression stack not empty on function exit! %d items",
(int)ctx->expr_stack.size);
return WASM_ERROR;
}
ctx->current_func = NULL;
return WASM_OK;
}
static WasmResult on_local_decl_count(uint32_t count, void* user_data) {
return WASM_OK;
}
static WasmResult on_local_decl(uint32_t decl_index,
uint32_t count,
WasmType type,
void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
size_t old_local_count = ctx->current_func->locals.types.size;
size_t new_local_count = old_local_count + count;
CHECK_ALLOC(
ctx, wasm_reserve_types(ctx->allocator, &ctx->current_func->locals.types,
new_local_count));
WASM_STATIC_ASSERT(sizeof(WasmType) == sizeof(uint8_t));
WasmTypeVector* types = &ctx->current_func->locals.types;
memset(&types->data[old_local_count], type, count);
types->size = new_local_count;
return WASM_OK;
}
static WasmResult reduce(WasmReadAstContext* ctx, WasmExpr* expr) {
ctx->just_pushed_fake_depth = 0;
int done = 0;
while (!done) {
done = 1;
if (ctx->expr_stack.size == 0) {
#if LOG
fprintf(stderr, "reduce: <- %u\n", expr->type);
#endif
WasmExprPtr* expr_ptr =
wasm_append_expr_ptr(ctx->allocator, &ctx->current_func->exprs);
CHECK_ALLOC_NULL(ctx, expr_ptr);
*expr_ptr = expr;
} else {
WasmExprNode* top = &ctx->expr_stack.data[ctx->expr_stack.size - 1];
assert(top->index < top->total);
#if LOG
fprintf(stderr, "reduce: %d(%d/%d) <- %d\n", top->expr->type, top->index,
top->total, expr->type);
#endif
switch (top->expr->type) {
case WASM_EXPR_TYPE_BINARY:
if (top->index == 0)
top->expr->binary.left = expr;
else
top->expr->binary.right = expr;
break;
case WASM_EXPR_TYPE_BLOCK: {
WasmExprPtr* expr_ptr =
wasm_append_expr_ptr(ctx->allocator, &top->expr->block.exprs);
CHECK_ALLOC_NULL(ctx, expr_ptr);
*expr_ptr = expr;
/* TODO(binji): remove after post order, only needed for if-block */
if (top->index + 1 == top->total)
pop_depth(ctx);
break;
}
case WASM_EXPR_TYPE_BR:
top->expr->br.expr = expr;
break;
case WASM_EXPR_TYPE_BR_IF:
if (top->index == 0)
top->expr->br_if.expr = expr;
else
top->expr->br_if.cond = expr;
break;
case WASM_EXPR_TYPE_BR_TABLE:
top->expr->br_table.expr = expr;
break;
case WASM_EXPR_TYPE_CALL:
case WASM_EXPR_TYPE_CALL_IMPORT: {
WasmExprPtr* expr_ptr =
wasm_append_expr_ptr(ctx->allocator, &top->expr->call.args);
CHECK_ALLOC_NULL(ctx, expr_ptr);
*expr_ptr = expr;
break;
}
case WASM_EXPR_TYPE_CALL_INDIRECT: {
if (top->index == 0) {
top->expr->call_indirect.expr = expr;
} else {
WasmExprPtr* expr_ptr = wasm_append_expr_ptr(
ctx->allocator, &top->expr->call_indirect.args);
CHECK_ALLOC_NULL(ctx, expr_ptr);
*expr_ptr = expr;
}
break;
}
case WASM_EXPR_TYPE_COMPARE:
if (top->index == 0)
top->expr->compare.left = expr;
else
top->expr->compare.right = expr;
break;
case WASM_EXPR_TYPE_CONVERT:
top->expr->convert.expr = expr;
break;
case WASM_EXPR_TYPE_GROW_MEMORY:
top->expr->grow_memory.expr = expr;
break;
case WASM_EXPR_TYPE_IF:
if (top->index == 0) {
top->expr->if_.cond = expr;
push_fake_depth(ctx);
} else {
top->expr->if_.true_ = expr;
pop_fake_depth_unless_block(ctx, expr);
}
break;
case WASM_EXPR_TYPE_IF_ELSE:
if (top->index == 0) {
top->expr->if_else.cond = expr;
push_fake_depth(ctx);
} else if (top->index == 1) {
top->expr->if_else.true_ = expr;
pop_fake_depth_unless_block(ctx, expr);
push_fake_depth(ctx);
} else {
top->expr->if_else.false_ = expr;
pop_fake_depth_unless_block(ctx, expr);
}
break;
case WASM_EXPR_TYPE_LOAD:
top->expr->load.addr = expr;
break;
case WASM_EXPR_TYPE_LOOP: {
WasmExprPtr* expr_ptr =
wasm_append_expr_ptr(ctx->allocator, &top->expr->loop.exprs);
CHECK_ALLOC_NULL(ctx, expr_ptr);
*expr_ptr = expr;
/* TODO(binji): remove after post order, only needed for if-block */
if (top->index + 1 == top->total) {
pop_depth(ctx);
pop_depth(ctx);
}
break;
}
case WASM_EXPR_TYPE_RETURN:
top->expr->return_.expr = expr;
break;
case WASM_EXPR_TYPE_SELECT:
if (top->index == 0)
top->expr->select.true_ = expr;
else if (top->index == 1)
top->expr->select.false_ = expr;
else
top->expr->select.cond = expr;
break;
case WASM_EXPR_TYPE_SET_LOCAL:
top->expr->set_local.expr = expr;
break;
case WASM_EXPR_TYPE_STORE:
if (top->index == 0)
top->expr->store.addr = expr;
else
top->expr->store.value = expr;
break;
case WASM_EXPR_TYPE_UNARY:
top->expr->unary.expr = expr;
break;
default:
case WASM_EXPR_TYPE_CONST:
case WASM_EXPR_TYPE_GET_LOCAL:
case WASM_EXPR_TYPE_MEMORY_SIZE:
case WASM_EXPR_TYPE_NOP:
case WASM_EXPR_TYPE_UNREACHABLE:
assert(0);
return WASM_ERROR;
}
if (++top->index == top->total) {
/* "recurse" and reduce the current expr */
expr = top->expr;
ctx->expr_stack.size--;
done = 0;
}
}
}
return WASM_OK;
}
static WasmResult shift(WasmReadAstContext* ctx,
WasmExpr* expr,
uint32_t count) {
ctx->just_pushed_fake_depth = 0;
if (count > 0) {
#if LOG
fprintf(stderr, "shift: %d %u\n", expr->type, count);
#endif
WasmExprNode* node =
wasm_append_expr_node(ctx->allocator, &ctx->expr_stack);
CHECK_ALLOC_NULL(ctx, node);
node->expr = expr;
node->index = 0;
node->total = count;
return WASM_OK;
} else {
return reduce(ctx, expr);
}
}
static WasmResult on_binary_expr(WasmOpcode opcode, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_binary_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->binary.opcode = opcode;
return shift(ctx, expr, 2);
}
static WasmResult on_block_expr(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
if (ctx->just_pushed_fake_depth)
pop_fake_depth(ctx);
CHECK_RESULT(push_depth(ctx));
WasmExpr* expr = wasm_new_block_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
return shift(ctx, expr, count);
}
static WasmResult on_br_expr(uint32_t depth, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_br_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->br.var.type = WASM_VAR_TYPE_INDEX;
CHECK_DEPTH(ctx, depth);
expr->br.var.index = translate_depth(ctx, depth);
return shift(ctx, expr, 1);
}
static WasmResult on_br_if_expr(uint32_t depth, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_br_if_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->br_if.var.type = WASM_VAR_TYPE_INDEX;
CHECK_DEPTH(ctx, depth);
expr->br_if.var.index = translate_depth(ctx, depth);
return shift(ctx, expr, 2);
}
static WasmResult on_br_table_expr(uint32_t num_targets,
uint32_t* target_depths,
uint32_t default_target_depth,
void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_br_table_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
CHECK_ALLOC(ctx, wasm_reserve_vars(ctx->allocator, &expr->br_table.targets,
num_targets));
expr->br_table.targets.size = num_targets;
int i;
for (i = 0; i < num_targets; ++i) {
WasmVar* var = &expr->br_table.targets.data[i];
var->type = WASM_VAR_TYPE_INDEX;
CHECK_DEPTH(ctx, target_depths[i]);
var->index = translate_depth(ctx, target_depths[i]);
}
expr->br_table.default_target.type = WASM_VAR_TYPE_INDEX;
CHECK_DEPTH(ctx, default_target_depth);
expr->br_table.default_target.index =
translate_depth(ctx, default_target_depth);
return shift(ctx, expr, 1);
}
static WasmResult on_call_expr(uint32_t func_index, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
assert(func_index < ctx->module->funcs.size);
WasmFunc* func = ctx->module->funcs.data[func_index];
uint32_t sig_index = func->type_var.index;
assert(sig_index < ctx->module->func_types.size);
WasmFuncType* func_type = ctx->module->func_types.data[sig_index];
WasmExpr* expr = wasm_new_call_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->call.var.type = WASM_VAR_TYPE_INDEX;
expr->call.var.index = func_index;
uint32_t num_params = func_type->sig.param_types.size;
return shift(ctx, expr, num_params);
}
static WasmResult on_call_import_expr(uint32_t import_index, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
assert(import_index < ctx->module->imports.size);
WasmImport* import = ctx->module->imports.data[import_index];
uint32_t sig_index = import->type_var.index;
assert(sig_index < ctx->module->func_types.size);
WasmFuncType* func_type = ctx->module->func_types.data[sig_index];
WasmExpr* expr = wasm_new_call_import_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->call.var.type = WASM_VAR_TYPE_INDEX;
expr->call.var.index = import_index;
uint32_t num_params = func_type->sig.param_types.size;
return shift(ctx, expr, num_params);
}
static WasmResult on_call_indirect_expr(uint32_t sig_index, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
assert(sig_index < ctx->module->func_types.size);
WasmFuncType* func_type = ctx->module->func_types.data[sig_index];
WasmExpr* expr = wasm_new_call_indirect_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->call_indirect.var.type = WASM_VAR_TYPE_INDEX;
expr->call_indirect.var.index = sig_index;
uint32_t num_params = func_type->sig.param_types.size;
return shift(ctx, expr, num_params + 1);
}
static WasmResult on_compare_expr(WasmOpcode opcode, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_compare_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->compare.opcode = opcode;
return shift(ctx, expr, 2);
}
static WasmResult on_i32_const_expr(uint32_t value, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_const_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->const_.type = WASM_TYPE_I32;
expr->const_.u32 = value;
return reduce(ctx, expr);
}
static WasmResult on_i64_const_expr(uint64_t value, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_const_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->const_.type = WASM_TYPE_I64;
expr->const_.u64 = value;
return reduce(ctx, expr);
}
static WasmResult on_f32_const_expr(uint32_t value_bits, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_const_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->const_.type = WASM_TYPE_F32;
expr->const_.f32_bits = value_bits;
return reduce(ctx, expr);
}
static WasmResult on_f64_const_expr(uint64_t value_bits, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_const_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->const_.type = WASM_TYPE_F64;
expr->const_.f64_bits = value_bits;
return reduce(ctx, expr);
}
static WasmResult on_convert_expr(WasmOpcode opcode, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_convert_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->convert.opcode = opcode;
return shift(ctx, expr, 1);
}
static WasmResult on_get_local_expr(uint32_t local_index, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_get_local_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->get_local.var.type = WASM_VAR_TYPE_INDEX;
expr->get_local.var.index = local_index;
CHECK_LOCAL(ctx, local_index);
return reduce(ctx, expr);
}
static WasmResult on_grow_memory_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_grow_memory_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
return shift(ctx, expr, 1);
}
static WasmResult on_if_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_if_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
return shift(ctx, expr, 2);
}
static WasmResult on_if_else_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_if_else_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
return shift(ctx, expr, 3);
}
static WasmResult on_load_expr(WasmOpcode opcode,
uint32_t alignment_log2,
uint32_t offset,
void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_load_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->load.opcode = opcode;
expr->load.align = 1 << alignment_log2;
expr->load.offset = offset;
return shift(ctx, expr, 1);
}
static WasmResult on_loop_expr(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
CHECK_RESULT(push_depth(ctx));
CHECK_RESULT(push_depth(ctx));
WasmExpr* expr = wasm_new_loop_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
return shift(ctx, expr, count);
}
static WasmResult on_memory_size_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr =
wasm_new_empty_expr(ctx->allocator, WASM_EXPR_TYPE_MEMORY_SIZE);
CHECK_ALLOC_NULL(ctx, expr);
return reduce(ctx, expr);
}
static WasmResult on_nop_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_empty_expr(ctx->allocator, WASM_EXPR_TYPE_NOP);
CHECK_ALLOC_NULL(ctx, expr);
return reduce(ctx, expr);
}
static WasmResult on_return_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
uint32_t sig_index = ctx->current_func->type_var.index;
assert(sig_index < ctx->module->func_types.size);
WasmFuncType* func_type = ctx->module->func_types.data[sig_index];
WasmExpr* expr = wasm_new_return_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
return shift(ctx, expr, func_type->sig.result_type == WASM_TYPE_VOID ? 0 : 1);
}
static WasmResult on_select_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_select_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
return shift(ctx, expr, 3);
}
static WasmResult on_set_local_expr(uint32_t local_index, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_set_local_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->set_local.var.type = WASM_VAR_TYPE_INDEX;
expr->set_local.var.index = local_index;
CHECK_LOCAL(ctx, local_index);
return shift(ctx, expr, 1);
}
static WasmResult on_store_expr(WasmOpcode opcode,
uint32_t alignment_log2,
uint32_t offset,
void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_store_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->store.opcode = opcode;
expr->store.align = 1 << alignment_log2;
expr->store.offset = offset;
return shift(ctx, expr, 2);
}
static WasmResult on_unary_expr(WasmOpcode opcode, void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr = wasm_new_unary_expr(ctx->allocator);
CHECK_ALLOC_NULL(ctx, expr);
expr->unary.opcode = opcode;
return shift(ctx, expr, 1);
}
static WasmResult on_unreachable_expr(void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(ctx->current_func);
WasmExpr* expr =
wasm_new_empty_expr(ctx->allocator, WASM_EXPR_TYPE_UNREACHABLE);
CHECK_ALLOC_NULL(ctx, expr);
return reduce(ctx, expr);
}
static WasmResult on_function_table_count(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_TABLE;
assert(ctx->module->table == NULL);
ctx->module->table = &field->table;
CHECK_ALLOC(ctx, wasm_reserve_vars(ctx->allocator, &field->table, count));
return WASM_OK;
}
static WasmResult on_function_table_entry(uint32_t index,
uint32_t func_index,
void* user_data) {
WasmReadAstContext* ctx = user_data;
assert(index < ctx->module->table->capacity);
WasmVar* func_var = wasm_append_var(ctx->allocator, ctx->module->table);
func_var->type = WASM_VAR_TYPE_INDEX;
assert(func_index < ctx->module->funcs.size);
func_var->index = func_index;
return WASM_OK;
}
static WasmResult on_start_function(uint32_t func_index, void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_START;
field->start.type = WASM_VAR_TYPE_INDEX;
assert(func_index < ctx->module->funcs.size);
field->start.index = func_index;
ctx->module->start = &field->start;
return WASM_OK;
}
static WasmResult on_export_count(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
CHECK_ALLOC(ctx, wasm_reserve_export_ptrs(ctx->allocator,
&ctx->module->exports, count));
return WASM_OK;
}
static WasmResult on_export(uint32_t index,
uint32_t func_index,
WasmStringSlice name,
void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModuleField* field =
wasm_append_module_field(ctx->allocator, ctx->module);
CHECK_ALLOC_NULL(ctx, field);
field->type = WASM_MODULE_FIELD_TYPE_EXPORT;
WasmExport* export = &field->export_;
WASM_ZERO_MEMORY(*export);
CHECK_ALLOC_NULL_STR(ctx,
export->name = dup_string_slice(ctx->allocator, name));
export->var.type = WASM_VAR_TYPE_INDEX;
assert(func_index < ctx->module->funcs.size);
export->var.index = func_index;
assert(index < ctx->module->exports.capacity);
WasmExportPtr* export_ptr =
wasm_append_export_ptr(ctx->allocator, &ctx->module->exports);
*export_ptr = export;
return WASM_OK;
}
static WasmResult on_function_names_count(uint32_t count, void* user_data) {
WasmReadAstContext* ctx = user_data;
if (count > ctx->module->funcs.size) {
print_error(ctx, "expected function name count (%d) <= function count (%d)",
count, (int)ctx->module->funcs.size);
return WASM_ERROR;
}
return WASM_OK;
}
static WasmResult on_function_name(uint32_t index,
WasmStringSlice name,
void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmStringSlice dup_name;
CHECK_ALLOC_NULL_STR(ctx, dup_name = dup_string_slice(ctx->allocator, name));
WasmBinding* binding = wasm_insert_binding(
ctx->allocator, &ctx->module->func_bindings, &dup_name);
CHECK_ALLOC_NULL(ctx, binding);
binding->index = index;
WasmFunc* func = ctx->module->funcs.data[index];
func->name = dup_name;
/* TODO(binji): We could update the call expressions to use the function name
* instead of the index */
return WASM_OK;
}
static WasmResult on_local_names_count(uint32_t index,
uint32_t count,
void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModule* module = ctx->module;
assert(index < module->funcs.size);
WasmFunc* func = module->funcs.data[index];
uint32_t num_params_and_locals = get_num_func_params_and_locals(module, func);
if (count > num_params_and_locals) {
print_error(ctx, "expected local name count (%d) <= local count (%d)",
count, num_params_and_locals);
return WASM_ERROR;
}
/* copy the signature from the function type */
int i;
WasmFuncSignature* sig = get_func_sig(module, func);
for (i = 0; i < sig->param_types.size; ++i) {
CHECK_ALLOC(ctx, wasm_append_type_value(ctx->allocator, &func->params.types,
&sig->param_types.data[i]));
}
func->result_type = sig->result_type;
func->flags |= WASM_FUNC_FLAG_HAS_SIGNATURE;
return WASM_OK;
}
static WasmResult on_local_name(uint32_t func_index,
uint32_t local_index,
WasmStringSlice name,
void* user_data) {
WasmReadAstContext* ctx = user_data;
WasmModule* module = ctx->module;
WasmFunc* func = module->funcs.data[func_index];
uint32_t num_params = get_num_func_params(module, func);
WasmStringSlice dup_name;
CHECK_ALLOC_NULL_STR(ctx, dup_name = dup_string_slice(ctx->allocator, name));
WasmBinding* binding;
if (local_index < num_params) {
/* param name */
binding =
wasm_insert_binding(ctx->allocator, &func->params.bindings, &dup_name);
} else {
/* local name */
binding =
wasm_insert_binding(ctx->allocator, &func->locals.bindings, &dup_name);
}
CHECK_ALLOC_NULL(ctx, binding);
binding->index = local_index;
/* TODO(binji): We could update the get_local/set_local expressions to use
* the name instead of the index */
return WASM_OK;
}
static WasmBinaryReader s_binary_reader = {
.user_data = NULL,
.on_error = &on_error,
.begin_memory_section = &begin_memory_section,
.on_memory_initial_size_pages = &on_memory_initial_size_pages,
.on_memory_max_size_pages = &on_memory_max_size_pages,
.on_memory_exported = &on_memory_exported,
.on_data_segment_count = &on_data_segment_count,
.on_data_segment = &on_data_segment,
.on_signature_count = &on_signature_count,
.on_signature = &on_signature,
.on_import_count = &on_import_count,
.on_import = &on_import,
.on_function_signatures_count = &on_function_signatures_count,
.on_function_signature = &on_function_signature,
.on_function_bodies_count = &on_function_bodies_count,
.begin_function_body = &begin_function_body,
.on_local_decl_count = &on_local_decl_count,
.on_local_decl = &on_local_decl,
.on_binary_expr = &on_binary_expr,
.on_block_expr = &on_block_expr,
.on_br_expr = &on_br_expr,
.on_br_if_expr = &on_br_if_expr,
.on_br_table_expr = &on_br_table_expr,
.on_call_expr = &on_call_expr,
.on_call_import_expr = &on_call_import_expr,
.on_call_indirect_expr = &on_call_indirect_expr,
.on_compare_expr = &on_compare_expr,
.on_i32_const_expr = &on_i32_const_expr,
.on_i64_const_expr = &on_i64_const_expr,
.on_f32_const_expr = &on_f32_const_expr,
.on_f64_const_expr = &on_f64_const_expr,
.on_convert_expr = &on_convert_expr,
.on_get_local_expr = &on_get_local_expr,
.on_grow_memory_expr = &on_grow_memory_expr,
.on_if_expr = &on_if_expr,
.on_if_else_expr = &on_if_else_expr,
.on_load_expr = &on_load_expr,
.on_loop_expr = &on_loop_expr,
.on_memory_size_expr = &on_memory_size_expr,
.on_nop_expr = &on_nop_expr,
.on_return_expr = &on_return_expr,
.on_select_expr = &on_select_expr,
.on_set_local_expr = &on_set_local_expr,
.on_store_expr = &on_store_expr,
.on_unary_expr = &on_unary_expr,
.on_unreachable_expr = &on_unreachable_expr,
.end_function_body = &end_function_body,
.on_function_table_count = &on_function_table_count,
.on_function_table_entry = &on_function_table_entry,
.on_start_function = &on_start_function,
.on_export_count = &on_export_count,
.on_export = &on_export,
.on_function_names_count = &on_function_names_count,
.on_function_name = &on_function_name,
.on_local_names_count = &on_local_names_count,
.on_local_name = &on_local_name,
};
WasmResult wasm_read_binary_ast(struct WasmAllocator* allocator,
const void* data,
size_t size,
WasmReadBinaryOptions* options,
struct WasmModule* out_module) {
WasmReadAstContext ctx;
WASM_ZERO_MEMORY(ctx);
ctx.allocator = allocator;
ctx.module = out_module;
WasmBinaryReader reader;
WASM_ZERO_MEMORY(reader);
reader = s_binary_reader;
reader.user_data = &ctx;
WasmResult result = wasm_read_binary(allocator, data, size, &reader, options);
wasm_destroy_expr_node_vector(allocator, &ctx.expr_stack);
wasm_destroy_uint32_vector(allocator, &ctx.depth_stack);
return result;
}
|