diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/host/genlibbc.lua | 75 |
2 files changed, 57 insertions, 20 deletions
diff --git a/src/Makefile b/src/Makefile index f3631a0d..95671792 100644 --- a/src/Makefile +++ b/src/Makefile | |||
@@ -559,7 +559,7 @@ clean: | |||
559 | $(HOST_RM) $(ALL_RM) | 559 | $(HOST_RM) $(ALL_RM) |
560 | 560 | ||
561 | libbc: | 561 | libbc: |
562 | ./$(LUAJIT_T) host/genlibbc.lua $(LJLIB_C) >host/buildvm_libbc.h | 562 | ./$(LUAJIT_T) host/genlibbc.lua -o host/buildvm_libbc.h $(LJLIB_C) |
563 | $(MAKE) all | 563 | $(MAKE) all |
564 | 564 | ||
565 | depend: | 565 | depend: |
diff --git a/src/host/genlibbc.lua b/src/host/genlibbc.lua index b0dbf17a..bdcfb588 100644 --- a/src/host/genlibbc.lua +++ b/src/host/genlibbc.lua | |||
@@ -6,15 +6,29 @@ | |||
6 | -- Released under the MIT license. See Copyright Notice in luajit.h | 6 | -- Released under the MIT license. See Copyright Notice in luajit.h |
7 | ---------------------------------------------------------------------------- | 7 | ---------------------------------------------------------------------------- |
8 | 8 | ||
9 | local function usage() | 9 | local function usage(arg) |
10 | io.stderr:write("Usage: ", arg and arg[0] or "genlibbc", " lib_*.c\n") | 10 | io.stderr:write("Usage: ", arg and arg[0] or "genlibbc", |
11 | " [-o buildvm_libbc.h] lib_*.c\n") | ||
11 | os.exit(1) | 12 | os.exit(1) |
12 | end | 13 | end |
13 | 14 | ||
14 | local function read_source() | 15 | local function parse_arg(arg) |
15 | if not (arg and arg[1]) then usage() end | 16 | local outfile = "-" |
17 | if not (arg and arg[1]) then | ||
18 | usage(arg) | ||
19 | end | ||
20 | if arg[1] == "-o" then | ||
21 | outfile = arg[2] | ||
22 | if not outfile then usage(arg) end | ||
23 | table.remove(arg, 1) | ||
24 | table.remove(arg, 1) | ||
25 | end | ||
26 | return outfile | ||
27 | end | ||
28 | |||
29 | local function read_files(names) | ||
16 | local src = "" | 30 | local src = "" |
17 | for _,name in ipairs(arg) do | 31 | for _,name in ipairs(names) do |
18 | local fp = assert(io.open(name)) | 32 | local fp = assert(io.open(name)) |
19 | src = src .. fp:read("*a") | 33 | src = src .. fp:read("*a") |
20 | fp:close() | 34 | fp:close() |
@@ -36,33 +50,56 @@ local function find_defs(src) | |||
36 | return defs | 50 | return defs |
37 | end | 51 | end |
38 | 52 | ||
39 | local function write_defs(fp, defs) | 53 | local function gen_header(defs) |
40 | fp:write("/* This is a generated file. DO NOT EDIT! */\n\n") | 54 | local t = {} |
41 | fp:write("static const int libbc_endian = ", | 55 | local function w(x) t[#t+1] = x end |
42 | string.byte(string.dump(function() end), 5) % 2, ";\n\n") | 56 | w("/* This is a generated file. DO NOT EDIT! */\n\n") |
57 | w("static const int libbc_endian = ") | ||
58 | w(string.byte(string.dump(function() end), 5) % 2) | ||
59 | w(";\n\n") | ||
43 | local s = "" | 60 | local s = "" |
44 | for _,name in ipairs(defs) do | 61 | for _,name in ipairs(defs) do |
45 | s = s .. defs[name] | 62 | s = s .. defs[name] |
46 | end | 63 | end |
47 | fp:write("static const uint8_t libbc_code[] = {\n") | 64 | w("static const uint8_t libbc_code[] = {\n") |
48 | local n = 0 | 65 | local n = 0 |
49 | for i=1,#s do | 66 | for i=1,#s do |
50 | local x = string.byte(s, i) | 67 | local x = string.byte(s, i) |
51 | fp:write(x, ",") | 68 | w(x); w(",") |
52 | n = n + (x < 10 and 2 or (x < 100 and 3 or 4)) | 69 | n = n + (x < 10 and 2 or (x < 100 and 3 or 4)) |
53 | if n >= 75 then n = 0; fp:write("\n") end | 70 | if n >= 75 then n = 0; w("\n") end |
54 | end | 71 | end |
55 | fp:write("0\n};\n\n") | 72 | w("0\n};\n\n") |
56 | fp:write("static const struct { const char *name; int ofs; } libbc_map[] = {\n") | 73 | w("static const struct { const char *name; int ofs; } libbc_map[] = {\n") |
57 | local m = 0 | 74 | local m = 0 |
58 | for _,name in ipairs(defs) do | 75 | for _,name in ipairs(defs) do |
59 | fp:write('{"', name, '",', m, '},\n') | 76 | w('{"'); w(name); w('",'); w(m) w('},\n') |
60 | m = m + #defs[name] | 77 | m = m + #defs[name] |
61 | end | 78 | end |
62 | fp:write("{NULL,", m, "}\n};\n\n") | 79 | w("{NULL,"); w(m); w("}\n};\n\n") |
63 | fp:flush() | 80 | return table.concat(t) |
64 | end | 81 | end |
65 | 82 | ||
66 | local src = read_source() | 83 | local function write_file(name, data) |
84 | if name == "-" then | ||
85 | assert(io.write(data)) | ||
86 | assert(io.flush()) | ||
87 | else | ||
88 | local fp = io.open(name) | ||
89 | if fp then | ||
90 | local old = fp:read("*a") | ||
91 | fp:close() | ||
92 | if data == old then return end | ||
93 | end | ||
94 | fp = assert(io.open(name, "w")) | ||
95 | assert(fp:write(data)) | ||
96 | assert(fp:close()) | ||
97 | end | ||
98 | end | ||
99 | |||
100 | local outfile = parse_arg(arg) | ||
101 | local src = read_files(arg) | ||
67 | local defs = find_defs(src) | 102 | local defs = find_defs(src) |
68 | write_defs(io.stdout, defs) | 103 | local hdr = gen_header(defs) |
104 | write_file(outfile, hdr) | ||
105 | |||