dasm_x86.lua 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. ------------------------------------------------------------------------------
  2. -- DynASM x86/x64 module.
  3. --
  4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.
  5. -- See dynasm.lua for full copyright notice.
  6. ------------------------------------------------------------------------------
  7. local x64 = x64
  8. -- Module information:
  9. local _info = {
  10. arch = x64 and "x64" or "x86",
  11. description = "DynASM x86/x64 module",
  12. version = "1.4.0",
  13. vernum = 10400,
  14. release = "2015-10-18",
  15. author = "Mike Pall",
  16. license = "MIT",
  17. }
  18. -- Exported glue functions for the arch-specific module.
  19. local _M = { _info = _info }
  20. -- Cache library functions.
  21. local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs
  22. local assert, unpack, setmetatable = assert, unpack or table.unpack, setmetatable
  23. local _s = string
  24. local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
  25. local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub
  26. local concat, sort, remove = table.concat, table.sort, table.remove
  27. local bit = bit or require("bit")
  28. local band, bxor, shl, shr = bit.band, bit.bxor, bit.lshift, bit.rshift
  29. -- Inherited tables and callbacks.
  30. local g_opt, g_arch
  31. local wline, werror, wfatal, wwarn
  32. -- Action name list.
  33. -- CHECK: Keep this in sync with the C code!
  34. local action_names = {
  35. -- int arg, 1 buffer pos:
  36. "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB",
  37. -- action arg (1 byte), int arg, 1 buffer pos (reg/num):
  38. "VREG", "SPACE", -- !x64: VREG support NYI.
  39. -- ptrdiff_t arg, 1 buffer pos (address): !x64
  40. "SETLABEL", "REL_A",
  41. -- action arg (1 byte) or int arg, 2 buffer pos (link, offset):
  42. "REL_LG", "REL_PC",
  43. -- action arg (1 byte) or int arg, 1 buffer pos (link):
  44. "IMM_LG", "IMM_PC",
  45. -- action arg (1 byte) or int arg, 1 buffer pos (offset):
  46. "LABEL_LG", "LABEL_PC",
  47. -- action arg (1 byte), 1 buffer pos (offset):
  48. "ALIGN",
  49. -- action args (2 bytes), no buffer pos.
  50. "EXTERN",
  51. -- action arg (1 byte), no buffer pos.
  52. "ESC",
  53. -- no action arg, no buffer pos.
  54. "MARK",
  55. -- action arg (1 byte), no buffer pos, terminal action:
  56. "SECTION",
  57. -- no args, no buffer pos, terminal action:
  58. "STOP"
  59. }
  60. -- Maximum number of section buffer positions for dasm_put().
  61. -- CHECK: Keep this in sync with the C code!
  62. local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines.
  63. -- Action name -> action number (dynamically generated below).
  64. local map_action = {}
  65. -- First action number. Everything below does not need to be escaped.
  66. local actfirst = 256-#action_names
  67. -- Action list buffer and string (only used to remove dupes).
  68. local actlist = {}
  69. local actstr = ""
  70. -- Argument list for next dasm_put(). Start with offset 0 into action list.
  71. local actargs = { 0 }
  72. -- Current number of section buffer positions for dasm_put().
  73. local secpos = 1
  74. ------------------------------------------------------------------------------
  75. -- Compute action numbers for action names.
  76. for n,name in ipairs(action_names) do
  77. local num = actfirst + n - 1
  78. map_action[name] = num
  79. end
  80. -- Dump action names and numbers.
  81. local function dumpactions(out)
  82. out:write("DynASM encoding engine action codes:\n")
  83. for n,name in ipairs(action_names) do
  84. local num = map_action[name]
  85. out:write(format(" %-10s %02X %d\n", name, num, num))
  86. end
  87. out:write("\n")
  88. end
  89. -- Write action list buffer as a huge static C array.
  90. local function writeactions(out, name)
  91. local nn = #actlist
  92. local last = actlist[nn] or 255
  93. actlist[nn] = nil -- Remove last byte.
  94. if nn == 0 then nn = 1 end
  95. out:write("static const unsigned char ", name, "[", nn, "] = {\n")
  96. local s = " "
  97. for n,b in ipairs(actlist) do
  98. s = s..b..","
  99. if #s >= 75 then
  100. assert(out:write(s, "\n"))
  101. s = " "
  102. end
  103. end
  104. out:write(s, last, "\n};\n\n") -- Add last byte back.
  105. end
  106. ------------------------------------------------------------------------------
  107. -- Add byte to action list.
  108. local function wputxb(n)
  109. assert(n >= 0 and n <= 255 and n % 1 == 0, "byte out of range")
  110. actlist[#actlist+1] = n
  111. end
  112. -- Add action to list with optional arg. Advance buffer pos, too.
  113. local function waction(action, a, num)
  114. wputxb(assert(map_action[action], "bad action name `"..action.."'"))
  115. if a then actargs[#actargs+1] = a end
  116. if a or num then secpos = secpos + (num or 1) end
  117. end
  118. -- Add call to embedded DynASM C code.
  119. local function wcall(func, args)
  120. wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true)
  121. end
  122. -- Delete duplicate action list chunks. A tad slow, but so what.
  123. local function dedupechunk(offset)
  124. local al, as = actlist, actstr
  125. local chunk = char(unpack(al, offset+1, #al))
  126. local orig = find(as, chunk, 1, true)
  127. if orig then
  128. actargs[1] = orig-1 -- Replace with original offset.
  129. for i=offset+1,#al do al[i] = nil end -- Kill dupe.
  130. else
  131. actstr = as..chunk
  132. end
  133. end
  134. -- Flush action list (intervening C code or buffer pos overflow).
  135. local function wflush(term)
  136. local offset = actargs[1]
  137. if #actlist == offset then return end -- Nothing to flush.
  138. if not term then waction("STOP") end -- Terminate action list.
  139. dedupechunk(offset)
  140. wcall("put", actargs) -- Add call to dasm_put().
  141. actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put().
  142. secpos = 1 -- The actionlist offset occupies a buffer position, too.
  143. end
  144. -- Put escaped byte.
  145. local function wputb(n)
  146. if n >= actfirst then waction("ESC") end -- Need to escape byte.
  147. wputxb(n)
  148. end
  149. ------------------------------------------------------------------------------
  150. -- Global label name -> global label number. With auto assignment on 1st use.
  151. local next_global = 10
  152. local map_global = setmetatable({}, { __index = function(t, name)
  153. if not match(name, "^[%a_][%w_@]*$") then werror("bad global label") end
  154. local n = next_global
  155. if n > 246 then werror("too many global labels") end
  156. next_global = n + 1
  157. t[name] = n
  158. return n
  159. end})
  160. -- Dump global labels.
  161. local function dumpglobals(out, lvl)
  162. local t = {}
  163. for name, n in pairs(map_global) do t[n] = name end
  164. out:write("Global labels:\n")
  165. for i=10,next_global-1 do
  166. out:write(format(" %s\n", t[i]))
  167. end
  168. out:write("\n")
  169. end
  170. -- Write global label enum.
  171. local function writeglobals(out, prefix)
  172. local t = {}
  173. for name, n in pairs(map_global) do t[n] = name end
  174. out:write("enum {\n")
  175. for i=10,next_global-1 do
  176. out:write(" ", prefix, gsub(t[i], "@.*", ""), ",\n")
  177. end
  178. out:write(" ", prefix, "_MAX\n};\n")
  179. end
  180. -- Write global label names.
  181. local function writeglobalnames(out, name)
  182. local t = {}
  183. for name, n in pairs(map_global) do t[n] = name end
  184. out:write("static const char *const ", name, "[] = {\n")
  185. for i=10,next_global-1 do
  186. out:write(" \"", t[i], "\",\n")
  187. end
  188. out:write(" (const char *)0\n};\n")
  189. end
  190. ------------------------------------------------------------------------------
  191. -- Extern label name -> extern label number. With auto assignment on 1st use.
  192. local next_extern = -1
  193. local map_extern = setmetatable({}, { __index = function(t, name)
  194. -- No restrictions on the name for now.
  195. local n = next_extern
  196. if n < -256 then werror("too many extern labels") end
  197. next_extern = n - 1
  198. t[name] = n
  199. return n
  200. end})
  201. -- Dump extern labels.
  202. local function dumpexterns(out, lvl)
  203. local t = {}
  204. for name, n in pairs(map_extern) do t[-n] = name end
  205. out:write("Extern labels:\n")
  206. for i=1,-next_extern-1 do
  207. out:write(format(" %s\n", t[i]))
  208. end
  209. out:write("\n")
  210. end
  211. -- Write extern label names.
  212. local function writeexternnames(out, name)
  213. local t = {}
  214. for name, n in pairs(map_extern) do t[-n] = name end
  215. out:write("static const char *const ", name, "[] = {\n")
  216. for i=1,-next_extern-1 do
  217. out:write(" \"", t[i], "\",\n")
  218. end
  219. out:write(" (const char *)0\n};\n")
  220. end
  221. ------------------------------------------------------------------------------
  222. -- Arch-specific maps.
  223. local map_archdef = {} -- Ext. register name -> int. name.
  224. local map_reg_rev = {} -- Int. register name -> ext. name.
  225. local map_reg_num = {} -- Int. register name -> register number.
  226. local map_reg_opsize = {} -- Int. register name -> operand size.
  227. local map_reg_valid_base = {} -- Int. register name -> valid base register?
  228. local map_reg_valid_index = {} -- Int. register name -> valid index register?
  229. local map_reg_needrex = {} -- Int. register name -> need rex vs. no rex.
  230. local reg_list = {} -- Canonical list of int. register names.
  231. local map_type = {} -- Type name -> { ctype, reg }
  232. local ctypenum = 0 -- Type number (for _PTx macros).
  233. local addrsize = x64 and "q" or "d" -- Size for address operands.
  234. -- Helper functions to fill register maps.
  235. local function mkrmap(sz, cl, names)
  236. local cname = format("@%s", sz)
  237. reg_list[#reg_list+1] = cname
  238. map_archdef[cl] = cname
  239. map_reg_rev[cname] = cl
  240. map_reg_num[cname] = -1
  241. map_reg_opsize[cname] = sz
  242. if sz == addrsize or sz == "d" then
  243. map_reg_valid_base[cname] = true
  244. map_reg_valid_index[cname] = true
  245. end
  246. if names then
  247. for n,name in ipairs(names) do
  248. local iname = format("@%s%x", sz, n-1)
  249. reg_list[#reg_list+1] = iname
  250. map_archdef[name] = iname
  251. map_reg_rev[iname] = name
  252. map_reg_num[iname] = n-1
  253. map_reg_opsize[iname] = sz
  254. if sz == "b" and n > 4 then map_reg_needrex[iname] = false end
  255. if sz == addrsize or sz == "d" then
  256. map_reg_valid_base[iname] = true
  257. map_reg_valid_index[iname] = true
  258. end
  259. end
  260. end
  261. for i=0,(x64 and sz ~= "f") and 15 or 7 do
  262. local needrex = sz == "b" and i > 3
  263. local iname = format("@%s%x%s", sz, i, needrex and "R" or "")
  264. if needrex then map_reg_needrex[iname] = true end
  265. local name
  266. if sz == "o" or sz == "y" then name = format("%s%d", cl, i)
  267. elseif sz == "f" then name = format("st%d", i)
  268. else name = format("r%d%s", i, sz == addrsize and "" or sz) end
  269. map_archdef[name] = iname
  270. if not map_reg_rev[iname] then
  271. reg_list[#reg_list+1] = iname
  272. map_reg_rev[iname] = name
  273. map_reg_num[iname] = i
  274. map_reg_opsize[iname] = sz
  275. if sz == addrsize or sz == "d" then
  276. map_reg_valid_base[iname] = true
  277. map_reg_valid_index[iname] = true
  278. end
  279. end
  280. end
  281. reg_list[#reg_list+1] = ""
  282. end
  283. -- Integer registers (qword, dword, word and byte sized).
  284. if x64 then
  285. mkrmap("q", "Rq", {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi"})
  286. end
  287. mkrmap("d", "Rd", {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"})
  288. mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"})
  289. mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"})
  290. map_reg_valid_index[map_archdef.esp] = false
  291. if x64 then map_reg_valid_index[map_archdef.rsp] = false end
  292. map_archdef["Ra"] = "@"..addrsize
  293. -- FP registers (internally tword sized, but use "f" as operand size).
  294. mkrmap("f", "Rf")
  295. -- SSE registers (oword sized, but qword and dword accessible).
  296. mkrmap("o", "xmm")
  297. -- AVX registers (yword sized, but oword, qword and dword accessible).
  298. mkrmap("y", "ymm")
  299. -- Operand size prefixes to codes.
  300. local map_opsize = {
  301. byte = "b", word = "w", dword = "d", qword = "q", oword = "o", yword = "y",
  302. tword = "t", aword = addrsize,
  303. }
  304. -- Operand size code to number.
  305. local map_opsizenum = {
  306. b = 1, w = 2, d = 4, q = 8, o = 16, y = 32, t = 10,
  307. }
  308. -- Operand size code to name.
  309. local map_opsizename = {
  310. b = "byte", w = "word", d = "dword", q = "qword", o = "oword", y = "yword",
  311. t = "tword", f = "fpword",
  312. }
  313. -- Valid index register scale factors.
  314. local map_xsc = {
  315. ["1"] = 0, ["2"] = 1, ["4"] = 2, ["8"] = 3,
  316. }
  317. -- Condition codes.
  318. local map_cc = {
  319. o = 0, no = 1, b = 2, nb = 3, e = 4, ne = 5, be = 6, nbe = 7,
  320. s = 8, ns = 9, p = 10, np = 11, l = 12, nl = 13, le = 14, nle = 15,
  321. c = 2, nae = 2, nc = 3, ae = 3, z = 4, nz = 5, na = 6, a = 7,
  322. pe = 10, po = 11, nge = 12, ge = 13, ng = 14, g = 15,
  323. }
  324. -- Reverse defines for registers.
  325. function _M.revdef(s)
  326. return gsub(s, "@%w+", map_reg_rev)
  327. end
  328. -- Dump register names and numbers
  329. local function dumpregs(out)
  330. out:write("Register names, sizes and internal numbers:\n")
  331. for _,reg in ipairs(reg_list) do
  332. if reg == "" then
  333. out:write("\n")
  334. else
  335. local name = map_reg_rev[reg]
  336. local num = map_reg_num[reg]
  337. local opsize = map_opsizename[map_reg_opsize[reg]]
  338. out:write(format(" %-5s %-8s %s\n", name, opsize,
  339. num < 0 and "(variable)" or num))
  340. end
  341. end
  342. end
  343. ------------------------------------------------------------------------------
  344. -- Put action for label arg (IMM_LG, IMM_PC, REL_LG, REL_PC).
  345. local function wputlabel(aprefix, imm, num)
  346. if type(imm) == "number" then
  347. if imm < 0 then
  348. waction("EXTERN")
  349. wputxb(aprefix == "IMM_" and 0 or 1)
  350. imm = -imm-1
  351. else
  352. waction(aprefix.."LG", nil, num);
  353. end
  354. wputxb(imm)
  355. else
  356. waction(aprefix.."PC", imm, num)
  357. end
  358. end
  359. -- Put signed byte or arg.
  360. local function wputsbarg(n)
  361. if type(n) == "number" then
  362. if n < -128 or n > 127 then
  363. werror("signed immediate byte out of range")
  364. end
  365. if n < 0 then n = n + 256 end
  366. wputb(n)
  367. else waction("IMM_S", n) end
  368. end
  369. -- Put unsigned byte or arg.
  370. local function wputbarg(n)
  371. if type(n) == "number" then
  372. if n < 0 or n > 255 then
  373. werror("unsigned immediate byte out of range")
  374. end
  375. wputb(n)
  376. else waction("IMM_B", n) end
  377. end
  378. -- Put unsigned word or arg.
  379. local function wputwarg(n)
  380. if type(n) == "number" then
  381. if shr(n, 16) ~= 0 then
  382. werror("unsigned immediate word out of range")
  383. end
  384. wputb(band(n, 255)); wputb(shr(n, 8));
  385. else waction("IMM_W", n) end
  386. end
  387. -- Put signed or unsigned dword or arg.
  388. local function wputdarg(n)
  389. local tn = type(n)
  390. if tn == "number" then
  391. wputb(band(n, 255))
  392. wputb(band(shr(n, 8), 255))
  393. wputb(band(shr(n, 16), 255))
  394. wputb(shr(n, 24))
  395. elseif tn == "table" then
  396. wputlabel("IMM_", n[1], 1)
  397. else
  398. waction("IMM_D", n)
  399. end
  400. end
  401. -- Put operand-size dependent number or arg (defaults to dword).
  402. local function wputszarg(sz, n)
  403. if not sz or sz == "d" or sz == "q" then wputdarg(n)
  404. elseif sz == "w" then wputwarg(n)
  405. elseif sz == "b" then wputbarg(n)
  406. elseif sz == "s" then wputsbarg(n)
  407. else werror("bad operand size") end
  408. end
  409. -- Put multi-byte opcode with operand-size dependent modifications.
  410. local function wputop(sz, op, rex, vex)
  411. if vex then
  412. local tail
  413. if vex.m == 1 and band(rex, 11) == 0 then
  414. wputb(0xc5)
  415. tail = shl(bxor(band(rex, 4), 4), 5)
  416. else
  417. wputb(0xc4)
  418. wputb(shl(bxor(band(rex, 7), 7), 5) + vex.m)
  419. tail = shl(band(rex, 8), 4)
  420. end
  421. local reg, vreg = 0, nil
  422. if vex.v then
  423. reg = vex.v.reg
  424. if not reg then werror("bad vex operand") end
  425. if reg < 0 then reg = 0; vreg = vex.v.vreg end
  426. end
  427. if sz == "y" or vex.l then tail = tail + 4 end
  428. wputb(tail + shl(bxor(reg, 15), 3) + vex.p)
  429. if vreg then waction("VREG", vreg); wputxb(4) end
  430. rex = 0
  431. if op >= 256 then werror("bad vex opcode") end
  432. end
  433. local r
  434. if rex ~= 0 and not x64 then werror("bad operand size") end
  435. if sz == "w" then wputb(102) end
  436. -- Needs >32 bit numbers, but only for crc32 eax, word [ebx]
  437. if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end
  438. if op >= 16777216 then wputb(shr(op, 24)); op = band(op, 0xffffff) end
  439. if op >= 65536 then
  440. if rex ~= 0 then
  441. local opc3 = band(op, 0xffff00)
  442. if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then
  443. wputb(64 + band(rex, 15)); rex = 0
  444. end
  445. end
  446. wputb(shr(op, 16)); op = band(op, 0xffff)
  447. end
  448. if op >= 256 then
  449. local b = shr(op, 8)
  450. if b == 15 and rex ~= 0 then wputb(64 + band(rex, 15)); rex = 0 end
  451. wputb(b)
  452. op = band(op, 255)
  453. end
  454. if rex ~= 0 then wputb(64 + band(rex, 15)) end
  455. if sz == "b" then op = op - 1 end
  456. wputb(op)
  457. end
  458. -- Put ModRM or SIB formatted byte.
  459. local function wputmodrm(m, s, rm, vs, vrm)
  460. assert(m < 4 and s < 16 and rm < 16, "bad modrm operands")
  461. wputb(shl(m, 6) + shl(band(s, 7), 3) + band(rm, 7))
  462. end
  463. -- Put ModRM/SIB plus optional displacement.
  464. local function wputmrmsib(t, imark, s, vsreg)
  465. local vreg, vxreg
  466. local reg, xreg = t.reg, t.xreg
  467. if reg and reg < 0 then reg = 0; vreg = t.vreg end
  468. if xreg and xreg < 0 then xreg = 0; vxreg = t.vxreg end
  469. if s < 0 then s = 0 end
  470. -- Register mode.
  471. if sub(t.mode, 1, 1) == "r" then
  472. wputmodrm(3, s, reg)
  473. if vsreg then waction("VREG", vsreg); wputxb(2) end
  474. if vreg then waction("VREG", vreg); wputxb(0) end
  475. return
  476. end
  477. local disp = t.disp
  478. local tdisp = type(disp)
  479. -- No base register?
  480. if not reg then
  481. local riprel = false
  482. if xreg then
  483. -- Indexed mode with index register only.
  484. -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp)
  485. wputmodrm(0, s, 4)
  486. if imark == "I" then waction("MARK") end
  487. if vsreg then waction("VREG", vsreg); wputxb(2) end
  488. wputmodrm(t.xsc, xreg, 5)
  489. if vxreg then waction("VREG", vxreg); wputxb(3) end
  490. else
  491. -- Pure 32 bit displacement.
  492. if x64 and tdisp ~= "table" then
  493. wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp)
  494. if imark == "I" then waction("MARK") end
  495. wputmodrm(0, 4, 5)
  496. else
  497. riprel = x64
  498. wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp)
  499. if imark == "I" then waction("MARK") end
  500. end
  501. if vsreg then waction("VREG", vsreg); wputxb(2) end
  502. end
  503. if riprel then -- Emit rip-relative displacement.
  504. if match("UWSiI", imark) then
  505. werror("NYI: rip-relative displacement followed by immediate")
  506. end
  507. -- The previous byte in the action buffer cannot be 0xe9 or 0x80-0x8f.
  508. wputlabel("REL_", disp[1], 2)
  509. else
  510. wputdarg(disp)
  511. end
  512. return
  513. end
  514. local m
  515. if tdisp == "number" then -- Check displacement size at assembly time.
  516. if disp == 0 and band(reg, 7) ~= 5 then -- [ebp] -> [ebp+0] (in SIB, too)
  517. if not vreg then m = 0 end -- Force DISP to allow [Rd(5)] -> [ebp+0]
  518. elseif disp >= -128 and disp <= 127 then m = 1
  519. else m = 2 end
  520. elseif tdisp == "table" then
  521. m = 2
  522. end
  523. -- Index register present or esp as base register: need SIB encoding.
  524. if xreg or band(reg, 7) == 4 then
  525. wputmodrm(m or 2, s, 4) -- ModRM.
  526. if m == nil or imark == "I" then waction("MARK") end
  527. if vsreg then waction("VREG", vsreg); wputxb(2) end
  528. wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB.
  529. if vxreg then waction("VREG", vxreg); wputxb(3) end
  530. if vreg then waction("VREG", vreg); wputxb(1) end
  531. else
  532. wputmodrm(m or 2, s, reg) -- ModRM.
  533. if (imark == "I" and (m == 1 or m == 2)) or
  534. (m == nil and (vsreg or vreg)) then waction("MARK") end
  535. if vsreg then waction("VREG", vsreg); wputxb(2) end
  536. if vreg then waction("VREG", vreg); wputxb(1) end
  537. end
  538. -- Put displacement.
  539. if m == 1 then wputsbarg(disp)
  540. elseif m == 2 then wputdarg(disp)
  541. elseif m == nil then waction("DISP", disp) end
  542. end
  543. ------------------------------------------------------------------------------
  544. -- Return human-readable operand mode string.
  545. local function opmodestr(op, args)
  546. local m = {}
  547. for i=1,#args do
  548. local a = args[i]
  549. m[#m+1] = sub(a.mode, 1, 1)..(a.opsize or "?")
  550. end
  551. return op.." "..concat(m, ",")
  552. end
  553. -- Convert number to valid integer or nil.
  554. local function toint(expr)
  555. local n = tonumber(expr)
  556. if n then
  557. if n % 1 ~= 0 or n < -2147483648 or n > 4294967295 then
  558. werror("bad integer number `"..expr.."'")
  559. end
  560. return n
  561. end
  562. end
  563. -- Parse immediate expression.
  564. local function immexpr(expr)
  565. -- &expr (pointer)
  566. if sub(expr, 1, 1) == "&" then
  567. return "iPJ", format("(ptrdiff_t)(%s)", sub(expr,2))
  568. end
  569. local prefix = sub(expr, 1, 2)
  570. -- =>expr (pc label reference)
  571. if prefix == "=>" then
  572. return "iJ", sub(expr, 3)
  573. end
  574. -- ->name (global label reference)
  575. if prefix == "->" then
  576. return "iJ", map_global[sub(expr, 3)]
  577. end
  578. -- [<>][1-9] (local label reference)
  579. local dir, lnum = match(expr, "^([<>])([1-9])$")
  580. if dir then -- Fwd: 247-255, Bkwd: 1-9.
  581. return "iJ", lnum + (dir == ">" and 246 or 0)
  582. end
  583. local extname = match(expr, "^extern%s+(%S+)$")
  584. if extname then
  585. return "iJ", map_extern[extname]
  586. end
  587. -- expr (interpreted as immediate)
  588. return "iI", expr
  589. end
  590. -- Parse displacement expression: +-num, +-expr, +-opsize*num
  591. local function dispexpr(expr)
  592. local disp = expr == "" and 0 or toint(expr)
  593. if disp then return disp end
  594. local c, dispt = match(expr, "^([+-])%s*(.+)$")
  595. if c == "+" then
  596. expr = dispt
  597. elseif not c then
  598. werror("bad displacement expression `"..expr.."'")
  599. end
  600. local opsize, tailops = match(dispt, "^(%w+)%s*%*%s*(.+)$")
  601. local ops, imm = map_opsize[opsize], toint(tailops)
  602. if ops and imm then
  603. if c == "-" then imm = -imm end
  604. return imm*map_opsizenum[ops]
  605. end
  606. local mode, iexpr = immexpr(dispt)
  607. if mode == "iJ" then
  608. if c == "-" then werror("cannot invert label reference") end
  609. return { iexpr }
  610. end
  611. return expr -- Need to return original signed expression.
  612. end
  613. -- Parse register or type expression.
  614. local function rtexpr(expr)
  615. if not expr then return end
  616. local tname, ovreg = match(expr, "^([%w_]+):(@[%w_]+)$")
  617. local tp = map_type[tname or expr]
  618. if tp then
  619. local reg = ovreg or tp.reg
  620. local rnum = map_reg_num[reg]
  621. if not rnum then
  622. werror("type `"..(tname or expr).."' needs a register override")
  623. end
  624. if not map_reg_valid_base[reg] then
  625. werror("bad base register override `"..(map_reg_rev[reg] or reg).."'")
  626. end
  627. return reg, rnum, tp
  628. end
  629. return expr, map_reg_num[expr]
  630. end
  631. -- Parse operand and return { mode, opsize, reg, xreg, xsc, disp, imm }.
  632. local function parseoperand(param)
  633. local t = {}
  634. local expr = param
  635. local opsize, tailops = match(param, "^(%w+)%s*(.+)$")
  636. if opsize then
  637. t.opsize = map_opsize[opsize]
  638. if t.opsize then expr = tailops end
  639. end
  640. local br = match(expr, "^%[%s*(.-)%s*%]$")
  641. repeat
  642. if br then
  643. t.mode = "xm"
  644. -- [disp]
  645. t.disp = toint(br)
  646. if t.disp then
  647. t.mode = x64 and "xm" or "xmO"
  648. break
  649. end
  650. -- [reg...]
  651. local tp
  652. local reg, tailr = match(br, "^([@%w_:]+)%s*(.*)$")
  653. reg, t.reg, tp = rtexpr(reg)
  654. if not t.reg then
  655. -- [expr]
  656. t.mode = x64 and "xm" or "xmO"
  657. t.disp = dispexpr("+"..br)
  658. break
  659. end
  660. if t.reg == -1 then
  661. t.vreg, tailr = match(tailr, "^(%b())(.*)$")
  662. if not t.vreg then werror("bad variable register expression") end
  663. end
  664. -- [xreg*xsc] or [xreg*xsc+-disp] or [xreg*xsc+-expr]
  665. local xsc, tailsc = match(tailr, "^%*%s*([1248])%s*(.*)$")
  666. if xsc then
  667. if not map_reg_valid_index[reg] then
  668. werror("bad index register `"..map_reg_rev[reg].."'")
  669. end
  670. t.xsc = map_xsc[xsc]
  671. t.xreg = t.reg
  672. t.vxreg = t.vreg
  673. t.reg = nil
  674. t.vreg = nil
  675. t.disp = dispexpr(tailsc)
  676. break
  677. end
  678. if not map_reg_valid_base[reg] then
  679. werror("bad base register `"..map_reg_rev[reg].."'")
  680. end
  681. -- [reg] or [reg+-disp]
  682. t.disp = toint(tailr) or (tailr == "" and 0)
  683. if t.disp then break end
  684. -- [reg+xreg...]
  685. local xreg, tailx = match(tailr, "^+%s*([@%w_:]+)%s*(.*)$")
  686. xreg, t.xreg, tp = rtexpr(xreg)
  687. if not t.xreg then
  688. -- [reg+-expr]
  689. t.disp = dispexpr(tailr)
  690. break
  691. end
  692. if not map_reg_valid_index[xreg] then
  693. werror("bad index register `"..map_reg_rev[xreg].."'")
  694. end
  695. if t.xreg == -1 then
  696. t.vxreg, tailx = match(tailx, "^(%b())(.*)$")
  697. if not t.vxreg then werror("bad variable register expression") end
  698. end
  699. -- [reg+xreg*xsc...]
  700. local xsc, tailsc = match(tailx, "^%*%s*([1248])%s*(.*)$")
  701. if xsc then
  702. t.xsc = map_xsc[xsc]
  703. tailx = tailsc
  704. end
  705. -- [...] or [...+-disp] or [...+-expr]
  706. t.disp = dispexpr(tailx)
  707. else
  708. -- imm or opsize*imm
  709. local imm = toint(expr)
  710. if not imm and sub(expr, 1, 1) == "*" and t.opsize then
  711. imm = toint(sub(expr, 2))
  712. if imm then
  713. imm = imm * map_opsizenum[t.opsize]
  714. t.opsize = nil
  715. end
  716. end
  717. if imm then
  718. if t.opsize then werror("bad operand size override") end
  719. local m = "i"
  720. if imm == 1 then m = m.."1" end
  721. if imm >= 4294967168 and imm <= 4294967295 then imm = imm-4294967296 end
  722. if imm >= -128 and imm <= 127 then m = m.."S" end
  723. t.imm = imm
  724. t.mode = m
  725. break
  726. end
  727. local tp
  728. local reg, tailr = match(expr, "^([@%w_:]+)%s*(.*)$")
  729. reg, t.reg, tp = rtexpr(reg)
  730. if t.reg then
  731. if t.reg == -1 then
  732. t.vreg, tailr = match(tailr, "^(%b())(.*)$")
  733. if not t.vreg then werror("bad variable register expression") end
  734. end
  735. -- reg
  736. if tailr == "" then
  737. if t.opsize then werror("bad operand size override") end
  738. t.opsize = map_reg_opsize[reg]
  739. if t.opsize == "f" then
  740. t.mode = t.reg == 0 and "fF" or "f"
  741. else
  742. if reg == "@w4" or (x64 and reg == "@d4") then
  743. wwarn("bad idea, try again with `"..(x64 and "rsp'" or "esp'"))
  744. end
  745. t.mode = t.reg == 0 and "rmR" or (reg == "@b1" and "rmC" or "rm")
  746. end
  747. t.needrex = map_reg_needrex[reg]
  748. break
  749. end
  750. -- type[idx], type[idx].field, type->field -> [reg+offset_expr]
  751. if not tp then werror("bad operand `"..param.."'") end
  752. t.mode = "xm"
  753. t.disp = format(tp.ctypefmt, tailr)
  754. else
  755. t.mode, t.imm = immexpr(expr)
  756. if sub(t.mode, -1) == "J" then
  757. if t.opsize and t.opsize ~= addrsize then
  758. werror("bad operand size override")
  759. end
  760. t.opsize = addrsize
  761. end
  762. end
  763. end
  764. until true
  765. return t
  766. end
  767. ------------------------------------------------------------------------------
  768. -- x86 Template String Description
  769. -- ===============================
  770. --
  771. -- Each template string is a list of [match:]pattern pairs,
  772. -- separated by "|". The first match wins. No match means a
  773. -- bad or unsupported combination of operand modes or sizes.
  774. --
  775. -- The match part and the ":" is omitted if the operation has
  776. -- no operands. Otherwise the first N characters are matched
  777. -- against the mode strings of each of the N operands.
  778. --
  779. -- The mode string for each operand type is (see parseoperand()):
  780. -- Integer register: "rm", +"R" for eax, ax, al, +"C" for cl
  781. -- FP register: "f", +"F" for st0
  782. -- Index operand: "xm", +"O" for [disp] (pure offset)
  783. -- Immediate: "i", +"S" for signed 8 bit, +"1" for 1,
  784. -- +"I" for arg, +"P" for pointer
  785. -- Any: +"J" for valid jump targets
  786. --
  787. -- So a match character "m" (mixed) matches both an integer register
  788. -- and an index operand (to be encoded with the ModRM/SIB scheme).
  789. -- But "r" matches only a register and "x" only an index operand
  790. -- (e.g. for FP memory access operations).
  791. --
  792. -- The operand size match string starts right after the mode match
  793. -- characters and ends before the ":". "dwb" or "qdwb" is assumed, if empty.
  794. -- The effective data size of the operation is matched against this list.
  795. --
  796. -- If only the regular "b", "w", "d", "q", "t" operand sizes are
  797. -- present, then all operands must be the same size. Unspecified sizes
  798. -- are ignored, but at least one operand must have a size or the pattern
  799. -- won't match (use the "byte", "word", "dword", "qword", "tword"
  800. -- operand size overrides. E.g.: mov dword [eax], 1).
  801. --
  802. -- If the list has a "1" or "2" prefix, the operand size is taken
  803. -- from the respective operand and any other operand sizes are ignored.
  804. -- If the list contains only ".", all operand sizes are ignored.
  805. -- If the list has a "/" prefix, the concatenated (mixed) operand sizes
  806. -- are compared to the match.
  807. --
  808. -- E.g. "rrdw" matches for either two dword registers or two word
  809. -- registers. "Fx2dq" matches an st0 operand plus an index operand
  810. -- pointing to a dword (float) or qword (double).
  811. --
  812. -- Every character after the ":" is part of the pattern string:
  813. -- Hex chars are accumulated to form the opcode (left to right).
  814. -- "n" disables the standard opcode mods
  815. -- (otherwise: -1 for "b", o16 prefix for "w", rex.w for "q")
  816. -- "X" Force REX.W.
  817. -- "r"/"R" adds the reg. number from the 1st/2nd operand to the opcode.
  818. -- "m"/"M" generates ModRM/SIB from the 1st/2nd operand.
  819. -- The spare 3 bits are either filled with the last hex digit or
  820. -- the result from a previous "r"/"R". The opcode is restored.
  821. -- "u" Use VEX encoding, vvvv unused.
  822. -- "v"/"V" Use VEX encoding, vvvv from 1st/2nd operand (the operand is
  823. -- removed from the list used by future characters).
  824. -- "L" Force VEX.L
  825. --
  826. -- All of the following characters force a flush of the opcode:
  827. -- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand.
  828. -- "s" stores a 4 bit immediate from the last register operand,
  829. -- followed by 4 zero bits.
  830. -- "S" stores a signed 8 bit immediate from the last operand.
  831. -- "U" stores an unsigned 8 bit immediate from the last operand.
  832. -- "W" stores an unsigned 16 bit immediate from the last operand.
  833. -- "i" stores an operand sized immediate from the last operand.
  834. -- "I" dito, but generates an action code to optionally modify
  835. -- the opcode (+2) for a signed 8 bit immediate.
  836. -- "J" generates one of the REL action codes from the last operand.
  837. --
  838. ------------------------------------------------------------------------------
  839. -- Template strings for x86 instructions. Ordered by first opcode byte.
  840. -- Unimplemented opcodes (deliberate omissions) are marked with *.
  841. local map_op = {
  842. -- 00-05: add...
  843. -- 06: *push es
  844. -- 07: *pop es
  845. -- 08-0D: or...
  846. -- 0E: *push cs
  847. -- 0F: two byte opcode prefix
  848. -- 10-15: adc...
  849. -- 16: *push ss
  850. -- 17: *pop ss
  851. -- 18-1D: sbb...
  852. -- 1E: *push ds
  853. -- 1F: *pop ds
  854. -- 20-25: and...
  855. es_0 = "26",
  856. -- 27: *daa
  857. -- 28-2D: sub...
  858. cs_0 = "2E",
  859. -- 2F: *das
  860. -- 30-35: xor...
  861. ss_0 = "36",
  862. -- 37: *aaa
  863. -- 38-3D: cmp...
  864. ds_0 = "3E",
  865. -- 3F: *aas
  866. inc_1 = x64 and "m:FF0m" or "rdw:40r|m:FF0m",
  867. dec_1 = x64 and "m:FF1m" or "rdw:48r|m:FF1m",
  868. push_1 = (x64 and "rq:n50r|rw:50r|mq:nFF6m|mw:FF6m" or
  869. "rdw:50r|mdw:FF6m").."|S.:6AS|ib:n6Ai|i.:68i",
  870. pop_1 = x64 and "rq:n58r|rw:58r|mq:n8F0m|mw:8F0m" or "rdw:58r|mdw:8F0m",
  871. -- 60: *pusha, *pushad, *pushaw
  872. -- 61: *popa, *popad, *popaw
  873. -- 62: *bound rdw,x
  874. -- 63: x86: *arpl mw,rw
  875. movsxd_2 = x64 and "rm/qd:63rM",
  876. fs_0 = "64",
  877. gs_0 = "65",
  878. o16_0 = "66",
  879. a16_0 = not x64 and "67" or nil,
  880. a32_0 = x64 and "67",
  881. -- 68: push idw
  882. -- 69: imul rdw,mdw,idw
  883. -- 6A: push ib
  884. -- 6B: imul rdw,mdw,S
  885. -- 6C: *insb
  886. -- 6D: *insd, *insw
  887. -- 6E: *outsb
  888. -- 6F: *outsd, *outsw
  889. -- 70-7F: jcc lb
  890. -- 80: add... mb,i
  891. -- 81: add... mdw,i
  892. -- 82: *undefined
  893. -- 83: add... mdw,S
  894. test_2 = "mr:85Rm|rm:85rM|Ri:A9ri|mi:F70mi",
  895. -- 86: xchg rb,mb
  896. -- 87: xchg rdw,mdw
  897. -- 88: mov mb,r
  898. -- 89: mov mdw,r
  899. -- 8A: mov r,mb
  900. -- 8B: mov r,mdw
  901. -- 8C: *mov mdw,seg
  902. lea_2 = "rx1dq:8DrM",
  903. -- 8E: *mov seg,mdw
  904. -- 8F: pop mdw
  905. nop_0 = "90",
  906. xchg_2 = "Rrqdw:90R|rRqdw:90r|rm:87rM|mr:87Rm",
  907. cbw_0 = "6698",
  908. cwde_0 = "98",
  909. cdqe_0 = "4898",
  910. cwd_0 = "6699",
  911. cdq_0 = "99",
  912. cqo_0 = "4899",
  913. -- 9A: *call iw:idw
  914. wait_0 = "9B",
  915. fwait_0 = "9B",
  916. pushf_0 = "9C",
  917. pushfd_0 = not x64 and "9C",
  918. pushfq_0 = x64 and "9C",
  919. popf_0 = "9D",
  920. popfd_0 = not x64 and "9D",
  921. popfq_0 = x64 and "9D",
  922. sahf_0 = "9E",
  923. lahf_0 = "9F",
  924. mov_2 = "OR:A3o|RO:A1O|mr:89Rm|rm:8BrM|rib:nB0ri|ridw:B8ri|mi:C70mi",
  925. movsb_0 = "A4",
  926. movsw_0 = "66A5",
  927. movsd_0 = "A5",
  928. cmpsb_0 = "A6",
  929. cmpsw_0 = "66A7",
  930. cmpsd_0 = "A7",
  931. -- A8: test Rb,i
  932. -- A9: test Rdw,i
  933. stosb_0 = "AA",
  934. stosw_0 = "66AB",
  935. stosd_0 = "AB",
  936. lodsb_0 = "AC",
  937. lodsw_0 = "66AD",
  938. lodsd_0 = "AD",
  939. scasb_0 = "AE",
  940. scasw_0 = "66AF",
  941. scasd_0 = "AF",
  942. -- B0-B7: mov rb,i
  943. -- B8-BF: mov rdw,i
  944. -- C0: rol... mb,i
  945. -- C1: rol... mdw,i
  946. ret_1 = "i.:nC2W",
  947. ret_0 = "C3",
  948. -- C4: *les rdw,mq
  949. -- C5: *lds rdw,mq
  950. -- C6: mov mb,i
  951. -- C7: mov mdw,i
  952. -- C8: *enter iw,ib
  953. leave_0 = "C9",
  954. -- CA: *retf iw
  955. -- CB: *retf
  956. int3_0 = "CC",
  957. int_1 = "i.:nCDU",
  958. into_0 = "CE",
  959. -- CF: *iret
  960. -- D0: rol... mb,1
  961. -- D1: rol... mdw,1
  962. -- D2: rol... mb,cl
  963. -- D3: rol... mb,cl
  964. -- D4: *aam ib
  965. -- D5: *aad ib
  966. -- D6: *salc
  967. -- D7: *xlat
  968. -- D8-DF: floating point ops
  969. -- E0: *loopne
  970. -- E1: *loope
  971. -- E2: *loop
  972. -- E3: *jcxz, *jecxz
  973. -- E4: *in Rb,ib
  974. -- E5: *in Rdw,ib
  975. -- E6: *out ib,Rb
  976. -- E7: *out ib,Rdw
  977. call_1 = x64 and "mq:nFF2m|J.:E8nJ" or "md:FF2m|J.:E8J",
  978. jmp_1 = x64 and "mq:nFF4m|J.:E9nJ" or "md:FF4m|J.:E9J", -- short: EB
  979. -- EA: *jmp iw:idw
  980. -- EB: jmp ib
  981. -- EC: *in Rb,dx
  982. -- ED: *in Rdw,dx
  983. -- EE: *out dx,Rb
  984. -- EF: *out dx,Rdw
  985. lock_0 = "F0",
  986. int1_0 = "F1",
  987. repne_0 = "F2",
  988. repnz_0 = "F2",
  989. rep_0 = "F3",
  990. repe_0 = "F3",
  991. repz_0 = "F3",
  992. -- F4: *hlt
  993. cmc_0 = "F5",
  994. -- F6: test... mb,i; div... mb
  995. -- F7: test... mdw,i; div... mdw
  996. clc_0 = "F8",
  997. stc_0 = "F9",
  998. -- FA: *cli
  999. cld_0 = "FC",
  1000. std_0 = "FD",
  1001. -- FE: inc... mb
  1002. -- FF: inc... mdw
  1003. -- misc ops
  1004. not_1 = "m:F72m",
  1005. neg_1 = "m:F73m",
  1006. mul_1 = "m:F74m",
  1007. imul_1 = "m:F75m",
  1008. div_1 = "m:F76m",
  1009. idiv_1 = "m:F77m",
  1010. imul_2 = "rmqdw:0FAFrM|rIqdw:69rmI|rSqdw:6BrmS|riqdw:69rmi",
  1011. imul_3 = "rmIqdw:69rMI|rmSqdw:6BrMS|rmiqdw:69rMi",
  1012. movzx_2 = "rm/db:0FB6rM|rm/qb:|rm/wb:0FB6rM|rm/dw:0FB7rM|rm/qw:",
  1013. movsx_2 = "rm/db:0FBErM|rm/qb:|rm/wb:0FBErM|rm/dw:0FBFrM|rm/qw:",
  1014. bswap_1 = "rqd:0FC8r",
  1015. bsf_2 = "rmqdw:0FBCrM",
  1016. bsr_2 = "rmqdw:0FBDrM",
  1017. bt_2 = "mrqdw:0FA3Rm|miqdw:0FBA4mU",
  1018. btc_2 = "mrqdw:0FBBRm|miqdw:0FBA7mU",
  1019. btr_2 = "mrqdw:0FB3Rm|miqdw:0FBA6mU",
  1020. bts_2 = "mrqdw:0FABRm|miqdw:0FBA5mU",
  1021. shld_3 = "mriqdw:0FA4RmU|mrC/qq:0FA5Rm|mrC/dd:|mrC/ww:",
  1022. shrd_3 = "mriqdw:0FACRmU|mrC/qq:0FADRm|mrC/dd:|mrC/ww:",
  1023. rdtsc_0 = "0F31", -- P1+
  1024. rdpmc_0 = "0F33", -- P6+
  1025. cpuid_0 = "0FA2", -- P1+
  1026. -- floating point ops
  1027. fst_1 = "ff:DDD0r|xd:D92m|xq:nDD2m",
  1028. fstp_1 = "ff:DDD8r|xd:D93m|xq:nDD3m|xt:DB7m",
  1029. fld_1 = "ff:D9C0r|xd:D90m|xq:nDD0m|xt:DB5m",
  1030. fpop_0 = "DDD8", -- Alias for fstp st0.
  1031. fist_1 = "xw:nDF2m|xd:DB2m",
  1032. fistp_1 = "xw:nDF3m|xd:DB3m|xq:nDF7m",
  1033. fild_1 = "xw:nDF0m|xd:DB0m|xq:nDF5m",
  1034. fxch_0 = "D9C9",
  1035. fxch_1 = "ff:D9C8r",
  1036. fxch_2 = "fFf:D9C8r|Fff:D9C8R",
  1037. fucom_1 = "ff:DDE0r",
  1038. fucom_2 = "Fff:DDE0R",
  1039. fucomp_1 = "ff:DDE8r",
  1040. fucomp_2 = "Fff:DDE8R",
  1041. fucomi_1 = "ff:DBE8r", -- P6+
  1042. fucomi_2 = "Fff:DBE8R", -- P6+
  1043. fucomip_1 = "ff:DFE8r", -- P6+
  1044. fucomip_2 = "Fff:DFE8R", -- P6+
  1045. fcomi_1 = "ff:DBF0r", -- P6+
  1046. fcomi_2 = "Fff:DBF0R", -- P6+
  1047. fcomip_1 = "ff:DFF0r", -- P6+
  1048. fcomip_2 = "Fff:DFF0R", -- P6+
  1049. fucompp_0 = "DAE9",
  1050. fcompp_0 = "DED9",
  1051. fldenv_1 = "x.:D94m",
  1052. fnstenv_1 = "x.:D96m",
  1053. fstenv_1 = "x.:9BD96m",
  1054. fldcw_1 = "xw:nD95m",
  1055. fstcw_1 = "xw:n9BD97m",
  1056. fnstcw_1 = "xw:nD97m",
  1057. fstsw_1 = "Rw:n9BDFE0|xw:n9BDD7m",
  1058. fnstsw_1 = "Rw:nDFE0|xw:nDD7m",
  1059. fclex_0 = "9BDBE2",
  1060. fnclex_0 = "DBE2",
  1061. fnop_0 = "D9D0",
  1062. -- D9D1-D9DF: unassigned
  1063. fchs_0 = "D9E0",
  1064. fabs_0 = "D9E1",
  1065. -- D9E2: unassigned
  1066. -- D9E3: unassigned
  1067. ftst_0 = "D9E4",
  1068. fxam_0 = "D9E5",
  1069. -- D9E6: unassigned
  1070. -- D9E7: unassigned
  1071. fld1_0 = "D9E8",
  1072. fldl2t_0 = "D9E9",
  1073. fldl2e_0 = "D9EA",
  1074. fldpi_0 = "D9EB",
  1075. fldlg2_0 = "D9EC",
  1076. fldln2_0 = "D9ED",
  1077. fldz_0 = "D9EE",
  1078. -- D9EF: unassigned
  1079. f2xm1_0 = "D9F0",
  1080. fyl2x_0 = "D9F1",
  1081. fptan_0 = "D9F2",
  1082. fpatan_0 = "D9F3",
  1083. fxtract_0 = "D9F4",
  1084. fprem1_0 = "D9F5",
  1085. fdecstp_0 = "D9F6",
  1086. fincstp_0 = "D9F7",
  1087. fprem_0 = "D9F8",
  1088. fyl2xp1_0 = "D9F9",
  1089. fsqrt_0 = "D9FA",
  1090. fsincos_0 = "D9FB",
  1091. frndint_0 = "D9FC",
  1092. fscale_0 = "D9FD",
  1093. fsin_0 = "D9FE",
  1094. fcos_0 = "D9FF",
  1095. -- SSE, SSE2
  1096. andnpd_2 = "rmo:660F55rM",
  1097. andnps_2 = "rmo:0F55rM",
  1098. andpd_2 = "rmo:660F54rM",
  1099. andps_2 = "rmo:0F54rM",
  1100. clflush_1 = "x.:0FAE7m",
  1101. cmppd_3 = "rmio:660FC2rMU",
  1102. cmpps_3 = "rmio:0FC2rMU",
  1103. cmpsd_3 = "rrio:F20FC2rMU|rxi/oq:",
  1104. cmpss_3 = "rrio:F30FC2rMU|rxi/od:",
  1105. comisd_2 = "rro:660F2FrM|rx/oq:",
  1106. comiss_2 = "rro:0F2FrM|rx/od:",
  1107. cvtdq2pd_2 = "rro:F30FE6rM|rx/oq:",
  1108. cvtdq2ps_2 = "rmo:0F5BrM",
  1109. cvtpd2dq_2 = "rmo:F20FE6rM",
  1110. cvtpd2ps_2 = "rmo:660F5ArM",
  1111. cvtpi2pd_2 = "rx/oq:660F2ArM",
  1112. cvtpi2ps_2 = "rx/oq:0F2ArM",
  1113. cvtps2dq_2 = "rmo:660F5BrM",
  1114. cvtps2pd_2 = "rro:0F5ArM|rx/oq:",
  1115. cvtsd2si_2 = "rr/do:F20F2DrM|rr/qo:|rx/dq:|rxq:",
  1116. cvtsd2ss_2 = "rro:F20F5ArM|rx/oq:",
  1117. cvtsi2sd_2 = "rm/od:F20F2ArM|rm/oq:F20F2ArXM",
  1118. cvtsi2ss_2 = "rm/od:F30F2ArM|rm/oq:F30F2ArXM",
  1119. cvtss2sd_2 = "rro:F30F5ArM|rx/od:",
  1120. cvtss2si_2 = "rr/do:F30F2DrM|rr/qo:|rxd:|rx/qd:",
  1121. cvttpd2dq_2 = "rmo:660FE6rM",
  1122. cvttps2dq_2 = "rmo:F30F5BrM",
  1123. cvttsd2si_2 = "rr/do:F20F2CrM|rr/qo:|rx/dq:|rxq:",
  1124. cvttss2si_2 = "rr/do:F30F2CrM|rr/qo:|rxd:|rx/qd:",
  1125. fxsave_1 = "x.:0FAE0m",
  1126. fxrstor_1 = "x.:0FAE1m",
  1127. ldmxcsr_1 = "xd:0FAE2m",
  1128. lfence_0 = "0FAEE8",
  1129. maskmovdqu_2 = "rro:660FF7rM",
  1130. mfence_0 = "0FAEF0",
  1131. movapd_2 = "rmo:660F28rM|mro:660F29Rm",
  1132. movaps_2 = "rmo:0F28rM|mro:0F29Rm",
  1133. movd_2 = "rm/od:660F6ErM|rm/oq:660F6ErXM|mr/do:660F7ERm|mr/qo:",
  1134. movdqa_2 = "rmo:660F6FrM|mro:660F7FRm",
  1135. movdqu_2 = "rmo:F30F6FrM|mro:F30F7FRm",
  1136. movhlps_2 = "rro:0F12rM",
  1137. movhpd_2 = "rx/oq:660F16rM|xr/qo:n660F17Rm",
  1138. movhps_2 = "rx/oq:0F16rM|xr/qo:n0F17Rm",
  1139. movlhps_2 = "rro:0F16rM",
  1140. movlpd_2 = "rx/oq:660F12rM|xr/qo:n660F13Rm",
  1141. movlps_2 = "rx/oq:0F12rM|xr/qo:n0F13Rm",
  1142. movmskpd_2 = "rr/do:660F50rM",
  1143. movmskps_2 = "rr/do:0F50rM",
  1144. movntdq_2 = "xro:660FE7Rm",
  1145. movnti_2 = "xrqd:0FC3Rm",
  1146. movntpd_2 = "xro:660F2BRm",
  1147. movntps_2 = "xro:0F2BRm",
  1148. movq_2 = "rro:F30F7ErM|rx/oq:|xr/qo:n660FD6Rm",
  1149. movsd_2 = "rro:F20F10rM|rx/oq:|xr/qo:nF20F11Rm",
  1150. movss_2 = "rro:F30F10rM|rx/od:|xr/do:F30F11Rm",
  1151. movupd_2 = "rmo:660F10rM|mro:660F11Rm",
  1152. movups_2 = "rmo:0F10rM|mro:0F11Rm",
  1153. orpd_2 = "rmo:660F56rM",
  1154. orps_2 = "rmo:0F56rM",
  1155. pause_0 = "F390",
  1156. pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nRmU", -- Mem op: SSE4.1 only.
  1157. pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:",
  1158. pmovmskb_2 = "rr/do:660FD7rM",
  1159. prefetchnta_1 = "xb:n0F180m",
  1160. prefetcht0_1 = "xb:n0F181m",
  1161. prefetcht1_1 = "xb:n0F182m",
  1162. prefetcht2_1 = "xb:n0F183m",
  1163. pshufd_3 = "rmio:660F70rMU",
  1164. pshufhw_3 = "rmio:F30F70rMU",
  1165. pshuflw_3 = "rmio:F20F70rMU",
  1166. pslld_2 = "rmo:660FF2rM|rio:660F726mU",
  1167. pslldq_2 = "rio:660F737mU",
  1168. psllq_2 = "rmo:660FF3rM|rio:660F736mU",
  1169. psllw_2 = "rmo:660FF1rM|rio:660F716mU",
  1170. psrad_2 = "rmo:660FE2rM|rio:660F724mU",
  1171. psraw_2 = "rmo:660FE1rM|rio:660F714mU",
  1172. psrld_2 = "rmo:660FD2rM|rio:660F722mU",
  1173. psrldq_2 = "rio:660F733mU",
  1174. psrlq_2 = "rmo:660FD3rM|rio:660F732mU",
  1175. psrlw_2 = "rmo:660FD1rM|rio:660F712mU",
  1176. rcpps_2 = "rmo:0F53rM",
  1177. rcpss_2 = "rro:F30F53rM|rx/od:",
  1178. rsqrtps_2 = "rmo:0F52rM",
  1179. rsqrtss_2 = "rmo:F30F52rM",
  1180. sfence_0 = "0FAEF8",
  1181. shufpd_3 = "rmio:660FC6rMU",
  1182. shufps_3 = "rmio:0FC6rMU",
  1183. stmxcsr_1 = "xd:0FAE3m",
  1184. ucomisd_2 = "rro:660F2ErM|rx/oq:",
  1185. ucomiss_2 = "rro:0F2ErM|rx/od:",
  1186. unpckhpd_2 = "rmo:660F15rM",
  1187. unpckhps_2 = "rmo:0F15rM",
  1188. unpcklpd_2 = "rmo:660F14rM",
  1189. unpcklps_2 = "rmo:0F14rM",
  1190. xorpd_2 = "rmo:660F57rM",
  1191. xorps_2 = "rmo:0F57rM",
  1192. -- SSE3 ops
  1193. fisttp_1 = "xw:nDF1m|xd:DB1m|xq:nDD1m",
  1194. addsubpd_2 = "rmo:660FD0rM",
  1195. addsubps_2 = "rmo:F20FD0rM",
  1196. haddpd_2 = "rmo:660F7CrM",
  1197. haddps_2 = "rmo:F20F7CrM",
  1198. hsubpd_2 = "rmo:660F7DrM",
  1199. hsubps_2 = "rmo:F20F7DrM",
  1200. lddqu_2 = "rxo:F20FF0rM",
  1201. movddup_2 = "rmo:F20F12rM",
  1202. movshdup_2 = "rmo:F30F16rM",
  1203. movsldup_2 = "rmo:F30F12rM",
  1204. -- SSSE3 ops
  1205. pabsb_2 = "rmo:660F381CrM",
  1206. pabsd_2 = "rmo:660F381ErM",
  1207. pabsw_2 = "rmo:660F381DrM",
  1208. palignr_3 = "rmio:660F3A0FrMU",
  1209. phaddd_2 = "rmo:660F3802rM",
  1210. phaddsw_2 = "rmo:660F3803rM",
  1211. phaddw_2 = "rmo:660F3801rM",
  1212. phsubd_2 = "rmo:660F3806rM",
  1213. phsubsw_2 = "rmo:660F3807rM",
  1214. phsubw_2 = "rmo:660F3805rM",
  1215. pmaddubsw_2 = "rmo:660F3804rM",
  1216. pmulhrsw_2 = "rmo:660F380BrM",
  1217. pshufb_2 = "rmo:660F3800rM",
  1218. psignb_2 = "rmo:660F3808rM",
  1219. psignd_2 = "rmo:660F380ArM",
  1220. psignw_2 = "rmo:660F3809rM",
  1221. -- SSE4.1 ops
  1222. blendpd_3 = "rmio:660F3A0DrMU",
  1223. blendps_3 = "rmio:660F3A0CrMU",
  1224. blendvpd_3 = "rmRo:660F3815rM",
  1225. blendvps_3 = "rmRo:660F3814rM",
  1226. dppd_3 = "rmio:660F3A41rMU",
  1227. dpps_3 = "rmio:660F3A40rMU",
  1228. extractps_3 = "mri/do:660F3A17RmU|rri/qo:660F3A17RXmU",
  1229. insertps_3 = "rrio:660F3A41rMU|rxi/od:",
  1230. movntdqa_2 = "rxo:660F382ArM",
  1231. mpsadbw_3 = "rmio:660F3A42rMU",
  1232. packusdw_2 = "rmo:660F382BrM",
  1233. pblendvb_3 = "rmRo:660F3810rM",
  1234. pblendw_3 = "rmio:660F3A0ErMU",
  1235. pcmpeqq_2 = "rmo:660F3829rM",
  1236. pextrb_3 = "rri/do:660F3A14nRmU|rri/qo:|xri/bo:",
  1237. pextrd_3 = "mri/do:660F3A16RmU",
  1238. pextrq_3 = "mri/qo:660F3A16RmU",
  1239. -- pextrw is SSE2, mem operand is SSE4.1 only
  1240. phminposuw_2 = "rmo:660F3841rM",
  1241. pinsrb_3 = "rri/od:660F3A20nrMU|rxi/ob:",
  1242. pinsrd_3 = "rmi/od:660F3A22rMU",
  1243. pinsrq_3 = "rmi/oq:660F3A22rXMU",
  1244. pmaxsb_2 = "rmo:660F383CrM",
  1245. pmaxsd_2 = "rmo:660F383DrM",
  1246. pmaxud_2 = "rmo:660F383FrM",
  1247. pmaxuw_2 = "rmo:660F383ErM",
  1248. pminsb_2 = "rmo:660F3838rM",
  1249. pminsd_2 = "rmo:660F3839rM",
  1250. pminud_2 = "rmo:660F383BrM",
  1251. pminuw_2 = "rmo:660F383ArM",
  1252. pmovsxbd_2 = "rro:660F3821rM|rx/od:",
  1253. pmovsxbq_2 = "rro:660F3822rM|rx/ow:",
  1254. pmovsxbw_2 = "rro:660F3820rM|rx/oq:",
  1255. pmovsxdq_2 = "rro:660F3825rM|rx/oq:",
  1256. pmovsxwd_2 = "rro:660F3823rM|rx/oq:",
  1257. pmovsxwq_2 = "rro:660F3824rM|rx/od:",
  1258. pmovzxbd_2 = "rro:660F3831rM|rx/od:",
  1259. pmovzxbq_2 = "rro:660F3832rM|rx/ow:",
  1260. pmovzxbw_2 = "rro:660F3830rM|rx/oq:",
  1261. pmovzxdq_2 = "rro:660F3835rM|rx/oq:",
  1262. pmovzxwd_2 = "rro:660F3833rM|rx/oq:",
  1263. pmovzxwq_2 = "rro:660F3834rM|rx/od:",
  1264. pmuldq_2 = "rmo:660F3828rM",
  1265. pmulld_2 = "rmo:660F3840rM",
  1266. ptest_2 = "rmo:660F3817rM",
  1267. roundpd_3 = "rmio:660F3A09rMU",
  1268. roundps_3 = "rmio:660F3A08rMU",
  1269. roundsd_3 = "rrio:660F3A0BrMU|rxi/oq:",
  1270. roundss_3 = "rrio:660F3A0ArMU|rxi/od:",
  1271. -- SSE4.2 ops
  1272. crc32_2 = "rmqd:F20F38F1rM|rm/dw:66F20F38F1rM|rm/db:F20F38F0rM|rm/qb:",
  1273. pcmpestri_3 = "rmio:660F3A61rMU",
  1274. pcmpestrm_3 = "rmio:660F3A60rMU",
  1275. pcmpgtq_2 = "rmo:660F3837rM",
  1276. pcmpistri_3 = "rmio:660F3A63rMU",
  1277. pcmpistrm_3 = "rmio:660F3A62rMU",
  1278. popcnt_2 = "rmqdw:F30FB8rM",
  1279. -- SSE4a
  1280. extrq_2 = "rro:660F79rM",
  1281. extrq_3 = "riio:660F780mUU",
  1282. insertq_2 = "rro:F20F79rM",
  1283. insertq_4 = "rriio:F20F78rMUU",
  1284. lzcnt_2 = "rmqdw:F30FBDrM",
  1285. movntsd_2 = "xr/qo:nF20F2BRm",
  1286. movntss_2 = "xr/do:F30F2BRm",
  1287. -- popcnt is also in SSE4.2
  1288. -- AES-NI
  1289. aesdec_2 = "rmo:660F38DErM",
  1290. aesdeclast_2 = "rmo:660F38DFrM",
  1291. aesenc_2 = "rmo:660F38DCrM",
  1292. aesenclast_2 = "rmo:660F38DDrM",
  1293. aesimc_2 = "rmo:660F38DBrM",
  1294. aeskeygenassist_3 = "rmio:660F3ADFrMU",
  1295. pclmulqdq_3 = "rmio:660F3A44rMU",
  1296. -- AVX FP ops
  1297. vaddsubpd_3 = "rrmoy:660FVD0rM",
  1298. vaddsubps_3 = "rrmoy:F20FVD0rM",
  1299. vandpd_3 = "rrmoy:660FV54rM",
  1300. vandps_3 = "rrmoy:0FV54rM",
  1301. vandnpd_3 = "rrmoy:660FV55rM",
  1302. vandnps_3 = "rrmoy:0FV55rM",
  1303. vblendpd_4 = "rrmioy:660F3AV0DrMU",
  1304. vblendps_4 = "rrmioy:660F3AV0CrMU",
  1305. vblendvpd_4 = "rrmroy:660F3AV4BrMs",
  1306. vblendvps_4 = "rrmroy:660F3AV4ArMs",
  1307. vbroadcastf128_2 = "rx/yo:660F38u1ArM",
  1308. vcmppd_4 = "rrmioy:660FVC2rMU",
  1309. vcmpps_4 = "rrmioy:0FVC2rMU",
  1310. vcmpsd_4 = "rrrio:F20FVC2rMU|rrxi/ooq:",
  1311. vcmpss_4 = "rrrio:F30FVC2rMU|rrxi/ood:",
  1312. vcomisd_2 = "rro:660Fu2FrM|rx/oq:",
  1313. vcomiss_2 = "rro:0Fu2FrM|rx/od:",
  1314. vcvtdq2pd_2 = "rro:F30FuE6rM|rx/oq:|rm/yo:",
  1315. vcvtdq2ps_2 = "rmoy:0Fu5BrM",
  1316. vcvtpd2dq_2 = "rmoy:F20FuE6rM",
  1317. vcvtpd2ps_2 = "rmoy:660Fu5ArM",
  1318. vcvtps2dq_2 = "rmoy:660Fu5BrM",
  1319. vcvtps2pd_2 = "rro:0Fu5ArM|rx/oq:|rm/yo:",
  1320. vcvtsd2si_2 = "rr/do:F20Fu2DrM|rx/dq:|rr/qo:|rxq:",
  1321. vcvtsd2ss_3 = "rrro:F20FV5ArM|rrx/ooq:",
  1322. vcvtsi2sd_3 = "rrm/ood:F20FV2ArM|rrm/ooq:F20FVX2ArM",
  1323. vcvtsi2ss_3 = "rrm/ood:F30FV2ArM|rrm/ooq:F30FVX2ArM",
  1324. vcvtss2sd_3 = "rrro:F30FV5ArM|rrx/ood:",
  1325. vcvtss2si_2 = "rr/do:F30Fu2DrM|rxd:|rr/qo:|rx/qd:",
  1326. vcvttpd2dq_2 = "rmo:660FuE6rM|rm/oy:660FuLE6rM",
  1327. vcvttps2dq_2 = "rmoy:F30Fu5BrM",
  1328. vcvttsd2si_2 = "rr/do:F20Fu2CrM|rx/dq:|rr/qo:|rxq:",
  1329. vcvttss2si_2 = "rr/do:F30Fu2CrM|rxd:|rr/qo:|rx/qd:",
  1330. vdppd_4 = "rrmio:660F3AV41rMU",
  1331. vdpps_4 = "rrmioy:660F3AV40rMU",
  1332. vextractf128_3 = "mri/oy:660F3AuL19RmU",
  1333. vextractps_3 = "mri/do:660F3Au17RmU",
  1334. vhaddpd_3 = "rrmoy:660FV7CrM",
  1335. vhaddps_3 = "rrmoy:F20FV7CrM",
  1336. vhsubpd_3 = "rrmoy:660FV7DrM",
  1337. vhsubps_3 = "rrmoy:F20FV7DrM",
  1338. vinsertf128_4 = "rrmi/yyo:660F3AV18rMU",
  1339. vinsertps_4 = "rrrio:660F3AV21rMU|rrxi/ood:",
  1340. vldmxcsr_1 = "xd:0FuAE2m",
  1341. vmaskmovps_3 = "rrxoy:660F38V2CrM|xrroy:660F38V2ERm",
  1342. vmaskmovpd_3 = "rrxoy:660F38V2DrM|xrroy:660F38V2FRm",
  1343. vmovapd_2 = "rmoy:660Fu28rM|mroy:660Fu29Rm",
  1344. vmovaps_2 = "rmoy:0Fu28rM|mroy:0Fu29Rm",
  1345. vmovd_2 = "rm/od:660Fu6ErM|rm/oq:660FuX6ErM|mr/do:660Fu7ERm|mr/qo:",
  1346. vmovq_2 = "rro:F30Fu7ErM|rx/oq:|xr/qo:660FuD6Rm",
  1347. vmovddup_2 = "rmy:F20Fu12rM|rro:|rx/oq:",
  1348. vmovhlps_3 = "rrro:0FV12rM",
  1349. vmovhpd_2 = "xr/qo:660Fu17Rm",
  1350. vmovhpd_3 = "rrx/ooq:660FV16rM",
  1351. vmovhps_2 = "xr/qo:0Fu17Rm",
  1352. vmovhps_3 = "rrx/ooq:0FV16rM",
  1353. vmovlhps_3 = "rrro:0FV16rM",
  1354. vmovlpd_2 = "xr/qo:660Fu13Rm",
  1355. vmovlpd_3 = "rrx/ooq:660FV12rM",
  1356. vmovlps_2 = "xr/qo:0Fu13Rm",
  1357. vmovlps_3 = "rrx/ooq:0FV12rM",
  1358. vmovmskpd_2 = "rr/do:660Fu50rM|rr/dy:660FuL50rM",
  1359. vmovmskps_2 = "rr/do:0Fu50rM|rr/dy:0FuL50rM",
  1360. vmovntpd_2 = "xroy:660Fu2BRm",
  1361. vmovntps_2 = "xroy:0Fu2BRm",
  1362. vmovsd_2 = "rx/oq:F20Fu10rM|xr/qo:F20Fu11Rm",
  1363. vmovsd_3 = "rrro:F20FV10rM",
  1364. vmovshdup_2 = "rmoy:F30Fu16rM",
  1365. vmovsldup_2 = "rmoy:F30Fu12rM",
  1366. vmovss_2 = "rx/od:F30Fu10rM|xr/do:F30Fu11Rm",
  1367. vmovss_3 = "rrro:F30FV10rM",
  1368. vmovupd_2 = "rmoy:660Fu10rM|mroy:660Fu11Rm",
  1369. vmovups_2 = "rmoy:0Fu10rM|mroy:0Fu11Rm",
  1370. vorpd_3 = "rrmoy:660FV56rM",
  1371. vorps_3 = "rrmoy:0FV56rM",
  1372. vpermilpd_3 = "rrmoy:660F38V0DrM|rmioy:660F3Au05rMU",
  1373. vpermilps_3 = "rrmoy:660F38V0CrM|rmioy:660F3Au04rMU",
  1374. vperm2f128_4 = "rrmiy:660F3AV06rMU",
  1375. vptestpd_2 = "rmoy:660F38u0FrM",
  1376. vptestps_2 = "rmoy:660F38u0ErM",
  1377. vrcpps_2 = "rmoy:0Fu53rM",
  1378. vrcpss_3 = "rrro:F30FV53rM|rrx/ood:",
  1379. vrsqrtps_2 = "rmoy:0Fu52rM",
  1380. vrsqrtss_3 = "rrro:F30FV52rM|rrx/ood:",
  1381. vroundpd_3 = "rmioy:660F3AV09rMU",
  1382. vroundps_3 = "rmioy:660F3AV08rMU",
  1383. vroundsd_4 = "rrrio:660F3AV0BrMU|rrxi/ooq:",
  1384. vroundss_4 = "rrrio:660F3AV0ArMU|rrxi/ood:",
  1385. vshufpd_4 = "rrmioy:660FVC6rMU",
  1386. vshufps_4 = "rrmioy:0FVC6rMU",
  1387. vsqrtps_2 = "rmoy:0Fu51rM",
  1388. vsqrtss_2 = "rro:F30Fu51rM|rx/od:",
  1389. vsqrtpd_2 = "rmoy:660Fu51rM",
  1390. vsqrtsd_2 = "rro:F20Fu51rM|rx/oq:",
  1391. vstmxcsr_1 = "xd:0FuAE3m",
  1392. vucomisd_2 = "rro:660Fu2ErM|rx/oq:",
  1393. vucomiss_2 = "rro:0Fu2ErM|rx/od:",
  1394. vunpckhpd_3 = "rrmoy:660FV15rM",
  1395. vunpckhps_3 = "rrmoy:0FV15rM",
  1396. vunpcklpd_3 = "rrmoy:660FV14rM",
  1397. vunpcklps_3 = "rrmoy:0FV14rM",
  1398. vxorpd_3 = "rrmoy:660FV57rM",
  1399. vxorps_3 = "rrmoy:0FV57rM",
  1400. vzeroall_0 = "0FuL77",
  1401. vzeroupper_0 = "0Fu77",
  1402. -- AVX2 FP ops
  1403. vbroadcastss_2 = "rx/od:660F38u18rM|rx/yd:|rro:|rr/yo:",
  1404. vbroadcastsd_2 = "rx/yq:660F38u19rM|rr/yo:",
  1405. -- *vgather* (!vsib)
  1406. vpermpd_3 = "rmiy:660F3AuX01rMU",
  1407. vpermps_3 = "rrmy:660F38V16rM",
  1408. -- AVX, AVX2 integer ops
  1409. -- In general, xmm requires AVX, ymm requires AVX2.
  1410. vlddqu_2 = "rxoy:F20FuF0rM",
  1411. vmaskmovdqu_2 = "rro:660FuF7rM",
  1412. vmovdqa_2 = "rmoy:660Fu6FrM|mroy:660Fu7FRm",
  1413. vmovdqu_2 = "rmoy:F30Fu6FrM|mroy:F30Fu7FRm",
  1414. vmovntdq_2 = "xroy:660FuE7Rm",
  1415. vmovntdqa_2 = "rxoy:660F38u2ArM",
  1416. vmpsadbw_4 = "rrmioy:660F3AV42rMU",
  1417. vpabsb_2 = "rmoy:660F38u1CrM",
  1418. vpabsd_2 = "rmoy:660F38u1ErM",
  1419. vpabsw_2 = "rmoy:660F38u1DrM",
  1420. vpackusdw_3 = "rrmoy:660F38V2BrM",
  1421. vpalignr_4 = "rrmioy:660F3AV0FrMU",
  1422. vpblendvb_4 = "rrmroy:660F3AV4CrMs",
  1423. vpblendw_4 = "rrmioy:660F3AV0ErMU",
  1424. vpclmulqdq_4 = "rrmio:660F3AV44rMU",
  1425. vpcmpeqq_3 = "rrmoy:660F38V29rM",
  1426. vpcmpestri_3 = "rmio:660F3Au61rMU",
  1427. vpcmpestrm_3 = "rmio:660F3Au60rMU",
  1428. vpcmpgtq_3 = "rrmoy:660F38V37rM",
  1429. vpcmpistri_3 = "rmio:660F3Au63rMU",
  1430. vpcmpistrm_3 = "rmio:660F3Au62rMU",
  1431. vpextrb_3 = "rri/do:660F3Au14nRmU|rri/qo:|xri/bo:",
  1432. vpextrw_3 = "rri/do:660FuC5rMU|xri/wo:660F3Au15nRmU",
  1433. vpextrd_3 = "mri/do:660F3Au16RmU",
  1434. vpextrq_3 = "mri/qo:660F3Au16RmU",
  1435. vphaddw_3 = "rrmoy:660F38V01rM",
  1436. vphaddd_3 = "rrmoy:660F38V02rM",
  1437. vphaddsw_3 = "rrmoy:660F38V03rM",
  1438. vphminposuw_2 = "rmo:660F38u41rM",
  1439. vphsubw_3 = "rrmoy:660F38V05rM",
  1440. vphsubd_3 = "rrmoy:660F38V06rM",
  1441. vphsubsw_3 = "rrmoy:660F38V07rM",
  1442. vpinsrb_4 = "rrri/ood:660F3AV20rMU|rrxi/oob:",
  1443. vpinsrw_4 = "rrri/ood:660FVC4rMU|rrxi/oow:",
  1444. vpinsrd_4 = "rrmi/ood:660F3AV22rMU",
  1445. vpinsrq_4 = "rrmi/ooq:660F3AVX22rMU",
  1446. vpmaddubsw_3 = "rrmoy:660F38V04rM",
  1447. vpmaxsb_3 = "rrmoy:660F38V3CrM",
  1448. vpmaxsd_3 = "rrmoy:660F38V3DrM",
  1449. vpmaxuw_3 = "rrmoy:660F38V3ErM",
  1450. vpmaxud_3 = "rrmoy:660F38V3FrM",
  1451. vpminsb_3 = "rrmoy:660F38V38rM",
  1452. vpminsd_3 = "rrmoy:660F38V39rM",
  1453. vpminuw_3 = "rrmoy:660F38V3ArM",
  1454. vpminud_3 = "rrmoy:660F38V3BrM",
  1455. vpmovmskb_2 = "rr/do:660FuD7rM|rr/dy:660FuLD7rM",
  1456. vpmovsxbw_2 = "rroy:660F38u20rM|rx/oq:|rx/yo:",
  1457. vpmovsxbd_2 = "rroy:660F38u21rM|rx/od:|rx/yq:",
  1458. vpmovsxbq_2 = "rroy:660F38u22rM|rx/ow:|rx/yd:",
  1459. vpmovsxwd_2 = "rroy:660F38u23rM|rx/oq:|rx/yo:",
  1460. vpmovsxwq_2 = "rroy:660F38u24rM|rx/od:|rx/yq:",
  1461. vpmovsxdq_2 = "rroy:660F38u25rM|rx/oq:|rx/yo:",
  1462. vpmovzxbw_2 = "rroy:660F38u30rM|rx/oq:|rx/yo:",
  1463. vpmovzxbd_2 = "rroy:660F38u31rM|rx/od:|rx/yq:",
  1464. vpmovzxbq_2 = "rroy:660F38u32rM|rx/ow:|rx/yd:",
  1465. vpmovzxwd_2 = "rroy:660F38u33rM|rx/oq:|rx/yo:",
  1466. vpmovzxwq_2 = "rroy:660F38u34rM|rx/od:|rx/yq:",
  1467. vpmovzxdq_2 = "rroy:660F38u35rM|rx/oq:|rx/yo:",
  1468. vpmuldq_3 = "rrmoy:660F38V28rM",
  1469. vpmulhrsw_3 = "rrmoy:660F38V0BrM",
  1470. vpmulld_3 = "rrmoy:660F38V40rM",
  1471. vpshufb_3 = "rrmoy:660F38V00rM",
  1472. vpshufd_3 = "rmioy:660Fu70rMU",
  1473. vpshufhw_3 = "rmioy:F30Fu70rMU",
  1474. vpshuflw_3 = "rmioy:F20Fu70rMU",
  1475. vpsignb_3 = "rrmoy:660F38V08rM",
  1476. vpsignw_3 = "rrmoy:660F38V09rM",
  1477. vpsignd_3 = "rrmoy:660F38V0ArM",
  1478. vpslldq_3 = "rrioy:660Fv737mU",
  1479. vpsllw_3 = "rrmoy:660FVF1rM|rrioy:660Fv716mU",
  1480. vpslld_3 = "rrmoy:660FVF2rM|rrioy:660Fv726mU",
  1481. vpsllq_3 = "rrmoy:660FVF3rM|rrioy:660Fv736mU",
  1482. vpsraw_3 = "rrmoy:660FVE1rM|rrioy:660Fv714mU",
  1483. vpsrad_3 = "rrmoy:660FVE2rM|rrioy:660Fv724mU",
  1484. vpsrldq_3 = "rrioy:660Fv733mU",
  1485. vpsrlw_3 = "rrmoy:660FVD1rM|rrioy:660Fv712mU",
  1486. vpsrld_3 = "rrmoy:660FVD2rM|rrioy:660Fv722mU",
  1487. vpsrlq_3 = "rrmoy:660FVD3rM|rrioy:660Fv732mU",
  1488. vptest_2 = "rmoy:660F38u17rM",
  1489. -- AVX2 integer ops
  1490. vbroadcasti128_2 = "rx/yo:660F38u5ArM",
  1491. vinserti128_4 = "rrmi/yyo:660F3AV38rMU",
  1492. vextracti128_3 = "mri/oy:660F3AuL39RmU",
  1493. vpblendd_4 = "rrmioy:660F3AV02rMU",
  1494. vpbroadcastb_2 = "rro:660F38u78rM|rx/ob:|rr/yo:|rx/yb:",
  1495. vpbroadcastw_2 = "rro:660F38u79rM|rx/ow:|rr/yo:|rx/yw:",
  1496. vpbroadcastd_2 = "rro:660F38u58rM|rx/od:|rr/yo:|rx/yd:",
  1497. vpbroadcastq_2 = "rro:660F38u59rM|rx/oq:|rr/yo:|rx/yq:",
  1498. vpermd_3 = "rrmy:660F38V36rM",
  1499. vpermq_3 = "rmiy:660F3AuX00rMU",
  1500. -- *vpgather* (!vsib)
  1501. vperm2i128_4 = "rrmiy:660F3AV46rMU",
  1502. vpmaskmovd_3 = "rrxoy:660F38V8CrM|xrroy:660F38V8ERm",
  1503. vpmaskmovq_3 = "rrxoy:660F38VX8CrM|xrroy:660F38VX8ERm",
  1504. vpsllvd_3 = "rrmoy:660F38V47rM",
  1505. vpsllvq_3 = "rrmoy:660F38VX47rM",
  1506. vpsravd_3 = "rrmoy:660F38V46rM",
  1507. vpsrlvd_3 = "rrmoy:660F38V45rM",
  1508. vpsrlvq_3 = "rrmoy:660F38VX45rM",
  1509. }
  1510. ------------------------------------------------------------------------------
  1511. -- Arithmetic ops.
  1512. for name,n in pairs{ add = 0, ["or"] = 1, adc = 2, sbb = 3,
  1513. ["and"] = 4, sub = 5, xor = 6, cmp = 7 } do
  1514. local n8 = shl(n, 3)
  1515. map_op[name.."_2"] = format(
  1516. "mr:%02XRm|rm:%02XrM|mI1qdw:81%XmI|mS1qdw:83%XmS|Ri1qdwb:%02Xri|mi1qdwb:81%Xmi",
  1517. 1+n8, 3+n8, n, n, 5+n8, n)
  1518. end
  1519. -- Shift ops.
  1520. for name,n in pairs{ rol = 0, ror = 1, rcl = 2, rcr = 3,
  1521. shl = 4, shr = 5, sar = 7, sal = 4 } do
  1522. map_op[name.."_2"] = format("m1:D1%Xm|mC1qdwb:D3%Xm|mi:C1%XmU", n, n, n)
  1523. end
  1524. -- Conditional ops.
  1525. for cc,n in pairs(map_cc) do
  1526. map_op["j"..cc.."_1"] = format("J.:n0F8%XJ", n) -- short: 7%X
  1527. map_op["set"..cc.."_1"] = format("mb:n0F9%X2m", n)
  1528. map_op["cmov"..cc.."_2"] = format("rmqdw:0F4%XrM", n) -- P6+
  1529. end
  1530. -- FP arithmetic ops.
  1531. for name,n in pairs{ add = 0, mul = 1, com = 2, comp = 3,
  1532. sub = 4, subr = 5, div = 6, divr = 7 } do
  1533. local nc = 0xc0 + shl(n, 3)
  1534. local nr = nc + (n < 4 and 0 or (n % 2 == 0 and 8 or -8))
  1535. local fn = "f"..name
  1536. map_op[fn.."_1"] = format("ff:D8%02Xr|xd:D8%Xm|xq:nDC%Xm", nc, n, n)
  1537. if n == 2 or n == 3 then
  1538. map_op[fn.."_2"] = format("Fff:D8%02XR|Fx2d:D8%XM|Fx2q:nDC%XM", nc, n, n)
  1539. else
  1540. map_op[fn.."_2"] = format("Fff:D8%02XR|fFf:DC%02Xr|Fx2d:D8%XM|Fx2q:nDC%XM", nc, nr, n, n)
  1541. map_op[fn.."p_1"] = format("ff:DE%02Xr", nr)
  1542. map_op[fn.."p_2"] = format("fFf:DE%02Xr", nr)
  1543. end
  1544. map_op["fi"..name.."_1"] = format("xd:DA%Xm|xw:nDE%Xm", n, n)
  1545. end
  1546. -- FP conditional moves.
  1547. for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do
  1548. local nc = 0xdac0 + shl(band(n, 3), 3) + shl(band(n, 4), 6)
  1549. map_op["fcmov"..cc.."_1"] = format("ff:%04Xr", nc) -- P6+
  1550. map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+
  1551. end
  1552. -- SSE / AVX FP arithmetic ops.
  1553. for name,n in pairs{ sqrt = 1, add = 8, mul = 9,
  1554. sub = 12, min = 13, div = 14, max = 15 } do
  1555. map_op[name.."ps_2"] = format("rmo:0F5%XrM", n)
  1556. map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n)
  1557. map_op[name.."pd_2"] = format("rmo:660F5%XrM", n)
  1558. map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n)
  1559. if n ~= 1 then
  1560. map_op["v"..name.."ps_3"] = format("rrmoy:0FV5%XrM", n)
  1561. map_op["v"..name.."ss_3"] = format("rrro:F30FV5%XrM|rrx/ood:", n)
  1562. map_op["v"..name.."pd_3"] = format("rrmoy:660FV5%XrM", n)
  1563. map_op["v"..name.."sd_3"] = format("rrro:F20FV5%XrM|rrx/ooq:", n)
  1564. end
  1565. end
  1566. -- SSE2 / AVX / AVX2 integer arithmetic ops (66 0F leaf).
  1567. for name,n in pairs{
  1568. paddb = 0xFC, paddw = 0xFD, paddd = 0xFE, paddq = 0xD4,
  1569. paddsb = 0xEC, paddsw = 0xED, packssdw = 0x6B,
  1570. packsswb = 0x63, packuswb = 0x67, paddusb = 0xDC,
  1571. paddusw = 0xDD, pand = 0xDB, pandn = 0xDF, pavgb = 0xE0,
  1572. pavgw = 0xE3, pcmpeqb = 0x74, pcmpeqd = 0x76,
  1573. pcmpeqw = 0x75, pcmpgtb = 0x64, pcmpgtd = 0x66,
  1574. pcmpgtw = 0x65, pmaddwd = 0xF5, pmaxsw = 0xEE,
  1575. pmaxub = 0xDE, pminsw = 0xEA, pminub = 0xDA,
  1576. pmulhuw = 0xE4, pmulhw = 0xE5, pmullw = 0xD5,
  1577. pmuludq = 0xF4, por = 0xEB, psadbw = 0xF6, psubb = 0xF8,
  1578. psubw = 0xF9, psubd = 0xFA, psubq = 0xFB, psubsb = 0xE8,
  1579. psubsw = 0xE9, psubusb = 0xD8, psubusw = 0xD9,
  1580. punpckhbw = 0x68, punpckhwd = 0x69, punpckhdq = 0x6A,
  1581. punpckhqdq = 0x6D, punpcklbw = 0x60, punpcklwd = 0x61,
  1582. punpckldq = 0x62, punpcklqdq = 0x6C, pxor = 0xEF
  1583. } do
  1584. map_op[name.."_2"] = format("rmo:660F%02XrM", n)
  1585. map_op["v"..name.."_3"] = format("rrmoy:660FV%02XrM", n)
  1586. end
  1587. ------------------------------------------------------------------------------
  1588. local map_vexarg = { u = false, v = 1, V = 2 }
  1589. -- Process pattern string.
  1590. local function dopattern(pat, args, sz, op, needrex)
  1591. local digit, addin, vex
  1592. local opcode = 0
  1593. local szov = sz
  1594. local narg = 1
  1595. local rex = 0
  1596. -- Limit number of section buffer positions used by a single dasm_put().
  1597. -- A single opcode needs a maximum of 6 positions.
  1598. if secpos+6 > maxsecpos then wflush() end
  1599. -- Process each character.
  1600. for c in gmatch(pat.."|", ".") do
  1601. if match(c, "%x") then -- Hex digit.
  1602. digit = byte(c) - 48
  1603. if digit > 48 then digit = digit - 39
  1604. elseif digit > 16 then digit = digit - 7 end
  1605. opcode = opcode*16 + digit
  1606. addin = nil
  1607. elseif c == "n" then -- Disable operand size mods for opcode.
  1608. szov = nil
  1609. elseif c == "X" then -- Force REX.W.
  1610. rex = 8
  1611. elseif c == "L" then -- Force VEX.L.
  1612. vex.l = true
  1613. elseif c == "r" then -- Merge 1st operand regno. into opcode.
  1614. addin = args[1]; opcode = opcode + (addin.reg % 8)
  1615. if narg < 2 then narg = 2 end
  1616. elseif c == "R" then -- Merge 2nd operand regno. into opcode.
  1617. addin = args[2]; opcode = opcode + (addin.reg % 8)
  1618. narg = 3
  1619. elseif c == "m" or c == "M" then -- Encode ModRM/SIB.
  1620. local s
  1621. if addin then
  1622. s = addin.reg
  1623. opcode = opcode - band(s, 7) -- Undo regno opcode merge.
  1624. else
  1625. s = band(opcode, 15) -- Undo last digit.
  1626. opcode = shr(opcode, 4)
  1627. end
  1628. local nn = c == "m" and 1 or 2
  1629. local t = args[nn]
  1630. if narg <= nn then narg = nn + 1 end
  1631. if szov == "q" and rex == 0 then rex = rex + 8 end
  1632. if t.reg and t.reg > 7 then rex = rex + 1 end
  1633. if t.xreg and t.xreg > 7 then rex = rex + 2 end
  1634. if s > 7 then rex = rex + 4 end
  1635. if needrex then rex = rex + 16 end
  1636. wputop(szov, opcode, rex, vex); opcode = nil
  1637. local imark = sub(pat, -1) -- Force a mark (ugly).
  1638. -- Put ModRM/SIB with regno/last digit as spare.
  1639. wputmrmsib(t, imark, s, addin and addin.vreg)
  1640. addin = nil
  1641. elseif map_vexarg[c] ~= nil then -- Encode using VEX prefix
  1642. local b = band(opcode, 255); opcode = shr(opcode, 8)
  1643. local m = 1
  1644. if b == 0x38 then m = 2
  1645. elseif b == 0x3a then m = 3 end
  1646. if m ~= 1 then b = band(opcode, 255); opcode = shr(opcode, 8) end
  1647. if b ~= 0x0f then
  1648. werror("expected `0F', `0F38', or `0F3A' to precede `"..c..
  1649. "' in pattern `"..pat.."' for `"..op.."'")
  1650. end
  1651. local v = map_vexarg[c]
  1652. if v then v = remove(args, v) end
  1653. b = band(opcode, 255)
  1654. local p = 0
  1655. if b == 0x66 then p = 1
  1656. elseif b == 0xf3 then p = 2
  1657. elseif b == 0xf2 then p = 3 end
  1658. if p ~= 0 then opcode = shr(opcode, 8) end
  1659. if opcode ~= 0 then wputop(nil, opcode, 0); opcode = 0 end
  1660. vex = { m = m, p = p, v = v }
  1661. else
  1662. if opcode then -- Flush opcode.
  1663. if szov == "q" and rex == 0 then rex = rex + 8 end
  1664. if needrex then rex = rex + 16 end
  1665. if addin and addin.reg == -1 then
  1666. wputop(szov, opcode - 7, rex, vex)
  1667. waction("VREG", addin.vreg); wputxb(0)
  1668. else
  1669. if addin and addin.reg > 7 then rex = rex + 1 end
  1670. wputop(szov, opcode, rex, vex)
  1671. end
  1672. opcode = nil
  1673. end
  1674. if c == "|" then break end
  1675. if c == "o" then -- Offset (pure 32 bit displacement).
  1676. wputdarg(args[1].disp); if narg < 2 then narg = 2 end
  1677. elseif c == "O" then
  1678. wputdarg(args[2].disp); narg = 3
  1679. else
  1680. -- Anything else is an immediate operand.
  1681. local a = args[narg]
  1682. narg = narg + 1
  1683. local mode, imm = a.mode, a.imm
  1684. if mode == "iJ" and not match("iIJ", c) then
  1685. werror("bad operand size for label")
  1686. end
  1687. if c == "S" then
  1688. wputsbarg(imm)
  1689. elseif c == "U" then
  1690. wputbarg(imm)
  1691. elseif c == "W" then
  1692. wputwarg(imm)
  1693. elseif c == "i" or c == "I" then
  1694. if mode == "iJ" then
  1695. wputlabel("IMM_", imm, 1)
  1696. elseif mode == "iI" and c == "I" then
  1697. waction(sz == "w" and "IMM_WB" or "IMM_DB", imm)
  1698. else
  1699. wputszarg(sz, imm)
  1700. end
  1701. elseif c == "J" then
  1702. if mode == "iPJ" then
  1703. waction("REL_A", imm) -- !x64 (secpos)
  1704. else
  1705. wputlabel("REL_", imm, 2)
  1706. end
  1707. elseif c == "s" then
  1708. local reg = a.reg
  1709. if reg < 0 then
  1710. wputb(0)
  1711. waction("VREG", a.vreg); wputxb(5)
  1712. else
  1713. wputb(shl(reg, 4))
  1714. end
  1715. else
  1716. werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'")
  1717. end
  1718. end
  1719. end
  1720. end
  1721. end
  1722. ------------------------------------------------------------------------------
  1723. -- Mapping of operand modes to short names. Suppress output with '#'.
  1724. local map_modename = {
  1725. r = "reg", R = "eax", C = "cl", x = "mem", m = "mrm", i = "imm",
  1726. f = "stx", F = "st0", J = "lbl", ["1"] = "1",
  1727. I = "#", S = "#", O = "#",
  1728. }
  1729. -- Return a table/string showing all possible operand modes.
  1730. local function templatehelp(template, nparams)
  1731. if nparams == 0 then return "" end
  1732. local t = {}
  1733. for tm in gmatch(template, "[^%|]+") do
  1734. local s = map_modename[sub(tm, 1, 1)]
  1735. s = s..gsub(sub(tm, 2, nparams), ".", function(c)
  1736. return ", "..map_modename[c]
  1737. end)
  1738. if not match(s, "#") then t[#t+1] = s end
  1739. end
  1740. return t
  1741. end
  1742. -- Match operand modes against mode match part of template.
  1743. local function matchtm(tm, args)
  1744. for i=1,#args do
  1745. if not match(args[i].mode, sub(tm, i, i)) then return end
  1746. end
  1747. return true
  1748. end
  1749. -- Handle opcodes defined with template strings.
  1750. map_op[".template__"] = function(params, template, nparams)
  1751. if not params then return templatehelp(template, nparams) end
  1752. local args = {}
  1753. -- Zero-operand opcodes have no match part.
  1754. if #params == 0 then
  1755. dopattern(template, args, "d", params.op, nil)
  1756. return
  1757. end
  1758. -- Determine common operand size (coerce undefined size) or flag as mixed.
  1759. local sz, szmix, needrex
  1760. for i,p in ipairs(params) do
  1761. args[i] = parseoperand(p)
  1762. local nsz = args[i].opsize
  1763. if nsz then
  1764. if sz and sz ~= nsz then szmix = true else sz = nsz end
  1765. end
  1766. local nrex = args[i].needrex
  1767. if nrex ~= nil then
  1768. if needrex == nil then
  1769. needrex = nrex
  1770. elseif needrex ~= nrex then
  1771. werror("bad mix of byte-addressable registers")
  1772. end
  1773. end
  1774. end
  1775. -- Try all match:pattern pairs (separated by '|').
  1776. local gotmatch, lastpat
  1777. for tm in gmatch(template, "[^%|]+") do
  1778. -- Split off size match (starts after mode match) and pattern string.
  1779. local szm, pat = match(tm, "^(.-):(.*)$", #args+1)
  1780. if pat == "" then pat = lastpat else lastpat = pat end
  1781. if matchtm(tm, args) then
  1782. local prefix = sub(szm, 1, 1)
  1783. if prefix == "/" then -- Exactly match leading operand sizes.
  1784. for i = #szm,1,-1 do
  1785. if i == 1 then
  1786. dopattern(pat, args, sz, params.op, needrex) -- Process pattern.
  1787. return
  1788. elseif args[i-1].opsize ~= sub(szm, i, i) then
  1789. break
  1790. end
  1791. end
  1792. else -- Match common operand size.
  1793. local szp = sz
  1794. if szm == "" then szm = x64 and "qdwb" or "dwb" end -- Default sizes.
  1795. if prefix == "1" then szp = args[1].opsize; szmix = nil
  1796. elseif prefix == "2" then szp = args[2].opsize; szmix = nil end
  1797. if not szmix and (prefix == "." or match(szm, szp or "#")) then
  1798. dopattern(pat, args, szp, params.op, needrex) -- Process pattern.
  1799. return
  1800. end
  1801. end
  1802. gotmatch = true
  1803. end
  1804. end
  1805. local msg = "bad operand mode"
  1806. if gotmatch then
  1807. if szmix then
  1808. msg = "mixed operand size"
  1809. else
  1810. msg = sz and "bad operand size" or "missing operand size"
  1811. end
  1812. end
  1813. werror(msg.." in `"..opmodestr(params.op, args).."'")
  1814. end
  1815. ------------------------------------------------------------------------------
  1816. -- x64-specific opcode for 64 bit immediates and displacements.
  1817. if x64 then
  1818. function map_op.mov64_2(params)
  1819. if not params then return { "reg, imm", "reg, [disp]", "[disp], reg" } end
  1820. if secpos+2 > maxsecpos then wflush() end
  1821. local opcode, op64, sz, rex, vreg
  1822. local op64 = match(params[1], "^%[%s*(.-)%s*%]$")
  1823. if op64 then
  1824. local a = parseoperand(params[2])
  1825. if a.mode ~= "rmR" then werror("bad operand mode") end
  1826. sz = a.opsize
  1827. rex = sz == "q" and 8 or 0
  1828. opcode = 0xa3
  1829. else
  1830. op64 = match(params[2], "^%[%s*(.-)%s*%]$")
  1831. local a = parseoperand(params[1])
  1832. if op64 then
  1833. if a.mode ~= "rmR" then werror("bad operand mode") end
  1834. sz = a.opsize
  1835. rex = sz == "q" and 8 or 0
  1836. opcode = 0xa1
  1837. else
  1838. if sub(a.mode, 1, 1) ~= "r" or a.opsize ~= "q" then
  1839. werror("bad operand mode")
  1840. end
  1841. op64 = params[2]
  1842. if a.reg == -1 then
  1843. vreg = a.vreg
  1844. opcode = 0xb8
  1845. else
  1846. opcode = 0xb8 + band(a.reg, 7)
  1847. end
  1848. rex = a.reg > 7 and 9 or 8
  1849. end
  1850. end
  1851. wputop(sz, opcode, rex)
  1852. if vreg then waction("VREG", vreg); wputxb(0) end
  1853. waction("IMM_D", format("(unsigned int)(%s)", op64))
  1854. waction("IMM_D", format("(unsigned int)((%s)>>32)", op64))
  1855. end
  1856. end
  1857. ------------------------------------------------------------------------------
  1858. -- Pseudo-opcodes for data storage.
  1859. local function op_data(params)
  1860. if not params then return "imm..." end
  1861. local sz = sub(params.op, 2, 2)
  1862. if sz == "a" then sz = addrsize end
  1863. for _,p in ipairs(params) do
  1864. local a = parseoperand(p)
  1865. if sub(a.mode, 1, 1) ~= "i" or (a.opsize and a.opsize ~= sz) then
  1866. werror("bad mode or size in `"..p.."'")
  1867. end
  1868. if a.mode == "iJ" then
  1869. wputlabel("IMM_", a.imm, 1)
  1870. else
  1871. wputszarg(sz, a.imm)
  1872. end
  1873. if secpos+2 > maxsecpos then wflush() end
  1874. end
  1875. end
  1876. map_op[".byte_*"] = op_data
  1877. map_op[".sbyte_*"] = op_data
  1878. map_op[".word_*"] = op_data
  1879. map_op[".dword_*"] = op_data
  1880. map_op[".aword_*"] = op_data
  1881. ------------------------------------------------------------------------------
  1882. -- Pseudo-opcode to mark the position where the action list is to be emitted.
  1883. map_op[".actionlist_1"] = function(params)
  1884. if not params then return "cvar" end
  1885. local name = params[1] -- No syntax check. You get to keep the pieces.
  1886. wline(function(out) writeactions(out, name) end)
  1887. end
  1888. -- Pseudo-opcode to mark the position where the global enum is to be emitted.
  1889. map_op[".globals_1"] = function(params)
  1890. if not params then return "prefix" end
  1891. local prefix = params[1] -- No syntax check. You get to keep the pieces.
  1892. wline(function(out) writeglobals(out, prefix) end)
  1893. end
  1894. -- Pseudo-opcode to mark the position where the global names are to be emitted.
  1895. map_op[".globalnames_1"] = function(params)
  1896. if not params then return "cvar" end
  1897. local name = params[1] -- No syntax check. You get to keep the pieces.
  1898. wline(function(out) writeglobalnames(out, name) end)
  1899. end
  1900. -- Pseudo-opcode to mark the position where the extern names are to be emitted.
  1901. map_op[".externnames_1"] = function(params)
  1902. if not params then return "cvar" end
  1903. local name = params[1] -- No syntax check. You get to keep the pieces.
  1904. wline(function(out) writeexternnames(out, name) end)
  1905. end
  1906. ------------------------------------------------------------------------------
  1907. -- Label pseudo-opcode (converted from trailing colon form).
  1908. map_op[".label_2"] = function(params)
  1909. if not params then return "[1-9] | ->global | =>pcexpr [, addr]" end
  1910. if secpos+2 > maxsecpos then wflush() end
  1911. local a = parseoperand(params[1])
  1912. local mode, imm = a.mode, a.imm
  1913. if type(imm) == "number" and (mode == "iJ" or (imm >= 1 and imm <= 9)) then
  1914. -- Local label (1: ... 9:) or global label (->global:).
  1915. waction("LABEL_LG", nil, 1)
  1916. wputxb(imm)
  1917. elseif mode == "iJ" then
  1918. -- PC label (=>pcexpr:).
  1919. waction("LABEL_PC", imm)
  1920. else
  1921. werror("bad label definition")
  1922. end
  1923. -- SETLABEL must immediately follow LABEL_LG/LABEL_PC.
  1924. local addr = params[2]
  1925. if addr then
  1926. local a = parseoperand(addr)
  1927. if a.mode == "iPJ" then
  1928. waction("SETLABEL", a.imm)
  1929. else
  1930. werror("bad label assignment")
  1931. end
  1932. end
  1933. end
  1934. map_op[".label_1"] = map_op[".label_2"]
  1935. ------------------------------------------------------------------------------
  1936. -- Alignment pseudo-opcode.
  1937. map_op[".align_1"] = function(params)
  1938. if not params then return "numpow2" end
  1939. if secpos+1 > maxsecpos then wflush() end
  1940. local align = tonumber(params[1]) or map_opsizenum[map_opsize[params[1]]]
  1941. if align then
  1942. local x = align
  1943. -- Must be a power of 2 in the range (2 ... 256).
  1944. for i=1,8 do
  1945. x = x / 2
  1946. if x == 1 then
  1947. waction("ALIGN", nil, 1)
  1948. wputxb(align-1) -- Action byte is 2**n-1.
  1949. return
  1950. end
  1951. end
  1952. end
  1953. werror("bad alignment")
  1954. end
  1955. -- Spacing pseudo-opcode.
  1956. map_op[".space_2"] = function(params)
  1957. if not params then return "num [, filler]" end
  1958. if secpos+1 > maxsecpos then wflush() end
  1959. waction("SPACE", params[1])
  1960. local fill = params[2]
  1961. if fill then
  1962. fill = tonumber(fill)
  1963. if not fill or fill < 0 or fill > 255 then werror("bad filler") end
  1964. end
  1965. wputxb(fill or 0)
  1966. end
  1967. map_op[".space_1"] = map_op[".space_2"]
  1968. ------------------------------------------------------------------------------
  1969. -- Pseudo-opcode for (primitive) type definitions (map to C types).
  1970. map_op[".type_3"] = function(params, nparams)
  1971. if not params then
  1972. return nparams == 2 and "name, ctype" or "name, ctype, reg"
  1973. end
  1974. local name, ctype, reg = params[1], params[2], params[3]
  1975. if not match(name, "^[%a_][%w_]*$") then
  1976. werror("bad type name `"..name.."'")
  1977. end
  1978. local tp = map_type[name]
  1979. if tp then
  1980. werror("duplicate type `"..name.."'")
  1981. end
  1982. if reg and not map_reg_valid_base[reg] then
  1983. werror("bad base register `"..(map_reg_rev[reg] or reg).."'")
  1984. end
  1985. -- Add #type to defines. A bit unclean to put it in map_archdef.
  1986. map_archdef["#"..name] = "sizeof("..ctype..")"
  1987. -- Add new type and emit shortcut define.
  1988. local num = ctypenum + 1
  1989. map_type[name] = {
  1990. ctype = ctype,
  1991. ctypefmt = format("Dt%X(%%s)", num),
  1992. reg = reg,
  1993. }
  1994. wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
  1995. ctypenum = num
  1996. end
  1997. map_op[".type_2"] = map_op[".type_3"]
  1998. -- Dump type definitions.
  1999. local function dumptypes(out, lvl)
  2000. local t = {}
  2001. for name in pairs(map_type) do t[#t+1] = name end
  2002. sort(t)
  2003. out:write("Type definitions:\n")
  2004. for _,name in ipairs(t) do
  2005. local tp = map_type[name]
  2006. local reg = tp.reg and map_reg_rev[tp.reg] or ""
  2007. out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
  2008. end
  2009. out:write("\n")
  2010. end
  2011. ------------------------------------------------------------------------------
  2012. -- Set the current section.
  2013. function _M.section(num)
  2014. waction("SECTION")
  2015. wputxb(num)
  2016. wflush(true) -- SECTION is a terminal action.
  2017. end
  2018. ------------------------------------------------------------------------------
  2019. -- Dump architecture description.
  2020. function _M.dumparch(out)
  2021. out:write(format("DynASM %s version %s, released %s\n\n",
  2022. _info.arch, _info.version, _info.release))
  2023. dumpregs(out)
  2024. dumpactions(out)
  2025. end
  2026. -- Dump all user defined elements.
  2027. function _M.dumpdef(out, lvl)
  2028. dumptypes(out, lvl)
  2029. dumpglobals(out, lvl)
  2030. dumpexterns(out, lvl)
  2031. end
  2032. ------------------------------------------------------------------------------
  2033. -- Pass callbacks from/to the DynASM core.
  2034. function _M.passcb(wl, we, wf, ww)
  2035. wline, werror, wfatal, wwarn = wl, we, wf, ww
  2036. return wflush
  2037. end
  2038. -- Setup the arch-specific module.
  2039. function _M.setup(arch, opt)
  2040. g_arch, g_opt = arch, opt
  2041. end
  2042. -- Merge the core maps and the arch-specific maps.
  2043. function _M.mergemaps(map_coreop, map_def)
  2044. setmetatable(map_op, { __index = map_coreop })
  2045. setmetatable(map_def, { __index = map_archdef })
  2046. return map_op, map_def
  2047. end
  2048. return _M
  2049. ------------------------------------------------------------------------------