107516736SAndi Kleen /* Simple expression parser */ 207516736SAndi Kleen %{ 326226a97SJiri Olsa #define YYDEBUG 1 43f965a7dSIan Rogers #include <assert.h> 5edfe7f55SIan Rogers #include <math.h> 61e7ab829SIan Rogers #include <stdlib.h> 707516736SAndi Kleen #include "util/debug.h" 807516736SAndi Kleen #define IN_EXPR_Y 1 907516736SAndi Kleen #include "expr.h" 1007516736SAndi Kleen %} 1107516736SAndi Kleen 12fc8c0a99SJiri Olsa %define api.pure full 13fc8c0a99SJiri Olsa 1407516736SAndi Kleen %parse-param { double *final_val } 15aecce63eSJiri Olsa %parse-param { struct expr_parse_ctx *ctx } 163f965a7dSIan Rogers %parse-param { bool compute_ids } 1726226a97SJiri Olsa %parse-param {void *scanner} 1826226a97SJiri Olsa %lex-param {void* scanner} 1907516736SAndi Kleen 2007516736SAndi Kleen %union { 2107516736SAndi Kleen double num; 2226226a97SJiri Olsa char *str; 233f965a7dSIan Rogers struct ids { 243f965a7dSIan Rogers /* 253f965a7dSIan Rogers * When creating ids, holds the working set of event ids. NULL 263f965a7dSIan Rogers * implies the set is empty. 273f965a7dSIan Rogers */ 283f965a7dSIan Rogers struct hashmap *ids; 293f965a7dSIan Rogers /* 303f965a7dSIan Rogers * The metric value. When not creating ids this is the value 313f965a7dSIan Rogers * read from a counter, a constant or some computed value. When 323f965a7dSIan Rogers * creating ids the value is either a constant or BOTTOM. NAN is 333f965a7dSIan Rogers * used as the special BOTTOM value, representing a "set of all 343f965a7dSIan Rogers * values" case. 353f965a7dSIan Rogers */ 363f965a7dSIan Rogers double val; 373f965a7dSIan Rogers } ids; 3807516736SAndi Kleen } 3907516736SAndi Kleen 409aba0adaSIan Rogers %token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT EXPR_ERROR 41d73bad06SAndi Kleen %left MIN MAX IF 4207516736SAndi Kleen %left '|' 4307516736SAndi Kleen %left '^' 4407516736SAndi Kleen %left '&' 45ff1a12f9SIan Rogers %left '<' '>' 4607516736SAndi Kleen %left '-' '+' 4707516736SAndi Kleen %left '*' '/' '%' 4807516736SAndi Kleen %left NEG NOT 493613f6c1SIan Rogers %type <num> NUMBER LITERAL 50aed0d6f8SIan Rogers %type <str> ID 51aed0d6f8SIan Rogers %destructor { free ($$); } <str> 523f965a7dSIan Rogers %type <ids> expr if_expr 533f965a7dSIan Rogers %destructor { ids__free($$.ids); } <ids> 5407516736SAndi Kleen 5507516736SAndi Kleen %{ 5626226a97SJiri Olsa static void expr_error(double *final_val __maybe_unused, 57aecce63eSJiri Olsa struct expr_parse_ctx *ctx __maybe_unused, 583f965a7dSIan Rogers bool compute_ids __maybe_unused, 5926226a97SJiri Olsa void *scanner, 6007516736SAndi Kleen const char *s) 6107516736SAndi Kleen { 6207516736SAndi Kleen pr_debug("%s\n", s); 6307516736SAndi Kleen } 6407516736SAndi Kleen 653f965a7dSIan Rogers /* 663f965a7dSIan Rogers * During compute ids, the special "bottom" value uses NAN to represent the set 673f965a7dSIan Rogers * of all values. NAN is selected as it isn't a useful constant value. 683f965a7dSIan Rogers */ 693f965a7dSIan Rogers #define BOTTOM NAN 703f965a7dSIan Rogers 71970f7afeSIan Rogers /* During computing ids, does val represent a constant (non-BOTTOM) value? */ 72970f7afeSIan Rogers static bool is_const(double val) 73970f7afeSIan Rogers { 74970f7afeSIan Rogers return isfinite(val); 75970f7afeSIan Rogers } 76970f7afeSIan Rogers 773f965a7dSIan Rogers static struct ids union_expr(struct ids ids1, struct ids ids2) 783f965a7dSIan Rogers { 793f965a7dSIan Rogers struct ids result = { 803f965a7dSIan Rogers .val = BOTTOM, 813f965a7dSIan Rogers .ids = ids__union(ids1.ids, ids2.ids), 823f965a7dSIan Rogers }; 833f965a7dSIan Rogers return result; 843f965a7dSIan Rogers } 853f965a7dSIan Rogers 861e7ab829SIan Rogers static struct ids handle_id(struct expr_parse_ctx *ctx, char *id, 879aba0adaSIan Rogers bool compute_ids, bool source_count) 881e7ab829SIan Rogers { 891e7ab829SIan Rogers struct ids result; 901e7ab829SIan Rogers 911e7ab829SIan Rogers if (!compute_ids) { 921e7ab829SIan Rogers /* 931e7ab829SIan Rogers * Compute the event's value from ID. If the ID isn't known then 941e7ab829SIan Rogers * it isn't used to compute the formula so set to NAN. 951e7ab829SIan Rogers */ 961e7ab829SIan Rogers struct expr_id_data *data; 971e7ab829SIan Rogers 981e7ab829SIan Rogers result.val = NAN; 999aba0adaSIan Rogers if (expr__resolve_id(ctx, id, &data) == 0) { 1009aba0adaSIan Rogers result.val = source_count 1019aba0adaSIan Rogers ? expr_id_data__source_count(data) 1029aba0adaSIan Rogers : expr_id_data__value(data); 1039aba0adaSIan Rogers } 1041e7ab829SIan Rogers result.ids = NULL; 1051e7ab829SIan Rogers free(id); 1061e7ab829SIan Rogers } else { 1071e7ab829SIan Rogers /* 1081e7ab829SIan Rogers * Set the value to BOTTOM to show that any value is possible 1091e7ab829SIan Rogers * when the event is computed. Create a set of just the ID. 1101e7ab829SIan Rogers */ 1111e7ab829SIan Rogers result.val = BOTTOM; 1121e7ab829SIan Rogers result.ids = ids__new(); 1131e7ab829SIan Rogers if (!result.ids || ids__insert(result.ids, id)) { 1141e7ab829SIan Rogers pr_err("Error creating IDs for '%s'", id); 1151e7ab829SIan Rogers free(id); 1161e7ab829SIan Rogers } 1171e7ab829SIan Rogers } 1181e7ab829SIan Rogers return result; 1191e7ab829SIan Rogers } 1201e7ab829SIan Rogers 121970f7afeSIan Rogers /* 122970f7afeSIan Rogers * If we're not computing ids or $1 and $3 are constants, compute the new 123970f7afeSIan Rogers * constant value using OP. Its invariant that there are no ids. If computing 124970f7afeSIan Rogers * ids for non-constants union the set of IDs that must be computed. 125970f7afeSIan Rogers */ 126e87576c5SIan Rogers #define BINARY_LONG_OP(RESULT, OP, LHS, RHS) \ 127970f7afeSIan Rogers if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \ 128970f7afeSIan Rogers assert(LHS.ids == NULL); \ 129970f7afeSIan Rogers assert(RHS.ids == NULL); \ 130*79802934SIan Rogers if (isnan(LHS.val) || isnan(RHS.val)) { \ 131*79802934SIan Rogers RESULT.val = NAN; \ 132*79802934SIan Rogers } else { \ 1333f965a7dSIan Rogers RESULT.val = (long)LHS.val OP (long)RHS.val; \ 134*79802934SIan Rogers } \ 1353f965a7dSIan Rogers RESULT.ids = NULL; \ 1363f965a7dSIan Rogers } else { \ 1373f965a7dSIan Rogers RESULT = union_expr(LHS, RHS); \ 1383f965a7dSIan Rogers } 139e87576c5SIan Rogers 140e87576c5SIan Rogers #define BINARY_OP(RESULT, OP, LHS, RHS) \ 141970f7afeSIan Rogers if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \ 142970f7afeSIan Rogers assert(LHS.ids == NULL); \ 143970f7afeSIan Rogers assert(RHS.ids == NULL); \ 144*79802934SIan Rogers if (isnan(LHS.val) || isnan(RHS.val)) { \ 145*79802934SIan Rogers RESULT.val = NAN; \ 146*79802934SIan Rogers } else { \ 1473f965a7dSIan Rogers RESULT.val = LHS.val OP RHS.val; \ 148*79802934SIan Rogers } \ 1493f965a7dSIan Rogers RESULT.ids = NULL; \ 1503f965a7dSIan Rogers } else { \ 1513f965a7dSIan Rogers RESULT = union_expr(LHS, RHS); \ 1523f965a7dSIan Rogers } 153e87576c5SIan Rogers 15407516736SAndi Kleen %} 15507516736SAndi Kleen %% 15607516736SAndi Kleen 1573f965a7dSIan Rogers start: if_expr 15826226a97SJiri Olsa { 1593f965a7dSIan Rogers if (compute_ids) 1603f965a7dSIan Rogers ctx->ids = ids__union($1.ids, ctx->ids); 16126226a97SJiri Olsa 1623f965a7dSIan Rogers if (final_val) 1633f965a7dSIan Rogers *final_val = $1.val; 1643f965a7dSIan Rogers } 1653f965a7dSIan Rogers ; 166d73bad06SAndi Kleen 1674b65fc7bSIan Rogers if_expr: expr IF expr ELSE if_expr 168c924e0ccSIan Rogers { 1693f965a7dSIan Rogers if (fpclassify($3.val) == FP_ZERO) { 170a8e4e880SIan Rogers /* 171a8e4e880SIan Rogers * The IF expression evaluated to 0 so treat as false, take the 172a8e4e880SIan Rogers * ELSE and discard everything else. 173a8e4e880SIan Rogers */ 1743f965a7dSIan Rogers $$.val = $5.val; 175a8e4e880SIan Rogers $$.ids = $5.ids; 176a8e4e880SIan Rogers ids__free($1.ids); 177a8e4e880SIan Rogers ids__free($3.ids); 178a8e4e880SIan Rogers } else if (!compute_ids || is_const($3.val)) { 179a8e4e880SIan Rogers /* 180a8e4e880SIan Rogers * If ids aren't computed then treat the expression as true. If 181a8e4e880SIan Rogers * ids are being computed and the IF expr is a non-zero 182a8e4e880SIan Rogers * constant, then also evaluate the true case. 183a8e4e880SIan Rogers */ 1843f965a7dSIan Rogers $$.val = $1.val; 185a8e4e880SIan Rogers $$.ids = $1.ids; 186a8e4e880SIan Rogers ids__free($3.ids); 187a8e4e880SIan Rogers ids__free($5.ids); 18894886961SIan Rogers } else if ($1.val == $5.val) { 18994886961SIan Rogers /* 19094886961SIan Rogers * LHS == RHS, so both are an identical constant. No need to 19194886961SIan Rogers * evaluate any events. 19294886961SIan Rogers */ 19394886961SIan Rogers $$.val = $1.val; 19494886961SIan Rogers $$.ids = NULL; 19594886961SIan Rogers ids__free($1.ids); 19694886961SIan Rogers ids__free($3.ids); 19794886961SIan Rogers ids__free($5.ids); 1983f965a7dSIan Rogers } else { 199a8e4e880SIan Rogers /* 200a8e4e880SIan Rogers * Value is either the LHS or RHS and we need the IF expression 201a8e4e880SIan Rogers * to compute it. 202a8e4e880SIan Rogers */ 2033f965a7dSIan Rogers $$ = union_expr($1, union_expr($3, $5)); 2043f965a7dSIan Rogers } 205c924e0ccSIan Rogers } 206d73bad06SAndi Kleen | expr 20707516736SAndi Kleen ; 20807516736SAndi Kleen 20907516736SAndi Kleen expr: NUMBER 210c924e0ccSIan Rogers { 2113f965a7dSIan Rogers $$.val = $1; 2123f965a7dSIan Rogers $$.ids = NULL; 213c924e0ccSIan Rogers } 2149aba0adaSIan Rogers | ID { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); } 2159aba0adaSIan Rogers | SOURCE_COUNT '(' ID ')' { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); } 216e87576c5SIan Rogers | expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); } 217e87576c5SIan Rogers | expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); } 218e87576c5SIan Rogers | expr '^' expr { BINARY_LONG_OP($$, ^, $1, $3); } 219e87576c5SIan Rogers | expr '<' expr { BINARY_OP($$, <, $1, $3); } 220e87576c5SIan Rogers | expr '>' expr { BINARY_OP($$, >, $1, $3); } 221e87576c5SIan Rogers | expr '+' expr { BINARY_OP($$, +, $1, $3); } 222e87576c5SIan Rogers | expr '-' expr { BINARY_OP($$, -, $1, $3); } 223e87576c5SIan Rogers | expr '*' expr { BINARY_OP($$, *, $1, $3); } 224c924e0ccSIan Rogers | expr '/' expr 225c924e0ccSIan Rogers { 2263f965a7dSIan Rogers if (fpclassify($3.val) == FP_ZERO) { 2279be27a5dSIan Rogers pr_debug("division by zero\n"); 2289be27a5dSIan Rogers YYABORT; 229970f7afeSIan Rogers } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) { 230970f7afeSIan Rogers assert($1.ids == NULL); 231970f7afeSIan Rogers assert($3.ids == NULL); 2323f965a7dSIan Rogers $$.val = $1.val / $3.val; 2333f965a7dSIan Rogers $$.ids = NULL; 2343f965a7dSIan Rogers } else { 235970f7afeSIan Rogers /* LHS and/or RHS need computing from event IDs so union. */ 2363f965a7dSIan Rogers $$ = union_expr($1, $3); 2373f965a7dSIan Rogers } 2389be27a5dSIan Rogers } 239c924e0ccSIan Rogers | expr '%' expr 240c924e0ccSIan Rogers { 2413f965a7dSIan Rogers if (fpclassify($3.val) == FP_ZERO) { 2429be27a5dSIan Rogers pr_debug("division by zero\n"); 2439be27a5dSIan Rogers YYABORT; 244970f7afeSIan Rogers } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) { 245970f7afeSIan Rogers assert($1.ids == NULL); 246970f7afeSIan Rogers assert($3.ids == NULL); 2473f965a7dSIan Rogers $$.val = (long)$1.val % (long)$3.val; 2483f965a7dSIan Rogers $$.ids = NULL; 2493f965a7dSIan Rogers } else { 250970f7afeSIan Rogers /* LHS and/or RHS need computing from event IDs so union. */ 2513f965a7dSIan Rogers $$ = union_expr($1, $3); 2523f965a7dSIan Rogers } 2539be27a5dSIan Rogers } 254c924e0ccSIan Rogers | D_RATIO '(' expr ',' expr ')' 255c924e0ccSIan Rogers { 2563f965a7dSIan Rogers if (fpclassify($5.val) == FP_ZERO) { 257970f7afeSIan Rogers /* 258970f7afeSIan Rogers * Division by constant zero always yields zero and no events 259970f7afeSIan Rogers * are necessary. 260970f7afeSIan Rogers */ 261970f7afeSIan Rogers assert($5.ids == NULL); 2623f965a7dSIan Rogers $$.val = 0.0; 263970f7afeSIan Rogers $$.ids = NULL; 264970f7afeSIan Rogers ids__free($3.ids); 265970f7afeSIan Rogers } else if (!compute_ids || (is_const($3.val) && is_const($5.val))) { 266970f7afeSIan Rogers assert($3.ids == NULL); 267970f7afeSIan Rogers assert($5.ids == NULL); 2683f965a7dSIan Rogers $$.val = $3.val / $5.val; 269970f7afeSIan Rogers $$.ids = NULL; 2703f965a7dSIan Rogers } else { 271970f7afeSIan Rogers /* LHS and/or RHS need computing from event IDs so union. */ 2723f965a7dSIan Rogers $$ = union_expr($3, $5); 2737f8fdcbbSIan Rogers } 2747f8fdcbbSIan Rogers } 275c924e0ccSIan Rogers | '-' expr %prec NEG 276c924e0ccSIan Rogers { 2773f965a7dSIan Rogers $$.val = -$2.val; 2783f965a7dSIan Rogers $$.ids = $2.ids; 279c924e0ccSIan Rogers } 280c924e0ccSIan Rogers | '(' if_expr ')' 281c924e0ccSIan Rogers { 282c924e0ccSIan Rogers $$ = $2; 283c924e0ccSIan Rogers } 284c924e0ccSIan Rogers | MIN '(' expr ',' expr ')' 285c924e0ccSIan Rogers { 2863f965a7dSIan Rogers if (!compute_ids) { 2873f965a7dSIan Rogers $$.val = $3.val < $5.val ? $3.val : $5.val; 2883f965a7dSIan Rogers $$.ids = NULL; 2893f965a7dSIan Rogers } else { 2903f965a7dSIan Rogers $$ = union_expr($3, $5); 2913f965a7dSIan Rogers } 292c924e0ccSIan Rogers } 293c924e0ccSIan Rogers | MAX '(' expr ',' expr ')' 294c924e0ccSIan Rogers { 2953f965a7dSIan Rogers if (!compute_ids) { 2963f965a7dSIan Rogers $$.val = $3.val > $5.val ? $3.val : $5.val; 2973f965a7dSIan Rogers $$.ids = NULL; 2983f965a7dSIan Rogers } else { 2993f965a7dSIan Rogers $$ = union_expr($3, $5); 3003f965a7dSIan Rogers } 301c924e0ccSIan Rogers } 3023613f6c1SIan Rogers | LITERAL 303c924e0ccSIan Rogers { 3043613f6c1SIan Rogers $$.val = $1; 3053f965a7dSIan Rogers $$.ids = NULL; 306c924e0ccSIan Rogers } 30707516736SAndi Kleen ; 30807516736SAndi Kleen 30907516736SAndi Kleen %% 310