lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've brute forced my way through by essentially duplicating pow in all the places it exists. If anyone would be kind enough to glance at the following diff and see if I'm doing something horribly wrong and or dangerous I'd greatly appreciate it.

Index: lcode.c
===================================================================
--- lcode.c	(revision 31635)
+++ lcode.c	(working copy)
@@ -799,7 +799,8 @@
       break;
     }
     case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
-    case OPR_MOD: case OPR_POW: {
+    case OPR_MOD: case OPR_POW: 
+    case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
       if (!isnumeral(v)) luaK_exp2RK(fs, v);
       break;
     }
@@ -843,7 +844,8 @@
       break;
     }
     case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
-    case OPR_MOD: case OPR_POW: {
+    case OPR_MOD: case OPR_POW: 
+    case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
       codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
       break;
     }
Index: lcode.h
===================================================================
--- lcode.h	(revision 31635)
+++ lcode.h	(working copy)
@@ -24,7 +24,8 @@
 ** grep "ORDER OPR" if you change these enums  (ORDER OP)
 */
 typedef enum BinOpr {
-  OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,
+  OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 
+  OPR_BAND, OPR_BOR, OPR_BXOR,
   OPR_CONCAT,
   OPR_EQ, OPR_LT, OPR_LE,
   OPR_NE, OPR_GT, OPR_GE,
Index: ldebug.c
===================================================================
--- ldebug.c	(revision 31635)
+++ ldebug.c	(working copy)
@@ -455,6 +455,9 @@
     case OP_DIV: tm = TM_DIV; break;
     case OP_MOD: tm = TM_MOD; break;
     case OP_POW: tm = TM_POW; break;
+    case OP_BAND: tm = TM_BAND; break;
+    case OP_BOR: tm = TM_BOR; break;
+    case OP_BXOR: tm = TM_BXOR; break;
     case OP_UNM: tm = TM_UNM; break;
     case OP_LEN: tm = TM_LEN; break;
     case OP_LT: tm = TM_LT; break;
Index: lobject.c
===================================================================
--- lobject.c	(revision 31635)
+++ lobject.c	(working copy)
@@ -78,6 +78,9 @@
     case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
     case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
     case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
+    case LUA_OPBAND: return luai_numband(NULL, v1, v2);
+    case LUA_OPBOR: return luai_numbor(NULL, v1, v2);
+    case LUA_OPBXOR: return luai_numbxor(NULL, v1, v2);
     case LUA_OPUNM: return luai_numunm(NULL, v1);
     default: lua_assert(0); return 0;
   }
Index: lopcodes.c
===================================================================
--- lopcodes.c	(revision 31635)
+++ lopcodes.c	(working copy)
@@ -34,6 +34,9 @@
   "DIV",
   "MOD",
   "POW",
+  "BAND",
+  "BOR",
+  "BXOR",
   "UNM",
   "NOT",
   "LEN",
@@ -82,6 +85,9 @@
  ,opmode(0, 1, OpArgK, OpArgK, iABC)		/* OP_DIV */
  ,opmode(0, 1, OpArgK, OpArgK, iABC)		/* OP_MOD */
  ,opmode(0, 1, OpArgK, OpArgK, iABC)		/* OP_POW */
+ ,opmode(0, 1, OpArgK, OpArgK, iABC)		/* OP_BAND */
+ ,opmode(0, 1, OpArgK, OpArgK, iABC)		/* OP_BOR */
+ ,opmode(0, 1, OpArgK, OpArgK, iABC)		/* OP_BXOR */
  ,opmode(0, 1, OpArgR, OpArgN, iABC)		/* OP_UNM */
  ,opmode(0, 1, OpArgR, OpArgN, iABC)		/* OP_NOT */
  ,opmode(0, 1, OpArgR, OpArgN, iABC)		/* OP_LEN */
Index: lopcodes.h
===================================================================
--- lopcodes.h	(revision 31635)
+++ lopcodes.h	(working copy)
@@ -190,10 +190,16 @@
 OP_DIV,/*	A B C	R(A) := RK(B) / RK(C)				*/
 OP_MOD,/*	A B C	R(A) := RK(B) % RK(C)				*/
 OP_POW,/*	A B C	R(A) := RK(B) ^ RK(C)				*/
+// new bitwise logic
+OP_BAND, 
+OP_BOR,
+OP_BXOR,
+
 OP_UNM,/*	A B	R(A) := -R(B)					*/
 OP_NOT,/*	A B	R(A) := not R(B)				*/
 OP_LEN,/*	A B	R(A) := length of R(B)				*/
 
+
 OP_CONCAT,/*	A B C	R(A) := R(B).. ... ..R(C)			*/
 
 OP_JMP,/*	A sBx	pc+=sBx; if (A) close all upvalues >= R(A) + 1	*/
Index: lparser.c
===================================================================
--- lparser.c	(revision 31635)
+++ lparser.c	(working copy)
@@ -999,6 +999,9 @@
     case '/': return OPR_DIV;
     case '%': return OPR_MOD;
     case '^': return OPR_POW;
+    case '&': return OPR_BAND;
+    case '|': return OPR_BOR;
+    case '^^': return OPR_BXOR;
     case TK_CONCAT: return OPR_CONCAT;
     case TK_NE: return OPR_NE;
     case TK_EQ: return OPR_EQ;
@@ -1018,6 +1021,7 @@
   lu_byte right; /* right priority */
 } priority[] = {  /* ORDER OPR */
    {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7},  /* `+' `-' `*' `/' `%' */
+   {6, 6}, {6, 6}, {6, 6}, /* & | ^^ */
    {10, 9}, {5, 4},                 /* ^, .. (right associative) */
    {3, 3}, {3, 3}, {3, 3},          /* ==, <, <= */
    {3, 3}, {3, 3}, {3, 3},          /* ~=, >, >= */
Index: ltm.c
===================================================================
--- ltm.c	(revision 31635)
+++ ltm.c	(working copy)
@@ -34,7 +34,7 @@
     "__index", "__newindex",
     "__gc", "__mode", "__len", "__eq",
     "__add", "__sub", "__mul", "__div", "__mod",
-    "__pow", "__unm", "__lt", "__le",
+    "__pow", "__band", "__bor", "__bxor", "__unm", "__lt", "__le",
     "__concat", "__call"
   };
   int i;
Index: ltm.h
===================================================================
--- ltm.h	(revision 31635)
+++ ltm.h	(working copy)
@@ -28,6 +28,9 @@
   TM_DIV,
   TM_MOD,
   TM_POW,
+  TM_BAND,
+  TM_BOR,
+  TM_BXOR,
   TM_UNM,
   TM_LT,
   TM_LE,
Index: lua.h
===================================================================
--- lua.h	(revision 31635)
+++ lua.h	(working copy)
@@ -181,7 +181,10 @@
 #define LUA_OPDIV	3
 #define LUA_OPMOD	4
 #define LUA_OPPOW	5
-#define LUA_OPUNM	6
+#define LUA_OPBAND 6
+#define LUA_OPBOR 7
+#define LUA_OPBXOR 8
+#define LUA_OPUNM	9
 
 LUA_API void  (lua_arith) (lua_State *L, int op);
 
Index: luaconf.h
===================================================================
--- luaconf.h	(revision 31635)
+++ luaconf.h	(working copy)
@@ -442,6 +442,10 @@
 #define luai_numlt(L,a,b)	((a)<(b))
 #define luai_numle(L,a,b)	((a)<=(b))
 #define luai_numisnan(L,a)	(!luai_numeq((a), (a)))
+
+#define luai_numband(L,a,b)	(((int)a)&((int)b))
+#define luai_numbor(L,a,b)	(((int)a)|((int)b))
+#define luai_numbxor(L,a,b)	(((int)a)^((int)b))
 #endif
 
 
Index: lvm.c
===================================================================
--- lvm.c	(revision 31635)
+++ lvm.c	(working copy)
@@ -429,6 +429,7 @@
   switch (op) {  /* finish its execution */
     case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
     case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
+    case OP_BAND: case OP_BOR: case OP_BXOR:
     case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
       setobjs2s(L, base + GETARG_A(inst), --L->top);
       break;
@@ -633,6 +634,15 @@
       vmcase(OP_POW,
         arith_op(luai_numpow, TM_POW);
       )
+      vmcase(OP_BAND,
+        arith_op(luai_numband, TM_BAND);
+      )
+      vmcase(OP_BOR,
+        arith_op(luai_numbor, TM_BOR);
+      )
+      vmcase(OP_BXOR,
+        arith_op(luai_numbxor, TM_BXOR);
+      )
       vmcase(OP_UNM,
         TValue *rb = RB(i);
         if (ttisnumber(rb)) {


-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Pierre-Yves Gérardy
Sent: Tuesday, May 07, 2013 2:30 PM
To: Lua mailing list
Subject: Re: bitwise operators Power Patch for 5.2

On Tue, May 7, 2013 at 10:04 PM, John Dunn <John_Dunn@qscaudio.com> wrote:
> The old patches don't apply at all to 5.2.1 so I'm wondering if anyone has patched Lua to add these operators. If not, would it be fairly straight for me to modify the source to add these myself?

It shouldn't be difficult to roll it out yourself. With little C experience, I was able to add a short lambda syntax a few years ago...

The code is clean and well structured, if terse. You just have to imitate what's already in place for the new operators in the lexer/parser and the new VM instructions (which would include number handling and metamethod dispatch).

You may take some hints from the existing patch as well, v5.2 is still similar to v5.1 in a lot of respects.

-- Pierre-Yves