@@ -627,7 +627,7 @@ static int nilK (FuncState *fs) {
627627
628628/*
629629** Check whether 'i' can be stored in an 'sC' operand.
630- ** Equivalent to (0 <= i + OFFSET_sC && i + OFFSET_sC <= MAXARG_C)
630+ ** Equivalent to (0 <= int2sC(i) && int2sC(i) <= MAXARG_C)
631631** but without risk of overflows in the addition.
632632*/
633633static int fitsC (lua_Integer i ) {
@@ -651,14 +651,9 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
651651}
652652
653653
654- static int floatI (lua_Number f , lua_Integer * fi ) {
655- return (luaV_flttointeger (f , fi , 0 ) && fitsBx (* fi ));
656- }
657-
658-
659654static void luaK_float (FuncState * fs , int reg , lua_Number f ) {
660655 lua_Integer fi ;
661- if (floatI (f , & fi ))
656+ if (luaV_flttointeger (f , & fi , 0 ) && fitsBx ( fi ))
662657 luaK_codeAsBx (fs , OP_LOADF , reg , cast_int (fi ));
663658 else
664659 luaK_codek (fs , reg , luaK_numberK (fs , f ));
@@ -1221,15 +1216,16 @@ static int isSCint (expdesc *e) {
12211216** Check whether expression 'e' is a literal integer or float in
12221217** proper range to fit in a register (sB or sC).
12231218*/
1224- static int isSCnumber (expdesc * e , lua_Integer * i , int * isfloat ) {
1219+ static int isSCnumber (expdesc * e , int * pi , int * isfloat ) {
1220+ lua_Integer i ;
12251221 if (e -> k == VKINT )
1226- * i = e -> u .ival ;
1227- else if (!(e -> k == VKFLT && floatI (e -> u .nval , i )))
1228- return 0 ; /* not a number */
1229- else
1222+ i = e -> u .ival ;
1223+ else if (e -> k == VKFLT && luaV_flttointeger (e -> u .nval , & i , 0 ))
12301224 * isfloat = 1 ;
1231- if (!hasjumps (e ) && fitsC (* i )) {
1232- * i += OFFSET_sC ;
1225+ else
1226+ return 0 ; /* not a number */
1227+ if (!hasjumps (e ) && fitsC (i )) {
1228+ * pi = int2sC (cast_int (i ));
12331229 return 1 ;
12341230 }
12351231 else
@@ -1347,12 +1343,6 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
13471343 e1 -> u .info = pc ;
13481344 e1 -> k = VRELOC ; /* all those operations are relocatable */
13491345 luaK_fixline (fs , line );
1350- if (op == OP_SHRI && flip ) {
1351- /* For the metamethod, undo the "changedir" did by 'codeshift' */
1352- event = TM_SHL ;
1353- v2 = - (v2 - OFFSET_sC ) + OFFSET_sC ;
1354- flip = 0 ;
1355- }
13561346 luaK_codeABCk (fs , mmop , v1 , v2 , event , flip ); /* to call metamethod */
13571347 luaK_fixline (fs , line );
13581348}
@@ -1377,7 +1367,8 @@ static void codebinexpval (FuncState *fs, OpCode op,
13771367static void codebini (FuncState * fs , OpCode op ,
13781368 expdesc * e1 , expdesc * e2 , int flip , int line ,
13791369 TMS event ) {
1380- int v2 = cast_int (e2 -> u .ival ) + OFFSET_sC ; /* immediate operand */
1370+ int v2 = int2sC (cast_int (e2 -> u .ival )); /* immediate operand */
1371+ lua_assert (e2 -> k == VKINT );
13811372 finishbinexpval (fs , e1 , e2 , op , v2 , flip , line , OP_MMBINI , event );
13821373}
13831374
@@ -1460,12 +1451,14 @@ static void codebitwise (FuncState *fs, BinOpr opr,
14601451static void codeshift (FuncState * fs , OpCode op ,
14611452 expdesc * e1 , expdesc * e2 , int line ) {
14621453 if (isSCint (e2 )) {
1463- int changedir = 0 ;
1464- if (op == OP_SHL ) {
1465- changedir = 1 ;
1466- e2 -> u .ival = - (e2 -> u .ival );
1454+ if (op == OP_SHR )
1455+ codebini (fs , OP_SHRI , e1 , e2 , 0 , line , TM_SHR );
1456+ else {
1457+ int offset = cast_int (e2 -> u .ival );
1458+ finishbinexpval (fs , e1 , e2 , OP_SHRI , int2sC (offset ),
1459+ 0 , line , OP_MMBINI , TM_SHL );
1460+ SETARG_C (fs -> f -> code [fs -> pc - 2 ], int2sC (- offset ));
14671461 }
1468- codebini (fs , OP_SHRI , e1 , e2 , changedir , line , TM_SHR );
14691462 }
14701463 else
14711464 codebinexpval (fs , op , e1 , e2 , line );
@@ -1478,18 +1471,18 @@ static void codeshift (FuncState *fs, OpCode op,
14781471*/
14791472static void codeorder (FuncState * fs , OpCode op , expdesc * e1 , expdesc * e2 ) {
14801473 int r1 , r2 ;
1481- lua_Integer im ;
1474+ int im ;
14821475 int isfloat = 0 ;
14831476 if (isSCnumber (e2 , & im , & isfloat )) {
14841477 /* use immediate operand */
14851478 r1 = luaK_exp2anyreg (fs , e1 );
1486- r2 = cast_int ( im ) ;
1479+ r2 = im ;
14871480 op = cast (OpCode , (op - OP_LT ) + OP_LTI );
14881481 }
14891482 else if (isSCnumber (e1 , & im , & isfloat )) {
14901483 /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
14911484 r1 = luaK_exp2anyreg (fs , e2 );
1492- r2 = cast_int ( im ) ;
1485+ r2 = im ;
14931486 op = (op == OP_LT ) ? OP_GTI : OP_GEI ;
14941487 }
14951488 else { /* regular case, compare two registers */
@@ -1508,7 +1501,7 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
15081501*/
15091502static void codeeq (FuncState * fs , BinOpr opr , expdesc * e1 , expdesc * e2 ) {
15101503 int r1 , r2 ;
1511- lua_Integer im ;
1504+ int im ;
15121505 int isfloat = 0 ; /* not needed here, but kept for symmetry */
15131506 OpCode op ;
15141507 if (e1 -> k != VNONRELOC ) {
@@ -1518,7 +1511,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15181511 r1 = luaK_exp2anyreg (fs , e1 ); /* 1nd expression must be in register */
15191512 if (isSCnumber (e2 , & im , & isfloat )) {
15201513 op = OP_EQI ;
1521- r2 = cast_int ( im ) ; /* immediate operand */
1514+ r2 = im ; /* immediate operand */
15221515 }
15231516 else if (luaK_exp2RK (fs , e2 )) { /* 1st expression is constant? */
15241517 op = OP_EQK ;
@@ -1591,8 +1584,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
15911584 }
15921585 case OPR_LT : case OPR_LE :
15931586 case OPR_GT : case OPR_GE : {
1594- lua_Integer dummy ;
1595- int dummy2 ;
1587+ int dummy , dummy2 ;
15961588 if (!isSCnumber (v , & dummy , & dummy2 ))
15971589 luaK_exp2anyreg (fs , v );
15981590 /* else keep numeral, which may be an immediate operand */
0 commit comments