Skip to content

Commit 08a6b36

Browse files
Peter Neissclaude
andcommitted
Phase 126: Aggressive macro removal - Convert 17 macros to C++ functions
Converted the following macros to modern C++ (template) functions: **Memory/Limits (llimits.h, lmem.h):** - l_numbits(t) → l_numbits<T>() template constexpr - log2maxs(t) → log2maxs<T>() template constexpr - MAX_SIZE → inline constexpr variable - MAX_LMEM → inline constexpr variable - luaM_limitN(n,t) → luaM_limitN<T>(n) template constexpr **Math library (lmathlib.cpp):** - trim64(x) → inline constexpr function - trim32(x) → inline constexpr function - I2UInt(x) → inline function - Int2I(x) → inline function **Memory management (lmem.cpp):** - callfrealloc(g,block,os,ns) → inline function - cantryagain(g) → inline bool function **Auxiliary library (lauxlib.cpp):** - addbuff(b,v) → template function with reference parameter **Table library (ltablib.cpp):** - aux_getn(L,n,w) → inline function (added forward declaration) **Stack management (lstack.cpp):** - condmovestack(L,pre,pos) → template function with lambdas **Opcodes (lopcodes.cpp):** - opmode(mm,ot,it,t,a,m) → inline constexpr function **API (lapi.cpp):** - checkresults(L,na,nr) → inline void function **Serialization (lundump.cpp):** - getaddr(S,n,t) → getaddr<T>(S,n) template function **Results:** - Converted 17 macros to functions - All tests pass - Performance: ~2.36s avg (excellent, well below 4.33s target) - Zero compiler warnings - Macro conversion progress: ~99.5% complete 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 4e0929a commit 08a6b36

File tree

14 files changed

+112
-58
lines changed

14 files changed

+112
-58
lines changed

src/auxiliary/lauxlib.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,11 +1150,15 @@ inline constexpr size_t BUFSEEDB = sizeof(void*) + sizeof(time_t);
11501150
/* Size for the buffer in int's, rounded up */
11511151
inline constexpr size_t BUFSEED = (BUFSEEDB + sizeof(int) - 1) / sizeof(int);
11521152

1153-
/*
1153+
/* Phase 126.2: Convert addbuff macro to inline template function
11541154
** Copy the contents of variable 'v' into the buffer pointed by 'b'.
11551155
** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.)
11561156
*/
1157-
#define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v))
1157+
template<typename T>
1158+
inline void addbuff(char*& b, const T& v) noexcept {
1159+
memcpy(&b[0], &v, sizeof(v));
1160+
b += sizeof(v);
1161+
}
11581162

11591163

11601164
static unsigned int luai_makeseed (void) {

src/compiler/lopcodes.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
#include "lopcodes.h"
1515

1616

17-
#define opmode(mm,ot,it,t,a,m) \
18-
(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | static_cast<int>(m))
17+
/* Phase 126.2: Convert opmode macro to inline constexpr function */
18+
inline constexpr int opmode(int mm, int ot, int it, int t, int a, OpMode m) noexcept {
19+
return ((mm << 7) | (ot << 6) | (it << 5) | (t << 4) | (a << 3) | static_cast<int>(m));
20+
}
1921

2022

2123
/* ORDER OP */

src/core/lapi.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -943,12 +943,14 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
943943
*/
944944

945945

946-
#define checkresults(L,na,nr) \
947-
(api_check(L, (nr) == LUA_MULTRET \
948-
|| (L->getCI()->topRef().p - L->getTop().p >= (nr) - (na)), \
949-
"results from function overflow current stack size"), \
950-
api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \
951-
"invalid number of results"))
946+
/* Phase 126.2: Convert checkresults macro to inline function */
947+
inline void checkresults(lua_State* L, int na, int nr) {
948+
api_check(L, (nr) == LUA_MULTRET
949+
|| (L->getCI()->topRef().p - L->getTop().p >= (nr) - (na)),
950+
"results from function overflow current stack size");
951+
api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS,
952+
"invalid number of results");
953+
}
952954

953955

954956
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,

src/core/lstack.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,23 @@ inline constexpr int STACKERRSPACE = 200;
7171
#endif
7272

7373

74-
/*
74+
/* Phase 126.2: Convert condmovestack macro to inline function
7575
** Conditional stack movement for debugging
7676
*/
7777
#if !defined(HARDSTACKTESTS)
78-
#define condmovestack(L,pre,pos) ((void)0)
78+
template<typename Pre, typename Pos>
79+
inline void condmovestack(lua_State* L, Pre&& pre, Pos&& pos) noexcept {
80+
cast_void(L); cast_void(pre); cast_void(pos);
81+
}
7982
#else
8083
/* realloc stack keeping its size */
81-
#define condmovestack(L,pre,pos) \
82-
{ int sz_ = (L)->getStackSubsystem().getSize(); pre; \
83-
(L)->getStackSubsystem().realloc(L, sz_, 0); pos; }
84+
template<typename Pre, typename Pos>
85+
inline void condmovestack(lua_State* L, Pre&& pre, Pos&& pos) {
86+
int sz_ = L->getStackSubsystem().getSize();
87+
pre();
88+
L->getStackSubsystem().realloc(L, sz_, 0);
89+
pos();
90+
}
8491
#endif
8592

8693

@@ -355,7 +362,7 @@ void LuaStack::shrink(lua_State* L) {
355362
realloc(L, nsize, 0); /* ok if that fails */
356363
}
357364
else /* don't change stack */
358-
condmovestack(L, (void)0, (void)0); /* (change only for debugging) */
365+
condmovestack(L, [](){}, [](){}); /* (change only for debugging) */
359366

360367
luaE_shrinkCI(L); /* shrink CI list */
361368
}

src/libraries/lmathlib.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,10 @@ static int math_type (lua_State *L) {
336336
** final result has to discard the extra bits.
337337
*/
338338

339-
/* avoid using extra bits when needed */
340-
#define trim64(x) ((x) & 0xffffffffffffffffu)
339+
/* Phase 126.2: Convert trim64 macro to inline constexpr function */
340+
inline constexpr Rand64 trim64(Rand64 x) noexcept {
341+
return x & 0xffffffffffffffffu;
342+
}
341343

342344

343345
/* rotate left 'x' by 'n' bits */
@@ -385,11 +387,14 @@ static lua_Number I2d (Rand64 x) {
385387
return res;
386388
}
387389

388-
/* convert a 'Rand64' to a 'lua_Unsigned' */
389-
#define I2UInt(x) ((lua_Unsigned)trim64(x))
390+
/* Phase 126.2: Convert I2UInt and Int2I macros to inline functions */
391+
inline lua_Unsigned I2UInt(Rand64 x) noexcept {
392+
return static_cast<lua_Unsigned>(trim64(x));
393+
}
390394

391-
/* convert a 'lua_Unsigned' to a 'Rand64' */
392-
#define Int2I(x) ((Rand64)(x))
395+
inline Rand64 Int2I(lua_Unsigned x) noexcept {
396+
return static_cast<Rand64>(x);
397+
}
393398

394399

395400
#else /* no 'Rand64' }{ */
@@ -409,8 +414,10 @@ typedef struct Rand64 {
409414
** Moreover, the final result has to discard the extra bits.
410415
*/
411416

412-
/* avoid using extra bits when needed */
413-
#define trim32(x) ((x) & 0xffffffffu)
417+
/* Phase 126.2: Convert trim32 macro to inline constexpr function */
418+
inline constexpr l_uint32 trim32(l_uint32 x) noexcept {
419+
return x & 0xffffffffu;
420+
}
414421

415422

416423
/*

src/libraries/ltablib.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@
3131
inline constexpr int TAB_RW = TAB_R | TAB_W; // read/write
3232

3333

34-
#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
34+
/* Forward declaration for aux_getn */
35+
static void checktab(lua_State *L, int arg, int what);
36+
37+
/* Phase 126.2: Convert aux_getn macro to inline function */
38+
inline lua_Integer aux_getn(lua_State* L, int n, int w) {
39+
checktab(L, n, w | TAB_L);
40+
return luaL_len(L, n);
41+
}
3542

3643

3744
static int checkfield (lua_State *L, const char *key, int n) {

src/memory/llimits.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
#include "lua.h"
1818

19-
20-
#define l_numbits(t) cast_int(sizeof(t) * CHAR_BIT)
21-
2219
/*
2320
** 'l_mem' is a signed integer big enough to count the total memory
2421
** used by Lua. (It is signed due to the use of debt in several
@@ -36,8 +33,7 @@ typedef long l_mem;
3633
typedef unsigned long lu_mem;
3734
#endif /* } */
3835

39-
#define MAX_LMEM \
40-
cast(l_mem, (cast(lu_mem, 1) << (l_numbits(l_mem) - 1)) - 1)
36+
/* MAX_LMEM defined later in this file after cast functions and l_numbits */
4137

4238

4339
/* chars used as small naturals (so that 'char' is reserved for characters) */
@@ -57,15 +53,13 @@ inline constexpr size_t MAX_SIZET = ((size_t)(~(size_t)0));
5753
/*
5854
** Maximum size for strings and userdata visible for Lua; should be
5955
** representable as a lua_Integer and as a size_t.
56+
** Defined later in this file after cast functions.
6057
*/
61-
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
62-
: cast_sizet(LUA_MAXINTEGER))
6358

64-
/*
65-
** floor of the log2 of the maximum signed value for integral type 't'.
59+
/* floor of the log2 of the maximum signed value for integral type 't'.
6660
** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
61+
** Defined later in this file after cast functions.
6762
*/
68-
#define log2maxs(t) (l_numbits(t) - 2)
6963

7064

7165
/*
@@ -236,6 +230,25 @@ inline constexpr unsigned int point2uint(T* p) noexcept {
236230
return cast_uint((L_P2I)(p) & std::numeric_limits<unsigned int>::max());
237231
}
238232

233+
/* Phase 126.1: Converted l_numbits from macro to template constexpr function */
234+
template<typename T>
235+
inline constexpr int l_numbits() noexcept {
236+
return cast_int(sizeof(T) * CHAR_BIT);
237+
}
238+
239+
/* Phase 126.1: Converted log2maxs from macro to template constexpr function */
240+
template<typename T>
241+
inline constexpr int log2maxs() noexcept {
242+
return l_numbits<T>() - 2;
243+
}
244+
245+
/* Phase 126.1: Converted MAX_SIZE from macro to inline constexpr (moved here after dependencies) */
246+
inline constexpr size_t MAX_SIZE = (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET
247+
: cast_sizet(LUA_MAXINTEGER));
248+
249+
/* Phase 126.1: Converted MAX_LMEM from macro to inline constexpr (moved here after dependencies) */
250+
inline constexpr l_mem MAX_LMEM = cast(l_mem, (cast(lu_mem, 1) << (l_numbits<l_mem>() - 1)) - 1);
251+
239252

240253
/* cast a signed lua_Integer to lua_Unsigned */
241254
#if !defined(l_castS2U)

src/memory/lmem.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,23 @@
4141
*/
4242

4343

44-
/*
45-
** Macro to call the allocation function.
46-
*/
47-
#define callfrealloc(g,block,os,ns) ((*g->getFrealloc())(g->getUd(), block, os, ns))
44+
/* Phase 126.2: Convert callfrealloc macro to inline function */
45+
inline void* callfrealloc(global_State* g, void* block, size_t os, size_t ns) noexcept {
46+
return (*g->getFrealloc())(g->getUd(), block, os, ns);
47+
}
4848

4949

50-
/*
50+
/* Phase 126.2: Convert cantryagain macro to inline function
5151
** When an allocation fails, it will try again after an emergency
5252
** collection, except when it cannot run a collection. The GC should
5353
** not be called while the state is not fully built, as the collector
5454
** is not yet fully initialized. Also, it should not be called when
5555
** 'gcstopem' is true, because then the interpreter is in the middle of
5656
** a collection step.
5757
*/
58-
#define cantryagain(g) (g->isComplete() && !g->getGCStopEm())
58+
inline bool cantryagain(global_State* g) noexcept {
59+
return g->isComplete() && !g->getGCStopEm();
60+
}
5961

6062

6163

src/memory/lmem.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@
3636

3737

3838
/*
39-
** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
39+
** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(T)', so that
4040
** the result is not larger than 'n' and cannot overflow a 'size_t'
41-
** when multiplied by the size of type 't'. (Assumes that 'n' is an
41+
** when multiplied by the size of type 'T'. (Assumes that 'n' is an
4242
** 'int' and that 'int' is not larger than 'size_t'.)
4343
*/
44-
#define luaM_limitN(n,t) \
45-
((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
46-
cast_int((MAX_SIZET/sizeof(t))))
44+
template<typename T>
45+
inline constexpr int luaM_limitN(int n) noexcept {
46+
return (cast_sizet(n) <= MAX_SIZET/sizeof(T)) ? n :
47+
cast_int((MAX_SIZET/sizeof(T)));
48+
}
4749

4850

4951
/*
@@ -122,7 +124,7 @@ inline T* luaM_reallocvector(lua_State* L, T* v, size_t oldn, size_t n) {
122124
template<typename T>
123125
inline void luaM_growvector(lua_State* L, T*& v, int nelems, int& size, int limit, const char* e) {
124126
v = static_cast<T*>(luaM_growaux_(L, v, nelems, &size, sizeof(T),
125-
luaM_limitN(limit, T), e));
127+
luaM_limitN<T>(limit), e));
126128
}
127129

128130
/* Shrink a vector to final_n elements, updating size */

src/objects/lstring.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inline constexpr size_t tstringFallocOffset() noexcept {
3232
/*
3333
** Maximum size for string table.
3434
*/
35-
inline constexpr int MAXSTRTB = cast_int(luaM_limitN(std::numeric_limits<int>::max(), TString*));
35+
inline constexpr int MAXSTRTB = cast_int(luaM_limitN<TString*>(std::numeric_limits<int>::max()));
3636

3737
/*
3838
** Initial size for the string table (must be power of 2).

0 commit comments

Comments
 (0)