^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) %option prefix="expr_"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) %option reentrant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) %option bison-bridge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) %{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "expr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "expr-bison.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) char *expr_get_text(yyscan_t yyscanner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) YYSTYPE *expr_get_lval(yyscan_t yyscanner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static double __value(YYSTYPE *yylval, char *str, int token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) double num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) num = strtod(str, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (errno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return EXPR_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) yylval->num = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int value(yyscan_t scanner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) YYSTYPE *yylval = expr_get_lval(scanner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) char *text = expr_get_text(scanner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return __value(yylval, text, NUMBER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Allow @ instead of / to be able to specify pmu/event/ without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * conflicts with normal division.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static char *normalize(char *str, int runtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) char *ret = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) char *dst = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) while (*str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (*str == '@')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *dst++ = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) else if (*str == '\\')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *dst++ = *++str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) else if (*str == '?') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) char *paramval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int size = asprintf(¶mval, "%d", runtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *dst++ = '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) while (i < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *dst++ = paramval[i++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) free(paramval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *dst++ = *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *dst = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int str(yyscan_t scanner, int token, int runtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) YYSTYPE *yylval = expr_get_lval(scanner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) char *text = expr_get_text(scanner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) yylval->str = normalize(strdup(text), runtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!yylval->str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return EXPR_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) yylval->str = normalize(yylval->str, runtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) %}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) number ([0-9]+\.?[0-9]*|[0-9]*\.?[0-9]+)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sch [-,=]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) spec \\{sch}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) sym [0-9a-zA-Z_\.:@?]+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) symbol ({spec}|{sym})+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) %%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int start_token = sctx->start_token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (sctx->start_token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sctx->start_token = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return start_token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) d_ratio { return D_RATIO; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) max { return MAX; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) min { return MIN; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if { return IF; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) else { return ELSE; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #smt_on { return SMT_ON; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {number} { return value(yyscanner); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {symbol} { return str(yyscanner, ID, sctx->runtime); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) "|" { return '|'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "^" { return '^'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "&" { return '&'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "<" { return '<'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ">" { return '>'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "-" { return '-'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) "+" { return '+'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) "*" { return '*'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "/" { return '/'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) "%" { return '%'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) "(" { return '('; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ")" { return ')'; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "," { return ','; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) . { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) %%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int expr_wrap(void *scanner __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }