-
Notifications
You must be signed in to change notification settings - Fork 7
/
ffi-cdecl.lua
99 lines (90 loc) · 2.97 KB
/
ffi-cdecl.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local script_dir = arg["script"]:gsub("[^/]+$","")
package.path = script_dir .. "gcc-lua-cdecl/?.lua;" .. package.path
local gcc = require("gcc")
local cdecl = require("gcc.cdecl")
local fficdecl = require("ffi-cdecl.ffi-cdecl")
-- Output generated assembly to /dev/null
gcc.set_asm_file_name(gcc.HOST_BIT_BUCKET)
-- Captured C declarations.
local decls = {}
-- Type declaration identifiers.
local types = {}
-- Parse C declaration from capture macro.
gcc.register_callback(gcc.PLUGIN_PRE_GENERICIZE, function(node)
local decl, id, ref = fficdecl.parse(node)
if decl then
if decl:class() == "type" or decl:code() == "type_decl" then
types[decl] = id
end
table.insert(decls, {decl = decl, id = id, ref = ref})
end
end)
-- Formats the given declaration as a string of C code.
local function format(decl, id, ref)
if decl:class() == "constant" then
return "static const int " .. id .. " = " .. decl:value()
end
if decl:class() == "declaration" and ref then
-- If we have an original typedef ref, use it instead of letting GCC resolve it to a canonical type (cdecl_c99_type hack)...
-- That's always a typedef, so, just format it like the original and call it a day.
-- The callback has already run, so the actual new name made it to the types map, meaning we'll use it in function calls & co.
return "typedef " .. ref .. " " .. id
end
return cdecl.declare(decl, function(node)
if node == decl then return id end
return types[node]
end)
end
-- Output captured C declarations to Lua file.
gcc.register_callback(gcc.PLUGIN_FINISH_UNIT, function()
local result = {}
for i, decl in ipairs(decls) do
-- Skip the C99 decls
-- NOTE: Do double-check those, because while this appears to help with size_t,
-- I've seen complex nested typedefs involving *ptr_t getting mangled instead...
-- NOTE: To check for suspicious type conversions, with an x86_64 compiler,
-- do a second run w/ -m32 in CPPFLAGS.
if decl.id == "bool"
or decl.id == "ptrdiff_t"
or decl.id == "size_t"
or decl.id == "wchar_t"
or decl.id == "int8_t"
or decl.id == "int16_t"
or decl.id == "int32_t"
or decl.id == "int64_t"
or decl.id == "uint8_t"
or decl.id == "uint16_t"
or decl.id == "uint32_t"
or decl.id == "uint64_t"
or decl.id == "intptr_t"
or decl.id == "uintptr_t"
or decl.id == "ssize_t"
then
goto continue
end
table.insert(result, format(decl.decl, decl.id, decl.ref) .. ";\n")
-- That's one janky-ass workaround to the lack of continue keyword (requires LuaJIT/Lua 5.2)...
::continue::
end
local f = assert(io.open(arg.output, "w"))
if arg.output:match(".*%.(.*)") == "py" then
f:write([=[
"""
File autogenerated by https://github.com/koreader/ffi-cdecl
"""
from cffi import FFI
ffi = FFI()
ffi.cdef("""
]=], table.concat(result), [=[
""")
]=])
else
f:write([=[
local ffi = require("ffi")
ffi.cdef[[
]=], table.concat(result), [=[
]]
]=])
end
f:close()
end)