1 /* Simple expression parser */
7 #include "util/debug.h"
10 #include "expr-bison.h"
11 int expr_lex(YYSTYPE * yylval_param , void *yyscanner);
16 %parse-param { double *final_val }
17 %parse-param { struct expr_parse_ctx *ctx }
18 %parse-param { bool compute_ids }
19 %parse-param {void *scanner}
20 %lex-param {void* scanner}
27 * When creating ids, holds the working set of event ids. NULL
28 * implies the set is empty.
32 * The metric value. When not creating ids this is the value
33 * read from a counter, a constant or some computed value. When
34 * creating ids the value is either a constant or BOTTOM. NAN is
35 * used as the special BOTTOM value, representing a "set of all
42 %token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT HAS_EVENT EXPR_ERROR
51 %type <num> NUMBER LITERAL
53 %destructor { free ($$); } <str>
54 %type <ids> expr if_expr
55 %destructor { ids__free($$.ids); } <ids>
58 static void expr_error(double *final_val __maybe_unused,
59 struct expr_parse_ctx *ctx __maybe_unused,
60 bool compute_ids __maybe_unused,
61 void *scanner __maybe_unused,
68 * During compute ids, the special "bottom" value uses NAN to represent the set
69 * of all values. NAN is selected as it isn't a useful constant value.
73 /* During computing ids, does val represent a constant (non-BOTTOM) value? */
74 static bool is_const(double val)
79 static struct ids union_expr(struct ids ids1, struct ids ids2)
83 .ids = ids__union(ids1.ids, ids2.ids),
88 static struct ids handle_id(struct expr_parse_ctx *ctx, char *id,
89 bool compute_ids, bool source_count)
95 * Compute the event's value from ID. If the ID isn't known then
96 * it isn't used to compute the formula so set to NAN.
98 struct expr_id_data *data;
101 if (expr__resolve_id(ctx, id, &data) == 0) {
102 result.val = source_count
103 ? expr_id_data__source_count(data)
104 : expr_id_data__value(data);
110 * Set the value to BOTTOM to show that any value is possible
111 * when the event is computed. Create a set of just the ID.
114 result.ids = ids__new();
115 if (!result.ids || ids__insert(result.ids, id)) {
116 pr_err("Error creating IDs for '%s'", id);
124 * If we're not computing ids or $1 and $3 are constants, compute the new
125 * constant value using OP. Its invariant that there are no ids. If computing
126 * ids for non-constants union the set of IDs that must be computed.
128 #define BINARY_OP(RESULT, OP, LHS, RHS) \
129 if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
130 assert(LHS.ids == NULL); \
131 assert(RHS.ids == NULL); \
132 if (isnan(LHS.val) || isnan(RHS.val)) { \
135 RESULT.val = LHS.val OP RHS.val; \
139 RESULT = union_expr(LHS, RHS); \
148 ctx->ids = ids__union($1.ids, ctx->ids);
155 if_expr: expr IF expr ELSE if_expr
157 if (fpclassify($3.val) == FP_ZERO) {
159 * The IF expression evaluated to 0 so treat as false, take the
160 * ELSE and discard everything else.
166 } else if (!compute_ids || is_const($3.val)) {
168 * If ids aren't computed then treat the expression as true. If
169 * ids are being computed and the IF expr is a non-zero
170 * constant, then also evaluate the true case.
176 } else if ($1.val == $5.val) {
178 * LHS == RHS, so both are an identical constant. No need to
179 * evaluate any events.
188 * Value is either the LHS or RHS and we need the IF expression
191 $$ = union_expr($1, union_expr($3, $5));
202 | ID { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
203 | SOURCE_COUNT '(' ID ')' { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
204 | HAS_EVENT '(' ID ')'
206 $$.val = expr__has_event(ctx, compute_ids, $3);
212 if (is_const($1.val) && is_const($3.val)) {
213 assert($1.ids == NULL);
214 assert($3.ids == NULL);
216 $$.val = (fpclassify($1.val) == FP_ZERO && fpclassify($3.val) == FP_ZERO) ? 0 : 1;
217 } else if (is_const($1.val)) {
218 assert($1.ids == NULL);
219 if (fpclassify($1.val) == FP_ZERO) {
226 } else if (is_const($3.val)) {
227 assert($3.ids == NULL);
228 if (fpclassify($3.val) == FP_ZERO) {
236 $$ = union_expr($1, $3);
241 if (is_const($1.val) && is_const($3.val)) {
242 assert($1.ids == NULL);
243 assert($3.ids == NULL);
244 $$.val = (fpclassify($1.val) != FP_ZERO && fpclassify($3.val) != FP_ZERO) ? 1 : 0;
246 } else if (is_const($1.val)) {
247 assert($1.ids == NULL);
248 if (fpclassify($1.val) != FP_ZERO) {
255 } else if (is_const($3.val)) {
256 assert($3.ids == NULL);
257 if (fpclassify($3.val) != FP_ZERO) {
265 $$ = union_expr($1, $3);
270 if (is_const($1.val) && is_const($3.val)) {
271 assert($1.ids == NULL);
272 assert($3.ids == NULL);
273 $$.val = (fpclassify($1.val) == FP_ZERO) != (fpclassify($3.val) == FP_ZERO) ? 1 : 0;
276 $$ = union_expr($1, $3);
279 | expr '<' expr { BINARY_OP($$, <, $1, $3); }
280 | expr '>' expr { BINARY_OP($$, >, $1, $3); }
281 | expr '+' expr { BINARY_OP($$, +, $1, $3); }
282 | expr '-' expr { BINARY_OP($$, -, $1, $3); }
283 | expr '*' expr { BINARY_OP($$, *, $1, $3); }
286 if (fpclassify($3.val) == FP_ZERO) {
287 pr_debug("division by zero\n");
288 assert($3.ids == NULL);
293 } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
294 assert($1.ids == NULL);
295 assert($3.ids == NULL);
296 $$.val = $1.val / $3.val;
299 /* LHS and/or RHS need computing from event IDs so union. */
300 $$ = union_expr($1, $3);
305 if (fpclassify($3.val) == FP_ZERO) {
306 pr_debug("division by zero\n");
308 } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
309 assert($1.ids == NULL);
310 assert($3.ids == NULL);
311 $$.val = (long)$1.val % (long)$3.val;
314 /* LHS and/or RHS need computing from event IDs so union. */
315 $$ = union_expr($1, $3);
318 | D_RATIO '(' expr ',' expr ')'
320 if (fpclassify($5.val) == FP_ZERO) {
322 * Division by constant zero always yields zero and no events
325 assert($5.ids == NULL);
329 } else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
330 assert($3.ids == NULL);
331 assert($5.ids == NULL);
332 $$.val = $3.val / $5.val;
335 /* LHS and/or RHS need computing from event IDs so union. */
336 $$ = union_expr($3, $5);
348 | MIN '(' expr ',' expr ')'
351 $$.val = $3.val < $5.val ? $3.val : $5.val;
354 $$ = union_expr($3, $5);
357 | MAX '(' expr ',' expr ')'
360 $$.val = $3.val > $5.val ? $3.val : $5.val;
363 $$ = union_expr($3, $5);