summaryrefslogtreecommitdiff
path: root/src/wasm-parse.h
blob: d645c14cd1b401e8897a0548bb80e3183ac363b7 (plain)
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
#ifndef WASM_PARSE_H
#define WASM_PARSE_H

#include "wasm.h"

typedef union WasmNumber {
  uint32_t i32;
  uint64_t i64;
  float f32;
  double f64;
} WasmNumber;

typedef uintptr_t WasmParserCookie;

typedef struct WasmParserCallbackInfo {
  WasmSourceLocation loc;
  WasmModule* module; /* may be NULL */
  WasmFunction* function; /* may be NULL */
  /* Saved and restored for matching before/after callback pairs */
  WasmParserCookie cookie;
  void* user_data;
} WasmParserCallbackInfo;

typedef struct WasmParserCallbacks {
  void* user_data;
  void (*error)(WasmParserCallbackInfo* info, const char* msg);
  void (*before_module)(WasmParserCallbackInfo* info);
  void (*after_module)(WasmParserCallbackInfo* info);
  /* This will not be called if the module fails to parse. */
  void (*before_module_destroy)(WasmParserCallbackInfo* info);
  void (*before_function)(WasmParserCallbackInfo* info);
  void (*after_function)(WasmParserCallbackInfo* info, int num_exprs);
  void (*before_export)(WasmParserCallbackInfo* info);
  void (*after_export)(WasmParserCallbackInfo* info, const char* exported_name);

  void (*before_binary)(WasmParserCallbackInfo* info, enum WasmOpcode opcode);
  void (*before_block)(WasmParserCallbackInfo* info, int with_label);
  void (*after_block)(WasmParserCallbackInfo* info,
                      WasmType type,
                      int num_exprs);
  void (*before_break)(WasmParserCallbackInfo* info,
                       int with_expr,
                       int label_depth);
  void (*after_break)(WasmParserCallbackInfo* info);
  void (*before_br_if)(WasmParserCallbackInfo* info);
  void (*after_br_if)(WasmParserCallbackInfo* info, int label_depth);
  void (*before_call)(WasmParserCallbackInfo* info, int function_index);
  void (*before_call_import)(WasmParserCallbackInfo* info, int import_index);
  void (*before_call_indirect)(WasmParserCallbackInfo* info,
                               int signature_index);
  void (*before_compare)(WasmParserCallbackInfo* info, enum WasmOpcode opcode);
  void (*after_const)(WasmParserCallbackInfo* info,
                      enum WasmOpcode opcode,
                      WasmType type,
                      WasmNumber value);
  void (*before_convert)(WasmParserCallbackInfo* info, enum WasmOpcode opcode);
  void (*before_label)(WasmParserCallbackInfo* info);
  void (*after_label)(WasmParserCallbackInfo* info, WasmType type);
  void (*after_get_local)(WasmParserCallbackInfo* info, int index);
  void (*before_loop)(WasmParserCallbackInfo* info,
                      int with_inner_label,
                      int with_outer_label);
  void (*after_loop)(WasmParserCallbackInfo* info, int num_exprs);
  void (*before_if)(WasmParserCallbackInfo* info);
  void (*after_if)(WasmParserCallbackInfo* info, WasmType type, int with_else);
  void (*before_load)(WasmParserCallbackInfo* info,
                      enum WasmOpcode opcode,
                      WasmMemType mem_type,
                      uint32_t alignment,
                      uint32_t offset,
                      int is_signed_load);
  void (*after_load_global)(WasmParserCallbackInfo* info, int index);
  void (*after_memory_size)(WasmParserCallbackInfo* info);
  void (*after_nop)(WasmParserCallbackInfo* info);
  void (*before_grow_memory)(WasmParserCallbackInfo* info);
  void (*before_return)(WasmParserCallbackInfo* info);
  void (*after_return)(WasmParserCallbackInfo* info, WasmType type);
  void (*before_select)(WasmParserCallbackInfo* info, enum WasmOpcode opcode);
  void (*before_set_local)(WasmParserCallbackInfo* info, int index);
  void (*before_store)(WasmParserCallbackInfo* info,
                       enum WasmOpcode opcode,
                       WasmMemType mem_type,
                       uint32_t alignment,
                       uint32_t offset);

  void (*before_store_global)(WasmParserCallbackInfo* info, int index);
  void (*before_tableswitch)(WasmParserCallbackInfo* info, int with_label);
  void (*before_tableswitch_case)(WasmParserCallbackInfo* info, int index);
  void (*before_tableswitch_br)(WasmParserCallbackInfo* info, int label_depth);
  void (*after_tableswitch_table)(WasmParserCallbackInfo* info, int num_cases);
  void (*before_tableswitch_key)(WasmParserCallbackInfo* info);
  void (*before_tableswitch_targets)(WasmParserCallbackInfo* info);
  void (*before_tableswitch_target)(WasmParserCallbackInfo* info);
  void (*after_tableswitch_target)(WasmParserCallbackInfo* info, int num_exprs);
  void (*after_tableswitch)(WasmParserCallbackInfo* info, int num_targets);
  void (*before_unary)(WasmParserCallbackInfo* info, enum WasmOpcode opcode);
  void (*before_unreachable)(WasmParserCallbackInfo* info);

  /* used in spec repo tests */
  void (*before_assert_return)(WasmParserCallbackInfo* info);
  void (*after_assert_return)(WasmParserCallbackInfo* info, WasmType type);
  void (*before_assert_return_nan)(WasmParserCallbackInfo* info);
  void (*after_assert_return_nan)(WasmParserCallbackInfo* info, WasmType type);
  void (*before_assert_trap)(WasmParserCallbackInfo* info);
  void (*after_assert_trap)(WasmParserCallbackInfo* info);
  void (*before_invoke)(WasmParserCallbackInfo* info,
                        const char* invoke_name,
                        int invoke_function_index);
  void (*after_invoke)(WasmParserCallbackInfo* info);

  /* called with the error from the module parsed inside of assert_invalid */
  void (*assert_invalid_error)(WasmParserCallbackInfo* info, const char* msg);
} WasmParserCallbacks;

typedef struct WasmParserOptions {
} WasmParserOptions;

EXTERN_C int wasm_parse_module(WasmSource* source,
                               WasmParserCallbacks* parser,
                               WasmParserOptions* options);
EXTERN_C int wasm_parse_file(WasmSource* source,
                             WasmParserCallbacks* parser,
                             WasmParserOptions* options);
EXTERN_C void wasm_copy_segment_data(WasmSegmentData data,
                                     char* dest,
                                     size_t size);

#endif /* WASM_PARSE_H */