dasm_arm64.lua 36 KB

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