Don't bother to chain NOPs.
[luajit-2.0.git] / lib / bcsave.lua
blobc34bec8960df0876feb75ae1af17915601294a87
1 ----------------------------------------------------------------------------
2 -- LuaJIT module to save/list bytecode.
3 --
4 -- Copyright (C) 2005-2011 Mike Pall. All rights reserved.
5 -- Released under the MIT/X license. See Copyright Notice in luajit.h
6 ----------------------------------------------------------------------------
7 --
8 -- This module saves or lists the bytecode for an input file.
9 -- It's run by the -b command line option.
11 ------------------------------------------------------------------------------
13 -- Cache some library functions and objects.
14 local jit = require("jit")
15 assert(jit.version_num == 20000, "LuaJIT core/library version mismatch")
17 ------------------------------------------------------------------------------
19 local function usage()
20 io.stderr:write[[
21 Save LuaJIT bytecode: luajit -b[options] input output
22 -l Only list bytecode.
23 -s Strip debug info (default).
24 -g Keep debug info.
25 -e chunk Use chunk string as input.
26 -- Stop handling options.
27 - Use stdin as input and/or stdout as output.
29 os.exit(1)
30 end
32 local function readfile(input)
33 if type(input) == "function" then return input end
34 if input == "-" then input = nil end
35 local f, err = loadfile(input)
36 if not f then
37 io.stderr:write("luajit: ", err, "\n")
38 os.exit(1)
39 end
40 return f
41 end
43 local function readstring(input)
44 local f, err = loadstring(input)
45 if not f then
46 io.stderr:write("luajit: ", err, "\n")
47 os.exit(1)
48 end
49 return f
50 end
52 local function savefile(name, mode)
53 if name == "-" then return io.stdout end
54 local fp, err = io.open(name, mode)
55 if not fp then
56 io.stderr:write("luajit: cannot write ", err, "\n")
57 os.exit(1)
58 end
59 return fp
60 end
62 ------------------------------------------------------------------------------
64 local function bclist(input, output)
65 local f = readfile(input)
66 require("jit.bc").dump(f, savefile(output, "w"), true)
67 end
69 local function bcsave(input, output, strip)
70 local f = readfile(input)
71 local s = string.dump(f, strip)
72 local fp = savefile(output, "wb")
73 local ok, err = fp:write(s)
74 if ok and output ~= "-" then ok, err = fp:close() end
75 if not ok then
76 io.stderr:write("luajit: cannot write ", arg[2], ": ", err, "\n")
77 os.exit(1)
78 end
79 end
81 local function docmd(...)
82 local arg = {...}
83 local n = 1
84 local list = false
85 local strip = true
86 while n <= #arg do
87 local a = arg[n]
88 if type(a) == "string" and string.sub(a, 1, 1) == "-" and a ~= "-" then
89 if a == "--" then table.remove(arg, n); break end
90 for m=2,#a do
91 local opt = string.sub(a, m, m)
92 if opt == "l" then
93 list = true
94 elseif opt == "s" then
95 strip = true
96 elseif opt == "g" then
97 strip = false
98 elseif opt == "e" then
99 if n ~= 1 or #arg < 2 or m ~= #a then usage() end
100 arg[2] = readstring(arg[2])
101 else
102 usage()
105 table.remove(arg, n)
106 else
107 n = n + 1
110 if list then
111 if #arg == 0 or #arg > 2 then usage() end
112 bclist(arg[1], arg[2] or "-")
113 else
114 if #arg ~= 2 then usage() end
115 bcsave(arg[1], arg[2], strip)
119 ------------------------------------------------------------------------------
121 -- Public module functions.
122 module(...)
124 start = docmd -- Process -b command line option.