aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lauxlib.c22
-rw-r--r--lauxlib.h8
-rw-r--r--lbaselib.c12
-rw-r--r--lcorolib.c10
-rw-r--r--ldblib.c6
-rw-r--r--ldo.c24
-rw-r--r--ldo.h2
-rw-r--r--lgc.c2
-rw-r--r--liolib.c17
-rw-r--r--llimits.h16
-rw-r--r--lmathlib.c5
-rw-r--r--lmem.c8
-rw-r--r--loadlib.c17
-rw-r--r--loslib.c8
-rw-r--r--lparser.c8
-rw-r--r--lstate.c2
-rw-r--r--lstring.c8
-rw-r--r--lstrlib.c41
-rw-r--r--ltable.c8
-rw-r--r--ltablib.c9
-rw-r--r--ltm.c5
-rw-r--r--luaconf.h20
-rw-r--r--lvm.c39
-rw-r--r--lvm.h6
24 files changed, 162 insertions, 141 deletions
diff --git a/lauxlib.c b/lauxlib.c
index e8fc486e..2610f90e 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -190,7 +190,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
190} 190}
191 191
192 192
193int luaL_typeerror (lua_State *L, int arg, const char *tname) { 193LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
194 const char *msg; 194 const char *msg;
195 const char *typearg; /* name for the type of the actual argument */ 195 const char *typearg; /* name for the type of the actual argument */
196 if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) 196 if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
@@ -378,7 +378,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
378** but without 'msg'.) 378** but without 'msg'.)
379*/ 379*/
380LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { 380LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
381 if (!lua_checkstack(L, space)) { 381 if (l_unlikely(!lua_checkstack(L, space))) {
382 if (msg) 382 if (msg)
383 luaL_error(L, "stack overflow (%s)", msg); 383 luaL_error(L, "stack overflow (%s)", msg);
384 else 384 else
@@ -388,20 +388,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
388 388
389 389
390LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { 390LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
391 if (lua_type(L, arg) != t) 391 if (l_unlikely(lua_type(L, arg) != t))
392 tag_error(L, arg, t); 392 tag_error(L, arg, t);
393} 393}
394 394
395 395
396LUALIB_API void luaL_checkany (lua_State *L, int arg) { 396LUALIB_API void luaL_checkany (lua_State *L, int arg) {
397 if (lua_type(L, arg) == LUA_TNONE) 397 if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
398 luaL_argerror(L, arg, "value expected"); 398 luaL_argerror(L, arg, "value expected");
399} 399}
400 400
401 401
402LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { 402LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
403 const char *s = lua_tolstring(L, arg, len); 403 const char *s = lua_tolstring(L, arg, len);
404 if (!s) tag_error(L, arg, LUA_TSTRING); 404 if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
405 return s; 405 return s;
406} 406}
407 407
@@ -420,7 +420,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
420LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { 420LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
421 int isnum; 421 int isnum;
422 lua_Number d = lua_tonumberx(L, arg, &isnum); 422 lua_Number d = lua_tonumberx(L, arg, &isnum);
423 if (!isnum) 423 if (l_unlikely(!isnum))
424 tag_error(L, arg, LUA_TNUMBER); 424 tag_error(L, arg, LUA_TNUMBER);
425 return d; 425 return d;
426} 426}
@@ -442,7 +442,7 @@ static void interror (lua_State *L, int arg) {
442LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { 442LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
443 int isnum; 443 int isnum;
444 lua_Integer d = lua_tointegerx(L, arg, &isnum); 444 lua_Integer d = lua_tointegerx(L, arg, &isnum);
445 if (!isnum) { 445 if (l_unlikely(!isnum)) {
446 interror(L, arg); 446 interror(L, arg);
447 } 447 }
448 return d; 448 return d;
@@ -475,7 +475,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
475 lua_Alloc allocf = lua_getallocf(L, &ud); 475 lua_Alloc allocf = lua_getallocf(L, &ud);
476 UBox *box = (UBox *)lua_touserdata(L, idx); 476 UBox *box = (UBox *)lua_touserdata(L, idx);
477 void *temp = allocf(ud, box->box, box->bsize, newsize); 477 void *temp = allocf(ud, box->box, box->bsize, newsize);
478 if (temp == NULL && newsize > 0) { /* allocation error? */ 478 if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
479 lua_pushliteral(L, "not enough memory"); 479 lua_pushliteral(L, "not enough memory");
480 lua_error(L); /* raise a memory error */ 480 lua_error(L); /* raise a memory error */
481 } 481 }
@@ -521,7 +521,7 @@ static void newbox (lua_State *L) {
521*/ 521*/
522static size_t newbuffsize (luaL_Buffer *B, size_t sz) { 522static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
523 size_t newsize = B->size * 2; /* double buffer size */ 523 size_t newsize = B->size * 2; /* double buffer size */
524 if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ 524 if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
525 return luaL_error(B->L, "buffer too large"); 525 return luaL_error(B->L, "buffer too large");
526 if (newsize < B->n + sz) /* double is not big enough? */ 526 if (newsize < B->n + sz) /* double is not big enough? */
527 newsize = B->n + sz; 527 newsize = B->n + sz;
@@ -861,7 +861,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
861 int isnum; 861 int isnum;
862 lua_len(L, idx); 862 lua_len(L, idx);
863 l = lua_tointegerx(L, -1, &isnum); 863 l = lua_tointegerx(L, -1, &isnum);
864 if (!isnum) 864 if (l_unlikely(!isnum))
865 luaL_error(L, "object length is not an integer"); 865 luaL_error(L, "object length is not an integer");
866 lua_pop(L, 1); /* remove object */ 866 lua_pop(L, 1); /* remove object */
867 return l; 867 return l;
@@ -1074,7 +1074,7 @@ static void warnfon (void *ud, const char *message, int tocont) {
1074 1074
1075LUALIB_API lua_State *luaL_newstate (void) { 1075LUALIB_API lua_State *luaL_newstate (void) {
1076 lua_State *L = lua_newstate(l_alloc, NULL); 1076 lua_State *L = lua_newstate(l_alloc, NULL);
1077 if (L) { 1077 if (l_likely(L)) {
1078 lua_atpanic(L, &panic); 1078 lua_atpanic(L, &panic);
1079 lua_setwarnf(L, warnfoff, L); /* default is warnings off */ 1079 lua_setwarnf(L, warnfoff, L); /* default is warnings off */
1080 } 1080 }
diff --git a/lauxlib.h b/lauxlib.h
index 65714911..9058e262 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -122,6 +122,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
122** =============================================================== 122** ===============================================================
123*/ 123*/
124 124
125#if !defined(l_likely)
126#define l_likely(x) x
127#endif
128
125 129
126#define luaL_newlibtable(L,l) \ 130#define luaL_newlibtable(L,l) \
127 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 131 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
@@ -130,10 +134,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
130 (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 134 (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
131 135
132#define luaL_argcheck(L, cond,arg,extramsg) \ 136#define luaL_argcheck(L, cond,arg,extramsg) \
133 ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) 137 ((void)(l_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
134 138
135#define luaL_argexpected(L,cond,arg,tname) \ 139#define luaL_argexpected(L,cond,arg,tname) \
136 ((void)((cond) || luaL_typeerror(L, (arg), (tname)))) 140 ((void)(l_likely(cond) || luaL_typeerror(L, (arg), (tname))))
137 141
138#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 142#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
139#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 143#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
diff --git a/lbaselib.c b/lbaselib.c
index 60786b3d..83ad306d 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -138,7 +138,7 @@ static int luaB_setmetatable (lua_State *L) {
138 int t = lua_type(L, 2); 138 int t = lua_type(L, 2);
139 luaL_checktype(L, 1, LUA_TTABLE); 139 luaL_checktype(L, 1, LUA_TTABLE);
140 luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); 140 luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
141 if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) 141 if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL))
142 return luaL_error(L, "cannot change a protected metatable"); 142 return luaL_error(L, "cannot change a protected metatable");
143 lua_settop(L, 2); 143 lua_settop(L, 2);
144 lua_setmetatable(L, 1); 144 lua_setmetatable(L, 1);
@@ -300,7 +300,7 @@ static int luaB_ipairs (lua_State *L) {
300 300
301 301
302static int load_aux (lua_State *L, int status, int envidx) { 302static int load_aux (lua_State *L, int status, int envidx) {
303 if (status == LUA_OK) { 303 if (l_likely(status == LUA_OK)) {
304 if (envidx != 0) { /* 'env' parameter? */ 304 if (envidx != 0) { /* 'env' parameter? */
305 lua_pushvalue(L, envidx); /* environment for loaded function */ 305 lua_pushvalue(L, envidx); /* environment for loaded function */
306 if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ 306 if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
@@ -356,7 +356,7 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
356 *size = 0; 356 *size = 0;
357 return NULL; 357 return NULL;
358 } 358 }
359 else if (!lua_isstring(L, -1)) 359 else if (l_unlikely(!lua_isstring(L, -1)))
360 luaL_error(L, "reader function must return a string"); 360 luaL_error(L, "reader function must return a string");
361 lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ 361 lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
362 return lua_tolstring(L, RESERVEDSLOT, size); 362 return lua_tolstring(L, RESERVEDSLOT, size);
@@ -394,7 +394,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
394static int luaB_dofile (lua_State *L) { 394static int luaB_dofile (lua_State *L) {
395 const char *fname = luaL_optstring(L, 1, NULL); 395 const char *fname = luaL_optstring(L, 1, NULL);
396 lua_settop(L, 1); 396 lua_settop(L, 1);
397 if (luaL_loadfile(L, fname) != LUA_OK) 397 if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK))
398 return lua_error(L); 398 return lua_error(L);
399 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); 399 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
400 return dofilecont(L, 0, 0); 400 return dofilecont(L, 0, 0);
@@ -402,7 +402,7 @@ static int luaB_dofile (lua_State *L) {
402 402
403 403
404static int luaB_assert (lua_State *L) { 404static int luaB_assert (lua_State *L) {
405 if (lua_toboolean(L, 1)) /* condition is true? */ 405 if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
406 return lua_gettop(L); /* return all arguments */ 406 return lua_gettop(L); /* return all arguments */
407 else { /* error */ 407 else { /* error */
408 luaL_checkany(L, 1); /* there must be a condition */ 408 luaL_checkany(L, 1); /* there must be a condition */
@@ -438,7 +438,7 @@ static int luaB_select (lua_State *L) {
438** ignored). 438** ignored).
439*/ 439*/
440static int finishpcall (lua_State *L, int status, lua_KContext extra) { 440static int finishpcall (lua_State *L, int status, lua_KContext extra) {
441 if (status != LUA_OK && status != LUA_YIELD) { /* error? */ 441 if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */
442 lua_pushboolean(L, 0); /* first result (false) */ 442 lua_pushboolean(L, 0); /* first result (false) */
443 lua_pushvalue(L, -2); /* error message */ 443 lua_pushvalue(L, -2); /* error message */
444 return 2; /* return false, msg */ 444 return 2; /* return false, msg */
diff --git a/lcorolib.c b/lcorolib.c
index ed7c58b2..fedbebec 100644
--- a/lcorolib.c
+++ b/lcorolib.c
@@ -31,14 +31,14 @@ static lua_State *getco (lua_State *L) {
31*/ 31*/
32static int auxresume (lua_State *L, lua_State *co, int narg) { 32static int auxresume (lua_State *L, lua_State *co, int narg) {
33 int status, nres; 33 int status, nres;
34 if (!lua_checkstack(co, narg)) { 34 if (l_unlikely(!lua_checkstack(co, narg))) {
35 lua_pushliteral(L, "too many arguments to resume"); 35 lua_pushliteral(L, "too many arguments to resume");
36 return -1; /* error flag */ 36 return -1; /* error flag */
37 } 37 }
38 lua_xmove(L, co, narg); 38 lua_xmove(L, co, narg);
39 status = lua_resume(co, L, narg, &nres); 39 status = lua_resume(co, L, narg, &nres);
40 if (status == LUA_OK || status == LUA_YIELD) { 40 if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
41 if (!lua_checkstack(L, nres + 1)) { 41 if (l_unlikely(!lua_checkstack(L, nres + 1))) {
42 lua_pop(co, nres); /* remove results anyway */ 42 lua_pop(co, nres); /* remove results anyway */
43 lua_pushliteral(L, "too many results to resume"); 43 lua_pushliteral(L, "too many results to resume");
44 return -1; /* error flag */ 44 return -1; /* error flag */
@@ -57,7 +57,7 @@ static int luaB_coresume (lua_State *L) {
57 lua_State *co = getco(L); 57 lua_State *co = getco(L);
58 int r; 58 int r;
59 r = auxresume(L, co, lua_gettop(L) - 1); 59 r = auxresume(L, co, lua_gettop(L) - 1);
60 if (r < 0) { 60 if (l_unlikely(r < 0)) {
61 lua_pushboolean(L, 0); 61 lua_pushboolean(L, 0);
62 lua_insert(L, -2); 62 lua_insert(L, -2);
63 return 2; /* return false + error message */ 63 return 2; /* return false + error message */
@@ -73,7 +73,7 @@ static int luaB_coresume (lua_State *L) {
73static int luaB_auxwrap (lua_State *L) { 73static int luaB_auxwrap (lua_State *L) {
74 lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 74 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
75 int r = auxresume(L, co, lua_gettop(L)); 75 int r = auxresume(L, co, lua_gettop(L));
76 if (r < 0) { /* error? */ 76 if (l_unlikely(r < 0)) { /* error? */
77 int stat = lua_status(co); 77 int stat = lua_status(co);
78 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ 78 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
79 stat = lua_resetthread(co); /* close its tbc variables */ 79 stat = lua_resetthread(co); /* close its tbc variables */
diff --git a/ldblib.c b/ldblib.c
index de6e38b3..6dcbaa98 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -33,7 +33,7 @@ static const char *const HOOKKEY = "_HOOKKEY";
33** checked. 33** checked.
34*/ 34*/
35static void checkstack (lua_State *L, lua_State *L1, int n) { 35static void checkstack (lua_State *L, lua_State *L1, int n) {
36 if (L != L1 && !lua_checkstack(L1, n)) 36 if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
37 luaL_error(L, "stack overflow"); 37 luaL_error(L, "stack overflow");
38} 38}
39 39
@@ -213,7 +213,7 @@ static int db_getlocal (lua_State *L) {
213 lua_Debug ar; 213 lua_Debug ar;
214 const char *name; 214 const char *name;
215 int level = (int)luaL_checkinteger(L, arg + 1); 215 int level = (int)luaL_checkinteger(L, arg + 1);
216 if (!lua_getstack(L1, level, &ar)) /* out of range? */ 216 if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
217 return luaL_argerror(L, arg+1, "level out of range"); 217 return luaL_argerror(L, arg+1, "level out of range");
218 checkstack(L, L1, 1); 218 checkstack(L, L1, 1);
219 name = lua_getlocal(L1, &ar, nvar); 219 name = lua_getlocal(L1, &ar, nvar);
@@ -238,7 +238,7 @@ static int db_setlocal (lua_State *L) {
238 lua_Debug ar; 238 lua_Debug ar;
239 int level = (int)luaL_checkinteger(L, arg + 1); 239 int level = (int)luaL_checkinteger(L, arg + 1);
240 int nvar = (int)luaL_checkinteger(L, arg + 2); 240 int nvar = (int)luaL_checkinteger(L, arg + 2);
241 if (!lua_getstack(L1, level, &ar)) /* out of range? */ 241 if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
242 return luaL_argerror(L, arg+1, "level out of range"); 242 return luaL_argerror(L, arg+1, "level out of range");
243 luaL_checkany(L, arg+3); 243 luaL_checkany(L, arg+3);
244 lua_settop(L, arg+3); 244 lua_settop(L, arg+3);
diff --git a/ldo.c b/ldo.c
index 5587b602..7c9ce06e 100644
--- a/ldo.c
+++ b/ldo.c
@@ -184,7 +184,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
184 StkId newstack = luaM_reallocvector(L, L->stack, 184 StkId newstack = luaM_reallocvector(L, L->stack,
185 lim + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); 185 lim + EXTRA_STACK, newsize + EXTRA_STACK, StackValue);
186 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); 186 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
187 if (unlikely(newstack == NULL)) { /* reallocation failed? */ 187 if (l_unlikely(newstack == NULL)) { /* reallocation failed? */
188 if (raiseerror) 188 if (raiseerror)
189 luaM_error(L); 189 luaM_error(L);
190 else return 0; /* do not raise an error */ 190 else return 0; /* do not raise an error */
@@ -204,7 +204,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
204*/ 204*/
205int luaD_growstack (lua_State *L, int n, int raiseerror) { 205int luaD_growstack (lua_State *L, int n, int raiseerror) {
206 int size = stacksize(L); 206 int size = stacksize(L);
207 if (unlikely(size > LUAI_MAXSTACK)) { 207 if (l_unlikely(size > LUAI_MAXSTACK)) {
208 /* if stack is larger than maximum, thread is already using the 208 /* if stack is larger than maximum, thread is already using the
209 extra space reserved for errors, that is, thread is handling 209 extra space reserved for errors, that is, thread is handling
210 a stack error; cannot grow further than that. */ 210 a stack error; cannot grow further than that. */
@@ -220,7 +220,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) {
220 newsize = LUAI_MAXSTACK; 220 newsize = LUAI_MAXSTACK;
221 if (newsize < needed) /* but must respect what was asked for */ 221 if (newsize < needed) /* but must respect what was asked for */
222 newsize = needed; 222 newsize = needed;
223 if (likely(newsize <= LUAI_MAXSTACK)) 223 if (l_likely(newsize <= LUAI_MAXSTACK))
224 return luaD_reallocstack(L, newsize, raiseerror); 224 return luaD_reallocstack(L, newsize, raiseerror);
225 else { /* stack overflow */ 225 else { /* stack overflow */
226 /* add extra size to be able to handle the error message */ 226 /* add extra size to be able to handle the error message */
@@ -376,7 +376,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) {
376void luaD_tryfuncTM (lua_State *L, StkId func) { 376void luaD_tryfuncTM (lua_State *L, StkId func) {
377 const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); 377 const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);
378 StkId p; 378 StkId p;
379 if (unlikely(ttisnil(tm))) 379 if (l_unlikely(ttisnil(tm)))
380 luaG_callerror(L, s2v(func)); /* nothing to call */ 380 luaG_callerror(L, s2v(func)); /* nothing to call */
381 for (p = L->top; p > func; p--) /* open space for metamethod */ 381 for (p = L->top; p > func; p--) /* open space for metamethod */
382 setobjs2s(L, p, p-1); 382 setobjs2s(L, p, p-1);
@@ -444,7 +444,7 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) {
444*/ 444*/
445void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { 445void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
446 int wanted = ci->nresults; 446 int wanted = ci->nresults;
447 if (L->hookmask && !hastocloseCfunc(wanted)) 447 if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted)))
448 rethook(L, ci, nres); 448 rethook(L, ci, nres);
449 /* move results to proper place */ 449 /* move results to proper place */
450 moveresults(L, ci->func, nres, wanted); 450 moveresults(L, ci->func, nres, wanted);
@@ -510,7 +510,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
510 ci->top = L->top + LUA_MINSTACK; 510 ci->top = L->top + LUA_MINSTACK;
511 ci->func = func; 511 ci->func = func;
512 lua_assert(ci->top <= L->stack_last); 512 lua_assert(ci->top <= L->stack_last);
513 if (L->hookmask & LUA_MASKCALL) { 513 if (l_unlikely(L->hookmask & LUA_MASKCALL)) {
514 int narg = cast_int(L->top - func) - 1; 514 int narg = cast_int(L->top - func) - 1;
515 luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); 515 luaD_hook(L, LUA_HOOKCALL, -1, 1, narg);
516 } 516 }
@@ -556,7 +556,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
556static void ccall (lua_State *L, StkId func, int nResults, int inc) { 556static void ccall (lua_State *L, StkId func, int nResults, int inc) {
557 CallInfo *ci; 557 CallInfo *ci;
558 L->nCcalls += inc; 558 L->nCcalls += inc;
559 if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) 559 if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
560 luaE_checkcstack(L); 560 luaE_checkcstack(L);
561 if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ 561 if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */
562 ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ 562 ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */
@@ -600,7 +600,7 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
600*/ 600*/
601static int finishpcallk (lua_State *L, CallInfo *ci) { 601static int finishpcallk (lua_State *L, CallInfo *ci) {
602 int status = getcistrecst(ci); /* get original status */ 602 int status = getcistrecst(ci); /* get original status */
603 if (status == LUA_OK) /* no error? */ 603 if (l_likely(status == LUA_OK)) /* no error? */
604 status = LUA_YIELD; /* was interrupted by an yield */ 604 status = LUA_YIELD; /* was interrupted by an yield */
605 else { /* error */ 605 else { /* error */
606 StkId func = restorestack(L, ci->u2.funcidx); 606 StkId func = restorestack(L, ci->u2.funcidx);
@@ -774,7 +774,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
774 status = luaD_rawrunprotected(L, resume, &nargs); 774 status = luaD_rawrunprotected(L, resume, &nargs);
775 /* continue running after recoverable errors */ 775 /* continue running after recoverable errors */
776 status = precover(L, status); 776 status = precover(L, status);
777 if (likely(!errorstatus(status))) 777 if (l_likely(!errorstatus(status)))
778 lua_assert(status == L->status); /* normal end or yield */ 778 lua_assert(status == L->status); /* normal end or yield */
779 else { /* unrecoverable error */ 779 else { /* unrecoverable error */
780 L->status = cast_byte(status); /* mark thread as 'dead' */ 780 L->status = cast_byte(status); /* mark thread as 'dead' */
@@ -800,7 +800,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
800 lua_lock(L); 800 lua_lock(L);
801 ci = L->ci; 801 ci = L->ci;
802 api_checknelems(L, nresults); 802 api_checknelems(L, nresults);
803 if (unlikely(!yieldable(L))) { 803 if (l_unlikely(!yieldable(L))) {
804 if (L != G(L)->mainthread) 804 if (L != G(L)->mainthread)
805 luaG_runerror(L, "attempt to yield across a C-call boundary"); 805 luaG_runerror(L, "attempt to yield across a C-call boundary");
806 else 806 else
@@ -853,7 +853,7 @@ int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
853 struct CloseP pcl; 853 struct CloseP pcl;
854 pcl.level = restorestack(L, level); pcl.status = status; 854 pcl.level = restorestack(L, level); pcl.status = status;
855 status = luaD_rawrunprotected(L, &closepaux, &pcl); 855 status = luaD_rawrunprotected(L, &closepaux, &pcl);
856 if (likely(status == LUA_OK)) /* no more errors? */ 856 if (l_likely(status == LUA_OK)) /* no more errors? */
857 return pcl.status; 857 return pcl.status;
858 else { /* an error occurred; restore saved state and repeat */ 858 else { /* an error occurred; restore saved state and repeat */
859 L->ci = old_ci; 859 L->ci = old_ci;
@@ -876,7 +876,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
876 ptrdiff_t old_errfunc = L->errfunc; 876 ptrdiff_t old_errfunc = L->errfunc;
877 L->errfunc = ef; 877 L->errfunc = ef;
878 status = luaD_rawrunprotected(L, func, u); 878 status = luaD_rawrunprotected(L, func, u);
879 if (unlikely(status != LUA_OK)) { /* an error occurred? */ 879 if (l_unlikely(status != LUA_OK)) { /* an error occurred? */
880 L->ci = old_ci; 880 L->ci = old_ci;
881 L->allowhook = old_allowhooks; 881 L->allowhook = old_allowhooks;
882 status = luaD_closeprotected(L, old_top, status); 882 status = luaD_closeprotected(L, old_top, status);
diff --git a/ldo.h b/ldo.h
index c7721d62..6bf0ed86 100644
--- a/ldo.h
+++ b/ldo.h
@@ -23,7 +23,7 @@
23** at every check. 23** at every check.
24*/ 24*/
25#define luaD_checkstackaux(L,n,pre,pos) \ 25#define luaD_checkstackaux(L,n,pre,pos) \
26 if (L->stack_last - L->top <= (n)) \ 26 if (l_unlikely(L->stack_last - L->top <= (n))) \
27 { pre; luaD_growstack(L, n, 1); pos; } \ 27 { pre; luaD_growstack(L, n, 1); pos; } \
28 else { condmovestack(L,pre,pos); } 28 else { condmovestack(L,pre,pos); }
29 29
diff --git a/lgc.c b/lgc.c
index bab9beb1..94e0486e 100644
--- a/lgc.c
+++ b/lgc.c
@@ -916,7 +916,7 @@ static void GCTM (lua_State *L) {
916 L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ 916 L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */
917 L->allowhook = oldah; /* restore hooks */ 917 L->allowhook = oldah; /* restore hooks */
918 g->gcrunning = running; /* restore state */ 918 g->gcrunning = running; /* restore state */
919 if (unlikely(status != LUA_OK)) { /* error while running __gc? */ 919 if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */
920 luaE_warnerror(L, "__gc metamethod"); 920 luaE_warnerror(L, "__gc metamethod");
921 L->top--; /* pops error object */ 921 L->top--; /* pops error object */
922 } 922 }
diff --git a/liolib.c b/liolib.c
index 79516724..b08397da 100644
--- a/liolib.c
+++ b/liolib.c
@@ -186,7 +186,7 @@ static int f_tostring (lua_State *L) {
186 186
187static FILE *tofile (lua_State *L) { 187static FILE *tofile (lua_State *L) {
188 LStream *p = tolstream(L); 188 LStream *p = tolstream(L);
189 if (isclosed(p)) 189 if (l_unlikely(isclosed(p)))
190 luaL_error(L, "attempt to use a closed file"); 190 luaL_error(L, "attempt to use a closed file");
191 lua_assert(p->f); 191 lua_assert(p->f);
192 return p->f; 192 return p->f;
@@ -261,7 +261,7 @@ static LStream *newfile (lua_State *L) {
261static void opencheck (lua_State *L, const char *fname, const char *mode) { 261static void opencheck (lua_State *L, const char *fname, const char *mode) {
262 LStream *p = newfile(L); 262 LStream *p = newfile(L);
263 p->f = fopen(fname, mode); 263 p->f = fopen(fname, mode);
264 if (p->f == NULL) 264 if (l_unlikely(p->f == NULL))
265 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); 265 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
266} 266}
267 267
@@ -309,7 +309,7 @@ static FILE *getiofile (lua_State *L, const char *findex) {
309 LStream *p; 309 LStream *p;
310 lua_getfield(L, LUA_REGISTRYINDEX, findex); 310 lua_getfield(L, LUA_REGISTRYINDEX, findex);
311 p = (LStream *)lua_touserdata(L, -1); 311 p = (LStream *)lua_touserdata(L, -1);
312 if (isclosed(p)) 312 if (l_unlikely(isclosed(p)))
313 luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); 313 luaL_error(L, "default %s file is closed", findex + IOPREF_LEN);
314 return p->f; 314 return p->f;
315} 315}
@@ -436,7 +436,7 @@ typedef struct {
436** Add current char to buffer (if not out of space) and read next one 436** Add current char to buffer (if not out of space) and read next one
437*/ 437*/
438static int nextc (RN *rn) { 438static int nextc (RN *rn) {
439 if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ 439 if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */
440 rn->buff[0] = '\0'; /* invalidate result */ 440 rn->buff[0] = '\0'; /* invalidate result */
441 return 0; /* fail */ 441 return 0; /* fail */
442 } 442 }
@@ -499,8 +499,8 @@ static int read_number (lua_State *L, FILE *f) {
499 ungetc(rn.c, rn.f); /* unread look-ahead char */ 499 ungetc(rn.c, rn.f); /* unread look-ahead char */
500 l_unlockfile(rn.f); 500 l_unlockfile(rn.f);
501 rn.buff[rn.n] = '\0'; /* finish string */ 501 rn.buff[rn.n] = '\0'; /* finish string */
502 if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ 502 if (l_likely(lua_stringtonumber(L, rn.buff)))
503 return 1; /* ok */ 503 return 1; /* ok, it is a valid number */
504 else { /* invalid format */ 504 else { /* invalid format */
505 lua_pushnil(L); /* "result" to be removed */ 505 lua_pushnil(L); /* "result" to be removed */
506 return 0; /* read fails */ 506 return 0; /* read fails */
@@ -676,7 +676,8 @@ static int g_write (lua_State *L, FILE *f, int arg) {
676 status = status && (fwrite(s, sizeof(char), l, f) == l); 676 status = status && (fwrite(s, sizeof(char), l, f) == l);
677 } 677 }
678 } 678 }
679 if (status) return 1; /* file handle already on stack top */ 679 if (l_likely(status))
680 return 1; /* file handle already on stack top */
680 else return luaL_fileresult(L, status, NULL); 681 else return luaL_fileresult(L, status, NULL);
681} 682}
682 683
@@ -703,7 +704,7 @@ static int f_seek (lua_State *L) {
703 luaL_argcheck(L, (lua_Integer)offset == p3, 3, 704 luaL_argcheck(L, (lua_Integer)offset == p3, 3,
704 "not an integer in proper range"); 705 "not an integer in proper range");
705 op = l_fseek(f, offset, mode[op]); 706 op = l_fseek(f, offset, mode[op]);
706 if (op) 707 if (l_unlikely(op))
707 return luaL_fileresult(L, 0, NULL); /* error */ 708 return luaL_fileresult(L, 0, NULL); /* error */
708 else { 709 else {
709 lua_pushinteger(L, (lua_Integer)l_ftell(f)); 710 lua_pushinteger(L, (lua_Integer)l_ftell(f));
diff --git a/llimits.h b/llimits.h
index d0394831..025f1c82 100644
--- a/llimits.h
+++ b/llimits.h
@@ -150,22 +150,6 @@ typedef LUAI_UACINT l_uacInt;
150 150
151 151
152/* 152/*
153** macros to improve jump prediction (used mainly for error handling)
154*/
155#if !defined(likely)
156
157#if defined(__GNUC__)
158#define likely(x) (__builtin_expect(((x) != 0), 1))
159#define unlikely(x) (__builtin_expect(((x) != 0), 0))
160#else
161#define likely(x) (x)
162#define unlikely(x) (x)
163#endif
164
165#endif
166
167
168/*
169** non-return type 153** non-return type
170*/ 154*/
171#if !defined(l_noret) 155#if !defined(l_noret)
diff --git a/lmathlib.c b/lmathlib.c
index 86def470..5f5983a4 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -73,7 +73,7 @@ static int math_atan (lua_State *L) {
73static int math_toint (lua_State *L) { 73static int math_toint (lua_State *L) {
74 int valid; 74 int valid;
75 lua_Integer n = lua_tointegerx(L, 1, &valid); 75 lua_Integer n = lua_tointegerx(L, 1, &valid);
76 if (valid) 76 if (l_likely(valid))
77 lua_pushinteger(L, n); 77 lua_pushinteger(L, n);
78 else { 78 else {
79 luaL_checkany(L, 1); 79 luaL_checkany(L, 1);
@@ -175,7 +175,8 @@ static int math_log (lua_State *L) {
175 lua_Number base = luaL_checknumber(L, 2); 175 lua_Number base = luaL_checknumber(L, 2);
176#if !defined(LUA_USE_C89) 176#if !defined(LUA_USE_C89)
177 if (base == l_mathop(2.0)) 177 if (base == l_mathop(2.0))
178 res = l_mathop(log2)(x); else 178 res = l_mathop(log2)(x);
179 else
179#endif 180#endif
180 if (base == l_mathop(10.0)) 181 if (base == l_mathop(10.0))
181 res = l_mathop(log10)(x); 182 res = l_mathop(log10)(x);
diff --git a/lmem.c b/lmem.c
index 4822a0ea..e90f991a 100644
--- a/lmem.c
+++ b/lmem.c
@@ -83,7 +83,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
83 if (nelems + 1 <= size) /* does one extra element still fit? */ 83 if (nelems + 1 <= size) /* does one extra element still fit? */
84 return block; /* nothing to be done */ 84 return block; /* nothing to be done */
85 if (size >= limit / 2) { /* cannot double it? */ 85 if (size >= limit / 2) { /* cannot double it? */
86 if (unlikely(size >= limit)) /* cannot grow even a little? */ 86 if (l_unlikely(size >= limit)) /* cannot grow even a little? */
87 luaG_runerror(L, "too many %s (limit is %d)", what, limit); 87 luaG_runerror(L, "too many %s (limit is %d)", what, limit);
88 size = limit; /* still have at least one free place */ 88 size = limit; /* still have at least one free place */
89 } 89 }
@@ -164,7 +164,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
164 global_State *g = G(L); 164 global_State *g = G(L);
165 lua_assert((osize == 0) == (block == NULL)); 165 lua_assert((osize == 0) == (block == NULL));
166 newblock = firsttry(g, block, osize, nsize); 166 newblock = firsttry(g, block, osize, nsize);
167 if (unlikely(newblock == NULL && nsize > 0)) { 167 if (l_unlikely(newblock == NULL && nsize > 0)) {
168 if (nsize > osize) /* not shrinking a block? */ 168 if (nsize > osize) /* not shrinking a block? */
169 newblock = tryagain(L, block, osize, nsize); 169 newblock = tryagain(L, block, osize, nsize);
170 if (newblock == NULL) /* still no memory? */ 170 if (newblock == NULL) /* still no memory? */
@@ -179,7 +179,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
179void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, 179void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
180 size_t nsize) { 180 size_t nsize) {
181 void *newblock = luaM_realloc_(L, block, osize, nsize); 181 void *newblock = luaM_realloc_(L, block, osize, nsize);
182 if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ 182 if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
183 luaM_error(L); 183 luaM_error(L);
184 return newblock; 184 return newblock;
185} 185}
@@ -191,7 +191,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
191 else { 191 else {
192 global_State *g = G(L); 192 global_State *g = G(L);
193 void *newblock = firsttry(g, NULL, tag, size); 193 void *newblock = firsttry(g, NULL, tag, size);
194 if (unlikely(newblock == NULL)) { 194 if (l_unlikely(newblock == NULL)) {
195 newblock = tryagain(L, NULL, tag, size); 195 newblock = tryagain(L, NULL, tag, size);
196 if (newblock == NULL) 196 if (newblock == NULL)
197 luaM_error(L); 197 luaM_error(L);
diff --git a/loadlib.c b/loadlib.c
index c0ec9a13..6f9fa373 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -132,14 +132,16 @@ static void lsys_unloadlib (void *lib) {
132 132
133static void *lsys_load (lua_State *L, const char *path, int seeglb) { 133static void *lsys_load (lua_State *L, const char *path, int seeglb) {
134 void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); 134 void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
135 if (lib == NULL) lua_pushstring(L, dlerror()); 135 if (l_unlikely(lib == NULL))
136 lua_pushstring(L, dlerror());
136 return lib; 137 return lib;
137} 138}
138 139
139 140
140static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { 141static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
141 lua_CFunction f = cast_func(dlsym(lib, sym)); 142 lua_CFunction f = cast_func(dlsym(lib, sym));
142 if (f == NULL) lua_pushstring(L, dlerror()); 143 if (l_unlikely(f == NULL))
144 lua_pushstring(L, dlerror());
143 return f; 145 return f;
144} 146}
145 147
@@ -410,7 +412,7 @@ static int ll_loadlib (lua_State *L) {
410 const char *path = luaL_checkstring(L, 1); 412 const char *path = luaL_checkstring(L, 1);
411 const char *init = luaL_checkstring(L, 2); 413 const char *init = luaL_checkstring(L, 2);
412 int stat = lookforfunc(L, path, init); 414 int stat = lookforfunc(L, path, init);
413 if (stat == 0) /* no errors? */ 415 if (l_likely(stat == 0)) /* no errors? */
414 return 1; /* return the loaded function */ 416 return 1; /* return the loaded function */
415 else { /* error; error message is on stack top */ 417 else { /* error; error message is on stack top */
416 luaL_pushfail(L); 418 luaL_pushfail(L);
@@ -523,14 +525,14 @@ static const char *findfile (lua_State *L, const char *name,
523 const char *path; 525 const char *path;
524 lua_getfield(L, lua_upvalueindex(1), pname); 526 lua_getfield(L, lua_upvalueindex(1), pname);
525 path = lua_tostring(L, -1); 527 path = lua_tostring(L, -1);
526 if (path == NULL) 528 if (l_unlikely(path == NULL))
527 luaL_error(L, "'package.%s' must be a string", pname); 529 luaL_error(L, "'package.%s' must be a string", pname);
528 return searchpath(L, name, path, ".", dirsep); 530 return searchpath(L, name, path, ".", dirsep);
529} 531}
530 532
531 533
532static int checkload (lua_State *L, int stat, const char *filename) { 534static int checkload (lua_State *L, int stat, const char *filename) {
533 if (stat) { /* module loaded successfully? */ 535 if (l_likely(stat)) { /* module loaded successfully? */
534 lua_pushstring(L, filename); /* will be 2nd argument to module */ 536 lua_pushstring(L, filename); /* will be 2nd argument to module */
535 return 2; /* return open function and file name */ 537 return 2; /* return open function and file name */
536 } 538 }
@@ -623,13 +625,14 @@ static void findloader (lua_State *L, const char *name) {
623 int i; 625 int i;
624 luaL_Buffer msg; /* to build error message */ 626 luaL_Buffer msg; /* to build error message */
625 /* push 'package.searchers' to index 3 in the stack */ 627 /* push 'package.searchers' to index 3 in the stack */
626 if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) 628 if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers")
629 != LUA_TTABLE))
627 luaL_error(L, "'package.searchers' must be a table"); 630 luaL_error(L, "'package.searchers' must be a table");
628 luaL_buffinit(L, &msg); 631 luaL_buffinit(L, &msg);
629 /* iterate over available searchers to find a loader */ 632 /* iterate over available searchers to find a loader */
630 for (i = 1; ; i++) { 633 for (i = 1; ; i++) {
631 luaL_addstring(&msg, "\n\t"); /* error-message prefix */ 634 luaL_addstring(&msg, "\n\t"); /* error-message prefix */
632 if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ 635 if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */
633 lua_pop(L, 1); /* remove nil */ 636 lua_pop(L, 1); /* remove nil */
634 luaL_buffsub(&msg, 2); /* remove prefix */ 637 luaL_buffsub(&msg, 2); /* remove prefix */
635 luaL_pushresult(&msg); /* create error message */ 638 luaL_pushresult(&msg); /* create error message */
diff --git a/loslib.c b/loslib.c
index e65e188b..3e20d622 100644
--- a/loslib.c
+++ b/loslib.c
@@ -170,7 +170,7 @@ static int os_tmpname (lua_State *L) {
170 char buff[LUA_TMPNAMBUFSIZE]; 170 char buff[LUA_TMPNAMBUFSIZE];
171 int err; 171 int err;
172 lua_tmpnam(buff, err); 172 lua_tmpnam(buff, err);
173 if (err) 173 if (l_unlikely(err))
174 return luaL_error(L, "unable to generate a unique filename"); 174 return luaL_error(L, "unable to generate a unique filename");
175 lua_pushstring(L, buff); 175 lua_pushstring(L, buff);
176 return 1; 176 return 1;
@@ -208,7 +208,7 @@ static int os_clock (lua_State *L) {
208*/ 208*/
209static void setfield (lua_State *L, const char *key, int value, int delta) { 209static void setfield (lua_State *L, const char *key, int value, int delta) {
210 #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) 210 #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
211 if (value > LUA_MAXINTEGER - delta) 211 if (l_unlikely(value > LUA_MAXINTEGER - delta))
212 luaL_error(L, "field '%s' is out-of-bound", key); 212 luaL_error(L, "field '%s' is out-of-bound", key);
213 #endif 213 #endif
214 lua_pushinteger(L, (lua_Integer)value + delta); 214 lua_pushinteger(L, (lua_Integer)value + delta);
@@ -253,9 +253,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
253 int t = lua_getfield(L, -1, key); /* get field and its type */ 253 int t = lua_getfield(L, -1, key); /* get field and its type */
254 lua_Integer res = lua_tointegerx(L, -1, &isnum); 254 lua_Integer res = lua_tointegerx(L, -1, &isnum);
255 if (!isnum) { /* field is not an integer? */ 255 if (!isnum) { /* field is not an integer? */
256 if (t != LUA_TNIL) /* some other value? */ 256 if (l_unlikely(t != LUA_TNIL)) /* some other value? */
257 return luaL_error(L, "field '%s' is not an integer", key); 257 return luaL_error(L, "field '%s' is not an integer", key);
258 else if (d < 0) /* absent field; no default? */ 258 else if (l_unlikely(d < 0)) /* absent field; no default? */
259 return luaL_error(L, "field '%s' missing in date table", key); 259 return luaL_error(L, "field '%s' missing in date table", key);
260 res = d; 260 res = d;
261 } 261 }
diff --git a/lparser.c b/lparser.c
index 249ba9a4..284ef1f0 100644
--- a/lparser.c
+++ b/lparser.c
@@ -128,7 +128,7 @@ static void checknext (LexState *ls, int c) {
128** in line 'where' (if that is not the current line). 128** in line 'where' (if that is not the current line).
129*/ 129*/
130static void check_match (LexState *ls, int what, int who, int where) { 130static void check_match (LexState *ls, int what, int who, int where) {
131 if (unlikely(!testnext(ls, what))) { 131 if (l_unlikely(!testnext(ls, what))) {
132 if (where == ls->linenumber) /* all in the same line? */ 132 if (where == ls->linenumber) /* all in the same line? */
133 error_expected(ls, what); /* do not need a complex message */ 133 error_expected(ls, what); /* do not need a complex message */
134 else { 134 else {
@@ -517,7 +517,7 @@ static void solvegoto (LexState *ls, int g, Labeldesc *label) {
517 Labellist *gl = &ls->dyd->gt; /* list of goto's */ 517 Labellist *gl = &ls->dyd->gt; /* list of goto's */
518 Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ 518 Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */
519 lua_assert(eqstr(gt->name, label->name)); 519 lua_assert(eqstr(gt->name, label->name));
520 if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ 520 if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */
521 jumpscopeerror(ls, gt); 521 jumpscopeerror(ls, gt);
522 luaK_patchlist(ls->fs, gt->pc, label->pc); 522 luaK_patchlist(ls->fs, gt->pc, label->pc);
523 for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ 523 for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */
@@ -1435,7 +1435,7 @@ static void breakstat (LexState *ls) {
1435*/ 1435*/
1436static void checkrepeated (LexState *ls, TString *name) { 1436static void checkrepeated (LexState *ls, TString *name) {
1437 Labeldesc *lb = findlabel(ls, name); 1437 Labeldesc *lb = findlabel(ls, name);
1438 if (unlikely(lb != NULL)) { /* already defined? */ 1438 if (l_unlikely(lb != NULL)) { /* already defined? */
1439 const char *msg = "label '%s' already defined on line %d"; 1439 const char *msg = "label '%s' already defined on line %d";
1440 msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); 1440 msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line);
1441 luaK_semerror(ls, msg); /* error */ 1441 luaK_semerror(ls, msg); /* error */
@@ -1520,7 +1520,7 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) {
1520 int offset = dest - (pc + 1); 1520 int offset = dest - (pc + 1);
1521 if (back) 1521 if (back)
1522 offset = -offset; 1522 offset = -offset;
1523 if (unlikely(offset > MAXARG_Bx)) 1523 if (l_unlikely(offset > MAXARG_Bx))
1524 luaX_syntaxerror(fs->ls, "control structure too long"); 1524 luaX_syntaxerror(fs->ls, "control structure too long");
1525 SETARG_Bx(*jmp, offset); 1525 SETARG_Bx(*jmp, offset);
1526} 1526}
diff --git a/lstate.c b/lstate.c
index 38078521..04909db3 100644
--- a/lstate.c
+++ b/lstate.c
@@ -172,7 +172,7 @@ void luaE_checkcstack (lua_State *L) {
172 172
173LUAI_FUNC void luaE_incCstack (lua_State *L) { 173LUAI_FUNC void luaE_incCstack (lua_State *L) {
174 L->nCcalls++; 174 L->nCcalls++;
175 if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) 175 if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
176 luaE_checkcstack(L); 176 luaE_checkcstack(L);
177} 177}
178 178
diff --git a/lstring.c b/lstring.c
index 138871c7..13dcaf42 100644
--- a/lstring.c
+++ b/lstring.c
@@ -89,7 +89,7 @@ void luaS_resize (lua_State *L, int nsize) {
89 if (nsize < osize) /* shrinking table? */ 89 if (nsize < osize) /* shrinking table? */
90 tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ 90 tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
91 newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); 91 newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
92 if (unlikely(newvect == NULL)) { /* reallocation failed? */ 92 if (l_unlikely(newvect == NULL)) { /* reallocation failed? */
93 if (nsize < osize) /* was it shrinking table? */ 93 if (nsize < osize) /* was it shrinking table? */
94 tablerehash(tb->hash, nsize, osize); /* restore to original size */ 94 tablerehash(tb->hash, nsize, osize); /* restore to original size */
95 /* leave table as it was */ 95 /* leave table as it was */
@@ -172,7 +172,7 @@ void luaS_remove (lua_State *L, TString *ts) {
172 172
173 173
174static void growstrtab (lua_State *L, stringtable *tb) { 174static void growstrtab (lua_State *L, stringtable *tb) {
175 if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ 175 if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
176 luaC_fullgc(L, 1); /* try to free some... */ 176 luaC_fullgc(L, 1); /* try to free some... */
177 if (tb->nuse == MAX_INT) /* still too many? */ 177 if (tb->nuse == MAX_INT) /* still too many? */
178 luaM_error(L); /* cannot even create a message... */ 178 luaM_error(L); /* cannot even create a message... */
@@ -223,7 +223,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
223 return internshrstr(L, str, l); 223 return internshrstr(L, str, l);
224 else { 224 else {
225 TString *ts; 225 TString *ts;
226 if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) 226 if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
227 luaM_toobig(L); 227 luaM_toobig(L);
228 ts = luaS_createlngstrobj(L, l); 228 ts = luaS_createlngstrobj(L, l);
229 memcpy(getstr(ts), str, l * sizeof(char)); 229 memcpy(getstr(ts), str, l * sizeof(char));
@@ -259,7 +259,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
259 Udata *u; 259 Udata *u;
260 int i; 260 int i;
261 GCObject *o; 261 GCObject *o;
262 if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) 262 if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
263 luaM_toobig(L); 263 luaM_toobig(L);
264 o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); 264 o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
265 u = gco2u(o); 265 u = gco2u(o);
diff --git a/lstrlib.c b/lstrlib.c
index c7242ea4..47e5b27a 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -152,8 +152,9 @@ static int str_rep (lua_State *L) {
152 const char *s = luaL_checklstring(L, 1, &l); 152 const char *s = luaL_checklstring(L, 1, &l);
153 lua_Integer n = luaL_checkinteger(L, 2); 153 lua_Integer n = luaL_checkinteger(L, 2);
154 const char *sep = luaL_optlstring(L, 3, "", &lsep); 154 const char *sep = luaL_optlstring(L, 3, "", &lsep);
155 if (n <= 0) lua_pushliteral(L, ""); 155 if (n <= 0)
156 else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */ 156 lua_pushliteral(L, "");
157 else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n))
157 return luaL_error(L, "resulting string too large"); 158 return luaL_error(L, "resulting string too large");
158 else { 159 else {
159 size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; 160 size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
@@ -181,7 +182,7 @@ static int str_byte (lua_State *L) {
181 size_t pose = getendpos(L, 3, pi, l); 182 size_t pose = getendpos(L, 3, pi, l);
182 int n, i; 183 int n, i;
183 if (posi > pose) return 0; /* empty interval; return no values */ 184 if (posi > pose) return 0; /* empty interval; return no values */
184 if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */ 185 if (l_unlikely(pose - posi >= (size_t)INT_MAX)) /* arithmetic overflow? */
185 return luaL_error(L, "string slice too long"); 186 return luaL_error(L, "string slice too long");
186 n = (int)(pose - posi) + 1; 187 n = (int)(pose - posi) + 1;
187 luaL_checkstack(L, n, "string slice too long"); 188 luaL_checkstack(L, n, "string slice too long");
@@ -235,7 +236,7 @@ static int str_dump (lua_State *L) {
235 luaL_checktype(L, 1, LUA_TFUNCTION); 236 luaL_checktype(L, 1, LUA_TFUNCTION);
236 lua_settop(L, 1); /* ensure function is on the top of the stack */ 237 lua_settop(L, 1); /* ensure function is on the top of the stack */
237 state.init = 0; 238 state.init = 0;
238 if (lua_dump(L, writer, &state, strip) != 0) 239 if (l_unlikely(lua_dump(L, writer, &state, strip) != 0))
239 return luaL_error(L, "unable to dump given function"); 240 return luaL_error(L, "unable to dump given function");
240 luaL_pushresult(&state.B); 241 luaL_pushresult(&state.B);
241 return 1; 242 return 1;
@@ -275,7 +276,8 @@ static int tonum (lua_State *L, int arg) {
275 276
276static void trymt (lua_State *L, const char *mtname) { 277static void trymt (lua_State *L, const char *mtname) {
277 lua_settop(L, 2); /* back to the original arguments */ 278 lua_settop(L, 2); /* back to the original arguments */
278 if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) 279 if (l_unlikely(lua_type(L, 2) == LUA_TSTRING ||
280 !luaL_getmetafield(L, 2, mtname)))
279 luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, 281 luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2,
280 luaL_typename(L, -2), luaL_typename(L, -1)); 282 luaL_typename(L, -2), luaL_typename(L, -1));
281 lua_insert(L, -3); /* put metamethod before arguments */ 283 lua_insert(L, -3); /* put metamethod before arguments */
@@ -383,7 +385,8 @@ static const char *match (MatchState *ms, const char *s, const char *p);
383 385
384static int check_capture (MatchState *ms, int l) { 386static int check_capture (MatchState *ms, int l) {
385 l -= '1'; 387 l -= '1';
386 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) 388 if (l_unlikely(l < 0 || l >= ms->level ||
389 ms->capture[l].len == CAP_UNFINISHED))
387 return luaL_error(ms->L, "invalid capture index %%%d", l + 1); 390 return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
388 return l; 391 return l;
389} 392}
@@ -400,14 +403,14 @@ static int capture_to_close (MatchState *ms) {
400static const char *classend (MatchState *ms, const char *p) { 403static const char *classend (MatchState *ms, const char *p) {
401 switch (*p++) { 404 switch (*p++) {
402 case L_ESC: { 405 case L_ESC: {
403 if (p == ms->p_end) 406 if (l_unlikely(p == ms->p_end))
404 luaL_error(ms->L, "malformed pattern (ends with '%%')"); 407 luaL_error(ms->L, "malformed pattern (ends with '%%')");
405 return p+1; 408 return p+1;
406 } 409 }
407 case '[': { 410 case '[': {
408 if (*p == '^') p++; 411 if (*p == '^') p++;
409 do { /* look for a ']' */ 412 do { /* look for a ']' */
410 if (p == ms->p_end) 413 if (l_unlikely(p == ms->p_end))
411 luaL_error(ms->L, "malformed pattern (missing ']')"); 414 luaL_error(ms->L, "malformed pattern (missing ']')");
412 if (*(p++) == L_ESC && p < ms->p_end) 415 if (*(p++) == L_ESC && p < ms->p_end)
413 p++; /* skip escapes (e.g. '%]') */ 416 p++; /* skip escapes (e.g. '%]') */
@@ -482,7 +485,7 @@ static int singlematch (MatchState *ms, const char *s, const char *p,
482 485
483static const char *matchbalance (MatchState *ms, const char *s, 486static const char *matchbalance (MatchState *ms, const char *s,
484 const char *p) { 487 const char *p) {
485 if (p >= ms->p_end - 1) 488 if (l_unlikely(p >= ms->p_end - 1))
486 luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); 489 luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
487 if (*s != *p) return NULL; 490 if (*s != *p) return NULL;
488 else { 491 else {
@@ -565,7 +568,7 @@ static const char *match_capture (MatchState *ms, const char *s, int l) {
565 568
566 569
567static const char *match (MatchState *ms, const char *s, const char *p) { 570static const char *match (MatchState *ms, const char *s, const char *p) {
568 if (ms->matchdepth-- == 0) 571 if (l_unlikely(ms->matchdepth-- == 0))
569 luaL_error(ms->L, "pattern too complex"); 572 luaL_error(ms->L, "pattern too complex");
570 init: /* using goto's to optimize tail recursion */ 573 init: /* using goto's to optimize tail recursion */
571 if (p != ms->p_end) { /* end of pattern? */ 574 if (p != ms->p_end) { /* end of pattern? */
@@ -599,7 +602,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
599 case 'f': { /* frontier? */ 602 case 'f': { /* frontier? */
600 const char *ep; char previous; 603 const char *ep; char previous;
601 p += 2; 604 p += 2;
602 if (*p != '[') 605 if (l_unlikely(*p != '['))
603 luaL_error(ms->L, "missing '[' after '%%f' in pattern"); 606 luaL_error(ms->L, "missing '[' after '%%f' in pattern");
604 ep = classend(ms, p); /* points to what is next */ 607 ep = classend(ms, p); /* points to what is next */
605 previous = (s == ms->src_init) ? '\0' : *(s - 1); 608 previous = (s == ms->src_init) ? '\0' : *(s - 1);
@@ -699,7 +702,7 @@ static const char *lmemfind (const char *s1, size_t l1,
699static size_t get_onecapture (MatchState *ms, int i, const char *s, 702static size_t get_onecapture (MatchState *ms, int i, const char *s,
700 const char *e, const char **cap) { 703 const char *e, const char **cap) {
701 if (i >= ms->level) { 704 if (i >= ms->level) {
702 if (i != 0) 705 if (l_unlikely(i != 0))
703 luaL_error(ms->L, "invalid capture index %%%d", i + 1); 706 luaL_error(ms->L, "invalid capture index %%%d", i + 1);
704 *cap = s; 707 *cap = s;
705 return e - s; 708 return e - s;
@@ -707,7 +710,7 @@ static size_t get_onecapture (MatchState *ms, int i, const char *s,
707 else { 710 else {
708 ptrdiff_t capl = ms->capture[i].len; 711 ptrdiff_t capl = ms->capture[i].len;
709 *cap = ms->capture[i].init; 712 *cap = ms->capture[i].init;
710 if (capl == CAP_UNFINISHED) 713 if (l_unlikely(capl == CAP_UNFINISHED))
711 luaL_error(ms->L, "unfinished capture"); 714 luaL_error(ms->L, "unfinished capture");
712 else if (capl == CAP_POSITION) 715 else if (capl == CAP_POSITION)
713 lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); 716 lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
@@ -926,7 +929,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
926 luaL_addlstring(b, s, e - s); /* keep original text */ 929 luaL_addlstring(b, s, e - s); /* keep original text */
927 return 0; /* no changes */ 930 return 0; /* no changes */
928 } 931 }
929 else if (!lua_isstring(L, -1)) 932 else if (l_unlikely(!lua_isstring(L, -1)))
930 return luaL_error(L, "invalid replacement value (a %s)", 933 return luaL_error(L, "invalid replacement value (a %s)",
931 luaL_typename(L, -1)); 934 luaL_typename(L, -1));
932 else { 935 else {
@@ -1058,7 +1061,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
1058 for (i = 0; i < n; i++) 1061 for (i = 0; i < n; i++)
1059 buff[i] = toupper(uchar(buff[i])); 1062 buff[i] = toupper(uchar(buff[i]));
1060 } 1063 }
1061 else if (fmt[SIZELENMOD] != 'a') 1064 else if (l_unlikely(fmt[SIZELENMOD] != 'a'))
1062 return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); 1065 return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
1063 return n; 1066 return n;
1064} 1067}
@@ -1411,7 +1414,7 @@ static int getnum (const char **fmt, int df) {
1411*/ 1414*/
1412static int getnumlimit (Header *h, const char **fmt, int df) { 1415static int getnumlimit (Header *h, const char **fmt, int df) {
1413 int sz = getnum(fmt, df); 1416 int sz = getnum(fmt, df);
1414 if (sz > MAXINTSIZE || sz <= 0) 1417 if (l_unlikely(sz > MAXINTSIZE || sz <= 0))
1415 return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", 1418 return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
1416 sz, MAXINTSIZE); 1419 sz, MAXINTSIZE);
1417 return sz; 1420 return sz;
@@ -1452,7 +1455,7 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
1452 case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; 1455 case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
1453 case 'c': 1456 case 'c':
1454 *size = getnum(fmt, -1); 1457 *size = getnum(fmt, -1);
1455 if (*size == -1) 1458 if (l_unlikely(*size == -1))
1456 luaL_error(h->L, "missing size for format option 'c'"); 1459 luaL_error(h->L, "missing size for format option 'c'");
1457 return Kchar; 1460 return Kchar;
1458 case 'z': return Kzstr; 1461 case 'z': return Kzstr;
@@ -1491,7 +1494,7 @@ static KOption getdetails (Header *h, size_t totalsize,
1491 else { 1494 else {
1492 if (align > h->maxalign) /* enforce maximum alignment */ 1495 if (align > h->maxalign) /* enforce maximum alignment */
1493 align = h->maxalign; 1496 align = h->maxalign;
1494 if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ 1497 if (l_unlikely((align & (align - 1)) != 0)) /* not a power of 2? */
1495 luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); 1498 luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
1496 *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); 1499 *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
1497 } 1500 }
@@ -1683,7 +1686,7 @@ static lua_Integer unpackint (lua_State *L, const char *str,
1683 else if (size > SZINT) { /* must check unread bytes */ 1686 else if (size > SZINT) { /* must check unread bytes */
1684 int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; 1687 int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
1685 for (i = limit; i < size; i++) { 1688 for (i = limit; i < size; i++) {
1686 if ((unsigned char)str[islittle ? i : size - 1 - i] != mask) 1689 if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask))
1687 luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); 1690 luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
1688 } 1691 }
1689 } 1692 }
diff --git a/ltable.c b/ltable.c
index e98bab71..b520cdf4 100644
--- a/ltable.c
+++ b/ltable.c
@@ -307,7 +307,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key,
307 return i; /* yes; that's the index */ 307 return i; /* yes; that's the index */
308 else { 308 else {
309 const TValue *n = getgeneric(t, key, 1); 309 const TValue *n = getgeneric(t, key, 1);
310 if (unlikely(isabstkey(n))) 310 if (l_unlikely(isabstkey(n)))
311 luaG_runerror(L, "invalid key to 'next'"); /* key not found */ 311 luaG_runerror(L, "invalid key to 'next'"); /* key not found */
312 i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ 312 i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
313 /* hash elements are numbered after array ones */ 313 /* hash elements are numbered after array ones */
@@ -541,7 +541,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
541 } 541 }
542 /* allocate new array */ 542 /* allocate new array */
543 newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); 543 newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue);
544 if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ 544 if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
545 freehash(L, &newt); /* release new hash part */ 545 freehash(L, &newt); /* release new hash part */
546 luaM_error(L); /* raise error (with array unchanged) */ 546 luaM_error(L); /* raise error (with array unchanged) */
547 } 547 }
@@ -635,7 +635,7 @@ static Node *getfreepos (Table *t) {
635void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { 635void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) {
636 Node *mp; 636 Node *mp;
637 TValue aux; 637 TValue aux;
638 if (unlikely(ttisnil(key))) 638 if (l_unlikely(ttisnil(key)))
639 luaG_runerror(L, "table index is nil"); 639 luaG_runerror(L, "table index is nil");
640 else if (ttisfloat(key)) { 640 else if (ttisfloat(key)) {
641 lua_Number f = fltvalue(key); 641 lua_Number f = fltvalue(key);
@@ -644,7 +644,7 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) {
644 setivalue(&aux, k); 644 setivalue(&aux, k);
645 key = &aux; /* insert it as an integer */ 645 key = &aux; /* insert it as an integer */
646 } 646 }
647 else if (unlikely(luai_numisnan(f))) 647 else if (l_unlikely(luai_numisnan(f)))
648 luaG_runerror(L, "table index is NaN"); 648 luaG_runerror(L, "table index is NaN");
649 } 649 }
650 if (ttisnil(value)) 650 if (ttisnil(value))
diff --git a/ltablib.c b/ltablib.c
index c7f0e4dc..d80eb801 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -145,7 +145,7 @@ static int tmove (lua_State *L) {
145 145
146static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { 146static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
147 lua_geti(L, 1, i); 147 lua_geti(L, 1, i);
148 if (!lua_isstring(L, -1)) 148 if (l_unlikely(!lua_isstring(L, -1)))
149 luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", 149 luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
150 luaL_typename(L, -1), i); 150 luaL_typename(L, -1), i);
151 luaL_addvalue(b); 151 luaL_addvalue(b);
@@ -196,7 +196,8 @@ static int tunpack (lua_State *L) {
196 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); 196 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
197 if (i > e) return 0; /* empty range */ 197 if (i > e) return 0; /* empty range */
198 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ 198 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
199 if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) 199 if (l_unlikely(n >= (unsigned int)INT_MAX ||
200 !lua_checkstack(L, (int)(++n))))
200 return luaL_error(L, "too many results to unpack"); 201 return luaL_error(L, "too many results to unpack");
201 for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ 202 for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
202 lua_geti(L, 1, i); 203 lua_geti(L, 1, i);
@@ -300,14 +301,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
300 for (;;) { 301 for (;;) {
301 /* next loop: repeat ++i while a[i] < P */ 302 /* next loop: repeat ++i while a[i] < P */
302 while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { 303 while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
303 if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ 304 if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */
304 luaL_error(L, "invalid order function for sorting"); 305 luaL_error(L, "invalid order function for sorting");
305 lua_pop(L, 1); /* remove a[i] */ 306 lua_pop(L, 1); /* remove a[i] */
306 } 307 }
307 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ 308 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */
308 /* next loop: repeat --j while P < a[j] */ 309 /* next loop: repeat --j while P < a[j] */
309 while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { 310 while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
310 if (j < i) /* j < i but a[j] > P ?? */ 311 if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */
311 luaL_error(L, "invalid order function for sorting"); 312 luaL_error(L, "invalid order function for sorting");
312 lua_pop(L, 1); /* remove a[j] */ 313 lua_pop(L, 1); /* remove a[j] */
313 } 314 }
diff --git a/ltm.c b/ltm.c
index 4770f96b..b657b783 100644
--- a/ltm.c
+++ b/ltm.c
@@ -147,7 +147,7 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
147 147
148void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 148void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
149 StkId res, TMS event) { 149 StkId res, TMS event) {
150 if (!callbinTM(L, p1, p2, res, event)) { 150 if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
151 switch (event) { 151 switch (event) {
152 case TM_BAND: case TM_BOR: case TM_BXOR: 152 case TM_BAND: case TM_BOR: case TM_BXOR:
153 case TM_SHL: case TM_SHR: case TM_BNOT: { 153 case TM_SHL: case TM_SHR: case TM_BNOT: {
@@ -166,7 +166,8 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
166 166
167void luaT_tryconcatTM (lua_State *L) { 167void luaT_tryconcatTM (lua_State *L) {
168 StkId top = L->top; 168 StkId top = L->top;
169 if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT)) 169 if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2,
170 TM_CONCAT)))
170 luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); 171 luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
171} 172}
172 173
diff --git a/luaconf.h b/luaconf.h
index a44858c4..ae73e2fd 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -660,6 +660,26 @@
660#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) 660#define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
661#endif 661#endif
662 662
663
664/*
665** macros to improve jump prediction, used mostly for error handling
666** and debug facilities.
667*/
668#if (defined(LUA_CORE) || defined(LUA_LIB)) && !defined(l_likely)
669
670#include <stdio.h>
671#if defined(__GNUC__)
672#define l_likely(x) (__builtin_expect(((x) != 0), 1))
673#define l_unlikely(x) (__builtin_expect(((x) != 0), 0))
674#else
675#define l_likely(x) (x)
676#define l_unlikely(x) (x)
677#endif
678
679#endif
680
681
682
663/* }================================================================== */ 683/* }================================================================== */
664 684
665 685
diff --git a/lvm.c b/lvm.c
index 1252ecbf..728acd46 100644
--- a/lvm.c
+++ b/lvm.c
@@ -235,11 +235,11 @@ static int forprep (lua_State *L, StkId ra) {
235 } 235 }
236 else { /* try making all values floats */ 236 else { /* try making all values floats */
237 lua_Number init; lua_Number limit; lua_Number step; 237 lua_Number init; lua_Number limit; lua_Number step;
238 if (unlikely(!tonumber(plimit, &limit))) 238 if (l_unlikely(!tonumber(plimit, &limit)))
239 luaG_forerror(L, plimit, "limit"); 239 luaG_forerror(L, plimit, "limit");
240 if (unlikely(!tonumber(pstep, &step))) 240 if (l_unlikely(!tonumber(pstep, &step)))
241 luaG_forerror(L, pstep, "step"); 241 luaG_forerror(L, pstep, "step");
242 if (unlikely(!tonumber(pinit, &init))) 242 if (l_unlikely(!tonumber(pinit, &init)))
243 luaG_forerror(L, pinit, "initial value"); 243 luaG_forerror(L, pinit, "initial value");
244 if (step == 0) 244 if (step == 0)
245 luaG_runerror(L, "'for' step is zero"); 245 luaG_runerror(L, "'for' step is zero");
@@ -292,7 +292,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
292 if (slot == NULL) { /* 't' is not a table? */ 292 if (slot == NULL) { /* 't' is not a table? */
293 lua_assert(!ttistable(t)); 293 lua_assert(!ttistable(t));
294 tm = luaT_gettmbyobj(L, t, TM_INDEX); 294 tm = luaT_gettmbyobj(L, t, TM_INDEX);
295 if (unlikely(notm(tm))) 295 if (l_unlikely(notm(tm)))
296 luaG_typeerror(L, t, "index"); /* no metamethod */ 296 luaG_typeerror(L, t, "index"); /* no metamethod */
297 /* else will try the metamethod */ 297 /* else will try the metamethod */
298 } 298 }
@@ -346,7 +346,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
346 } 346 }
347 else { /* not a table; check metamethod */ 347 else { /* not a table; check metamethod */
348 tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); 348 tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
349 if (unlikely(notm(tm))) 349 if (l_unlikely(notm(tm)))
350 luaG_typeerror(L, t, "index"); 350 luaG_typeerror(L, t, "index");
351 } 351 }
352 /* try the metamethod */ 352 /* try the metamethod */
@@ -651,7 +651,7 @@ void luaV_concat (lua_State *L, int total) {
651 /* collect total length and number of strings */ 651 /* collect total length and number of strings */
652 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { 652 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
653 size_t l = vslen(s2v(top - n - 1)); 653 size_t l = vslen(s2v(top - n - 1));
654 if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) 654 if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl))
655 luaG_runerror(L, "string length overflow"); 655 luaG_runerror(L, "string length overflow");
656 tl += l; 656 tl += l;
657 } 657 }
@@ -695,7 +695,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
695 } 695 }
696 default: { /* try metamethod */ 696 default: { /* try metamethod */
697 tm = luaT_gettmbyobj(L, rb, TM_LEN); 697 tm = luaT_gettmbyobj(L, rb, TM_LEN);
698 if (unlikely(notm(tm))) /* no metamethod? */ 698 if (l_unlikely(notm(tm))) /* no metamethod? */
699 luaG_typeerror(L, rb, "get length of"); 699 luaG_typeerror(L, rb, "get length of");
700 break; 700 break;
701 } 701 }
@@ -711,7 +711,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
711** otherwise 'floor(q) == trunc(q) - 1'. 711** otherwise 'floor(q) == trunc(q) - 1'.
712*/ 712*/
713lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { 713lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
714 if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ 714 if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
715 if (n == 0) 715 if (n == 0)
716 luaG_runerror(L, "attempt to divide by zero"); 716 luaG_runerror(L, "attempt to divide by zero");
717 return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ 717 return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
@@ -731,7 +731,7 @@ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
731** about luaV_idiv.) 731** about luaV_idiv.)
732*/ 732*/
733lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { 733lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
734 if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ 734 if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
735 if (n == 0) 735 if (n == 0)
736 luaG_runerror(L, "attempt to perform 'n%%0'"); 736 luaG_runerror(L, "attempt to perform 'n%%0'");
737 return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ 737 return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
@@ -1049,7 +1049,8 @@ void luaV_finishOp (lua_State *L) {
1049#define updatebase(ci) (base = ci->func + 1) 1049#define updatebase(ci) (base = ci->func + 1)
1050 1050
1051 1051
1052#define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } } 1052#define updatestack(ci) \
1053 { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } }
1053 1054
1054 1055
1055/* 1056/*
@@ -1107,7 +1108,7 @@ void luaV_finishOp (lua_State *L) {
1107 1108
1108/* fetch an instruction and prepare its execution */ 1109/* fetch an instruction and prepare its execution */
1109#define vmfetch() { \ 1110#define vmfetch() { \
1110 if (trap) { /* stack reallocation or hooks? */ \ 1111 if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \
1111 trap = luaG_traceexec(L, pc); /* handle hooks */ \ 1112 trap = luaG_traceexec(L, pc); /* handle hooks */ \
1112 updatebase(ci); /* correct stack */ \ 1113 updatebase(ci); /* correct stack */ \
1113 } \ 1114 } \
@@ -1135,7 +1136,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1135 cl = clLvalue(s2v(ci->func)); 1136 cl = clLvalue(s2v(ci->func));
1136 k = cl->p->k; 1137 k = cl->p->k;
1137 pc = ci->u.l.savedpc; 1138 pc = ci->u.l.savedpc;
1138 if (trap) { 1139 if (l_unlikely(trap)) {
1139 if (pc == cl->p->code) { /* first instruction (not resuming)? */ 1140 if (pc == cl->p->code) { /* first instruction (not resuming)? */
1140 if (cl->p->is_vararg) 1141 if (cl->p->is_vararg)
1141 trap = 0; /* hooks will start after VARARGPREP instruction */ 1142 trap = 0; /* hooks will start after VARARGPREP instruction */
@@ -1678,23 +1679,23 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1678 goto ret; 1679 goto ret;
1679 } 1680 }
1680 vmcase(OP_RETURN0) { 1681 vmcase(OP_RETURN0) {
1681 if (L->hookmask) { 1682 if (l_unlikely(L->hookmask)) {
1682 L->top = ra; 1683 L->top = ra;
1683 savepc(ci); 1684 savepc(ci);
1684 luaD_poscall(L, ci, 0); /* no hurry... */ 1685 luaD_poscall(L, ci, 0); /* no hurry... */
1685 trap = 1; 1686 trap = 1;
1686 } 1687 }
1687 else { /* do the 'poscall' here */ 1688 else { /* do the 'poscall' here */
1688 int nres = ci->nresults; 1689 int nres;
1689 L->ci = ci->previous; /* back to caller */ 1690 L->ci = ci->previous; /* back to caller */
1690 L->top = base - 1; 1691 L->top = base - 1;
1691 while (nres-- > 0) 1692 for (nres = ci->nresults; l_unlikely(nres > 0); nres--)
1692 setnilvalue(s2v(L->top++)); /* all results are nil */ 1693 setnilvalue(s2v(L->top++)); /* all results are nil */
1693 } 1694 }
1694 goto ret; 1695 goto ret;
1695 } 1696 }
1696 vmcase(OP_RETURN1) { 1697 vmcase(OP_RETURN1) {
1697 if (L->hookmask) { 1698 if (l_unlikely(L->hookmask)) {
1698 L->top = ra + 1; 1699 L->top = ra + 1;
1699 savepc(ci); 1700 savepc(ci);
1700 luaD_poscall(L, ci, 1); /* no hurry... */ 1701 luaD_poscall(L, ci, 1); /* no hurry... */
@@ -1708,8 +1709,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1708 else { 1709 else {
1709 setobjs2s(L, base - 1, ra); /* at least this result */ 1710 setobjs2s(L, base - 1, ra); /* at least this result */
1710 L->top = base; 1711 L->top = base;
1711 while (--nres > 0) /* complete missing results */ 1712 for (; l_unlikely(nres > 1); nres--)
1712 setnilvalue(s2v(L->top++)); 1713 setnilvalue(s2v(L->top++)); /* complete missing results */
1713 } 1714 }
1714 } 1715 }
1715 ret: /* return from a Lua function */ 1716 ret: /* return from a Lua function */
@@ -1812,7 +1813,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1812 } 1813 }
1813 vmcase(OP_VARARGPREP) { 1814 vmcase(OP_VARARGPREP) {
1814 ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); 1815 ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p));
1815 if (trap) { 1816 if (l_unlikely(trap)) { /* previous "Protect" updated trap */
1816 luaD_hookcall(L, ci); 1817 luaD_hookcall(L, ci);
1817 L->oldpc = 1; /* next opcode will be seen as a "new" line */ 1818 L->oldpc = 1; /* next opcode will be seen as a "new" line */
1818 } 1819 }
diff --git a/lvm.h b/lvm.h
index 2d4ac160..1bc16f3a 100644
--- a/lvm.h
+++ b/lvm.h
@@ -60,12 +60,14 @@ typedef enum {
60 60
61/* convert an object to an integer (including string coercion) */ 61/* convert an object to an integer (including string coercion) */
62#define tointeger(o,i) \ 62#define tointeger(o,i) \
63 (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 63 (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
64 : luaV_tointeger(o,i,LUA_FLOORN2I))
64 65
65 66
66/* convert an object to an integer (without string coercion) */ 67/* convert an object to an integer (without string coercion) */
67#define tointegerns(o,i) \ 68#define tointegerns(o,i) \
68 (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) 69 (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
70 : luaV_tointegerns(o,i,LUA_FLOORN2I))
69 71
70 72
71#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 73#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))