diff options
author | Mike Pall <mike> | 2022-06-13 21:23:16 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2022-06-13 21:23:16 +0200 |
commit | 0065cff7e0222c234b75a71e72b8883df5d000c2 (patch) | |
tree | ac76a239b9a59a44a5f4f145da457d31b83d1553 /src/host | |
parent | 6053b04815ecbc8eec1e361ceb64e68fb8fac1b3 (diff) | |
download | luajit-0065cff7e0222c234b75a71e72b8883df5d000c2.tar.gz luajit-0065cff7e0222c234b75a71e72b8883df5d000c2.tar.bz2 luajit-0065cff7e0222c234b75a71e72b8883df5d000c2.zip |
Make embedded bytecode readable and forward-compatible.
Diffstat (limited to 'src/host')
-rw-r--r-- | src/host/buildvm_libbc.h | 93 | ||||
-rw-r--r-- | src/host/genlibbc.lua | 47 |
2 files changed, 95 insertions, 45 deletions
diff --git a/src/host/buildvm_libbc.h b/src/host/buildvm_libbc.h index 8f07f436..276463b2 100644 --- a/src/host/buildvm_libbc.h +++ b/src/host/buildvm_libbc.h | |||
@@ -4,42 +4,67 @@ static const int libbc_endian = 0; | |||
4 | 4 | ||
5 | static const uint8_t libbc_code[] = { | 5 | static const uint8_t libbc_code[] = { |
6 | #if LJ_FR2 | 6 | #if LJ_FR2 |
7 | 0,1,2,0,0,1,2,24,1,0,0,76,1,2,0,241,135,158,166,3,220,203,178,130,4,0,1,2,0, | 7 | /* math.deg */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,241,135,158,166,3, |
8 | 0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0,1,2,0,0,0,3, | 8 | 220,203,178,130,4, |
9 | 16,0,5,0,21,1,0,0,76,1,2,0,0,2,10,0,0,0,15,16,0,12,0,16,1,9,0,41,2,1,0,21,3, | 9 | /* math.rad */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,243,244,148,165,20, |
10 | 0,0,41,4,1,0,77,2,8,128,18,6,1,0,18,8,5,0,59,9,5,0,66,6,3,2,10,6,0,0,88,7,1, | 10 | 198,190,199,252,3, |
11 | 128,76,6,2,0,79,2,248,127,75,0,1,0,0,2,11,0,0,1,16,16,0,12,0,16,1,9,0,43,2, | 11 | /* string.len */ 0,1,2,0,0,0,3,BC_ISTYPE,0,5,0,BC_LEN,1,0,0,BC_RET1,1,2,0, |
12 | 0,0,18,3,0,0,42,4,0,0,88,5,7,128,18,7,1,0,18,9,5,0,18,10,6,0,66,7,3,2,10,7, | 12 | /* table.foreachi */ 0,2,10,0,0,0,15,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0, |
13 | 0,0,88,8,1,128,76,7,2,0,70,5,3,3,82,5,247,127,75,0,1,0,1,255,255,249,255,15, | 13 | BC_KSHORT,2,1,0,BC_LEN,3,0,0,BC_KSHORT,4,1,0,BC_FORI,2,8,128,BC_MOV,6,1,0, |
14 | 0,1,2,0,0,0,3,16,0,12,0,21,1,0,0,76,1,2,0,0,2,10,0,0,2,30,16,0,12,0,21,2,0, | 14 | BC_MOV,8,5,0,BC_TGETR,9,5,0,BC_CALL,6,3,2,BC_ISEQP,6,0,0,BC_JMP,7,1,128, |
15 | 0,11,1,0,0,88,3,7,128,8,2,0,0,88,3,23,128,59,3,2,0,43,4,0,0,64,4,2,0,76,3,2, | 15 | BC_RET1,6,2,0,BC_FORL,2,248,127,BC_RET0,0,1,0, |
16 | 0,88,3,18,128,16,1,14,0,41,3,1,0,3,3,1,0,88,3,14,128,3,1,2,0,88,3,12,128,59, | 16 | /* table.foreach */ 0,2,11,0,0,1,16,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0,BC_KPRI, |
17 | 3,1,0,22,4,1,1,18,5,2,0,41,6,1,0,77,4,4,128,23,8,1,7,59,9,7,0,64,9,8,0,79,4, | 17 | 2,0,0,BC_MOV,3,0,0,BC_KNUM,4,0,0,BC_JMP,5,7,128,BC_MOV,7,1,0,BC_MOV,9,5,0, |
18 | 252,127,43,4,0,0,64,4,2,0,76,3,2,0,75,0,1,0,0,2,0,5,12,0,0,0,35,16,0,12,0,16, | 18 | BC_MOV,10,6,0,BC_CALL,7,3,2,BC_ISEQP,7,0,0,BC_JMP,8,1,128,BC_RET1,7,2,0, |
19 | 1,14,0,16,2,14,0,16,3,14,0,11,4,0,0,88,5,1,128,18,4,0,0,16,4,12,0,3,1,2,0,88, | 19 | BC_ITERN,5,3,3,BC_ITERL,5,247,127,BC_RET0,0,1,0,1,255,255,249,255,15, |
20 | 5,24,128,33,5,1,3,0,2,3,0,88,6,4,128,2,3,1,0,88,6,2,128,4,4,0,0,88,6,9,128, | 20 | /* table.getn */ 0,1,2,0,0,0,3,BC_ISTYPE,0,12,0,BC_LEN,1,0,0,BC_RET1,1,2,0, |
21 | 18,6,1,0,18,7,2,0,41,8,1,0,77,6,4,128,32,10,5,9,59,11,9,0,64,11,10,4,79,6,252, | 21 | /* table.remove */ 0,2,10,0,0,2,30,BC_ISTYPE,0,12,0,BC_LEN,2,0,0,BC_ISNEP,1,0, |
22 | 127,88,6,8,128,18,6,2,0,18,7,1,0,41,8,255,255,77,6,4,128,32,10,5,9,59,11,9, | 22 | 0,BC_JMP,3,7,128,BC_ISEQN,2,0,0,BC_JMP,3,23,128,BC_TGETR,3,2,0,BC_KPRI,4,0,0, |
23 | 0,64,11,10,4,79,6,252,127,76,4,2,0,0 | 23 | BC_TSETR,4,2,0,BC_RET1,3,2,0,BC_JMP,3,18,128,BC_ISTYPE,1,14,0,BC_KSHORT,3,1,0, |
24 | BC_ISGT,3,1,0,BC_JMP,3,14,128,BC_ISGT,1,2,0,BC_JMP,3,12,128,BC_TGETR,3,1,0, | ||
25 | BC_ADDVN,4,1,1,BC_MOV,5,2,0,BC_KSHORT,6,1,0,BC_FORI,4,4,128,BC_SUBVN,8,1,7, | ||
26 | BC_TGETR,9,7,0,BC_TSETR,9,8,0,BC_FORL,4,252,127,BC_KPRI,4,0,0,BC_TSETR,4,2,0, | ||
27 | BC_RET1,3,2,0,BC_RET0,0,1,0,0,2, | ||
28 | /* table.move */ 0,5,12,0,0,0,35,BC_ISTYPE,0,12,0,BC_ISTYPE,1,14,0,BC_ISTYPE, | ||
29 | 2,14,0,BC_ISTYPE,3,14,0,BC_ISNEP,4,0,0,BC_JMP,5,1,128,BC_MOV,4,0,0,BC_ISTYPE, | ||
30 | 4,12,0,BC_ISGT,1,2,0,BC_JMP,5,24,128,BC_SUBVV,5,1,3,BC_ISLT,2,3,0,BC_JMP,6,4, | ||
31 | 128,BC_ISLE,3,1,0,BC_JMP,6,2,128,BC_ISEQV,4,0,0,BC_JMP,6,9,128,BC_MOV,6,1,0, | ||
32 | BC_MOV,7,2,0,BC_KSHORT,8,1,0,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0, | ||
33 | BC_TSETR,11,10,4,BC_FORL,6,252,127,BC_JMP,6,8,128,BC_MOV,6,2,0,BC_MOV,7,1,0, | ||
34 | BC_KSHORT,8,255,255,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0,BC_TSETR, | ||
35 | 11,10,4,BC_FORL,6,252,127,BC_RET1,4,2,0, | ||
24 | #else | 36 | #else |
25 | 0,1,2,0,0,1,2,24,1,0,0,76,1,2,0,241,135,158,166,3,220,203,178,130,4,0,1,2,0, | 37 | /* math.deg */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,241,135,158,166,3, |
26 | 0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0,1,2,0,0,0,3, | 38 | 220,203,178,130,4, |
27 | 16,0,5,0,21,1,0,0,76,1,2,0,0,2,9,0,0,0,15,16,0,12,0,16,1,9,0,41,2,1,0,21,3, | 39 | /* math.rad */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,243,244,148,165,20, |
28 | 0,0,41,4,1,0,77,2,8,128,18,6,1,0,18,7,5,0,59,8,5,0,66,6,3,2,10,6,0,0,88,7,1, | 40 | 198,190,199,252,3, |
29 | 128,76,6,2,0,79,2,248,127,75,0,1,0,0,2,10,0,0,1,16,16,0,12,0,16,1,9,0,43,2, | 41 | /* string.len */ 0,1,2,0,0,0,3,BC_ISTYPE,0,5,0,BC_LEN,1,0,0,BC_RET1,1,2,0, |
30 | 0,0,18,3,0,0,42,4,0,0,88,5,7,128,18,7,1,0,18,8,5,0,18,9,6,0,66,7,3,2,10,7,0, | 42 | /* table.foreachi */ 0,2,9,0,0,0,15,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0, |
31 | 0,88,8,1,128,76,7,2,0,70,5,3,3,82,5,247,127,75,0,1,0,1,255,255,249,255,15,0, | 43 | BC_KSHORT,2,1,0,BC_LEN,3,0,0,BC_KSHORT,4,1,0,BC_FORI,2,8,128,BC_MOV,6,1,0, |
32 | 1,2,0,0,0,3,16,0,12,0,21,1,0,0,76,1,2,0,0,2,10,0,0,2,30,16,0,12,0,21,2,0,0, | 44 | BC_MOV,7,5,0,BC_TGETR,8,5,0,BC_CALL,6,3,2,BC_ISEQP,6,0,0,BC_JMP,7,1,128, |
33 | 11,1,0,0,88,3,7,128,8,2,0,0,88,3,23,128,59,3,2,0,43,4,0,0,64,4,2,0,76,3,2,0, | 45 | BC_RET1,6,2,0,BC_FORL,2,248,127,BC_RET0,0,1,0, |
34 | 88,3,18,128,16,1,14,0,41,3,1,0,3,3,1,0,88,3,14,128,3,1,2,0,88,3,12,128,59,3, | 46 | /* table.foreach */ 0,2,10,0,0,1,16,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0,BC_KPRI, |
35 | 1,0,22,4,1,1,18,5,2,0,41,6,1,0,77,4,4,128,23,8,1,7,59,9,7,0,64,9,8,0,79,4,252, | 47 | 2,0,0,BC_MOV,3,0,0,BC_KNUM,4,0,0,BC_JMP,5,7,128,BC_MOV,7,1,0,BC_MOV,8,5,0, |
36 | 127,43,4,0,0,64,4,2,0,76,3,2,0,75,0,1,0,0,2,0,5,12,0,0,0,35,16,0,12,0,16,1, | 48 | BC_MOV,9,6,0,BC_CALL,7,3,2,BC_ISEQP,7,0,0,BC_JMP,8,1,128,BC_RET1,7,2,0, |
37 | 14,0,16,2,14,0,16,3,14,0,11,4,0,0,88,5,1,128,18,4,0,0,16,4,12,0,3,1,2,0,88, | 49 | BC_ITERN,5,3,3,BC_ITERL,5,247,127,BC_RET0,0,1,0,1,255,255,249,255,15, |
38 | 5,24,128,33,5,1,3,0,2,3,0,88,6,4,128,2,3,1,0,88,6,2,128,4,4,0,0,88,6,9,128, | 50 | /* table.getn */ 0,1,2,0,0,0,3,BC_ISTYPE,0,12,0,BC_LEN,1,0,0,BC_RET1,1,2,0, |
39 | 18,6,1,0,18,7,2,0,41,8,1,0,77,6,4,128,32,10,5,9,59,11,9,0,64,11,10,4,79,6,252, | 51 | /* table.remove */ 0,2,10,0,0,2,30,BC_ISTYPE,0,12,0,BC_LEN,2,0,0,BC_ISNEP,1,0, |
40 | 127,88,6,8,128,18,6,2,0,18,7,1,0,41,8,255,255,77,6,4,128,32,10,5,9,59,11,9, | 52 | 0,BC_JMP,3,7,128,BC_ISEQN,2,0,0,BC_JMP,3,23,128,BC_TGETR,3,2,0,BC_KPRI,4,0,0, |
41 | 0,64,11,10,4,79,6,252,127,76,4,2,0,0 | 53 | BC_TSETR,4,2,0,BC_RET1,3,2,0,BC_JMP,3,18,128,BC_ISTYPE,1,14,0,BC_KSHORT,3,1,0, |
54 | BC_ISGT,3,1,0,BC_JMP,3,14,128,BC_ISGT,1,2,0,BC_JMP,3,12,128,BC_TGETR,3,1,0, | ||
55 | BC_ADDVN,4,1,1,BC_MOV,5,2,0,BC_KSHORT,6,1,0,BC_FORI,4,4,128,BC_SUBVN,8,1,7, | ||
56 | BC_TGETR,9,7,0,BC_TSETR,9,8,0,BC_FORL,4,252,127,BC_KPRI,4,0,0,BC_TSETR,4,2,0, | ||
57 | BC_RET1,3,2,0,BC_RET0,0,1,0,0,2, | ||
58 | /* table.move */ 0,5,12,0,0,0,35,BC_ISTYPE,0,12,0,BC_ISTYPE,1,14,0,BC_ISTYPE, | ||
59 | 2,14,0,BC_ISTYPE,3,14,0,BC_ISNEP,4,0,0,BC_JMP,5,1,128,BC_MOV,4,0,0,BC_ISTYPE, | ||
60 | 4,12,0,BC_ISGT,1,2,0,BC_JMP,5,24,128,BC_SUBVV,5,1,3,BC_ISLT,2,3,0,BC_JMP,6,4, | ||
61 | 128,BC_ISLE,3,1,0,BC_JMP,6,2,128,BC_ISEQV,4,0,0,BC_JMP,6,9,128,BC_MOV,6,1,0, | ||
62 | BC_MOV,7,2,0,BC_KSHORT,8,1,0,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0, | ||
63 | BC_TSETR,11,10,4,BC_FORL,6,252,127,BC_JMP,6,8,128,BC_MOV,6,2,0,BC_MOV,7,1,0, | ||
64 | BC_KSHORT,8,255,255,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0,BC_TSETR, | ||
65 | 11,10,4,BC_FORL,6,252,127,BC_RET1,4,2,0, | ||
42 | #endif | 66 | #endif |
67 | 0 | ||
43 | }; | 68 | }; |
44 | 69 | ||
45 | static const struct { const char *name; int ofs; } libbc_map[] = { | 70 | static const struct { const char *name; int ofs; } libbc_map[] = { |
diff --git a/src/host/genlibbc.lua b/src/host/genlibbc.lua index 072a7495..ba18812c 100644 --- a/src/host/genlibbc.lua +++ b/src/host/genlibbc.lua | |||
@@ -79,9 +79,11 @@ local name2itype = { | |||
79 | str = 5, func = 9, tab = 12, int = 14, num = 15 | 79 | str = 5, func = 9, tab = 12, int = 14, num = 15 |
80 | } | 80 | } |
81 | 81 | ||
82 | local BC = {} | 82 | local BC, BCN = {}, {} |
83 | for i=0,#bcnames/6-1 do | 83 | for i=0,#bcnames/6-1 do |
84 | BC[string.gsub(string.sub(bcnames, i*6+1, i*6+6), " ", "")] = i | 84 | local name = bcnames:sub(i*6+1, i*6+6):gsub(" ", "") |
85 | BC[name] = i | ||
86 | BCN[i] = name | ||
85 | end | 87 | end |
86 | local xop, xra = isbe and 3 or 0, isbe and 2 or 1 | 88 | local xop, xra = isbe and 3 or 0, isbe and 2 or 1 |
87 | local xrc, xrb = isbe and 1 or 2, isbe and 0 or 3 | 89 | local xrc, xrb = isbe and 1 or 2, isbe and 0 or 3 |
@@ -96,6 +98,7 @@ local function fixup_dump(dump, fixup) | |||
96 | p = read_uleb128(p) | 98 | p = read_uleb128(p) |
97 | p = read_uleb128(p) | 99 | p = read_uleb128(p) |
98 | p, sizebc = read_uleb128(p) | 100 | p, sizebc = read_uleb128(p) |
101 | local startbc = tonumber(p - start) | ||
99 | local rawtab = {} | 102 | local rawtab = {} |
100 | for i=0,sizebc-1 do | 103 | for i=0,sizebc-1 do |
101 | local op = p[xop] | 104 | local op = p[xop] |
@@ -132,7 +135,7 @@ local function fixup_dump(dump, fixup) | |||
132 | local ndump = ffi.string(start, n) | 135 | local ndump = ffi.string(start, n) |
133 | -- Fixup hi-part of 0x4dp80 to LJ_KEYINDEX. | 136 | -- Fixup hi-part of 0x4dp80 to LJ_KEYINDEX. |
134 | ndump = ndump:gsub("\x80\x80\xcd\xaa\x04", "\xff\xff\xf9\xff\x0f") | 137 | ndump = ndump:gsub("\x80\x80\xcd\xaa\x04", "\xff\xff\xf9\xff\x0f") |
135 | return ndump | 138 | return { dump = ndump, startbc = startbc, sizebc = sizebc } |
136 | end | 139 | end |
137 | 140 | ||
138 | local function find_defs(src) | 141 | local function find_defs(src) |
@@ -152,24 +155,46 @@ local function gen_header(defs) | |||
152 | local function w(x) t[#t+1] = x end | 155 | local function w(x) t[#t+1] = x end |
153 | w("/* This is a generated file. DO NOT EDIT! */\n\n") | 156 | w("/* This is a generated file. DO NOT EDIT! */\n\n") |
154 | w("static const int libbc_endian = ") w(isbe and 1 or 0) w(";\n\n") | 157 | w("static const int libbc_endian = ") w(isbe and 1 or 0) w(";\n\n") |
155 | local s = "" | 158 | local s, sb = "", "" |
156 | for _,name in ipairs(defs) do | 159 | for i,name in ipairs(defs) do |
157 | s = s .. defs[name] | 160 | local d = defs[name] |
161 | s = s .. d.dump | ||
162 | sb = sb .. string.char(i) .. ("\0"):rep(d.startbc - 1) | ||
163 | .. (isbe and "\0\0\0\255" or "\255\0\0\0"):rep(d.sizebc) | ||
164 | .. ("\0"):rep(#d.dump - d.startbc - d.sizebc*4) | ||
158 | end | 165 | end |
159 | w("static const uint8_t libbc_code[] = {\n") | 166 | w("static const uint8_t libbc_code[] = {\n") |
160 | local n = 0 | 167 | local n = 0 |
161 | for i=1,#s do | 168 | for i=1,#s do |
162 | local x = string.byte(s, i) | 169 | local x = string.byte(s, i) |
163 | w(x); w(",") | 170 | local xb = string.byte(sb, i) |
164 | n = n + (x < 10 and 2 or (x < 100 and 3 or 4)) | 171 | if xb == 255 then |
165 | if n >= 75 then n = 0; w("\n") end | 172 | local name = BCN[x] |
173 | local m = #name + 4 | ||
174 | if n + m > 78 then n = 0; w("\n") end | ||
175 | n = n + m | ||
176 | w("BC_"); w(name) | ||
177 | else | ||
178 | local m = x < 10 and 2 or (x < 100 and 3 or 4) | ||
179 | if xb == 0 then | ||
180 | if n + m > 78 then n = 0; w("\n") end | ||
181 | else | ||
182 | local name = defs[xb]:gsub("_", ".") | ||
183 | if n ~= 0 then w("\n") end | ||
184 | w("/* "); w(name); w(" */ ") | ||
185 | n = #name + 7 | ||
186 | end | ||
187 | n = n + m | ||
188 | w(x) | ||
189 | end | ||
190 | w(",") | ||
166 | end | 191 | end |
167 | w("0\n};\n\n") | 192 | w("\n0\n};\n\n") |
168 | w("static const struct { const char *name; int ofs; } libbc_map[] = {\n") | 193 | w("static const struct { const char *name; int ofs; } libbc_map[] = {\n") |
169 | local m = 0 | 194 | local m = 0 |
170 | for _,name in ipairs(defs) do | 195 | for _,name in ipairs(defs) do |
171 | w('{"'); w(name); w('",'); w(m) w('},\n') | 196 | w('{"'); w(name); w('",'); w(m) w('},\n') |
172 | m = m + #defs[name] | 197 | m = m + #defs[name].dump |
173 | end | 198 | end |
174 | w("{NULL,"); w(m); w("}\n};\n\n") | 199 | w("{NULL,"); w(m); w("}\n};\n\n") |
175 | return table.concat(t) | 200 | return table.concat(t) |