dasm_x86.lua 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. ------------------------------------------------------------------------------
  2. -- DynASM x86/x64 module.
  3. --
  4. -- Copyright (C) 2005-2010 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.2.1",
  13. vernum = 10201,
  14. release = "2010-01-09",
  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, 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 = table.concat, table.sort
  27. -- Inherited tables and callbacks.
  28. local g_opt, g_arch
  29. local wline, werror, wfatal, wwarn
  30. -- Action name list.
  31. -- CHECK: Keep this in sync with the C code!
  32. local action_names = {
  33. -- int arg, 1 buffer pos:
  34. "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB",
  35. -- action arg (1 byte), int arg, 1 buffer pos (reg/num):
  36. "VREG", "SPACE", -- !x64: VREG support NYI.
  37. -- ptrdiff_t arg, 1 buffer pos (address): !x64
  38. "SETLABEL", "REL_A",
  39. -- action arg (1 byte) or int arg, 2 buffer pos (link, offset):
  40. "REL_LG", "REL_PC",
  41. -- action arg (1 byte) or int arg, 1 buffer pos (link):
  42. "IMM_LG", "IMM_PC",
  43. -- action arg (1 byte) or int arg, 1 buffer pos (offset):
  44. "LABEL_LG", "LABEL_PC",
  45. -- action arg (1 byte), 1 buffer pos (offset):
  46. "ALIGN",
  47. -- action args (2 bytes), no buffer pos.
  48. "EXTERN",
  49. -- action arg (1 byte), no buffer pos.
  50. "ESC",
  51. -- no action arg, no buffer pos.
  52. "MARK",
  53. -- action arg (1 byte), no buffer pos, terminal action:
  54. "SECTION",
  55. -- no args, no buffer pos, terminal action:
  56. "STOP"
  57. }
  58. -- Maximum number of section buffer positions for dasm_put().
  59. -- CHECK: Keep this in sync with the C code!
  60. local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines.
  61. -- Action name -> action number (dynamically generated below).
  62. local map_action = {}
  63. -- First action number. Everything below does not need to be escaped.
  64. local actfirst = 256-#action_names
  65. -- Action list buffer and string (only used to remove dupes).
  66. local actlist = {}
  67. local actstr = ""
  68. -- Argument list for next dasm_put(). Start with offset 0 into action list.
  69. local actargs = { 0 }
  70. -- Current number of section buffer positions for dasm_put().
  71. local secpos = 1
  72. ------------------------------------------------------------------------------
  73. -- Compute action numbers for action names.
  74. for n,name in ipairs(action_names) do
  75. local num = actfirst + n - 1
  76. map_action[name] = num
  77. end
  78. -- Dump action names and numbers.
  79. local function dumpactions(out)
  80. out:write("DynASM encoding engine action codes:\n")
  81. for n,name in ipairs(action_names) do
  82. local num = map_action[name]
  83. out:write(format(" %-10s %02X %d\n", name, num, num))
  84. end
  85. out:write("\n")
  86. end
  87. -- Write action list buffer as a huge static C array.
  88. local function writeactions(out, name)
  89. local nn = #actlist
  90. local last = actlist[nn] or 255
  91. actlist[nn] = nil -- Remove last byte.
  92. if nn == 0 then nn = 1 end
  93. out:write("static const unsigned char ", name, "[", nn, "] = {\n")
  94. local s = " "
  95. for n,b in ipairs(actlist) do
  96. s = s..b..","
  97. if #s >= 75 then
  98. assert(out:write(s, "\n"))
  99. s = " "
  100. end
  101. end
  102. out:write(s, last, "\n};\n\n") -- Add last byte back.
  103. end
  104. ------------------------------------------------------------------------------
  105. -- Add byte to action list.
  106. local function wputxb(n)
  107. assert(n >= 0 and n <= 255 and n % 1 == 0, "byte out of range")
  108. actlist[#actlist+1] = n
  109. end
  110. -- Add action to list with optional arg. Advance buffer pos, too.
  111. local function waction(action, a, num)
  112. wputxb(assert(map_action[action], "bad action name `"..action.."'"))
  113. if a then actargs[#actargs+1] = a end
  114. if a or num then secpos = secpos + (num or 1) end
  115. end
  116. -- Add call to embedded DynASM C code.
  117. local function wcall(func, args)
  118. wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true)
  119. end
  120. -- Delete duplicate action list chunks. A tad slow, but so what.
  121. local function dedupechunk(offset)
  122. local al, as = actlist, actstr
  123. local chunk = char(unpack(al, offset+1, #al))
  124. local orig = find(as, chunk, 1, true)
  125. if orig then
  126. actargs[1] = orig-1 -- Replace with original offset.
  127. for i=offset+1,#al do al[i] = nil end -- Kill dupe.
  128. else
  129. actstr = as..chunk
  130. end
  131. end
  132. -- Flush action list (intervening C code or buffer pos overflow).
  133. local function wflush(term)
  134. local offset = actargs[1]
  135. if #actlist == offset then return end -- Nothing to flush.
  136. if not term then waction("STOP") end -- Terminate action list.
  137. dedupechunk(offset)
  138. wcall("put", actargs) -- Add call to dasm_put().
  139. actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put().
  140. secpos = 1 -- The actionlist offset occupies a buffer position, too.
  141. end
  142. -- Put escaped byte.
  143. local function wputb(n)
  144. if n >= actfirst then waction("ESC") end -- Need to escape byte.
  145. wputxb(n)
  146. end
  147. ------------------------------------------------------------------------------
  148. -- Global label name -> global label number. With auto assignment on 1st use.
  149. local next_global = 10
  150. local map_global = setmetatable({}, { __index = function(t, name)
  151. if not match(name, "^[%a_][%w_@]*$") then werror("bad global label") end
  152. local n = next_global
  153. if n > 246 then werror("too many global labels") end
  154. next_global = n + 1
  155. t[name] = n
  156. return n
  157. end})
  158. -- Dump global labels.
  159. local function dumpglobals(out, lvl)
  160. local t = {}
  161. for name, n in pairs(map_global) do t[n] = name end
  162. out:write("Global labels:\n")
  163. for i=10,next_global-1 do
  164. out:write(format(" %s\n", t[i]))
  165. end
  166. out:write("\n")
  167. end
  168. -- Write global label enum.
  169. local function writeglobals(out, prefix)
  170. local t = {}
  171. for name, n in pairs(map_global) do t[n] = name end
  172. out:write("enum {\n")
  173. for i=10,next_global-1 do
  174. out:write(" ", prefix, gsub(t[i], "@.*", ""), ",\n")
  175. end
  176. out:write(" ", prefix, "_MAX\n};\n")
  177. end
  178. -- Write global label names.
  179. local function writeglobalnames(out, name)
  180. local t = {}
  181. for name, n in pairs(map_global) do t[n] = name end
  182. out:write("static const char *const ", name, "[] = {\n")
  183. for i=10,next_global-1 do
  184. out:write(" \"", t[i], "\",\n")
  185. end
  186. out:write(" (const char *)0\n};\n")
  187. end
  188. ------------------------------------------------------------------------------
  189. -- Extern label name -> extern label number. With auto assignment on 1st use.
  190. local next_extern = -1
  191. local map_extern = setmetatable({}, { __index = function(t, name)
  192. -- No restrictions on the name for now.
  193. local n = next_extern
  194. if n < -256 then werror("too many extern labels") end
  195. next_extern = n - 1
  196. t[name] = n
  197. return n
  198. end})
  199. -- Dump extern labels.
  200. local function dumpexterns(out, lvl)
  201. local t = {}
  202. for name, n in pairs(map_extern) do t[-n] = name end
  203. out:write("Extern labels:\n")
  204. for i=1,-next_extern-1 do
  205. out:write(format(" %s\n", t[i]))
  206. end
  207. out:write("\n")
  208. end
  209. -- Write extern label names.
  210. local function writeexternnames(out, name)
  211. local t = {}
  212. for name, n in pairs(map_extern) do t[-n] = name end
  213. out:write("static const char *const ", name, "[] = {\n")
  214. for i=1,-next_extern-1 do
  215. out:write(" \"", t[i], "\",\n")
  216. end
  217. out:write(" (const char *)0\n};\n")
  218. end
  219. ------------------------------------------------------------------------------
  220. -- Arch-specific maps.
  221. local map_archdef = {} -- Ext. register name -> int. name.
  222. local map_reg_rev = {} -- Int. register name -> ext. name.
  223. local map_reg_num = {} -- Int. register name -> register number.
  224. local map_reg_opsize = {} -- Int. register name -> operand size.
  225. local map_reg_valid_base = {} -- Int. register name -> valid base register?
  226. local map_reg_valid_index = {} -- Int. register name -> valid index register?
  227. local map_reg_needrex = {} -- Int. register name -> need rex vs. no rex.
  228. local reg_list = {} -- Canonical list of int. register names.
  229. local map_type = {} -- Type name -> { ctype, reg }
  230. local ctypenum = 0 -- Type number (for _PTx macros).
  231. local addrsize = x64 and "q" or "d" -- Size for address operands.
  232. -- Helper functions to fill register maps.
  233. local function mkrmap(sz, cl, names)
  234. local cname = format("@%s", sz)
  235. reg_list[#reg_list+1] = cname
  236. map_archdef[cl] = cname
  237. map_reg_rev[cname] = cl
  238. map_reg_num[cname] = -1
  239. map_reg_opsize[cname] = sz
  240. if sz == addrsize or sz == "d" then
  241. map_reg_valid_base[cname] = true
  242. map_reg_valid_index[cname] = true
  243. end
  244. if names then
  245. for n,name in ipairs(names) do
  246. local iname = format("@%s%x", sz, n-1)
  247. reg_list[#reg_list+1] = iname
  248. map_archdef[name] = iname
  249. map_reg_rev[iname] = name
  250. map_reg_num[iname] = n-1
  251. map_reg_opsize[iname] = sz
  252. if sz == "b" and n > 4 then map_reg_needrex[iname] = false end
  253. if sz == addrsize or sz == "d" then
  254. map_reg_valid_base[iname] = true
  255. map_reg_valid_index[iname] = true
  256. end
  257. end
  258. end
  259. for i=0,(x64 and sz ~= "f") and 15 or 7 do
  260. local needrex = sz == "b" and i > 3
  261. local iname = format("@%s%x%s", sz, i, needrex and "R" or "")
  262. if needrex then map_reg_needrex[iname] = true end
  263. local name
  264. if sz == "o" then name = format("xmm%d", i)
  265. elseif sz == "f" then name = format("st%d", i)
  266. else name = format("r%d%s", i, sz == addrsize and "" or sz) end
  267. map_archdef[name] = iname
  268. if not map_reg_rev[iname] then
  269. reg_list[#reg_list+1] = iname
  270. map_reg_rev[iname] = name
  271. map_reg_num[iname] = i
  272. map_reg_opsize[iname] = sz
  273. if sz == addrsize or sz == "d" then
  274. map_reg_valid_base[iname] = true
  275. map_reg_valid_index[iname] = true
  276. end
  277. end
  278. end
  279. reg_list[#reg_list+1] = ""
  280. end
  281. -- Integer registers (qword, dword, word and byte sized).
  282. if x64 then
  283. mkrmap("q", "Rq", {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi"})
  284. end
  285. mkrmap("d", "Rd", {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"})
  286. mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"})
  287. mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"})
  288. map_reg_valid_index[map_archdef.esp] = false
  289. if x64 then map_reg_valid_index[map_archdef.rsp] = false end
  290. map_archdef["Ra"] = "@"..addrsize
  291. -- FP registers (internally tword sized, but use "f" as operand size).
  292. mkrmap("f", "Rf")
  293. -- SSE registers (oword sized, but qword and dword accessible).
  294. mkrmap("o", "xmm")
  295. -- Operand size prefixes to codes.
  296. local map_opsize = {
  297. byte = "b", word = "w", dword = "d", qword = "q", oword = "o", tword = "t",
  298. aword = addrsize,
  299. }
  300. -- Operand size code to number.
  301. local map_opsizenum = {
  302. b = 1, w = 2, d = 4, q = 8, o = 16, t = 10,
  303. }
  304. -- Operand size code to name.
  305. local map_opsizename = {
  306. b = "byte", w = "word", d = "dword", q = "qword", o = "oword", t = "tword",
  307. f = "fpword",
  308. }
  309. -- Valid index register scale factors.
  310. local map_xsc = {
  311. ["1"] = 0, ["2"] = 1, ["4"] = 2, ["8"] = 3,
  312. }
  313. -- Condition codes.
  314. local map_cc = {
  315. o = 0, no = 1, b = 2, nb = 3, e = 4, ne = 5, be = 6, nbe = 7,
  316. s = 8, ns = 9, p = 10, np = 11, l = 12, nl = 13, le = 14, nle = 15,
  317. c = 2, nae = 2, nc = 3, ae = 3, z = 4, nz = 5, na = 6, a = 7,
  318. pe = 10, po = 11, nge = 12, ge = 13, ng = 14, g = 15,
  319. }
  320. -- Reverse defines for registers.
  321. function _M.revdef(s)
  322. return gsub(s, "@%w+", map_reg_rev)
  323. end
  324. -- Dump register names and numbers
  325. local function dumpregs(out)
  326. out:write("Register names, sizes and internal numbers:\n")
  327. for _,reg in ipairs(reg_list) do
  328. if reg == "" then
  329. out:write("\n")
  330. else
  331. local name = map_reg_rev[reg]
  332. local num = map_reg_num[reg]
  333. local opsize = map_opsizename[map_reg_opsize[reg]]
  334. out:write(format(" %-5s %-8s %s\n", name, opsize,
  335. num < 0 and "(variable)" or num))
  336. end
  337. end
  338. end
  339. ------------------------------------------------------------------------------
  340. -- Put action for label arg (IMM_LG, IMM_PC, REL_LG, REL_PC).
  341. local function wputlabel(aprefix, imm, num)
  342. if type(imm) == "number" then
  343. if imm < 0 then
  344. waction("EXTERN")
  345. wputxb(aprefix == "IMM_" and 0 or 1)
  346. imm = -imm-1
  347. else
  348. waction(aprefix.."LG", nil, num);
  349. end
  350. wputxb(imm)
  351. else
  352. waction(aprefix.."PC", imm, num)
  353. end
  354. end
  355. -- Put signed byte or arg.
  356. local function wputsbarg(n)
  357. if type(n) == "number" then
  358. if n < -128 or n > 127 then
  359. werror("signed immediate byte out of range")
  360. end
  361. if n < 0 then n = n + 256 end
  362. wputb(n)
  363. else waction("IMM_S", n) end
  364. end
  365. -- Put unsigned byte or arg.
  366. local function wputbarg(n)
  367. if type(n) == "number" then
  368. if n < 0 or n > 255 then
  369. werror("unsigned immediate byte out of range")
  370. end
  371. wputb(n)
  372. else waction("IMM_B", n) end
  373. end
  374. -- Put unsigned word or arg.
  375. local function wputwarg(n)
  376. if type(n) == "number" then
  377. if n < 0 or n > 65535 then
  378. werror("unsigned immediate word out of range")
  379. end
  380. local r = n%256; n = (n-r)/256; wputb(r); wputb(n);
  381. else waction("IMM_W", n) end
  382. end
  383. -- Put signed or unsigned dword or arg.
  384. local function wputdarg(n)
  385. local tn = type(n)
  386. if tn == "number" then
  387. if n < 0 then n = n + 4294967296 end
  388. local r = n%256; n = (n-r)/256; wputb(r);
  389. r = n%256; n = (n-r)/256; wputb(r);
  390. r = n%256; n = (n-r)/256; wputb(r); wputb(n);
  391. elseif tn == "table" then
  392. wputlabel("IMM_", n[1], 1)
  393. else
  394. waction("IMM_D", n)
  395. end
  396. end
  397. -- Put operand-size dependent number or arg (defaults to dword).
  398. local function wputszarg(sz, n)
  399. if not sz or sz == "d" or sz == "q" then wputdarg(n)
  400. elseif sz == "w" then wputwarg(n)
  401. elseif sz == "b" then wputbarg(n)
  402. elseif sz == "s" then wputsbarg(n)
  403. else werror("bad operand size") end
  404. end
  405. -- Put multi-byte opcode with operand-size dependent modifications.
  406. local function wputop(sz, op, rex)
  407. local r
  408. if rex ~= 0 and not x64 then werror("bad operand size") end
  409. if sz == "w" then wputb(102) end
  410. -- Needs >32 bit numbers, but only for crc32 eax, word [ebx]
  411. if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end
  412. if op >= 16777216 then r = op % 16777216 wputb((op-r) / 16777216) op = r end
  413. if op >= 65536 then
  414. if rex ~= 0 then
  415. local opc3 = op - op % 256
  416. if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then
  417. wputb(64 + rex % 16); rex = 0
  418. end
  419. end
  420. r = op % 65536 wputb((op-r) / 65536) op = r
  421. end
  422. if op >= 256 then
  423. r = op % 256
  424. local b = (op-r) / 256
  425. if b == 15 and rex ~= 0 then wputb(64 + rex % 16); rex = 0 end
  426. wputb(b)
  427. op = r
  428. end
  429. if rex ~= 0 then wputb(64 + rex % 16) end
  430. if sz == "b" then op = op - 1 end
  431. wputb(op)
  432. end
  433. -- Put ModRM or SIB formatted byte.
  434. local function wputmodrm(m, s, rm, vs, vrm)
  435. assert(m < 4 and s < 16 and rm < 16, "bad modrm operands")
  436. wputb(64*m + 8*(s%8) + (rm%8))
  437. end
  438. -- Put ModRM/SIB plus optional displacement.
  439. local function wputmrmsib(t, imark, s, vsreg)
  440. local vreg, vxreg
  441. local reg, xreg = t.reg, t.xreg
  442. if reg and reg < 0 then reg = 0; vreg = t.vreg end
  443. if xreg and xreg < 0 then xreg = 0; vxreg = t.vxreg end
  444. if s < 0 then s = 0 end
  445. -- Register mode.
  446. if sub(t.mode, 1, 1) == "r" then
  447. wputmodrm(3, s, reg)
  448. if vsreg then waction("VREG", vsreg); wputxb(2) end
  449. if vreg then waction("VREG", vreg); wputxb(0) end
  450. return
  451. end
  452. local disp = t.disp
  453. local tdisp = type(disp)
  454. -- No base register?
  455. if not reg then
  456. local riprel = false
  457. if xreg then
  458. -- Indexed mode with index register only.
  459. -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp)
  460. wputmodrm(0, s, 4)
  461. if imark == "I" then waction("MARK") end
  462. if vsreg then waction("VREG", vsreg); wputxb(2) end
  463. wputmodrm(t.xsc, xreg, 5)
  464. if vxreg then waction("VREG", vxreg); wputxb(3) end
  465. else
  466. -- Pure 32 bit displacement.
  467. if x64 and tdisp ~= "table" then
  468. wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp)
  469. wputmodrm(0, 4, 5)
  470. else
  471. riprel = x64
  472. wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp)
  473. end
  474. if imark == "I" then waction("MARK") end
  475. if vsreg then waction("VREG", vsreg); wputxb(2) end
  476. end
  477. if riprel then -- Emit rip-relative displacement.
  478. if match("UWSiI", imark) then
  479. werror("NYI: rip-relative displacement followed by immediate")
  480. end
  481. -- The previous byte in the action buffer cannot be 0xe9 or 0x80-0x8f.
  482. wputlabel("REL_", disp[1], 2)
  483. else
  484. wputdarg(disp)
  485. end
  486. return
  487. end
  488. local m
  489. if tdisp == "number" then -- Check displacement size at assembly time.
  490. if disp == 0 and (reg%8) ~= 5 then -- [ebp] -> [ebp+0] (in SIB, too)
  491. if not vreg then m = 0 end -- Force DISP to allow [Rd(5)] -> [ebp+0]
  492. elseif disp >= -128 and disp <= 127 then m = 1
  493. else m = 2 end
  494. elseif tdisp == "table" then
  495. m = 2
  496. end
  497. -- Index register present or esp as base register: need SIB encoding.
  498. if xreg or (reg%8) == 4 then
  499. wputmodrm(m or 2, s, 4) -- ModRM.
  500. if m == nil or imark == "I" then waction("MARK") end
  501. if vsreg then waction("VREG", vsreg); wputxb(2) end
  502. wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB.
  503. if vxreg then waction("VREG", vxreg); wputxb(3) end
  504. if vreg then waction("VREG", vreg); wputxb(1) end
  505. else
  506. wputmodrm(m or 2, s, reg) -- ModRM.
  507. if (imark == "I" and (m == 1 or m == 2)) or
  508. (m == nil and (vsreg or vreg)) then waction("MARK") end
  509. if vsreg then waction("VREG", vsreg); wputxb(2) end
  510. if vreg then waction("VREG", vreg); wputxb(1) end
  511. end
  512. -- Put displacement.
  513. if m == 1 then wputsbarg(disp)
  514. elseif m == 2 then wputdarg(disp)
  515. elseif m == nil then waction("DISP", disp) end
  516. end
  517. ------------------------------------------------------------------------------
  518. -- Return human-readable operand mode string.
  519. local function opmodestr(op, args)
  520. local m = {}
  521. for i=1,#args do
  522. local a = args[i]
  523. m[#m+1] = sub(a.mode, 1, 1)..(a.opsize or "?")
  524. end
  525. return op.." "..concat(m, ",")
  526. end
  527. -- Convert number to valid integer or nil.
  528. local function toint(expr)
  529. local n = tonumber(expr)
  530. if n then
  531. if n % 1 ~= 0 or n < -2147483648 or n > 4294967295 then
  532. werror("bad integer number `"..expr.."'")
  533. end
  534. return n
  535. end
  536. end
  537. -- Parse immediate expression.
  538. local function immexpr(expr)
  539. -- &expr (pointer)
  540. if sub(expr, 1, 1) == "&" then
  541. return "iPJ", format("(ptrdiff_t)(%s)", sub(expr,2))
  542. end
  543. local prefix = sub(expr, 1, 2)
  544. -- =>expr (pc label reference)
  545. if prefix == "=>" then
  546. return "iJ", sub(expr, 3)
  547. end
  548. -- ->name (global label reference)
  549. if prefix == "->" then
  550. return "iJ", map_global[sub(expr, 3)]
  551. end
  552. -- [<>][1-9] (local label reference)
  553. local dir, lnum = match(expr, "^([<>])([1-9])$")
  554. if dir then -- Fwd: 247-255, Bkwd: 1-9.
  555. return "iJ", lnum + (dir == ">" and 246 or 0)
  556. end
  557. local extname = match(expr, "^extern%s+(%S+)$")
  558. if extname then
  559. return "iJ", map_extern[extname]
  560. end
  561. -- expr (interpreted as immediate)
  562. return "iI", expr
  563. end
  564. -- Parse displacement expression: +-num, +-expr, +-opsize*num
  565. local function dispexpr(expr)
  566. local disp = expr == "" and 0 or toint(expr)
  567. if disp then return disp end
  568. local c, dispt = match(expr, "^([+-])%s*(.+)$")
  569. if c == "+" then
  570. expr = dispt
  571. elseif not c then
  572. werror("bad displacement expression `"..expr.."'")
  573. end
  574. local opsize, tailops = match(dispt, "^(%w+)%s*%*%s*(.+)$")
  575. local ops, imm = map_opsize[opsize], toint(tailops)
  576. if ops and imm then
  577. if c == "-" then imm = -imm end
  578. return imm*map_opsizenum[ops]
  579. end
  580. local mode, iexpr = immexpr(dispt)
  581. if mode == "iJ" then
  582. if c == "-" then werror("cannot invert label reference") end
  583. return { iexpr }
  584. end
  585. return expr -- Need to return original signed expression.
  586. end
  587. -- Parse register or type expression.
  588. local function rtexpr(expr)
  589. if not expr then return end
  590. local tname, ovreg = match(expr, "^([%w_]+):(@[%w_]+)$")
  591. local tp = map_type[tname or expr]
  592. if tp then
  593. local reg = ovreg or tp.reg
  594. local rnum = map_reg_num[reg]
  595. if not rnum then
  596. werror("type `"..(tname or expr).."' needs a register override")
  597. end
  598. if not map_reg_valid_base[reg] then
  599. werror("bad base register override `"..(map_reg_rev[reg] or reg).."'")
  600. end
  601. return reg, rnum, tp
  602. end
  603. return expr, map_reg_num[expr]
  604. end
  605. -- Parse operand and return { mode, opsize, reg, xreg, xsc, disp, imm }.
  606. local function parseoperand(param)
  607. local t = {}
  608. local expr = param
  609. local opsize, tailops = match(param, "^(%w+)%s*(.+)$")
  610. if opsize then
  611. t.opsize = map_opsize[opsize]
  612. if t.opsize then expr = tailops end
  613. end
  614. local br = match(expr, "^%[%s*(.-)%s*%]$")
  615. repeat
  616. if br then
  617. t.mode = "xm"
  618. -- [disp]
  619. t.disp = toint(br)
  620. if t.disp then
  621. t.mode = x64 and "xm" or "xmO"
  622. break
  623. end
  624. -- [reg...]
  625. local tp
  626. local reg, tailr = match(br, "^([@%w_:]+)%s*(.*)$")
  627. reg, t.reg, tp = rtexpr(reg)
  628. if not t.reg then
  629. -- [expr]
  630. t.mode = x64 and "xm" or "xmO"
  631. t.disp = dispexpr("+"..br)
  632. break
  633. end
  634. if t.reg == -1 then
  635. t.vreg, tailr = match(tailr, "^(%b())(.*)$")
  636. if not t.vreg then werror("bad variable register expression") end
  637. end
  638. -- [xreg*xsc] or [xreg*xsc+-disp] or [xreg*xsc+-expr]
  639. local xsc, tailsc = match(tailr, "^%*%s*([1248])%s*(.*)$")
  640. if xsc then
  641. if not map_reg_valid_index[reg] then
  642. werror("bad index register `"..map_reg_rev[reg].."'")
  643. end
  644. t.xsc = map_xsc[xsc]
  645. t.xreg = t.reg
  646. t.vxreg = t.vreg
  647. t.reg = nil
  648. t.vreg = nil
  649. t.disp = dispexpr(tailsc)
  650. break
  651. end
  652. if not map_reg_valid_base[reg] then
  653. werror("bad base register `"..map_reg_rev[reg].."'")
  654. end
  655. -- [reg] or [reg+-disp]
  656. t.disp = toint(tailr) or (tailr == "" and 0)
  657. if t.disp then break end
  658. -- [reg+xreg...]
  659. local xreg, tailx = match(tailr, "^+%s*([@%w_:]+)%s*(.*)$")
  660. xreg, t.xreg, tp = rtexpr(xreg)
  661. if not t.xreg then
  662. -- [reg+-expr]
  663. t.disp = dispexpr(tailr)
  664. break
  665. end
  666. if not map_reg_valid_index[xreg] then
  667. werror("bad index register `"..map_reg_rev[xreg].."'")
  668. end
  669. if t.xreg == -1 then
  670. t.vxreg, tailx = match(tailx, "^(%b())(.*)$")
  671. if not t.vxreg then werror("bad variable register expression") end
  672. end
  673. -- [reg+xreg*xsc...]
  674. local xsc, tailsc = match(tailx, "^%*%s*([1248])%s*(.*)$")
  675. if xsc then
  676. t.xsc = map_xsc[xsc]
  677. tailx = tailsc
  678. end
  679. -- [...] or [...+-disp] or [...+-expr]
  680. t.disp = dispexpr(tailx)
  681. else
  682. -- imm or opsize*imm
  683. local imm = toint(expr)
  684. if not imm and sub(expr, 1, 1) == "*" and t.opsize then
  685. imm = toint(sub(expr, 2))
  686. if imm then
  687. imm = imm * map_opsizenum[t.opsize]
  688. t.opsize = nil
  689. end
  690. end
  691. if imm then
  692. if t.opsize then werror("bad operand size override") end
  693. local m = "i"
  694. if imm == 1 then m = m.."1" end
  695. if imm >= 4294967168 and imm <= 4294967295 then imm = imm-4294967296 end
  696. if imm >= -128 and imm <= 127 then m = m.."S" end
  697. t.imm = imm
  698. t.mode = m
  699. break
  700. end
  701. local tp
  702. local reg, tailr = match(expr, "^([@%w_:]+)%s*(.*)$")
  703. reg, t.reg, tp = rtexpr(reg)
  704. if t.reg then
  705. if t.reg == -1 then
  706. t.vreg, tailr = match(tailr, "^(%b())(.*)$")
  707. if not t.vreg then werror("bad variable register expression") end
  708. end
  709. -- reg
  710. if tailr == "" then
  711. if t.opsize then werror("bad operand size override") end
  712. t.opsize = map_reg_opsize[reg]
  713. if t.opsize == "f" then
  714. t.mode = t.reg == 0 and "fF" or "f"
  715. else
  716. if reg == "@w4" or (x64 and reg == "@d4") then
  717. wwarn("bad idea, try again with `"..(x64 and "rsp'" or "esp'"))
  718. end
  719. t.mode = t.reg == 0 and "rmR" or (reg == "@b1" and "rmC" or "rm")
  720. end
  721. t.needrex = map_reg_needrex[reg]
  722. break
  723. end
  724. -- type[idx], type[idx].field, type->field -> [reg+offset_expr]
  725. if not tp then werror("bad operand `"..param.."'") end
  726. t.mode = "xm"
  727. t.disp = format(tp.ctypefmt, tailr)
  728. else
  729. t.mode, t.imm = immexpr(expr)
  730. if sub(t.mode, -1) == "J" then
  731. if t.opsize and t.opsize ~= addrsize then
  732. werror("bad operand size override")
  733. end
  734. t.opsize = addrsize
  735. end
  736. end
  737. end
  738. until true
  739. return t
  740. end
  741. ------------------------------------------------------------------------------
  742. -- x86 Template String Description
  743. -- ===============================
  744. --
  745. -- Each template string is a list of [match:]pattern pairs,
  746. -- separated by "|". The first match wins. No match means a
  747. -- bad or unsupported combination of operand modes or sizes.
  748. --
  749. -- The match part and the ":" is omitted if the operation has
  750. -- no operands. Otherwise the first N characters are matched
  751. -- against the mode strings of each of the N operands.
  752. --
  753. -- The mode string for each operand type is (see parseoperand()):
  754. -- Integer register: "rm", +"R" for eax, ax, al, +"C" for cl
  755. -- FP register: "f", +"F" for st0
  756. -- Index operand: "xm", +"O" for [disp] (pure offset)
  757. -- Immediate: "i", +"S" for signed 8 bit, +"1" for 1,
  758. -- +"I" for arg, +"P" for pointer
  759. -- Any: +"J" for valid jump targets
  760. --
  761. -- So a match character "m" (mixed) matches both an integer register
  762. -- and an index operand (to be encoded with the ModRM/SIB scheme).
  763. -- But "r" matches only a register and "x" only an index operand
  764. -- (e.g. for FP memory access operations).
  765. --
  766. -- The operand size match string starts right after the mode match
  767. -- characters and ends before the ":". "dwb" or "qdwb" is assumed, if empty.
  768. -- The effective data size of the operation is matched against this list.
  769. --
  770. -- If only the regular "b", "w", "d", "q", "t" operand sizes are
  771. -- present, then all operands must be the same size. Unspecified sizes
  772. -- are ignored, but at least one operand must have a size or the pattern
  773. -- won't match (use the "byte", "word", "dword", "qword", "tword"
  774. -- operand size overrides. E.g.: mov dword [eax], 1).
  775. --
  776. -- If the list has a "1" or "2" prefix, the operand size is taken
  777. -- from the respective operand and any other operand sizes are ignored.
  778. -- If the list contains only ".", all operand sizes are ignored.
  779. -- If the list has a "/" prefix, the concatenated (mixed) operand sizes
  780. -- are compared to the match.
  781. --
  782. -- E.g. "rrdw" matches for either two dword registers or two word
  783. -- registers. "Fx2dq" matches an st0 operand plus an index operand
  784. -- pointing to a dword (float) or qword (double).
  785. --
  786. -- Every character after the ":" is part of the pattern string:
  787. -- Hex chars are accumulated to form the opcode (left to right).
  788. -- "n" disables the standard opcode mods
  789. -- (otherwise: -1 for "b", o16 prefix for "w", rex.w for "q")
  790. -- "X" Force REX.W.
  791. -- "r"/"R" adds the reg. number from the 1st/2nd operand to the opcode.
  792. -- "m"/"M" generates ModRM/SIB from the 1st/2nd operand.
  793. -- The spare 3 bits are either filled with the last hex digit or
  794. -- the result from a previous "r"/"R". The opcode is restored.
  795. --
  796. -- All of the following characters force a flush of the opcode:
  797. -- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand.
  798. -- "S" stores a signed 8 bit immediate from the last operand.
  799. -- "U" stores an unsigned 8 bit immediate from the last operand.
  800. -- "W" stores an unsigned 16 bit immediate from the last operand.
  801. -- "i" stores an operand sized immediate from the last operand.
  802. -- "I" dito, but generates an action code to optionally modify
  803. -- the opcode (+2) for a signed 8 bit immediate.
  804. -- "J" generates one of the REL action codes from the last operand.
  805. --
  806. ------------------------------------------------------------------------------
  807. -- Template strings for x86 instructions. Ordered by first opcode byte.
  808. -- Unimplemented opcodes (deliberate omissions) are marked with *.
  809. local map_op = {
  810. -- 00-05: add...
  811. -- 06: *push es
  812. -- 07: *pop es
  813. -- 08-0D: or...
  814. -- 0E: *push cs
  815. -- 0F: two byte opcode prefix
  816. -- 10-15: adc...
  817. -- 16: *push ss
  818. -- 17: *pop ss
  819. -- 18-1D: sbb...
  820. -- 1E: *push ds
  821. -- 1F: *pop ds
  822. -- 20-25: and...
  823. es_0 = "26",
  824. -- 27: *daa
  825. -- 28-2D: sub...
  826. cs_0 = "2E",
  827. -- 2F: *das
  828. -- 30-35: xor...
  829. ss_0 = "36",
  830. -- 37: *aaa
  831. -- 38-3D: cmp...
  832. ds_0 = "3E",
  833. -- 3F: *aas
  834. inc_1 = x64 and "m:FF0m" or "rdw:40r|m:FF0m",
  835. dec_1 = x64 and "m:FF1m" or "rdw:48r|m:FF1m",
  836. push_1 = (x64 and "rq:n50r|rw:50r|mq:nFF6m|mw:FF6m" or
  837. "rdw:50r|mdw:FF6m").."|S.:6AS|ib:n6Ai|i.:68i",
  838. pop_1 = x64 and "rq:n58r|rw:58r|mq:n8F0m|mw:8F0m" or "rdw:58r|mdw:8F0m",
  839. -- 60: *pusha, *pushad, *pushaw
  840. -- 61: *popa, *popad, *popaw
  841. -- 62: *bound rdw,x
  842. -- 63: x86: *arpl mw,rw
  843. movsxd_2 = x64 and "rm/qd:63rM",
  844. fs_0 = "64",
  845. gs_0 = "65",
  846. o16_0 = "66",
  847. a16_0 = not x64 and "67" or nil,
  848. a32_0 = x64 and "67",
  849. -- 68: push idw
  850. -- 69: imul rdw,mdw,idw
  851. -- 6A: push ib
  852. -- 6B: imul rdw,mdw,S
  853. -- 6C: *insb
  854. -- 6D: *insd, *insw
  855. -- 6E: *outsb
  856. -- 6F: *outsd, *outsw
  857. -- 70-7F: jcc lb
  858. -- 80: add... mb,i
  859. -- 81: add... mdw,i
  860. -- 82: *undefined
  861. -- 83: add... mdw,S
  862. test_2 = "mr:85Rm|rm:85rM|Ri:A9ri|mi:F70mi",
  863. -- 86: xchg rb,mb
  864. -- 87: xchg rdw,mdw
  865. -- 88: mov mb,r
  866. -- 89: mov mdw,r
  867. -- 8A: mov r,mb
  868. -- 8B: mov r,mdw
  869. -- 8C: *mov mdw,seg
  870. lea_2 = "rx1dq:8DrM",
  871. -- 8E: *mov seg,mdw
  872. -- 8F: pop mdw
  873. nop_0 = "90",
  874. xchg_2 = "Rrqdw:90R|rRqdw:90r|rm:87rM|mr:87Rm",
  875. cbw_0 = "6698",
  876. cwde_0 = "98",
  877. cdqe_0 = "4898",
  878. cwd_0 = "6699",
  879. cdq_0 = "99",
  880. cqo_0 = "4899",
  881. -- 9A: *call iw:idw
  882. wait_0 = "9B",
  883. fwait_0 = "9B",
  884. pushf_0 = "9C",
  885. pushfd_0 = not x64 and "9C",
  886. pushfq_0 = x64 and "9C",
  887. popf_0 = "9D",
  888. popfd_0 = not x64 and "9D",
  889. popfq_0 = x64 and "9D",
  890. sahf_0 = "9E",
  891. lahf_0 = "9F",
  892. mov_2 = "OR:A3o|RO:A1O|mr:89Rm|rm:8BrM|rib:nB0ri|ridw:B8ri|mi:C70mi",
  893. movsb_0 = "A4",
  894. movsw_0 = "66A5",
  895. movsd_0 = "A5",
  896. cmpsb_0 = "A6",
  897. cmpsw_0 = "66A7",
  898. cmpsd_0 = "A7",
  899. -- A8: test Rb,i
  900. -- A9: test Rdw,i
  901. stosb_0 = "AA",
  902. stosw_0 = "66AB",
  903. stosd_0 = "AB",
  904. lodsb_0 = "AC",
  905. lodsw_0 = "66AD",
  906. lodsd_0 = "AD",
  907. scasb_0 = "AE",
  908. scasw_0 = "66AF",
  909. scasd_0 = "AF",
  910. -- B0-B7: mov rb,i
  911. -- B8-BF: mov rdw,i
  912. -- C0: rol... mb,i
  913. -- C1: rol... mdw,i
  914. ret_1 = "i.:nC2W",
  915. ret_0 = "C3",
  916. -- C4: *les rdw,mq
  917. -- C5: *lds rdw,mq
  918. -- C6: mov mb,i
  919. -- C7: mov mdw,i
  920. -- C8: *enter iw,ib
  921. leave_0 = "C9",
  922. -- CA: *retf iw
  923. -- CB: *retf
  924. int3_0 = "CC",
  925. int_1 = "i.:nCDU",
  926. into_0 = "CE",
  927. -- CF: *iret
  928. -- D0: rol... mb,1
  929. -- D1: rol... mdw,1
  930. -- D2: rol... mb,cl
  931. -- D3: rol... mb,cl
  932. -- D4: *aam ib
  933. -- D5: *aad ib
  934. -- D6: *salc
  935. -- D7: *xlat
  936. -- D8-DF: floating point ops
  937. -- E0: *loopne
  938. -- E1: *loope
  939. -- E2: *loop
  940. -- E3: *jcxz, *jecxz
  941. -- E4: *in Rb,ib
  942. -- E5: *in Rdw,ib
  943. -- E6: *out ib,Rb
  944. -- E7: *out ib,Rdw
  945. call_1 = x64 and "mq:nFF2m|J.:E8nJ" or "md:FF2m|J.:E8J",
  946. jmp_1 = x64 and "mq:nFF4m|J.:E9nJ" or "md:FF4m|J.:E9J", -- short: EB
  947. -- EA: *jmp iw:idw
  948. -- EB: jmp ib
  949. -- EC: *in Rb,dx
  950. -- ED: *in Rdw,dx
  951. -- EE: *out dx,Rb
  952. -- EF: *out dx,Rdw
  953. -- F0: *lock
  954. int1_0 = "F1",
  955. repne_0 = "F2",
  956. repnz_0 = "F2",
  957. rep_0 = "F3",
  958. repe_0 = "F3",
  959. repz_0 = "F3",
  960. -- F4: *hlt
  961. cmc_0 = "F5",
  962. -- F6: test... mb,i; div... mb
  963. -- F7: test... mdw,i; div... mdw
  964. clc_0 = "F8",
  965. stc_0 = "F9",
  966. -- FA: *cli
  967. cld_0 = "FC",
  968. std_0 = "FD",
  969. -- FE: inc... mb
  970. -- FF: inc... mdw
  971. -- misc ops
  972. not_1 = "m:F72m",
  973. neg_1 = "m:F73m",
  974. mul_1 = "m:F74m",
  975. imul_1 = "m:F75m",
  976. div_1 = "m:F76m",
  977. idiv_1 = "m:F77m",
  978. imul_2 = "rmqdw:0FAFrM|rIqdw:69rmI|rSqdw:6BrmS|riqdw:69rmi",
  979. imul_3 = "rmIqdw:69rMI|rmSqdw:6BrMS|rmiqdw:69rMi",
  980. movzx_2 = "rm/db:0FB6rM|rm/qb:|rm/wb:0FB6rM|rm/dw:0FB7rM|rm/qw:",
  981. movsx_2 = "rm/db:0FBErM|rm/qb:|rm/wb:0FBErM|rm/dw:0FBFrM|rm/qw:",
  982. bswap_1 = "rqd:0FC8r",
  983. bsf_2 = "rmqdw:0FBCrM",
  984. bsr_2 = "rmqdw:0FBDrM",
  985. bt_2 = "mrqdw:0FA3Rm|miqdw:0FBA4mU",
  986. btc_2 = "mrqdw:0FBBRm|miqdw:0FBA7mU",
  987. btr_2 = "mrqdw:0FB3Rm|miqdw:0FBA6mU",
  988. bts_2 = "mrqdw:0FABRm|miqdw:0FBA5mU",
  989. rdtsc_0 = "0F31", -- P1+
  990. cpuid_0 = "0FA2", -- P1+
  991. -- floating point ops
  992. fst_1 = "ff:DDD0r|xd:D92m|xq:nDD2m",
  993. fstp_1 = "ff:DDD8r|xd:D93m|xq:nDD3m|xt:DB7m",
  994. fld_1 = "ff:D9C0r|xd:D90m|xq:nDD0m|xt:DB5m",
  995. fpop_0 = "DDD8", -- Alias for fstp st0.
  996. fist_1 = "xw:nDF2m|xd:DB2m",
  997. fistp_1 = "xw:nDF3m|xd:DB3m|xq:nDF7m",
  998. fild_1 = "xw:nDF0m|xd:DB0m|xq:nDF5m",
  999. fxch_0 = "D9C9",
  1000. fxch_1 = "ff:D9C8r",
  1001. fxch_2 = "fFf:D9C8r|Fff:D9C8R",
  1002. fucom_1 = "ff:DDE0r",
  1003. fucom_2 = "Fff:DDE0R",
  1004. fucomp_1 = "ff:DDE8r",
  1005. fucomp_2 = "Fff:DDE8R",
  1006. fucomi_1 = "ff:DBE8r", -- P6+
  1007. fucomi_2 = "Fff:DBE8R", -- P6+
  1008. fucomip_1 = "ff:DFE8r", -- P6+
  1009. fucomip_2 = "Fff:DFE8R", -- P6+
  1010. fcomi_1 = "ff:DBF0r", -- P6+
  1011. fcomi_2 = "Fff:DBF0R", -- P6+
  1012. fcomip_1 = "ff:DFF0r", -- P6+
  1013. fcomip_2 = "Fff:DFF0R", -- P6+
  1014. fucompp_0 = "DAE9",
  1015. fcompp_0 = "DED9",
  1016. fldcw_1 = "xw:nD95m",
  1017. fstcw_1 = "xw:n9BD97m",
  1018. fnstcw_1 = "xw:nD97m",
  1019. fstsw_1 = "Rw:n9BDFE0|xw:n9BDD7m",
  1020. fnstsw_1 = "Rw:nDFE0|xw:nDD7m",
  1021. fclex_0 = "9BDBE2",
  1022. fnclex_0 = "DBE2",
  1023. fnop_0 = "D9D0",
  1024. -- D9D1-D9DF: unassigned
  1025. fchs_0 = "D9E0",
  1026. fabs_0 = "D9E1",
  1027. -- D9E2: unassigned
  1028. -- D9E3: unassigned
  1029. ftst_0 = "D9E4",
  1030. fxam_0 = "D9E5",
  1031. -- D9E6: unassigned
  1032. -- D9E7: unassigned
  1033. fld1_0 = "D9E8",
  1034. fldl2t_0 = "D9E9",
  1035. fldl2e_0 = "D9EA",
  1036. fldpi_0 = "D9EB",
  1037. fldlg2_0 = "D9EC",
  1038. fldln2_0 = "D9ED",
  1039. fldz_0 = "D9EE",
  1040. -- D9EF: unassigned
  1041. f2xm1_0 = "D9F0",
  1042. fyl2x_0 = "D9F1",
  1043. fptan_0 = "D9F2",
  1044. fpatan_0 = "D9F3",
  1045. fxtract_0 = "D9F4",
  1046. fprem1_0 = "D9F5",
  1047. fdecstp_0 = "D9F6",
  1048. fincstp_0 = "D9F7",
  1049. fprem_0 = "D9F8",
  1050. fyl2xp1_0 = "D9F9",
  1051. fsqrt_0 = "D9FA",
  1052. fsincos_0 = "D9FB",
  1053. frndint_0 = "D9FC",
  1054. fscale_0 = "D9FD",
  1055. fsin_0 = "D9FE",
  1056. fcos_0 = "D9FF",
  1057. -- SSE, SSE2
  1058. andnpd_2 = "rmo:660F55rM",
  1059. andnps_2 = "rmo:0F55rM",
  1060. andpd_2 = "rmo:660F54rM",
  1061. andps_2 = "rmo:0F54rM",
  1062. clflush_1 = "x.:0FAE7m",
  1063. cmppd_3 = "rmio:660FC2rMU",
  1064. cmpps_3 = "rmio:0FC2rMU",
  1065. cmpsd_3 = "rrio:F20FC2rMU|rxi/oq:",
  1066. cmpss_3 = "rrio:F30FC2rMU|rxi/od:",
  1067. comisd_2 = "rro:660F2FrM|rx/oq:",
  1068. comiss_2 = "rro:0F2FrM|rx/od:",
  1069. cvtdq2pd_2 = "rro:F30FE6rM|rx/oq:",
  1070. cvtdq2ps_2 = "rmo:0F5BrM",
  1071. cvtpd2dq_2 = "rmo:F20FE6rM",
  1072. cvtpd2ps_2 = "rmo:660F5ArM",
  1073. cvtpi2pd_2 = "rx/oq:660F2ArM",
  1074. cvtpi2ps_2 = "rx/oq:0F2ArM",
  1075. cvtps2dq_2 = "rmo:660F5BrM",
  1076. cvtps2pd_2 = "rro:0F5ArM|rx/oq:",
  1077. cvtsd2si_2 = "rr/do:F20F2DrM|rr/qo:|rx/dq:|rxq:",
  1078. cvtsd2ss_2 = "rro:F20F5ArM|rx/oq:",
  1079. cvtsi2sd_2 = "rm/od:F20F2ArM|rm/oq:F20F2ArXM",
  1080. cvtsi2ss_2 = "rm/od:F30F2ArM|rm/oq:F30F2ArXM",
  1081. cvtss2sd_2 = "rro:F30F5ArM|rx/od:",
  1082. cvtss2si_2 = "rr/do:F20F2CrM|rr/qo:|rxd:|rx/qd:",
  1083. cvttpd2dq_2 = "rmo:660FE6rM",
  1084. cvttps2dq_2 = "rmo:F30F5BrM",
  1085. cvttsd2si_2 = "rr/do:F20F2CrM|rr/qo:|rx/dq:|rxq:",
  1086. cvttss2si_2 = "rr/do:F30F2CrM|rr/qo:|rxd:|rx/qd:",
  1087. ldmxcsr_1 = "xd:0FAE2m",
  1088. lfence_0 = "0FAEE8",
  1089. maskmovdqu_2 = "rro:660FF7rM",
  1090. mfence_0 = "0FAEF0",
  1091. movapd_2 = "rmo:660F28rM|mro:660F29Rm",
  1092. movaps_2 = "rmo:0F28rM|mro:0F29Rm",
  1093. movd_2 = "rm/od:660F6ErM|rm/oq:660F6ErXM|mr/do:660F7ERm|mr/qo:",
  1094. movdqa_2 = "rmo:660F6FrM|mro:660F7FRm",
  1095. movdqu_2 = "rmo:F30F6FrM|mro:F30F7FRm",
  1096. movhlps_2 = "rro:0F12rM",
  1097. movhpd_2 = "rx/oq:660F16rM|xr/qo:n660F17Rm",
  1098. movhps_2 = "rx/oq:0F16rM|xr/qo:n0F17Rm",
  1099. movlhps_2 = "rro:0F16rM",
  1100. movlpd_2 = "rx/oq:660F12rM|xr/qo:n660F13Rm",
  1101. movlps_2 = "rx/oq:0F12rM|xr/qo:n0F13Rm",
  1102. movmskpd_2 = "rr/do:660F50rM",
  1103. movmskps_2 = "rr/do:0F50rM",
  1104. movntdq_2 = "xro:660FE7Rm",
  1105. movnti_2 = "xrqd:0FC3Rm",
  1106. movntpd_2 = "xro:660F2BRm",
  1107. movntps_2 = "xro:0F2BRm",
  1108. movq_2 = "rro:F30F7ErM|rx/oq:|xr/qo:n660FD6Rm",
  1109. movsd_2 = "rro:F20F10rM|rx/oq:|xr/qo:nF20F11Rm",
  1110. movss_2 = "rro:F30F10rM|rx/od:|xr/do:F30F11Rm",
  1111. movupd_2 = "rmo:660F10rM|mro:660F11Rm",
  1112. movups_2 = "rmo:0F10rM|mro:0F11Rm",
  1113. orpd_2 = "rmo:660F56rM",
  1114. orps_2 = "rmo:0F56rM",
  1115. packssdw_2 = "rmo:660F6BrM",
  1116. packsswb_2 = "rmo:660F63rM",
  1117. packuswb_2 = "rmo:660F67rM",
  1118. paddb_2 = "rmo:660FFCrM",
  1119. paddd_2 = "rmo:660FFErM",
  1120. paddq_2 = "rmo:660FD4rM",
  1121. paddsb_2 = "rmo:660FECrM",
  1122. paddsw_2 = "rmo:660FEDrM",
  1123. paddusb_2 = "rmo:660FDCrM",
  1124. paddusw_2 = "rmo:660FDDrM",
  1125. paddw_2 = "rmo:660FFDrM",
  1126. pand_2 = "rmo:660FDBrM",
  1127. pandn_2 = "rmo:660FDFrM",
  1128. pause_0 = "F390",
  1129. pavgb_2 = "rmo:660FE0rM",
  1130. pavgw_2 = "rmo:660FE3rM",
  1131. pcmpeqb_2 = "rmo:660F74rM",
  1132. pcmpeqd_2 = "rmo:660F76rM",
  1133. pcmpeqw_2 = "rmo:660F75rM",
  1134. pcmpgtb_2 = "rmo:660F64rM",
  1135. pcmpgtd_2 = "rmo:660F66rM",
  1136. pcmpgtw_2 = "rmo:660F65rM",
  1137. pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nrMU", -- Mem op: SSE4.1 only.
  1138. pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:",
  1139. pmaddwd_2 = "rmo:660FF5rM",
  1140. pmaxsw_2 = "rmo:660FEErM",
  1141. pmaxub_2 = "rmo:660FDErM",
  1142. pminsw_2 = "rmo:660FEArM",
  1143. pminub_2 = "rmo:660FDArM",
  1144. pmovmskb_2 = "rr/do:660FD7rM",
  1145. pmulhuw_2 = "rmo:660FE4rM",
  1146. pmulhw_2 = "rmo:660FE5rM",
  1147. pmullw_2 = "rmo:660FD5rM",
  1148. pmuludq_2 = "rmo:660FF4rM",
  1149. por_2 = "rmo:660FEBrM",
  1150. prefetchnta_1 = "xb:n0F180m",
  1151. prefetcht0_1 = "xb:n0F181m",
  1152. prefetcht1_1 = "xb:n0F182m",
  1153. prefetcht2_1 = "xb:n0F183m",
  1154. psadbw_2 = "rmo:660FF6rM",
  1155. pshufd_3 = "rmio:660F70rMU",
  1156. pshufhw_3 = "rmio:F30F70rMU",
  1157. pshuflw_3 = "rmio:F20F70rMU",
  1158. pslld_2 = "rmo:660FF2rM|rio:660F726mU",
  1159. pslldq_2 = "rio:660F737mU",
  1160. psllq_2 = "rmo:660FF3rM|rio:660F736mU",
  1161. psllw_2 = "rmo:660FF1rM|rio:660F716mU",
  1162. psrad_2 = "rmo:660FE2rM|rio:660F724mU",
  1163. psraw_2 = "rmo:660FE1rM|rio:660F714mU",
  1164. psrld_2 = "rmo:660FD2rM|rio:660F722mU",
  1165. psrldq_2 = "rio:660F733mU",
  1166. psrlq_2 = "rmo:660FD3rM|rio:660F732mU",
  1167. psrlw_2 = "rmo:660FD1rM|rio:660F712mU",
  1168. psubb_2 = "rmo:660FF8rM",
  1169. psubd_2 = "rmo:660FFArM",
  1170. psubq_2 = "rmo:660FFBrM",
  1171. psubsb_2 = "rmo:660FE8rM",
  1172. psubsw_2 = "rmo:660FE9rM",
  1173. psubusb_2 = "rmo:660FD8rM",
  1174. psubusw_2 = "rmo:660FD9rM",
  1175. psubw_2 = "rmo:660FF9rM",
  1176. punpckhbw_2 = "rmo:660F68rM",
  1177. punpckhdq_2 = "rmo:660F6ArM",
  1178. punpckhqdq_2 = "rmo:660F6DrM",
  1179. punpckhwd_2 = "rmo:660F69rM",
  1180. punpcklbw_2 = "rmo:660F60rM",
  1181. punpckldq_2 = "rmo:660F62rM",
  1182. punpcklqdq_2 = "rmo:660F6CrM",
  1183. punpcklwd_2 = "rmo:660F61rM",
  1184. pxor_2 = "rmo:660FEFrM",
  1185. rcpps_2 = "rmo:0F53rM",
  1186. rcpss_2 = "rro:F30F53rM|rx/od:",
  1187. rsqrtps_2 = "rmo:0F52rM",
  1188. rsqrtss_2 = "rmo:F30F52rM",
  1189. sfence_0 = "0FAEF8",
  1190. shufpd_3 = "rmio:660FC6rMU",
  1191. shufps_3 = "rmio:0FC6rMU",
  1192. stmxcsr_1 = "xd:0FAE3m",
  1193. ucomisd_2 = "rro:660F2ErM|rx/oq:",
  1194. ucomiss_2 = "rro:0F2ErM|rx/od:",
  1195. unpckhpd_2 = "rmo:660F15rM",
  1196. unpckhps_2 = "rmo:0F15rM",
  1197. unpcklpd_2 = "rmo:660F14rM",
  1198. unpcklps_2 = "rmo:0F14rM",
  1199. xorpd_2 = "rmo:660F57rM",
  1200. xorps_2 = "rmo:0F57rM",
  1201. -- SSE3 ops
  1202. fisttp_1 = "xw:nDF1m|xd:DB1m|xq:nDD1m",
  1203. addsubpd_2 = "rmo:660FD0rM",
  1204. addsubps_2 = "rmo:F20FD0rM",
  1205. haddpd_2 = "rmo:660F7CrM",
  1206. haddps_2 = "rmo:F20F7CrM",
  1207. hsubpd_2 = "rmo:660F7DrM",
  1208. hsubps_2 = "rmo:F20F7DrM",
  1209. lddqu_2 = "rxo:F20FF0rM",
  1210. movddup_2 = "rmo:F20F12rM",
  1211. movshdup_2 = "rmo:F30F16rM",
  1212. movsldup_2 = "rmo:F30F12rM",
  1213. -- SSSE3 ops
  1214. pabsb_2 = "rmo:660F381CrM",
  1215. pabsd_2 = "rmo:660F381ErM",
  1216. pabsw_2 = "rmo:660F381DrM",
  1217. palignr_3 = "rmio:660F3A0FrMU",
  1218. phaddd_2 = "rmo:660F3802rM",
  1219. phaddsw_2 = "rmo:660F3803rM",
  1220. phaddw_2 = "rmo:660F3801rM",
  1221. phsubd_2 = "rmo:660F3806rM",
  1222. phsubsw_2 = "rmo:660F3807rM",
  1223. phsubw_2 = "rmo:660F3805rM",
  1224. pmaddubsw_2 = "rmo:660F3804rM",
  1225. pmulhrsw_2 = "rmo:660F380BrM",
  1226. pshufb_2 = "rmo:660F3800rM",
  1227. psignb_2 = "rmo:660F3808rM",
  1228. psignd_2 = "rmo:660F380ArM",
  1229. psignw_2 = "rmo:660F3809rM",
  1230. -- SSE4.1 ops
  1231. blendpd_3 = "rmio:660F3A0DrMU",
  1232. blendps_3 = "rmio:660F3A0CrMU",
  1233. blendvpd_3 = "rmRo:660F3815rM",
  1234. blendvps_3 = "rmRo:660F3814rM",
  1235. dppd_3 = "rmio:660F3A41rMU",
  1236. dpps_3 = "rmio:660F3A40rMU",
  1237. extractps_3 = "mri/do:660F3A17RmU|rri/qo:660F3A17RXmU",
  1238. insertps_3 = "rrio:660F3A41rMU|rxi/od:",
  1239. movntdqa_2 = "rmo:660F382ArM",
  1240. mpsadbw_3 = "rmio:660F3A42rMU",
  1241. packusdw_2 = "rmo:660F382BrM",
  1242. pblendvb_3 = "rmRo:660F3810rM",
  1243. pblendw_3 = "rmio:660F3A0ErMU",
  1244. pcmpeqq_2 = "rmo:660F3829rM",
  1245. pextrb_3 = "rri/do:660F3A14nRmU|rri/qo:|xri/bo:",
  1246. pextrd_3 = "mri/do:660F3A16RmU",
  1247. pextrq_3 = "mri/qo:660F3A16RmU",
  1248. -- pextrw is SSE2, mem operand is SSE4.1 only
  1249. phminposuw_2 = "rmo:660F3841rM",
  1250. pinsrb_3 = "rri/od:660F3A20nrMU|rxi/ob:",
  1251. pinsrd_3 = "rmi/od:660F3A22rMU",
  1252. pinsrq_3 = "rmi/oq:660F3A22rXMU",
  1253. pmaxsb_2 = "rmo:660F383CrM",
  1254. pmaxsd_2 = "rmo:660F383DrM",
  1255. pmaxud_2 = "rmo:660F383FrM",
  1256. pmaxuw_2 = "rmo:660F383ErM",
  1257. pminsb_2 = "rmo:660F3838rM",
  1258. pminsd_2 = "rmo:660F3839rM",
  1259. pminud_2 = "rmo:660F383BrM",
  1260. pminuw_2 = "rmo:660F383ArM",
  1261. pmovsxbd_2 = "rro:660F3821rM|rx/od:",
  1262. pmovsxbq_2 = "rro:660F3822rM|rx/ow:",
  1263. pmovsxbw_2 = "rro:660F3820rM|rx/oq:",
  1264. pmovsxdq_2 = "rro:660F3825rM|rx/oq:",
  1265. pmovsxwd_2 = "rro:660F3823rM|rx/oq:",
  1266. pmovsxwq_2 = "rro:660F3824rM|rx/od:",
  1267. pmovzxbd_2 = "rro:660F3831rM|rx/od:",
  1268. pmovzxbq_2 = "rro:660F3832rM|rx/ow:",
  1269. pmovzxbw_2 = "rro:660F3830rM|rx/oq:",
  1270. pmovzxdq_2 = "rro:660F3835rM|rx/oq:",
  1271. pmovzxwd_2 = "rro:660F3833rM|rx/oq:",
  1272. pmovzxwq_2 = "rro:660F3834rM|rx/od:",
  1273. pmuldq_2 = "rmo:660F3828rM",
  1274. pmulld_2 = "rmo:660F3840rM",
  1275. ptest_2 = "rmo:660F3817rM",
  1276. roundpd_3 = "rmio:660F3A09rMU",
  1277. roundps_3 = "rmio:660F3A08rMU",
  1278. roundsd_3 = "rrio:660F3A0BrMU|rxi/oq:",
  1279. roundss_3 = "rrio:660F3A0ArMU|rxi/od:",
  1280. -- SSE4.2 ops
  1281. crc32_2 = "rmqd:F20F38F1rM|rm/dw:66F20F38F1rM|rm/db:F20F38F0rM|rm/qb:",
  1282. pcmpestri_3 = "rmio:660F3A61rMU",
  1283. pcmpestrm_3 = "rmio:660F3A60rMU",
  1284. pcmpgtq_2 = "rmo:660F3837rM",
  1285. pcmpistri_3 = "rmio:660F3A63rMU",
  1286. pcmpistrm_3 = "rmio:660F3A62rMU",
  1287. popcnt_2 = "rmqdw:F30FB8rM",
  1288. -- SSE4a
  1289. extrq_2 = "rro:660F79rM",
  1290. extrq_3 = "riio:660F780mUU",
  1291. insertq_2 = "rro:F20F79rM",
  1292. insertq_4 = "rriio:F20F78rMUU",
  1293. lzcnt_2 = "rmqdw:F30FBDrM",
  1294. movntsd_2 = "xr/qo:nF20F2BRm",
  1295. movntss_2 = "xr/do:F30F2BRm",
  1296. -- popcnt is also in SSE4.2
  1297. }
  1298. ------------------------------------------------------------------------------
  1299. -- Arithmetic ops.
  1300. for name,n in pairs{ add = 0, ["or"] = 1, adc = 2, sbb = 3,
  1301. ["and"] = 4, sub = 5, xor = 6, cmp = 7 } do
  1302. local n8 = n * 8
  1303. map_op[name.."_2"] = format(
  1304. "mr:%02XRm|rm:%02XrM|mI1qdw:81%XmI|mS1qdw:83%XmS|Ri1qdwb:%02Xri|mi1qdwb:81%Xmi",
  1305. 1+n8, 3+n8, n, n, 5+n8, n)
  1306. end
  1307. -- Shift ops.
  1308. for name,n in pairs{ rol = 0, ror = 1, rcl = 2, rcr = 3,
  1309. shl = 4, shr = 5, sar = 7, sal = 4 } do
  1310. map_op[name.."_2"] = format("m1:D1%Xm|mC1qdwb:D3%Xm|mi:C1%XmU", n, n, n)
  1311. end
  1312. -- Conditional ops.
  1313. for cc,n in pairs(map_cc) do
  1314. map_op["j"..cc.."_1"] = format("J.:n0F8%XJ", n) -- short: 7%X
  1315. map_op["set"..cc.."_1"] = format("mb:n0F9%X2m", n)
  1316. map_op["cmov"..cc.."_2"] = format("rmqdw:0F4%XrM", n) -- P6+
  1317. end
  1318. -- FP arithmetic ops.
  1319. for name,n in pairs{ add = 0, mul = 1, com = 2, comp = 3,
  1320. sub = 4, subr = 5, div = 6, divr = 7 } do
  1321. local nc = 192 + n * 8
  1322. local nr = nc + (n < 4 and 0 or (n % 2 == 0 and 8 or -8))
  1323. local fn = "f"..name
  1324. map_op[fn.."_1"] = format("ff:D8%02Xr|xd:D8%Xm|xq:nDC%Xm", nc, n, n)
  1325. if n == 2 or n == 3 then
  1326. map_op[fn.."_2"] = format("Fff:D8%02XR|Fx2d:D8%XM|Fx2q:nDC%XM", nc, n, n)
  1327. else
  1328. map_op[fn.."_2"] = format("Fff:D8%02XR|fFf:DC%02Xr|Fx2d:D8%XM|Fx2q:nDC%XM", nc, nr, n, n)
  1329. map_op[fn.."p_1"] = format("ff:DE%02Xr", nr)
  1330. map_op[fn.."p_2"] = format("fFf:DE%02Xr", nr)
  1331. end
  1332. map_op["fi"..name.."_1"] = format("xd:DA%Xm|xw:nDE%Xm", n, n)
  1333. end
  1334. -- FP conditional moves.
  1335. for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do
  1336. local n4 = n % 4
  1337. local nc = 56000 + n4 * 8 + (n-n4) * 64
  1338. map_op["fcmov"..cc.."_1"] = format("ff:%04Xr", nc) -- P6+
  1339. map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+
  1340. end
  1341. -- SSE FP arithmetic ops.
  1342. for name,n in pairs{ sqrt = 1, add = 8, mul = 9,
  1343. sub = 12, min = 13, div = 14, max = 15 } do
  1344. map_op[name.."ps_2"] = format("rmo:0F5%XrM", n)
  1345. map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n)
  1346. map_op[name.."pd_2"] = format("rmo:660F5%XrM", n)
  1347. map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n)
  1348. end
  1349. ------------------------------------------------------------------------------
  1350. -- Process pattern string.
  1351. local function dopattern(pat, args, sz, op, needrex)
  1352. local digit, addin
  1353. local opcode = 0
  1354. local szov = sz
  1355. local narg = 1
  1356. local rex = 0
  1357. -- Limit number of section buffer positions used by a single dasm_put().
  1358. -- A single opcode needs a maximum of 5 positions.
  1359. if secpos+5 > maxsecpos then wflush() end
  1360. -- Process each character.
  1361. for c in gmatch(pat.."|", ".") do
  1362. if match(c, "%x") then -- Hex digit.
  1363. digit = byte(c) - 48
  1364. if digit > 48 then digit = digit - 39
  1365. elseif digit > 16 then digit = digit - 7 end
  1366. opcode = opcode*16 + digit
  1367. addin = nil
  1368. elseif c == "n" then -- Disable operand size mods for opcode.
  1369. szov = nil
  1370. elseif c == "X" then -- Force REX.W.
  1371. rex = 8
  1372. elseif c == "r" then -- Merge 1st operand regno. into opcode.
  1373. addin = args[1]; opcode = opcode + (addin.reg % 8)
  1374. if narg < 2 then narg = 2 end
  1375. elseif c == "R" then -- Merge 2nd operand regno. into opcode.
  1376. addin = args[2]; opcode = opcode + (addin.reg % 8)
  1377. narg = 3
  1378. elseif c == "m" or c == "M" then -- Encode ModRM/SIB.
  1379. local s
  1380. if addin then
  1381. s = addin.reg
  1382. opcode = opcode - (s%8) -- Undo regno opcode merge.
  1383. else
  1384. s = opcode % 16 -- Undo last digit.
  1385. opcode = (opcode - s) / 16
  1386. end
  1387. local nn = c == "m" and 1 or 2
  1388. local t = args[nn]
  1389. if narg <= nn then narg = nn + 1 end
  1390. if szov == "q" and rex == 0 then rex = rex + 8 end
  1391. if t.reg and t.reg > 7 then rex = rex + 1 end
  1392. if t.xreg and t.xreg > 7 then rex = rex + 2 end
  1393. if s > 7 then rex = rex + 4 end
  1394. if needrex then rex = rex + 16 end
  1395. wputop(szov, opcode, rex); opcode = nil
  1396. local imark = sub(pat, -1) -- Force a mark (ugly).
  1397. -- Put ModRM/SIB with regno/last digit as spare.
  1398. wputmrmsib(t, imark, s, addin and addin.vreg)
  1399. addin = nil
  1400. else
  1401. if opcode then -- Flush opcode.
  1402. if szov == "q" and rex == 0 then rex = rex + 8 end
  1403. if needrex then rex = rex + 16 end
  1404. if addin and addin.reg == -1 then
  1405. wputop(szov, opcode + 1, rex)
  1406. waction("VREG", addin.vreg); wputxb(0)
  1407. else
  1408. if addin and addin.reg > 7 then rex = rex + 1 end
  1409. wputop(szov, opcode, rex)
  1410. end
  1411. opcode = nil
  1412. end
  1413. if c == "|" then break end
  1414. if c == "o" then -- Offset (pure 32 bit displacement).
  1415. wputdarg(args[1].disp); if narg < 2 then narg = 2 end
  1416. elseif c == "O" then
  1417. wputdarg(args[2].disp); narg = 3
  1418. else
  1419. -- Anything else is an immediate operand.
  1420. local a = args[narg]
  1421. narg = narg + 1
  1422. local mode, imm = a.mode, a.imm
  1423. if mode == "iJ" and not match("iIJ", c) then
  1424. werror("bad operand size for label")
  1425. end
  1426. if c == "S" then
  1427. wputsbarg(imm)
  1428. elseif c == "U" then
  1429. wputbarg(imm)
  1430. elseif c == "W" then
  1431. wputwarg(imm)
  1432. elseif c == "i" or c == "I" then
  1433. if mode == "iJ" then
  1434. wputlabel("IMM_", imm, 1)
  1435. elseif mode == "iI" and c == "I" then
  1436. waction(sz == "w" and "IMM_WB" or "IMM_DB", imm)
  1437. else
  1438. wputszarg(sz, imm)
  1439. end
  1440. elseif c == "J" then
  1441. if mode == "iPJ" then
  1442. waction("REL_A", imm) -- !x64 (secpos)
  1443. else
  1444. wputlabel("REL_", imm, 2)
  1445. end
  1446. else
  1447. werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'")
  1448. end
  1449. end
  1450. end
  1451. end
  1452. end
  1453. ------------------------------------------------------------------------------
  1454. -- Mapping of operand modes to short names. Suppress output with '#'.
  1455. local map_modename = {
  1456. r = "reg", R = "eax", C = "cl", x = "mem", m = "mrm", i = "imm",
  1457. f = "stx", F = "st0", J = "lbl", ["1"] = "1",
  1458. I = "#", S = "#", O = "#",
  1459. }
  1460. -- Return a table/string showing all possible operand modes.
  1461. local function templatehelp(template, nparams)
  1462. if nparams == 0 then return "" end
  1463. local t = {}
  1464. for tm in gmatch(template, "[^%|]+") do
  1465. local s = map_modename[sub(tm, 1, 1)]
  1466. s = s..gsub(sub(tm, 2, nparams), ".", function(c)
  1467. return ", "..map_modename[c]
  1468. end)
  1469. if not match(s, "#") then t[#t+1] = s end
  1470. end
  1471. return t
  1472. end
  1473. -- Match operand modes against mode match part of template.
  1474. local function matchtm(tm, args)
  1475. for i=1,#args do
  1476. if not match(args[i].mode, sub(tm, i, i)) then return end
  1477. end
  1478. return true
  1479. end
  1480. -- Handle opcodes defined with template strings.
  1481. map_op[".template__"] = function(params, template, nparams)
  1482. if not params then return templatehelp(template, nparams) end
  1483. local args = {}
  1484. -- Zero-operand opcodes have no match part.
  1485. if #params == 0 then
  1486. dopattern(template, args, "d", params.op, nil)
  1487. return
  1488. end
  1489. -- Determine common operand size (coerce undefined size) or flag as mixed.
  1490. local sz, szmix, needrex
  1491. for i,p in ipairs(params) do
  1492. args[i] = parseoperand(p)
  1493. local nsz = args[i].opsize
  1494. if nsz then
  1495. if sz and sz ~= nsz then szmix = true else sz = nsz end
  1496. end
  1497. local nrex = args[i].needrex
  1498. if nrex ~= nil then
  1499. if needrex == nil then
  1500. needrex = nrex
  1501. elseif needrex ~= nrex then
  1502. werror("bad mix of byte-addressable registers")
  1503. end
  1504. end
  1505. end
  1506. -- Try all match:pattern pairs (separated by '|').
  1507. local gotmatch, lastpat
  1508. for tm in gmatch(template, "[^%|]+") do
  1509. -- Split off size match (starts after mode match) and pattern string.
  1510. local szm, pat = match(tm, "^(.-):(.*)$", #args+1)
  1511. if pat == "" then pat = lastpat else lastpat = pat end
  1512. if matchtm(tm, args) then
  1513. local prefix = sub(szm, 1, 1)
  1514. if prefix == "/" then -- Match both operand sizes.
  1515. if args[1].opsize == sub(szm, 2, 2) and
  1516. args[2].opsize == sub(szm, 3, 3) then
  1517. dopattern(pat, args, sz, params.op, needrex) -- Process pattern.
  1518. return
  1519. end
  1520. else -- Match common operand size.
  1521. local szp = sz
  1522. if szm == "" then szm = x64 and "qdwb" or "dwb" end -- Default sizes.
  1523. if prefix == "1" then szp = args[1].opsize; szmix = nil
  1524. elseif prefix == "2" then szp = args[2].opsize; szmix = nil end
  1525. if not szmix and (prefix == "." or match(szm, szp or "#")) then
  1526. dopattern(pat, args, szp, params.op, needrex) -- Process pattern.
  1527. return
  1528. end
  1529. end
  1530. gotmatch = true
  1531. end
  1532. end
  1533. local msg = "bad operand mode"
  1534. if gotmatch then
  1535. if szmix then
  1536. msg = "mixed operand size"
  1537. else
  1538. msg = sz and "bad operand size" or "missing operand size"
  1539. end
  1540. end
  1541. werror(msg.." in `"..opmodestr(params.op, args).."'")
  1542. end
  1543. ------------------------------------------------------------------------------
  1544. -- x64-specific opcode for 64 bit immediates and displacements.
  1545. if x64 then
  1546. function map_op.mov64_2(params)
  1547. if not params then return { "reg, imm", "reg, [disp]", "[disp], reg" } end
  1548. if secpos+2 > maxsecpos then wflush() end
  1549. local opcode, op64, sz, rex
  1550. local op64 = match(params[1], "^%[%s*(.-)%s*%]$")
  1551. if op64 then
  1552. local a = parseoperand(params[2])
  1553. if a.mode ~= "rmR" then werror("bad operand mode") end
  1554. sz = a.opsize
  1555. rex = sz == "q" and 8 or 0
  1556. opcode = 0xa3
  1557. else
  1558. op64 = match(params[2], "^%[%s*(.-)%s*%]$")
  1559. local a = parseoperand(params[1])
  1560. if op64 then
  1561. if a.mode ~= "rmR" then werror("bad operand mode") end
  1562. sz = a.opsize
  1563. rex = sz == "q" and 8 or 0
  1564. opcode = 0xa1
  1565. else
  1566. if sub(a.mode, 1, 1) ~= "r" or a.opsize ~= "q" then
  1567. werror("bad operand mode")
  1568. end
  1569. op64 = params[2]
  1570. opcode = 0xb8 + (a.reg%8) -- !x64: no VREG support.
  1571. rex = a.reg > 7 and 9 or 8
  1572. end
  1573. end
  1574. wputop(sz, opcode, rex)
  1575. waction("IMM_D", format("(unsigned int)(%s)", op64))
  1576. waction("IMM_D", format("(unsigned int)((%s)>>32)", op64))
  1577. end
  1578. end
  1579. ------------------------------------------------------------------------------
  1580. -- Pseudo-opcodes for data storage.
  1581. local function op_data(params)
  1582. if not params then return "imm..." end
  1583. local sz = sub(params.op, 2, 2)
  1584. if sz == "a" then sz = addrsize end
  1585. for _,p in ipairs(params) do
  1586. local a = parseoperand(p)
  1587. if sub(a.mode, 1, 1) ~= "i" or (a.opsize and a.opsize ~= sz) then
  1588. werror("bad mode or size in `"..p.."'")
  1589. end
  1590. if a.mode == "iJ" then
  1591. wputlabel("IMM_", a.imm, 1)
  1592. else
  1593. wputszarg(sz, a.imm)
  1594. end
  1595. if secpos+2 > maxsecpos then wflush() end
  1596. end
  1597. end
  1598. map_op[".byte_*"] = op_data
  1599. map_op[".sbyte_*"] = op_data
  1600. map_op[".word_*"] = op_data
  1601. map_op[".dword_*"] = op_data
  1602. map_op[".aword_*"] = op_data
  1603. ------------------------------------------------------------------------------
  1604. -- Pseudo-opcode to mark the position where the action list is to be emitted.
  1605. map_op[".actionlist_1"] = function(params)
  1606. if not params then return "cvar" end
  1607. local name = params[1] -- No syntax check. You get to keep the pieces.
  1608. wline(function(out) writeactions(out, name) end)
  1609. end
  1610. -- Pseudo-opcode to mark the position where the global enum is to be emitted.
  1611. map_op[".globals_1"] = function(params)
  1612. if not params then return "prefix" end
  1613. local prefix = params[1] -- No syntax check. You get to keep the pieces.
  1614. wline(function(out) writeglobals(out, prefix) end)
  1615. end
  1616. -- Pseudo-opcode to mark the position where the global names are to be emitted.
  1617. map_op[".globalnames_1"] = function(params)
  1618. if not params then return "cvar" end
  1619. local name = params[1] -- No syntax check. You get to keep the pieces.
  1620. wline(function(out) writeglobalnames(out, name) end)
  1621. end
  1622. -- Pseudo-opcode to mark the position where the extern names are to be emitted.
  1623. map_op[".externnames_1"] = function(params)
  1624. if not params then return "cvar" end
  1625. local name = params[1] -- No syntax check. You get to keep the pieces.
  1626. wline(function(out) writeexternnames(out, name) end)
  1627. end
  1628. ------------------------------------------------------------------------------
  1629. -- Label pseudo-opcode (converted from trailing colon form).
  1630. map_op[".label_2"] = function(params)
  1631. if not params then return "[1-9] | ->global | =>pcexpr [, addr]" end
  1632. if secpos+2 > maxsecpos then wflush() end
  1633. local a = parseoperand(params[1])
  1634. local mode, imm = a.mode, a.imm
  1635. if type(imm) == "number" and (mode == "iJ" or (imm >= 1 and imm <= 9)) then
  1636. -- Local label (1: ... 9:) or global label (->global:).
  1637. waction("LABEL_LG", nil, 1)
  1638. wputxb(imm)
  1639. elseif mode == "iJ" then
  1640. -- PC label (=>pcexpr:).
  1641. waction("LABEL_PC", imm)
  1642. else
  1643. werror("bad label definition")
  1644. end
  1645. -- SETLABEL must immediately follow LABEL_LG/LABEL_PC.
  1646. local addr = params[2]
  1647. if addr then
  1648. local a = parseoperand(addr)
  1649. if a.mode == "iPJ" then
  1650. waction("SETLABEL", a.imm)
  1651. else
  1652. werror("bad label assignment")
  1653. end
  1654. end
  1655. end
  1656. map_op[".label_1"] = map_op[".label_2"]
  1657. ------------------------------------------------------------------------------
  1658. -- Alignment pseudo-opcode.
  1659. map_op[".align_1"] = function(params)
  1660. if not params then return "numpow2" end
  1661. if secpos+1 > maxsecpos then wflush() end
  1662. local align = tonumber(params[1]) or map_opsizenum[map_opsize[params[1]]]
  1663. if align then
  1664. local x = align
  1665. -- Must be a power of 2 in the range (2 ... 256).
  1666. for i=1,8 do
  1667. x = x / 2
  1668. if x == 1 then
  1669. waction("ALIGN", nil, 1)
  1670. wputxb(align-1) -- Action byte is 2**n-1.
  1671. return
  1672. end
  1673. end
  1674. end
  1675. werror("bad alignment")
  1676. end
  1677. -- Spacing pseudo-opcode.
  1678. map_op[".space_2"] = function(params)
  1679. if not params then return "num [, filler]" end
  1680. if secpos+1 > maxsecpos then wflush() end
  1681. waction("SPACE", params[1])
  1682. local fill = params[2]
  1683. if fill then
  1684. fill = tonumber(fill)
  1685. if not fill or fill < 0 or fill > 255 then werror("bad filler") end
  1686. end
  1687. wputxb(fill or 0)
  1688. end
  1689. map_op[".space_1"] = map_op[".space_2"]
  1690. ------------------------------------------------------------------------------
  1691. -- Pseudo-opcode for (primitive) type definitions (map to C types).
  1692. map_op[".type_3"] = function(params, nparams)
  1693. if not params then
  1694. return nparams == 2 and "name, ctype" or "name, ctype, reg"
  1695. end
  1696. local name, ctype, reg = params[1], params[2], params[3]
  1697. if not match(name, "^[%a_][%w_]*$") then
  1698. werror("bad type name `"..name.."'")
  1699. end
  1700. local tp = map_type[name]
  1701. if tp then
  1702. werror("duplicate type `"..name.."'")
  1703. end
  1704. if reg and not map_reg_valid_base[reg] then
  1705. werror("bad base register `"..(map_reg_rev[reg] or reg).."'")
  1706. end
  1707. -- Add #type to defines. A bit unclean to put it in map_archdef.
  1708. map_archdef["#"..name] = "sizeof("..ctype..")"
  1709. -- Add new type and emit shortcut define.
  1710. local num = ctypenum + 1
  1711. map_type[name] = {
  1712. ctype = ctype,
  1713. ctypefmt = format("Dt%X(%%s)", num),
  1714. reg = reg,
  1715. }
  1716. wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
  1717. ctypenum = num
  1718. end
  1719. map_op[".type_2"] = map_op[".type_3"]
  1720. -- Dump type definitions.
  1721. local function dumptypes(out, lvl)
  1722. local t = {}
  1723. for name in pairs(map_type) do t[#t+1] = name end
  1724. sort(t)
  1725. out:write("Type definitions:\n")
  1726. for _,name in ipairs(t) do
  1727. local tp = map_type[name]
  1728. local reg = tp.reg and map_reg_rev[tp.reg] or ""
  1729. out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
  1730. end
  1731. out:write("\n")
  1732. end
  1733. ------------------------------------------------------------------------------
  1734. -- Set the current section.
  1735. function _M.section(num)
  1736. waction("SECTION")
  1737. wputxb(num)
  1738. wflush(true) -- SECTION is a terminal action.
  1739. end
  1740. ------------------------------------------------------------------------------
  1741. -- Dump architecture description.
  1742. function _M.dumparch(out)
  1743. out:write(format("DynASM %s version %s, released %s\n\n",
  1744. _info.arch, _info.version, _info.release))
  1745. dumpregs(out)
  1746. dumpactions(out)
  1747. end
  1748. -- Dump all user defined elements.
  1749. function _M.dumpdef(out, lvl)
  1750. dumptypes(out, lvl)
  1751. dumpglobals(out, lvl)
  1752. dumpexterns(out, lvl)
  1753. end
  1754. ------------------------------------------------------------------------------
  1755. -- Pass callbacks from/to the DynASM core.
  1756. function _M.passcb(wl, we, wf, ww)
  1757. wline, werror, wfatal, wwarn = wl, we, wf, ww
  1758. return wflush
  1759. end
  1760. -- Setup the arch-specific module.
  1761. function _M.setup(arch, opt)
  1762. g_arch, g_opt = arch, opt
  1763. end
  1764. -- Merge the core maps and the arch-specific maps.
  1765. function _M.mergemaps(map_coreop, map_def)
  1766. setmetatable(map_op, { __index = map_coreop })
  1767. setmetatable(map_def, { __index = map_archdef })
  1768. return map_op, map_def
  1769. end
  1770. return _M
  1771. ------------------------------------------------------------------------------