dasm_arm64.lua 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. ------------------------------------------------------------------------------
  2. -- DynASM ARM64 module.
  3. --
  4. -- Copyright (C) 2005-2016 Mike Pall. All rights reserved.
  5. -- See dynasm.lua for full copyright notice.
  6. ------------------------------------------------------------------------------
  7. -- Module information:
  8. local _info = {
  9. arch = "arm",
  10. description = "DynASM ARM64 module",
  11. version = "1.4.0",
  12. vernum = 10400,
  13. release = "2015-10-18",
  14. author = "Mike Pall",
  15. license = "MIT",
  16. }
  17. -- Exported glue functions for the arch-specific module.
  18. local _M = { _info = _info }
  19. -- Cache library functions.
  20. local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs
  21. local assert, setmetatable, rawget = assert, setmetatable, rawget
  22. local _s = string
  23. local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
  24. local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub
  25. local concat, sort, insert = table.concat, table.sort, table.insert
  26. local bit = bit or require("bit")
  27. local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift
  28. local ror, tohex = bit.ror, bit.tohex
  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. "STOP", "SECTION", "ESC", "REL_EXT",
  36. "ALIGN", "REL_LG", "LABEL_LG",
  37. "REL_PC", "LABEL_PC", "IMM", "IMM6", "IMM12", "IMM13W", "IMM13X", "IMML",
  38. }
  39. -- Maximum number of section buffer positions for dasm_put().
  40. -- CHECK: Keep this in sync with the C code!
  41. local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines.
  42. -- Action name -> action number.
  43. local map_action = {}
  44. for n,name in ipairs(action_names) do
  45. map_action[name] = n-1
  46. end
  47. -- Action list buffer.
  48. local actlist = {}
  49. -- Argument list for next dasm_put(). Start with offset 0 into action list.
  50. local actargs = { 0 }
  51. -- Current number of section buffer positions for dasm_put().
  52. local secpos = 1
  53. ------------------------------------------------------------------------------
  54. -- Dump action names and numbers.
  55. local function dumpactions(out)
  56. out:write("DynASM encoding engine action codes:\n")
  57. for n,name in ipairs(action_names) do
  58. local num = map_action[name]
  59. out:write(format(" %-10s %02X %d\n", name, num, num))
  60. end
  61. out:write("\n")
  62. end
  63. -- Write action list buffer as a huge static C array.
  64. local function writeactions(out, name)
  65. local nn = #actlist
  66. if nn == 0 then nn = 1; actlist[0] = map_action.STOP end
  67. out:write("static const unsigned int ", name, "[", nn, "] = {\n")
  68. for i = 1,nn-1 do
  69. assert(out:write("0x", tohex(actlist[i]), ",\n"))
  70. end
  71. assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n"))
  72. end
  73. ------------------------------------------------------------------------------
  74. -- Add word to action list.
  75. local function wputxw(n)
  76. assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
  77. actlist[#actlist+1] = n
  78. end
  79. -- Add action to list with optional arg. Advance buffer pos, too.
  80. local function waction(action, val, a, num)
  81. local w = assert(map_action[action], "bad action name `"..action.."'")
  82. wputxw(w * 0x10000 + (val or 0))
  83. if a then actargs[#actargs+1] = a end
  84. if a or num then secpos = secpos + (num or 1) end
  85. end
  86. -- Flush action list (intervening C code or buffer pos overflow).
  87. local function wflush(term)
  88. if #actlist == actargs[1] then return end -- Nothing to flush.
  89. if not term then waction("STOP") end -- Terminate action list.
  90. wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true)
  91. actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put().
  92. secpos = 1 -- The actionlist offset occupies a buffer position, too.
  93. end
  94. -- Put escaped word.
  95. local function wputw(n)
  96. if n <= 0x000fffff then waction("ESC") end
  97. wputxw(n)
  98. end
  99. -- Reserve position for word.
  100. local function wpos()
  101. local pos = #actlist+1
  102. actlist[pos] = ""
  103. return pos
  104. end
  105. -- Store word to reserved position.
  106. local function wputpos(pos, n)
  107. assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
  108. if n <= 0x000fffff then
  109. insert(actlist, pos+1, n)
  110. n = map_action.ESC * 0x10000
  111. end
  112. actlist[pos] = n
  113. end
  114. ------------------------------------------------------------------------------
  115. -- Global label name -> global label number. With auto assignment on 1st use.
  116. local next_global = 20
  117. local map_global = setmetatable({}, { __index = function(t, name)
  118. if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end
  119. local n = next_global
  120. if n > 2047 then werror("too many global labels") end
  121. next_global = n + 1
  122. t[name] = n
  123. return n
  124. end})
  125. -- Dump global labels.
  126. local function dumpglobals(out, lvl)
  127. local t = {}
  128. for name, n in pairs(map_global) do t[n] = name end
  129. out:write("Global labels:\n")
  130. for i=20,next_global-1 do
  131. out:write(format(" %s\n", t[i]))
  132. end
  133. out:write("\n")
  134. end
  135. -- Write global label enum.
  136. local function writeglobals(out, prefix)
  137. local t = {}
  138. for name, n in pairs(map_global) do t[n] = name end
  139. out:write("enum {\n")
  140. for i=20,next_global-1 do
  141. out:write(" ", prefix, t[i], ",\n")
  142. end
  143. out:write(" ", prefix, "_MAX\n};\n")
  144. end
  145. -- Write global label names.
  146. local function writeglobalnames(out, name)
  147. local t = {}
  148. for name, n in pairs(map_global) do t[n] = name end
  149. out:write("static const char *const ", name, "[] = {\n")
  150. for i=20,next_global-1 do
  151. out:write(" \"", t[i], "\",\n")
  152. end
  153. out:write(" (const char *)0\n};\n")
  154. end
  155. ------------------------------------------------------------------------------
  156. -- Extern label name -> extern label number. With auto assignment on 1st use.
  157. local next_extern = 0
  158. local map_extern_ = {}
  159. local map_extern = setmetatable({}, { __index = function(t, name)
  160. -- No restrictions on the name for now.
  161. local n = next_extern
  162. if n > 2047 then werror("too many extern labels") end
  163. next_extern = n + 1
  164. t[name] = n
  165. map_extern_[n] = name
  166. return n
  167. end})
  168. -- Dump extern labels.
  169. local function dumpexterns(out, lvl)
  170. out:write("Extern labels:\n")
  171. for i=0,next_extern-1 do
  172. out:write(format(" %s\n", map_extern_[i]))
  173. end
  174. out:write("\n")
  175. end
  176. -- Write extern label names.
  177. local function writeexternnames(out, name)
  178. out:write("static const char *const ", name, "[] = {\n")
  179. for i=0,next_extern-1 do
  180. out:write(" \"", map_extern_[i], "\",\n")
  181. end
  182. out:write(" (const char *)0\n};\n")
  183. end
  184. ------------------------------------------------------------------------------
  185. -- Arch-specific maps.
  186. -- Ext. register name -> int. name.
  187. local map_archdef = { xzr = "@x31", wzr = "@w31", lr = "x30", }
  188. -- Int. register name -> ext. name.
  189. local map_reg_rev = { ["@x31"] = "xzr", ["@w31"] = "wzr", x30 = "lr", }
  190. local map_type = {} -- Type name -> { ctype, reg }
  191. local ctypenum = 0 -- Type number (for Dt... macros).
  192. -- Reverse defines for registers.
  193. function _M.revdef(s)
  194. return map_reg_rev[s] or s
  195. end
  196. local map_shift = { lsl = 0, lsr = 1, asr = 2, }
  197. local map_extend = {
  198. uxtb = 0, uxth = 1, uxtw = 2, uxtx = 3,
  199. sxtb = 4, sxth = 5, sxtw = 6, sxtx = 7,
  200. }
  201. local map_cond = {
  202. eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7,
  203. hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14,
  204. hs = 2, lo = 3,
  205. }
  206. ------------------------------------------------------------------------------
  207. local parse_reg_type
  208. local function parse_reg(expr)
  209. if not expr then werror("expected register name") end
  210. local tname, ovreg = match(expr, "^([%w_]+):(@?%l%d+)$")
  211. local tp = map_type[tname or expr]
  212. if tp then
  213. local reg = ovreg or tp.reg
  214. if not reg then
  215. werror("type `"..(tname or expr).."' needs a register override")
  216. end
  217. expr = reg
  218. end
  219. local ok31, rt, r = match(expr, "^(@?)([xwqdshb])([123]?[0-9])$")
  220. if r then
  221. r = tonumber(r)
  222. if r <= 30 or (r == 31 and ok31 ~= "" or (rt ~= "w" and rt ~= "x")) then
  223. if not parse_reg_type then
  224. parse_reg_type = rt
  225. elseif parse_reg_type ~= rt then
  226. werror("register size mismatch")
  227. end
  228. return r, tp
  229. end
  230. end
  231. werror("bad register name `"..expr.."'")
  232. end
  233. local function parse_reg_base(expr)
  234. if expr == "sp" then return 0x3e0 end
  235. local base, tp = parse_reg(expr)
  236. if parse_reg_type ~= "x" then werror("bad register type") end
  237. parse_reg_type = false
  238. return shl(base, 5), tp
  239. end
  240. local parse_ctx = {}
  241. local loadenv = setfenv and function(s)
  242. local code = loadstring(s, "")
  243. if code then setfenv(code, parse_ctx) end
  244. return code
  245. end or function(s)
  246. return load(s, "", nil, parse_ctx)
  247. end
  248. -- Try to parse simple arithmetic, too, since some basic ops are aliases.
  249. local function parse_number(n)
  250. local x = tonumber(n)
  251. if x then return x end
  252. local code = loadenv("return "..n)
  253. if code then
  254. local ok, y = pcall(code)
  255. if ok then return y end
  256. end
  257. return nil
  258. end
  259. local function parse_imm(imm, bits, shift, scale, signed)
  260. imm = match(imm, "^#(.*)$")
  261. if not imm then werror("expected immediate operand") end
  262. local n = parse_number(imm)
  263. if n then
  264. local m = sar(n, scale)
  265. if shl(m, scale) == n then
  266. if signed then
  267. local s = sar(m, bits-1)
  268. if s == 0 then return shl(m, shift)
  269. elseif s == -1 then return shl(m + shl(1, bits), shift) end
  270. else
  271. if sar(m, bits) == 0 then return shl(m, shift) end
  272. end
  273. end
  274. werror("out of range immediate `"..imm.."'")
  275. else
  276. waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm)
  277. return 0
  278. end
  279. end
  280. local function parse_imm12(imm)
  281. imm = match(imm, "^#(.*)$")
  282. if not imm then werror("expected immediate operand") end
  283. local n = parse_number(imm)
  284. if n then
  285. if shr(n, 12) == 0 then
  286. return shl(n, 10)
  287. elseif band(n, 0xff000fff) == 0 then
  288. return shr(n, 2) + 0x00400000
  289. end
  290. werror("out of range immediate `"..imm.."'")
  291. else
  292. waction("IMM12", 0, imm)
  293. return 0
  294. end
  295. end
  296. local function parse_imm13(imm)
  297. imm = match(imm, "^#(.*)$")
  298. if not imm then werror("expected immediate operand") end
  299. local n = parse_number(imm)
  300. local r64 = parse_reg_type == "x"
  301. if n and n % 1 == 0 and n >= 0 and n <= 0xffffffff then
  302. local inv = false
  303. if band(n, 1) == 1 then n = bit.bnot(n); inv = true end
  304. local t = {}
  305. for i=1,32 do t[i] = band(n, 1); n = shr(n, 1) end
  306. local b = table.concat(t)
  307. b = b..(r64 and (inv and "1" or "0"):rep(32) or b)
  308. local p0, p1, p0a, p1a = b:match("^(0+)(1+)(0*)(1*)")
  309. if p0 then
  310. local w = p1a == "" and (r64 and 64 or 32) or #p1+#p0a
  311. if band(w, w-1) == 0 and b == b:sub(1, w):rep(64/w) then
  312. local s = band(-2*w, 0x3f) - 1
  313. if w == 64 then s = s + 0x1000 end
  314. if inv then
  315. return shl(w-#p1-#p0, 16) + shl(s+w-#p1, 10)
  316. else
  317. return shl(w-#p0, 16) + shl(s+#p1, 10)
  318. end
  319. end
  320. end
  321. werror("out of range immediate `"..imm.."'")
  322. elseif r64 then
  323. waction("IMM13X", 0, format("(unsigned int)(%s)", imm))
  324. actargs[#actargs+1] = format("(unsigned int)((unsigned long long)(%s)>>32)", imm)
  325. return 0
  326. else
  327. waction("IMM13W", 0, imm)
  328. return 0
  329. end
  330. end
  331. local function parse_imm6(imm)
  332. imm = match(imm, "^#(.*)$")
  333. if not imm then werror("expected immediate operand") end
  334. local n = parse_number(imm)
  335. if n then
  336. if n >= 0 and n <= 63 then
  337. return shl(band(n, 0x1f), 19) + (n >= 32 and 0x80000000 or 0)
  338. end
  339. werror("out of range immediate `"..imm.."'")
  340. else
  341. waction("IMM6", 0, imm)
  342. return 0
  343. end
  344. end
  345. local function parse_imm_load(imm, scale)
  346. local n = parse_number(imm)
  347. if n then
  348. local m = sar(n, scale)
  349. if shl(m, scale) == n and m >= 0 and m < 0x1000 then
  350. return shl(m, 10) + 0x01000000 -- Scaled, unsigned 12 bit offset.
  351. elseif n >= -256 and n < 256 then
  352. return shl(band(n, 511), 12) -- Unscaled, signed 9 bit offset.
  353. end
  354. werror("out of range immediate `"..imm.."'")
  355. else
  356. waction("IMML", 0, imm)
  357. return 0
  358. end
  359. end
  360. local function parse_fpimm(imm)
  361. imm = match(imm, "^#(.*)$")
  362. if not imm then werror("expected immediate operand") end
  363. local n = parse_number(imm)
  364. if n then
  365. local m, e = math.frexp(n)
  366. local s, e2 = 0, band(e-2, 7)
  367. if m < 0 then m = -m; s = 0x00100000 end
  368. m = m*32-16
  369. if m % 1 == 0 and m >= 0 and m <= 15 and sar(shl(e2, 29), 29)+2 == e then
  370. return s + shl(e2, 17) + shl(m, 13)
  371. end
  372. werror("out of range immediate `"..imm.."'")
  373. else
  374. werror("NYI fpimm action")
  375. end
  376. end
  377. local function parse_shift(expr)
  378. local s, s2 = match(expr, "^(%S+)%s*(.*)$")
  379. s = map_shift[s]
  380. if not s then werror("expected shift operand") end
  381. return parse_imm(s2, 6, 10, 0, false) + shl(s, 22)
  382. end
  383. local function parse_lslx16(expr)
  384. local n = match(expr, "^lsl%s*#(%d+)$")
  385. n = tonumber(n)
  386. if not n then werror("expected shift operand") end
  387. if band(n, parse_reg_type == "x" and 0xffffffcf or 0xffffffef) ~= 0 then
  388. werror("bad shift amount")
  389. end
  390. return shl(n, 17)
  391. end
  392. local function parse_extend(expr)
  393. local s, s2 = match(expr, "^(%S+)%s*(.*)$")
  394. if s == "lsl" then
  395. s = parse_reg_type == "x" and 3 or 2
  396. else
  397. s = map_extend[s]
  398. end
  399. if not s then werror("expected extend operand") end
  400. return (s2 == "" and 0 or parse_imm(s2, 3, 10, 0, false)) + shl(s, 13)
  401. end
  402. local function parse_cond(expr, inv)
  403. local c = map_cond[expr]
  404. if not c then werror("expected condition operand") end
  405. return shl(bit.bxor(c, inv), 12)
  406. end
  407. local function parse_load(params, nparams, n, op)
  408. if params[n+2] then werror("too many operands") end
  409. local pn, p2 = params[n], params[n+1]
  410. local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
  411. if not p1 then
  412. if not p2 then
  413. local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$")
  414. if reg and tailr ~= "" then
  415. local base, tp = parse_reg_base(reg)
  416. if tp then
  417. waction("IMML", 0, format(tp.ctypefmt, tailr))
  418. return op + base
  419. end
  420. end
  421. end
  422. werror("expected address operand")
  423. end
  424. local scale = shr(op, 30)
  425. if p2 then
  426. if wb == "!" then werror("bad use of '!'") end
  427. op = op + parse_reg_base(p1) + parse_imm(p2, 9, 12, 0, true) + 0x400
  428. elseif wb == "!" then
  429. local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$")
  430. if not p1a then werror("bad use of '!'") end
  431. op = op + parse_reg_base(p1a) + parse_imm(p2a, 9, 12, 0, true) + 0xc00
  432. else
  433. local p1a, p2a = match(p1, "^([^,%s]*)%s*(.*)$")
  434. op = op + parse_reg_base(p1a)
  435. if p2a ~= "" then
  436. local imm = match(p2a, "^,%s*#(.*)$")
  437. if imm then
  438. op = op + parse_imm_load(imm, scale)
  439. else
  440. local p2b, p3b, p3s = match(p2a, "^,%s*([^,%s]*)%s*,?%s*(%S*)%s*(.*)$")
  441. op = op + shl(parse_reg(p2b), 16) + 0x00200800
  442. if parse_reg_type ~= "x" and parse_reg_type ~= "w" then
  443. werror("bad index register type")
  444. end
  445. if p3b == "" then
  446. if parse_reg_type ~= "x" then werror("bad index register type") end
  447. op = op + 0x6000
  448. else
  449. if p3s == "" or p3s == "#0" then
  450. elseif p3s == "#"..scale then
  451. op = op + 0x1000
  452. else
  453. werror("bad scale")
  454. end
  455. if parse_reg_type == "x" then
  456. if p3b == "lsl" and p3s ~= "" then op = op + 0x6000
  457. elseif p3b == "sxtx" then op = op + 0xe000
  458. else
  459. werror("bad extend/shift specifier")
  460. end
  461. else
  462. if p3b == "uxtw" then op = op + 0x4000
  463. elseif p3b == "sxtw" then op = op + 0xc000
  464. else
  465. werror("bad extend/shift specifier")
  466. end
  467. end
  468. end
  469. end
  470. else
  471. if wb == "!" then werror("bad use of '!'") end
  472. op = op + 0x01000000
  473. end
  474. end
  475. return op
  476. end
  477. local function parse_load_pair(params, nparams, n, op)
  478. if params[n+2] then werror("too many operands") end
  479. local pn, p2 = params[n], params[n+1]
  480. local scale = shr(op, 30) == 0 and 2 or 3
  481. local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
  482. if not p1 then
  483. if not p2 then
  484. local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$")
  485. if reg and tailr ~= "" then
  486. local base, tp = parse_reg_base(reg)
  487. if tp then
  488. waction("IMM", 32768+7*32+15+scale*1024, format(tp.ctypefmt, tailr))
  489. return op + base + 0x01000000
  490. end
  491. end
  492. end
  493. werror("expected address operand")
  494. end
  495. if p2 then
  496. if wb == "!" then werror("bad use of '!'") end
  497. op = op + 0x00800000
  498. else
  499. local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$")
  500. if p1a then p1, p2 = p1a, p2a else p2 = "#0" end
  501. op = op + (wb == "!" and 0x01800000 or 0x01000000)
  502. end
  503. return op + parse_reg_base(p1) + parse_imm(p2, 7, 15, scale, true)
  504. end
  505. local function parse_label(label, def)
  506. local prefix = sub(label, 1, 2)
  507. -- =>label (pc label reference)
  508. if prefix == "=>" then
  509. return "PC", 0, sub(label, 3)
  510. end
  511. -- ->name (global label reference)
  512. if prefix == "->" then
  513. return "LG", map_global[sub(label, 3)]
  514. end
  515. if def then
  516. -- [1-9] (local label definition)
  517. if match(label, "^[1-9]$") then
  518. return "LG", 10+tonumber(label)
  519. end
  520. else
  521. -- [<>][1-9] (local label reference)
  522. local dir, lnum = match(label, "^([<>])([1-9])$")
  523. if dir then -- Fwd: 1-9, Bkwd: 11-19.
  524. return "LG", lnum + (dir == ">" and 0 or 10)
  525. end
  526. -- extern label (extern label reference)
  527. local extname = match(label, "^extern%s+(%S+)$")
  528. if extname then
  529. return "EXT", map_extern[extname]
  530. end
  531. end
  532. werror("bad label `"..label.."'")
  533. end
  534. local function branch_type(op)
  535. if band(op, 0x7c000000) == 0x14000000 then return 0 -- B, BL
  536. elseif shr(op, 24) == 0x54 or band(op, 0x7e000000) == 0x34000000 or
  537. band(op, 0x3b000000) == 0x18000000 then
  538. return 0x800 -- B.cond, CBZ, CBNZ, LDR* literal
  539. elseif band(op, 0x7e000000) == 0x36000000 then return 0x1000 -- TBZ, TBNZ
  540. elseif band(op, 0x9f000000) == 0x10000000 then return 0x2000 -- ADR
  541. elseif band(op, 0x9f000000) == band(0x90000000) then return 0x3000 -- ADRP
  542. else
  543. assert(false, "unknown branch type")
  544. end
  545. end
  546. ------------------------------------------------------------------------------
  547. local map_op, op_template
  548. local function op_alias(opname, f)
  549. return function(params, nparams)
  550. if not params then return "-> "..opname:sub(1, -3) end
  551. f(params, nparams)
  552. op_template(params, map_op[opname], nparams)
  553. end
  554. end
  555. local function alias_bfx(p)
  556. p[4] = "#("..p[3]:sub(2)..")+("..p[4]:sub(2)..")-1"
  557. end
  558. local function alias_bfiz(p)
  559. parse_reg(p[1])
  560. if parse_reg_type == "w" then
  561. p[3] = "#-("..p[3]:sub(2)..")%32"
  562. p[4] = "#("..p[4]:sub(2)..")-1"
  563. else
  564. p[3] = "#-("..p[3]:sub(2)..")%64"
  565. p[4] = "#("..p[4]:sub(2)..")-1"
  566. end
  567. end
  568. local alias_lslimm = op_alias("ubfm_4", function(p)
  569. parse_reg(p[1])
  570. local sh = p[3]:sub(2)
  571. if parse_reg_type == "w" then
  572. p[3] = "#-("..sh..")%32"
  573. p[4] = "#31-("..sh..")"
  574. else
  575. p[3] = "#-("..sh..")%64"
  576. p[4] = "#63-("..sh..")"
  577. end
  578. end)
  579. -- Template strings for ARM instructions.
  580. map_op = {
  581. -- Basic data processing instructions.
  582. add_3 = "0b000000DNMg|11000000pDpNIg|8b206000pDpNMx",
  583. add_4 = "0b000000DNMSg|0b200000DNMXg|8b200000pDpNMXx|8b200000pDpNxMwX",
  584. adds_3 = "2b000000DNMg|31000000DpNIg|ab206000DpNMx",
  585. adds_4 = "2b000000DNMSg|2b200000DNMXg|ab200000DpNMXx|ab200000DpNxMwX",
  586. cmn_2 = "2b00001fNMg|3100001fpNIg|ab20601fpNMx",
  587. cmn_3 = "2b00001fNMSg|2b20001fNMXg|ab20001fpNMXx|ab20001fpNxMwX",
  588. sub_3 = "4b000000DNMg|51000000pDpNIg|cb206000pDpNMx",
  589. sub_4 = "4b000000DNMSg|4b200000DNMXg|cb200000pDpNMXx|cb200000pDpNxMwX",
  590. subs_3 = "6b000000DNMg|71000000DpNIg|eb206000DpNMx",
  591. subs_4 = "6b000000DNMSg|6b200000DNMXg|eb200000DpNMXx|eb200000DpNxMwX",
  592. cmp_2 = "6b00001fNMg|7100001fpNIg|eb20601fpNMx",
  593. cmp_3 = "6b00001fNMSg|6b20001fNMXg|eb20001fpNMXx|eb20001fpNxMwX",
  594. neg_2 = "4b0003e0DMg",
  595. neg_3 = "4b0003e0DMSg",
  596. negs_2 = "6b0003e0DMg",
  597. negs_3 = "6b0003e0DMSg",
  598. adc_3 = "1a000000DNMg",
  599. adcs_3 = "3a000000DNMg",
  600. sbc_3 = "5a000000DNMg",
  601. sbcs_3 = "7a000000DNMg",
  602. ngc_2 = "5a0003e0DMg",
  603. ngcs_2 = "7a0003e0DMg",
  604. and_3 = "0a000000DNMg|12000000pDNig",
  605. and_4 = "0a000000DNMSg",
  606. orr_3 = "2a000000DNMg|32000000pDNig",
  607. orr_4 = "2a000000DNMSg",
  608. eor_3 = "4a000000DNMg|52000000pDNig",
  609. eor_4 = "4a000000DNMSg",
  610. ands_3 = "6a000000DNMg|72000000DNig",
  611. ands_4 = "6a000000DNMSg",
  612. tst_2 = "6a00001fNMg|7200001fNig",
  613. tst_3 = "6a00001fNMSg",
  614. bic_3 = "0a200000DNMg",
  615. bic_4 = "0a200000DNMSg",
  616. orn_3 = "2a200000DNMg",
  617. orn_4 = "2a200000DNMSg",
  618. eon_3 = "4a200000DNMg",
  619. eon_4 = "4a200000DNMSg",
  620. bics_3 = "6a200000DNMg",
  621. bics_4 = "6a200000DNMSg",
  622. movn_2 = "12800000DWg",
  623. movn_3 = "12800000DWRg",
  624. movz_2 = "52800000DWg",
  625. movz_3 = "52800000DWRg",
  626. movk_2 = "72800000DWg",
  627. movk_3 = "72800000DWRg",
  628. -- TODO: this doesn't cover all valid immediates for mov reg, #imm.
  629. mov_2 = "2a0003e0DMg|52800000DW|320003e0pDig|11000000pDpNg",
  630. mov_3 = "2a0003e0DMSg",
  631. mvn_2 = "2a2003e0DMg",
  632. mvn_3 = "2a2003e0DMSg",
  633. adr_2 = "10000000DBx",
  634. adrp_2 = "90000000DBx",
  635. csel_4 = "1a800000DNMCg",
  636. csinc_4 = "1a800400DNMCg",
  637. csinv_4 = "5a800000DNMCg",
  638. csneg_4 = "5a800400DNMCg",
  639. cset_2 = "1a9f07e0Dcg",
  640. csetm_2 = "5a9f03e0Dcg",
  641. cinc_3 = "1a800400DNmcg",
  642. cinv_3 = "5a800000DNmcg",
  643. cneg_3 = "5a800400DNmcg",
  644. ccmn_4 = "3a400000NMVCg|3a400800N5VCg",
  645. ccmp_4 = "7a400000NMVCg|7a400800N5VCg",
  646. madd_4 = "1b000000DNMAg",
  647. msub_4 = "1b008000DNMAg",
  648. mul_3 = "1b007c00DNMg",
  649. mneg_3 = "1b00fc00DNMg",
  650. smaddl_4 = "9b200000DxNMwAx",
  651. smsubl_4 = "9b208000DxNMwAx",
  652. smull_3 = "9b207c00DxNMw",
  653. smnegl_3 = "9b20fc00DxNMw",
  654. smulh_3 = "9b407c00DNMx",
  655. umaddl_4 = "9ba00000DxNMwAx",
  656. umsubl_4 = "9ba08000DxNMwAx",
  657. umull_3 = "9ba07c00DxNMw",
  658. umnegl_3 = "9ba0fc00DxNMw",
  659. umulh_3 = "9bc07c00DNMx",
  660. udiv_3 = "1ac00800DNMg",
  661. sdiv_3 = "1ac00c00DNMg",
  662. -- Bit operations.
  663. sbfm_4 = "13000000DN12w|93400000DN12x",
  664. bfm_4 = "33000000DN12w|b3400000DN12x",
  665. ubfm_4 = "53000000DN12w|d3400000DN12x",
  666. extr_4 = "13800000DNM2w|93c00000DNM2x",
  667. sxtb_2 = "13001c00DNw|93401c00DNx",
  668. sxth_2 = "13003c00DNw|93403c00DNx",
  669. sxtw_2 = "93407c00DxNw",
  670. uxtb_2 = "53001c00DNw",
  671. uxth_2 = "53003c00DNw",
  672. sbfx_4 = op_alias("sbfm_4", alias_bfx),
  673. bfxil_4 = op_alias("bfm_4", alias_bfx),
  674. ubfx_4 = op_alias("ubfm_4", alias_bfx),
  675. sbfiz_4 = op_alias("sbfm_4", alias_bfiz),
  676. bfi_4 = op_alias("bfm_4", alias_bfiz),
  677. ubfiz_4 = op_alias("ubfm_4", alias_bfiz),
  678. lsl_3 = function(params, nparams)
  679. if params and params[3]:byte() == 35 then
  680. return alias_lslimm(params, nparams)
  681. else
  682. return op_template(params, "1ac02000DNMg", nparams)
  683. end
  684. end,
  685. lsr_3 = "1ac02400DNMg|53007c00DN1w|d340fc00DN1x",
  686. asr_3 = "1ac02800DNMg|13007c00DN1w|9340fc00DN1x",
  687. ror_3 = "1ac02c00DNMg|13800000DNm2w|93c00000DNm2x",
  688. clz_2 = "5ac01000DNg",
  689. cls_2 = "5ac01400DNg",
  690. rbit_2 = "5ac00000DNg",
  691. rev_2 = "5ac00800DNw|dac00c00DNx",
  692. rev16_2 = "5ac00400DNg",
  693. rev32_2 = "dac00800DNx",
  694. -- Loads and stores.
  695. ["strb_*"] = "38000000DwL",
  696. ["ldrb_*"] = "38400000DwL",
  697. ["ldrsb_*"] = "38c00000DwL|38800000DxL",
  698. ["strh_*"] = "78000000DwL",
  699. ["ldrh_*"] = "78400000DwL",
  700. ["ldrsh_*"] = "78c00000DwL|78800000DxL",
  701. ["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL",
  702. ["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL",
  703. ["ldrsw_*"] = "98000000DxB|b8800000DxL",
  704. -- NOTE: ldur etc. are handled by ldr et al.
  705. ["stp_*"] = "28000000DAwP|a8000000DAxP|2c000000DAsP|6c000000DAdP",
  706. ["ldp_*"] = "28400000DAwP|a8400000DAxP|2c400000DAsP|6c400000DAdP",
  707. ["ldpsw_*"] = "68400000DAxP",
  708. -- Branches.
  709. b_1 = "14000000B",
  710. bl_1 = "94000000B",
  711. blr_1 = "d63f0000Nx",
  712. br_1 = "d61f0000Nx",
  713. ret_0 = "d65f03c0",
  714. ret_1 = "d65f0000Nx",
  715. -- b.cond is added below.
  716. cbz_2 = "34000000DBg",
  717. cbnz_2 = "35000000DBg",
  718. tbz_3 = "36000000DTBw|36000000DTBx",
  719. tbnz_3 = "37000000DTBw|37000000DTBx",
  720. -- Miscellaneous instructions.
  721. -- TODO: hlt, hvc, smc, svc, eret, dcps[123], drps, mrs, msr
  722. -- TODO: sys, sysl, ic, dc, at, tlbi
  723. -- TODO: hint, yield, wfe, wfi, sev, sevl
  724. -- TODO: clrex, dsb, dmb, isb
  725. nop_0 = "d503201f",
  726. brk_0 = "d4200000",
  727. brk_1 = "d4200000W",
  728. -- Floating point instructions.
  729. fmov_2 = "1e204000DNf|1e260000DwNs|1e270000DsNw|9e660000DxNd|9e670000DdNx|1e201000DFf",
  730. fabs_2 = "1e20c000DNf",
  731. fneg_2 = "1e214000DNf",
  732. fsqrt_2 = "1e21c000DNf",
  733. fcvt_2 = "1e22c000DdNs|1e624000DsNd",
  734. -- TODO: half-precision and fixed-point conversions.
  735. fcvtas_2 = "1e240000DwNs|9e240000DxNs|1e640000DwNd|9e640000DxNd",
  736. fcvtau_2 = "1e250000DwNs|9e250000DxNs|1e650000DwNd|9e650000DxNd",
  737. fcvtms_2 = "1e300000DwNs|9e300000DxNs|1e700000DwNd|9e700000DxNd",
  738. fcvtmu_2 = "1e310000DwNs|9e310000DxNs|1e710000DwNd|9e710000DxNd",
  739. fcvtns_2 = "1e200000DwNs|9e200000DxNs|1e600000DwNd|9e600000DxNd",
  740. fcvtnu_2 = "1e210000DwNs|9e210000DxNs|1e610000DwNd|9e610000DxNd",
  741. fcvtps_2 = "1e280000DwNs|9e280000DxNs|1e680000DwNd|9e680000DxNd",
  742. fcvtpu_2 = "1e290000DwNs|9e290000DxNs|1e690000DwNd|9e690000DxNd",
  743. fcvtzs_2 = "1e380000DwNs|9e380000DxNs|1e780000DwNd|9e780000DxNd",
  744. fcvtzu_2 = "1e390000DwNs|9e390000DxNs|1e790000DwNd|9e790000DxNd",
  745. scvtf_2 = "1e220000DsNw|9e220000DsNx|1e620000DdNw|9e620000DdNx",
  746. ucvtf_2 = "1e230000DsNw|9e230000DsNx|1e630000DdNw|9e630000DdNx",
  747. frintn_2 = "1e244000DNf",
  748. frintp_2 = "1e24c000DNf",
  749. frintm_2 = "1e254000DNf",
  750. frintz_2 = "1e25c000DNf",
  751. frinta_2 = "1e264000DNf",
  752. frintx_2 = "1e274000DNf",
  753. frinti_2 = "1e27c000DNf",
  754. fadd_3 = "1e202800DNMf",
  755. fsub_3 = "1e203800DNMf",
  756. fmul_3 = "1e200800DNMf",
  757. fnmul_3 = "1e208800DNMf",
  758. fdiv_3 = "1e201800DNMf",
  759. fmadd_4 = "1f000000DNMAf",
  760. fmsub_4 = "1f008000DNMAf",
  761. fnmadd_4 = "1f200000DNMAf",
  762. fnmsub_4 = "1f208000DNMAf",
  763. fmax_3 = "1e204800DNMf",
  764. fmaxnm_3 = "1e206800DNMf",
  765. fmin_3 = "1e205800DNMf",
  766. fminnm_3 = "1e207800DNMf",
  767. fcmp_2 = "1e202000NMf|1e202008NZf",
  768. fcmpe_2 = "1e202010NMf|1e202018NZf",
  769. fccmp_4 = "1e200400NMVCf",
  770. fccmpe_4 = "1e200410NMVCf",
  771. fcsel_4 = "1e200c00DNMCf",
  772. -- TODO: crc32*, aes*, sha*, pmull
  773. -- TODO: SIMD instructions.
  774. }
  775. for cond,c in pairs(map_cond) do
  776. map_op["b"..cond.."_1"] = tohex(0x54000000+c).."B"
  777. end
  778. ------------------------------------------------------------------------------
  779. -- Handle opcodes defined with template strings.
  780. local function parse_template(params, template, nparams, pos)
  781. local op = tonumber(sub(template, 1, 8), 16)
  782. local n = 1
  783. local rtt = {}
  784. parse_reg_type = false
  785. -- Process each character.
  786. for p in gmatch(sub(template, 9), ".") do
  787. local q = params[n]
  788. if p == "D" then
  789. op = op + parse_reg(q); n = n + 1
  790. elseif p == "N" then
  791. op = op + shl(parse_reg(q), 5); n = n + 1
  792. elseif p == "M" then
  793. op = op + shl(parse_reg(q), 16); n = n + 1
  794. elseif p == "A" then
  795. op = op + shl(parse_reg(q), 10); n = n + 1
  796. elseif p == "m" then
  797. op = op + shl(parse_reg(params[n-1]), 16)
  798. elseif p == "p" then
  799. if q == "sp" then params[n] = "@x31" end
  800. elseif p == "g" then
  801. if parse_reg_type == "x" then
  802. op = op + 0x80000000
  803. elseif parse_reg_type ~= "w" then
  804. werror("bad register type")
  805. end
  806. parse_reg_type = false
  807. elseif p == "f" then
  808. if parse_reg_type == "d" then
  809. op = op + 0x00400000
  810. elseif parse_reg_type ~= "s" then
  811. werror("bad register type")
  812. end
  813. parse_reg_type = false
  814. elseif p == "x" or p == "w" or p == "d" or p == "s" then
  815. if parse_reg_type ~= p then
  816. werror("register size mismatch")
  817. end
  818. parse_reg_type = false
  819. elseif p == "L" then
  820. op = parse_load(params, nparams, n, op)
  821. elseif p == "P" then
  822. op = parse_load_pair(params, nparams, n, op)
  823. elseif p == "B" then
  824. local mode, v, s = parse_label(q, false); n = n + 1
  825. local m = branch_type(op)
  826. waction("REL_"..mode, v+m, s, 1)
  827. elseif p == "I" then
  828. op = op + parse_imm12(q); n = n + 1
  829. elseif p == "i" then
  830. op = op + parse_imm13(q); n = n + 1
  831. elseif p == "W" then
  832. op = op + parse_imm(q, 16, 5, 0, false); n = n + 1
  833. elseif p == "T" then
  834. op = op + parse_imm6(q); n = n + 1
  835. elseif p == "1" then
  836. op = op + parse_imm(q, 6, 16, 0, false); n = n + 1
  837. elseif p == "2" then
  838. op = op + parse_imm(q, 6, 10, 0, false); n = n + 1
  839. elseif p == "5" then
  840. op = op + parse_imm(q, 5, 16, 0, false); n = n + 1
  841. elseif p == "V" then
  842. op = op + parse_imm(q, 4, 0, 0, false); n = n + 1
  843. elseif p == "F" then
  844. op = op + parse_fpimm(q); n = n + 1
  845. elseif p == "Z" then
  846. if q ~= "#0" and q ~= "#0.0" then werror("expected zero immediate") end
  847. n = n + 1
  848. elseif p == "S" then
  849. op = op + parse_shift(q); n = n + 1
  850. elseif p == "X" then
  851. op = op + parse_extend(q); n = n + 1
  852. elseif p == "R" then
  853. op = op + parse_lslx16(q); n = n + 1
  854. elseif p == "C" then
  855. op = op + parse_cond(q, 0); n = n + 1
  856. elseif p == "c" then
  857. op = op + parse_cond(q, 1); n = n + 1
  858. else
  859. assert(false)
  860. end
  861. end
  862. wputpos(pos, op)
  863. end
  864. function op_template(params, template, nparams)
  865. if not params then return template:gsub("%x%x%x%x%x%x%x%x", "") end
  866. -- Limit number of section buffer positions used by a single dasm_put().
  867. -- A single opcode needs a maximum of 3 positions.
  868. if secpos+3 > maxsecpos then wflush() end
  869. local pos = wpos()
  870. local lpos, apos, spos = #actlist, #actargs, secpos
  871. local ok, err
  872. for t in gmatch(template, "[^|]+") do
  873. ok, err = pcall(parse_template, params, t, nparams, pos)
  874. if ok then return end
  875. secpos = spos
  876. actlist[lpos+1] = nil
  877. actlist[lpos+2] = nil
  878. actlist[lpos+3] = nil
  879. actargs[apos+1] = nil
  880. actargs[apos+2] = nil
  881. actargs[apos+3] = nil
  882. end
  883. error(err, 0)
  884. end
  885. map_op[".template__"] = op_template
  886. ------------------------------------------------------------------------------
  887. -- Pseudo-opcode to mark the position where the action list is to be emitted.
  888. map_op[".actionlist_1"] = function(params)
  889. if not params then return "cvar" end
  890. local name = params[1] -- No syntax check. You get to keep the pieces.
  891. wline(function(out) writeactions(out, name) end)
  892. end
  893. -- Pseudo-opcode to mark the position where the global enum is to be emitted.
  894. map_op[".globals_1"] = function(params)
  895. if not params then return "prefix" end
  896. local prefix = params[1] -- No syntax check. You get to keep the pieces.
  897. wline(function(out) writeglobals(out, prefix) end)
  898. end
  899. -- Pseudo-opcode to mark the position where the global names are to be emitted.
  900. map_op[".globalnames_1"] = function(params)
  901. if not params then return "cvar" end
  902. local name = params[1] -- No syntax check. You get to keep the pieces.
  903. wline(function(out) writeglobalnames(out, name) end)
  904. end
  905. -- Pseudo-opcode to mark the position where the extern names are to be emitted.
  906. map_op[".externnames_1"] = function(params)
  907. if not params then return "cvar" end
  908. local name = params[1] -- No syntax check. You get to keep the pieces.
  909. wline(function(out) writeexternnames(out, name) end)
  910. end
  911. ------------------------------------------------------------------------------
  912. -- Label pseudo-opcode (converted from trailing colon form).
  913. map_op[".label_1"] = function(params)
  914. if not params then return "[1-9] | ->global | =>pcexpr" end
  915. if secpos+1 > maxsecpos then wflush() end
  916. local mode, n, s = parse_label(params[1], true)
  917. if mode == "EXT" then werror("bad label definition") end
  918. waction("LABEL_"..mode, n, s, 1)
  919. end
  920. ------------------------------------------------------------------------------
  921. -- Pseudo-opcodes for data storage.
  922. map_op[".long_*"] = function(params)
  923. if not params then return "imm..." end
  924. for _,p in ipairs(params) do
  925. local n = tonumber(p)
  926. if not n then werror("bad immediate `"..p.."'") end
  927. if n < 0 then n = n + 2^32 end
  928. wputw(n)
  929. if secpos+2 > maxsecpos then wflush() end
  930. end
  931. end
  932. -- Alignment pseudo-opcode.
  933. map_op[".align_1"] = function(params)
  934. if not params then return "numpow2" end
  935. if secpos+1 > maxsecpos then wflush() end
  936. local align = tonumber(params[1])
  937. if align then
  938. local x = align
  939. -- Must be a power of 2 in the range (2 ... 256).
  940. for i=1,8 do
  941. x = x / 2
  942. if x == 1 then
  943. waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1.
  944. return
  945. end
  946. end
  947. end
  948. werror("bad alignment")
  949. end
  950. ------------------------------------------------------------------------------
  951. -- Pseudo-opcode for (primitive) type definitions (map to C types).
  952. map_op[".type_3"] = function(params, nparams)
  953. if not params then
  954. return nparams == 2 and "name, ctype" or "name, ctype, reg"
  955. end
  956. local name, ctype, reg = params[1], params[2], params[3]
  957. if not match(name, "^[%a_][%w_]*$") then
  958. werror("bad type name `"..name.."'")
  959. end
  960. local tp = map_type[name]
  961. if tp then
  962. werror("duplicate type `"..name.."'")
  963. end
  964. -- Add #type to defines. A bit unclean to put it in map_archdef.
  965. map_archdef["#"..name] = "sizeof("..ctype..")"
  966. -- Add new type and emit shortcut define.
  967. local num = ctypenum + 1
  968. map_type[name] = {
  969. ctype = ctype,
  970. ctypefmt = format("Dt%X(%%s)", num),
  971. reg = reg,
  972. }
  973. wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
  974. ctypenum = num
  975. end
  976. map_op[".type_2"] = map_op[".type_3"]
  977. -- Dump type definitions.
  978. local function dumptypes(out, lvl)
  979. local t = {}
  980. for name in pairs(map_type) do t[#t+1] = name end
  981. sort(t)
  982. out:write("Type definitions:\n")
  983. for _,name in ipairs(t) do
  984. local tp = map_type[name]
  985. local reg = tp.reg or ""
  986. out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
  987. end
  988. out:write("\n")
  989. end
  990. ------------------------------------------------------------------------------
  991. -- Set the current section.
  992. function _M.section(num)
  993. waction("SECTION", num)
  994. wflush(true) -- SECTION is a terminal action.
  995. end
  996. ------------------------------------------------------------------------------
  997. -- Dump architecture description.
  998. function _M.dumparch(out)
  999. out:write(format("DynASM %s version %s, released %s\n\n",
  1000. _info.arch, _info.version, _info.release))
  1001. dumpactions(out)
  1002. end
  1003. -- Dump all user defined elements.
  1004. function _M.dumpdef(out, lvl)
  1005. dumptypes(out, lvl)
  1006. dumpglobals(out, lvl)
  1007. dumpexterns(out, lvl)
  1008. end
  1009. ------------------------------------------------------------------------------
  1010. -- Pass callbacks from/to the DynASM core.
  1011. function _M.passcb(wl, we, wf, ww)
  1012. wline, werror, wfatal, wwarn = wl, we, wf, ww
  1013. return wflush
  1014. end
  1015. -- Setup the arch-specific module.
  1016. function _M.setup(arch, opt)
  1017. g_arch, g_opt = arch, opt
  1018. end
  1019. -- Merge the core maps and the arch-specific maps.
  1020. function _M.mergemaps(map_coreop, map_def)
  1021. setmetatable(map_op, { __index = map_coreop })
  1022. setmetatable(map_def, { __index = map_archdef })
  1023. return map_op, map_def
  1024. end
  1025. return _M
  1026. ------------------------------------------------------------------------------