-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.t
64 lines (58 loc) · 1.75 KB
/
compile.t
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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local types = {}
types[bool] = "bool"
types[rawstring] = "char *"
types[float] = "float"
types[double] = "double"
types[tuple()] = "void"
types[opaque] = "void"
types[&opaque] = "void *"
for _, pre in pairs{"", "u"} do
for i = 0, 3 do
local sz = 8 * 2 ^ i
local locint = pre .. "int" .. sz
local typ = _G[locint]
types[typ] = locint .. "_t"
end
end
local function toC(T)
if T:ispointer() and not types[T] then
local ref = 0
while T:ispointer() do
T = T.type
ref = ref + 1
end
local TC = types[T]
return TC .. " " .. string.rep("*", ref)
else
return types[T] or error("Unknown type " .. tostring(T))
end
end
local function getCheader(methods)
local headerfile = terralib.newlist()
headerfile:insert("// Autogenerated file. Do not edit!")
headerfile:insert("#pragma once")
headerfile:insert("#include <stdint.h>")
headerfile:insert("#include <stdbool.h>")
for name, method in pairs(methods) do
local param = method.type.parameters:map(toC)
local returntype = toC(method.type.returntype)
local func = ("%s %s(%s);"):format(returntype, name, param:concat(", "))
headerfile:insert(func)
end
return headerfile:concat("\n")
end
local function generateCAPI(name, methods)
local prototypes = getCheader(methods)
local header = io.open(name .. ".h", "w")
header:write(prototypes)
header:close()
terralib.saveobj(name .. ".o", methods)
end
return {
getCheader = getCheader,
generateCAPI = generateCAPI,
}