First effort to revive BasicBlock and Function classes and
[jitcs.git] / tools / x86_insalias2data.lua
blob211449fd71ca3a4bfa4c92be98ca8da08a8f1f8e
1 local _setfenv, _ipairs, _loadstr = setfenv, ipairs, loadstring
2 local _open = io.open
3 local _match, _gmatch, _fmt = string.match, string.gmatch, string.format
4 local _gsub, _sub = string.gsub, string.sub
5 local _exec = os.execute
7 local function readfile(n)
8 local f = _open(n) or _open("tools/"..n)
9 local ctnt = f:read("*a")
10 f:close()
11 return ctnt
12 end
14 local testname, outname = ...
15 if (testname == nil or testname == "") then
16 testname = "..\\src-lib\\x86\\src\\_x86_insalias.ltxt"
17 end
18 if outname == nil or outname == "" then
19 outname = _match(testname, "^(.-)%.[^.\\/]*$") .. ".dat"
20 end
22 local emblua = _loadstr(readfile("emblua.lua"))
23 local aliaslist = emblua(testname,{type="string"})
25 local function rep_r(c)
26 if c == "R" then return "R" end
27 if c == "M" then return "M" end
28 if c == "m" then return "M" end
29 end
30 local function rep_w(c)
31 if c == "R" then return "W" end
32 if c == "M" then return "M32" end
33 if c == "m" then return "M" end
34 end
35 local function rep_d(c)
36 if c == "R" then return "D" end
37 if c == "M" then return "M64" end
38 if c == "m" then return "M" end
39 end
40 local aliases, aliases32, aliases64 = {}, {}, {}
41 for l in _gmatch(aliaslist, "([^\r\n]*)[\r\n]") do
42 if _match(l, "^%s*//") then
44 else
45 if _match(l, "%$([RMm])") then
46 l = _gsub(l, "%$([RMm])", rep_r)
47 .. " -> " .. _gsub(l, "%$([RMm])", rep_w)
48 .. ", " .. _gsub(l, "%$([RMm])", rep_d)
49 end
50 local l,r32,r64 = l:match("^%s*(%S+)%s*%->%s*(%S+)%s*,%s*(%S+)%s*$")
51 if l and r32 and r64 then
52 if r32 == r64 and r32 ~= "INVALID" then
53 aliases[#aliases+1] = {l,r32};
54 else
55 if r32 ~= "INVALID" then aliases32[#aliases32+1] = {l,r32}; end
56 if r64 ~= "INVALID" then aliases64[#aliases64+1] = {l,r64}; end
57 end
58 end
59 end
60 end
61 local f = _open(outname, "w")
62 f:write("return {\n")
63 f:write("aliases = {\n")
64 for k,v in _ipairs(aliases) do
65 f:write(_fmt(" {%q, %q},\n", v[1], v[2]))
66 end
67 f:write("}, aliases32 = {\n")
68 for k,v in _ipairs(aliases32) do
69 f:write(_fmt(" {%q, %q},\n", v[1], v[2]))
70 end
71 f:write("}, aliases64 = {\n")
72 for k,v in _ipairs(aliases64) do
73 f:write(_fmt(" {%q, %q},\n", v[1], v[2]))
74 end
75 f:write("}}\n")
76 f:close()