xref: /openbmc/linux/tools/perf/util/expr.y (revision edfe7f554ab8f083556c718ecbcefda509c46851)
107516736SAndi Kleen /* Simple expression parser */
207516736SAndi Kleen %{
326226a97SJiri Olsa #define YYDEBUG 1
4*edfe7f55SIan Rogers #include <math.h>
526226a97SJiri Olsa #include <stdio.h>
607516736SAndi Kleen #include "util.h"
707516736SAndi Kleen #include "util/debug.h"
88520a98dSArnaldo Carvalho de Melo #include <stdlib.h> // strtod()
907516736SAndi Kleen #define IN_EXPR_Y 1
1007516736SAndi Kleen #include "expr.h"
11d73bad06SAndi Kleen #include "smt.h"
1207516736SAndi Kleen #include <string.h>
1307516736SAndi Kleen 
143e21a28aSIan Rogers static double d_ratio(double val0, double val1)
153e21a28aSIan Rogers {
163e21a28aSIan Rogers 	if (val1 == 0) {
173e21a28aSIan Rogers 		return 0;
183e21a28aSIan Rogers 	}
193e21a28aSIan Rogers 	return  val0 / val1;
203e21a28aSIan Rogers }
213e21a28aSIan Rogers 
2207516736SAndi Kleen %}
2307516736SAndi Kleen 
24fc8c0a99SJiri Olsa %define api.pure full
25fc8c0a99SJiri Olsa 
2607516736SAndi Kleen %parse-param { double *final_val }
27aecce63eSJiri Olsa %parse-param { struct expr_parse_ctx *ctx }
2826226a97SJiri Olsa %parse-param {void *scanner}
2926226a97SJiri Olsa %lex-param {void* scanner}
3007516736SAndi Kleen 
3107516736SAndi Kleen %union {
3207516736SAndi Kleen 	double	 num;
3326226a97SJiri Olsa 	char	*str;
3407516736SAndi Kleen }
3507516736SAndi Kleen 
3626226a97SJiri Olsa %token EXPR_PARSE EXPR_OTHER EXPR_ERROR
3707516736SAndi Kleen %token <num> NUMBER
3826226a97SJiri Olsa %token <str> ID
3963657578SIan Rogers %destructor { free ($$); } <str>
403e21a28aSIan Rogers %token MIN MAX IF ELSE SMT_ON D_RATIO
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
49d73bad06SAndi Kleen %type <num> expr if_expr
5007516736SAndi Kleen 
5107516736SAndi Kleen %{
5226226a97SJiri Olsa static void expr_error(double *final_val __maybe_unused,
53aecce63eSJiri Olsa 		       struct expr_parse_ctx *ctx __maybe_unused,
5426226a97SJiri Olsa 		       void *scanner,
5507516736SAndi Kleen 		       const char *s)
5607516736SAndi Kleen {
5707516736SAndi Kleen 	pr_debug("%s\n", s);
5807516736SAndi Kleen }
5907516736SAndi Kleen 
6007516736SAndi Kleen %}
6107516736SAndi Kleen %%
6207516736SAndi Kleen 
6326226a97SJiri Olsa start:
6426226a97SJiri Olsa EXPR_PARSE all_expr
6526226a97SJiri Olsa |
6626226a97SJiri Olsa EXPR_OTHER all_other
6726226a97SJiri Olsa 
6826226a97SJiri Olsa all_other: all_other other
6926226a97SJiri Olsa |
7026226a97SJiri Olsa 
7126226a97SJiri Olsa other: ID
7226226a97SJiri Olsa {
73332603c2SJiri Olsa 	expr__add_id(ctx, $1);
7426226a97SJiri Olsa }
7526226a97SJiri Olsa |
76cb59fa79SIan Rogers MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
773e21a28aSIan Rogers |
78ff1a12f9SIan Rogers '<' | '>' | D_RATIO
7926226a97SJiri Olsa 
80d73bad06SAndi Kleen all_expr: if_expr			{ *final_val = $1; }
81d73bad06SAndi Kleen 	;
82d73bad06SAndi Kleen 
83d73bad06SAndi Kleen if_expr:
84d73bad06SAndi Kleen 	expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
85d73bad06SAndi Kleen 	| expr
8607516736SAndi Kleen 	;
8707516736SAndi Kleen 
8807516736SAndi Kleen expr:	  NUMBER
895c5f5e83SJiri Olsa 	| ID			{
905c5f5e83SJiri Olsa 					struct expr_id_data *data;
915c5f5e83SJiri Olsa 
92*edfe7f55SIan Rogers 					$$ = NAN;
93*edfe7f55SIan Rogers 					if (expr__resolve_id(ctx, $1, &data) == 0)
9429396cd5SIan Rogers 						$$ = expr_id_data__value(data);
95*edfe7f55SIan Rogers 
9663657578SIan Rogers 					free($1);
9707516736SAndi Kleen 				}
98d73bad06SAndi Kleen 	| expr '|' expr		{ $$ = (long)$1 | (long)$3; }
99d73bad06SAndi Kleen 	| expr '&' expr		{ $$ = (long)$1 & (long)$3; }
100d73bad06SAndi Kleen 	| expr '^' expr		{ $$ = (long)$1 ^ (long)$3; }
101ff1a12f9SIan Rogers 	| expr '<' expr		{ $$ = $1 < $3; }
102ff1a12f9SIan Rogers 	| expr '>' expr		{ $$ = $1 > $3; }
10307516736SAndi Kleen 	| expr '+' expr		{ $$ = $1 + $3; }
10407516736SAndi Kleen 	| expr '-' expr		{ $$ = $1 - $3; }
10507516736SAndi Kleen 	| expr '*' expr		{ $$ = $1 * $3; }
1069be27a5dSIan Rogers 	| expr '/' expr		{ if ($3 == 0) {
1079be27a5dSIan Rogers 					pr_debug("division by zero\n");
1089be27a5dSIan Rogers 					YYABORT;
1099be27a5dSIan Rogers 				  }
1109be27a5dSIan Rogers 				  $$ = $1 / $3;
1119be27a5dSIan Rogers 	                        }
1129be27a5dSIan Rogers 	| expr '%' expr		{ if ((long)$3 == 0) {
1139be27a5dSIan Rogers 					pr_debug("division by zero\n");
1149be27a5dSIan Rogers 					YYABORT;
1159be27a5dSIan Rogers 				  }
1169be27a5dSIan Rogers 				  $$ = (long)$1 % (long)$3;
1179be27a5dSIan Rogers 	                        }
11807516736SAndi Kleen 	| '-' expr %prec NEG	{ $$ = -$2; }
119d73bad06SAndi Kleen 	| '(' if_expr ')'	{ $$ = $2; }
120d73bad06SAndi Kleen 	| MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
121d73bad06SAndi Kleen 	| MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
122d73bad06SAndi Kleen 	| SMT_ON		 { $$ = smt_on() > 0; }
1233e21a28aSIan Rogers 	| D_RATIO '(' expr ',' expr ')' { $$ = d_ratio($3,$5); }
12407516736SAndi Kleen 	;
12507516736SAndi Kleen 
12607516736SAndi Kleen %%
127