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