dasm_arm.lua 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. ------------------------------------------------------------------------------
  2. -- DynASM ARM module.
  3. --
  4. -- Copyright (C) 2005-2011 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 ARM module",
  11. version = "1.3.0",
  12. vernum = 10300,
  13. release = "2011-05-05",
  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. -- Inherited tables and callbacks.
  27. local g_opt, g_arch
  28. local wline, werror, wfatal, wwarn
  29. -- Action name list.
  30. -- CHECK: Keep this in sync with the C code!
  31. local action_names = {
  32. "STOP", "SECTION", "ESC", "REL_EXT",
  33. "ALIGN", "REL_LG", "LABEL_LG",
  34. "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM16", "IMML8", "IMML12",
  35. }
  36. -- Maximum number of section buffer positions for dasm_put().
  37. -- CHECK: Keep this in sync with the C code!
  38. local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines.
  39. -- Action name -> action number.
  40. local map_action = {}
  41. for n,name in ipairs(action_names) do
  42. map_action[name] = n-1
  43. end
  44. -- Action list buffer.
  45. local actlist = {}
  46. -- Argument list for next dasm_put(). Start with offset 0 into action list.
  47. local actargs = { 0 }
  48. -- Current number of section buffer positions for dasm_put().
  49. local secpos = 1
  50. ------------------------------------------------------------------------------
  51. -- Return 8 digit hex number.
  52. local function tohex(x)
  53. return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua.
  54. end
  55. -- Dump action names and numbers.
  56. local function dumpactions(out)
  57. out:write("DynASM encoding engine action codes:\n")
  58. for n,name in ipairs(action_names) do
  59. local num = map_action[name]
  60. out:write(format(" %-10s %02X %d\n", name, num, num))
  61. end
  62. out:write("\n")
  63. end
  64. -- Write action list buffer as a huge static C array.
  65. local function writeactions(out, name)
  66. local nn = #actlist
  67. if nn == 0 then nn = 1; actlist[0] = map_action.STOP end
  68. out:write("static const unsigned int ", name, "[", nn, "] = {\n")
  69. for i = 1,nn-1 do
  70. assert(out:write("0x", tohex(actlist[i]), ",\n"))
  71. end
  72. assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n"))
  73. end
  74. ------------------------------------------------------------------------------
  75. -- Add word to action list.
  76. local function wputxw(n)
  77. assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
  78. actlist[#actlist+1] = n
  79. end
  80. -- Add action to list with optional arg. Advance buffer pos, too.
  81. local function waction(action, val, a, num)
  82. local w = assert(map_action[action], "bad action name `"..action.."'")
  83. wputxw(w * 0x10000 + (val or 0))
  84. if a then actargs[#actargs+1] = a end
  85. if a or num then secpos = secpos + (num or 1) end
  86. end
  87. -- Flush action list (intervening C code or buffer pos overflow).
  88. local function wflush(term)
  89. if #actlist == actargs[1] then return end -- Nothing to flush.
  90. if not term then waction("STOP") end -- Terminate action list.
  91. wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true)
  92. actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put().
  93. secpos = 1 -- The actionlist offset occupies a buffer position, too.
  94. end
  95. -- Put escaped word.
  96. local function wputw(n)
  97. if n <= 0x000fffff then waction("ESC") end
  98. wputxw(n)
  99. end
  100. -- Reserve position for word.
  101. local function wpos()
  102. local pos = #actlist+1
  103. actlist[pos] = ""
  104. return pos
  105. end
  106. -- Store word to reserved position.
  107. local function wputpos(pos, n)
  108. assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
  109. if n <= 0x000fffff then
  110. insert(actlist, pos+1, n)
  111. n = map_action.ESC * 0x10000
  112. end
  113. actlist[pos] = n
  114. end
  115. ------------------------------------------------------------------------------
  116. -- Global label name -> global label number. With auto assignment on 1st use.
  117. local next_global = 20
  118. local map_global = setmetatable({}, { __index = function(t, name)
  119. if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end
  120. local n = next_global
  121. if n > 2047 then werror("too many global labels") end
  122. next_global = n + 1
  123. t[name] = n
  124. return n
  125. end})
  126. -- Dump global labels.
  127. local function dumpglobals(out, lvl)
  128. local t = {}
  129. for name, n in pairs(map_global) do t[n] = name end
  130. out:write("Global labels:\n")
  131. for i=20,next_global-1 do
  132. out:write(format(" %s\n", t[i]))
  133. end
  134. out:write("\n")
  135. end
  136. -- Write global label enum.
  137. local function writeglobals(out, prefix)
  138. local t = {}
  139. for name, n in pairs(map_global) do t[n] = name end
  140. out:write("enum {\n")
  141. for i=20,next_global-1 do
  142. out:write(" ", prefix, t[i], ",\n")
  143. end
  144. out:write(" ", prefix, "_MAX\n};\n")
  145. end
  146. -- Write global label names.
  147. local function writeglobalnames(out, name)
  148. local t = {}
  149. for name, n in pairs(map_global) do t[n] = name end
  150. out:write("static const char *const ", name, "[] = {\n")
  151. for i=20,next_global-1 do
  152. out:write(" \"", t[i], "\",\n")
  153. end
  154. out:write(" (const char *)0\n};\n")
  155. end
  156. ------------------------------------------------------------------------------
  157. -- Extern label name -> extern label number. With auto assignment on 1st use.
  158. local next_extern = 0
  159. local map_extern_ = {}
  160. local map_extern = setmetatable({}, { __index = function(t, name)
  161. -- No restrictions on the name for now.
  162. local n = next_extern
  163. if n > 2047 then werror("too many extern labels") end
  164. next_extern = n + 1
  165. t[name] = n
  166. map_extern_[n] = name
  167. return n
  168. end})
  169. -- Dump extern labels.
  170. local function dumpexterns(out, lvl)
  171. out:write("Extern labels:\n")
  172. for i=0,next_extern-1 do
  173. out:write(format(" %s\n", map_extern_[i]))
  174. end
  175. out:write("\n")
  176. end
  177. -- Write extern label names.
  178. local function writeexternnames(out, name)
  179. out:write("static const char *const ", name, "[] = {\n")
  180. for i=0,next_extern-1 do
  181. out:write(" \"", map_extern_[i], "\",\n")
  182. end
  183. out:write(" (const char *)0\n};\n")
  184. end
  185. ------------------------------------------------------------------------------
  186. -- Arch-specific maps.
  187. -- Ext. register name -> int. name.
  188. local map_archdef = { sp = "r13", lr = "r14", pc = "r15", }
  189. -- Int. register name -> ext. name.
  190. local map_reg_rev = { r13 = "sp", r14 = "lr", r15 = "pc", }
  191. local map_type = {} -- Type name -> { ctype, reg }
  192. local ctypenum = 0 -- Type number (for Dt... macros).
  193. -- Reverse defines for registers.
  194. function _M.revdef(s)
  195. return map_reg_rev[s] or s
  196. end
  197. local map_shift = { lsl = 0, lsr = 1, asr = 2, ror = 3, }
  198. local map_cond = {
  199. eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7,
  200. hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14,
  201. hs = 2, lo = 3,
  202. }
  203. ------------------------------------------------------------------------------
  204. -- Template strings for ARM instructions.
  205. local map_op = {
  206. -- Basic data processing instructions.
  207. and_3 = "e0000000DNPs",
  208. eor_3 = "e0200000DNPs",
  209. sub_3 = "e0400000DNPs",
  210. rsb_3 = "e0600000DNPs",
  211. add_3 = "e0800000DNPs",
  212. adc_3 = "e0a00000DNPs",
  213. sbc_3 = "e0c00000DNPs",
  214. rsc_3 = "e0e00000DNPs",
  215. tst_2 = "e1100000NP",
  216. teq_2 = "e1300000NP",
  217. cmp_2 = "e1500000NP",
  218. cmn_2 = "e1700000NP",
  219. orr_3 = "e1800000DNPs",
  220. mov_2 = "e1a00000DPs",
  221. bic_3 = "e1c00000DNPs",
  222. mvn_2 = "e1e00000DPs",
  223. and_4 = "e0000000DNMps",
  224. eor_4 = "e0200000DNMps",
  225. sub_4 = "e0400000DNMps",
  226. rsb_4 = "e0600000DNMps",
  227. add_4 = "e0800000DNMps",
  228. adc_4 = "e0a00000DNMps",
  229. sbc_4 = "e0c00000DNMps",
  230. rsc_4 = "e0e00000DNMps",
  231. tst_3 = "e1100000NMp",
  232. teq_3 = "e1300000NMp",
  233. cmp_3 = "e1500000NMp",
  234. cmn_3 = "e1700000NMp",
  235. orr_4 = "e1800000DNMps",
  236. mov_3 = "e1a00000DMps",
  237. bic_4 = "e1c00000DNMps",
  238. mvn_3 = "e1e00000DMps",
  239. lsl_3 = "e1a00000DMws",
  240. lsr_3 = "e1a00020DMws",
  241. asr_3 = "e1a00040DMws",
  242. ror_3 = "e1a00060DMws",
  243. rrx_2 = "e1a00060DMs",
  244. -- Multiply and multiply-accumulate.
  245. mul_3 = "e0000090NMSs",
  246. mla_4 = "e0200090NMSDs",
  247. umaal_4 = "e0400090DNMSs", -- v6
  248. mls_4 = "e0600090DNMSs", -- v6T2
  249. umull_4 = "e0800090DNMSs",
  250. umlal_4 = "e0a00090DNMSs",
  251. smull_4 = "e0c00090DNMSs",
  252. smlal_4 = "e0e00090DNMSs",
  253. -- Halfword multiply and multiply-accumulate.
  254. smlabb_4 = "e1000080NMSD", -- v5TE
  255. smlatb_4 = "e10000a0NMSD", -- v5TE
  256. smlabt_4 = "e10000c0NMSD", -- v5TE
  257. smlatt_4 = "e10000e0NMSD", -- v5TE
  258. smlawb_4 = "e1200080NMSD", -- v5TE
  259. smulwb_3 = "e12000a0NMS", -- v5TE
  260. smlawt_4 = "e12000c0NMSD", -- v5TE
  261. smulwt_3 = "e12000e0NMS", -- v5TE
  262. smlalbb_4 = "e1400080NMSD", -- v5TE
  263. smlaltb_4 = "e14000a0NMSD", -- v5TE
  264. smlalbt_4 = "e14000c0NMSD", -- v5TE
  265. smlaltt_4 = "e14000e0NMSD", -- v5TE
  266. smulbb_3 = "e1600080NMS", -- v5TE
  267. smultb_3 = "e16000a0NMS", -- v5TE
  268. smulbt_3 = "e16000c0NMS", -- v5TE
  269. smultt_3 = "e16000e0NMS", -- v5TE
  270. -- Miscellaneous data processing instructions.
  271. clz_2 = "e16f0f10DM", -- v5T
  272. rev_2 = "e6bf0f30DM", -- v6
  273. rev16_2 = "e6bf0fb0DM", -- v6
  274. revsh_2 = "e6ff0fb0DM", -- v6
  275. sel_3 = "e6800fb0DNM", -- v6
  276. usad8_3 = "e780f010NMS", -- v6
  277. usada8_4 = "e7800010NMSD", -- v6
  278. rbit_2 = "e6ff0f30DM", -- v6T2
  279. movw_2 = "e3000000DW", -- v6T2
  280. movt_2 = "e3400000DW", -- v6T2
  281. -- Note: the X encodes width-1, not width.
  282. sbfx_4 = "e7a00050DMvX", -- v6T2
  283. ubfx_4 = "e7e00050DMvX", -- v6T2
  284. -- Note: the X encodes the msb field, not the width.
  285. bfc_3 = "e7c0001fDvX", -- v6T2
  286. bfi_4 = "e7c00010DMvX", -- v6T2
  287. -- Packing and unpacking instructions.
  288. pkhbt_3 = "e6800010DNM", pkhbt_4 = "e6800010DNMv", -- v6
  289. pkhtb_3 = "e6800050DNM", pkhtb_4 = "e6800050DNMv", -- v6
  290. sxtab_3 = "e6a00070DNM", sxtab_4 = "e6a00070DNMv", -- v6
  291. sxtab16_3 = "e6800070DNM", sxtab16_4 = "e6800070DNMv", -- v6
  292. sxtah_3 = "e6b00070DNM", sxtah_4 = "e6b00070DNMv", -- v6
  293. sxtb_2 = "e6af0070DM", sxtb_3 = "e6af0070DMv", -- v6
  294. sxtb16_2 = "e68f0070DM", sxtb16_3 = "e68f0070DMv", -- v6
  295. sxth_2 = "e6bf0070DM", sxth_3 = "e6bf0070DMv", -- v6
  296. uxtab_3 = "e6e00070DNM", uxtab_4 = "e6e00070DNMv", -- v6
  297. uxtab16_3 = "e6c00070DNM", uxtab16_4 = "e6c00070DNMv", -- v6
  298. uxtah_3 = "e6f00070DNM", uxtah_4 = "e6f00070DNMv", -- v6
  299. uxtb_2 = "e6ef0070DM", uxtb_3 = "e6ef0070DMv", -- v6
  300. uxtb16_2 = "e6cf0070DM", uxtb16_3 = "e6cf0070DMv", -- v6
  301. uxth_2 = "e6ff0070DM", uxth_3 = "e6ff0070DMv", -- v6
  302. -- Saturating instructions.
  303. qadd_3 = "e1000050DMN", -- v5TE
  304. qsub_3 = "e1200050DMN", -- v5TE
  305. qdadd_3 = "e1400050DMN", -- v5TE
  306. qdsub_3 = "e1600050DMN", -- v5TE
  307. -- Note: the X for ssat* encodes sat_imm-1, not sat_imm.
  308. ssat_3 = "e6a00010DXM", ssat_4 = "e6a00010DXMp", -- v6
  309. usat_3 = "e6e00010DXM", usat_4 = "e6e00010DXMp", -- v6
  310. ssat16_3 = "e6a00f30DXM", -- v6
  311. usat16_3 = "e6e00f30DXM", -- v6
  312. -- Parallel addition and subtraction.
  313. sadd16_3 = "e6100f10DNM", -- v6
  314. sasx_3 = "e6100f30DNM", -- v6
  315. ssax_3 = "e6100f50DNM", -- v6
  316. ssub16_3 = "e6100f70DNM", -- v6
  317. sadd8_3 = "e6100f90DNM", -- v6
  318. ssub8_3 = "e6100ff0DNM", -- v6
  319. qadd16_3 = "e6200f10DNM", -- v6
  320. qasx_3 = "e6200f30DNM", -- v6
  321. qsax_3 = "e6200f50DNM", -- v6
  322. qsub16_3 = "e6200f70DNM", -- v6
  323. qadd8_3 = "e6200f90DNM", -- v6
  324. qsub8_3 = "e6200ff0DNM", -- v6
  325. shadd16_3 = "e6300f10DNM", -- v6
  326. shasx_3 = "e6300f30DNM", -- v6
  327. shsax_3 = "e6300f50DNM", -- v6
  328. shsub16_3 = "e6300f70DNM", -- v6
  329. shadd8_3 = "e6300f90DNM", -- v6
  330. shsub8_3 = "e6300ff0DNM", -- v6
  331. uadd16_3 = "e6500f10DNM", -- v6
  332. uasx_3 = "e6500f30DNM", -- v6
  333. usax_3 = "e6500f50DNM", -- v6
  334. usub16_3 = "e6500f70DNM", -- v6
  335. uadd8_3 = "e6500f90DNM", -- v6
  336. usub8_3 = "e6500ff0DNM", -- v6
  337. uqadd16_3 = "e6600f10DNM", -- v6
  338. uqasx_3 = "e6600f30DNM", -- v6
  339. uqsax_3 = "e6600f50DNM", -- v6
  340. uqsub16_3 = "e6600f70DNM", -- v6
  341. uqadd8_3 = "e6600f90DNM", -- v6
  342. uqsub8_3 = "e6600ff0DNM", -- v6
  343. uhadd16_3 = "e6700f10DNM", -- v6
  344. uhasx_3 = "e6700f30DNM", -- v6
  345. uhsax_3 = "e6700f50DNM", -- v6
  346. uhsub16_3 = "e6700f70DNM", -- v6
  347. uhadd8_3 = "e6700f90DNM", -- v6
  348. uhsub8_3 = "e6700ff0DNM", -- v6
  349. -- Load/store instructions.
  350. str_2 = "e4000000DL", str_3 = "e4000000DL", str_4 = "e4000000DL",
  351. strb_2 = "e4400000DL", strb_3 = "e4400000DL", strb_4 = "e4400000DL",
  352. ldr_2 = "e4100000DL", ldr_3 = "e4100000DL", ldr_4 = "e4100000DL",
  353. ldrb_2 = "e4500000DL", ldrb_3 = "e4500000DL", ldrb_4 = "e4500000DL",
  354. strh_2 = "e00000b0DL", strh_3 = "e00000b0DL",
  355. ldrh_2 = "e01000b0DL", ldrh_3 = "e01000b0DL",
  356. ldrd_2 = "e00000d0DL", ldrd_3 = "e00000d0DL", -- v5TE
  357. ldrsb_2 = "e01000d0DL", ldrsb_3 = "e01000d0DL",
  358. strd_2 = "e00000f0DL", strd_3 = "e00000f0DL", -- v5TE
  359. ldrsh_2 = "e01000f0DL", ldrsh_3 = "e01000f0DL",
  360. ldm_2 = "e8900000nR", ldmia_2 = "e8900000nR", ldmfd_2 = "e8900000nR",
  361. ldmda_2 = "e8100000nR", ldmfa_2 = "e8100000nR",
  362. ldmdb_2 = "e9100000nR", ldmea_2 = "e9100000nR",
  363. ldmib_2 = "e9900000nR", ldmed_2 = "e9900000nR",
  364. stm_2 = "e8800000nR", stmia_2 = "e8800000nR", stmfd_2 = "e8800000nR",
  365. stmda_2 = "e8000000nR", stmfa_2 = "e8000000nR",
  366. stmdb_2 = "e9000000nR", stmea_2 = "e9000000nR",
  367. stmib_2 = "e9800000nR", stmed_2 = "e9800000nR",
  368. pop_1 = "e8bd0000R", push_1 = "e92d0000R",
  369. -- Branch instructions.
  370. b_1 = "ea000000B",
  371. bl_1 = "eb000000B",
  372. blx_1 = "e12fff30C",
  373. bx_1 = "e12fff10M",
  374. -- Miscellaneous instructions.
  375. nop_0 = "e1a00000",
  376. mrs_1 = "e10f0000D",
  377. bkpt_1 = "e1200070K", -- v5T
  378. svc_1 = "ef000000T", swi_1 = "ef000000T",
  379. ud_0 = "e7f001f0",
  380. -- NYI: Advanced SIMD and VFP instructions.
  381. -- NYI instructions, since I have no need for them right now:
  382. -- swp, swpb, strex, ldrex, strexd, ldrexd, strexb, ldrexb, strexh, ldrexh
  383. -- msr, nopv6, yield, wfe, wfi, sev, dbg, bxj, smc, srs, rfe
  384. -- cps, setend, pli, pld, pldw, clrex, dsb, dmb, isb
  385. -- stc, ldc, mcr, mcr2, mrc, mrc2, mcrr, mcrr2, mrrc, mrrc2, cdp, cdp2
  386. }
  387. -- Add mnemonics for "s" variants.
  388. do
  389. local t = {}
  390. for k,v in pairs(map_op) do
  391. if sub(v, -1) == "s" then
  392. local v2 = sub(v, 1, 2)..char(byte(v, 3)+1)..sub(v, 4, -2)
  393. t[sub(k, 1, -3).."s"..sub(k, -2)] = v2
  394. end
  395. end
  396. for k,v in pairs(t) do
  397. map_op[k] = v
  398. end
  399. end
  400. ------------------------------------------------------------------------------
  401. local function parse_gpr(expr)
  402. local tname, ovreg = match(expr, "^([%w_]+):(r1?[0-9])$")
  403. local tp = map_type[tname or expr]
  404. if tp then
  405. local reg = ovreg or tp.reg
  406. if not reg then
  407. werror("type `"..(tname or expr).."' needs a register override")
  408. end
  409. expr = reg
  410. end
  411. local r = match(expr, "^r(1?[0-9])$")
  412. if r then
  413. r = tonumber(r)
  414. if r <= 15 then return r, tp end
  415. end
  416. werror("bad register name `"..expr.."'")
  417. end
  418. local function parse_gpr_pm(expr)
  419. local pm, expr2 = match(expr, "^([+-]?)(.*)$")
  420. return parse_gpr(expr2), (pm == "-")
  421. end
  422. local function parse_reglist(reglist)
  423. reglist = match(reglist, "^{%s*([^}]*)}$")
  424. if not reglist then werror("register list expected") end
  425. local rr = 0
  426. for p in gmatch(reglist..",", "%s*([^,]*),") do
  427. local rbit = 2^parse_gpr(gsub(p, "%s+$", ""))
  428. if ((rr - (rr % rbit)) / rbit) % 2 ~= 0 then
  429. werror("duplicate register `"..p.."'")
  430. end
  431. rr = rr + rbit
  432. end
  433. return rr
  434. end
  435. local function parse_imm(imm, bits, shift, scale, signed)
  436. imm = match(imm, "^#(.*)$")
  437. if not imm then werror("expected immediate operand") end
  438. local n = tonumber(imm)
  439. if n then
  440. if n % 2^scale == 0 then
  441. n = n / 2^scale
  442. if signed then
  443. if n >= 0 then
  444. if n < 2^(bits-1) then return n*2^shift end
  445. else
  446. if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end
  447. end
  448. else
  449. if n >= 0 and n <= 2^bits-1 then return n*2^shift end
  450. end
  451. end
  452. werror("out of range immediate `"..imm.."'")
  453. else
  454. waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm)
  455. return 0
  456. end
  457. end
  458. local function parse_imm12(imm)
  459. local n = tonumber(imm)
  460. if n then
  461. local m = n
  462. for i=0,-15,-1 do
  463. if m >= 0 and m <= 255 and n % 1 == 0 then return m + (i%16) * 256 end
  464. local t = m % 4
  465. m = (m - t) / 4 + t * 2^30
  466. end
  467. werror("out of range immediate `"..imm.."'")
  468. else
  469. waction("IMM12", 0, imm)
  470. return 0
  471. end
  472. end
  473. local function parse_imm16(imm)
  474. imm = match(imm, "^#(.*)$")
  475. if not imm then werror("expected immediate operand") end
  476. local n = tonumber(imm)
  477. if n then
  478. if n >= 0 and n <= 65535 and n % 1 == 0 then
  479. local t = n % 4096
  480. return (n - t) * 16 + t
  481. end
  482. werror("out of range immediate `"..imm.."'")
  483. else
  484. waction("IMM16", 32*16, imm)
  485. return 0
  486. end
  487. end
  488. local function parse_imm_load(imm, ext)
  489. local n = tonumber(imm)
  490. if n then
  491. if ext then
  492. if n >= -255 and n <= 255 then
  493. local up = 0x00800000
  494. if n < 0 then n = -n; up = 0 end
  495. return (n-(n%16))*16+(n%16) + up
  496. end
  497. else
  498. if n >= -4095 and n <= 4095 then
  499. if n >= 0 then return n+0x00800000 end
  500. return -n
  501. end
  502. end
  503. werror("out of range immediate `"..imm.."'")
  504. else
  505. waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), imm)
  506. return 0
  507. end
  508. end
  509. local function parse_shift(shift, gprok)
  510. if shift == "rrx" then
  511. return 3 * 32
  512. else
  513. local s, s2 = match(shift, "^(%S+)%s*(.*)$")
  514. s = map_shift[s]
  515. if not s then werror("expected shift operand") end
  516. if sub(s2, 1, 1) == "#" then
  517. return parse_imm(s2, 5, 7, 0, false) + s * 32
  518. else
  519. if not gprok then werror("expected immediate shift operand") end
  520. return parse_gpr(s2) * 256 + s * 32 + 16
  521. end
  522. end
  523. end
  524. local function parse_label(label, def)
  525. local prefix = sub(label, 1, 2)
  526. -- =>label (pc label reference)
  527. if prefix == "=>" then
  528. return "PC", 0, sub(label, 3)
  529. end
  530. -- ->name (global label reference)
  531. if prefix == "->" then
  532. return "LG", map_global[sub(label, 3)]
  533. end
  534. if def then
  535. -- [1-9] (local label definition)
  536. if match(label, "^[1-9]$") then
  537. return "LG", 10+tonumber(label)
  538. end
  539. else
  540. -- [<>][1-9] (local label reference)
  541. local dir, lnum = match(label, "^([<>])([1-9])$")
  542. if dir then -- Fwd: 1-9, Bkwd: 11-19.
  543. return "LG", lnum + (dir == ">" and 0 or 10)
  544. end
  545. -- extern label (extern label reference)
  546. local extname = match(label, "^extern%s+(%S+)$")
  547. if extname then
  548. return "EXT", map_extern[extname]
  549. end
  550. end
  551. werror("bad label `"..label.."'")
  552. end
  553. local function parse_load(params, nparams, n, op)
  554. local oplo = op % 256
  555. local ext, ldrd = (oplo ~= 0), (oplo == 208)
  556. local d
  557. if (ldrd or oplo == 240) then
  558. d = ((op - (op % 4096)) / 4096) % 16
  559. if d % 2 ~= 0 then werror("odd destination register") end
  560. end
  561. local pn = params[n]
  562. local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
  563. local p2 = params[n+1]
  564. if not p1 then
  565. if not p2 then
  566. if match(pn, "^[<>=%-]") or match(pn, "^extern%s+") then
  567. local mode, n, s = parse_label(pn, false)
  568. waction("REL_"..mode, n + (ext and 0x1800 or 0x0800), s, 1)
  569. return op + 15 * 65536 + 0x01000000 + (ext and 0x00400000 or 0)
  570. end
  571. local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$")
  572. if reg and tailr ~= "" then
  573. local d, tp = parse_gpr(reg)
  574. if tp then
  575. waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12),
  576. format(tp.ctypefmt, tailr))
  577. return op + d * 65536 + 0x01000000 + (ext and 0x00400000 or 0)
  578. end
  579. end
  580. end
  581. werror("expected address operand")
  582. end
  583. if wb == "!" then op = op + 0x00200000 end
  584. if p2 then
  585. if wb == "!" then werror("bad use of '!'") end
  586. local p3 = params[n+2]
  587. op = op + parse_gpr(p1) * 65536
  588. local imm = match(p2, "^#(.*)$")
  589. if imm then
  590. local m = parse_imm_load(imm, ext)
  591. if p3 then werror("too many parameters") end
  592. op = op + m + (ext and 0x00400000 or 0)
  593. else
  594. local m, neg = parse_gpr_pm(p2)
  595. if ldrd and (m == d or m-1 == d) then werror("register conflict") end
  596. op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000)
  597. if p3 then op = op + parse_shift(p3) end
  598. end
  599. else
  600. local p1a, p2 = match(p1, "^([^,%s]*)%s*(.*)$")
  601. op = op + parse_gpr(p1a) * 65536 + 0x01000000
  602. if p2 ~= "" then
  603. local imm = match(p2, "^,%s*#(.*)$")
  604. if imm then
  605. local m = parse_imm_load(imm, ext)
  606. op = op + m + (ext and 0x00400000 or 0)
  607. else
  608. local p2a, p3 = match(p2, "^,%s*([^,%s]*)%s*,?%s*(.*)$")
  609. local m, neg = parse_gpr_pm(p2a)
  610. if ldrd and (m == d or m-1 == d) then werror("register conflict") end
  611. op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000)
  612. if p3 ~= "" then
  613. if ext then werror("too many parameters") end
  614. op = op + parse_shift(p3)
  615. end
  616. end
  617. else
  618. if wb == "!" then werror("bad use of '!'") end
  619. op = op + (ext and 0x00c00000 or 0x00800000)
  620. end
  621. end
  622. return op
  623. end
  624. ------------------------------------------------------------------------------
  625. -- Handle opcodes defined with template strings.
  626. map_op[".template__"] = function(params, template, nparams)
  627. if not params then return sub(template, 9) end
  628. local op = tonumber(sub(template, 1, 8), 16)
  629. local n = 1
  630. -- Limit number of section buffer positions used by a single dasm_put().
  631. -- A single opcode needs a maximum of 3 positions.
  632. if secpos+3 > maxsecpos then wflush() end
  633. local pos = wpos()
  634. -- Process each character.
  635. for p in gmatch(sub(template, 9), ".") do
  636. if p == "D" then
  637. op = op + parse_gpr(params[n]) * 4096; n = n + 1
  638. elseif p == "N" then
  639. op = op + parse_gpr(params[n]) * 65536; n = n + 1
  640. elseif p == "S" then
  641. op = op + parse_gpr(params[n]) * 256; n = n + 1
  642. elseif p == "M" then
  643. op = op + parse_gpr(params[n]); n = n + 1
  644. elseif p == "P" then
  645. local imm = match(params[n], "^#(.*)$")
  646. if imm then
  647. op = op + parse_imm12(imm) + 0x02000000
  648. else
  649. op = op + parse_gpr(params[n])
  650. end
  651. n = n + 1
  652. elseif p == "p" then
  653. op = op + parse_shift(params[n], true); n = n + 1
  654. elseif p == "L" then
  655. op = parse_load(params, nparams, n, op)
  656. elseif p == "B" then
  657. local mode, n, s = parse_label(params[n], false)
  658. waction("REL_"..mode, n, s, 1)
  659. elseif p == "C" then -- blx gpr vs. blx label.
  660. local p = params[n]
  661. if match(p, "^([%w_]+):(r1?[0-9])$") or match(p, "^r(1?[0-9])$") then
  662. op = op + parse_gpr(p)
  663. else
  664. if op < 0xe0000000 then werror("unconditional instruction") end
  665. local mode, n, s = parse_label(p, false)
  666. waction("REL_"..mode, n, s, 1)
  667. op = 0xfa000000
  668. end
  669. elseif p == "n" then
  670. local r, wb = match(params[n], "^([^!]*)(!?)$")
  671. op = op + parse_gpr(r) * 65536 + (wb == "!" and 0x00200000 or 0)
  672. n = n + 1
  673. elseif p == "R" then
  674. op = op + parse_reglist(params[n]); n = n + 1
  675. elseif p == "W" then
  676. op = op + parse_imm16(params[n]); n = n + 1
  677. elseif p == "v" then
  678. op = op + parse_imm(params[n], 5, 7, 0, false); n = n + 1
  679. elseif p == "w" then
  680. local imm = match(params[n], "^#(.*)$")
  681. if imm then
  682. op = op + parse_imm(params[n], 5, 7, 0, false); n = n + 1
  683. else
  684. op = op + parse_gpr(params[n]) * 256 + 16
  685. end
  686. elseif p == "X" then
  687. op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1
  688. elseif p == "K" then
  689. local imm = tonumber(match(params[n], "^#(.*)$")); n = n + 1
  690. if not imm or imm % 1 ~= 0 or imm < 0 or imm > 0xffff then
  691. werror("bad immediate operand")
  692. end
  693. local t = imm % 16
  694. op = op + (imm - t) * 16 + t
  695. elseif p == "T" then
  696. op = op + parse_imm(params[n], 24, 0, 0, false); n = n + 1
  697. elseif p == "s" then
  698. -- Ignored.
  699. else
  700. assert(false)
  701. end
  702. end
  703. wputpos(pos, op)
  704. end
  705. ------------------------------------------------------------------------------
  706. -- Pseudo-opcode to mark the position where the action list is to be emitted.
  707. map_op[".actionlist_1"] = function(params)
  708. if not params then return "cvar" end
  709. local name = params[1] -- No syntax check. You get to keep the pieces.
  710. wline(function(out) writeactions(out, name) end)
  711. end
  712. -- Pseudo-opcode to mark the position where the global enum is to be emitted.
  713. map_op[".globals_1"] = function(params)
  714. if not params then return "prefix" end
  715. local prefix = params[1] -- No syntax check. You get to keep the pieces.
  716. wline(function(out) writeglobals(out, prefix) end)
  717. end
  718. -- Pseudo-opcode to mark the position where the global names are to be emitted.
  719. map_op[".globalnames_1"] = function(params)
  720. if not params then return "cvar" end
  721. local name = params[1] -- No syntax check. You get to keep the pieces.
  722. wline(function(out) writeglobalnames(out, name) end)
  723. end
  724. -- Pseudo-opcode to mark the position where the extern names are to be emitted.
  725. map_op[".externnames_1"] = function(params)
  726. if not params then return "cvar" end
  727. local name = params[1] -- No syntax check. You get to keep the pieces.
  728. wline(function(out) writeexternnames(out, name) end)
  729. end
  730. ------------------------------------------------------------------------------
  731. -- Label pseudo-opcode (converted from trailing colon form).
  732. map_op[".label_1"] = function(params)
  733. if not params then return "[1-9] | ->global | =>pcexpr" end
  734. if secpos+1 > maxsecpos then wflush() end
  735. local mode, n, s = parse_label(params[1], true)
  736. if mode == "EXT" then werror("bad label definition") end
  737. waction("LABEL_"..mode, n, s, 1)
  738. end
  739. ------------------------------------------------------------------------------
  740. -- Pseudo-opcodes for data storage.
  741. map_op[".long_*"] = function(params)
  742. if not params then return "imm..." end
  743. for _,p in ipairs(params) do
  744. local n = tonumber(p)
  745. if not n then werror("bad immediate `"..p.."'") end
  746. if n < 0 then n = n + 2^32 end
  747. wputw(n)
  748. if secpos+2 > maxsecpos then wflush() end
  749. end
  750. end
  751. -- Alignment pseudo-opcode.
  752. map_op[".align_1"] = function(params)
  753. if not params then return "numpow2" end
  754. if secpos+1 > maxsecpos then wflush() end
  755. local align = tonumber(params[1])
  756. if align then
  757. local x = align
  758. -- Must be a power of 2 in the range (2 ... 256).
  759. for i=1,8 do
  760. x = x / 2
  761. if x == 1 then
  762. waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1.
  763. return
  764. end
  765. end
  766. end
  767. werror("bad alignment")
  768. end
  769. ------------------------------------------------------------------------------
  770. -- Pseudo-opcode for (primitive) type definitions (map to C types).
  771. map_op[".type_3"] = function(params, nparams)
  772. if not params then
  773. return nparams == 2 and "name, ctype" or "name, ctype, reg"
  774. end
  775. local name, ctype, reg = params[1], params[2], params[3]
  776. if not match(name, "^[%a_][%w_]*$") then
  777. werror("bad type name `"..name.."'")
  778. end
  779. local tp = map_type[name]
  780. if tp then
  781. werror("duplicate type `"..name.."'")
  782. end
  783. -- Add #type to defines. A bit unclean to put it in map_archdef.
  784. map_archdef["#"..name] = "sizeof("..ctype..")"
  785. -- Add new type and emit shortcut define.
  786. local num = ctypenum + 1
  787. map_type[name] = {
  788. ctype = ctype,
  789. ctypefmt = format("Dt%X(%%s)", num),
  790. reg = reg,
  791. }
  792. wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
  793. ctypenum = num
  794. end
  795. map_op[".type_2"] = map_op[".type_3"]
  796. -- Dump type definitions.
  797. local function dumptypes(out, lvl)
  798. local t = {}
  799. for name in pairs(map_type) do t[#t+1] = name end
  800. sort(t)
  801. out:write("Type definitions:\n")
  802. for _,name in ipairs(t) do
  803. local tp = map_type[name]
  804. local reg = tp.reg or ""
  805. out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
  806. end
  807. out:write("\n")
  808. end
  809. ------------------------------------------------------------------------------
  810. -- Set the current section.
  811. function _M.section(num)
  812. waction("SECTION", num)
  813. wflush(true) -- SECTION is a terminal action.
  814. end
  815. ------------------------------------------------------------------------------
  816. -- Dump architecture description.
  817. function _M.dumparch(out)
  818. out:write(format("DynASM %s version %s, released %s\n\n",
  819. _info.arch, _info.version, _info.release))
  820. dumpactions(out)
  821. end
  822. -- Dump all user defined elements.
  823. function _M.dumpdef(out, lvl)
  824. dumptypes(out, lvl)
  825. dumpglobals(out, lvl)
  826. dumpexterns(out, lvl)
  827. end
  828. ------------------------------------------------------------------------------
  829. -- Pass callbacks from/to the DynASM core.
  830. function _M.passcb(wl, we, wf, ww)
  831. wline, werror, wfatal, wwarn = wl, we, wf, ww
  832. return wflush
  833. end
  834. -- Setup the arch-specific module.
  835. function _M.setup(arch, opt)
  836. g_arch, g_opt = arch, opt
  837. end
  838. -- Merge the core maps and the arch-specific maps.
  839. function _M.mergemaps(map_coreop, map_def)
  840. setmetatable(map_op, { __index = function(t, k)
  841. local v = map_coreop[k]
  842. if v then return v end
  843. local cc = sub(k, -4, -3)
  844. local cv = map_cond[cc]
  845. if cv then
  846. local v = rawget(t, sub(k, 1, -5)..sub(k, -2))
  847. if type(v) == "string" then return format("%x%s", cv, sub(v, 2)) end
  848. end
  849. end })
  850. setmetatable(map_def, { __index = map_archdef })
  851. return map_op, map_def
  852. end
  853. return _M
  854. ------------------------------------------------------------------------------