-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_clang.lua
77 lines (54 loc) · 1.37 KB
/
test_clang.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
-- quick'n'dirty way to get current path:
function cmd(str)
local f = assert(io.popen(str, "r"))
local res = f:read()
f:close()
return res and res:gsub("%c", "") -- remove control characters
end
local separator = package.config:sub( 1, 1 )
local pwd = assert(cmd"pwd") .. separator
local script = pwd .. (arg and arg[0] or "")
local path = {separator}
for w in string.gmatch(script, string.format("[^%s]+%s", separator, separator)) do
table.insert(path, w)
end
path = table.concat(path)
print(path)
require "clang"
for k, v in pairs(clang) do
print(string.format("clang.%s = %s", k, tostring(v)))
end
local cc = clang.Compiler()
print()
--local app = (require"app")
cc:include(path .. "headers")
if not clang.hosttriple:find("apple") then
-- problematic on OSX
cc:include("/usr/include")
cc:include("/usr/include/i386-linux-gnu")
cc:include("/usr/include/x86_64-linux-gnu")
end
cc:define("SCALAR=7")
assert(cc:compile([=[
extern "C" {
#include "lua.h"
#include "lauxlib.h"
}
extern "C" int test(lua_State * L) {
double x = luaL_optnumber(L, 1, 0);
lua_pushnumber(L, x*5);
return 1;
}
]=]))
--cc:dump()
cc:optimize()
cc:dump()
print(cc:datalayout())
print(cc:targettriple())
print(clang.lua_pointer())
local jit = cc:jit()
print(jit:getfunctionptr("test"))
local f = assert(jit:pushcfunction("test"), "failed to find function")
for i = 1, 10 do
print(i, f(i))
end