@@ -1351,6 +1351,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1,
13511351}
13521352
13531353
1354+ /*
1355+ ** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP)
1356+ */
1357+ l_sinline OpCode binopr2op (BinOpr opr , BinOpr baser , OpCode base ) {
1358+ lua_assert (baser <= opr &&
1359+ ((baser == OPR_ADD && opr <= OPR_SHR ) ||
1360+ (baser == OPR_LT && opr <= OPR_LE )));
1361+ return cast (OpCode , (cast_int (opr ) - cast_int (baser )) + cast_int (base ));
1362+ }
1363+
1364+
1365+ /*
1366+ ** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP)
1367+ */
1368+ l_sinline OpCode unopr2op (UnOpr opr ) {
1369+ return cast (OpCode , (cast_int (opr ) - cast_int (OPR_MINUS )) +
1370+ cast_int (OP_UNM ));
1371+ }
1372+
1373+
1374+ /*
1375+ ** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM)
1376+ */
1377+ l_sinline TMS binopr2TM (BinOpr opr ) {
1378+ lua_assert (OPR_ADD <= opr && opr <= OPR_SHR );
1379+ return cast (TMS , (cast_int (opr ) - cast_int (OPR_ADD )) + cast_int (TM_ADD ));
1380+ }
1381+
1382+
13541383/*
13551384** Emit code for unary expressions that "produce values"
13561385** (everything but 'not').
@@ -1389,12 +1418,15 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
13891418** Emit code for binary expressions that "produce values" over
13901419** two registers.
13911420*/
1392- static void codebinexpval (FuncState * fs , OpCode op ,
1421+ static void codebinexpval (FuncState * fs , BinOpr opr ,
13931422 expdesc * e1 , expdesc * e2 , int line ) {
1394- int v2 = luaK_exp2anyreg (fs , e2 ); /* both operands are in registers */
1423+ OpCode op = binopr2op (opr , OPR_ADD , OP_ADD );
1424+ int v2 = luaK_exp2anyreg (fs , e2 ); /* make sure 'e2' is in a register */
1425+ /* 'e1' must be already in a register or it is a constant */
1426+ lua_assert ((VNIL <= e1 -> k && e1 -> k <= VKSTR ) ||
1427+ e1 -> k == VNONRELOC || e1 -> k == VRELOC );
13951428 lua_assert (OP_ADD <= op && op <= OP_SHR );
1396- finishbinexpval (fs , e1 , e2 , op , v2 , 0 , line , OP_MMBIN ,
1397- cast (TMS , (op - OP_ADD ) + TM_ADD ));
1429+ finishbinexpval (fs , e1 , e2 , op , v2 , 0 , line , OP_MMBIN , binopr2TM (opr ));
13981430}
13991431
14001432
@@ -1410,6 +1442,18 @@ static void codebini (FuncState *fs, OpCode op,
14101442}
14111443
14121444
1445+ /*
1446+ ** Code binary operators with K operand.
1447+ */
1448+ static void codebinK (FuncState * fs , BinOpr opr ,
1449+ expdesc * e1 , expdesc * e2 , int flip , int line ) {
1450+ TMS event = binopr2TM (opr );
1451+ int v2 = e2 -> u .info ; /* K index */
1452+ OpCode op = binopr2op (opr , OPR_ADD , OP_ADDK );
1453+ finishbinexpval (fs , e1 , e2 , op , v2 , flip , line , OP_MMBINK , event );
1454+ }
1455+
1456+
14131457/* Try to code a binary operator negating its second operand.
14141458** For the metamethod, 2nd operand must keep its original value.
14151459*/
@@ -1437,24 +1481,27 @@ static void swapexps (expdesc *e1, expdesc *e2) {
14371481}
14381482
14391483
1484+ /*
1485+ ** Code binary operators with no constant operand.
1486+ */
1487+ static void codebinNoK (FuncState * fs , BinOpr opr ,
1488+ expdesc * e1 , expdesc * e2 , int flip , int line ) {
1489+ if (flip )
1490+ swapexps (e1 , e2 ); /* back to original order */
1491+ codebinexpval (fs , opr , e1 , e2 , line ); /* use standard operators */
1492+ }
1493+
1494+
14401495/*
14411496** Code arithmetic operators ('+', '-', ...). If second operand is a
14421497** constant in the proper range, use variant opcodes with K operands.
14431498*/
14441499static void codearith (FuncState * fs , BinOpr opr ,
14451500 expdesc * e1 , expdesc * e2 , int flip , int line ) {
1446- TMS event = cast (TMS , opr + TM_ADD );
1447- if (tonumeral (e2 , NULL ) && luaK_exp2K (fs , e2 )) { /* K operand? */
1448- int v2 = e2 -> u .info ; /* K index */
1449- OpCode op = cast (OpCode , opr + OP_ADDK );
1450- finishbinexpval (fs , e1 , e2 , op , v2 , flip , line , OP_MMBINK , event );
1451- }
1452- else { /* 'e2' is neither an immediate nor a K operand */
1453- OpCode op = cast (OpCode , opr + OP_ADD );
1454- if (flip )
1455- swapexps (e1 , e2 ); /* back to original order */
1456- codebinexpval (fs , op , e1 , e2 , line ); /* use standard operators */
1457- }
1501+ if (tonumeral (e2 , NULL ) && luaK_exp2K (fs , e2 )) /* K operand? */
1502+ codebinK (fs , opr , e1 , e2 , flip , line );
1503+ else /* 'e2' is neither an immediate nor a K operand */
1504+ codebinNoK (fs , opr , e1 , e2 , flip , line );
14581505}
14591506
14601507
@@ -1471,61 +1518,55 @@ static void codecommutative (FuncState *fs, BinOpr op,
14711518 flip = 1 ;
14721519 }
14731520 if (op == OPR_ADD && isSCint (e2 )) /* immediate operand? */
1474- codebini (fs , cast ( OpCode , OP_ADDI ) , e1 , e2 , flip , line , TM_ADD );
1521+ codebini (fs , OP_ADDI , e1 , e2 , flip , line , TM_ADD );
14751522 else
14761523 codearith (fs , op , e1 , e2 , flip , line );
14771524}
14781525
14791526
14801527/*
1481- ** Code bitwise operations; they are all associative , so the function
1528+ ** Code bitwise operations; they are all commutative , so the function
14821529** tries to put an integer constant as the 2nd operand (a K operand).
14831530*/
14841531static void codebitwise (FuncState * fs , BinOpr opr ,
14851532 expdesc * e1 , expdesc * e2 , int line ) {
14861533 int flip = 0 ;
1487- int v2 ;
1488- OpCode op ;
1489- if (e1 -> k == VKINT && luaK_exp2RK (fs , e1 )) {
1534+ if (e1 -> k == VKINT ) {
14901535 swapexps (e1 , e2 ); /* 'e2' will be the constant operand */
14911536 flip = 1 ;
14921537 }
1493- else if (!(e2 -> k == VKINT && luaK_exp2RK (fs , e2 ))) { /* no constants? */
1494- op = cast (OpCode , opr + OP_ADD );
1495- codebinexpval (fs , op , e1 , e2 , line ); /* all-register opcodes */
1496- return ;
1497- }
1498- v2 = e2 -> u .info ; /* index in K array */
1499- op = cast (OpCode , opr + OP_ADDK );
1500- lua_assert (ttisinteger (& fs -> f -> k [v2 ]));
1501- finishbinexpval (fs , e1 , e2 , op , v2 , flip , line , OP_MMBINK ,
1502- cast (TMS , opr + TM_ADD ));
1538+ if (e2 -> k == VKINT && luaK_exp2K (fs , e2 )) /* K operand? */
1539+ codebinK (fs , opr , e1 , e2 , flip , line );
1540+ else /* no constants */
1541+ codebinNoK (fs , opr , e1 , e2 , flip , line );
15031542}
15041543
15051544
15061545/*
15071546** Emit code for order comparisons. When using an immediate operand,
15081547** 'isfloat' tells whether the original value was a float.
15091548*/
1510- static void codeorder (FuncState * fs , OpCode op , expdesc * e1 , expdesc * e2 ) {
1549+ static void codeorder (FuncState * fs , BinOpr opr , expdesc * e1 , expdesc * e2 ) {
15111550 int r1 , r2 ;
15121551 int im ;
15131552 int isfloat = 0 ;
1553+ OpCode op ;
15141554 if (isSCnumber (e2 , & im , & isfloat )) {
15151555 /* use immediate operand */
15161556 r1 = luaK_exp2anyreg (fs , e1 );
15171557 r2 = im ;
1518- op = cast ( OpCode , ( op - OP_LT ) + OP_LTI );
1558+ op = binopr2op ( opr , OPR_LT , OP_LTI );
15191559 }
15201560 else if (isSCnumber (e1 , & im , & isfloat )) {
15211561 /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
15221562 r1 = luaK_exp2anyreg (fs , e2 );
15231563 r2 = im ;
1524- op = ( op == OP_LT ) ? OP_GTI : OP_GEI ;
1564+ op = binopr2op ( opr , OPR_LT , OP_GTI ) ;
15251565 }
15261566 else { /* regular case, compare two registers */
15271567 r1 = luaK_exp2anyreg (fs , e1 );
15281568 r2 = luaK_exp2anyreg (fs , e2 );
1569+ op = binopr2op (opr , OPR_LT , OP_LT );
15291570 }
15301571 freeexps (fs , e1 , e2 );
15311572 e1 -> u .info = condjump (fs , op , r1 , r2 , isfloat , 1 );
@@ -1551,7 +1592,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15511592 op = OP_EQI ;
15521593 r2 = im ; /* immediate operand */
15531594 }
1554- else if (luaK_exp2RK (fs , e2 )) { /* 1st expression is constant? */
1595+ else if (luaK_exp2RK (fs , e2 )) { /* 2nd expression is constant? */
15551596 op = OP_EQK ;
15561597 r2 = e2 -> u .info ; /* constant index */
15571598 }
@@ -1568,16 +1609,16 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15681609/*
15691610** Apply prefix operation 'op' to expression 'e'.
15701611*/
1571- void luaK_prefix (FuncState * fs , UnOpr op , expdesc * e , int line ) {
1612+ void luaK_prefix (FuncState * fs , UnOpr opr , expdesc * e , int line ) {
15721613 static const expdesc ef = {VKINT , {0 }, NO_JUMP , NO_JUMP };
15731614 luaK_dischargevars (fs , e );
1574- switch (op ) {
1615+ switch (opr ) {
15751616 case OPR_MINUS : case OPR_BNOT : /* use 'ef' as fake 2nd operand */
1576- if (constfolding (fs , op + LUA_OPUNM , e , & ef ))
1617+ if (constfolding (fs , opr + LUA_OPUNM , e , & ef ))
15771618 break ;
15781619 /* else */ /* FALLTHROUGH */
15791620 case OPR_LEN :
1580- codeunexpval (fs , cast ( OpCode , op + OP_UNM ), e , line );
1621+ codeunexpval (fs , unopr2op ( opr ), e , line );
15811622 break ;
15821623 case OPR_NOT : codenot (fs , e ); break ;
15831624 default : lua_assert (0 );
@@ -1611,7 +1652,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
16111652 case OPR_SHL : case OPR_SHR : {
16121653 if (!tonumeral (v , NULL ))
16131654 luaK_exp2anyreg (fs , v );
1614- /* else keep numeral, which may be folded with 2nd operand */
1655+ /* else keep numeral, which may be folded or used as an immediate
1656+ operand */
16151657 break ;
16161658 }
16171659 case OPR_EQ : case OPR_NE : {
@@ -1706,30 +1748,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
17061748 /* coded as (r1 >> -I) */ ;
17071749 }
17081750 else /* regular case (two registers) */
1709- codebinexpval (fs , OP_SHL , e1 , e2 , line );
1751+ codebinexpval (fs , opr , e1 , e2 , line );
17101752 break ;
17111753 }
17121754 case OPR_SHR : {
17131755 if (isSCint (e2 ))
17141756 codebini (fs , OP_SHRI , e1 , e2 , 0 , line , TM_SHR ); /* r1 >> I */
17151757 else /* regular case (two registers) */
1716- codebinexpval (fs , OP_SHR , e1 , e2 , line );
1758+ codebinexpval (fs , opr , e1 , e2 , line );
17171759 break ;
17181760 }
17191761 case OPR_EQ : case OPR_NE : {
17201762 codeeq (fs , opr , e1 , e2 );
17211763 break ;
17221764 }
1723- case OPR_LT : case OPR_LE : {
1724- OpCode op = cast (OpCode , (opr - OPR_EQ ) + OP_EQ );
1725- codeorder (fs , op , e1 , e2 );
1726- break ;
1727- }
17281765 case OPR_GT : case OPR_GE : {
17291766 /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
1730- OpCode op = cast (OpCode , (opr - OPR_NE ) + OP_EQ );
17311767 swapexps (e1 , e2 );
1732- codeorder (fs , op , e1 , e2 );
1768+ opr = cast (BinOpr , (opr - OPR_GT ) + OPR_LT );
1769+ } /* FALLTHROUGH */
1770+ case OPR_LT : case OPR_LE : {
1771+ codeorder (fs , opr , e1 , e2 );
17331772 break ;
17341773 }
17351774 default : lua_assert (0 );
0 commit comments