-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_fns.c
258 lines (246 loc) · 4.51 KB
/
populate_fns.c
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
#include "types.h"
#include "sume_tokens.h"
#include "from_ir.h"
#include "data_s.h"
#include "populate_fns.h"
int add_fn(
char* prog,
uint prog_len,
uint* place,
fn_info* fn_list,
uint* fn_num,
char rt)
{
char type_buf[64];
uint type_len;
fn_info* cur_fn = &fn_list[*fn_num];
cur_fn->rt = rt;
if (get_type_list(
prog,
prog_len,
place,
type_buf,
&type_len) < 0)
return -1;
if (add_fn_params(cur_fn, type_buf, type_len) < 0)
return -1;
if (get_type_list(
prog,
prog_len,
place,
type_buf,
&type_len) < 0)
return -1;
if (add_fn_locals(cur_fn, type_buf, type_len) < 0)
return -1;
*fn_num += 1;
return 0;
}
int
populate_fns(
char* prog,
uint prog_len,
fn_info* fn_list,
uint* fn_num)
{
uint place = 0;
*fn_num = 0;
while (place < prog_len) {
instr_token token = prog[place];
place += 1;
switch (token) {
case i_clz:
case i_ctz:
case i_popcnt:
case i_add:
case i_and:
case i_div_s:
case i_div_u:
case i_mul:
case i_or:
case i_rem_s:
case i_rem_u:
case i_rotl:
case i_rotr:
case i_shl:
case i_shr_s:
case i_shr_u:
case i_sub:
case i_xor:
case i_eqz:
case i_eq:
case i_ge_s:
case i_ge_u:
case i_gt_s:
case i_gt_u:
case i_le_s:
case i_le_u:
case i_lt_s:
case i_lt_u:
case i_ne:
case i_ld32:
case i_ld16_s:
case i_ld16_u:
case i_ld8_s:
case i_ld8_u:
case i_memsz:
case str32:
case str16:
case str8:
case trap:
case block:
case loop:
case ret:
case end:
break;
case i_const:
case get_l:
case set_l:
case br:
case br_if:
case call:
if (get_int(prog, prog_len, &place, 0) < 0)
return -1;
break;
case v_declfn:
if (add_fn(prog,
prog_len,
&place,
fn_list,
fn_num, 0) < 0)
return -1;
break;
case i_declfn: ;
if (add_fn(prog,
prog_len,
&place,
fn_list,
fn_num, INT_T) < 0)
return -1;
break;
default:
return -1;
}
}
return 0;
}
int
asm_add_fn(
char* prog,
uint prog_len,
uint* place,
asm_fn_info* fn_list,
uint* fn_num,
char rt)
{
char type_buf[64];
uint type_len;
asm_fn_info* cur_fn = &fn_list[*fn_num];
cur_fn->rt = rt;
if (get_type_list(
prog,
prog_len,
place,
type_buf,
&type_len) < 0)
return -1;
cur_fn->param_n = type_len;
if (get_type_list(
prog,
prog_len,
place,
type_buf,
&type_len) < 0)
return -1;
cur_fn->local_n = type_len;
*fn_num += 1;
return 0;
}
int
asm_populate_fns(
char* prog,
uint prog_len,
asm_fn_info* fn_list,
uint* fn_num)
{
uint place = 0;
*fn_num = 0;
while (place < prog_len) {
instr_token token = prog[place];
place += 1;
switch (token) {
case i_clz:
case i_ctz:
case i_popcnt:
case i_add:
case i_and:
case i_div_s:
case i_div_u:
case i_mul:
case i_or:
case i_rem_s:
case i_rem_u:
case i_rotl:
case i_rotr:
case i_shl:
case i_shr_s:
case i_shr_u:
case i_sub:
case i_xor:
case i_eqz:
case i_eq:
case i_ge_s:
case i_ge_u:
case i_gt_s:
case i_gt_u:
case i_le_s:
case i_le_u:
case i_lt_s:
case i_lt_u:
case i_ne:
case i_ld32:
case i_ld16_s:
case i_ld16_u:
case i_ld8_s:
case i_ld8_u:
case i_memsz:
case str32:
case str16:
case str8:
case trap:
case block:
case loop:
case ret:
case end:
break;
case i_const:
case get_l:
case set_l:
case br:
case br_if:
case call:
if (get_int(prog, prog_len, &place, 0) < 0)
return -1;
break;
case v_declfn:
if (asm_add_fn(prog,
prog_len,
&place,
fn_list,
fn_num, 0) < 0)
return -1;
break;
case i_declfn: ;
if (asm_add_fn(prog,
prog_len,
&place,
fn_list,
fn_num, INT_T) < 0)
return -1;
break;
default:
return -1;
}
}
return 0;
}