1 /* Simple expression parser */
7 #include "util/debug.h"
14 %parse-param { double *final_val }
15 %parse-param { struct expr_parse_ctx *ctx }
16 %parse-param { bool compute_ids }
17 %parse-param {void *scanner}
18 %lex-param {void* scanner}
25 * When creating ids, holds the working set of event ids. NULL
26 * implies the set is empty.
30 * The metric value. When not creating ids this is the value
31 * read from a counter, a constant or some computed value. When
32 * creating ids the value is either a constant or BOTTOM. NAN is
33 * used as the special BOTTOM value, representing a "set of all
40 %token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT HAS_EVENT EXPR_ERROR
49 %type <num> NUMBER LITERAL
51 %destructor { free ($$); } <str>
52 %type <ids> expr if_expr
53 %destructor { ids__free($$.ids); } <ids>
56 static void expr_error(double *final_val __maybe_unused,
57 struct expr_parse_ctx *ctx __maybe_unused,
58 bool compute_ids __maybe_unused,
66 * During compute ids, the special "bottom" value uses NAN to represent the set
67 * of all values. NAN is selected as it isn't a useful constant value.
71 /* During computing ids, does val represent a constant (non-BOTTOM) value? */
72 static bool is_const(double val)
77 static struct ids union_expr(struct ids ids1, struct ids ids2)
81 .ids = ids__union(ids1.ids, ids2.ids),
86 static struct ids handle_id(struct expr_parse_ctx *ctx, char *id,
87 bool compute_ids, bool source_count)
93 * Compute the event's value from ID. If the ID isn't known then
94 * it isn't used to compute the formula so set to NAN.
96 struct expr_id_data *data;
99 if (expr__resolve_id(ctx, id, &data) == 0) {
100 result.val = source_count
101 ? expr_id_data__source_count(data)
102 : expr_id_data__value(data);
108 * Set the value to BOTTOM to show that any value is possible
109 * when the event is computed. Create a set of just the ID.
112 result.ids = ids__new();
113 if (!result.ids || ids__insert(result.ids, id)) {
114 pr_err("Error creating IDs for '%s'", id);
122 * If we're not computing ids or $1 and $3 are constants, compute the new
123 * constant value using OP. Its invariant that there are no ids. If computing
124 * ids for non-constants union the set of IDs that must be computed.
126 #define BINARY_OP(RESULT, OP, LHS, RHS) \
127 if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
128 assert(LHS.ids == NULL); \
129 assert(RHS.ids == NULL); \
130 if (isnan(LHS.val) || isnan(RHS.val)) { \
133 RESULT.val = LHS.val OP RHS.val; \
137 RESULT = union_expr(LHS, RHS); \
146 ctx->ids = ids__union($1.ids, ctx->ids);
153 if_expr: expr IF expr ELSE if_expr
155 if (fpclassify($3.val) == FP_ZERO) {
157 * The IF expression evaluated to 0 so treat as false, take the
158 * ELSE and discard everything else.
164 } else if (!compute_ids || is_const($3.val)) {
166 * If ids aren't computed then treat the expression as true. If
167 * ids are being computed and the IF expr is a non-zero
168 * constant, then also evaluate the true case.
174 } else if ($1.val == $5.val) {
176 * LHS == RHS, so both are an identical constant. No need to
177 * evaluate any events.
186 * Value is either the LHS or RHS and we need the IF expression
189 $$ = union_expr($1, union_expr($3, $5));
200 | ID { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
201 | SOURCE_COUNT '(' ID ')' { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
202 | HAS_EVENT '(' ID ')'
204 $$.val = expr__has_event(ctx, compute_ids, $3);
210 if (is_const($1.val) && is_const($3.val)) {
211 assert($1.ids == NULL);
212 assert($3.ids == NULL);
214 $$.val = (fpclassify($1.val) == FP_ZERO && fpclassify($3.val) == FP_ZERO) ? 0 : 1;
215 } else if (is_const($1.val)) {
216 assert($1.ids == NULL);
217 if (fpclassify($1.val) == FP_ZERO) {
224 } else if (is_const($3.val)) {
225 assert($3.ids == NULL);
226 if (fpclassify($3.val) == FP_ZERO) {
234 $$ = union_expr($1, $3);
239 if (is_const($1.val) && is_const($3.val)) {
240 assert($1.ids == NULL);
241 assert($3.ids == NULL);
242 $$.val = (fpclassify($1.val) != FP_ZERO && fpclassify($3.val) != FP_ZERO) ? 1 : 0;
244 } else if (is_const($1.val)) {
245 assert($1.ids == NULL);
246 if (fpclassify($1.val) != FP_ZERO) {
253 } else if (is_const($3.val)) {
254 assert($3.ids == NULL);
255 if (fpclassify($3.val) != FP_ZERO) {
263 $$ = union_expr($1, $3);
268 if (is_const($1.val) && is_const($3.val)) {
269 assert($1.ids == NULL);
270 assert($3.ids == NULL);
271 $$.val = (fpclassify($1.val) == FP_ZERO) != (fpclassify($3.val) == FP_ZERO) ? 1 : 0;
274 $$ = union_expr($1, $3);
277 | expr '<' expr { BINARY_OP($$, <, $1, $3); }
278 | expr '>' expr { BINARY_OP($$, >, $1, $3); }
279 | expr '+' expr { BINARY_OP($$, +, $1, $3); }
280 | expr '-' expr { BINARY_OP($$, -, $1, $3); }
281 | expr '*' expr { BINARY_OP($$, *, $1, $3); }
284 if (fpclassify($3.val) == FP_ZERO) {
285 pr_debug("division by zero\n");
286 assert($3.ids == NULL);
291 } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
292 assert($1.ids == NULL);
293 assert($3.ids == NULL);
294 $$.val = $1.val / $3.val;
297 /* LHS and/or RHS need computing from event IDs so union. */
298 $$ = union_expr($1, $3);
303 if (fpclassify($3.val) == FP_ZERO) {
304 pr_debug("division by zero\n");
306 } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
307 assert($1.ids == NULL);
308 assert($3.ids == NULL);
309 $$.val = (long)$1.val % (long)$3.val;
312 /* LHS and/or RHS need computing from event IDs so union. */
313 $$ = union_expr($1, $3);
316 | D_RATIO '(' expr ',' expr ')'
318 if (fpclassify($5.val) == FP_ZERO) {
320 * Division by constant zero always yields zero and no events
323 assert($5.ids == NULL);
327 } else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
328 assert($3.ids == NULL);
329 assert($5.ids == NULL);
330 $$.val = $3.val / $5.val;
333 /* LHS and/or RHS need computing from event IDs so union. */
334 $$ = union_expr($3, $5);
346 | MIN '(' expr ',' expr ')'
349 $$.val = $3.val < $5.val ? $3.val : $5.val;
352 $$ = union_expr($3, $5);
355 | MAX '(' expr ',' expr ')'
358 $$.val = $3.val > $5.val ? $3.val : $5.val;
361 $$ = union_expr($3, $5);