aboutsummaryrefslogtreecommitdiff
path: root/src/jit/bcsave.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/jit/bcsave.lua')
-rw-r--r--src/jit/bcsave.lua47
1 files changed, 24 insertions, 23 deletions
diff --git a/src/jit/bcsave.lua b/src/jit/bcsave.lua
index 7d19cb064..544b506c1 100644
--- a/src/jit/bcsave.lua
+++ b/src/jit/bcsave.lua
@@ -20,6 +20,7 @@ local LJBC_PREFIX = "luaJIT_BC_"
20local type, assert = type, assert 20local type, assert = type, assert
21local format = string.format 21local format = string.format
22local tremove, tconcat = table.remove, table.concat 22local tremove, tconcat = table.remove, table.concat
23local bswap = bit.bswap
23 24
24------------------------------------------------------------------------------ 25------------------------------------------------------------------------------
25 26
@@ -111,7 +112,7 @@ local map_os = {
111local function checkarg(str, map, err) 112local function checkarg(str, map, err)
112 str = str:lower() 113 str = str:lower()
113 local s = check(map[str], "unknown ", err) 114 local s = check(map[str], "unknown ", err)
114 return type(s) == "string" and s or str 115 return type(s) == "string" ? s : str
115end 116end
116 117
117local function detecttype(str) 118local function detecttype(str)
@@ -142,7 +143,7 @@ end
142 143
143local function bcsave_tail(fp, output, s) 144local function bcsave_tail(fp, output, s)
144 local ok, err = fp:write(s) 145 local ok, err = fp:write(s)
145 if ok and output ~= "-" then ok, err = fp:close() end 146 if ok and output != "-" then ok, err = fp:close() end
146 check(ok, "cannot write ", output, ": ", err) 147 check(ok, "cannot write ", output, ": ", err)
147end 148end
148 149
@@ -179,12 +180,12 @@ static const unsigned char %s%s[] = {
179 local t, n, m = {}, 0, 0 180 local t, n, m = {}, 0, 0
180 for i=1,#s do 181 for i=1,#s do
181 local b = tostring(string.byte(s, i)) 182 local b = tostring(string.byte(s, i))
182 m = m + #b + 1 183 m += #b + 1
183 if m > 78 then 184 if m > 78 then
184 fp:write(tconcat(t, ",", 1, n), ",\n") 185 fp:write(tconcat(t, ",", 1, n), ",\n")
185 n, m = 0, #b + 1 186 n, m = 0, #b + 1
186 end 187 end
187 n = n + 1 188 n += 1
188 t[n] = b 189 t[n] = b
189 end 190 end
190 bcsave_tail(fp, output, tconcat(t, ",", 1, n).."\n};\n") 191 bcsave_tail(fp, output, tconcat(t, ",", 1, n).."\n};\n")
@@ -248,19 +249,19 @@ typedef struct {
248 -- Handle different host/target endianess. 249 -- Handle different host/target endianess.
249 local function f32(x) return x end 250 local function f32(x) return x end
250 local f16, fofs = f32, f32 251 local f16, fofs = f32, f32
251 if ffi.abi("be") ~= isbe then 252 if ffi.abi("be") != isbe then
252 f32 = bit.bswap 253 f32 = bswap
253 function f16(x) return bit.rshift(bit.bswap(x), 16) end 254 function f16(x) return bswap(x) >> 16 end
254 if is64 then 255 if is64 then
255 local two32 = ffi.cast("int64_t", 2^32) 256 local two32 = ffi.cast("int64_t", 2^32)
256 function fofs(x) return bit.bswap(x)*two32 end 257 function fofs(x) return bswap(x)*two32 end
257 else 258 else
258 fofs = f32 259 fofs = f32
259 end 260 end
260 end 261 end
261 262
262 -- Create ELF object and fill in header. 263 -- Create ELF object and fill in header.
263 local o = ffi.new(is64 and "ELF64obj" or "ELF32obj") 264 local o = ffi.new(is64 ? "ELF64obj" : "ELF32obj")
264 local hdr = o.hdr 265 local hdr = o.hdr
265 if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi. 266 if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi.
266 local bf = assert(io.open("/bin/ls", "rb")) 267 local bf = assert(io.open("/bin/ls", "rb"))
@@ -272,8 +273,8 @@ typedef struct {
272 hdr.emagic = "\127ELF" 273 hdr.emagic = "\127ELF"
273 hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0 274 hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0
274 end 275 end
275 hdr.eclass = is64 and 2 or 1 276 hdr.eclass = is64 ? 2 : 1
276 hdr.eendian = isbe and 2 or 1 277 hdr.eendian = isbe ? 2 : 1
277 hdr.eversion = 1 278 hdr.eversion = 1
278 hdr.type = f16(1) 279 hdr.type = f16(1)
279 hdr.machine = f16(ai.m) 280 hdr.machine = f16(ai.m)
@@ -294,7 +295,7 @@ typedef struct {
294 sect.align = fofs(1) 295 sect.align = fofs(1)
295 sect.name = f32(ofs) 296 sect.name = f32(ofs)
296 ffi.copy(o.space+ofs, name) 297 ffi.copy(o.space+ofs, name)
297 ofs = ofs + #name+1 298 ofs += #name+1
298 end 299 end
299 o.sect[1].type = f32(2) -- .symtab 300 o.sect[1].type = f32(2) -- .symtab
300 o.sect[1].link = f32(3) 301 o.sect[1].link = f32(3)
@@ -314,7 +315,7 @@ typedef struct {
314 o.sect[3].ofs = fofs(sofs + ofs) 315 o.sect[3].ofs = fofs(sofs + ofs)
315 o.sect[3].size = fofs(#symname+2) 316 o.sect[3].size = fofs(#symname+2)
316 ffi.copy(o.space+ofs+1, symname) 317 ffi.copy(o.space+ofs+1, symname)
317 ofs = ofs + #symname + 2 318 ofs += #symname + 2
318 o.sect[4].type = f32(1) -- .rodata 319 o.sect[4].type = f32(1) -- .rodata
319 o.sect[4].flags = fofs(2) 320 o.sect[4].flags = fofs(2)
320 o.sect[4].ofs = fofs(sofs + ofs) 321 o.sect[4].ofs = fofs(sofs + ofs)
@@ -381,8 +382,8 @@ typedef struct {
381 local function f32(x) return x end 382 local function f32(x) return x end
382 local f16 = f32 383 local f16 = f32
383 if ffi.abi("be") then 384 if ffi.abi("be") then
384 f32 = bit.bswap 385 f32 = bswap
385 function f16(x) return bit.rshift(bit.bswap(x), 16) end 386 function f16(x) return bswap(x) >> 16 end
386 end 387 end
387 388
388 -- Create PE object and fill in header. 389 -- Create PE object and fill in header.
@@ -422,7 +423,7 @@ typedef struct {
422 o.strtabsize = f32(ofs + 4) 423 o.strtabsize = f32(ofs + 4)
423 o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs) 424 o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs)
424 ffi.copy(o.space + ofs, symexport) 425 ffi.copy(o.space + ofs, symexport)
425 ofs = ofs + #symexport 426 ofs += #symexport
426 o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs) 427 o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs)
427 428
428 -- Write PE object file. 429 -- Write PE object file.
@@ -475,11 +476,11 @@ typedef struct {
475]] 476]]
476 local symname = '_'..LJBC_PREFIX..ctx.modname 477 local symname = '_'..LJBC_PREFIX..ctx.modname
477 local cputype, cpusubtype = 0x01000007, 3 478 local cputype, cpusubtype = 0x01000007, 3
478 if ctx.arch ~= "x64" then 479 if ctx.arch != "x64" then
479 check(ctx.arch == "arm64", "unsupported architecture for OSX") 480 check(ctx.arch == "arm64", "unsupported architecture for OSX")
480 cputype, cpusubtype = 0x0100000c, 0 481 cputype, cpusubtype = 0x0100000c, 0
481 end 482 end
482 local function aligned(v, a) return bit.band(v+a-1, -a) end 483 local function aligned(v, a) return v+a-1 & -a end
483 484
484 -- Create Mach-O object and fill in header. 485 -- Create Mach-O object and fill in header.
485 local o = ffi.new("mach_obj_64") 486 local o = ffi.new("mach_obj_64")
@@ -579,7 +580,7 @@ local function docmd(...)
579 local gc64 = "" 580 local gc64 = ""
580 while n <= #arg do 581 while n <= #arg do
581 local a = arg[n] 582 local a = arg[n]
582 if type(a) == "string" and a:sub(1, 1) == "-" and a ~= "-" then 583 if type(a) == "string" and a:sub(1, 1) == "-" and a != "-" then
583 tremove(arg, n) 584 tremove(arg, n)
584 if a == "--" then break end 585 if a == "--" then break end
585 for m=2,#a do 586 for m=2,#a do
@@ -595,9 +596,9 @@ local function docmd(...)
595 elseif opt == "d" then 596 elseif opt == "d" then
596 ctx.mode = ctx.mode .. opt 597 ctx.mode = ctx.mode .. opt
597 else 598 else
598 if arg[n] == nil or m ~= #a then usage() end 599 if arg[n] == nil or m != #a then usage() end
599 if opt == "e" then 600 if opt == "e" then
600 if n ~= 1 then usage() end 601 if n != 1 then usage() end
601 ctx.string = true 602 ctx.string = true
602 elseif opt == "n" then 603 elseif opt == "n" then
603 ctx.modname = checkmodname(tremove(arg, n)) 604 ctx.modname = checkmodname(tremove(arg, n))
@@ -615,7 +616,7 @@ local function docmd(...)
615 end 616 end
616 end 617 end
617 else 618 else
618 n = n + 1 619 n += 1
619 end 620 end
620 end 621 end
621 ctx.mode = ctx.mode .. strip .. gc64 622 ctx.mode = ctx.mode .. strip .. gc64
@@ -623,7 +624,7 @@ local function docmd(...)
623 if #arg == 0 or #arg > 2 then usage() end 624 if #arg == 0 or #arg > 2 then usage() end
624 bclist(ctx, arg[1], arg[2] or "-") 625 bclist(ctx, arg[1], arg[2] or "-")
625 else 626 else
626 if #arg ~= 2 then usage() end 627 if #arg != 2 then usage() end
627 bcsave(ctx, arg[1], arg[2]) 628 bcsave(ctx, arg[1], arg[2])
628 end 629 end
629end 630end