2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 expr_init_common(struct expr_node *node, enum expr_node_kind kind)
34 memset(&node->u, 0, sizeof(node->u));
38 expr_init_self(struct expr_node *node)
40 expr_init_common(node, EXPR_OP_SELF);
44 expr_init_named(struct expr_node *node,
45 const char *name, int own_name)
47 expr_init_common(node, EXPR_OP_NAMED);
48 node->u.name.s = name;
49 node->u.name.own = own_name;
53 expr_init_argno(struct expr_node *node, size_t num)
55 expr_init_common(node, EXPR_OP_ARGNO);
60 expr_init_const(struct expr_node *node, struct value *val)
62 expr_init_common(node, EXPR_OP_CONST);
67 expr_init_const_word(struct expr_node *node, long l,
68 struct arg_type_info *type, int own_type)
71 value_init_detached(&val, NULL, type, own_type);
72 value_set_word(&val, l);
73 expr_init_const(node, &val);
77 expr_init_index(struct expr_node *node,
78 struct expr_node *lhs, int own_lhs,
79 struct expr_node *rhs, int own_rhs)
81 expr_init_common(node, EXPR_OP_INDEX);
83 node->own_lhs = own_lhs;
85 node->u.node.own = own_rhs;
89 expr_init_up(struct expr_node *node, struct expr_node *lhs, int own_lhs)
92 expr_init_common(node, EXPR_OP_UP);
94 node->own_lhs = own_lhs;
98 expr_init_cb1(struct expr_node *node,
99 int (*cb)(struct value *ret_value, struct value *value,
100 struct value_dict *arguments, void *data),
101 struct expr_node *lhs, int own_lhs, void *data)
103 expr_init_common(node, EXPR_OP_CALL1);
105 node->own_lhs = own_lhs;
106 node->u.call.u.cb1 = cb;
107 node->u.call.data = data;
111 expr_init_cb2(struct expr_node *node,
112 int (*cb)(struct value *ret_value,
113 struct value *lhs, struct value *rhs,
114 struct value_dict *arguments, void *data),
115 struct expr_node *lhs, int own_lhs,
116 struct expr_node *rhs, int own_rhs, void *data)
118 expr_init_common(node, EXPR_OP_CALL2);
120 node->own_lhs = own_lhs;
121 node->u.call.rhs = rhs;
122 node->u.call.own_rhs = own_rhs;
123 node->u.call.u.cb2 = cb;
124 node->u.call.data = data;
128 release_expr(struct expr_node *node, int own)
137 expr_destroy(struct expr_node *node)
142 switch (node->kind) {
148 value_destroy(&node->u.value);
152 if (node->u.name.own)
153 free((char *)node->u.name.s);
157 release_expr(node->lhs, node->own_lhs);
158 release_expr(node->u.node.n, node->u.node.own);
162 release_expr(node->u.call.rhs, node->u.call.own_rhs);
166 release_expr(node->lhs, node->own_lhs);
170 assert(!"Invalid value of node kind");
175 expr_alloc_and_clone(struct expr_node **retpp, struct expr_node *node, int own)
179 *retpp = malloc(sizeof **retpp);
180 if (*retpp == NULL || expr_clone(*retpp, node) < 0) {
189 expr_clone(struct expr_node *retp, const struct expr_node *node)
193 switch (node->kind) {
194 struct expr_node *nlhs;
195 struct expr_node *nrhs;
202 return value_clone(&retp->u.value, &node->u.value);
206 && (retp->u.name.s = strdup(node->u.name.s)) == NULL)
211 if (expr_alloc_and_clone(&nlhs, node->lhs, node->own_lhs) < 0)
214 if (expr_alloc_and_clone(&nrhs, node->u.node.n,
215 node->u.node.own) < 0) {
216 if (nlhs != node->lhs) {
224 retp->u.node.n = nrhs;
228 if (expr_alloc_and_clone(&nrhs, node->u.call.rhs,
229 node->u.call.own_rhs) < 0)
231 retp->u.call.rhs = nrhs;
233 if (expr_alloc_and_clone(&nlhs, node->lhs, node->own_lhs) < 0) {
234 if (node->u.call.own_rhs) {
246 if (expr_alloc_and_clone(&nlhs, node->lhs, node->own_lhs) < 0)
253 assert(!"Invalid value of node kind");
258 expr_is_compile_constant(struct expr_node *node)
260 return node->kind == EXPR_OP_CONST;
264 eval_up(struct expr_node *node, struct value *context,
265 struct value_dict *arguments, struct value *ret_value)
267 if (expr_eval(node->lhs, context, arguments, ret_value) < 0)
269 struct value *parent = value_get_parental_struct(ret_value);
270 if (parent == NULL) {
271 value_destroy(ret_value);
274 *ret_value = *parent;
279 eval_cb1(struct expr_node *node, struct value *context,
280 struct value_dict *arguments, struct value *ret_value)
283 if (expr_eval(node->lhs, context, arguments, &val) < 0)
287 if (node->u.call.u.cb1(ret_value, &val, arguments,
288 node->u.call.data) < 0)
291 /* N.B. the callback must return its own value, or somehow
292 * clone the incoming argument. */
298 eval_cb2(struct expr_node *node, struct value *context,
299 struct value_dict *arguments, struct value *ret_value)
302 if (expr_eval(node->lhs, context, arguments, &lhs) < 0)
306 if (expr_eval(node->u.call.rhs, context, arguments, &rhs) < 0) {
312 if (node->u.call.u.cb2(ret_value, &lhs, &rhs, arguments,
313 node->u.call.data) < 0)
316 /* N.B. the callback must return its own value, or somehow
317 * clone the incoming argument. */
324 eval_index(struct expr_node *node, struct value *context,
325 struct value_dict *arguments, struct value *ret_value)
328 if (expr_eval(node->lhs, context, arguments, &lhs) < 0)
332 if (expr_eval_word(node->u.node.n, context, arguments, &l) < 0) {
338 if (value_init_element(ret_value, &lhs, (size_t)l) < 0)
344 expr_eval(struct expr_node *node, struct value *context,
345 struct value_dict *arguments, struct value *ret_value)
347 switch (node->kind) {
350 valp = val_dict_get_num(arguments, node->u.num);
357 valp = val_dict_get_name(arguments, node->u.name.s);
364 *ret_value = *context;
368 *ret_value = node->u.value;
372 return eval_index(node, context, arguments, ret_value);
375 return eval_up(node, context, arguments, ret_value);
378 return eval_cb1(node, context, arguments, ret_value);
381 return eval_cb2(node, context, arguments, ret_value);
384 assert(!"Unknown node kind.");
389 expr_eval_word(struct expr_node *node, struct value *context,
390 struct value_dict *arguments, long *ret_value)
393 if (expr_eval(node, context, arguments, &val) < 0)
396 if (value_extract_word(&val, ret_value, arguments) < 0)
403 expr_eval_constant(struct expr_node *node, long *valuep)
405 assert(expr_is_compile_constant(node));
406 return expr_eval_word(node, NULL, NULL, valuep);
412 static struct expr_node *nodep = NULL;
414 static struct expr_node node;
415 expr_init_self(&node);