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
|
/*
* This file is part of ltrace.
* Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include "expr.h"
static void
expr_init_common(struct expr_node *node, enum expr_node_kind kind)
{
node->kind = kind;
node->lhs = NULL;
node->own_lhs = 0;
memset(&node->u, 0, sizeof(node->u));
}
void
expr_init_self(struct expr_node *node)
{
expr_init_common(node, EXPR_OP_SELF);
}
void
expr_init_named(struct expr_node *node,
const char *name, int own_name)
{
expr_init_common(node, EXPR_OP_NAMED);
node->u.name.s = name;
node->u.name.own = own_name;
}
void
expr_init_argno(struct expr_node *node, size_t num)
{
expr_init_common(node, EXPR_OP_ARGNO);
node->u.num = num;
}
void
expr_init_const(struct expr_node *node, struct value *val)
{
expr_init_common(node, EXPR_OP_CONST);
node->u.value = *val;
}
void
expr_init_const_word(struct expr_node *node, long l,
struct arg_type_info *type, int own_type)
{
struct value val;
value_init_detached(&val, NULL, type, own_type);
value_set_word(&val, l);
expr_init_const(node, &val);
}
void
expr_init_index(struct expr_node *node,
struct expr_node *lhs, int own_lhs,
struct expr_node *rhs, int own_rhs)
{
expr_init_common(node, EXPR_OP_INDEX);
node->lhs = lhs;
node->own_lhs = own_lhs;
node->u.node.n = rhs;
node->u.node.own = own_rhs;
}
void
expr_init_up(struct expr_node *node, struct expr_node *lhs, int own_lhs)
{
assert(lhs != NULL);
expr_init_common(node, EXPR_OP_UP);
node->lhs = lhs;
node->own_lhs = own_lhs;
}
void
expr_init_cb1(struct expr_node *node,
int (*cb)(struct value *ret_value, struct value *value,
struct value_dict *arguments, void *data),
struct expr_node *lhs, int own_lhs, void *data)
{
expr_init_common(node, EXPR_OP_CALL1);
node->lhs = lhs;
node->own_lhs = own_lhs;
node->u.call.u.cb1 = cb;
node->u.call.data = data;
}
void
expr_init_cb2(struct expr_node *node,
int (*cb)(struct value *ret_value,
struct value *lhs, struct value *rhs,
struct value_dict *arguments, void *data),
struct expr_node *lhs, int own_lhs,
struct expr_node *rhs, int own_rhs, void *data)
{
expr_init_common(node, EXPR_OP_CALL2);
node->lhs = lhs;
node->own_lhs = own_lhs;
node->u.call.rhs = rhs;
node->u.call.own_rhs = own_rhs;
node->u.call.u.cb2 = cb;
node->u.call.data = data;
}
static void
release_expr(struct expr_node *node, int own)
{
if (own) {
expr_destroy(node);
free(node);
}
}
void
expr_destroy(struct expr_node *node)
{
if (node == NULL)
return;
switch (node->kind) {
case EXPR_OP_ARGNO:
case EXPR_OP_SELF:
return;
case EXPR_OP_CONST:
value_destroy(&node->u.value);
return;
case EXPR_OP_NAMED:
if (node->u.name.own)
free((char *)node->u.name.s);
return;
case EXPR_OP_INDEX:
release_expr(node->lhs, node->own_lhs);
release_expr(node->u.node.n, node->u.node.own);
return;
case EXPR_OP_CALL2:
release_expr(node->u.call.rhs, node->u.call.own_rhs);
/* Fall through. */
case EXPR_OP_UP:
case EXPR_OP_CALL1:
release_expr(node->lhs, node->own_lhs);
return;
}
assert(!"Invalid value of node kind");
abort();
}
static int
expr_alloc_and_clone(struct expr_node **retpp, struct expr_node *node, int own)
{
*retpp = node;
if (own) {
*retpp = malloc(sizeof **retpp);
if (*retpp == NULL || expr_clone(*retpp, node) < 0) {
free(*retpp);
return -1;
}
}
return 0;
}
int
expr_clone(struct expr_node *retp, const struct expr_node *node)
{
*retp = *node;
switch (node->kind) {
struct expr_node *nlhs;
struct expr_node *nrhs;
case EXPR_OP_ARGNO:
case EXPR_OP_SELF:
return 0;
case EXPR_OP_CONST:
return value_clone(&retp->u.value, &node->u.value);
case EXPR_OP_NAMED:
if (node->u.name.own
&& (retp->u.name.s = strdup(node->u.name.s)) == NULL)
return -1;
return 0;
case EXPR_OP_INDEX:
if (expr_alloc_and_clone(&nlhs, node->lhs, node->own_lhs) < 0)
return -1;
if (expr_alloc_and_clone(&nrhs, node->u.node.n,
node->u.node.own) < 0) {
if (nlhs != node->lhs) {
expr_destroy(nlhs);
free(nlhs);
}
return -1;
}
retp->lhs = nlhs;
retp->u.node.n = nrhs;
return 0;
case EXPR_OP_CALL2:
if (expr_alloc_and_clone(&nrhs, node->u.call.rhs,
node->u.call.own_rhs) < 0)
return -1;
retp->u.call.rhs = nrhs;
/* Fall through. */
case EXPR_OP_UP:
case EXPR_OP_CALL1:
if (expr_alloc_and_clone(&nlhs, node->lhs, node->own_lhs) < 0) {
if (node->kind == EXPR_OP_CALL2
&& node->u.call.own_rhs) {
expr_destroy(nrhs);
free(nrhs);
return -1;
}
}
retp->lhs = nlhs;
return 0;
}
assert(!"Invalid value of node kind");
abort();
}
int
expr_is_compile_constant(struct expr_node *node)
{
return node->kind == EXPR_OP_CONST;
}
static int
eval_up(struct expr_node *node, struct value *context,
struct value_dict *arguments, struct value *ret_value)
{
if (expr_eval(node->lhs, context, arguments, ret_value) < 0)
return -1;
struct value *parent = value_get_parental_struct(ret_value);
if (parent == NULL) {
value_destroy(ret_value);
return -1;
}
*ret_value = *parent;
return 0;
}
static int
eval_cb1(struct expr_node *node, struct value *context,
struct value_dict *arguments, struct value *ret_value)
{
struct value val;
if (expr_eval(node->lhs, context, arguments, &val) < 0)
return -1;
int ret = 0;
if (node->u.call.u.cb1(ret_value, &val, arguments,
node->u.call.data) < 0)
ret = -1;
/* N.B. the callback must return its own value, or somehow
* clone the incoming argument. */
value_destroy(&val);
return ret;
}
static int
eval_cb2(struct expr_node *node, struct value *context,
struct value_dict *arguments, struct value *ret_value)
{
struct value lhs;
if (expr_eval(node->lhs, context, arguments, &lhs) < 0)
return -1;
struct value rhs;
if (expr_eval(node->u.call.rhs, context, arguments, &rhs) < 0) {
value_destroy(&lhs);
return -1;
}
int ret = 0;
if (node->u.call.u.cb2(ret_value, &lhs, &rhs, arguments,
node->u.call.data) < 0)
ret = -1;
/* N.B. the callback must return its own value, or somehow
* clone the incoming argument. */
value_destroy(&lhs);
value_destroy(&rhs);
return ret;
}
int
eval_index(struct expr_node *node, struct value *context,
struct value_dict *arguments, struct value *ret_value)
{
struct value lhs;
if (expr_eval(node->lhs, context, arguments, &lhs) < 0)
return -1;
long l;
if (expr_eval_word(node->u.node.n, context, arguments, &l) < 0) {
fail:
value_destroy(&lhs);
return -1;
}
if (value_init_element(ret_value, &lhs, (size_t)l) < 0)
goto fail;
return 0;
}
int
expr_eval(struct expr_node *node, struct value *context,
struct value_dict *arguments, struct value *ret_value)
{
switch (node->kind) {
struct value *valp;
case EXPR_OP_ARGNO:
valp = val_dict_get_num(arguments, node->u.num);
if (valp == NULL)
return -1;
*ret_value = *valp;
return 0;
case EXPR_OP_NAMED:
valp = val_dict_get_name(arguments, node->u.name.s);
if (valp == NULL)
return -1;
*ret_value = *valp;
return 0;
case EXPR_OP_SELF:
*ret_value = *context;
return 0;
case EXPR_OP_CONST:
*ret_value = node->u.value;
return 0;
case EXPR_OP_INDEX:
return eval_index(node, context, arguments, ret_value);
case EXPR_OP_UP:
return eval_up(node, context, arguments, ret_value);
case EXPR_OP_CALL1:
return eval_cb1(node, context, arguments, ret_value);
case EXPR_OP_CALL2:
return eval_cb2(node, context, arguments, ret_value);
}
assert(!"Unknown node kind.");
abort();
}
int
expr_eval_word(struct expr_node *node, struct value *context,
struct value_dict *arguments, long *ret_value)
{
struct value val;
if (expr_eval(node, context, arguments, &val) < 0)
return -1;
int ret = 0;
if (value_extract_word(&val, ret_value, arguments) < 0)
ret = -1;
value_destroy(&val);
return ret;
}
int
expr_eval_constant(struct expr_node *node, long *valuep)
{
assert(expr_is_compile_constant(node));
return expr_eval_word(node, NULL, NULL, valuep);
}
struct expr_node *
expr_self(void)
{
static struct expr_node *nodep = NULL;
if (nodep == NULL) {
static struct expr_node node;
expr_init_self(&node);
nodep = &node;
}
return nodep;
}
|