Browse code

script engine: different operators for int and str +

- different operators for integer + (RVE_IPLUS_OP) and str plus
(RVE_CONCAT_OP). The generic plus (RVE_PLUS_OP) is still
present, the new operators are only used in internal
optimizations (see below) for now.
- if an expression involving the generic '+' is always of type int
or str, replace the generic '+' with the interger or string
version (makes further optimizations possible).

Andrei Pelinescu-Onciul authored on 18/12/2008 13:46:05
Showing 2 changed files
... ...
@@ -385,10 +385,13 @@ enum rval_type rve_guess_type( struct rval_expr* rve)
385 385
 		case RVE_LTE_OP:
386 386
 		case RVE_EQ_OP:
387 387
 		case RVE_DIFF_OP:
388
+		case RVE_IPLUS_OP:
388 389
 			return RV_INT;
389 390
 		case RVE_PLUS_OP:
390 391
 			/* '+' evaluates to the type of the left operand */
391 392
 			return rve_guess_type(rve->left.rve);
393
+		case RVE_CONCAT_OP:
394
+			return RV_STR;
392 395
 		case RVE_NONE_OP:
393 396
 			break;
394 397
 	}
... ...
@@ -437,6 +440,8 @@ int rve_is_constant(struct rval_expr* rve)
437 437
 		case RVE_EQ_OP:
438 438
 		case RVE_DIFF_OP:
439 439
 		case RVE_PLUS_OP:
440
+		case RVE_IPLUS_OP:
441
+		case RVE_CONCAT_OP:
440 442
 			return rve_is_constant(rve->left.rve) &&
441 443
 					rve_is_constant(rve->right.rve);
442 444
 		case RVE_NONE_OP:
... ...
@@ -473,6 +478,8 @@ static int rve_op_unary(enum rval_expr_op op)
473 473
 		case RVE_EQ_OP:
474 474
 		case RVE_DIFF_OP:
475 475
 		case RVE_PLUS_OP:
476
+		case RVE_IPLUS_OP:
477
+		case RVE_CONCAT_OP:
476 478
 			return 0;
477 479
 		case RVE_NONE_OP:
478 480
 			return -1;
... ...
@@ -532,6 +539,7 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
532 532
 		case RVE_GTE_OP:
533 533
 		case RVE_LT_OP:
534 534
 		case RVE_LTE_OP:
535
+		case RVE_IPLUS_OP:
535 536
 			*type=RV_INT;
536 537
 			if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
537 538
 				if (type1==RV_STR){
... ...
@@ -587,6 +595,28 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
587 587
 					return 1;
588 588
 				}
589 589
 			}
590
+		case RVE_CONCAT_OP:
591
+			*type=RV_STR;
592
+			if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
593
+				if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
594
+									exp_t)){
595
+					if ((type2!=type1) && (type1!=RV_NONE) &&
596
+							(type2!=RV_NONE) && 
597
+							!(type1==RV_STR && type2==RV_INT)){
598
+						if (bad_rve) *bad_rve=rve->right.rve;
599
+						if (bad_t) *bad_t=type2;
600
+						if (exp_t) *exp_t=type1;
601
+						return 0;
602
+					}
603
+					if (type1==RV_INT){
604
+						if (bad_rve) *bad_rve=rve->left.rve;
605
+						if (bad_t) *bad_t=type1;
606
+						if (exp_t) *exp_t=RV_STR;
607
+						return 0;
608
+					}
609
+					return 1;
610
+				}
611
+			}
590 612
 		case RVE_NONE_OP:
591 613
 			break;
592 614
 	}
... ...
@@ -936,6 +966,7 @@ inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
936 936
 {
937 937
 	switch(op){
938 938
 		case RVE_PLUS_OP:
939
+		case RVE_IPLUS_OP:
939 940
 			*res=v1+v2;
940 941
 			break;
941 942
 		case RVE_MINUS_OP:
... ...
@@ -981,6 +1012,10 @@ inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
981 981
 		case RVE_DIFF_OP:
982 982
 			*res=v1 != v2;
983 983
 			break;
984
+		case RVE_CONCAT_OP:
985
+			*res=0;
986
+			/* invalid operand for int */
987
+			return -1;
984 988
 		default:
985 989
 			BUG("rv unsupported intop %d\n", op);
986 990
 			return -1;
... ...
@@ -1286,6 +1321,7 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1286 1286
 		case RVE_DIV_OP:
1287 1287
 		case RVE_MINUS_OP:
1288 1288
 		case RVE_PLUS_OP:
1289
+		case RVE_IPLUS_OP:
1289 1290
 		case RVE_BOR_OP:
1290 1291
 		case RVE_BAND_OP:
1291 1292
 		case RVE_GT_OP:
... ...
@@ -1376,6 +1412,10 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1376 1376
 				rval_destroy(rv2);
1377 1377
 			break;
1378 1378
 #endif
1379
+		case RVE_CONCAT_OP:
1380
+			*res=0;
1381
+			ret=-1;
1382
+			break;
1379 1383
 		case RVE_NONE_OP:
1380 1384
 		/*default:*/
1381 1385
 			BUG("invalid rval int expression operation %d\n", rve->op);
... ...
@@ -1446,6 +1486,7 @@ int rval_expr_eval_rvint(			   struct run_act_ctx* h,
1446 1446
 		case RVE_LTE_OP:
1447 1447
 		case RVE_EQ_OP:
1448 1448
 		case RVE_DIFF_OP:
1449
+		case RVE_IPLUS_OP:
1449 1450
 			/* operator forces integer type */
1450 1451
 			ret=rval_expr_eval_int(h, msg, res_i, rve);
1451 1452
 			*res_rv=0;
... ...
@@ -1479,6 +1520,10 @@ int rval_expr_eval_rvint(			   struct run_act_ctx* h,
1479 1479
 			}
1480 1480
 			rval_cache_clean(&c1);
1481 1481
 			break;
1482
+		case RVE_CONCAT_OP:
1483
+			*res_rv=rval_expr_eval(h, msg, rve);
1484
+			ret=-(*res_rv==0);
1485
+			break;
1482 1486
 		case RVE_NONE_OP:
1483 1487
 		/*default:*/
1484 1488
 			BUG("invalid rval expression operation %d\n", rve->op);
... ...
@@ -1535,6 +1580,7 @@ struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
1535 1535
 		case RVE_LTE_OP:
1536 1536
 		case RVE_EQ_OP:
1537 1537
 		case RVE_DIFF_OP:
1538
+		case RVE_IPLUS_OP:
1538 1539
 			/* operator forces integer type */
1539 1540
 			r=rval_expr_eval_int(h, msg, &i, rve);
1540 1541
 			if (likely(r==0)){
... ...
@@ -1603,6 +1649,19 @@ struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
1603 1603
 			}
1604 1604
 			rval_cache_clean(&c1);
1605 1605
 			break;
1606
+		case RVE_CONCAT_OP:
1607
+			rv1=rval_expr_eval(h, msg, rve->left.rve);
1608
+			if (unlikely(rv1==0)){
1609
+				ERR("rval expression evaluation failed\n");
1610
+				goto error;
1611
+			}
1612
+			rv2=rval_expr_eval(h, msg, rve->right.rve);
1613
+			if (unlikely(rv2==0)){
1614
+				ERR("rval expression evaluation failed\n");
1615
+				goto error;
1616
+			}
1617
+			ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
1618
+			break;
1606 1619
 		case RVE_NONE_OP:
1607 1620
 		/*default:*/
1608 1621
 			BUG("invalid rval expression operation %d\n", rve->op);
... ...
@@ -1767,8 +1826,10 @@ struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
1767 1767
 		case RVE_LT_OP:
1768 1768
 		case RVE_LTE_OP:
1769 1769
 		case RVE_PLUS_OP:
1770
+		case RVE_IPLUS_OP:
1770 1771
 		case RVE_EQ_OP:
1771 1772
 		case RVE_DIFF_OP:
1773
+		case RVE_CONCAT_OP:
1772 1774
 			break;
1773 1775
 		default:
1774 1776
 			BUG("unsupported operator %d\n", op);
... ...
@@ -1802,6 +1863,8 @@ static int rve_op_is_assoc(enum rval_expr_op op)
1802 1802
 		case RVE_MINUS_OP:
1803 1803
 			return 0;
1804 1804
 		case RVE_PLUS_OP:
1805
+		case RVE_IPLUS_OP:
1806
+		case RVE_CONCAT_OP:
1805 1807
 		case RVE_MUL_OP:
1806 1808
 		case RVE_BAND_OP:
1807 1809
 		case RVE_BOR_OP:
... ...
@@ -1838,6 +1901,7 @@ static int rve_op_is_commutative(enum rval_expr_op op, enum rval_type type)
1838 1838
 			return 0;
1839 1839
 		case RVE_PLUS_OP:
1840 1840
 			return type==RV_INT; /* commutative only for INT*/
1841
+		case RVE_IPLUS_OP:
1841 1842
 		case RVE_MUL_OP:
1842 1843
 		case RVE_BAND_OP:
1843 1844
 		case RVE_BOR_OP:
... ...
@@ -1851,6 +1915,7 @@ static int rve_op_is_commutative(enum rval_expr_op op, enum rval_type type)
1851 1851
 		case RVE_LTE_OP:
1852 1852
 		case RVE_EQ_OP:
1853 1853
 		case RVE_DIFF_OP:
1854
+		case RVE_CONCAT_OP:
1854 1855
 			return 0;
1855 1856
 	}
1856 1857
 	return 0;
... ...
@@ -2158,9 +2223,10 @@ static int rve_opt_01(struct rval_expr* rve, enum rval_type rve_type)
2158 2158
 				}
2159 2159
 				break;
2160 2160
 			case RVE_PLUS_OP:
2161
+			case RVE_IPLUS_OP:
2161 2162
 				/* we must make sure that this is an int PLUS
2162 2163
 				   (because "foo"+0 is valid => "foo0") */
2163
-				if ((i==0) && (rve_type==RV_INT)){
2164
+				if ((i==0) && ((op==RVE_IPLUS_OP)||(rve_type==RV_INT))){
2164 2165
 					/* $v +  0 -> $v
2165 2166
 					 *  0 + $v -> $v */
2166 2167
 					rve_destroy(ct_rve);
... ...
@@ -2206,14 +2272,32 @@ static int rve_opt_01(struct rval_expr* rve, enum rval_type rve_type)
2206 2206
 							op, i);
2207 2207
 			}
2208 2208
 		}
2209
-	}
2210
-	/* no optimization for strings for now
2209
+	}else if (rv->type==RV_STR){
2210
+		switch(op){
2211
+			case RVE_CONCAT_OP:
2212
+				if (rv->v.s.len==0){
2213
+					/* $v . "" -> $v 
2214
+					   "" . $v -> $v */
2215
+					rve_destroy(ct_rve);
2216
+					pos=rve->fpos;
2217
+					*rve=*v_rve; /* replace current expr. with $v */
2218
+					rve->fpos=pos;
2219
+					pkg_free(v_rve);/* rve_destroy(v_rve) would free
2220
+									   everything*/
2221
+					ret=1;
2222
+				}
2223
+				break;
2224
+			default:
2225
+				break;
2226
+		}
2227
+	/* no optimization for generic RVE_PLUS_OP for now, only for RVE_CONCAT_OP
2211 2228
 	   (We could optimize $v + "" or ""+$v, but this ""+$v is a way
2212 2229
 	    to force convert $v to str , it might mess up type checking
2213 2230
 	    (e.g. errors w/o optimization and no errors with) and it brings
2214 2231
 	    a very small benefit anyway (it's unlikely we'll see a lot of
2215 2232
 	    "")
2216 2233
 	*/
2234
+	}
2217 2235
 	if (rv) rval_destroy(rv);
2218 2236
 	return ret;
2219 2237
 error:
... ...
@@ -2232,7 +2316,7 @@ static int rve_optimize(struct rval_expr* rve)
2232 2232
 	enum rval_expr_op op;
2233 2233
 	int flags;
2234 2234
 	struct rval_expr tmp_rve;
2235
-	enum rval_type type;
2235
+	enum rval_type type, l_type;
2236 2236
 	struct rval_expr* bad_rve;
2237 2237
 	enum rval_type bad_type, exp_type;
2238 2238
 	
... ...
@@ -2285,7 +2369,7 @@ static int rve_optimize(struct rval_expr* rve)
2285 2285
 				rv->v.l=-rv->v.l;
2286 2286
 				if (rve_replace_with_ct_rv(rve->right.rve, rv)<0)
2287 2287
 					goto error;
2288
-				rve->op=RVE_PLUS_OP;
2288
+				rve->op=RVE_IPLUS_OP;
2289 2289
 				DBG("FIXUP RVE: optimized $v - a into $v + (%d)\n",
2290 2290
 								(int)rve->right.rve->left.rval.v.l);
2291 2291
 			}
... ...
@@ -2293,6 +2377,22 @@ static int rve_optimize(struct rval_expr* rve)
2293 2293
 			rv=0;
2294 2294
 		}
2295 2295
 		
2296
+		/* e1 PLUS_OP e2 -> change op if we know e1 basic type */
2297
+		if (rve->op==RVE_PLUS_OP){
2298
+			l_type=rve_guess_type(rve->left.rve);
2299
+			if (l_type==RV_INT){
2300
+				rve->op=RVE_IPLUS_OP;
2301
+				DBG("FIXUP RVE (%d,%d-%d,%d): changed + into interger plus\n",
2302
+						rve->fpos.s_line, rve->fpos.s_col,
2303
+						rve->fpos.e_line, rve->fpos.e_col);
2304
+			}else if (l_type==RV_STR){
2305
+				rve->op=RVE_CONCAT_OP;
2306
+				DBG("FIXUP RVE (%d,%d-%d,%d): changed + into string concat\n",
2307
+						rve->fpos.s_line, rve->fpos.s_col,
2308
+						rve->fpos.e_line, rve->fpos.e_col);
2309
+			}
2310
+		}
2311
+		
2296 2312
 		/* $v * 0 => 0; $v * 1 => $v (for *, /, &, |, &&, ||, +, -) */
2297 2313
 		if (rve_opt_01(rve, type)==1){
2298 2314
 			/* success, rve was changed => return now
... ...
@@ -2510,8 +2610,10 @@ int fix_rval_expr(void** p)
2510 2510
 		case RVE_LT_OP:
2511 2511
 		case RVE_LTE_OP:
2512 2512
 		case RVE_PLUS_OP:
2513
+		case RVE_IPLUS_OP:
2513 2514
 		case RVE_EQ_OP:
2514 2515
 		case RVE_DIFF_OP:
2516
+		case RVE_CONCAT_OP:
2515 2517
 			ret=fix_rval_expr((void**)&rve->left.rve);
2516 2518
 			if (ret<0) return ret;
2517 2519
 			ret=fix_rval_expr((void**)&rve->right.rve);
... ...
@@ -59,10 +59,12 @@ enum rval_expr_op{
59 59
 	RVE_GTE_OP,   /*  2 members, returns left >= right */
60 60
 	RVE_LT_OP,    /*  2 members, returns left  < right */
61 61
 	RVE_LTE_OP,   /*  2 members, returns left <= right */
62
+	RVE_IPLUS_OP, /* 2 members, integer +, returns int(a)+int(b) */
62 63
 	/* common int & str */
63
-	RVE_PLUS_OP,  /* 2 members, returns left + right  (int or str)*/
64
+	RVE_PLUS_OP,  /* generic plus (int or str) returns left + right */
64 65
 	RVE_EQ_OP,    /*  2 members, returns left == right  (int)*/
65 66
 	RVE_DIFF_OP,  /*  2 members, returns left != right  (int)*/
67
+	RVE_CONCAT_OP,/* string concatenation, returns left . right */
66 68
 	/* str only */
67 69
 };
68 70