Skip to content

Commit 4f653a2

Browse files
committed
Merge remote-tracking branch 'main_repo/v2.1' into v2.1
2 parents f5fd9b1 + 5790d25 commit 4f653a2

19 files changed

+132
-187
lines changed

doc/ext_buffer.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,9 @@ <h3 id="serialize_format">Serialization Format Specification</h3>
588588
tab → 0x08 // Empty table
589589
| 0x09 h.U h*{object object} // Key/value hash
590590
| 0x0a a.U a*object // 0-based array
591-
| 0x0b a.U a*object h.U h*{object object} // Mixed
591+
| 0x0b a.U h.U a*object h*{object object} // Mixed
592592
| 0x0c a.U (a-1)*object // 1-based array
593-
| 0x0d a.U (a-1)*object h.U h*{object object} // Mixed
593+
| 0x0d a.U h.U (a-1)*object h*{object object} // Mixed
594594
tab_mt → 0x0e (index-1).U tab // Metatable dict entry
595595

596596
int64 → 0x10 int.L // FFI int64_t

src/jit/bcsave.lua

+34-121
Original file line numberDiff line numberDiff line change
@@ -439,24 +439,12 @@ typedef struct
439439
{
440440
mach_header; uint32_t reserved;
441441
} mach_header_64;
442-
typedef struct {
443-
uint32_t cmd, cmdsize;
444-
char segname[16];
445-
uint32_t vmaddr, vmsize, fileoff, filesize;
446-
uint32_t maxprot, initprot, nsects, flags;
447-
} mach_segment_command;
448442
typedef struct {
449443
uint32_t cmd, cmdsize;
450444
char segname[16];
451445
uint64_t vmaddr, vmsize, fileoff, filesize;
452446
uint32_t maxprot, initprot, nsects, flags;
453447
} mach_segment_command_64;
454-
typedef struct {
455-
char sectname[16], segname[16];
456-
uint32_t addr, size;
457-
uint32_t offset, align, reloff, nreloc, flags;
458-
uint32_t reserved1, reserved2;
459-
} mach_section;
460448
typedef struct {
461449
char sectname[16], segname[16];
462450
uint64_t addr, size;
@@ -466,133 +454,58 @@ typedef struct {
466454
typedef struct {
467455
uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize;
468456
} mach_symtab_command;
469-
typedef struct {
470-
int32_t strx;
471-
uint8_t type, sect;
472-
int16_t desc;
473-
uint32_t value;
474-
} mach_nlist;
475457
typedef struct {
476458
int32_t strx;
477459
uint8_t type, sect;
478460
uint16_t desc;
479461
uint64_t value;
480462
} mach_nlist_64;
481-
typedef struct
482-
{
483-
int32_t magic, nfat_arch;
484-
} mach_fat_header;
485-
typedef struct
486-
{
487-
int32_t cputype, cpusubtype, offset, size, align;
488-
} mach_fat_arch;
489-
typedef struct {
490-
struct {
491-
mach_header hdr;
492-
mach_segment_command seg;
493-
mach_section sec;
494-
mach_symtab_command sym;
495-
} arch[1];
496-
mach_nlist sym_entry;
497-
uint8_t space[4096];
498-
} mach_obj;
499463
typedef struct {
500-
struct {
501-
mach_header_64 hdr;
502-
mach_segment_command_64 seg;
503-
mach_section_64 sec;
504-
mach_symtab_command sym;
505-
} arch[1];
464+
mach_header_64 hdr;
465+
mach_segment_command_64 seg;
466+
mach_section_64 sec;
467+
mach_symtab_command sym;
506468
mach_nlist_64 sym_entry;
507469
uint8_t space[4096];
508470
} mach_obj_64;
509-
typedef struct {
510-
mach_fat_header fat;
511-
mach_fat_arch fat_arch[2];
512-
struct {
513-
mach_header hdr;
514-
mach_segment_command seg;
515-
mach_section sec;
516-
mach_symtab_command sym;
517-
} arch[2];
518-
mach_nlist sym_entry;
519-
uint8_t space[4096];
520-
} mach_fat_obj;
521-
typedef struct {
522-
mach_fat_header fat;
523-
mach_fat_arch fat_arch[2];
524-
struct {
525-
mach_header_64 hdr;
526-
mach_segment_command_64 seg;
527-
mach_section_64 sec;
528-
mach_symtab_command sym;
529-
} arch[2];
530-
mach_nlist_64 sym_entry;
531-
uint8_t space[4096];
532-
} mach_fat_obj_64;
533471
]]
534472
local symname = '_'..LJBC_PREFIX..ctx.modname
535-
local isfat, is64, align, mobj = false, false, 4, "mach_obj"
536-
if ctx.arch == "x64" then
537-
is64, align, mobj = true, 8, "mach_obj_64"
538-
elseif ctx.arch == "arm" then
539-
isfat, mobj = true, "mach_fat_obj"
540-
elseif ctx.arch == "arm64" then
541-
is64, align, isfat, mobj = true, 8, true, "mach_fat_obj_64"
542-
else
543-
check(ctx.arch == "x86", "unsupported architecture for OSX")
473+
local cputype, cpusubtype = 0x01000007, 3
474+
if ctx.arch ~= "x64" then
475+
check(ctx.arch == "arm64", "unsupported architecture for OSX")
476+
cputype, cpusubtype = 0x0100000c, 0
544477
end
545478
local function aligned(v, a) return bit.band(v+a-1, -a) end
546-
local be32 = bit.bswap -- Mach-O FAT is BE, supported archs are LE.
547479

548480
-- Create Mach-O object and fill in header.
549-
local o = ffi.new(mobj)
550-
local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, align)
551-
local cputype = ({ x86={7}, x64={0x01000007}, arm={7,12}, arm64={0x01000007,0x0100000c} })[ctx.arch]
552-
local cpusubtype = ({ x86={3}, x64={3}, arm={3,9}, arm64={3,0} })[ctx.arch]
553-
if isfat then
554-
o.fat.magic = be32(0xcafebabe)
555-
o.fat.nfat_arch = be32(#cpusubtype)
556-
end
481+
local o = ffi.new("mach_obj_64")
482+
local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, 8)
557483

558484
-- Fill in sections and symbols.
559-
for i=0,#cpusubtype-1 do
560-
local ofs = 0
561-
if isfat then
562-
local a = o.fat_arch[i]
563-
a.cputype = be32(cputype[i+1])
564-
a.cpusubtype = be32(cpusubtype[i+1])
565-
-- Subsequent slices overlap each other to share data.
566-
ofs = ffi.offsetof(o, "arch") + i*ffi.sizeof(o.arch[0])
567-
a.offset = be32(ofs)
568-
a.size = be32(mach_size-ofs+#s)
569-
end
570-
local a = o.arch[i]
571-
a.hdr.magic = is64 and 0xfeedfacf or 0xfeedface
572-
a.hdr.cputype = cputype[i+1]
573-
a.hdr.cpusubtype = cpusubtype[i+1]
574-
a.hdr.filetype = 1
575-
a.hdr.ncmds = 2
576-
a.hdr.sizeofcmds = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)+ffi.sizeof(a.sym)
577-
a.seg.cmd = is64 and 0x19 or 0x1
578-
a.seg.cmdsize = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)
579-
a.seg.vmsize = #s
580-
a.seg.fileoff = mach_size-ofs
581-
a.seg.filesize = #s
582-
a.seg.maxprot = 1
583-
a.seg.initprot = 1
584-
a.seg.nsects = 1
585-
ffi.copy(a.sec.sectname, "__data")
586-
ffi.copy(a.sec.segname, "__DATA")
587-
a.sec.size = #s
588-
a.sec.offset = mach_size-ofs
589-
a.sym.cmd = 2
590-
a.sym.cmdsize = ffi.sizeof(a.sym)
591-
a.sym.symoff = ffi.offsetof(o, "sym_entry")-ofs
592-
a.sym.nsyms = 1
593-
a.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)-ofs
594-
a.sym.strsize = aligned(#symname+2, align)
595-
end
485+
o.hdr.magic = 0xfeedfacf
486+
o.hdr.cputype = cputype
487+
o.hdr.cpusubtype = cpusubtype
488+
o.hdr.filetype = 1
489+
o.hdr.ncmds = 2
490+
o.hdr.sizeofcmds = ffi.sizeof(o.seg)+ffi.sizeof(o.sec)+ffi.sizeof(o.sym)
491+
o.seg.cmd = 0x19
492+
o.seg.cmdsize = ffi.sizeof(o.seg)+ffi.sizeof(o.sec)
493+
o.seg.vmsize = #s
494+
o.seg.fileoff = mach_size
495+
o.seg.filesize = #s
496+
o.seg.maxprot = 1
497+
o.seg.initprot = 1
498+
o.seg.nsects = 1
499+
ffi.copy(o.sec.sectname, "__data")
500+
ffi.copy(o.sec.segname, "__DATA")
501+
o.sec.size = #s
502+
o.sec.offset = mach_size
503+
o.sym.cmd = 2
504+
o.sym.cmdsize = ffi.sizeof(o.sym)
505+
o.sym.symoff = ffi.offsetof(o, "sym_entry")
506+
o.sym.nsyms = 1
507+
o.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)
508+
o.sym.strsize = aligned(#symname+2, 8)
596509
o.sym_entry.type = 0xf
597510
o.sym_entry.sect = 1
598511
o.sym_entry.strx = 1

src/jit/dump.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,12 @@ local recdepth = 0
552552
local function fmterr(err, info)
553553
if type(err) == "number" then
554554
if type(info) == "function" then info = fmtfunc(info) end
555-
err = format(vmdef.traceerr[err], info)
555+
local fmt = vmdef.traceerr[err]
556+
if fmt == "NYI: bytecode %s" then
557+
local oidx = 6 * info
558+
info = sub(vmdef.bcnames, oidx+1, oidx+6)
559+
end
560+
err = format(fmt, info)
556561
end
557562
return err
558563
end

src/jit/v.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ local jit = require("jit")
6262
local jutil = require("jit.util")
6363
local vmdef = require("jit.vmdef")
6464
local funcinfo, traceinfo = jutil.funcinfo, jutil.traceinfo
65-
local type, format = type, string.format
65+
local type, sub, format = type, string.sub, string.format
6666
local stdout, stderr = io.stdout, io.stderr
6767

6868
-- Active flag and output file handle.
@@ -89,7 +89,12 @@ end
8989
local function fmterr(err, info)
9090
if type(err) == "number" then
9191
if type(info) == "function" then info = fmtfunc(info) end
92-
err = format(vmdef.traceerr[err], info)
92+
local fmt = vmdef.traceerr[err]
93+
if fmt == "NYI: bytecode %s" then
94+
local oidx = 6 * info
95+
info = sub(vmdef.bcnames, oidx+1, oidx+6)
96+
end
97+
err = format(fmt, info)
9398
end
9499
return err
95100
end

src/lib_ffi.c

+2-18
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ LJLIB_CF(ffi_new) LJLIB_REC(.)
513513
/* Handle ctype __gc metamethod. Use the fast lookup here. */
514514
cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);
515515
if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {
516-
GCtab *t = cts->finalizer;
516+
GCtab *t = tabref(G(L)->gcroot[GCROOT_FFI_FIN]);
517517
if (gcref(t->metatable)) {
518518
/* Add to finalizer table, if still enabled. */
519519
copyTV(L, lj_tab_set(L, t, o-1), tv);
@@ -765,7 +765,7 @@ LJLIB_CF(ffi_abi) LJLIB_REC(.)
765765
return 1;
766766
}
767767

768-
LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */
768+
LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to miscmap table. */
769769

770770
LJLIB_CF(ffi_metatype)
771771
{
@@ -791,8 +791,6 @@ LJLIB_CF(ffi_metatype)
791791
return 1;
792792
}
793793

794-
LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */
795-
796794
LJLIB_CF(ffi_gc) LJLIB_REC(.)
797795
{
798796
GCcdata *cd = ffi_checkcdata(L, 1);
@@ -825,19 +823,6 @@ LJLIB_PUSH(top-2) LJLIB_SET(arch)
825823

826824
/* ------------------------------------------------------------------------ */
827825

828-
/* Create special weak-keyed finalizer table. */
829-
static GCtab *ffi_finalizer(lua_State *L)
830-
{
831-
/* NOBARRIER: The table is new (marked white). */
832-
GCtab *t = lj_tab_new(L, 0, 1);
833-
settabV(L, L->top++, t);
834-
setgcref(t->metatable, obj2gco(t));
835-
setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
836-
lj_str_newlit(L, "k"));
837-
t->nomm = (uint8_t)(~(1u<<MM_mode));
838-
return t;
839-
}
840-
841826
/* Register FFI module as loaded. */
842827
static void ffi_register_module(lua_State *L)
843828
{
@@ -853,7 +838,6 @@ LUALIB_API int luaopen_ffi(lua_State *L)
853838
{
854839
CTState *cts = lj_ctype_init(L);
855840
settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1)));
856-
cts->finalizer = ffi_finalizer(L);
857841
LJ_LIB_REG(L, NULL, ffi_meta);
858842
/* NOBARRIER: basemt is a GC root. */
859843
setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));

src/lj_alloc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ static size_t release_unused_segments(mstate m)
10571057
mchunkptr p = align_as_chunk(base);
10581058
size_t psize = chunksize(p);
10591059
/* Can unmap if first chunk holds entire segment and not pinned */
1060-
if (!cinuse(p) && (char *)p + psize >= base + size - TOP_FOOT_SIZE) {
1060+
if (!cinuse(p) && (char *)p + psize == (char *)mem2chunk(sp)) {
10611061
tchunkptr tp = (tchunkptr)p;
10621062
if (p == m->dv) {
10631063
m->dv = 0;

src/lj_api.c

+1
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ LUA_API int lua_setmetatable(lua_State *L, int idx)
10521052
/* Flush cache, since traces specialize to basemt. But not during __gc. */
10531053
if (lj_trace_flushall(L))
10541054
lj_err_caller(L, LJ_ERR_NOGCMM);
1055+
o = index2adr(L, idx); /* Stack may have been reallocated. */
10551056
if (tvisbool(o)) {
10561057
/* NOBARRIER: basemt is a GC root. */
10571058
setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));

src/lj_arch.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
#define LJ_TARGET_POSIX (LUAJIT_OS > LUAJIT_OS_WINDOWS)
125125
#define LJ_TARGET_DLOPEN LJ_TARGET_POSIX
126126

127-
#if TARGET_OS_IPHONE
127+
#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
128128
#define LJ_TARGET_IOS 1
129129
#else
130130
#define LJ_TARGET_IOS 0

src/lj_asm_arm64.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ static void asm_href(ASMState *as, IRIns *ir, IROp merge)
787787
int destused = ra_used(ir);
788788
Reg dest = ra_dest(as, ir, allow);
789789
Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
790-
Reg tmp = RID_TMP, type = RID_NONE, key, tkey;
790+
Reg tmp = RID_TMP, type = RID_NONE, key = RID_NONE, tkey;
791791
IRRef refkey = ir->op2;
792792
IRIns *irkey = IR(refkey);
793793
int isk = irref_isk(refkey);

src/lj_cdata.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd)
8686

8787
void lj_cdata_setfin(lua_State *L, GCcdata *cd, GCobj *obj, uint32_t it)
8888
{
89-
GCtab *t = ctype_ctsG(G(L))->finalizer;
89+
GCtab *t = tabref(G(L)->gcroot[GCROOT_FFI_FIN]);
9090
if (gcref(t->metatable)) {
9191
/* Add cdata to finalizer table, if still enabled. */
9292
TValue *tv, tmp;

src/lj_ctype.c

+12
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,18 @@ CTState *lj_ctype_init(lua_State *L)
643643
return cts;
644644
}
645645

646+
/* Create special weak-keyed finalizer table. */
647+
void lj_ctype_initfin(lua_State *L)
648+
{
649+
/* NOBARRIER: The table is new (marked white). */
650+
GCtab *t = lj_tab_new(L, 0, 1);
651+
setgcref(t->metatable, obj2gco(t));
652+
setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
653+
lj_str_newlit(L, "k"));
654+
t->nomm = (uint8_t)(~(1u<<MM_mode));
655+
setgcref(G(L)->gcroot[GCROOT_FFI_FIN], obj2gco(t));
656+
}
657+
646658
/* Free C type table and state. */
647659
void lj_ctype_freestate(global_State *g)
648660
{

src/lj_ctype.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ typedef struct CTState {
177177
MSize sizetab; /* Size of C type table. */
178178
lua_State *L; /* Lua state (needed for errors and allocations). */
179179
global_State *g; /* Global state. */
180-
GCtab *finalizer; /* Map of cdata to finalizer. */
181180
GCtab *miscmap; /* Map of -CTypeID to metatable and cb slot to func. */
182181
CCallback cb; /* Temporary callback state. */
183182
CTypeID1 hash[CTHASH_SIZE]; /* Hash anchors for C type table. */
@@ -476,6 +475,7 @@ LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name);
476475
LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned);
477476
LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size);
478477
LJ_FUNC CTState *lj_ctype_init(lua_State *L);
478+
LJ_FUNC void lj_ctype_initfin(lua_State *L);
479479
LJ_FUNC void lj_ctype_freestate(global_State *g);
480480

481481
#endif

0 commit comments

Comments
 (0)