1------------------------------------------------------------------------------
2-- DynASM ARM module.
3--
4-- Copyright (C) 2005-2016 Mike Pall. All rights reserved.
5-- See dynasm.lua for full copyright notice.
6------------------------------------------------------------------------------
7
8-- Module information:
9local _info = {
10  arch =	"arm",
11  description =	"DynASM ARM module",
12  version =	"1.4.0",
13  vernum =	 10400,
14  release =	"2015-10-18",
15  author =	"Mike Pall",
16  license =	"MIT",
17}
18
19-- Exported glue functions for the arch-specific module.
20local _M = { _info = _info }
21
22-- Cache library functions.
23local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs
24local assert, setmetatable, rawget = assert, setmetatable, rawget
25local _s = string
26local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
27local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub
28local concat, sort, insert = table.concat, table.sort, table.insert
29local bit = bit or require("bit")
30local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift
31local ror, tohex = bit.ror, bit.tohex
32
33-- Inherited tables and callbacks.
34local g_opt, g_arch
35local wline, werror, wfatal, wwarn
36
37-- Action name list.
38-- CHECK: Keep this in sync with the C code!
39local action_names = {
40  "STOP", "SECTION", "ESC", "REL_EXT",
41  "ALIGN", "REL_LG", "LABEL_LG",
42  "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM16", "IMML8", "IMML12", "IMMV8",
43}
44
45-- Maximum number of section buffer positions for dasm_put().
46-- CHECK: Keep this in sync with the C code!
47local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines.
48
49-- Action name -> action number.
50local map_action = {}
51for n,name in ipairs(action_names) do
52  map_action[name] = n-1
53end
54
55-- Action list buffer.
56local actlist = {}
57
58-- Argument list for next dasm_put(). Start with offset 0 into action list.
59local actargs = { 0 }
60
61-- Current number of section buffer positions for dasm_put().
62local secpos = 1
63
64------------------------------------------------------------------------------
65
66-- Dump action names and numbers.
67local function dumpactions(out)
68  out:write("DynASM encoding engine action codes:\n")
69  for n,name in ipairs(action_names) do
70    local num = map_action[name]
71    out:write(format("  %-10s %02X  %d\n", name, num, num))
72  end
73  out:write("\n")
74end
75
76-- Write action list buffer as a huge static C array.
77local function writeactions(out, name)
78  local nn = #actlist
79  if nn == 0 then nn = 1; actlist[0] = map_action.STOP end
80  out:write("static const unsigned int ", name, "[", nn, "] = {\n")
81  for i = 1,nn-1 do
82    assert(out:write("0x", tohex(actlist[i]), ",\n"))
83  end
84  assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n"))
85end
86
87------------------------------------------------------------------------------
88
89-- Add word to action list.
90local function wputxw(n)
91  assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
92  actlist[#actlist+1] = n
93end
94
95-- Add action to list with optional arg. Advance buffer pos, too.
96local function waction(action, val, a, num)
97  local w = assert(map_action[action], "bad action name `"..action.."'")
98  wputxw(w * 0x10000 + (val or 0))
99  if a then actargs[#actargs+1] = a end
100  if a or num then secpos = secpos + (num or 1) end
101end
102
103-- Flush action list (intervening C code or buffer pos overflow).
104local function wflush(term)
105  if #actlist == actargs[1] then return end -- Nothing to flush.
106  if not term then waction("STOP") end -- Terminate action list.
107  wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true)
108  actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put().
109  secpos = 1 -- The actionlist offset occupies a buffer position, too.
110end
111
112-- Put escaped word.
113local function wputw(n)
114  if n <= 0x000fffff then waction("ESC") end
115  wputxw(n)
116end
117
118-- Reserve position for word.
119local function wpos()
120  local pos = #actlist+1
121  actlist[pos] = ""
122  return pos
123end
124
125-- Store word to reserved position.
126local function wputpos(pos, n)
127  assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
128  if n <= 0x000fffff then
129    insert(actlist, pos+1, n)
130    n = map_action.ESC * 0x10000
131  end
132  actlist[pos] = n
133end
134
135------------------------------------------------------------------------------
136
137-- Global label name -> global label number. With auto assignment on 1st use.
138local next_global = 20
139local map_global = setmetatable({}, { __index = function(t, name)
140  if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end
141  local n = next_global
142  if n > 2047 then werror("too many global labels") end
143  next_global = n + 1
144  t[name] = n
145  return n
146end})
147
148-- Dump global labels.
149local function dumpglobals(out, lvl)
150  local t = {}
151  for name, n in pairs(map_global) do t[n] = name end
152  out:write("Global labels:\n")
153  for i=20,next_global-1 do
154    out:write(format("  %s\n", t[i]))
155  end
156  out:write("\n")
157end
158
159-- Write global label enum.
160local function writeglobals(out, prefix)
161  local t = {}
162  for name, n in pairs(map_global) do t[n] = name end
163  out:write("enum {\n")
164  for i=20,next_global-1 do
165    out:write("  ", prefix, t[i], ",\n")
166  end
167  out:write("  ", prefix, "_MAX\n};\n")
168end
169
170-- Write global label names.
171local function writeglobalnames(out, name)
172  local t = {}
173  for name, n in pairs(map_global) do t[n] = name end
174  out:write("static const char *const ", name, "[] = {\n")
175  for i=20,next_global-1 do
176    out:write("  \"", t[i], "\",\n")
177  end
178  out:write("  (const char *)0\n};\n")
179end
180
181------------------------------------------------------------------------------
182
183-- Extern label name -> extern label number. With auto assignment on 1st use.
184local next_extern = 0
185local map_extern_ = {}
186local map_extern = setmetatable({}, { __index = function(t, name)
187  -- No restrictions on the name for now.
188  local n = next_extern
189  if n > 2047 then werror("too many extern labels") end
190  next_extern = n + 1
191  t[name] = n
192  map_extern_[n] = name
193  return n
194end})
195
196-- Dump extern labels.
197local function dumpexterns(out, lvl)
198  out:write("Extern labels:\n")
199  for i=0,next_extern-1 do
200    out:write(format("  %s\n", map_extern_[i]))
201  end
202  out:write("\n")
203end
204
205-- Write extern label names.
206local function writeexternnames(out, name)
207  out:write("static const char *const ", name, "[] = {\n")
208  for i=0,next_extern-1 do
209    out:write("  \"", map_extern_[i], "\",\n")
210  end
211  out:write("  (const char *)0\n};\n")
212end
213
214------------------------------------------------------------------------------
215
216-- Arch-specific maps.
217
218-- Ext. register name -> int. name.
219local map_archdef = { sp = "r13", lr = "r14", pc = "r15", }
220
221-- Int. register name -> ext. name.
222local map_reg_rev = { r13 = "sp", r14 = "lr", r15 = "pc", }
223
224local map_type = {}		-- Type name -> { ctype, reg }
225local ctypenum = 0		-- Type number (for Dt... macros).
226
227-- Reverse defines for registers.
228function _M.revdef(s)
229  return map_reg_rev[s] or s
230end
231
232local map_shift = { lsl = 0, lsr = 1, asr = 2, ror = 3, }
233
234local map_cond = {
235  eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7,
236  hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14,
237  hs = 2, lo = 3,
238}
239
240------------------------------------------------------------------------------
241
242-- Template strings for ARM instructions.
243local map_op = {
244  -- Basic data processing instructions.
245  and_3 = "e0000000DNPs",
246  eor_3 = "e0200000DNPs",
247  sub_3 = "e0400000DNPs",
248  rsb_3 = "e0600000DNPs",
249  add_3 = "e0800000DNPs",
250  adc_3 = "e0a00000DNPs",
251  sbc_3 = "e0c00000DNPs",
252  rsc_3 = "e0e00000DNPs",
253  tst_2 = "e1100000NP",
254  teq_2 = "e1300000NP",
255  cmp_2 = "e1500000NP",
256  cmn_2 = "e1700000NP",
257  orr_3 = "e1800000DNPs",
258  mov_2 = "e1a00000DPs",
259  bic_3 = "e1c00000DNPs",
260  mvn_2 = "e1e00000DPs",
261
262  and_4 = "e0000000DNMps",
263  eor_4 = "e0200000DNMps",
264  sub_4 = "e0400000DNMps",
265  rsb_4 = "e0600000DNMps",
266  add_4 = "e0800000DNMps",
267  adc_4 = "e0a00000DNMps",
268  sbc_4 = "e0c00000DNMps",
269  rsc_4 = "e0e00000DNMps",
270  tst_3 = "e1100000NMp",
271  teq_3 = "e1300000NMp",
272  cmp_3 = "e1500000NMp",
273  cmn_3 = "e1700000NMp",
274  orr_4 = "e1800000DNMps",
275  mov_3 = "e1a00000DMps",
276  bic_4 = "e1c00000DNMps",
277  mvn_3 = "e1e00000DMps",
278
279  lsl_3 = "e1a00000DMws",
280  lsr_3 = "e1a00020DMws",
281  asr_3 = "e1a00040DMws",
282  ror_3 = "e1a00060DMws",
283  rrx_2 = "e1a00060DMs",
284
285  -- Multiply and multiply-accumulate.
286  mul_3 = "e0000090NMSs",
287  mla_4 = "e0200090NMSDs",
288  umaal_4 = "e0400090DNMSs",	-- v6
289  mls_4 = "e0600090DNMSs",	-- v6T2
290  umull_4 = "e0800090DNMSs",
291  umlal_4 = "e0a00090DNMSs",
292  smull_4 = "e0c00090DNMSs",
293  smlal_4 = "e0e00090DNMSs",
294
295  -- Halfword multiply and multiply-accumulate.
296  smlabb_4 = "e1000080NMSD",	-- v5TE
297  smlatb_4 = "e10000a0NMSD",	-- v5TE
298  smlabt_4 = "e10000c0NMSD",	-- v5TE
299  smlatt_4 = "e10000e0NMSD",	-- v5TE
300  smlawb_4 = "e1200080NMSD",	-- v5TE
301  smulwb_3 = "e12000a0NMS",	-- v5TE
302  smlawt_4 = "e12000c0NMSD",	-- v5TE
303  smulwt_3 = "e12000e0NMS",	-- v5TE
304  smlalbb_4 = "e1400080NMSD",	-- v5TE
305  smlaltb_4 = "e14000a0NMSD",	-- v5TE
306  smlalbt_4 = "e14000c0NMSD",	-- v5TE
307  smlaltt_4 = "e14000e0NMSD",	-- v5TE
308  smulbb_3 = "e1600080NMS",	-- v5TE
309  smultb_3 = "e16000a0NMS",	-- v5TE
310  smulbt_3 = "e16000c0NMS",	-- v5TE
311  smultt_3 = "e16000e0NMS",	-- v5TE
312
313  -- Miscellaneous data processing instructions.
314  clz_2 = "e16f0f10DM", -- v5T
315  rev_2 = "e6bf0f30DM", -- v6
316  rev16_2 = "e6bf0fb0DM", -- v6
317  revsh_2 = "e6ff0fb0DM", -- v6
318  sel_3 = "e6800fb0DNM", -- v6
319  usad8_3 = "e780f010NMS", -- v6
320  usada8_4 = "e7800010NMSD", -- v6
321  rbit_2 = "e6ff0f30DM", -- v6T2
322  movw_2 = "e3000000DW", -- v6T2
323  movt_2 = "e3400000DW", -- v6T2
324  -- Note: the X encodes width-1, not width.
325  sbfx_4 = "e7a00050DMvX", -- v6T2
326  ubfx_4 = "e7e00050DMvX", -- v6T2
327  -- Note: the X encodes the msb field, not the width.
328  bfc_3 = "e7c0001fDvX", -- v6T2
329  bfi_4 = "e7c00010DMvX", -- v6T2
330
331  -- Packing and unpacking instructions.
332  pkhbt_3 = "e6800010DNM", pkhbt_4 = "e6800010DNMv", -- v6
333  pkhtb_3 = "e6800050DNM", pkhtb_4 = "e6800050DNMv", -- v6
334  sxtab_3 = "e6a00070DNM", sxtab_4 = "e6a00070DNMv", -- v6
335  sxtab16_3 = "e6800070DNM", sxtab16_4 = "e6800070DNMv", -- v6
336  sxtah_3 = "e6b00070DNM", sxtah_4 = "e6b00070DNMv", -- v6
337  sxtb_2 = "e6af0070DM", sxtb_3 = "e6af0070DMv", -- v6
338  sxtb16_2 = "e68f0070DM", sxtb16_3 = "e68f0070DMv", -- v6
339  sxth_2 = "e6bf0070DM", sxth_3 = "e6bf0070DMv", -- v6
340  uxtab_3 = "e6e00070DNM", uxtab_4 = "e6e00070DNMv", -- v6
341  uxtab16_3 = "e6c00070DNM", uxtab16_4 = "e6c00070DNMv", -- v6
342  uxtah_3 = "e6f00070DNM", uxtah_4 = "e6f00070DNMv", -- v6
343  uxtb_2 = "e6ef0070DM", uxtb_3 = "e6ef0070DMv", -- v6
344  uxtb16_2 = "e6cf0070DM", uxtb16_3 = "e6cf0070DMv", -- v6
345  uxth_2 = "e6ff0070DM", uxth_3 = "e6ff0070DMv", -- v6
346
347  -- Saturating instructions.
348  qadd_3 = "e1000050DMN",	-- v5TE
349  qsub_3 = "e1200050DMN",	-- v5TE
350  qdadd_3 = "e1400050DMN",	-- v5TE
351  qdsub_3 = "e1600050DMN",	-- v5TE
352  -- Note: the X for ssat* encodes sat_imm-1, not sat_imm.
353  ssat_3 = "e6a00010DXM", ssat_4 = "e6a00010DXMp", -- v6
354  usat_3 = "e6e00010DXM", usat_4 = "e6e00010DXMp", -- v6
355  ssat16_3 = "e6a00f30DXM", -- v6
356  usat16_3 = "e6e00f30DXM", -- v6
357
358  -- Parallel addition and subtraction.
359  sadd16_3 = "e6100f10DNM", -- v6
360  sasx_3 = "e6100f30DNM", -- v6
361  ssax_3 = "e6100f50DNM", -- v6
362  ssub16_3 = "e6100f70DNM", -- v6
363  sadd8_3 = "e6100f90DNM", -- v6
364  ssub8_3 = "e6100ff0DNM", -- v6
365  qadd16_3 = "e6200f10DNM", -- v6
366  qasx_3 = "e6200f30DNM", -- v6
367  qsax_3 = "e6200f50DNM", -- v6
368  qsub16_3 = "e6200f70DNM", -- v6
369  qadd8_3 = "e6200f90DNM", -- v6
370  qsub8_3 = "e6200ff0DNM", -- v6
371  shadd16_3 = "e6300f10DNM", -- v6
372  shasx_3 = "e6300f30DNM", -- v6
373  shsax_3 = "e6300f50DNM", -- v6
374  shsub16_3 = "e6300f70DNM", -- v6
375  shadd8_3 = "e6300f90DNM", -- v6
376  shsub8_3 = "e6300ff0DNM", -- v6
377  uadd16_3 = "e6500f10DNM", -- v6
378  uasx_3 = "e6500f30DNM", -- v6
379  usax_3 = "e6500f50DNM", -- v6
380  usub16_3 = "e6500f70DNM", -- v6
381  uadd8_3 = "e6500f90DNM", -- v6
382  usub8_3 = "e6500ff0DNM", -- v6
383  uqadd16_3 = "e6600f10DNM", -- v6
384  uqasx_3 = "e6600f30DNM", -- v6
385  uqsax_3 = "e6600f50DNM", -- v6
386  uqsub16_3 = "e6600f70DNM", -- v6
387  uqadd8_3 = "e6600f90DNM", -- v6
388  uqsub8_3 = "e6600ff0DNM", -- v6
389  uhadd16_3 = "e6700f10DNM", -- v6
390  uhasx_3 = "e6700f30DNM", -- v6
391  uhsax_3 = "e6700f50DNM", -- v6
392  uhsub16_3 = "e6700f70DNM", -- v6
393  uhadd8_3 = "e6700f90DNM", -- v6
394  uhsub8_3 = "e6700ff0DNM", -- v6
395
396  -- Load/store instructions.
397  str_2 = "e4000000DL", str_3 = "e4000000DL", str_4 = "e4000000DL",
398  strb_2 = "e4400000DL", strb_3 = "e4400000DL", strb_4 = "e4400000DL",
399  ldr_2 = "e4100000DL", ldr_3 = "e4100000DL", ldr_4 = "e4100000DL",
400  ldrb_2 = "e4500000DL", ldrb_3 = "e4500000DL", ldrb_4 = "e4500000DL",
401  strh_2 = "e00000b0DL", strh_3 = "e00000b0DL",
402  ldrh_2 = "e01000b0DL", ldrh_3 = "e01000b0DL",
403  ldrd_2 = "e00000d0DL", ldrd_3 = "e00000d0DL", -- v5TE
404  ldrsb_2 = "e01000d0DL", ldrsb_3 = "e01000d0DL",
405  strd_2 = "e00000f0DL", strd_3 = "e00000f0DL", -- v5TE
406  ldrsh_2 = "e01000f0DL", ldrsh_3 = "e01000f0DL",
407
408  ldm_2 = "e8900000oR", ldmia_2 = "e8900000oR", ldmfd_2 = "e8900000oR",
409  ldmda_2 = "e8100000oR", ldmfa_2 = "e8100000oR",
410  ldmdb_2 = "e9100000oR", ldmea_2 = "e9100000oR",
411  ldmib_2 = "e9900000oR", ldmed_2 = "e9900000oR",
412  stm_2 = "e8800000oR", stmia_2 = "e8800000oR", stmfd_2 = "e8800000oR",
413  stmda_2 = "e8000000oR", stmfa_2 = "e8000000oR",
414  stmdb_2 = "e9000000oR", stmea_2 = "e9000000oR",
415  stmib_2 = "e9800000oR", stmed_2 = "e9800000oR",
416  pop_1 = "e8bd0000R", push_1 = "e92d0000R",
417
418  -- Branch instructions.
419  b_1 = "ea000000B",
420  bl_1 = "eb000000B",
421  blx_1 = "e12fff30C",
422  bx_1 = "e12fff10M",
423
424  -- Miscellaneous instructions.
425  nop_0 = "e1a00000",
426  mrs_1 = "e10f0000D",
427  bkpt_1 = "e1200070K", -- v5T
428  svc_1 = "ef000000T", swi_1 = "ef000000T",
429  ud_0 = "e7f001f0",
430
431  -- VFP instructions.
432  ["vadd.f32_3"] = "ee300a00dnm",
433  ["vadd.f64_3"] = "ee300b00Gdnm",
434  ["vsub.f32_3"] = "ee300a40dnm",
435  ["vsub.f64_3"] = "ee300b40Gdnm",
436  ["vmul.f32_3"] = "ee200a00dnm",
437  ["vmul.f64_3"] = "ee200b00Gdnm",
438  ["vnmul.f32_3"] = "ee200a40dnm",
439  ["vnmul.f64_3"] = "ee200b40Gdnm",
440  ["vmla.f32_3"] = "ee000a00dnm",
441  ["vmla.f64_3"] = "ee000b00Gdnm",
442  ["vmls.f32_3"] = "ee000a40dnm",
443  ["vmls.f64_3"] = "ee000b40Gdnm",
444  ["vnmla.f32_3"] = "ee100a40dnm",
445  ["vnmla.f64_3"] = "ee100b40Gdnm",
446  ["vnmls.f32_3"] = "ee100a00dnm",
447  ["vnmls.f64_3"] = "ee100b00Gdnm",
448  ["vdiv.f32_3"] = "ee800a00dnm",
449  ["vdiv.f64_3"] = "ee800b00Gdnm",
450
451  ["vabs.f32_2"] = "eeb00ac0dm",
452  ["vabs.f64_2"] = "eeb00bc0Gdm",
453  ["vneg.f32_2"] = "eeb10a40dm",
454  ["vneg.f64_2"] = "eeb10b40Gdm",
455  ["vsqrt.f32_2"] = "eeb10ac0dm",
456  ["vsqrt.f64_2"] = "eeb10bc0Gdm",
457  ["vcmp.f32_2"] = "eeb40a40dm",
458  ["vcmp.f64_2"] = "eeb40b40Gdm",
459  ["vcmpe.f32_2"] = "eeb40ac0dm",
460  ["vcmpe.f64_2"] = "eeb40bc0Gdm",
461  ["vcmpz.f32_1"] = "eeb50a40d",
462  ["vcmpz.f64_1"] = "eeb50b40Gd",
463  ["vcmpze.f32_1"] = "eeb50ac0d",
464  ["vcmpze.f64_1"] = "eeb50bc0Gd",
465
466  vldr_2 = "ed100a00dl|ed100b00Gdl",
467  vstr_2 = "ed000a00dl|ed000b00Gdl",
468  vldm_2 = "ec900a00or",
469  vldmia_2 = "ec900a00or",
470  vldmdb_2 = "ed100a00or",
471  vpop_1 = "ecbd0a00r",
472  vstm_2 = "ec800a00or",
473  vstmia_2 = "ec800a00or",
474  vstmdb_2 = "ed000a00or",
475  vpush_1 = "ed2d0a00r",
476
477  ["vmov.f32_2"] = "eeb00a40dm|eeb00a00dY",	-- #imm is VFPv3 only
478  ["vmov.f64_2"] = "eeb00b40Gdm|eeb00b00GdY",	-- #imm is VFPv3 only
479  vmov_2 = "ee100a10Dn|ee000a10nD",
480  vmov_3 = "ec500a10DNm|ec400a10mDN|ec500b10GDNm|ec400b10GmDN",
481
482  vmrs_0 = "eef1fa10",
483  vmrs_1 = "eef10a10D",
484  vmsr_1 = "eee10a10D",
485
486  ["vcvt.s32.f32_2"] = "eebd0ac0dm",
487  ["vcvt.s32.f64_2"] = "eebd0bc0dGm",
488  ["vcvt.u32.f32_2"] = "eebc0ac0dm",
489  ["vcvt.u32.f64_2"] = "eebc0bc0dGm",
490  ["vcvtr.s32.f32_2"] = "eebd0a40dm",
491  ["vcvtr.s32.f64_2"] = "eebd0b40dGm",
492  ["vcvtr.u32.f32_2"] = "eebc0a40dm",
493  ["vcvtr.u32.f64_2"] = "eebc0b40dGm",
494  ["vcvt.f32.s32_2"] = "eeb80ac0dm",
495  ["vcvt.f64.s32_2"] = "eeb80bc0GdFm",
496  ["vcvt.f32.u32_2"] = "eeb80a40dm",
497  ["vcvt.f64.u32_2"] = "eeb80b40GdFm",
498  ["vcvt.f32.f64_2"] = "eeb70bc0dGm",
499  ["vcvt.f64.f32_2"] = "eeb70ac0GdFm",
500
501  -- VFPv4 only:
502  ["vfma.f32_3"] = "eea00a00dnm",
503  ["vfma.f64_3"] = "eea00b00Gdnm",
504  ["vfms.f32_3"] = "eea00a40dnm",
505  ["vfms.f64_3"] = "eea00b40Gdnm",
506  ["vfnma.f32_3"] = "ee900a40dnm",
507  ["vfnma.f64_3"] = "ee900b40Gdnm",
508  ["vfnms.f32_3"] = "ee900a00dnm",
509  ["vfnms.f64_3"] = "ee900b00Gdnm",
510
511  -- NYI: Advanced SIMD instructions.
512
513  -- NYI: I have no need for these instructions right now:
514  -- swp, swpb, strex, ldrex, strexd, ldrexd, strexb, ldrexb, strexh, ldrexh
515  -- msr, nopv6, yield, wfe, wfi, sev, dbg, bxj, smc, srs, rfe
516  -- cps, setend, pli, pld, pldw, clrex, dsb, dmb, isb
517  -- stc, ldc, mcr, mcr2, mrc, mrc2, mcrr, mcrr2, mrrc, mrrc2, cdp, cdp2
518}
519
520-- Add mnemonics for "s" variants.
521do
522  local t = {}
523  for k,v in pairs(map_op) do
524    if sub(v, -1) == "s" then
525      local v2 = sub(v, 1, 2)..char(byte(v, 3)+1)..sub(v, 4, -2)
526      t[sub(k, 1, -3).."s"..sub(k, -2)] = v2
527    end
528  end
529  for k,v in pairs(t) do
530    map_op[k] = v
531  end
532end
533
534------------------------------------------------------------------------------
535
536local function parse_gpr(expr)
537  local tname, ovreg = match(expr, "^([%w_]+):(r1?[0-9])$")
538  local tp = map_type[tname or expr]
539  if tp then
540    local reg = ovreg or tp.reg
541    if not reg then
542      werror("type `"..(tname or expr).."' needs a register override")
543    end
544    expr = reg
545  end
546  local r = match(expr, "^r(1?[0-9])$")
547  if r then
548    r = tonumber(r)
549    if r <= 15 then return r, tp end
550  end
551  werror("bad register name `"..expr.."'")
552end
553
554local function parse_gpr_pm(expr)
555  local pm, expr2 = match(expr, "^([+-]?)(.*)$")
556  return parse_gpr(expr2), (pm == "-")
557end
558
559local function parse_vr(expr, tp)
560  local t, r = match(expr, "^([sd])([0-9]+)$")
561  if t == tp then
562    r = tonumber(r)
563    if r <= 31 then
564      if t == "s" then return shr(r, 1), band(r, 1) end
565      return band(r, 15), shr(r, 4)
566    end
567  end
568  werror("bad register name `"..expr.."'")
569end
570
571local function parse_reglist(reglist)
572  reglist = match(reglist, "^{%s*([^}]*)}$")
573  if not reglist then werror("register list expected") end
574  local rr = 0
575  for p in gmatch(reglist..",", "%s*([^,]*),") do
576    local rbit = shl(1, parse_gpr(gsub(p, "%s+$", "")))
577    if band(rr, rbit) ~= 0 then
578      werror("duplicate register `"..p.."'")
579    end
580    rr = rr + rbit
581  end
582  return rr
583end
584
585local function parse_vrlist(reglist)
586  local ta, ra, tb, rb = match(reglist,
587			   "^{%s*([sd])([0-9]+)%s*%-%s*([sd])([0-9]+)%s*}$")
588  ra, rb = tonumber(ra), tonumber(rb)
589  if ta and ta == tb and ra and rb and ra <= 31 and rb <= 31 and ra <= rb then
590    local nr = rb+1 - ra
591    if ta == "s" then
592      return shl(shr(ra,1),12)+shl(band(ra,1),22) + nr
593    else
594      return shl(band(ra,15),12)+shl(shr(ra,4),22) + nr*2 + 0x100
595    end
596  end
597  werror("register list expected")
598end
599
600local function parse_imm(imm, bits, shift, scale, signed)
601  imm = match(imm, "^#(.*)$")
602  if not imm then werror("expected immediate operand") end
603  local n = tonumber(imm)
604  if n then
605    local m = sar(n, scale)
606    if shl(m, scale) == n then
607      if signed then
608	local s = sar(m, bits-1)
609	if s == 0 then return shl(m, shift)
610	elseif s == -1 then return shl(m + shl(1, bits), shift) end
611      else
612	if sar(m, bits) == 0 then return shl(m, shift) end
613      end
614    end
615    werror("out of range immediate `"..imm.."'")
616  else
617    waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm)
618    return 0
619  end
620end
621
622local function parse_imm12(imm)
623  local n = tonumber(imm)
624  if n then
625    local m = band(n)
626    for i=0,-15,-1 do
627      if shr(m, 8) == 0 then return m + shl(band(i, 15), 8) end
628      m = ror(m, 2)
629    end
630    werror("out of range immediate `"..imm.."'")
631  else
632    waction("IMM12", 0, imm)
633    return 0
634  end
635end
636
637local function parse_imm16(imm)
638  imm = match(imm, "^#(.*)$")
639  if not imm then werror("expected immediate operand") end
640  local n = tonumber(imm)
641  if n then
642    if shr(n, 16) == 0 then return band(n, 0x0fff) + shl(band(n, 0xf000), 4) end
643    werror("out of range immediate `"..imm.."'")
644  else
645    waction("IMM16", 32*16, imm)
646    return 0
647  end
648end
649
650local function parse_imm_load(imm, ext)
651  local n = tonumber(imm)
652  if n then
653    if ext then
654      if n >= -255 and n <= 255 then
655	local up = 0x00800000
656	if n < 0 then n = -n; up = 0 end
657	return shl(band(n, 0xf0), 4) + band(n, 0x0f) + up
658      end
659    else
660      if n >= -4095 and n <= 4095 then
661	if n >= 0 then return n+0x00800000 end
662	return -n
663      end
664    end
665    werror("out of range immediate `"..imm.."'")
666  else
667    waction(ext and "IMML8" or "IMML12", 32768 + shl(ext and 8 or 12, 5), imm)
668    return 0
669  end
670end
671
672local function parse_shift(shift, gprok)
673  if shift == "rrx" then
674    return 3 * 32
675  else
676    local s, s2 = match(shift, "^(%S+)%s*(.*)$")
677    s = map_shift[s]
678    if not s then werror("expected shift operand") end
679    if sub(s2, 1, 1) == "#" then
680      return parse_imm(s2, 5, 7, 0, false) + shl(s, 5)
681    else
682      if not gprok then werror("expected immediate shift operand") end
683      return shl(parse_gpr(s2), 8) + shl(s, 5) + 16
684    end
685  end
686end
687
688local function parse_label(label, def)
689  local prefix = sub(label, 1, 2)
690  -- =>label (pc label reference)
691  if prefix == "=>" then
692    return "PC", 0, sub(label, 3)
693  end
694  -- ->name (global label reference)
695  if prefix == "->" then
696    return "LG", map_global[sub(label, 3)]
697  end
698  if def then
699    -- [1-9] (local label definition)
700    if match(label, "^[1-9]$") then
701      return "LG", 10+tonumber(label)
702    end
703  else
704    -- [<>][1-9] (local label reference)
705    local dir, lnum = match(label, "^([<>])([1-9])$")
706    if dir then -- Fwd: 1-9, Bkwd: 11-19.
707      return "LG", lnum + (dir == ">" and 0 or 10)
708    end
709    -- extern label (extern label reference)
710    local extname = match(label, "^extern%s+(%S+)$")
711    if extname then
712      return "EXT", map_extern[extname]
713    end
714  end
715  werror("bad label `"..label.."'")
716end
717
718local function parse_load(params, nparams, n, op)
719  local oplo = band(op, 255)
720  local ext, ldrd = (oplo ~= 0), (oplo == 208)
721  local d
722  if (ldrd or oplo == 240) then
723    d = band(shr(op, 12), 15)
724    if band(d, 1) ~= 0 then werror("odd destination register") end
725  end
726  local pn = params[n]
727  local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
728  local p2 = params[n+1]
729  if not p1 then
730    if not p2 then
731      if match(pn, "^[<>=%-]") or match(pn, "^extern%s+") then
732	local mode, n, s = parse_label(pn, false)
733	waction("REL_"..mode, n + (ext and 0x1800 or 0x0800), s, 1)
734	return op + 15 * 65536 + 0x01000000 + (ext and 0x00400000 or 0)
735      end
736      local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$")
737      if reg and tailr ~= "" then
738	local d, tp = parse_gpr(reg)
739	if tp then
740	  waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12),
741		  format(tp.ctypefmt, tailr))
742	  return op + shl(d, 16) + 0x01000000 + (ext and 0x00400000 or 0)
743	end
744      end
745    end
746    werror("expected address operand")
747  end
748  if wb == "!" then op = op + 0x00200000 end
749  if p2 then
750    if wb == "!" then werror("bad use of '!'") end
751    local p3 = params[n+2]
752    op = op + shl(parse_gpr(p1), 16)
753    local imm = match(p2, "^#(.*)$")
754    if imm then
755      local m = parse_imm_load(imm, ext)
756      if p3 then werror("too many parameters") end
757      op = op + m + (ext and 0x00400000 or 0)
758    else
759      local m, neg = parse_gpr_pm(p2)
760      if ldrd and (m == d or m-1 == d) then werror("register conflict") end
761      op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000)
762      if p3 then op = op + parse_shift(p3) end
763    end
764  else
765    local p1a, p2 = match(p1, "^([^,%s]*)%s*(.*)$")
766    op = op + shl(parse_gpr(p1a), 16) + 0x01000000
767    if p2 ~= "" then
768      local imm = match(p2, "^,%s*#(.*)$")
769      if imm then
770	local m = parse_imm_load(imm, ext)
771	op = op + m + (ext and 0x00400000 or 0)
772      else
773	local p2a, p3 = match(p2, "^,%s*([^,%s]*)%s*,?%s*(.*)$")
774	local m, neg = parse_gpr_pm(p2a)
775	if ldrd and (m == d or m-1 == d) then werror("register conflict") end
776	op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000)
777	if p3 ~= "" then
778	  if ext then werror("too many parameters") end
779	  op = op + parse_shift(p3)
780	end
781      end
782    else
783      if wb == "!" then werror("bad use of '!'") end
784      op = op + (ext and 0x00c00000 or 0x00800000)
785    end
786  end
787  return op
788end
789
790local function parse_vload(q)
791  local reg, imm = match(q, "^%[%s*([^,%s]*)%s*(.*)%]$")
792  if reg then
793    local d = shl(parse_gpr(reg), 16)
794    if imm == "" then return d end
795    imm = match(imm, "^,%s*#(.*)$")
796    if imm then
797      local n = tonumber(imm)
798      if n then
799	if n >= -1020 and n <= 1020 and n%4 == 0 then
800	  return d + (n >= 0 and n/4+0x00800000 or -n/4)
801	end
802	werror("out of range immediate `"..imm.."'")
803      else
804	waction("IMMV8", 32768 + 32*8, imm)
805	return d
806      end
807    end
808  else
809    if match(q, "^[<>=%-]") or match(q, "^extern%s+") then
810      local mode, n, s = parse_label(q, false)
811      waction("REL_"..mode, n + 0x2800, s, 1)
812      return 15 * 65536
813    end
814    local reg, tailr = match(q, "^([%w_:]+)%s*(.*)$")
815    if reg and tailr ~= "" then
816      local d, tp = parse_gpr(reg)
817      if tp then
818	waction("IMMV8", 32768 + 32*8, format(tp.ctypefmt, tailr))
819	return shl(d, 16)
820      end
821    end
822  end
823  werror("expected address operand")
824end
825
826------------------------------------------------------------------------------
827
828-- Handle opcodes defined with template strings.
829local function parse_template(params, template, nparams, pos)
830  local op = tonumber(sub(template, 1, 8), 16)
831  local n = 1
832  local vr = "s"
833
834  -- Process each character.
835  for p in gmatch(sub(template, 9), ".") do
836    local q = params[n]
837    if p == "D" then
838      op = op + shl(parse_gpr(q), 12); n = n + 1
839    elseif p == "N" then
840      op = op + shl(parse_gpr(q), 16); n = n + 1
841    elseif p == "S" then
842      op = op + shl(parse_gpr(q), 8); n = n + 1
843    elseif p == "M" then
844      op = op + parse_gpr(q); n = n + 1
845    elseif p == "d" then
846      local r,h = parse_vr(q, vr); op = op+shl(r,12)+shl(h,22); n = n + 1
847    elseif p == "n" then
848      local r,h = parse_vr(q, vr); op = op+shl(r,16)+shl(h,7); n = n + 1
849    elseif p == "m" then
850      local r,h = parse_vr(q, vr); op = op+r+shl(h,5); n = n + 1
851    elseif p == "P" then
852      local imm = match(q, "^#(.*)$")
853      if imm then
854	op = op + parse_imm12(imm) + 0x02000000
855      else
856	op = op + parse_gpr(q)
857      end
858      n = n + 1
859    elseif p == "p" then
860      op = op + parse_shift(q, true); n = n + 1
861    elseif p == "L" then
862      op = parse_load(params, nparams, n, op)
863    elseif p == "l" then
864      op = op + parse_vload(q)
865    elseif p == "B" then
866      local mode, n, s = parse_label(q, false)
867      waction("REL_"..mode, n, s, 1)
868    elseif p == "C" then -- blx gpr vs. blx label.
869      if match(q, "^([%w_]+):(r1?[0-9])$") or match(q, "^r(1?[0-9])$") then
870	op = op + parse_gpr(q)
871      else
872	if op < 0xe0000000 then werror("unconditional instruction") end
873	local mode, n, s = parse_label(q, false)
874	waction("REL_"..mode, n, s, 1)
875	op = 0xfa000000
876      end
877    elseif p == "F" then
878      vr = "s"
879    elseif p == "G" then
880      vr = "d"
881    elseif p == "o" then
882      local r, wb = match(q, "^([^!]*)(!?)$")
883      op = op + shl(parse_gpr(r), 16) + (wb == "!" and 0x00200000 or 0)
884      n = n + 1
885    elseif p == "R" then
886      op = op + parse_reglist(q); n = n + 1
887    elseif p == "r" then
888      op = op + parse_vrlist(q); n = n + 1
889    elseif p == "W" then
890      op = op + parse_imm16(q); n = n + 1
891    elseif p == "v" then
892      op = op + parse_imm(q, 5, 7, 0, false); n = n + 1
893    elseif p == "w" then
894      local imm = match(q, "^#(.*)$")
895      if imm then
896	op = op + parse_imm(q, 5, 7, 0, false); n = n + 1
897      else
898	op = op + shl(parse_gpr(q), 8) + 16
899      end
900    elseif p == "X" then
901      op = op + parse_imm(q, 5, 16, 0, false); n = n + 1
902    elseif p == "Y" then
903      local imm = tonumber(match(q, "^#(.*)$")); n = n + 1
904      if not imm or shr(imm, 8) ~= 0 then
905	werror("bad immediate operand")
906      end
907      op = op + shl(band(imm, 0xf0), 12) + band(imm, 0x0f)
908    elseif p == "K" then
909      local imm = tonumber(match(q, "^#(.*)$")); n = n + 1
910      if not imm or shr(imm, 16) ~= 0 then
911	werror("bad immediate operand")
912      end
913      op = op + shl(band(imm, 0xfff0), 4) + band(imm, 0x000f)
914    elseif p == "T" then
915      op = op + parse_imm(q, 24, 0, 0, false); n = n + 1
916    elseif p == "s" then
917      -- Ignored.
918    else
919      assert(false)
920    end
921  end
922  wputpos(pos, op)
923end
924
925map_op[".template__"] = function(params, template, nparams)
926  if not params then return template:gsub("%x%x%x%x%x%x%x%x", "") end
927
928  -- Limit number of section buffer positions used by a single dasm_put().
929  -- A single opcode needs a maximum of 3 positions.
930  if secpos+3 > maxsecpos then wflush() end
931  local pos = wpos()
932  local lpos, apos, spos = #actlist, #actargs, secpos
933
934  local ok, err
935  for t in gmatch(template, "[^|]+") do
936    ok, err = pcall(parse_template, params, t, nparams, pos)
937    if ok then return end
938    secpos = spos
939    actlist[lpos+1] = nil
940    actlist[lpos+2] = nil
941    actlist[lpos+3] = nil
942    actargs[apos+1] = nil
943    actargs[apos+2] = nil
944    actargs[apos+3] = nil
945  end
946  error(err, 0)
947end
948
949------------------------------------------------------------------------------
950
951-- Pseudo-opcode to mark the position where the action list is to be emitted.
952map_op[".actionlist_1"] = function(params)
953  if not params then return "cvar" end
954  local name = params[1] -- No syntax check. You get to keep the pieces.
955  wline(function(out) writeactions(out, name) end)
956end
957
958-- Pseudo-opcode to mark the position where the global enum is to be emitted.
959map_op[".globals_1"] = function(params)
960  if not params then return "prefix" end
961  local prefix = params[1] -- No syntax check. You get to keep the pieces.
962  wline(function(out) writeglobals(out, prefix) end)
963end
964
965-- Pseudo-opcode to mark the position where the global names are to be emitted.
966map_op[".globalnames_1"] = function(params)
967  if not params then return "cvar" end
968  local name = params[1] -- No syntax check. You get to keep the pieces.
969  wline(function(out) writeglobalnames(out, name) end)
970end
971
972-- Pseudo-opcode to mark the position where the extern names are to be emitted.
973map_op[".externnames_1"] = function(params)
974  if not params then return "cvar" end
975  local name = params[1] -- No syntax check. You get to keep the pieces.
976  wline(function(out) writeexternnames(out, name) end)
977end
978
979------------------------------------------------------------------------------
980
981-- Label pseudo-opcode (converted from trailing colon form).
982map_op[".label_1"] = function(params)
983  if not params then return "[1-9] | ->global | =>pcexpr" end
984  if secpos+1 > maxsecpos then wflush() end
985  local mode, n, s = parse_label(params[1], true)
986  if mode == "EXT" then werror("bad label definition") end
987  waction("LABEL_"..mode, n, s, 1)
988end
989
990------------------------------------------------------------------------------
991
992-- Pseudo-opcodes for data storage.
993map_op[".long_*"] = function(params)
994  if not params then return "imm..." end
995  for _,p in ipairs(params) do
996    local n = tonumber(p)
997    if not n then werror("bad immediate `"..p.."'") end
998    if n < 0 then n = n + 2^32 end
999    wputw(n)
1000    if secpos+2 > maxsecpos then wflush() end
1001  end
1002end
1003
1004-- Alignment pseudo-opcode.
1005map_op[".align_1"] = function(params)
1006  if not params then return "numpow2" end
1007  if secpos+1 > maxsecpos then wflush() end
1008  local align = tonumber(params[1])
1009  if align then
1010    local x = align
1011    -- Must be a power of 2 in the range (2 ... 256).
1012    for i=1,8 do
1013      x = x / 2
1014      if x == 1 then
1015	waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1.
1016	return
1017      end
1018    end
1019  end
1020  werror("bad alignment")
1021end
1022
1023------------------------------------------------------------------------------
1024
1025-- Pseudo-opcode for (primitive) type definitions (map to C types).
1026map_op[".type_3"] = function(params, nparams)
1027  if not params then
1028    return nparams == 2 and "name, ctype" or "name, ctype, reg"
1029  end
1030  local name, ctype, reg = params[1], params[2], params[3]
1031  if not match(name, "^[%a_][%w_]*$") then
1032    werror("bad type name `"..name.."'")
1033  end
1034  local tp = map_type[name]
1035  if tp then
1036    werror("duplicate type `"..name.."'")
1037  end
1038  -- Add #type to defines. A bit unclean to put it in map_archdef.
1039  map_archdef["#"..name] = "sizeof("..ctype..")"
1040  -- Add new type and emit shortcut define.
1041  local num = ctypenum + 1
1042  map_type[name] = {
1043    ctype = ctype,
1044    ctypefmt = format("Dt%X(%%s)", num),
1045    reg = reg,
1046  }
1047  wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
1048  ctypenum = num
1049end
1050map_op[".type_2"] = map_op[".type_3"]
1051
1052-- Dump type definitions.
1053local function dumptypes(out, lvl)
1054  local t = {}
1055  for name in pairs(map_type) do t[#t+1] = name end
1056  sort(t)
1057  out:write("Type definitions:\n")
1058  for _,name in ipairs(t) do
1059    local tp = map_type[name]
1060    local reg = tp.reg or ""
1061    out:write(format("  %-20s %-20s %s\n", name, tp.ctype, reg))
1062  end
1063  out:write("\n")
1064end
1065
1066------------------------------------------------------------------------------
1067
1068-- Set the current section.
1069function _M.section(num)
1070  waction("SECTION", num)
1071  wflush(true) -- SECTION is a terminal action.
1072end
1073
1074------------------------------------------------------------------------------
1075
1076-- Dump architecture description.
1077function _M.dumparch(out)
1078  out:write(format("DynASM %s version %s, released %s\n\n",
1079    _info.arch, _info.version, _info.release))
1080  dumpactions(out)
1081end
1082
1083-- Dump all user defined elements.
1084function _M.dumpdef(out, lvl)
1085  dumptypes(out, lvl)
1086  dumpglobals(out, lvl)
1087  dumpexterns(out, lvl)
1088end
1089
1090------------------------------------------------------------------------------
1091
1092-- Pass callbacks from/to the DynASM core.
1093function _M.passcb(wl, we, wf, ww)
1094  wline, werror, wfatal, wwarn = wl, we, wf, ww
1095  return wflush
1096end
1097
1098-- Setup the arch-specific module.
1099function _M.setup(arch, opt)
1100  g_arch, g_opt = arch, opt
1101end
1102
1103-- Merge the core maps and the arch-specific maps.
1104function _M.mergemaps(map_coreop, map_def)
1105  setmetatable(map_op, { __index = function(t, k)
1106    local v = map_coreop[k]
1107    if v then return v end
1108    local k1, cc, k2 = match(k, "^(.-)(..)([._].*)$")
1109    local cv = map_cond[cc]
1110    if cv then
1111      local v = rawget(t, k1..k2)
1112      if type(v) == "string" then
1113	local scv = format("%x", cv)
1114	return gsub(scv..sub(v, 2), "|e", "|"..scv)
1115      end
1116    end
1117  end })
1118  setmetatable(map_def, { __index = map_archdef })
1119  return map_op, map_def
1120end
1121
1122return _M
1123
1124------------------------------------------------------------------------------
1125
1126