From Lua 5.2: debug.getupvalue() returns "" for C function upvalues.
[luajit-2.0/celess22.git] / dynasm / dasm_ppc.lua
blob020ef0d978b0f79916f58fdc38dbaf8c7359c1cc
1 ------------------------------------------------------------------------------
2 -- DynASM PPC module.
3 --
4 -- Copyright (C) 2005-2012 Mike Pall. All rights reserved.
5 -- See dynasm.lua for full copyright notice.
6 ------------------------------------------------------------------------------
8 -- Module information:
9 local _info = {
10 arch = "ppc",
11 description = "DynASM PPC module",
12 version = "1.3.0",
13 vernum = 10300,
14 release = "2011-05-05",
15 author = "Mike Pall",
16 license = "MIT",
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 = assert, setmetatable
25 local _s = string
26 local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
27 local match, gmatch = _s.match, _s.gmatch
28 local concat, sort = table.concat, table.sort
29 local bit = bit or require("bit")
30 local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift
31 local tohex = bit.tohex
33 -- Inherited tables and callbacks.
34 local g_opt, g_arch
35 local wline, werror, wfatal, wwarn
37 -- Action name list.
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",
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.
50 local map_action = {}
51 for n,name in ipairs(action_names) do
52 map_action[name] = n-1
53 end
55 -- Action list buffer.
56 local actlist = {}
58 -- Argument list for next dasm_put(). Start with offset 0 into action list.
59 local actargs = { 0 }
61 -- Current number of section buffer positions for dasm_put().
62 local secpos = 1
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))
72 end
73 out:write("\n")
74 end
76 -- Write action list buffer as a huge static C array.
77 local 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"))
85 end
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
93 end
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.
112 -- Put escaped word.
113 local function wputw(n)
114 if n <= 0xffffff then waction("ESC") end
115 wputxw(n)
118 -- Reserve position for word.
119 local function wpos()
120 local pos = #actlist+1
121 actlist[pos] = ""
122 return pos
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 actlist[pos] = n
131 ------------------------------------------------------------------------------
133 -- Global label name -> global label number. With auto assignment on 1st use.
134 local next_global = 20
135 local map_global = setmetatable({}, { __index = function(t, name)
136 if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end
137 local n = next_global
138 if n > 2047 then werror("too many global labels") end
139 next_global = n + 1
140 t[name] = n
141 return n
142 end})
144 -- Dump global labels.
145 local function dumpglobals(out, lvl)
146 local t = {}
147 for name, n in pairs(map_global) do t[n] = name end
148 out:write("Global labels:\n")
149 for i=20,next_global-1 do
150 out:write(format(" %s\n", t[i]))
152 out:write("\n")
155 -- Write global label enum.
156 local function writeglobals(out, prefix)
157 local t = {}
158 for name, n in pairs(map_global) do t[n] = name end
159 out:write("enum {\n")
160 for i=20,next_global-1 do
161 out:write(" ", prefix, t[i], ",\n")
163 out:write(" ", prefix, "_MAX\n};\n")
166 -- Write global label names.
167 local function writeglobalnames(out, name)
168 local t = {}
169 for name, n in pairs(map_global) do t[n] = name end
170 out:write("static const char *const ", name, "[] = {\n")
171 for i=20,next_global-1 do
172 out:write(" \"", t[i], "\",\n")
174 out:write(" (const char *)0\n};\n")
177 ------------------------------------------------------------------------------
179 -- Extern label name -> extern label number. With auto assignment on 1st use.
180 local next_extern = 0
181 local map_extern_ = {}
182 local map_extern = setmetatable({}, { __index = function(t, name)
183 -- No restrictions on the name for now.
184 local n = next_extern
185 if n > 2047 then werror("too many extern labels") end
186 next_extern = n + 1
187 t[name] = n
188 map_extern_[n] = name
189 return n
190 end})
192 -- Dump extern labels.
193 local function dumpexterns(out, lvl)
194 out:write("Extern labels:\n")
195 for i=0,next_extern-1 do
196 out:write(format(" %s\n", map_extern_[i]))
198 out:write("\n")
201 -- Write extern label names.
202 local function writeexternnames(out, name)
203 out:write("static const char *const ", name, "[] = {\n")
204 for i=0,next_extern-1 do
205 out:write(" \"", map_extern_[i], "\",\n")
207 out:write(" (const char *)0\n};\n")
210 ------------------------------------------------------------------------------
212 -- Arch-specific maps.
213 local map_archdef = { sp = "r1" } -- Ext. register name -> int. name.
215 local map_type = {} -- Type name -> { ctype, reg }
216 local ctypenum = 0 -- Type number (for Dt... macros).
218 -- Reverse defines for registers.
219 function _M.revdef(s)
220 if s == "r1" then return "sp" end
221 return s
224 local map_cond = {
225 lt = 0, gt = 1, eq = 2, so = 3,
226 ge = 4, le = 5, ne = 6, ns = 7,
229 ------------------------------------------------------------------------------
231 -- Template strings for PPC instructions.
232 local map_op = {
233 tdi_3 = "08000000ARI",
234 twi_3 = "0c000000ARI",
235 mulli_3 = "1c000000RRI",
236 subfic_3 = "20000000RRI",
237 cmplwi_3 = "28000000XRU",
238 cmplwi_2 = "28000000-RU",
239 cmpldi_3 = "28200000XRU",
240 cmpldi_2 = "28200000-RU",
241 cmpwi_3 = "2c000000XRI",
242 cmpwi_2 = "2c000000-RI",
243 cmpdi_3 = "2c200000XRI",
244 cmpdi_2 = "2c200000-RI",
245 addic_3 = "30000000RRI",
246 ["addic._3"] = "34000000RRI",
247 addi_3 = "38000000RR0I",
248 li_2 = "38000000RI",
249 la_2 = "38000000RD",
250 addis_3 = "3c000000RR0I",
251 lis_2 = "3c000000RI",
252 lus_2 = "3c000000RU",
253 bc_3 = "40000000AAK",
254 bcl_3 = "40000001AAK",
255 bdnz_1 = "42000000K",
256 bdz_1 = "42400000K",
257 sc_0 = "44000000",
258 b_1 = "48000000J",
259 bl_1 = "48000001J",
260 rlwimi_5 = "50000000RR~AAA.",
261 rlwinm_5 = "54000000RR~AAA.",
262 rlwnm_5 = "5c000000RR~RAA.",
263 ori_3 = "60000000RR~U",
264 nop_0 = "60000000",
265 oris_3 = "64000000RR~U",
266 xori_3 = "68000000RR~U",
267 xoris_3 = "6c000000RR~U",
268 ["andi._3"] = "70000000RR~U",
269 ["andis._3"] = "74000000RR~U",
270 lwz_2 = "80000000RD",
271 lwzu_2 = "84000000RD",
272 lbz_2 = "88000000RD",
273 lbzu_2 = "8c000000RD",
274 stw_2 = "90000000RD",
275 stwu_2 = "94000000RD",
276 stb_2 = "98000000RD",
277 stbu_2 = "9c000000RD",
278 lhz_2 = "a0000000RD",
279 lhzu_2 = "a4000000RD",
280 lha_2 = "a8000000RD",
281 lhau_2 = "ac000000RD",
282 sth_2 = "b0000000RD",
283 sthu_2 = "b4000000RD",
284 lmw_2 = "b8000000RD",
285 stmw_2 = "bc000000RD",
286 lfs_2 = "c0000000FD",
287 lfsu_2 = "c4000000FD",
288 lfd_2 = "c8000000FD",
289 lfdu_2 = "cc000000FD",
290 stfs_2 = "d0000000FD",
291 stfsu_2 = "d4000000FD",
292 stfd_2 = "d8000000FD",
293 stfdu_2 = "dc000000FD",
294 ld_2 = "e8000000RD", -- NYI: displacement must be divisible by 4.
295 ldu_2 = "e8000001RD",
296 lwa_2 = "e8000002RD",
297 std_2 = "f8000000RD",
298 stdu_2 = "f8000001RD",
300 -- Primary opcode 19:
301 mcrf_2 = "4c000000XX",
302 isync_0 = "4c00012c",
303 crnor_3 = "4c000042CCC",
304 crnot_2 = "4c000042CC=",
305 crandc_3 = "4c000102CCC",
306 crxor_3 = "4c000182CCC",
307 crclr_1 = "4c000182C==",
308 crnand_3 = "4c0001c2CCC",
309 crand_3 = "4c000202CCC",
310 creqv_3 = "4c000242CCC",
311 crset_1 = "4c000242C==",
312 crorc_3 = "4c000342CCC",
313 cror_3 = "4c000382CCC",
314 crmove_2 = "4c000382CC=",
315 bclr_2 = "4c000020AA",
316 bclrl_2 = "4c000021AA",
317 bcctr_2 = "4c000420AA",
318 bcctrl_2 = "4c000421AA",
319 blr_0 = "4e800020",
320 blrl_0 = "4e800021",
321 bctr_0 = "4e800420",
322 bctrl_0 = "4e800421",
324 -- Primary opcode 31:
325 cmpw_3 = "7c000000XRR",
326 cmpw_2 = "7c000000-RR",
327 cmpd_3 = "7c200000XRR",
328 cmpd_2 = "7c200000-RR",
329 tw_3 = "7c000008ARR",
330 subfc_3 = "7c000010RRR.",
331 subc_3 = "7c000010RRR~.",
332 mulhdu_3 = "7c000012RRR.",
333 addc_3 = "7c000014RRR.",
334 mulhwu_3 = "7c000016RRR.",
335 isel_4 = "7c00001eRRRC",
336 isellt_3 = "7c00001eRRR",
337 iselgt_3 = "7c00005eRRR",
338 iseleq_3 = "7c00009eRRR",
339 mfcr_1 = "7c000026R",
340 mfocrf_2 = "7c100026RG",
341 mtcrf_2 = "7c000120GR",
342 mtocrf_2 = "7c100120GR",
343 lwarx_3 = "7c000028RR0R",
344 ldx_3 = "7c00002aRR0R",
345 lwzx_3 = "7c00002eRR0R",
346 slw_3 = "7c000030RR~R.",
347 cntlzw_2 = "7c000034RR~",
348 sld_3 = "7c000036RR~R.",
349 and_3 = "7c000038RR~R.",
350 cmplw_3 = "7c000040XRR",
351 cmplw_2 = "7c000040-RR",
352 cmpld_3 = "7c200040XRR",
353 cmpld_2 = "7c200040-RR",
354 subf_3 = "7c000050RRR.",
355 sub_3 = "7c000050RRR~.",
356 ldux_3 = "7c00006aRR0R",
357 dcbst_2 = "7c00006c-RR",
358 lwzux_3 = "7c00006eRR0R",
359 cntlzd_2 = "7c000074RR~",
360 andc_3 = "7c000078RR~R.",
361 td_3 = "7c000088ARR",
362 mulhd_3 = "7c000092RRR.",
363 mulhw_3 = "7c000096RRR.",
364 ldarx_3 = "7c0000a8RR0R",
365 dcbf_2 = "7c0000ac-RR",
366 lbzx_3 = "7c0000aeRR0R",
367 neg_2 = "7c0000d0RR.",
368 lbzux_3 = "7c0000eeRR0R",
369 popcntb_2 = "7c0000f4RR~",
370 not_2 = "7c0000f8RR~%.",
371 nor_3 = "7c0000f8RR~R.",
372 subfe_3 = "7c000110RRR.",
373 sube_3 = "7c000110RRR~.",
374 adde_3 = "7c000114RRR.",
375 stdx_3 = "7c00012aRR0R",
376 stwcx_3 = "7c00012cRR0R.",
377 stwx_3 = "7c00012eRR0R",
378 prtyw_2 = "7c000134RR~",
379 stdux_3 = "7c00016aRR0R",
380 stwux_3 = "7c00016eRR0R",
381 prtyd_2 = "7c000174RR~",
382 subfze_2 = "7c000190RR.",
383 addze_2 = "7c000194RR.",
384 stdcx_3 = "7c0001acRR0R.",
385 stbx_3 = "7c0001aeRR0R",
386 subfme_2 = "7c0001d0RR.",
387 mulld_3 = "7c0001d2RRR.",
388 addme_2 = "7c0001d4RR.",
389 mullw_3 = "7c0001d6RRR.",
390 dcbtst_2 = "7c0001ec-RR",
391 stbux_3 = "7c0001eeRR0R",
392 add_3 = "7c000214RRR.",
393 dcbt_2 = "7c00022c-RR",
394 lhzx_3 = "7c00022eRR0R",
395 eqv_3 = "7c000238RR~R.",
396 eciwx_3 = "7c00026cRR0R",
397 lhzux_3 = "7c00026eRR0R",
398 xor_3 = "7c000278RR~R.",
399 mfspefscr_1 = "7c0082a6R",
400 mfxer_1 = "7c0102a6R",
401 mflr_1 = "7c0802a6R",
402 mfctr_1 = "7c0902a6R",
403 lwax_3 = "7c0002aaRR0R",
404 lhax_3 = "7c0002aeRR0R",
405 mftb_1 = "7c0c42e6R",
406 mftbu_1 = "7c0d42e6R",
407 lwaux_3 = "7c0002eaRR0R",
408 lhaux_3 = "7c0002eeRR0R",
409 sthx_3 = "7c00032eRR0R",
410 orc_3 = "7c000338RR~R.",
411 ecowx_3 = "7c00036cRR0R",
412 sthux_3 = "7c00036eRR0R",
413 or_3 = "7c000378RR~R.",
414 mr_2 = "7c000378RR~%.",
415 divdu_3 = "7c000392RRR.",
416 divwu_3 = "7c000396RRR.",
417 mtspefscr_1 = "7c0083a6R",
418 mtxer_1 = "7c0103a6R",
419 mtlr_1 = "7c0803a6R",
420 mtctr_1 = "7c0903a6R",
421 dcbi_2 = "7c0003ac-RR",
422 nand_3 = "7c0003b8RR~R.",
423 divd_3 = "7c0003d2RRR.",
424 divw_3 = "7c0003d6RRR.",
425 cmpb_3 = "7c0003f8RR~R.",
426 mcrxr_1 = "7c000400X",
427 subfco_3 = "7c000410RRR.",
428 subco_3 = "7c000410RRR~.",
429 addco_3 = "7c000414RRR.",
430 ldbrx_3 = "7c000428RR0R",
431 lswx_3 = "7c00042aRR0R",
432 lwbrx_3 = "7c00042cRR0R",
433 lfsx_3 = "7c00042eFR0R",
434 srw_3 = "7c000430RR~R.",
435 srd_3 = "7c000436RR~R.",
436 subfo_3 = "7c000450RRR.",
437 subo_3 = "7c000450RRR~.",
438 lfsux_3 = "7c00046eFR0R",
439 lswi_3 = "7c0004aaRR0A",
440 sync_0 = "7c0004ac",
441 lwsync_0 = "7c2004ac",
442 ptesync_0 = "7c4004ac",
443 lfdx_3 = "7c0004aeFR0R",
444 nego_2 = "7c0004d0RR.",
445 lfdux_3 = "7c0004eeFR0R",
446 subfeo_3 = "7c000510RRR.",
447 subeo_3 = "7c000510RRR~.",
448 addeo_3 = "7c000514RRR.",
449 stdbrx_3 = "7c000528RR0R",
450 stswx_3 = "7c00052aRR0R",
451 stwbrx_3 = "7c00052cRR0R",
452 stfsx_3 = "7c00052eFR0R",
453 stfsux_3 = "7c00056eFR0R",
454 subfzeo_2 = "7c000590RR.",
455 addzeo_2 = "7c000594RR.",
456 stswi_3 = "7c0005aaRR0A",
457 stfdx_3 = "7c0005aeFR0R",
458 subfmeo_2 = "7c0005d0RR.",
459 mulldo_3 = "7c0005d2RRR.",
460 addmeo_2 = "7c0005d4RR.",
461 mullwo_3 = "7c0005d6RRR.",
462 dcba_2 = "7c0005ec-RR",
463 stfdux_3 = "7c0005eeFR0R",
464 addo_3 = "7c000614RRR.",
465 lhbrx_3 = "7c00062cRR0R",
466 sraw_3 = "7c000630RR~R.",
467 srad_3 = "7c000634RR~R.",
468 srawi_3 = "7c000670RR~A.",
469 sradi_3 = "7c000674RR~H.",
470 eieio_0 = "7c0006ac",
471 lfiwax_3 = "7c0006aeFR0R",
472 sthbrx_3 = "7c00072cRR0R",
473 extsh_2 = "7c000734RR~.",
474 extsb_2 = "7c000774RR~.",
475 divduo_3 = "7c000792RRR.",
476 divwou_3 = "7c000796RRR.",
477 icbi_2 = "7c0007ac-RR",
478 stfiwx_3 = "7c0007aeFR0R",
479 extsw_2 = "7c0007b4RR~.",
480 divdo_3 = "7c0007d2RRR.",
481 divwo_3 = "7c0007d6RRR.",
482 dcbz_2 = "7c0007ec-RR",
484 -- Primary opcode 30:
485 rldicl_4 = "78000000RR~HM.",
486 rldicr_4 = "78000004RR~HM.",
487 rldic_4 = "78000008RR~HM.",
488 rldimi_4 = "7800000cRR~HM.",
489 rldcl_4 = "78000010RR~RM.",
490 rldcr_4 = "78000012RR~RM.",
492 -- Primary opcode 59:
493 fdivs_3 = "ec000024FFF.",
494 fsubs_3 = "ec000028FFF.",
495 fadds_3 = "ec00002aFFF.",
496 fsqrts_2 = "ec00002cF-F.",
497 fres_2 = "ec000030F-F.",
498 fmuls_3 = "ec000032FF-F.",
499 frsqrtes_2 = "ec000034F-F.",
500 fmsubs_4 = "ec000038FFFF~.",
501 fmadds_4 = "ec00003aFFFF~.",
502 fnmsubs_4 = "ec00003cFFFF~.",
503 fnmadds_4 = "ec00003eFFFF~.",
505 -- Primary opcode 63:
506 fdiv_3 = "fc000024FFF.",
507 fsub_3 = "fc000028FFF.",
508 fadd_3 = "fc00002aFFF.",
509 fsqrt_2 = "fc00002cF-F.",
510 fsel_4 = "fc00002eFFFF~.",
511 fre_2 = "fc000030F-F.",
512 fmul_3 = "fc000032FF-F.",
513 frsqrte_2 = "fc000034F-F.",
514 fmsub_4 = "fc000038FFFF~.",
515 fmadd_4 = "fc00003aFFFF~.",
516 fnmsub_4 = "fc00003cFFFF~.",
517 fnmadd_4 = "fc00003eFFFF~.",
518 fcmpu_3 = "fc000000XFF",
519 fcpsgn_3 = "fc000010FFF.",
520 fcmpo_3 = "fc000040XFF",
521 mtfsb1_1 = "fc00004cA",
522 fneg_2 = "fc000050F-F.",
523 mcrfs_2 = "fc000080XX",
524 mtfsb0_1 = "fc00008cA",
525 fmr_2 = "fc000090F-F.",
526 frsp_2 = "fc000018F-F.",
527 fctiw_2 = "fc00001cF-F.",
528 fctiwz_2 = "fc00001eF-F.",
529 mtfsfi_2 = "fc00010cAA", -- NYI: upshift.
530 fnabs_2 = "fc000110F-F.",
531 fabs_2 = "fc000210F-F.",
532 frin_2 = "fc000310F-F.",
533 friz_2 = "fc000350F-F.",
534 frip_2 = "fc000390F-F.",
535 frim_2 = "fc0003d0F-F.",
536 mffs_1 = "fc00048eF.",
537 -- NYI: mtfsf, mtfsb0, mtfsb1.
538 fctid_2 = "fc00065cF-F.",
539 fctidz_2 = "fc00065eF-F.",
540 fcfid_2 = "fc00069cF-F.",
542 -- Primary opcode 4, SPE APU extension:
543 evaddw_3 = "10000200RRR",
544 evaddiw_3 = "10000202RAR~",
545 evsubw_3 = "10000204RRR~",
546 evsubiw_3 = "10000206RAR~",
547 evabs_2 = "10000208RR",
548 evneg_2 = "10000209RR",
549 evextsb_2 = "1000020aRR",
550 evextsh_2 = "1000020bRR",
551 evrndw_2 = "1000020cRR",
552 evcntlzw_2 = "1000020dRR",
553 evcntlsw_2 = "1000020eRR",
554 brinc_3 = "1000020fRRR",
555 evand_3 = "10000211RRR",
556 evandc_3 = "10000212RRR",
557 evxor_3 = "10000216RRR",
558 evor_3 = "10000217RRR",
559 evmr_2 = "10000217RR=",
560 evnor_3 = "10000218RRR",
561 evnot_2 = "10000218RR=",
562 eveqv_3 = "10000219RRR",
563 evorc_3 = "1000021bRRR",
564 evnand_3 = "1000021eRRR",
565 evsrwu_3 = "10000220RRR",
566 evsrws_3 = "10000221RRR",
567 evsrwiu_3 = "10000222RRA",
568 evsrwis_3 = "10000223RRA",
569 evslw_3 = "10000224RRR",
570 evslwi_3 = "10000226RRA",
571 evrlw_3 = "10000228RRR",
572 evsplati_2 = "10000229RS",
573 evrlwi_3 = "1000022aRRA",
574 evsplatfi_2 = "1000022bRS",
575 evmergehi_3 = "1000022cRRR",
576 evmergelo_3 = "1000022dRRR",
577 evcmpgtu_3 = "10000230XRR",
578 evcmpgtu_2 = "10000230-RR",
579 evcmpgts_3 = "10000231XRR",
580 evcmpgts_2 = "10000231-RR",
581 evcmpltu_3 = "10000232XRR",
582 evcmpltu_2 = "10000232-RR",
583 evcmplts_3 = "10000233XRR",
584 evcmplts_2 = "10000233-RR",
585 evcmpeq_3 = "10000234XRR",
586 evcmpeq_2 = "10000234-RR",
587 evsel_4 = "10000278RRRW",
588 evsel_3 = "10000278RRR",
589 evfsadd_3 = "10000280RRR",
590 evfssub_3 = "10000281RRR",
591 evfsabs_2 = "10000284RR",
592 evfsnabs_2 = "10000285RR",
593 evfsneg_2 = "10000286RR",
594 evfsmul_3 = "10000288RRR",
595 evfsdiv_3 = "10000289RRR",
596 evfscmpgt_3 = "1000028cXRR",
597 evfscmpgt_2 = "1000028c-RR",
598 evfscmplt_3 = "1000028dXRR",
599 evfscmplt_2 = "1000028d-RR",
600 evfscmpeq_3 = "1000028eXRR",
601 evfscmpeq_2 = "1000028e-RR",
602 evfscfui_2 = "10000290R-R",
603 evfscfsi_2 = "10000291R-R",
604 evfscfuf_2 = "10000292R-R",
605 evfscfsf_2 = "10000293R-R",
606 evfsctui_2 = "10000294R-R",
607 evfsctsi_2 = "10000295R-R",
608 evfsctuf_2 = "10000296R-R",
609 evfsctsf_2 = "10000297R-R",
610 evfsctuiz_2 = "10000298R-R",
611 evfsctsiz_2 = "1000029aR-R",
612 evfststgt_3 = "1000029cXRR",
613 evfststgt_2 = "1000029c-RR",
614 evfststlt_3 = "1000029dXRR",
615 evfststlt_2 = "1000029d-RR",
616 evfststeq_3 = "1000029eXRR",
617 evfststeq_2 = "1000029e-RR",
618 efsadd_3 = "100002c0RRR",
619 efssub_3 = "100002c1RRR",
620 efsabs_2 = "100002c4RR",
621 efsnabs_2 = "100002c5RR",
622 efsneg_2 = "100002c6RR",
623 efsmul_3 = "100002c8RRR",
624 efsdiv_3 = "100002c9RRR",
625 efscmpgt_3 = "100002ccXRR",
626 efscmpgt_2 = "100002cc-RR",
627 efscmplt_3 = "100002cdXRR",
628 efscmplt_2 = "100002cd-RR",
629 efscmpeq_3 = "100002ceXRR",
630 efscmpeq_2 = "100002ce-RR",
631 efscfd_2 = "100002cfR-R",
632 efscfui_2 = "100002d0R-R",
633 efscfsi_2 = "100002d1R-R",
634 efscfuf_2 = "100002d2R-R",
635 efscfsf_2 = "100002d3R-R",
636 efsctui_2 = "100002d4R-R",
637 efsctsi_2 = "100002d5R-R",
638 efsctuf_2 = "100002d6R-R",
639 efsctsf_2 = "100002d7R-R",
640 efsctuiz_2 = "100002d8R-R",
641 efsctsiz_2 = "100002daR-R",
642 efststgt_3 = "100002dcXRR",
643 efststgt_2 = "100002dc-RR",
644 efststlt_3 = "100002ddXRR",
645 efststlt_2 = "100002dd-RR",
646 efststeq_3 = "100002deXRR",
647 efststeq_2 = "100002de-RR",
648 efdadd_3 = "100002e0RRR",
649 efdsub_3 = "100002e1RRR",
650 efdcfuid_2 = "100002e2R-R",
651 efdcfsid_2 = "100002e3R-R",
652 efdabs_2 = "100002e4RR",
653 efdnabs_2 = "100002e5RR",
654 efdneg_2 = "100002e6RR",
655 efdmul_3 = "100002e8RRR",
656 efddiv_3 = "100002e9RRR",
657 efdctuidz_2 = "100002eaR-R",
658 efdctsidz_2 = "100002ebR-R",
659 efdcmpgt_3 = "100002ecXRR",
660 efdcmpgt_2 = "100002ec-RR",
661 efdcmplt_3 = "100002edXRR",
662 efdcmplt_2 = "100002ed-RR",
663 efdcmpeq_3 = "100002eeXRR",
664 efdcmpeq_2 = "100002ee-RR",
665 efdcfs_2 = "100002efR-R",
666 efdcfui_2 = "100002f0R-R",
667 efdcfsi_2 = "100002f1R-R",
668 efdcfuf_2 = "100002f2R-R",
669 efdcfsf_2 = "100002f3R-R",
670 efdctui_2 = "100002f4R-R",
671 efdctsi_2 = "100002f5R-R",
672 efdctuf_2 = "100002f6R-R",
673 efdctsf_2 = "100002f7R-R",
674 efdctuiz_2 = "100002f8R-R",
675 efdctsiz_2 = "100002faR-R",
676 efdtstgt_3 = "100002fcXRR",
677 efdtstgt_2 = "100002fc-RR",
678 efdtstlt_3 = "100002fdXRR",
679 efdtstlt_2 = "100002fd-RR",
680 efdtsteq_3 = "100002feXRR",
681 efdtsteq_2 = "100002fe-RR",
682 evlddx_3 = "10000300RR0R",
683 evldd_2 = "10000301R8",
684 evldwx_3 = "10000302RR0R",
685 evldw_2 = "10000303R8",
686 evldhx_3 = "10000304RR0R",
687 evldh_2 = "10000305R8",
688 evlwhex_3 = "10000310RR0R",
689 evlwhe_2 = "10000311R4",
690 evlwhoux_3 = "10000314RR0R",
691 evlwhou_2 = "10000315R4",
692 evlwhosx_3 = "10000316RR0R",
693 evlwhos_2 = "10000317R4",
694 evstddx_3 = "10000320RR0R",
695 evstdd_2 = "10000321R8",
696 evstdwx_3 = "10000322RR0R",
697 evstdw_2 = "10000323R8",
698 evstdhx_3 = "10000324RR0R",
699 evstdh_2 = "10000325R8",
700 evstwhex_3 = "10000330RR0R",
701 evstwhe_2 = "10000331R4",
702 evstwhox_3 = "10000334RR0R",
703 evstwho_2 = "10000335R4",
704 evstwwex_3 = "10000338RR0R",
705 evstwwe_2 = "10000339R4",
706 evstwwox_3 = "1000033cRR0R",
707 evstwwo_2 = "1000033dR4",
708 evmhessf_3 = "10000403RRR",
709 evmhossf_3 = "10000407RRR",
710 evmheumi_3 = "10000408RRR",
711 evmhesmi_3 = "10000409RRR",
712 evmhesmf_3 = "1000040bRRR",
713 evmhoumi_3 = "1000040cRRR",
714 evmhosmi_3 = "1000040dRRR",
715 evmhosmf_3 = "1000040fRRR",
716 evmhessfa_3 = "10000423RRR",
717 evmhossfa_3 = "10000427RRR",
718 evmheumia_3 = "10000428RRR",
719 evmhesmia_3 = "10000429RRR",
720 evmhesmfa_3 = "1000042bRRR",
721 evmhoumia_3 = "1000042cRRR",
722 evmhosmia_3 = "1000042dRRR",
723 evmhosmfa_3 = "1000042fRRR",
724 evmwhssf_3 = "10000447RRR",
725 evmwlumi_3 = "10000448RRR",
726 evmwhumi_3 = "1000044cRRR",
727 evmwhsmi_3 = "1000044dRRR",
728 evmwhsmf_3 = "1000044fRRR",
729 evmwssf_3 = "10000453RRR",
730 evmwumi_3 = "10000458RRR",
731 evmwsmi_3 = "10000459RRR",
732 evmwsmf_3 = "1000045bRRR",
733 evmwhssfa_3 = "10000467RRR",
734 evmwlumia_3 = "10000468RRR",
735 evmwhumia_3 = "1000046cRRR",
736 evmwhsmia_3 = "1000046dRRR",
737 evmwhsmfa_3 = "1000046fRRR",
738 evmwssfa_3 = "10000473RRR",
739 evmwumia_3 = "10000478RRR",
740 evmwsmia_3 = "10000479RRR",
741 evmwsmfa_3 = "1000047bRRR",
742 evmra_2 = "100004c4RR",
743 evdivws_3 = "100004c6RRR",
744 evdivwu_3 = "100004c7RRR",
745 evmwssfaa_3 = "10000553RRR",
746 evmwumiaa_3 = "10000558RRR",
747 evmwsmiaa_3 = "10000559RRR",
748 evmwsmfaa_3 = "1000055bRRR",
749 evmwssfan_3 = "100005d3RRR",
750 evmwumian_3 = "100005d8RRR",
751 evmwsmian_3 = "100005d9RRR",
752 evmwsmfan_3 = "100005dbRRR",
753 evmergehilo_3 = "1000022eRRR",
754 evmergelohi_3 = "1000022fRRR",
755 evlhhesplatx_3 = "10000308RR0R",
756 evlhhesplat_2 = "10000309R2",
757 evlhhousplatx_3 = "1000030cRR0R",
758 evlhhousplat_2 = "1000030dR2",
759 evlhhossplatx_3 = "1000030eRR0R",
760 evlhhossplat_2 = "1000030fR2",
761 evlwwsplatx_3 = "10000318RR0R",
762 evlwwsplat_2 = "10000319R4",
763 evlwhsplatx_3 = "1000031cRR0R",
764 evlwhsplat_2 = "1000031dR4",
765 evaddusiaaw_2 = "100004c0RR",
766 evaddssiaaw_2 = "100004c1RR",
767 evsubfusiaaw_2 = "100004c2RR",
768 evsubfssiaaw_2 = "100004c3RR",
769 evaddumiaaw_2 = "100004c8RR",
770 evaddsmiaaw_2 = "100004c9RR",
771 evsubfumiaaw_2 = "100004caRR",
772 evsubfsmiaaw_2 = "100004cbRR",
773 evmheusiaaw_3 = "10000500RRR",
774 evmhessiaaw_3 = "10000501RRR",
775 evmhessfaaw_3 = "10000503RRR",
776 evmhousiaaw_3 = "10000504RRR",
777 evmhossiaaw_3 = "10000505RRR",
778 evmhossfaaw_3 = "10000507RRR",
779 evmheumiaaw_3 = "10000508RRR",
780 evmhesmiaaw_3 = "10000509RRR",
781 evmhesmfaaw_3 = "1000050bRRR",
782 evmhoumiaaw_3 = "1000050cRRR",
783 evmhosmiaaw_3 = "1000050dRRR",
784 evmhosmfaaw_3 = "1000050fRRR",
785 evmhegumiaa_3 = "10000528RRR",
786 evmhegsmiaa_3 = "10000529RRR",
787 evmhegsmfaa_3 = "1000052bRRR",
788 evmhogumiaa_3 = "1000052cRRR",
789 evmhogsmiaa_3 = "1000052dRRR",
790 evmhogsmfaa_3 = "1000052fRRR",
791 evmwlusiaaw_3 = "10000540RRR",
792 evmwlssiaaw_3 = "10000541RRR",
793 evmwlumiaaw_3 = "10000548RRR",
794 evmwlsmiaaw_3 = "10000549RRR",
795 evmheusianw_3 = "10000580RRR",
796 evmhessianw_3 = "10000581RRR",
797 evmhessfanw_3 = "10000583RRR",
798 evmhousianw_3 = "10000584RRR",
799 evmhossianw_3 = "10000585RRR",
800 evmhossfanw_3 = "10000587RRR",
801 evmheumianw_3 = "10000588RRR",
802 evmhesmianw_3 = "10000589RRR",
803 evmhesmfanw_3 = "1000058bRRR",
804 evmhoumianw_3 = "1000058cRRR",
805 evmhosmianw_3 = "1000058dRRR",
806 evmhosmfanw_3 = "1000058fRRR",
807 evmhegumian_3 = "100005a8RRR",
808 evmhegsmian_3 = "100005a9RRR",
809 evmhegsmfan_3 = "100005abRRR",
810 evmhogumian_3 = "100005acRRR",
811 evmhogsmian_3 = "100005adRRR",
812 evmhogsmfan_3 = "100005afRRR",
813 evmwlusianw_3 = "100005c0RRR",
814 evmwlssianw_3 = "100005c1RRR",
815 evmwlumianw_3 = "100005c8RRR",
816 evmwlsmianw_3 = "100005c9RRR",
818 -- NYI: Book E instructions.
821 -- Add mnemonics for "." variants.
823 local t = {}
824 for k,v in pairs(map_op) do
825 if sub(v, -1) == "." then
826 local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2)
827 t[sub(k, 1, -3).."."..sub(k, -2)] = v2
830 for k,v in pairs(t) do
831 map_op[k] = v
835 -- Add more branch mnemonics.
836 for cond,c in pairs(map_cond) do
837 local b1 = "b"..cond
838 local c1 = shl(band(c, 3), 16) + (c < 4 and 0x01000000 or 0)
839 -- bX[l]
840 map_op[b1.."_1"] = tohex(0x40800000 + c1).."K"
841 map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K"
842 map_op[b1.."l_1"] = tohex(0x40800001 + c1).."K"
843 map_op[b1.."_2"] = tohex(0x40800000 + c1).."-XK"
844 map_op[b1.."y_2"] = tohex(0x40a00000 + c1).."-XK"
845 map_op[b1.."l_2"] = tohex(0x40800001 + c1).."-XK"
846 -- bXlr[l]
847 map_op[b1.."lr_0"] = tohex(0x4c800020 + c1)
848 map_op[b1.."lrl_0"] = tohex(0x4c800021 + c1)
849 map_op[b1.."ctr_0"] = tohex(0x4c800420 + c1)
850 map_op[b1.."ctrl_0"] = tohex(0x4c800421 + c1)
851 -- bXctr[l]
852 map_op[b1.."lr_1"] = tohex(0x4c800020 + c1).."-X"
853 map_op[b1.."lrl_1"] = tohex(0x4c800021 + c1).."-X"
854 map_op[b1.."ctr_1"] = tohex(0x4c800420 + c1).."-X"
855 map_op[b1.."ctrl_1"] = tohex(0x4c800421 + c1).."-X"
858 ------------------------------------------------------------------------------
860 local function parse_gpr(expr)
861 local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$")
862 local tp = map_type[tname or expr]
863 if tp then
864 local reg = ovreg or tp.reg
865 if not reg then
866 werror("type `"..(tname or expr).."' needs a register override")
868 expr = reg
870 local r = match(expr, "^r([1-3]?[0-9])$")
871 if r then
872 r = tonumber(r)
873 if r <= 31 then return r, tp end
875 werror("bad register name `"..expr.."'")
878 local function parse_fpr(expr)
879 local r = match(expr, "^f([1-3]?[0-9])$")
880 if r then
881 r = tonumber(r)
882 if r <= 31 then return r end
884 werror("bad register name `"..expr.."'")
887 local function parse_cr(expr)
888 local r = match(expr, "^cr([0-7])$")
889 if r then return tonumber(r) end
890 werror("bad condition register name `"..expr.."'")
893 local function parse_cond(expr)
894 local r, cond = match(expr, "^4%*cr([0-7])%+(%w%w)$")
895 if r then
896 r = tonumber(r)
897 local c = map_cond[cond]
898 if c and c < 4 then return r*4+c end
900 werror("bad condition bit name `"..expr.."'")
903 local function parse_imm(imm, bits, shift, scale, signed)
904 local n = tonumber(imm)
905 if n then
906 local m = sar(n, scale)
907 if shl(m, scale) == n then
908 if signed then
909 local s = sar(m, bits-1)
910 if s == 0 then return shl(m, shift)
911 elseif s == -1 then return shl(m + shl(1, bits), shift) end
912 else
913 if sar(m, bits) == 0 then return shl(m, shift) end
916 werror("out of range immediate `"..imm.."'")
917 elseif match(imm, "^r([1-3]?[0-9])$") or
918 match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then
919 werror("expected immediate operand, got register")
920 else
921 waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm)
922 return 0
926 local function parse_shiftmask(imm, isshift)
927 local n = tonumber(imm)
928 if n then
929 if shr(n, 6) == 0 then
930 local lsb = band(imm, 31)
931 local msb = imm - lsb
932 return isshift and (shl(lsb, 11)+shr(msb, 4)) or (shl(lsb, 6)+msb)
934 werror("out of range immediate `"..imm.."'")
935 elseif match(imm, "^r([1-3]?[0-9])$") or
936 match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then
937 werror("expected immediate operand, got register")
938 else
939 werror("NYI: parameterized 64 bit shift/mask")
943 local function parse_disp(disp)
944 local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$")
945 if imm then
946 local r = parse_gpr(reg)
947 if r == 0 then werror("cannot use r0 in displacement") end
948 return shl(r, 16) + parse_imm(imm, 16, 0, 0, true)
950 local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$")
951 if reg and tailr ~= "" then
952 local r, tp = parse_gpr(reg)
953 if r == 0 then werror("cannot use r0 in displacement") end
954 if tp then
955 waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr))
956 return shl(r, 16)
959 werror("bad displacement `"..disp.."'")
962 local function parse_u5disp(disp, scale)
963 local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$")
964 if imm then
965 local r = parse_gpr(reg)
966 if r == 0 then werror("cannot use r0 in displacement") end
967 return shl(r, 16) + parse_imm(imm, 5, 11, scale, false)
969 local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$")
970 if reg and tailr ~= "" then
971 local r, tp = parse_gpr(reg)
972 if r == 0 then werror("cannot use r0 in displacement") end
973 if tp then
974 waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr))
975 return shl(r, 16)
978 werror("bad displacement `"..disp.."'")
981 local function parse_label(label, def)
982 local prefix = sub(label, 1, 2)
983 -- =>label (pc label reference)
984 if prefix == "=>" then
985 return "PC", 0, sub(label, 3)
987 -- ->name (global label reference)
988 if prefix == "->" then
989 return "LG", map_global[sub(label, 3)]
991 if def then
992 -- [1-9] (local label definition)
993 if match(label, "^[1-9]$") then
994 return "LG", 10+tonumber(label)
996 else
997 -- [<>][1-9] (local label reference)
998 local dir, lnum = match(label, "^([<>])([1-9])$")
999 if dir then -- Fwd: 1-9, Bkwd: 11-19.
1000 return "LG", lnum + (dir == ">" and 0 or 10)
1002 -- extern label (extern label reference)
1003 local extname = match(label, "^extern%s+(%S+)$")
1004 if extname then
1005 return "EXT", map_extern[extname]
1008 werror("bad label `"..label.."'")
1011 ------------------------------------------------------------------------------
1013 -- Handle opcodes defined with template strings.
1014 map_op[".template__"] = function(params, template, nparams)
1015 if not params then return sub(template, 9) end
1016 local op = tonumber(sub(template, 1, 8), 16)
1017 local n, rs = 1, 26
1019 -- Limit number of section buffer positions used by a single dasm_put().
1020 -- A single opcode needs a maximum of 3 positions (rlwinm).
1021 if secpos+3 > maxsecpos then wflush() end
1022 local pos = wpos()
1024 -- Process each character.
1025 for p in gmatch(sub(template, 9), ".") do
1026 if p == "R" then
1027 rs = rs - 5; op = op + shl(parse_gpr(params[n]), rs); n = n + 1
1028 elseif p == "F" then
1029 rs = rs - 5; op = op + shl(parse_fpr(params[n]), rs); n = n + 1
1030 elseif p == "A" then
1031 rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1
1032 elseif p == "S" then
1033 rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, true); n = n + 1
1034 elseif p == "I" then
1035 op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1
1036 elseif p == "U" then
1037 op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1
1038 elseif p == "D" then
1039 op = op + parse_disp(params[n]); n = n + 1
1040 elseif p == "2" then
1041 op = op + parse_u5disp(params[n], 1); n = n + 1
1042 elseif p == "4" then
1043 op = op + parse_u5disp(params[n], 2); n = n + 1
1044 elseif p == "8" then
1045 op = op + parse_u5disp(params[n], 3); n = n + 1
1046 elseif p == "C" then
1047 rs = rs - 5; op = op + shl(parse_cond(params[n]), rs); n = n + 1
1048 elseif p == "X" then
1049 rs = rs - 5; op = op + shl(parse_cr(params[n]), rs+2); n = n + 1
1050 elseif p == "W" then
1051 op = op + parse_cr(params[n]); n = n + 1
1052 elseif p == "G" then
1053 op = op + parse_imm(params[n], 8, 12, 0, false); n = n + 1
1054 elseif p == "H" then
1055 op = op + parse_shiftmask(params[n], true); n = n + 1
1056 elseif p == "M" then
1057 op = op + parse_shiftmask(params[n], false); n = n + 1
1058 elseif p == "J" or p == "K" then
1059 local mode, n, s = parse_label(params[n], false)
1060 if p == "K" then n = n + 2048 end
1061 waction("REL_"..mode, n, s, 1)
1062 n = n + 1
1063 elseif p == "0" then
1064 if band(shr(op, rs), 31) == 0 then werror("cannot use r0") end
1065 elseif p == "=" or p == "%" then
1066 local t = band(shr(op, p == "%" and rs+5 or rs), 31)
1067 rs = rs - 5
1068 op = op + shl(t, rs)
1069 elseif p == "~" then
1070 local mm = shl(31, rs)
1071 local lo = band(op, mm)
1072 local hi = band(op, shl(mm, 5))
1073 op = op - lo - hi + shl(lo, 5) + shr(hi, 5)
1074 elseif p == "-" then
1075 rs = rs - 5
1076 elseif p == "." then
1077 -- Ignored.
1078 else
1079 assert(false)
1082 wputpos(pos, op)
1085 ------------------------------------------------------------------------------
1087 -- Pseudo-opcode to mark the position where the action list is to be emitted.
1088 map_op[".actionlist_1"] = function(params)
1089 if not params then return "cvar" end
1090 local name = params[1] -- No syntax check. You get to keep the pieces.
1091 wline(function(out) writeactions(out, name) end)
1094 -- Pseudo-opcode to mark the position where the global enum is to be emitted.
1095 map_op[".globals_1"] = function(params)
1096 if not params then return "prefix" end
1097 local prefix = params[1] -- No syntax check. You get to keep the pieces.
1098 wline(function(out) writeglobals(out, prefix) end)
1101 -- Pseudo-opcode to mark the position where the global names are to be emitted.
1102 map_op[".globalnames_1"] = function(params)
1103 if not params then return "cvar" end
1104 local name = params[1] -- No syntax check. You get to keep the pieces.
1105 wline(function(out) writeglobalnames(out, name) end)
1108 -- Pseudo-opcode to mark the position where the extern names are to be emitted.
1109 map_op[".externnames_1"] = function(params)
1110 if not params then return "cvar" end
1111 local name = params[1] -- No syntax check. You get to keep the pieces.
1112 wline(function(out) writeexternnames(out, name) end)
1115 ------------------------------------------------------------------------------
1117 -- Label pseudo-opcode (converted from trailing colon form).
1118 map_op[".label_1"] = function(params)
1119 if not params then return "[1-9] | ->global | =>pcexpr" end
1120 if secpos+1 > maxsecpos then wflush() end
1121 local mode, n, s = parse_label(params[1], true)
1122 if mode == "EXT" then werror("bad label definition") end
1123 waction("LABEL_"..mode, n, s, 1)
1126 ------------------------------------------------------------------------------
1128 -- Pseudo-opcodes for data storage.
1129 map_op[".long_*"] = function(params)
1130 if not params then return "imm..." end
1131 for _,p in ipairs(params) do
1132 local n = tonumber(p)
1133 if not n then werror("bad immediate `"..p.."'") end
1134 if n < 0 then n = n + 2^32 end
1135 wputw(n)
1136 if secpos+2 > maxsecpos then wflush() end
1140 -- Alignment pseudo-opcode.
1141 map_op[".align_1"] = function(params)
1142 if not params then return "numpow2" end
1143 if secpos+1 > maxsecpos then wflush() end
1144 local align = tonumber(params[1])
1145 if align then
1146 local x = align
1147 -- Must be a power of 2 in the range (2 ... 256).
1148 for i=1,8 do
1149 x = x / 2
1150 if x == 1 then
1151 waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1.
1152 return
1156 werror("bad alignment")
1159 ------------------------------------------------------------------------------
1161 -- Pseudo-opcode for (primitive) type definitions (map to C types).
1162 map_op[".type_3"] = function(params, nparams)
1163 if not params then
1164 return nparams == 2 and "name, ctype" or "name, ctype, reg"
1166 local name, ctype, reg = params[1], params[2], params[3]
1167 if not match(name, "^[%a_][%w_]*$") then
1168 werror("bad type name `"..name.."'")
1170 local tp = map_type[name]
1171 if tp then
1172 werror("duplicate type `"..name.."'")
1174 -- Add #type to defines. A bit unclean to put it in map_archdef.
1175 map_archdef["#"..name] = "sizeof("..ctype..")"
1176 -- Add new type and emit shortcut define.
1177 local num = ctypenum + 1
1178 map_type[name] = {
1179 ctype = ctype,
1180 ctypefmt = format("Dt%X(%%s)", num),
1181 reg = reg,
1183 wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
1184 ctypenum = num
1186 map_op[".type_2"] = map_op[".type_3"]
1188 -- Dump type definitions.
1189 local function dumptypes(out, lvl)
1190 local t = {}
1191 for name in pairs(map_type) do t[#t+1] = name end
1192 sort(t)
1193 out:write("Type definitions:\n")
1194 for _,name in ipairs(t) do
1195 local tp = map_type[name]
1196 local reg = tp.reg or ""
1197 out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
1199 out:write("\n")
1202 ------------------------------------------------------------------------------
1204 -- Set the current section.
1205 function _M.section(num)
1206 waction("SECTION", num)
1207 wflush(true) -- SECTION is a terminal action.
1210 ------------------------------------------------------------------------------
1212 -- Dump architecture description.
1213 function _M.dumparch(out)
1214 out:write(format("DynASM %s version %s, released %s\n\n",
1215 _info.arch, _info.version, _info.release))
1216 dumpactions(out)
1219 -- Dump all user defined elements.
1220 function _M.dumpdef(out, lvl)
1221 dumptypes(out, lvl)
1222 dumpglobals(out, lvl)
1223 dumpexterns(out, lvl)
1226 ------------------------------------------------------------------------------
1228 -- Pass callbacks from/to the DynASM core.
1229 function _M.passcb(wl, we, wf, ww)
1230 wline, werror, wfatal, wwarn = wl, we, wf, ww
1231 return wflush
1234 -- Setup the arch-specific module.
1235 function _M.setup(arch, opt)
1236 g_arch, g_opt = arch, opt
1239 -- Merge the core maps and the arch-specific maps.
1240 function _M.mergemaps(map_coreop, map_def)
1241 setmetatable(map_op, { __index = map_coreop })
1242 setmetatable(map_def, { __index = map_archdef })
1243 return map_op, map_def
1246 return _M
1248 ------------------------------------------------------------------------------