1 ------------------------------------------------------------------------------
4 -- Copyright (C) 2005-2023 Mike Pall. All rights reserved.
5 -- See dynasm.lua for full copyright notice.
6 ------------------------------------------------------------------------------
11 description
= "DynASM ARM module",
14 release
= "2021-05-02",
19 -- Exported glue functions for the arch-specific module.
20 local _M
= { _info
= _info
}
22 -- Cache library functions.
23 local type, tonumber, pairs
, ipairs
= type, tonumber, pairs
, ipairs
24 local assert, setmetatable
, rawget = assert, setmetatable
, rawget
26 local sub
, format, byte
, char
= _s
.sub
, _s
.format, _s
.byte
, _s
.char
27 local match
, gmatch
, gsub = _s
.match
, _s
.gmatch
, _s
.gsub
28 local concat
, sort, insert
= table.concat
, table.sort, table.insert
29 local bit
= bit
or require("bit")
30 local band
, shl
, shr
, sar
= bit
.band
, bit
.lshift
, bit
.rshift
, bit
.arshift
31 local ror
, tohex
= bit
.ror
, bit
.tohex
33 -- Inherited tables and callbacks.
35 local wline
, werror
, wfatal
, wwarn
38 -- CHECK: Keep this in sync with the C code!
39 local 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",
45 -- Maximum number of section buffer positions for dasm_put().
46 -- CHECK: Keep this in sync with the C code!
47 local maxsecpos
= 25 -- Keep this low, to avoid excessively long C lines.
49 -- Action name -> action number.
51 for n
,name
in ipairs(action_names
) do
52 map_action
[name
] = n
-1
55 -- Action list buffer.
58 -- Argument list for next dasm_put(). Start with offset 0 into action list.
61 -- Current number of section buffer positions for dasm_put().
64 ------------------------------------------------------------------------------
66 -- Dump action names and numbers.
67 local 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
))
76 -- Write action list buffer as a huge static C array.
77 local function writeactions(out
, name
)
79 if nn
== 0 then nn
= 1; actlist
[0] = map_action
.STOP
end
80 out
:write("static const unsigned int ", name
, "[", nn
, "] = {\n")
82 assert(out
:write("0x", tohex(actlist
[i
]), ",\n"))
84 assert(out
:write("0x", tohex(actlist
[nn
]), "\n};\n\n"))
87 ------------------------------------------------------------------------------
89 -- Add word to action list.
90 local function wputxw(n
)
91 assert(n
>= 0 and n
<= 0xffffffff and n
% 1 == 0, "word out of range")
92 actlist
[#actlist
+1] = n
95 -- Add action to list with optional arg. Advance buffer pos, too.
96 local 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
103 -- Flush action list (intervening C code or buffer pos overflow).
104 local 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.
113 local function wputw(n
)
114 if n
<= 0x000fffff then waction("ESC") end
118 -- Reserve position for word.
119 local function wpos()
120 local pos
= #actlist
+1
125 -- Store word to reserved position.
126 local 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
135 ------------------------------------------------------------------------------
137 -- Global label name -> global label number. With auto assignment on 1st use.
138 local next_global
= 20
139 local 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
148 -- Dump global labels.
149 local function dumpglobals(out
, lvl
)
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
]))
159 -- Write global label enum.
160 local function writeglobals(out
, prefix
)
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")
167 out
:write(" ", prefix
, "_MAX\n};\n")
170 -- Write global label names.
171 local function writeglobalnames(out
, name
)
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")
178 out
:write(" (const char *)0\n};\n")
181 ------------------------------------------------------------------------------
183 -- Extern label name -> extern label number. With auto assignment on 1st use.
184 local next_extern
= 0
185 local map_extern_
= {}
186 local 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
192 map_extern_
[n
] = name
196 -- Dump extern labels.
197 local 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
]))
205 -- Write extern label names.
206 local 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")
211 out
:write(" (const char *)0\n};\n")
214 ------------------------------------------------------------------------------
216 -- Arch-specific maps.
218 -- Ext. register name -> int. name.
219 local map_archdef
= { sp
= "r13", lr
= "r14", pc
= "r15", }
221 -- Int. register name -> ext. name.
222 local map_reg_rev
= { r13
= "sp", r14
= "lr", r15
= "pc", }
224 local map_type
= {} -- Type name -> { ctype, reg }
225 local ctypenum
= 0 -- Type number (for Dt... macros).
227 -- Reverse defines for registers.
228 function _M
.revdef(s
)
229 return map_reg_rev
[s
] or s
232 local map_shift
= { lsl
= 0, lsr
= 1, asr
= 2, ror
= 3, }
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,
240 ------------------------------------------------------------------------------
242 -- Template strings for ARM instructions.
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",
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",
279 lsl_3
= "e1a00000DMws",
280 lsr_3
= "e1a00020DMws",
281 asr_3
= "e1a00040DMws",
282 ror_3
= "e1a00060DMws",
283 rrx_2
= "e1a00060DMs",
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",
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
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
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
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
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
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",
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",
418 -- Branch instructions.
424 -- Miscellaneous instructions.
427 bkpt_1
= "e1200070K", -- v5T
428 svc_1
= "ef000000T", swi_1
= "ef000000T",
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",
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",
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",
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",
483 vmrs_1
= "eef10a10D",
484 vmsr_1
= "eee10a10D",
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",
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",
511 -- NYI: Advanced SIMD instructions.
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
520 -- Add mnemonics for "s" variants.
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
529 for k
,v
in pairs(t
) do
534 ------------------------------------------------------------------------------
536 local function parse_gpr(expr
)
537 local tname
, ovreg
= match(expr
, "^([%w_]+):(r1?[0-9])$")
538 local tp
= map_type
[tname
or expr
]
540 local reg
= ovreg
or tp
.reg
542 werror("type `"..(tname
or expr
).."' needs a register override")
546 local r
= match(expr
, "^r(1?[0-9])$")
549 if r
<= 15 then return r
, tp
end
551 werror("bad register name `"..expr
.."'")
554 local function parse_gpr_pm(expr
)
555 local pm
, expr2
= match(expr
, "^([+-]?)(.*)$")
556 return parse_gpr(expr2
), (pm
== "-")
559 local function parse_vr(expr
, tp
)
560 local t
, r
= match(expr
, "^([sd])([0-9]+)$")
564 if t
== "s" then return shr(r
, 1), band(r
, 1) end
565 return band(r
, 15), shr(r
, 4)
568 werror("bad register name `"..expr
.."'")
571 local function parse_reglist(reglist
)
572 reglist
= match(reglist
, "^{%s*([^}]*)}$")
573 if not reglist
then werror("register list expected") end
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
.."'")
585 local 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
592 return shl(shr(ra
,1),12)+shl(band(ra
,1),22) + nr
594 return shl(band(ra
,15),12)+shl(shr(ra
,4),22) + nr
*2 + 0x100
597 werror("register list expected")
600 local 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
)
605 local m
= sar(n
, scale
)
606 if shl(m
, scale
) == n
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
612 if sar(m
, bits
) == 0 then return shl(m
, shift
) end
615 werror("out of range immediate `"..imm
.."'")
617 waction("IMM", (signed
and 32768 or 0)+scale
*1024+bits
*32+shift
, imm
)
622 local function parse_imm12(imm
)
623 local n
= tonumber(imm
)
627 if shr(m
, 8) == 0 then return m
+ shl(band(i
, 15), 8) end
630 werror("out of range immediate `"..imm
.."'")
632 waction("IMM12", 0, imm
)
637 local function parse_imm16(imm
)
638 imm
= match(imm
, "^#(.*)$")
639 if not imm
then werror("expected immediate operand") end
640 local n
= tonumber(imm
)
642 if shr(n
, 16) == 0 then return band(n
, 0x0fff) + shl(band(n
, 0xf000), 4) end
643 werror("out of range immediate `"..imm
.."'")
645 waction("IMM16", 32*16, imm
)
650 local function parse_imm_load(imm
, ext
)
651 local n
= tonumber(imm
)
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
660 if n
>= -4095 and n
<= 4095 then
661 if n
>= 0 then return n
+0x00800000 end
665 werror("out of range immediate `"..imm
.."'")
667 waction(ext
and "IMML8" or "IMML12", 32768 + shl(ext
and 8 or 12, 5), imm
)
672 local function parse_shift(shift
, gprok
)
673 if shift
== "rrx" then
676 local s
, s2
= match(shift
, "^(%S+)%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)
682 if not gprok
then werror("expected immediate shift operand") end
683 return shl(parse_gpr(s2
), 8) + shl(s
, 5) + 16
688 local 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)
694 -- ->name (global label reference)
695 if prefix
== "->" then
696 return "LG", map_global
[sub(label
, 3)]
699 -- [1-9] (local label definition)
700 if match(label
, "^[1-9]$") then
701 return "LG", 10+tonumber(label
)
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)
709 -- extern label (extern label reference)
710 local extname
= match(label
, "^extern%s+(%S+)$")
712 return "EXT", map_extern
[extname
]
715 werror("bad label `"..label
.."'")
718 local function parse_load(params
, nparams
, n
, op
)
719 local oplo
= band(op
, 255)
720 local ext
, ldrd
= (oplo
~= 0), (oplo
== 208)
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
727 local p1
, wb
= match(pn
, "^%[%s*(.-)%s*%](!?)$")
728 local p2
= params
[n
+1]
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)
736 local reg
, tailr
= match(pn
, "^([%w_:]+)%s*(.*)$")
737 if reg
and tailr
~= "" then
738 local d
, tp
= parse_gpr(reg
)
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)
746 werror("expected address operand")
748 if wb
== "!" then op
= op
+ 0x00200000 end
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
, "^#(.*)$")
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)
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
765 local p1a
, p2
= match(p1
, "^([^,%s]*)%s*(.*)$")
766 op
= op
+ shl(parse_gpr(p1a
), 16) + 0x01000000
768 local imm
= match(p2
, "^,%s*#(.*)$")
770 local m
= parse_imm_load(imm
, ext
)
771 op
= op
+ m
+ (ext
and 0x00400000 or 0)
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)
778 if ext
then werror("too many parameters") end
779 op
= op
+ parse_shift(p3
)
783 if wb
== "!" then werror("bad use of '!'") end
784 op
= op
+ (ext
and 0x00c00000 or 0x00800000)
790 local function parse_vload(q
)
791 local reg
, imm
= match(q
, "^%[%s*([^,%s]*)%s*(.*)%]$")
793 local d
= shl(parse_gpr(reg
), 16)
794 if imm
== "" then return d
end
795 imm
= match(imm
, "^,%s*#(.*)$")
797 local n
= tonumber(imm
)
799 if n
>= -1020 and n
<= 1020 and n
%4 == 0 then
800 return d
+ (n
>= 0 and n
/4+0x00800000 or -n
/4)
802 werror("out of range immediate `"..imm
.."'")
804 waction("IMMV8", 32768 + 32*8, imm
)
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)
814 local reg
, tailr
= match(q
, "^([%w_:]+)%s*(.*)$")
815 if reg
and tailr
~= "" then
816 local d
, tp
= parse_gpr(reg
)
818 waction("IMMV8", 32768 + 32*8, format(tp
.ctypefmt
, tailr
))
823 werror("expected address operand")
826 ------------------------------------------------------------------------------
828 -- Handle opcodes defined with template strings.
829 local function parse_template(params
, template
, nparams
, pos
)
830 local op
= tonumber(sub(template
, 1, 8), 16)
834 -- Process each character.
835 for p
in gmatch(sub(template
, 9), ".") do
838 op
= op
+ shl(parse_gpr(q
), 12); n
= n
+ 1
840 op
= op
+ shl(parse_gpr(q
), 16); n
= n
+ 1
842 op
= op
+ shl(parse_gpr(q
), 8); n
= n
+ 1
844 op
= op
+ parse_gpr(q
); n
= n
+ 1
846 local r
,h
= parse_vr(q
, vr
); op
= op
+shl(r
,12)+shl(h
,22); n
= n
+ 1
848 local r
,h
= parse_vr(q
, vr
); op
= op
+shl(r
,16)+shl(h
,7); n
= n
+ 1
850 local r
,h
= parse_vr(q
, vr
); op
= op
+r
+shl(h
,5); n
= n
+ 1
852 local imm
= match(q
, "^#(.*)$")
854 op
= op
+ parse_imm12(imm
) + 0x02000000
856 op
= op
+ parse_gpr(q
)
860 op
= op
+ parse_shift(q
, true); n
= n
+ 1
862 op
= parse_load(params
, nparams
, n
, op
)
864 op
= op
+ parse_vload(q
)
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
)
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)
882 local r
, wb
= match(q
, "^([^!]*)(!?)$")
883 op
= op
+ shl(parse_gpr(r
), 16) + (wb
== "!" and 0x00200000 or 0)
886 op
= op
+ parse_reglist(q
); n
= n
+ 1
888 op
= op
+ parse_vrlist(q
); n
= n
+ 1
890 op
= op
+ parse_imm16(q
); n
= n
+ 1
892 op
= op
+ parse_imm(q
, 5, 7, 0, false); n
= n
+ 1
894 local imm
= match(q
, "^#(.*)$")
896 op
= op
+ parse_imm(q
, 5, 7, 0, false); n
= n
+ 1
898 op
= op
+ shl(parse_gpr(q
), 8) + 16
901 op
= op
+ parse_imm(q
, 5, 16, 0, false); n
= n
+ 1
903 local imm
= tonumber(match(q
, "^#(.*)$")); n
= n
+ 1
904 if not imm
or shr(imm
, 8) ~= 0 then
905 werror("bad immediate operand")
907 op
= op
+ shl(band(imm
, 0xf0), 12) + band(imm
, 0x0f)
909 local imm
= tonumber(match(q
, "^#(.*)$")); n
= n
+ 1
910 if not imm
or shr(imm
, 16) ~= 0 then
911 werror("bad immediate operand")
913 op
= op
+ shl(band(imm
, 0xfff0), 4) + band(imm
, 0x000f)
915 op
= op
+ parse_imm(q
, 24, 0, 0, false); n
= n
+ 1
925 map_op
[".template__"] = function(params
, template
, nparams
)
926 if not params
then return template
:gsub("%x%x%x%x%x%x%x%x", "") end
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
932 local lpos
, apos
, spos
= #actlist
, #actargs
, secpos
935 for t
in gmatch(template
, "[^|]+") do
936 ok
, err
= pcall(parse_template
, params
, t
, nparams
, pos
)
937 if ok
then return end
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
949 ------------------------------------------------------------------------------
951 -- Pseudo-opcode to mark the position where the action list is to be emitted.
952 map_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)
958 -- Pseudo-opcode to mark the position where the global enum is to be emitted.
959 map_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)
965 -- Pseudo-opcode to mark the position where the global names are to be emitted.
966 map_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)
972 -- Pseudo-opcode to mark the position where the extern names are to be emitted.
973 map_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)
979 ------------------------------------------------------------------------------
981 -- Label pseudo-opcode (converted from trailing colon form).
982 map_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)
990 ------------------------------------------------------------------------------
992 -- Pseudo-opcodes for data storage.
993 map_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
1000 if secpos
+2 > maxsecpos
then wflush() end
1004 -- Alignment pseudo-opcode.
1005 map_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])
1011 -- Must be a power of 2 in the range (2 ... 256).
1015 waction("ALIGN", align
-1, nil, 1) -- Action byte is 2**n-1.
1020 werror("bad alignment")
1023 ------------------------------------------------------------------------------
1025 -- Pseudo-opcode for (primitive) type definitions (map to C types).
1026 map_op
[".type_3"] = function(params
, nparams
)
1028 return nparams
== 2 and "name, ctype" or "name, ctype, reg"
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
.."'")
1034 local tp
= map_type
[name
]
1036 werror("duplicate type `"..name
.."'")
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
1044 ctypefmt
= format("Dt%X(%%s)", num
),
1047 wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num
, ctype
))
1050 map_op
[".type_2"] = map_op
[".type_3"]
1052 -- Dump type definitions.
1053 local function dumptypes(out
, lvl
)
1055 for name
in pairs(map_type
) do t
[#t
+1] = name
end
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
))
1066 ------------------------------------------------------------------------------
1068 -- Set the current section.
1069 function _M
.section(num
)
1070 waction("SECTION", num
)
1071 wflush(true) -- SECTION is a terminal action.
1074 ------------------------------------------------------------------------------
1076 -- Dump architecture description.
1077 function _M
.dumparch(out
)
1078 out
:write(format("DynASM %s version %s, released %s\n\n",
1079 _info
.arch
, _info
.version
, _info
.release
))
1083 -- Dump all user defined elements.
1084 function _M
.dumpdef(out
, lvl
)
1086 dumpglobals(out
, lvl
)
1087 dumpexterns(out
, lvl
)
1090 ------------------------------------------------------------------------------
1092 -- Pass callbacks from/to the DynASM core.
1093 function _M
.passcb(wl
, we
, wf
, ww
)
1094 wline
, werror
, wfatal
, wwarn
= wl
, we
, wf
, ww
1098 -- Setup the arch-specific module.
1099 function _M
.setup(arch
, opt
)
1100 g_arch
, g_opt
= arch
, opt
1103 -- Merge the core maps and the arch-specific maps.
1104 function _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
]
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
)
1118 setmetatable(map_def
, { __index
= map_archdef
})
1119 return map_op
, map_def
1124 ------------------------------------------------------------------------------