dasm_x86.lua 70 KB

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