diff options
Diffstat (limited to '')
-rw-r--r-- | src/host/genlibbc.lua | 47 |
1 files changed, 36 insertions, 11 deletions
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) |