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
|
/*
* 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.
*/
#ifndef WASM_AST_H_
#define WASM_AST_H_
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include "binding-hash.h"
#include "common.h"
#include "type-vector.h"
#include "vector.h"
typedef enum WasmVarType {
WASM_VAR_TYPE_INDEX,
WASM_VAR_TYPE_NAME,
} WasmVarType;
typedef struct WasmVar {
WasmLocation loc;
WasmVarType type;
union {
int64_t index;
WasmStringSlice name;
};
} WasmVar;
WASM_DEFINE_VECTOR(var, WasmVar);
typedef WasmStringSlice WasmLabel;
WASM_DEFINE_VECTOR(string_slice, WasmStringSlice);
typedef struct WasmConst {
WasmLocation loc;
WasmType type;
union {
uint32_t u32;
uint64_t u64;
uint32_t f32_bits;
uint64_t f64_bits;
};
} WasmConst;
WASM_DEFINE_VECTOR(const, WasmConst);
typedef enum WasmExprType {
WASM_EXPR_TYPE_BINARY,
WASM_EXPR_TYPE_BLOCK,
WASM_EXPR_TYPE_BR,
WASM_EXPR_TYPE_BR_IF,
WASM_EXPR_TYPE_BR_TABLE,
WASM_EXPR_TYPE_CALL,
WASM_EXPR_TYPE_CALL_INDIRECT,
WASM_EXPR_TYPE_COMPARE,
WASM_EXPR_TYPE_CONST,
WASM_EXPR_TYPE_CONVERT,
WASM_EXPR_TYPE_CURRENT_MEMORY,
WASM_EXPR_TYPE_DROP,
WASM_EXPR_TYPE_GET_GLOBAL,
WASM_EXPR_TYPE_GET_LOCAL,
WASM_EXPR_TYPE_GROW_MEMORY,
WASM_EXPR_TYPE_IF,
WASM_EXPR_TYPE_LOAD,
WASM_EXPR_TYPE_LOOP,
WASM_EXPR_TYPE_NOP,
WASM_EXPR_TYPE_RETURN,
WASM_EXPR_TYPE_SELECT,
WASM_EXPR_TYPE_SET_GLOBAL,
WASM_EXPR_TYPE_SET_LOCAL,
WASM_EXPR_TYPE_STORE,
WASM_EXPR_TYPE_TEE_LOCAL,
WASM_EXPR_TYPE_UNARY,
WASM_EXPR_TYPE_UNREACHABLE,
} WasmExprType;
typedef WasmTypeVector WasmBlockSignature;
typedef struct WasmBlock {
WasmLabel label;
WasmBlockSignature sig;
struct WasmExpr* first;
} WasmBlock;
typedef struct WasmExpr WasmExpr;
struct WasmExpr {
WasmLocation loc;
WasmExprType type;
WasmExpr* next;
union {
struct { WasmOpcode opcode; } binary, compare, convert, unary;
WasmBlock block, loop;
struct { WasmVar var; } br, br_if;
struct { WasmVarVector targets; WasmVar default_target; } br_table;
struct { WasmVar var; } call, call_indirect;
WasmConst const_;
struct { WasmVar var; } get_global, set_global;
struct { WasmVar var; } get_local, set_local, tee_local;
struct { WasmBlock true_; struct WasmExpr* false_; } if_;
struct { WasmOpcode opcode; uint32_t align; uint64_t offset; } load, store;
};
};
typedef struct WasmFuncSignature {
WasmTypeVector param_types;
WasmTypeVector result_types;
} WasmFuncSignature;
typedef struct WasmFuncType {
WasmStringSlice name;
WasmFuncSignature sig;
} WasmFuncType;
typedef WasmFuncType* WasmFuncTypePtr;
WASM_DEFINE_VECTOR(func_type_ptr, WasmFuncTypePtr);
enum {
WASM_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE = 1,
/* set if the signature is owned by module */
WASM_FUNC_DECLARATION_FLAG_SHARED_SIGNATURE = 2,
};
typedef uint32_t WasmFuncDeclarationFlags;
typedef struct WasmFuncDeclaration {
WasmFuncDeclarationFlags flags;
WasmVar type_var;
WasmFuncSignature sig;
} WasmFuncDeclaration;
typedef struct WasmFunc {
WasmStringSlice name;
WasmFuncDeclaration decl;
WasmTypeVector local_types;
WasmBindingHash param_bindings;
WasmBindingHash local_bindings;
WasmExpr* first_expr;
} WasmFunc;
typedef WasmFunc* WasmFuncPtr;
WASM_DEFINE_VECTOR(func_ptr, WasmFuncPtr);
typedef struct WasmGlobal {
WasmStringSlice name;
WasmType type;
WasmBool mutable_;
WasmExpr* init_expr;
} WasmGlobal;
typedef WasmGlobal* WasmGlobalPtr;
WASM_DEFINE_VECTOR(global_ptr, WasmGlobalPtr);
typedef struct WasmTable {
WasmStringSlice name;
WasmLimits elem_limits;
} WasmTable;
typedef WasmTable* WasmTablePtr;
WASM_DEFINE_VECTOR(table_ptr, WasmTablePtr);
typedef struct WasmElemSegment {
WasmVar table_var;
WasmExpr* offset;
WasmVarVector vars;
} WasmElemSegment;
typedef WasmElemSegment* WasmElemSegmentPtr;
WASM_DEFINE_VECTOR(elem_segment_ptr, WasmElemSegmentPtr);
typedef struct WasmMemory {
WasmStringSlice name;
WasmLimits page_limits;
} WasmMemory;
typedef WasmMemory* WasmMemoryPtr;
WASM_DEFINE_VECTOR(memory_ptr, WasmMemoryPtr);
typedef struct WasmDataSegment {
WasmVar memory_var;
WasmExpr* offset;
void* data;
size_t size;
} WasmDataSegment;
typedef WasmDataSegment* WasmDataSegmentPtr;
WASM_DEFINE_VECTOR(data_segment_ptr, WasmDataSegmentPtr);
typedef struct WasmImport {
WasmStringSlice module_name;
WasmStringSlice field_name;
WasmExternalKind kind;
union {
/* an imported func is has the type WasmFunc so it can be more easily
* included in the Module's vector of funcs; but only the
* WasmFuncDeclaration will have any useful information */
WasmFunc func;
WasmTable table;
WasmMemory memory;
WasmGlobal global;
};
} WasmImport;
typedef WasmImport* WasmImportPtr;
WASM_DEFINE_VECTOR(import_ptr, WasmImportPtr);
typedef struct WasmExport {
WasmStringSlice name;
WasmExternalKind kind;
WasmVar var;
} WasmExport;
typedef WasmExport* WasmExportPtr;
WASM_DEFINE_VECTOR(export_ptr, WasmExportPtr);
typedef enum WasmModuleFieldType {
WASM_MODULE_FIELD_TYPE_FUNC,
WASM_MODULE_FIELD_TYPE_GLOBAL,
WASM_MODULE_FIELD_TYPE_IMPORT,
WASM_MODULE_FIELD_TYPE_EXPORT,
WASM_MODULE_FIELD_TYPE_FUNC_TYPE,
WASM_MODULE_FIELD_TYPE_TABLE,
WASM_MODULE_FIELD_TYPE_ELEM_SEGMENT,
WASM_MODULE_FIELD_TYPE_MEMORY,
WASM_MODULE_FIELD_TYPE_DATA_SEGMENT,
WASM_MODULE_FIELD_TYPE_START,
} WasmModuleFieldType;
typedef struct WasmModuleField {
WasmLocation loc;
WasmModuleFieldType type;
struct WasmModuleField* next;
union {
WasmFunc func;
WasmGlobal global;
WasmImport import;
WasmExport export_;
WasmFuncType func_type;
WasmTable table;
WasmElemSegment elem_segment;
WasmMemory memory;
WasmDataSegment data_segment;
WasmVar start;
};
} WasmModuleField;
typedef struct WasmModule {
WasmLocation loc;
WasmStringSlice name;
WasmModuleField* first_field;
WasmModuleField* last_field;
uint32_t num_func_imports;
uint32_t num_table_imports;
uint32_t num_memory_imports;
uint32_t num_global_imports;
/* cached for convenience; the pointers are shared with values that are
* stored in either WasmModuleField or WasmImport. */
WasmFuncPtrVector funcs;
WasmGlobalPtrVector globals;
WasmImportPtrVector imports;
WasmExportPtrVector exports;
WasmFuncTypePtrVector func_types;
WasmTablePtrVector tables;
WasmElemSegmentPtrVector elem_segments;
WasmMemoryPtrVector memories;
WasmDataSegmentPtrVector data_segments;
WasmVar* start;
WasmBindingHash func_bindings;
WasmBindingHash global_bindings;
WasmBindingHash export_bindings;
WasmBindingHash func_type_bindings;
WasmBindingHash table_bindings;
WasmBindingHash memory_bindings;
} WasmModule;
typedef enum WasmRawModuleType {
WASM_RAW_MODULE_TYPE_TEXT,
WASM_RAW_MODULE_TYPE_BINARY,
} WasmRawModuleType;
/* "raw" means that the binary module has not yet been decoded. This is only
* necessary when embedded in assert_invalid. In that case we want to defer
* decoding errors until wasm_check_assert_invalid is called. This isn't needed
* when parsing text, as assert_invalid always assumes that text parsing
* succeeds. */
typedef struct WasmRawModule {
WasmRawModuleType type;
union {
WasmModule* text;
struct {
WasmLocation loc;
WasmStringSlice name;
void* data;
size_t size;
} binary;
};
} WasmRawModule;
typedef enum WasmActionType {
WASM_ACTION_TYPE_INVOKE,
WASM_ACTION_TYPE_GET,
} WasmActionType;
typedef struct WasmActionInvoke {
WasmStringSlice name;
WasmConstVector args;
} WasmActionInvoke;
typedef struct WasmActionGet {
WasmStringSlice name;
} WasmActionGet;
typedef struct WasmAction {
WasmLocation loc;
WasmActionType type;
WasmVar module_var;
union {
WasmActionInvoke invoke;
WasmActionGet get;
};
} WasmAction;
typedef enum WasmCommandType {
WASM_COMMAND_TYPE_MODULE,
WASM_COMMAND_TYPE_ACTION,
WASM_COMMAND_TYPE_REGISTER,
WASM_COMMAND_TYPE_ASSERT_MALFORMED,
WASM_COMMAND_TYPE_ASSERT_INVALID,
WASM_COMMAND_TYPE_ASSERT_UNLINKABLE,
WASM_COMMAND_TYPE_ASSERT_UNINSTANTIABLE,
WASM_COMMAND_TYPE_ASSERT_RETURN,
WASM_COMMAND_TYPE_ASSERT_RETURN_NAN,
WASM_COMMAND_TYPE_ASSERT_TRAP,
WASM_NUM_COMMAND_TYPES,
} WasmCommandType;
typedef struct WasmCommand {
WasmCommandType type;
union {
WasmModule module;
WasmAction action;
struct { WasmStringSlice module_name; WasmVar var; } register_;
struct { WasmAction action; WasmConstVector expected; } assert_return;
struct { WasmAction action; } assert_return_nan;
struct { WasmAction action; WasmStringSlice text; } assert_trap;
struct {
WasmRawModule module;
WasmStringSlice text;
} assert_malformed, assert_invalid, assert_unlinkable,
assert_uninstantiable;
};
} WasmCommand;
WASM_DEFINE_VECTOR(command, WasmCommand);
typedef struct WasmScript {
struct WasmAllocator* allocator;
WasmCommandVector commands;
WasmBindingHash module_bindings;
} WasmScript;
typedef struct WasmExprVisitor {
void* user_data;
WasmResult (*on_binary_expr)(WasmExpr*, void* user_data);
WasmResult (*begin_block_expr)(WasmExpr*, void* user_data);
WasmResult (*end_block_expr)(WasmExpr*, void* user_data);
WasmResult (*on_br_expr)(WasmExpr*, void* user_data);
WasmResult (*on_br_if_expr)(WasmExpr*, void* user_data);
WasmResult (*on_br_table_expr)(WasmExpr*, void* user_data);
WasmResult (*on_call_expr)(WasmExpr*, void* user_data);
WasmResult (*on_call_indirect_expr)(WasmExpr*, void* user_data);
WasmResult (*on_compare_expr)(WasmExpr*, void* user_data);
WasmResult (*on_const_expr)(WasmExpr*, void* user_data);
WasmResult (*on_convert_expr)(WasmExpr*, void* user_data);
WasmResult (*on_current_memory_expr)(WasmExpr*, void* user_data);
WasmResult (*on_drop_expr)(WasmExpr*, void* user_data);
WasmResult (*on_get_global_expr)(WasmExpr*, void* user_data);
WasmResult (*on_get_local_expr)(WasmExpr*, void* user_data);
WasmResult (*on_grow_memory_expr)(WasmExpr*, void* user_data);
WasmResult (*begin_if_expr)(WasmExpr*, void* user_data);
WasmResult (*after_if_true_expr)(WasmExpr*, void* user_data);
WasmResult (*end_if_expr)(WasmExpr*, void* user_data);
WasmResult (*on_load_expr)(WasmExpr*, void* user_data);
WasmResult (*begin_loop_expr)(WasmExpr*, void* user_data);
WasmResult (*end_loop_expr)(WasmExpr*, void* user_data);
WasmResult (*on_nop_expr)(WasmExpr*, void* user_data);
WasmResult (*on_return_expr)(WasmExpr*, void* user_data);
WasmResult (*on_select_expr)(WasmExpr*, void* user_data);
WasmResult (*on_set_global_expr)(WasmExpr*, void* user_data);
WasmResult (*on_set_local_expr)(WasmExpr*, void* user_data);
WasmResult (*on_store_expr)(WasmExpr*, void* user_data);
WasmResult (*on_tee_local_expr)(WasmExpr*, void* user_data);
WasmResult (*on_unary_expr)(WasmExpr*, void* user_data);
WasmResult (*on_unreachable_expr)(WasmExpr*, void* user_data);
} WasmExprVisitor;
WASM_EXTERN_C_BEGIN
WasmModuleField* wasm_append_module_field(struct WasmAllocator*, WasmModule*);
/* ownership of the function signature is passed to the module */
WasmFuncType* wasm_append_implicit_func_type(struct WasmAllocator*,
WasmLocation*,
WasmModule*,
WasmFuncSignature*);
/* WasmExpr creation functions */
WasmExpr* wasm_new_binary_expr(struct WasmAllocator*);
WasmExpr* wasm_new_block_expr(struct WasmAllocator*);
WasmExpr* wasm_new_br_expr(struct WasmAllocator*);
WasmExpr* wasm_new_br_if_expr(struct WasmAllocator*);
WasmExpr* wasm_new_br_table_expr(struct WasmAllocator*);
WasmExpr* wasm_new_call_expr(struct WasmAllocator*);
WasmExpr* wasm_new_call_indirect_expr(struct WasmAllocator*);
WasmExpr* wasm_new_compare_expr(struct WasmAllocator*);
WasmExpr* wasm_new_const_expr(struct WasmAllocator*);
WasmExpr* wasm_new_convert_expr(struct WasmAllocator*);
WasmExpr* wasm_new_current_memory_expr(struct WasmAllocator*);
WasmExpr* wasm_new_drop_expr(struct WasmAllocator*);
WasmExpr* wasm_new_get_global_expr(struct WasmAllocator*);
WasmExpr* wasm_new_get_local_expr(struct WasmAllocator*);
WasmExpr* wasm_new_grow_memory_expr(struct WasmAllocator*);
WasmExpr* wasm_new_if_expr(struct WasmAllocator*);
WasmExpr* wasm_new_load_expr(struct WasmAllocator*);
WasmExpr* wasm_new_loop_expr(struct WasmAllocator*);
WasmExpr* wasm_new_nop_expr(struct WasmAllocator*);
WasmExpr* wasm_new_return_expr(struct WasmAllocator*);
WasmExpr* wasm_new_select_expr(struct WasmAllocator*);
WasmExpr* wasm_new_set_global_expr(struct WasmAllocator*);
WasmExpr* wasm_new_set_local_expr(struct WasmAllocator*);
WasmExpr* wasm_new_store_expr(struct WasmAllocator*);
WasmExpr* wasm_new_tee_local_expr(struct WasmAllocator*);
WasmExpr* wasm_new_unary_expr(struct WasmAllocator*);
WasmExpr* wasm_new_unreachable_expr(struct WasmAllocator*);
/* destruction functions. not needed unless you're creating your own AST
elements */
void wasm_destroy_script(struct WasmScript*);
void wasm_destroy_action(struct WasmAllocator*, struct WasmAction*);
void wasm_destroy_block(struct WasmAllocator*, struct WasmBlock*);
void wasm_destroy_command_vector_and_elements(struct WasmAllocator*,
WasmCommandVector*);
void wasm_destroy_command(struct WasmAllocator*, WasmCommand*);
void wasm_destroy_data_segment(struct WasmAllocator*, WasmDataSegment*);
void wasm_destroy_elem_segment(struct WasmAllocator*, WasmElemSegment*);
void wasm_destroy_export(struct WasmAllocator*, WasmExport*);
void wasm_destroy_expr(struct WasmAllocator*, WasmExpr*);
void wasm_destroy_expr_list(struct WasmAllocator*, WasmExpr*);
void wasm_destroy_func_declaration(struct WasmAllocator*, WasmFuncDeclaration*);
void wasm_destroy_func_signature(struct WasmAllocator*, WasmFuncSignature*);
void wasm_destroy_func_type(struct WasmAllocator*, WasmFuncType*);
void wasm_destroy_func(struct WasmAllocator*, WasmFunc*);
void wasm_destroy_import(struct WasmAllocator*, WasmImport*);
void wasm_destroy_memory(struct WasmAllocator*, WasmMemory*);
void wasm_destroy_module(struct WasmAllocator*, WasmModule*);
void wasm_destroy_raw_module(struct WasmAllocator*, WasmRawModule*);
void wasm_destroy_table(struct WasmAllocator*, WasmTable*);
void wasm_destroy_var_vector_and_elements(struct WasmAllocator*,
WasmVarVector*);
void wasm_destroy_var(struct WasmAllocator*, WasmVar*);
/* traversal functions */
WasmResult wasm_visit_func(WasmFunc* func, WasmExprVisitor*);
WasmResult wasm_visit_expr_list(WasmExpr* expr, WasmExprVisitor*);
/* convenience functions for looking through the AST */
int wasm_get_index_from_var(const WasmBindingHash* bindings,
const WasmVar* var);
int wasm_get_func_index_by_var(const WasmModule* module, const WasmVar* var);
int wasm_get_global_index_by_var(const WasmModule* func, const WasmVar* var);
int wasm_get_func_type_index_by_var(const WasmModule* module,
const WasmVar* var);
int wasm_get_func_type_index_by_sig(const WasmModule* module,
const WasmFuncSignature* sig);
int wasm_get_func_type_index_by_decl(const WasmModule* module,
const WasmFuncDeclaration* decl);
int wasm_get_table_index_by_var(const WasmModule* module, const WasmVar* var);
int wasm_get_memory_index_by_var(const WasmModule* module, const WasmVar* var);
int wasm_get_import_index_by_var(const WasmModule* module, const WasmVar* var);
int wasm_get_local_index_by_var(const WasmFunc* func, const WasmVar* var);
int wasm_get_module_index_by_var(const WasmScript* script, const WasmVar* var);
WasmFuncPtr wasm_get_func_by_var(const WasmModule* module, const WasmVar* var);
WasmGlobalPtr wasm_get_global_by_var(const WasmModule* func,
const WasmVar* var);
WasmFuncTypePtr wasm_get_func_type_by_var(const WasmModule* module,
const WasmVar* var);
WasmTablePtr wasm_get_table_by_var(const WasmModule* module,
const WasmVar* var);
WasmMemoryPtr wasm_get_memory_by_var(const WasmModule* module,
const WasmVar* var);
WasmImportPtr wasm_get_import_by_var(const WasmModule* module,
const WasmVar* var);
WasmExportPtr wasm_get_export_by_name(const WasmModule* module,
const WasmStringSlice* name);
WasmModule* wasm_get_first_module(const WasmScript* script);
WasmModule* wasm_get_module_by_var(const WasmScript* script,
const WasmVar* var);
void wasm_make_type_binding_reverse_mapping(
struct WasmAllocator*,
const WasmTypeVector*,
const WasmBindingHash*,
WasmStringSliceVector* out_reverse_mapping);
static WASM_INLINE WasmBool
wasm_decl_has_func_type(const WasmFuncDeclaration* decl) {
return decl->flags & WASM_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE;
}
static WASM_INLINE WasmBool
wasm_signatures_are_equal(const WasmFuncSignature* sig1,
const WasmFuncSignature* sig2) {
return wasm_type_vectors_are_equal(&sig1->param_types, &sig2->param_types) &&
wasm_type_vectors_are_equal(&sig1->result_types, &sig2->result_types);
}
static WASM_INLINE size_t wasm_get_num_params(const WasmFunc* func) {
return func->decl.sig.param_types.size;
}
static WASM_INLINE size_t wasm_get_num_results(const WasmFunc* func) {
return func->decl.sig.result_types.size;
}
static WASM_INLINE size_t wasm_get_num_locals(const WasmFunc* func) {
return func->local_types.size;
}
static WASM_INLINE size_t wasm_get_num_params_and_locals(const WasmFunc* func) {
return wasm_get_num_params(func) + wasm_get_num_locals(func);
}
static WASM_INLINE WasmType wasm_get_param_type(const WasmFunc* func,
int index) {
assert((size_t)index < func->decl.sig.param_types.size);
return func->decl.sig.param_types.data[index];
}
static WASM_INLINE WasmType wasm_get_local_type(const WasmFunc* func,
int index) {
assert((size_t)index < wasm_get_num_locals(func));
return func->local_types.data[index];
}
static WASM_INLINE WasmType wasm_get_result_type(const WasmFunc* func,
int index) {
assert((size_t)index < func->decl.sig.result_types.size);
return func->decl.sig.result_types.data[index];
}
static WASM_INLINE WasmType
wasm_get_func_type_param_type(const WasmFuncType* func_type, int index) {
return func_type->sig.param_types.data[index];
}
static WASM_INLINE size_t
wasm_get_func_type_num_params(const WasmFuncType* func_type) {
return func_type->sig.param_types.size;
}
static WASM_INLINE WasmType
wasm_get_func_type_result_type(const WasmFuncType* func_type, int index) {
return func_type->sig.result_types.data[index];
}
static WASM_INLINE size_t
wasm_get_func_type_num_results(const WasmFuncType* func_type) {
return func_type->sig.result_types.size;
}
static WASM_INLINE const WasmLocation* wasm_get_raw_module_location(
const WasmRawModule* raw) {
switch (raw->type) {
case WASM_RAW_MODULE_TYPE_BINARY: return &raw->binary.loc;
case WASM_RAW_MODULE_TYPE_TEXT: return &raw->text->loc;
default:
assert(0);
return NULL;
}
}
WASM_EXTERN_C_END
#endif /* WASM_AST_H_ */
|