Browse code

script engine: optimize op($v, ct) where ct=0 or 1

- optimize: $v*0->0, 0*$v->0, $v*1->$v 1*$v->$v
$v/1->$v, 0/$v->0
$v-0->$v
$v&0->0, 0&$v->0
$v|0->$v, 0|$v->$v
$v&&0->0, 0&&$v->0, $v&&1->$v, 1&&$v->$v
$v||1->1, 1||$v->1, $v||0->$v, 0||$v->$v
$v+0->$v, 0+$v->$v if typeof(expression) is int (forced
by the context)
- fix runtime && and ||:
0 && expr -> don't evaluate expr, return 0
1 || expr -> don't evaluate expr, return 1

Andrei Pelinescu-Onciul authored on 18/12/2008 11:51:50
Showing 1 changed files
... ...
@@ -1288,8 +1288,6 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1288 1288
 		case RVE_PLUS_OP:
1289 1289
 		case RVE_BOR_OP:
1290 1290
 		case RVE_BAND_OP:
1291
-		case RVE_LAND_OP:
1292
-		case RVE_LOR_OP:
1293 1291
 		case RVE_GT_OP:
1294 1292
 		case RVE_GTE_OP:
1295 1293
 		case RVE_LT_OP:
... ...
@@ -1302,6 +1300,34 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1302 1302
 				break;
1303 1303
 			ret=int_intop2(res, rve->op, i1, i2);
1304 1304
 			break;
1305
+		case RVE_LAND_OP:
1306
+			if (unlikely(
1307
+					(ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1308
+				break;
1309
+			if (i1==0){
1310
+				*res=0;
1311
+			}else{
1312
+				if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1313
+										rve->right.rve)) <0) )
1314
+					break;
1315
+				*res=i1 && i2;
1316
+			}
1317
+			ret=0;
1318
+			break;
1319
+		case RVE_LOR_OP:
1320
+			if (unlikely(
1321
+					(ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1322
+				break;
1323
+			if (i1){
1324
+				*res=1;
1325
+			}else{
1326
+				if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1327
+										rve->right.rve)) <0) )
1328
+					break;
1329
+				*res=i1 || i2;
1330
+			}
1331
+			ret=0;
1332
+			break;
1305 1333
 		case RVE_EQ_OP:
1306 1334
 		case RVE_DIFF_OP:
1307 1335
 			/* if left is string, eval left & right as string and
... ...
@@ -1593,6 +1619,32 @@ error:
1593 1593
 
1594 1594
 
1595 1595
 
1596
+/** evals a rval expr and always returns a new rval.
1597
+ * like rval_expr_eval, but always returns a new rvalue (never a reference
1598
+ * to an exisiting one).
1599
+ * WARNING: result must be rval_destroy()'ed if non-null (it might be
1600
+ * a reference to another rval). The result can be modified only
1601
+ * if rv_chg_in_place() returns true.
1602
+ * @result rvalue on success, 0 on error
1603
+ */
1604
+struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
1605
+								struct rval_expr* rve)
1606
+{
1607
+	struct rvalue* ret;
1608
+	struct rvalue* rv;
1609
+	
1610
+	ret=rval_expr_eval(h, msg, rve);
1611
+	if (ret && !rv_chg_in_place(ret)){
1612
+		rv=ret;
1613
+		/* create a new rv */
1614
+		ret=rval_new(rv->type, &rv->v, 0);
1615
+		rval_destroy(rv);
1616
+	}
1617
+	return ret;
1618
+}
1619
+
1620
+
1621
+
1596 1622
 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
1597 1623
  *
1598 1624
  * @param rv_type - rval type
... ...
@@ -1941,6 +1993,221 @@ static int rve_replace_with_ct_rv(struct rval_expr* rve, struct rvalue* rv)
1941 1941
 
1942 1942
 
1943 1943
 
1944
+/** optimize op($v, 0) or op($v, 1).
1945
+ * Note: internal use only from rve_optimize
1946
+ * It should be called after ct optimization, for non-contant
1947
+ *  expressions (the left or right side is not constant).
1948
+ * @return 1 on success (rve was changed), 0 on failure and -1 on error
1949
+ */
1950
+static int rve_opt_01(struct rval_expr* rve, enum rval_type rve_type)
1951
+{
1952
+	struct rvalue* rv;
1953
+	struct rval_expr* ct_rve;
1954
+	struct rval_expr* v_rve;
1955
+	int i;
1956
+	int ret;
1957
+	enum rval_expr_op op;
1958
+	int right; /* debugging msg */
1959
+	
1960
+	rv=0;
1961
+	ret=0;
1962
+	right=0;
1963
+	
1964
+	if (rve_is_constant(rve->right.rve)){
1965
+		ct_rve=rve->right.rve;
1966
+		v_rve=rve->left.rve;
1967
+		right=1;
1968
+	}else if (rve_is_constant(rve->left.rve)){
1969
+		ct_rve=rve->left.rve;
1970
+		v_rve=rve->right.rve;
1971
+		right=0;
1972
+	}else
1973
+		return 0; /* op($v, $w) */
1974
+	
1975
+	/* rval_expr_eval_new() instead of rval_expr_eval() to avoid
1976
+	   referencing a ct_rve->left.rval if ct_rve is a rval, which
1977
+	   would prevent rve_destroy(ct_rve) from working */
1978
+	if ((rv=rval_expr_eval_new(0, 0, ct_rve))==0){
1979
+		ERR("optimization failure, bad expression\n");
1980
+		goto error;
1981
+	}
1982
+	op=rve->op;
1983
+	if (rv->type==RV_INT){
1984
+		i=rv->v.l;
1985
+		switch(op){
1986
+			case RVE_MUL_OP:
1987
+				if (i==0){
1988
+					/* $v *  0 -> 0
1989
+					 *  0 * $v -> 0 */
1990
+					if (rve_replace_with_ct_rv(rve, rv)<0)
1991
+						goto error;
1992
+					ret=1;
1993
+				}else if (i==1){
1994
+					/* $v *  1 -> $v
1995
+					 *  1 * $v -> $v */
1996
+					rve_destroy(ct_rve);
1997
+					*rve=*v_rve; /* replace current expr. with $v */
1998
+					pkg_free(v_rve);/* rve_destroy(v_rve) would free
1999
+									   everything*/
2000
+					ret=1;
2001
+				}
2002
+				break;
2003
+			case RVE_DIV_OP:
2004
+				if (i==0){
2005
+					if (ct_rve==rve->left.rve){
2006
+						/* 0 / $v -> 0 */
2007
+						if (rve_replace_with_ct_rv(rve, rv)<0)
2008
+							goto error;
2009
+						ret=1;
2010
+					}else{
2011
+						/* $v / 0 */
2012
+						ERR("RVE divide by 0 at %d,%d\n",
2013
+								ct_rve->fpos.s_line, ct_rve->fpos.s_col);
2014
+					}
2015
+				}else if (i==1){
2016
+					if (ct_rve==rve->right.rve){
2017
+						/* $v / 1 -> $v */
2018
+						rve_destroy(ct_rve);
2019
+						*rve=*v_rve; /* replace current expr. with $v */
2020
+						pkg_free(v_rve);/* rve_destroy(v_rve) would free
2021
+										   everything*/
2022
+						ret=1;
2023
+					}
2024
+				}
2025
+				break;
2026
+			case RVE_MINUS_OP:
2027
+				if (i==0){
2028
+					if (ct_rve==rve->right.rve){
2029
+						/* $v - 0 -> $v */
2030
+						rve_destroy(ct_rve);
2031
+						*rve=*v_rve; /* replace current expr. with $v */
2032
+						pkg_free(v_rve);/* rve_destroy(v_rve) would free
2033
+										   everything*/
2034
+						ret=1;
2035
+					}
2036
+					/* ? 0 - $v -> -($v)  ? */
2037
+				}
2038
+				break;
2039
+			case RVE_BAND_OP:
2040
+				if (i==0){
2041
+					/* $v &  0 -> 0
2042
+					 *  0 & $v -> 0 */
2043
+					if (rve_replace_with_ct_rv(rve, rv)<0)
2044
+						goto error;
2045
+					ret=1;
2046
+				}
2047
+				/* no 0xffffff optimization for now (haven't decide on
2048
+				   the number of bits ) */
2049
+				break;
2050
+			case RVE_BOR_OP:
2051
+				if (i==0){
2052
+					/* $v |  0 -> $v
2053
+					 *  0 | $v -> $v */
2054
+					rve_destroy(ct_rve);
2055
+					*rve=*v_rve; /* replace current expr. with $v */
2056
+					pkg_free(v_rve);/* rve_destroy(v_rve) would free
2057
+									   everything*/
2058
+					ret=1;
2059
+				}
2060
+				break;
2061
+			case RVE_LAND_OP:
2062
+				if (i==0){
2063
+					/* $v &&  0 -> 0
2064
+					 *  0 && $v -> 0 */
2065
+					if (rve_replace_with_ct_rv(rve, rv)<0)
2066
+						goto error;
2067
+					ret=1;
2068
+				}else if (i==1){
2069
+					/* $v &&  1 -> $v
2070
+					 *  1 && $v -> $v */
2071
+					rve_destroy(ct_rve);
2072
+					*rve=*v_rve; /* replace current expr. with $v */
2073
+					pkg_free(v_rve);/* rve_destroy(v_rve) would free
2074
+									   everything*/
2075
+					ret=1;
2076
+				}
2077
+				break;
2078
+			case RVE_LOR_OP:
2079
+				if (i==1){
2080
+					/* $v ||  1 -> 1
2081
+					 *  1 || $v -> 1 */
2082
+					if (rve_replace_with_ct_rv(rve, rv)<0)
2083
+						goto error;
2084
+					ret=1;
2085
+				}else if (i==0){
2086
+					/* $v ||  0 -> $v
2087
+					 *  0 && $v -> $v */
2088
+					rve_destroy(ct_rve);
2089
+					*rve=*v_rve; /* replace current expr. with $v */
2090
+					pkg_free(v_rve);/* rve_destroy(v_rve) would free
2091
+									   everything*/
2092
+					ret=1;
2093
+				}
2094
+				break;
2095
+			case RVE_PLUS_OP:
2096
+				/* we must make sure that this is an int PLUS
2097
+				   (because "foo"+0 is valid => "foo0") */
2098
+				if ((i==0) && (rve_type==RV_INT)){
2099
+					/* $v +  0 -> $v
2100
+					 *  0 + $v -> $v */
2101
+					rve_destroy(ct_rve);
2102
+					*rve=*v_rve; /* replace current expr. with $v */
2103
+					pkg_free(v_rve);/* rve_destroy(v_rve) would free
2104
+									   everything*/
2105
+					ret=1;
2106
+				}
2107
+				break;
2108
+			default:
2109
+				/* do nothing */
2110
+				break;
2111
+		}
2112
+		/* debugging messages */
2113
+		if (ret==1){
2114
+			if (right){
2115
+				if ((rve->op==RVE_RVAL_OP) && (rve->left.rval.type==RV_INT))
2116
+					DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
2117
+							" op%d($v, %d) -> %d\n", 
2118
+							rve->fpos.s_line, rve->fpos.s_col,
2119
+							rve->fpos.e_line, rve->fpos.s_col,
2120
+							op, i, (int)rve->left.rval.v.l);
2121
+				else
2122
+					DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
2123
+							" op%d($v, %d) -> $v\n",
2124
+							rve->fpos.s_line, rve->fpos.s_col,
2125
+							rve->fpos.e_line, rve->fpos.s_col,
2126
+							op, i);
2127
+			}else{
2128
+				if ((rve->op==RVE_RVAL_OP) && (rve->left.rval.type==RV_INT))
2129
+					DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
2130
+							" op%d(%d, $v) -> %d\n", 
2131
+							rve->fpos.s_line, rve->fpos.s_col,
2132
+							rve->fpos.e_line, rve->fpos.s_col,
2133
+							op, i, (int)rve->left.rval.v.l);
2134
+				else
2135
+					DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
2136
+							" op%d(%d, $v) -> $v\n",
2137
+							rve->fpos.s_line, rve->fpos.s_col,
2138
+							rve->fpos.e_line, rve->fpos.s_col,
2139
+							op, i);
2140
+			}
2141
+		}
2142
+	}
2143
+	/* no optimization for strings for now
2144
+	   (We could optimize $v + "" or ""+$v, but this ""+$v is a way
2145
+	    to force convert $v to str , it might mess up type checking
2146
+	    (e.g. errors w/o optimization and no errors with) and it brings
2147
+	    a very small benefit anyway (it's unlikely we'll see a lot of
2148
+	    "")
2149
+	*/
2150
+	if (rv) rval_destroy(rv);
2151
+	return ret;
2152
+error:
2153
+	if (rv) rval_destroy(rv);
2154
+	return -1;
2155
+}
2156
+
2157
+
2158
+
1944 2159
 /** tries to optimize a rval_expr. */
1945 2160
 static int rve_optimize(struct rval_expr* rve)
1946 2161
 {
... ...
@@ -1987,8 +2254,9 @@ static int rve_optimize(struct rval_expr* rve)
1987 1987
 		rve_optimize(rve->left.rve);
1988 1988
 		rve_optimize(rve->right.rve);
1989 1989
 		if (!rve_check_type(&type, rve, &bad_rve, &bad_type, &exp_type)){
1990
-			ERR("optimization failure, type mismatch in expression, "
1990
+			ERR("optimization failure, type mismatch in expression (%d,%d), "
1991 1991
 					"type %s, but expected %s\n",
1992
+					bad_rve->fpos.s_line, bad_rve->fpos.s_col,
1992 1993
 					rval_type_name(bad_type), rval_type_name(exp_type));
1993 1994
 			return 0;
1994 1995
 		}
... ...
@@ -2010,7 +2278,14 @@ static int rve_optimize(struct rval_expr* rve)
2010 2010
 			rv=0;
2011 2011
 		}
2012 2012
 		
2013
-		/* TODO: $v * 0 => 0; $v * 1 => $v (for *, /, &, |, &&, ||, +, -) */
2013
+		/* $v * 0 => 0; $v * 1 => $v (for *, /, &, |, &&, ||, +, -) */
2014
+		if (rve_opt_01(rve, type)==1){
2015
+			/* success, rve was changed => return now
2016
+			  (since this is recursively invoked the "new" rve
2017
+			   is already optimized) */
2018
+			ret=1;
2019
+			goto end;
2020
+		}
2014 2021
 		
2015 2022
 		/* op(op($v, a), b) => op($v, op(a,b)) */
2016 2023
 		if (rve_is_constant(rve->right.rve)){
... ...
@@ -2170,6 +2445,7 @@ static int rve_optimize(struct rval_expr* rve)
2170 2170
 		}
2171 2171
 		/* op(op($v,a), op($w,b)) => no optimizations for now (TODO) */
2172 2172
 	}
2173
+end:
2173 2174
 	return ret;
2174 2175
 error:
2175 2176
 	if (rv) rval_destroy(rv);