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" 10ddc8e4c9SIan Rogers #include "expr-bison.h" 11ddc8e4c9SIan Rogers int expr_lex(YYSTYPE * yylval_param , void *yyscanner); 1207516736SAndi Kleen %} 1307516736SAndi Kleen 14fc8c0a99SJiri Olsa %define api.pure full 15fc8c0a99SJiri Olsa 1607516736SAndi Kleen %parse-param { double *final_val } 17aecce63eSJiri Olsa %parse-param { struct expr_parse_ctx *ctx } 183f965a7dSIan Rogers %parse-param { bool compute_ids } 1926226a97SJiri Olsa %parse-param {void *scanner} 2026226a97SJiri Olsa %lex-param {void* scanner} 2107516736SAndi Kleen 2207516736SAndi Kleen %union { 2307516736SAndi Kleen double num; 2426226a97SJiri Olsa char *str; 253f965a7dSIan Rogers struct ids { 263f965a7dSIan Rogers /* 273f965a7dSIan Rogers * When creating ids, holds the working set of event ids. NULL 283f965a7dSIan Rogers * implies the set is empty. 293f965a7dSIan Rogers */ 303f965a7dSIan Rogers struct hashmap *ids; 313f965a7dSIan Rogers /* 323f965a7dSIan Rogers * The metric value. When not creating ids this is the value 333f965a7dSIan Rogers * read from a counter, a constant or some computed value. When 343f965a7dSIan Rogers * creating ids the value is either a constant or BOTTOM. NAN is 353f965a7dSIan Rogers * used as the special BOTTOM value, representing a "set of all 363f965a7dSIan Rogers * values" case. 373f965a7dSIan Rogers */ 383f965a7dSIan Rogers double val; 393f965a7dSIan Rogers } ids; 4007516736SAndi Kleen } 4107516736SAndi Kleen 42*9d5da30eSJames Clark %token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT HAS_EVENT STRCMP_CPUID_STR EXPR_ERROR 43d73bad06SAndi Kleen %left MIN MAX IF 4407516736SAndi Kleen %left '|' 4507516736SAndi Kleen %left '^' 4607516736SAndi Kleen %left '&' 47ff1a12f9SIan Rogers %left '<' '>' 4807516736SAndi Kleen %left '-' '+' 4907516736SAndi Kleen %left '*' '/' '%' 5007516736SAndi Kleen %left NEG NOT 513613f6c1SIan Rogers %type <num> NUMBER LITERAL 52aed0d6f8SIan Rogers %type <str> ID 53aed0d6f8SIan Rogers %destructor { free ($$); } <str> 543f965a7dSIan Rogers %type <ids> expr if_expr 553f965a7dSIan Rogers %destructor { ids__free($$.ids); } <ids> 5607516736SAndi Kleen 5707516736SAndi Kleen %{ 5826226a97SJiri Olsa static void expr_error(double *final_val __maybe_unused, 59aecce63eSJiri Olsa struct expr_parse_ctx *ctx __maybe_unused, 603f965a7dSIan Rogers bool compute_ids __maybe_unused, 61ddc8e4c9SIan Rogers void *scanner __maybe_unused, 6207516736SAndi Kleen const char *s) 6307516736SAndi Kleen { 6407516736SAndi Kleen pr_debug("%s\n", s); 6507516736SAndi Kleen } 6607516736SAndi Kleen 673f965a7dSIan Rogers /* 683f965a7dSIan Rogers * During compute ids, the special "bottom" value uses NAN to represent the set 693f965a7dSIan Rogers * of all values. NAN is selected as it isn't a useful constant value. 703f965a7dSIan Rogers */ 713f965a7dSIan Rogers #define BOTTOM NAN 723f965a7dSIan Rogers 73970f7afeSIan Rogers /* During computing ids, does val represent a constant (non-BOTTOM) value? */ 74970f7afeSIan Rogers static bool is_const(double val) 75970f7afeSIan Rogers { 76970f7afeSIan Rogers return isfinite(val); 77970f7afeSIan Rogers } 78970f7afeSIan Rogers 793f965a7dSIan Rogers static struct ids union_expr(struct ids ids1, struct ids ids2) 803f965a7dSIan Rogers { 813f965a7dSIan Rogers struct ids result = { 823f965a7dSIan Rogers .val = BOTTOM, 833f965a7dSIan Rogers .ids = ids__union(ids1.ids, ids2.ids), 843f965a7dSIan Rogers }; 853f965a7dSIan Rogers return result; 863f965a7dSIan Rogers } 873f965a7dSIan Rogers 881e7ab829SIan Rogers static struct ids handle_id(struct expr_parse_ctx *ctx, char *id, 899aba0adaSIan Rogers bool compute_ids, bool source_count) 901e7ab829SIan Rogers { 911e7ab829SIan Rogers struct ids result; 921e7ab829SIan Rogers 931e7ab829SIan Rogers if (!compute_ids) { 941e7ab829SIan Rogers /* 951e7ab829SIan Rogers * Compute the event's value from ID. If the ID isn't known then 961e7ab829SIan Rogers * it isn't used to compute the formula so set to NAN. 971e7ab829SIan Rogers */ 981e7ab829SIan Rogers struct expr_id_data *data; 991e7ab829SIan Rogers 1001e7ab829SIan Rogers result.val = NAN; 1019aba0adaSIan Rogers if (expr__resolve_id(ctx, id, &data) == 0) { 1029aba0adaSIan Rogers result.val = source_count 1039aba0adaSIan Rogers ? expr_id_data__source_count(data) 1049aba0adaSIan Rogers : expr_id_data__value(data); 1059aba0adaSIan Rogers } 1061e7ab829SIan Rogers result.ids = NULL; 1071e7ab829SIan Rogers free(id); 1081e7ab829SIan Rogers } else { 1091e7ab829SIan Rogers /* 1101e7ab829SIan Rogers * Set the value to BOTTOM to show that any value is possible 1111e7ab829SIan Rogers * when the event is computed. Create a set of just the ID. 1121e7ab829SIan Rogers */ 1131e7ab829SIan Rogers result.val = BOTTOM; 1141e7ab829SIan Rogers result.ids = ids__new(); 1151e7ab829SIan Rogers if (!result.ids || ids__insert(result.ids, id)) { 1161e7ab829SIan Rogers pr_err("Error creating IDs for '%s'", id); 1171e7ab829SIan Rogers free(id); 1181e7ab829SIan Rogers } 1191e7ab829SIan Rogers } 1201e7ab829SIan Rogers return result; 1211e7ab829SIan Rogers } 1221e7ab829SIan Rogers 123970f7afeSIan Rogers /* 124970f7afeSIan Rogers * If we're not computing ids or $1 and $3 are constants, compute the new 125970f7afeSIan Rogers * constant value using OP. Its invariant that there are no ids. If computing 126970f7afeSIan Rogers * ids for non-constants union the set of IDs that must be computed. 127970f7afeSIan Rogers */ 128e87576c5SIan Rogers #define BINARY_OP(RESULT, OP, LHS, RHS) \ 129970f7afeSIan Rogers if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \ 130970f7afeSIan Rogers assert(LHS.ids == NULL); \ 131970f7afeSIan Rogers assert(RHS.ids == NULL); \ 13279802934SIan Rogers if (isnan(LHS.val) || isnan(RHS.val)) { \ 13379802934SIan Rogers RESULT.val = NAN; \ 13479802934SIan Rogers } else { \ 1353f965a7dSIan Rogers RESULT.val = LHS.val OP RHS.val; \ 13679802934SIan Rogers } \ 1373f965a7dSIan Rogers RESULT.ids = NULL; \ 1383f965a7dSIan Rogers } else { \ 1393f965a7dSIan Rogers RESULT = union_expr(LHS, RHS); \ 1403f965a7dSIan Rogers } 141e87576c5SIan Rogers 14207516736SAndi Kleen %} 14307516736SAndi Kleen %% 14407516736SAndi Kleen 1453f965a7dSIan Rogers start: if_expr 14626226a97SJiri Olsa { 1473f965a7dSIan Rogers if (compute_ids) 1483f965a7dSIan Rogers ctx->ids = ids__union($1.ids, ctx->ids); 14926226a97SJiri Olsa 1503f965a7dSIan Rogers if (final_val) 1513f965a7dSIan Rogers *final_val = $1.val; 1523f965a7dSIan Rogers } 1533f965a7dSIan Rogers ; 154d73bad06SAndi Kleen 1554b65fc7bSIan Rogers if_expr: expr IF expr ELSE if_expr 156c924e0ccSIan Rogers { 1573f965a7dSIan Rogers if (fpclassify($3.val) == FP_ZERO) { 158a8e4e880SIan Rogers /* 159a8e4e880SIan Rogers * The IF expression evaluated to 0 so treat as false, take the 160a8e4e880SIan Rogers * ELSE and discard everything else. 161a8e4e880SIan Rogers */ 1623f965a7dSIan Rogers $$.val = $5.val; 163a8e4e880SIan Rogers $$.ids = $5.ids; 164a8e4e880SIan Rogers ids__free($1.ids); 165a8e4e880SIan Rogers ids__free($3.ids); 166a8e4e880SIan Rogers } else if (!compute_ids || is_const($3.val)) { 167a8e4e880SIan Rogers /* 168a8e4e880SIan Rogers * If ids aren't computed then treat the expression as true. If 169a8e4e880SIan Rogers * ids are being computed and the IF expr is a non-zero 170a8e4e880SIan Rogers * constant, then also evaluate the true case. 171a8e4e880SIan Rogers */ 1723f965a7dSIan Rogers $$.val = $1.val; 173a8e4e880SIan Rogers $$.ids = $1.ids; 174a8e4e880SIan Rogers ids__free($3.ids); 175a8e4e880SIan Rogers ids__free($5.ids); 17694886961SIan Rogers } else if ($1.val == $5.val) { 17794886961SIan Rogers /* 17894886961SIan Rogers * LHS == RHS, so both are an identical constant. No need to 17994886961SIan Rogers * evaluate any events. 18094886961SIan Rogers */ 18194886961SIan Rogers $$.val = $1.val; 18294886961SIan Rogers $$.ids = NULL; 18394886961SIan Rogers ids__free($1.ids); 18494886961SIan Rogers ids__free($3.ids); 18594886961SIan Rogers ids__free($5.ids); 1863f965a7dSIan Rogers } else { 187a8e4e880SIan Rogers /* 188a8e4e880SIan Rogers * Value is either the LHS or RHS and we need the IF expression 189a8e4e880SIan Rogers * to compute it. 190a8e4e880SIan Rogers */ 1913f965a7dSIan Rogers $$ = union_expr($1, union_expr($3, $5)); 1923f965a7dSIan Rogers } 193c924e0ccSIan Rogers } 194d73bad06SAndi Kleen | expr 19507516736SAndi Kleen ; 19607516736SAndi Kleen 19707516736SAndi Kleen expr: NUMBER 198c924e0ccSIan Rogers { 1993f965a7dSIan Rogers $$.val = $1; 2003f965a7dSIan Rogers $$.ids = NULL; 201c924e0ccSIan Rogers } 2029aba0adaSIan Rogers | ID { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); } 2039aba0adaSIan Rogers | SOURCE_COUNT '(' ID ')' { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); } 2044a4a9bf9SIan Rogers | HAS_EVENT '(' ID ')' 2054a4a9bf9SIan Rogers { 2064a4a9bf9SIan Rogers $$.val = expr__has_event(ctx, compute_ids, $3); 2074a4a9bf9SIan Rogers $$.ids = NULL; 2084a4a9bf9SIan Rogers free($3); 2094a4a9bf9SIan Rogers } 210*9d5da30eSJames Clark | STRCMP_CPUID_STR '(' ID ')' 211*9d5da30eSJames Clark { 212*9d5da30eSJames Clark $$.val = expr__strcmp_cpuid_str(ctx, compute_ids, $3); 213*9d5da30eSJames Clark $$.ids = NULL; 214*9d5da30eSJames Clark free($3); 215*9d5da30eSJames Clark } 2166f765bbbSIan Rogers | expr '|' expr 2176f765bbbSIan Rogers { 2186f765bbbSIan Rogers if (is_const($1.val) && is_const($3.val)) { 2196f765bbbSIan Rogers assert($1.ids == NULL); 2206f765bbbSIan Rogers assert($3.ids == NULL); 2216f765bbbSIan Rogers $$.ids = NULL; 2226f765bbbSIan Rogers $$.val = (fpclassify($1.val) == FP_ZERO && fpclassify($3.val) == FP_ZERO) ? 0 : 1; 2236f765bbbSIan Rogers } else if (is_const($1.val)) { 2246f765bbbSIan Rogers assert($1.ids == NULL); 2256f765bbbSIan Rogers if (fpclassify($1.val) == FP_ZERO) { 2266f765bbbSIan Rogers $$ = $3; 2276f765bbbSIan Rogers } else { 2286f765bbbSIan Rogers $$.val = 1; 2296f765bbbSIan Rogers $$.ids = NULL; 2306f765bbbSIan Rogers ids__free($3.ids); 2316f765bbbSIan Rogers } 2326f765bbbSIan Rogers } else if (is_const($3.val)) { 2336f765bbbSIan Rogers assert($3.ids == NULL); 2346f765bbbSIan Rogers if (fpclassify($3.val) == FP_ZERO) { 2356f765bbbSIan Rogers $$ = $1; 2366f765bbbSIan Rogers } else { 2376f765bbbSIan Rogers $$.val = 1; 2386f765bbbSIan Rogers $$.ids = NULL; 2396f765bbbSIan Rogers ids__free($1.ids); 2406f765bbbSIan Rogers } 2416f765bbbSIan Rogers } else { 2426f765bbbSIan Rogers $$ = union_expr($1, $3); 2436f765bbbSIan Rogers } 2446f765bbbSIan Rogers } 2456f765bbbSIan Rogers | expr '&' expr 2466f765bbbSIan Rogers { 2476f765bbbSIan Rogers if (is_const($1.val) && is_const($3.val)) { 2486f765bbbSIan Rogers assert($1.ids == NULL); 2496f765bbbSIan Rogers assert($3.ids == NULL); 2506f765bbbSIan Rogers $$.val = (fpclassify($1.val) != FP_ZERO && fpclassify($3.val) != FP_ZERO) ? 1 : 0; 2516f765bbbSIan Rogers $$.ids = NULL; 2526f765bbbSIan Rogers } else if (is_const($1.val)) { 2536f765bbbSIan Rogers assert($1.ids == NULL); 2546f765bbbSIan Rogers if (fpclassify($1.val) != FP_ZERO) { 2556f765bbbSIan Rogers $$ = $3; 2566f765bbbSIan Rogers } else { 2576f765bbbSIan Rogers $$.val = 0; 2586f765bbbSIan Rogers $$.ids = NULL; 2596f765bbbSIan Rogers ids__free($3.ids); 2606f765bbbSIan Rogers } 2616f765bbbSIan Rogers } else if (is_const($3.val)) { 2626f765bbbSIan Rogers assert($3.ids == NULL); 2636f765bbbSIan Rogers if (fpclassify($3.val) != FP_ZERO) { 2646f765bbbSIan Rogers $$ = $1; 2656f765bbbSIan Rogers } else { 2666f765bbbSIan Rogers $$.val = 0; 2676f765bbbSIan Rogers $$.ids = NULL; 2686f765bbbSIan Rogers ids__free($1.ids); 2696f765bbbSIan Rogers } 2706f765bbbSIan Rogers } else { 2716f765bbbSIan Rogers $$ = union_expr($1, $3); 2726f765bbbSIan Rogers } 2736f765bbbSIan Rogers } 2746f765bbbSIan Rogers | expr '^' expr 2756f765bbbSIan Rogers { 2766f765bbbSIan Rogers if (is_const($1.val) && is_const($3.val)) { 2776f765bbbSIan Rogers assert($1.ids == NULL); 2786f765bbbSIan Rogers assert($3.ids == NULL); 2796f765bbbSIan Rogers $$.val = (fpclassify($1.val) == FP_ZERO) != (fpclassify($3.val) == FP_ZERO) ? 1 : 0; 2806f765bbbSIan Rogers $$.ids = NULL; 2816f765bbbSIan Rogers } else { 2826f765bbbSIan Rogers $$ = union_expr($1, $3); 2836f765bbbSIan Rogers } 2846f765bbbSIan Rogers } 285e87576c5SIan Rogers | expr '<' expr { BINARY_OP($$, <, $1, $3); } 286e87576c5SIan Rogers | expr '>' expr { BINARY_OP($$, >, $1, $3); } 287e87576c5SIan Rogers | expr '+' expr { BINARY_OP($$, +, $1, $3); } 288e87576c5SIan Rogers | expr '-' expr { BINARY_OP($$, -, $1, $3); } 289e87576c5SIan Rogers | expr '*' expr { BINARY_OP($$, *, $1, $3); } 290c924e0ccSIan Rogers | expr '/' expr 291c924e0ccSIan Rogers { 2923f965a7dSIan Rogers if (fpclassify($3.val) == FP_ZERO) { 2939be27a5dSIan Rogers pr_debug("division by zero\n"); 2942a939c86SIan Rogers assert($3.ids == NULL); 2952a939c86SIan Rogers if (compute_ids) 2962a939c86SIan Rogers ids__free($1.ids); 2972a939c86SIan Rogers $$.val = NAN; 2982a939c86SIan Rogers $$.ids = NULL; 299970f7afeSIan Rogers } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) { 300970f7afeSIan Rogers assert($1.ids == NULL); 301970f7afeSIan Rogers assert($3.ids == NULL); 3023f965a7dSIan Rogers $$.val = $1.val / $3.val; 3033f965a7dSIan Rogers $$.ids = NULL; 3043f965a7dSIan Rogers } else { 305970f7afeSIan Rogers /* LHS and/or RHS need computing from event IDs so union. */ 3063f965a7dSIan Rogers $$ = union_expr($1, $3); 3073f965a7dSIan Rogers } 3089be27a5dSIan Rogers } 309c924e0ccSIan Rogers | expr '%' expr 310c924e0ccSIan Rogers { 3113f965a7dSIan Rogers if (fpclassify($3.val) == FP_ZERO) { 3129be27a5dSIan Rogers pr_debug("division by zero\n"); 3139be27a5dSIan Rogers YYABORT; 314970f7afeSIan Rogers } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) { 315970f7afeSIan Rogers assert($1.ids == NULL); 316970f7afeSIan Rogers assert($3.ids == NULL); 3173f965a7dSIan Rogers $$.val = (long)$1.val % (long)$3.val; 3183f965a7dSIan Rogers $$.ids = NULL; 3193f965a7dSIan Rogers } else { 320970f7afeSIan Rogers /* LHS and/or RHS need computing from event IDs so union. */ 3213f965a7dSIan Rogers $$ = union_expr($1, $3); 3223f965a7dSIan Rogers } 3239be27a5dSIan Rogers } 324c924e0ccSIan Rogers | D_RATIO '(' expr ',' expr ')' 325c924e0ccSIan Rogers { 3263f965a7dSIan Rogers if (fpclassify($5.val) == FP_ZERO) { 327970f7afeSIan Rogers /* 328970f7afeSIan Rogers * Division by constant zero always yields zero and no events 329970f7afeSIan Rogers * are necessary. 330970f7afeSIan Rogers */ 331970f7afeSIan Rogers assert($5.ids == NULL); 3323f965a7dSIan Rogers $$.val = 0.0; 333970f7afeSIan Rogers $$.ids = NULL; 334970f7afeSIan Rogers ids__free($3.ids); 335970f7afeSIan Rogers } else if (!compute_ids || (is_const($3.val) && is_const($5.val))) { 336970f7afeSIan Rogers assert($3.ids == NULL); 337970f7afeSIan Rogers assert($5.ids == NULL); 3383f965a7dSIan Rogers $$.val = $3.val / $5.val; 339970f7afeSIan Rogers $$.ids = NULL; 3403f965a7dSIan Rogers } else { 341970f7afeSIan Rogers /* LHS and/or RHS need computing from event IDs so union. */ 3423f965a7dSIan Rogers $$ = union_expr($3, $5); 3437f8fdcbbSIan Rogers } 3447f8fdcbbSIan Rogers } 345c924e0ccSIan Rogers | '-' expr %prec NEG 346c924e0ccSIan Rogers { 3473f965a7dSIan Rogers $$.val = -$2.val; 3483f965a7dSIan Rogers $$.ids = $2.ids; 349c924e0ccSIan Rogers } 350c924e0ccSIan Rogers | '(' if_expr ')' 351c924e0ccSIan Rogers { 352c924e0ccSIan Rogers $$ = $2; 353c924e0ccSIan Rogers } 354c924e0ccSIan Rogers | MIN '(' expr ',' expr ')' 355c924e0ccSIan Rogers { 3563f965a7dSIan Rogers if (!compute_ids) { 3573f965a7dSIan Rogers $$.val = $3.val < $5.val ? $3.val : $5.val; 3583f965a7dSIan Rogers $$.ids = NULL; 3593f965a7dSIan Rogers } else { 3603f965a7dSIan Rogers $$ = union_expr($3, $5); 3613f965a7dSIan Rogers } 362c924e0ccSIan Rogers } 363c924e0ccSIan Rogers | MAX '(' expr ',' expr ')' 364c924e0ccSIan Rogers { 3653f965a7dSIan Rogers if (!compute_ids) { 3663f965a7dSIan Rogers $$.val = $3.val > $5.val ? $3.val : $5.val; 3673f965a7dSIan Rogers $$.ids = NULL; 3683f965a7dSIan Rogers } else { 3693f965a7dSIan Rogers $$ = union_expr($3, $5); 3703f965a7dSIan Rogers } 371c924e0ccSIan Rogers } 3723613f6c1SIan Rogers | LITERAL 373c924e0ccSIan Rogers { 3743613f6c1SIan Rogers $$.val = $1; 3753f965a7dSIan Rogers $$.ids = NULL; 376c924e0ccSIan Rogers } 37707516736SAndi Kleen ; 37807516736SAndi Kleen 37907516736SAndi Kleen %% 380