Adding copyright notices to most files. Also add readme file, and some
[jitcs.git] / tools / x86_insalias2data.lua
blob83ca0866045283d990d4dc0f5cddab0dc8402888
1 --[[
2 Instruction Alias To Data tool for X86:
3 Copyright (C) 2013-2014 Dirk Steinke.
4 See copyright and license notice in COPYRIGHT or include/jitcs.h
6 The tool reads src/data/x86_insalias.ltxt and transforms it into
7 src/data/x86_insalias.dat.
8 ]]
10 local _setfenv, _ipairs, _loadstr = setfenv, ipairs, loadstring
11 local _open = io.open
12 local _match, _gmatch, _fmt = string.match, string.gmatch, string.format
13 local _gsub, _sub = string.gsub, string.sub
14 local _exec = os.execute
16 local function readfile(n)
17 local f = _open(n) or _open("tools/"..n)
18 local ctnt = f:read("*a")
19 f:close()
20 return ctnt
21 end
23 local testname, outname = ...
24 if (testname == nil or testname == "") then
25 testname = "..\\src-lib\\x86\\src\\_x86_insalias.ltxt"
26 end
27 if outname == nil or outname == "" then
28 outname = _match(testname, "^(.-)%.[^.\\/]*$") .. ".dat"
29 end
31 local emblua = _loadstr(readfile("emblua.lua"))
32 local aliaslist = emblua(testname,{type="string"})
34 local function rep_r(c)
35 if c == "R" then return "R" end
36 if c == "M" then return "M" end
37 if c == "m" then return "M" end
38 end
39 local function rep_w(c)
40 if c == "R" then return "W" end
41 if c == "M" then return "M32" end
42 if c == "m" then return "M" end
43 end
44 local function rep_d(c)
45 if c == "R" then return "D" end
46 if c == "M" then return "M64" end
47 if c == "m" then return "M" end
48 end
49 local aliases, aliases32, aliases64 = {}, {}, {}
50 for l in _gmatch(aliaslist, "([^\r\n]*)[\r\n]") do
51 if _match(l, "^%s*//") then
53 else
54 if _match(l, "%$([RMm])") then
55 l = _gsub(l, "%$([RMm])", rep_r)
56 .. " -> " .. _gsub(l, "%$([RMm])", rep_w)
57 .. ", " .. _gsub(l, "%$([RMm])", rep_d)
58 end
59 local l,r32,r64 = l:match("^%s*(%S+)%s*%->%s*(%S+)%s*,%s*(%S+)%s*$")
60 if l and r32 and r64 then
61 if r32 == r64 and r32 ~= "INVALID" then
62 aliases[#aliases+1] = {l,r32};
63 else
64 if r32 ~= "INVALID" then aliases32[#aliases32+1] = {l,r32}; end
65 if r64 ~= "INVALID" then aliases64[#aliases64+1] = {l,r64}; end
66 end
67 end
68 end
69 end
70 local f = _open(outname, "w")
71 f:write("return {\n")
72 f:write("aliases = {\n")
73 for k,v in _ipairs(aliases) do
74 f:write(_fmt(" {%q, %q},\n", v[1], v[2]))
75 end
76 f:write("}, aliases32 = {\n")
77 for k,v in _ipairs(aliases32) do
78 f:write(_fmt(" {%q, %q},\n", v[1], v[2]))
79 end
80 f:write("}, aliases64 = {\n")
81 for k,v in _ipairs(aliases64) do
82 f:write(_fmt(" {%q, %q},\n", v[1], v[2]))
83 end
84 f:write("}}\n")
85 f:close()