FFI: Finalize cdata before userdata when closing the state.
[luajit-2.0.git] / dynasm / dasm_mips.lua
blobaa33f0cccf1707d1500266934e4df62e053fe783
1 ------------------------------------------------------------------------------
2 -- DynASM MIPS 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 = "mips",
11 description = "DynASM MIPS module",
12 version = "1.3.0",
13 vernum = 10300,
14 release = "2012-01-23",
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
30 -- Inherited tables and callbacks.
31 local g_opt, g_arch
32 local wline, werror, wfatal, wwarn
34 -- Action name list.
35 -- CHECK: Keep this in sync with the C code!
36 local action_names = {
37 "STOP", "SECTION", "ESC", "REL_EXT",
38 "ALIGN", "REL_LG", "LABEL_LG",
39 "REL_PC", "LABEL_PC", "IMM",
42 -- Maximum number of section buffer positions for dasm_put().
43 -- CHECK: Keep this in sync with the C code!
44 local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines.
46 -- Action name -> action number.
47 local map_action = {}
48 for n,name in ipairs(action_names) do
49 map_action[name] = n-1
50 end
52 -- Action list buffer.
53 local actlist = {}
55 -- Argument list for next dasm_put(). Start with offset 0 into action list.
56 local actargs = { 0 }
58 -- Current number of section buffer positions for dasm_put().
59 local secpos = 1
61 ------------------------------------------------------------------------------
63 -- Return 8 digit hex number.
64 local function tohex(x)
65 return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua.
66 end
68 -- Dump action names and numbers.
69 local function dumpactions(out)
70 out:write("DynASM encoding engine action codes:\n")
71 for n,name in ipairs(action_names) do
72 local num = map_action[name]
73 out:write(format(" %-10s %02X %d\n", name, num, num))
74 end
75 out:write("\n")
76 end
78 -- Write action list buffer as a huge static C array.
79 local function writeactions(out, name)
80 local nn = #actlist
81 if nn == 0 then nn = 1; actlist[0] = map_action.STOP end
82 out:write("static const unsigned int ", name, "[", nn, "] = {\n")
83 for i = 1,nn-1 do
84 assert(out:write("0x", tohex(actlist[i]), ",\n"))
85 end
86 assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n"))
87 end
89 ------------------------------------------------------------------------------
91 -- Add word to action list.
92 local function wputxw(n)
93 assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
94 actlist[#actlist+1] = n
95 end
97 -- Add action to list with optional arg. Advance buffer pos, too.
98 local function waction(action, val, a, num)
99 local w = assert(map_action[action], "bad action name `"..action.."'")
100 wputxw(0xff000000 + w * 0x10000 + (val or 0))
101 if a then actargs[#actargs+1] = a end
102 if a or num then secpos = secpos + (num or 1) end
105 -- Flush action list (intervening C code or buffer pos overflow).
106 local function wflush(term)
107 if #actlist == actargs[1] then return end -- Nothing to flush.
108 if not term then waction("STOP") end -- Terminate action list.
109 wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true)
110 actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put().
111 secpos = 1 -- The actionlist offset occupies a buffer position, too.
114 -- Put escaped word.
115 local function wputw(n)
116 if n >= 0xff000000 then waction("ESC") end
117 wputxw(n)
120 -- Reserve position for word.
121 local function wpos()
122 local pos = #actlist+1
123 actlist[pos] = ""
124 return pos
127 -- Store word to reserved position.
128 local function wputpos(pos, n)
129 assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
130 actlist[pos] = n
133 ------------------------------------------------------------------------------
135 -- Global label name -> global label number. With auto assignment on 1st use.
136 local next_global = 20
137 local map_global = setmetatable({}, { __index = function(t, name)
138 if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end
139 local n = next_global
140 if n > 2047 then werror("too many global labels") end
141 next_global = n + 1
142 t[name] = n
143 return n
144 end})
146 -- Dump global labels.
147 local function dumpglobals(out, lvl)
148 local t = {}
149 for name, n in pairs(map_global) do t[n] = name end
150 out:write("Global labels:\n")
151 for i=20,next_global-1 do
152 out:write(format(" %s\n", t[i]))
154 out:write("\n")
157 -- Write global label enum.
158 local function writeglobals(out, prefix)
159 local t = {}
160 for name, n in pairs(map_global) do t[n] = name end
161 out:write("enum {\n")
162 for i=20,next_global-1 do
163 out:write(" ", prefix, t[i], ",\n")
165 out:write(" ", prefix, "_MAX\n};\n")
168 -- Write global label names.
169 local function writeglobalnames(out, name)
170 local t = {}
171 for name, n in pairs(map_global) do t[n] = name end
172 out:write("static const char *const ", name, "[] = {\n")
173 for i=20,next_global-1 do
174 out:write(" \"", t[i], "\",\n")
176 out:write(" (const char *)0\n};\n")
179 ------------------------------------------------------------------------------
181 -- Extern label name -> extern label number. With auto assignment on 1st use.
182 local next_extern = 0
183 local map_extern_ = {}
184 local map_extern = setmetatable({}, { __index = function(t, name)
185 -- No restrictions on the name for now.
186 local n = next_extern
187 if n > 2047 then werror("too many extern labels") end
188 next_extern = n + 1
189 t[name] = n
190 map_extern_[n] = name
191 return n
192 end})
194 -- Dump extern labels.
195 local function dumpexterns(out, lvl)
196 out:write("Extern labels:\n")
197 for i=0,next_extern-1 do
198 out:write(format(" %s\n", map_extern_[i]))
200 out:write("\n")
203 -- Write extern label names.
204 local function writeexternnames(out, name)
205 out:write("static const char *const ", name, "[] = {\n")
206 for i=0,next_extern-1 do
207 out:write(" \"", map_extern_[i], "\",\n")
209 out:write(" (const char *)0\n};\n")
212 ------------------------------------------------------------------------------
214 -- Arch-specific maps.
215 local map_archdef = { sp="r29", ra="r31" } -- Ext. register name -> int. name.
217 local map_type = {} -- Type name -> { ctype, reg }
218 local ctypenum = 0 -- Type number (for Dt... macros).
220 -- Reverse defines for registers.
221 function _M.revdef(s)
222 if s == "r29" then return "sp"
223 elseif s == "r31" then return "ra" end
224 return s
227 ------------------------------------------------------------------------------
229 -- Template strings for MIPS instructions.
230 local map_op = {
231 -- First-level opcodes.
232 j_1 = "08000000J",
233 jal_1 = "0c000000J",
234 b_1 = "10000000B",
235 beqz_2 = "10000000SB",
236 beq_3 = "10000000STB",
237 bnez_2 = "14000000SB",
238 bne_3 = "14000000STB",
239 blez_2 = "18000000SB",
240 bgtz_2 = "1c000000SB",
241 addi_3 = "20000000TSI",
242 li_2 = "24000000TI",
243 addiu_3 = "24000000TSI",
244 slti_3 = "28000000TSI",
245 sltiu_3 = "2c000000TSI",
246 andi_3 = "30000000TSU",
247 lu_2 = "34000000TU",
248 ori_3 = "34000000TSU",
249 xori_3 = "38000000TSU",
250 lui_2 = "3c000000TU",
251 beqzl_2 = "50000000SB",
252 beql_3 = "50000000STB",
253 bnezl_2 = "54000000SB",
254 bnel_3 = "54000000STB",
255 blezl_2 = "58000000SB",
256 bgtzl_2 = "5c000000SB",
257 lb_2 = "80000000TO",
258 lh_2 = "84000000TO",
259 lwl_2 = "88000000TO",
260 lw_2 = "8c000000TO",
261 lbu_2 = "90000000TO",
262 lhu_2 = "94000000TO",
263 lwr_2 = "98000000TO",
264 sb_2 = "a0000000TO",
265 sh_2 = "a4000000TO",
266 swl_2 = "a8000000TO",
267 sw_2 = "ac000000TO",
268 swr_2 = "b8000000TO",
269 cache_2 = "bc000000NO",
270 ll_2 = "c0000000TO",
271 lwc1_2 = "c4000000HO",
272 pref_2 = "cc000000NO",
273 ldc1_2 = "d4000000HO",
274 sc_2 = "e0000000TO",
275 swc1_2 = "e4000000HO",
276 sdc1_2 = "f4000000HO",
278 -- Opcode SPECIAL.
279 nop_0 = "00000000",
280 sll_3 = "00000000DTA",
281 movf_2 = "00000001DS",
282 movf_3 = "00000001DSC",
283 movt_2 = "00010001DS",
284 movt_3 = "00010001DSC",
285 srl_3 = "00000002DTA",
286 rotr_3 = "00200002DTA",
287 sra_3 = "00000003DTA",
288 sllv_3 = "00000004DTS",
289 srlv_3 = "00000006DTS",
290 rotrv_3 = "00000046DTS",
291 srav_3 = "00000007DTS",
292 jr_1 = "00000008S",
293 jalr_1 = "0000f809S",
294 jalr_2 = "00000009DS",
295 movz_3 = "0000000aDST",
296 movn_3 = "0000000bDST",
297 syscall_0 = "0000000c",
298 syscall_1 = "0000000cY",
299 break_0 = "0000000d",
300 break_1 = "0000000dY",
301 sync_0 = "0000000f",
302 mfhi_1 = "00000010D",
303 mthi_1 = "00000011S",
304 mflo_1 = "00000012D",
305 mtlo_1 = "00000013S",
306 mult_2 = "00000018ST",
307 multu_2 = "00000019ST",
308 div_2 = "0000001aST",
309 divu_2 = "0000001bST",
310 add_3 = "00000020DST",
311 move_2 = "00000021DS",
312 addu_3 = "00000021DST",
313 sub_3 = "00000022DST",
314 negu_2 = "00000023DT",
315 subu_3 = "00000023DST",
316 and_3 = "00000024DST",
317 or_3 = "00000025DST",
318 xor_3 = "00000026DST",
319 not_2 = "00000027DS",
320 nor_3 = "00000027DST",
321 slt_3 = "0000002aDST",
322 sltu_3 = "0000002bDST",
323 tge_2 = "00000030ST",
324 tge_3 = "00000030STZ",
325 tgeu_2 = "00000031ST",
326 tgeu_3 = "00000031STZ",
327 tlt_2 = "00000032ST",
328 tlt_3 = "00000032STZ",
329 tltu_2 = "00000033ST",
330 tltu_3 = "00000033STZ",
331 teq_2 = "00000034ST",
332 teq_3 = "00000034STZ",
333 tne_2 = "00000036ST",
334 tne_3 = "00000036STZ",
336 -- Opcode REGIMM.
337 bltz_2 = "04000000SB",
338 bgez_2 = "04010000SB",
339 bltzl_2 = "04020000SB",
340 bgezl_2 = "04030000SB",
341 tgei_2 = "04080000SI",
342 tgeiu_2 = "04090000SI",
343 tlti_2 = "040a0000SI",
344 tltiu_2 = "040b0000SI",
345 teqi_2 = "040c0000SI",
346 tnei_2 = "040e0000SI",
347 bltzal_2 = "04100000SB",
348 bal_1 = "04110000B",
349 bgezal_2 = "04110000SB",
350 bltzall_2 = "04120000SB",
351 bgezall_2 = "04130000SB",
352 synci_1 = "041f0000O",
354 -- Opcode SPECIAL2.
355 madd_2 = "70000000ST",
356 maddu_2 = "70000001ST",
357 mul_3 = "70000002DST",
358 msub_2 = "70000004ST",
359 msubu_2 = "70000005ST",
360 clz_2 = "70000020DS=",
361 clo_2 = "70000021DS=",
362 sdbbp_0 = "7000003f",
363 sdbbp_1 = "7000003fY",
365 -- Opcode SPECIAL3.
366 ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1
367 ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1
368 wsbh_2 = "7c0000a0DT",
369 seb_2 = "7c000420DT",
370 seh_2 = "7c000620DT",
371 rdhwr_2 = "7c00003bTD",
373 -- Opcode COP0.
374 mfc0_2 = "40000000TD",
375 mfc0_3 = "40000000TDW",
376 mtc0_2 = "40800000TD",
377 mtc0_3 = "40800000TDW",
378 rdpgpr_2 = "41400000DT",
379 di_0 = "41606000",
380 di_1 = "41606000T",
381 ei_0 = "41606020",
382 ei_1 = "41606020T",
383 wrpgpr_2 = "41c00000DT",
384 tlbr_0 = "42000001",
385 tlbwi_0 = "42000002",
386 tlbwr_0 = "42000006",
387 tlbp_0 = "42000008",
388 eret_0 = "42000018",
389 deret_0 = "4200001f",
390 wait_0 = "42000020",
392 -- Opcode COP1.
393 mfc1_2 = "44000000TG",
394 cfc1_2 = "44400000TG",
395 mfhc1_2 = "44600000TG",
396 mtc1_2 = "44800000TG",
397 ctc1_2 = "44c00000TG",
398 mthc1_2 = "44e00000TG",
400 bc1f_1 = "45000000B",
401 bc1f_2 = "45000000CB",
402 bc1t_1 = "45010000B",
403 bc1t_2 = "45010000CB",
404 bc1fl_1 = "45020000B",
405 bc1fl_2 = "45020000CB",
406 bc1tl_1 = "45030000B",
407 bc1tl_2 = "45030000CB",
409 ["add.s_3"] = "46000000FGH",
410 ["sub.s_3"] = "46000001FGH",
411 ["mul.s_3"] = "46000002FGH",
412 ["div.s_3"] = "46000003FGH",
413 ["sqrt.s_2"] = "46000004FG",
414 ["abs.s_2"] = "46000005FG",
415 ["mov.s_2"] = "46000006FG",
416 ["neg.s_2"] = "46000007FG",
417 ["round.l.s_2"] = "46000008FG",
418 ["trunc.l.s_2"] = "46000009FG",
419 ["ceil.l.s_2"] = "4600000aFG",
420 ["floor.l.s_2"] = "4600000bFG",
421 ["round.w.s_2"] = "4600000cFG",
422 ["trunc.w.s_2"] = "4600000dFG",
423 ["ceil.w.s_2"] = "4600000eFG",
424 ["floor.w.s_2"] = "4600000fFG",
425 ["movf.s_2"] = "46000011FG",
426 ["movf.s_3"] = "46000011FGC",
427 ["movt.s_2"] = "46010011FG",
428 ["movt.s_3"] = "46010011FGC",
429 ["movz.s_3"] = "46000012FGT",
430 ["movn.s_3"] = "46000013FGT",
431 ["recip.s_2"] = "46000015FG",
432 ["rsqrt.s_2"] = "46000016FG",
433 ["cvt.d.s_2"] = "46000021FG",
434 ["cvt.w.s_2"] = "46000024FG",
435 ["cvt.l.s_2"] = "46000025FG",
436 ["cvt.ps.s_3"] = "46000026FGH",
437 ["c.f.s_2"] = "46000030GH",
438 ["c.f.s_3"] = "46000030VGH",
439 ["c.un.s_2"] = "46000031GH",
440 ["c.un.s_3"] = "46000031VGH",
441 ["c.eq.s_2"] = "46000032GH",
442 ["c.eq.s_3"] = "46000032VGH",
443 ["c.ueq.s_2"] = "46000033GH",
444 ["c.ueq.s_3"] = "46000033VGH",
445 ["c.olt.s_2"] = "46000034GH",
446 ["c.olt.s_3"] = "46000034VGH",
447 ["c.ult.s_2"] = "46000035GH",
448 ["c.ult.s_3"] = "46000035VGH",
449 ["c.ole.s_2"] = "46000036GH",
450 ["c.ole.s_3"] = "46000036VGH",
451 ["c.ule.s_2"] = "46000037GH",
452 ["c.ule.s_3"] = "46000037VGH",
453 ["c.sf.s_2"] = "46000038GH",
454 ["c.sf.s_3"] = "46000038VGH",
455 ["c.ngle.s_2"] = "46000039GH",
456 ["c.ngle.s_3"] = "46000039VGH",
457 ["c.seq.s_2"] = "4600003aGH",
458 ["c.seq.s_3"] = "4600003aVGH",
459 ["c.ngl.s_2"] = "4600003bGH",
460 ["c.ngl.s_3"] = "4600003bVGH",
461 ["c.lt.s_2"] = "4600003cGH",
462 ["c.lt.s_3"] = "4600003cVGH",
463 ["c.nge.s_2"] = "4600003dGH",
464 ["c.nge.s_3"] = "4600003dVGH",
465 ["c.le.s_2"] = "4600003eGH",
466 ["c.le.s_3"] = "4600003eVGH",
467 ["c.ngt.s_2"] = "4600003fGH",
468 ["c.ngt.s_3"] = "4600003fVGH",
470 ["add.d_3"] = "46200000FGH",
471 ["sub.d_3"] = "46200001FGH",
472 ["mul.d_3"] = "46200002FGH",
473 ["div.d_3"] = "46200003FGH",
474 ["sqrt.d_2"] = "46200004FG",
475 ["abs.d_2"] = "46200005FG",
476 ["mov.d_2"] = "46200006FG",
477 ["neg.d_2"] = "46200007FG",
478 ["round.l.d_2"] = "46200008FG",
479 ["trunc.l.d_2"] = "46200009FG",
480 ["ceil.l.d_2"] = "4620000aFG",
481 ["floor.l.d_2"] = "4620000bFG",
482 ["round.w.d_2"] = "4620000cFG",
483 ["trunc.w.d_2"] = "4620000dFG",
484 ["ceil.w.d_2"] = "4620000eFG",
485 ["floor.w.d_2"] = "4620000fFG",
486 ["movf.d_2"] = "46200011FG",
487 ["movf.d_3"] = "46200011FGC",
488 ["movt.d_2"] = "46210011FG",
489 ["movt.d_3"] = "46210011FGC",
490 ["movz.d_3"] = "46200012FGT",
491 ["movn.d_3"] = "46200013FGT",
492 ["recip.d_2"] = "46200015FG",
493 ["rsqrt.d_2"] = "46200016FG",
494 ["cvt.s.d_2"] = "46200020FG",
495 ["cvt.w.d_2"] = "46200024FG",
496 ["cvt.l.d_2"] = "46200025FG",
497 ["c.f.d_2"] = "46200030GH",
498 ["c.f.d_3"] = "46200030VGH",
499 ["c.un.d_2"] = "46200031GH",
500 ["c.un.d_3"] = "46200031VGH",
501 ["c.eq.d_2"] = "46200032GH",
502 ["c.eq.d_3"] = "46200032VGH",
503 ["c.ueq.d_2"] = "46200033GH",
504 ["c.ueq.d_3"] = "46200033VGH",
505 ["c.olt.d_2"] = "46200034GH",
506 ["c.olt.d_3"] = "46200034VGH",
507 ["c.ult.d_2"] = "46200035GH",
508 ["c.ult.d_3"] = "46200035VGH",
509 ["c.ole.d_2"] = "46200036GH",
510 ["c.ole.d_3"] = "46200036VGH",
511 ["c.ule.d_2"] = "46200037GH",
512 ["c.ule.d_3"] = "46200037VGH",
513 ["c.sf.d_2"] = "46200038GH",
514 ["c.sf.d_3"] = "46200038VGH",
515 ["c.ngle.d_2"] = "46200039GH",
516 ["c.ngle.d_3"] = "46200039VGH",
517 ["c.seq.d_2"] = "4620003aGH",
518 ["c.seq.d_3"] = "4620003aVGH",
519 ["c.ngl.d_2"] = "4620003bGH",
520 ["c.ngl.d_3"] = "4620003bVGH",
521 ["c.lt.d_2"] = "4620003cGH",
522 ["c.lt.d_3"] = "4620003cVGH",
523 ["c.nge.d_2"] = "4620003dGH",
524 ["c.nge.d_3"] = "4620003dVGH",
525 ["c.le.d_2"] = "4620003eGH",
526 ["c.le.d_3"] = "4620003eVGH",
527 ["c.ngt.d_2"] = "4620003fGH",
528 ["c.ngt.d_3"] = "4620003fVGH",
530 ["add.ps_3"] = "46c00000FGH",
531 ["sub.ps_3"] = "46c00001FGH",
532 ["mul.ps_3"] = "46c00002FGH",
533 ["abs.ps_2"] = "46c00005FG",
534 ["mov.ps_2"] = "46c00006FG",
535 ["neg.ps_2"] = "46c00007FG",
536 ["movf.ps_2"] = "46c00011FG",
537 ["movf.ps_3"] = "46c00011FGC",
538 ["movt.ps_2"] = "46c10011FG",
539 ["movt.ps_3"] = "46c10011FGC",
540 ["movz.ps_3"] = "46c00012FGT",
541 ["movn.ps_3"] = "46c00013FGT",
542 ["cvt.s.pu_2"] = "46c00020FG",
543 ["cvt.s.pl_2"] = "46c00028FG",
544 ["pll.ps_3"] = "46c0002cFGH",
545 ["plu.ps_3"] = "46c0002dFGH",
546 ["pul.ps_3"] = "46c0002eFGH",
547 ["puu.ps_3"] = "46c0002fFGH",
548 ["c.f.ps_2"] = "46c00030GH",
549 ["c.f.ps_3"] = "46c00030VGH",
550 ["c.un.ps_2"] = "46c00031GH",
551 ["c.un.ps_3"] = "46c00031VGH",
552 ["c.eq.ps_2"] = "46c00032GH",
553 ["c.eq.ps_3"] = "46c00032VGH",
554 ["c.ueq.ps_2"] = "46c00033GH",
555 ["c.ueq.ps_3"] = "46c00033VGH",
556 ["c.olt.ps_2"] = "46c00034GH",
557 ["c.olt.ps_3"] = "46c00034VGH",
558 ["c.ult.ps_2"] = "46c00035GH",
559 ["c.ult.ps_3"] = "46c00035VGH",
560 ["c.ole.ps_2"] = "46c00036GH",
561 ["c.ole.ps_3"] = "46c00036VGH",
562 ["c.ule.ps_2"] = "46c00037GH",
563 ["c.ule.ps_3"] = "46c00037VGH",
564 ["c.sf.ps_2"] = "46c00038GH",
565 ["c.sf.ps_3"] = "46c00038VGH",
566 ["c.ngle.ps_2"] = "46c00039GH",
567 ["c.ngle.ps_3"] = "46c00039VGH",
568 ["c.seq.ps_2"] = "46c0003aGH",
569 ["c.seq.ps_3"] = "46c0003aVGH",
570 ["c.ngl.ps_2"] = "46c0003bGH",
571 ["c.ngl.ps_3"] = "46c0003bVGH",
572 ["c.lt.ps_2"] = "46c0003cGH",
573 ["c.lt.ps_3"] = "46c0003cVGH",
574 ["c.nge.ps_2"] = "46c0003dGH",
575 ["c.nge.ps_3"] = "46c0003dVGH",
576 ["c.le.ps_2"] = "46c0003eGH",
577 ["c.le.ps_3"] = "46c0003eVGH",
578 ["c.ngt.ps_2"] = "46c0003fGH",
579 ["c.ngt.ps_3"] = "46c0003fVGH",
581 ["cvt.s.w_2"] = "46800020FG",
582 ["cvt.d.w_2"] = "46800021FG",
584 ["cvt.s.l_2"] = "46a00020FG",
585 ["cvt.d.l_2"] = "46a00021FG",
587 -- Opcode COP1X.
588 lwxc1_2 = "4c000000FX",
589 ldxc1_2 = "4c000001FX",
590 luxc1_2 = "4c000005FX",
591 swxc1_2 = "4c000008FX",
592 sdxc1_2 = "4c000009FX",
593 suxc1_2 = "4c00000dFX",
594 prefx_2 = "4c00000fMX",
595 ["alnv.ps_4"] = "4c00001eFGHS",
596 ["madd.s_4"] = "4c000020FRGH",
597 ["madd.d_4"] = "4c000021FRGH",
598 ["madd.ps_4"] = "4c000026FRGH",
599 ["msub.s_4"] = "4c000028FRGH",
600 ["msub.d_4"] = "4c000029FRGH",
601 ["msub.ps_4"] = "4c00002eFRGH",
602 ["nmadd.s_4"] = "4c000030FRGH",
603 ["nmadd.d_4"] = "4c000031FRGH",
604 ["nmadd.ps_4"] = "4c000036FRGH",
605 ["nmsub.s_4"] = "4c000038FRGH",
606 ["nmsub.d_4"] = "4c000039FRGH",
607 ["nmsub.ps_4"] = "4c00003eFRGH",
610 ------------------------------------------------------------------------------
612 local function parse_gpr(expr)
613 local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$")
614 local tp = map_type[tname or expr]
615 if tp then
616 local reg = ovreg or tp.reg
617 if not reg then
618 werror("type `"..(tname or expr).."' needs a register override")
620 expr = reg
622 local r = match(expr, "^r([1-3]?[0-9])$")
623 if r then
624 r = tonumber(r)
625 if r <= 31 then return r, tp end
627 werror("bad register name `"..expr.."'")
630 local function parse_fpr(expr)
631 local r = match(expr, "^f([1-3]?[0-9])$")
632 if r then
633 r = tonumber(r)
634 if r <= 31 then return r end
636 werror("bad register name `"..expr.."'")
639 local function parse_imm(imm, bits, shift, scale, signed)
640 local n = tonumber(imm)
641 if n then
642 if n % 2^scale == 0 then
643 n = n / 2^scale
644 if signed then
645 if n >= 0 then
646 if n < 2^(bits-1) then return n*2^shift end
647 else
648 if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end
650 else
651 if n >= 0 and n <= 2^bits-1 then return n*2^shift end
654 werror("out of range immediate `"..imm.."'")
655 elseif match(imm, "^[rf]([1-3]?[0-9])$") or
656 match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then
657 werror("expected immediate operand, got register")
658 else
659 waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm)
660 return 0
664 local function parse_disp(disp)
665 local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$")
666 if imm then
667 local r = parse_gpr(reg)*2^21
668 local extname = match(imm, "^extern%s+(%S+)$")
669 if extname then
670 waction("REL_EXT", map_extern[extname], nil, 1)
671 return r
672 else
673 return r + parse_imm(imm, 16, 0, 0, true)
676 local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$")
677 if reg and tailr ~= "" then
678 local r, tp = parse_gpr(reg)
679 if tp then
680 waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr))
681 return r*2^21
684 werror("bad displacement `"..disp.."'")
687 local function parse_index(idx)
688 local rt, rs = match(idx, "^(.*)%(([%w_:]+)%)$")
689 if rt then
690 rt = parse_gpr(rt)
691 rs = parse_gpr(rs)
692 return rt*2^16 + rs*2^21
694 werror("bad index `"..idx.."'")
697 local function parse_label(label, def)
698 local prefix = sub(label, 1, 2)
699 -- =>label (pc label reference)
700 if prefix == "=>" then
701 return "PC", 0, sub(label, 3)
703 -- ->name (global label reference)
704 if prefix == "->" then
705 return "LG", map_global[sub(label, 3)]
707 if def then
708 -- [1-9] (local label definition)
709 if match(label, "^[1-9]$") then
710 return "LG", 10+tonumber(label)
712 else
713 -- [<>][1-9] (local label reference)
714 local dir, lnum = match(label, "^([<>])([1-9])$")
715 if dir then -- Fwd: 1-9, Bkwd: 11-19.
716 return "LG", lnum + (dir == ">" and 0 or 10)
718 -- extern label (extern label reference)
719 local extname = match(label, "^extern%s+(%S+)$")
720 if extname then
721 return "EXT", map_extern[extname]
724 werror("bad label `"..label.."'")
727 ------------------------------------------------------------------------------
729 -- Handle opcodes defined with template strings.
730 map_op[".template__"] = function(params, template, nparams)
731 if not params then return sub(template, 9) end
732 local op = tonumber(sub(template, 1, 8), 16)
733 local n = 1
735 -- Limit number of section buffer positions used by a single dasm_put().
736 -- A single opcode needs a maximum of 2 positions (ins/ext).
737 if secpos+2 > maxsecpos then wflush() end
738 local pos = wpos()
740 -- Process each character.
741 for p in gmatch(sub(template, 9), ".") do
742 if p == "D" then
743 op = op + parse_gpr(params[n]) * 2^11; n = n + 1
744 elseif p == "T" then
745 op = op + parse_gpr(params[n]) * 2^16; n = n + 1
746 elseif p == "S" then
747 op = op + parse_gpr(params[n]) * 2^21; n = n + 1
748 elseif p == "F" then
749 op = op + parse_fpr(params[n]) * 2^6; n = n + 1
750 elseif p == "G" then
751 op = op + parse_fpr(params[n]) * 2^11; n = n + 1
752 elseif p == "H" then
753 op = op + parse_fpr(params[n]) * 2^16; n = n + 1
754 elseif p == "R" then
755 op = op + parse_fpr(params[n]) * 2^21; n = n + 1
756 elseif p == "I" then
757 op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1
758 elseif p == "U" then
759 op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1
760 elseif p == "O" then
761 op = op + parse_disp(params[n]); n = n + 1
762 elseif p == "X" then
763 op = op + parse_index(params[n]); n = n + 1
764 elseif p == "B" or p == "J" then
765 local mode, n, s = parse_label(params[n], false)
766 if p == "B" then n = n + 2048 end
767 waction("REL_"..mode, n, s, 1)
768 n = n + 1
769 elseif p == "A" then
770 op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1
771 elseif p == "M" then
772 op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1
773 elseif p == "N" then
774 op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1
775 elseif p == "C" then
776 op = op + parse_imm(params[n], 3, 18, 0, false); n = n + 1
777 elseif p == "V" then
778 op = op + parse_imm(params[n], 3, 8, 0, false); n = n + 1
779 elseif p == "W" then
780 op = op + parse_imm(params[n], 3, 0, 0, false); n = n + 1
781 elseif p == "Y" then
782 op = op + parse_imm(params[n], 20, 6, 0, false); n = n + 1
783 elseif p == "Z" then
784 op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1
785 elseif p == "=" then
786 local d = ((op - op % 2^11) / 2^11) % 32
787 op = op + d * 2^16 -- Copy D to T for clz, clo.
788 else
789 assert(false)
792 wputpos(pos, op)
795 ------------------------------------------------------------------------------
797 -- Pseudo-opcode to mark the position where the action list is to be emitted.
798 map_op[".actionlist_1"] = function(params)
799 if not params then return "cvar" end
800 local name = params[1] -- No syntax check. You get to keep the pieces.
801 wline(function(out) writeactions(out, name) end)
804 -- Pseudo-opcode to mark the position where the global enum is to be emitted.
805 map_op[".globals_1"] = function(params)
806 if not params then return "prefix" end
807 local prefix = params[1] -- No syntax check. You get to keep the pieces.
808 wline(function(out) writeglobals(out, prefix) end)
811 -- Pseudo-opcode to mark the position where the global names are to be emitted.
812 map_op[".globalnames_1"] = function(params)
813 if not params then return "cvar" end
814 local name = params[1] -- No syntax check. You get to keep the pieces.
815 wline(function(out) writeglobalnames(out, name) end)
818 -- Pseudo-opcode to mark the position where the extern names are to be emitted.
819 map_op[".externnames_1"] = function(params)
820 if not params then return "cvar" end
821 local name = params[1] -- No syntax check. You get to keep the pieces.
822 wline(function(out) writeexternnames(out, name) end)
825 ------------------------------------------------------------------------------
827 -- Label pseudo-opcode (converted from trailing colon form).
828 map_op[".label_1"] = function(params)
829 if not params then return "[1-9] | ->global | =>pcexpr" end
830 if secpos+1 > maxsecpos then wflush() end
831 local mode, n, s = parse_label(params[1], true)
832 if mode == "EXT" then werror("bad label definition") end
833 waction("LABEL_"..mode, n, s, 1)
836 ------------------------------------------------------------------------------
838 -- Pseudo-opcodes for data storage.
839 map_op[".long_*"] = function(params)
840 if not params then return "imm..." end
841 for _,p in ipairs(params) do
842 local n = tonumber(p)
843 if not n then werror("bad immediate `"..p.."'") end
844 if n < 0 then n = n + 2^32 end
845 wputw(n)
846 if secpos+2 > maxsecpos then wflush() end
850 -- Alignment pseudo-opcode.
851 map_op[".align_1"] = function(params)
852 if not params then return "numpow2" end
853 if secpos+1 > maxsecpos then wflush() end
854 local align = tonumber(params[1])
855 if align then
856 local x = align
857 -- Must be a power of 2 in the range (2 ... 256).
858 for i=1,8 do
859 x = x / 2
860 if x == 1 then
861 waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1.
862 return
866 werror("bad alignment")
869 ------------------------------------------------------------------------------
871 -- Pseudo-opcode for (primitive) type definitions (map to C types).
872 map_op[".type_3"] = function(params, nparams)
873 if not params then
874 return nparams == 2 and "name, ctype" or "name, ctype, reg"
876 local name, ctype, reg = params[1], params[2], params[3]
877 if not match(name, "^[%a_][%w_]*$") then
878 werror("bad type name `"..name.."'")
880 local tp = map_type[name]
881 if tp then
882 werror("duplicate type `"..name.."'")
884 -- Add #type to defines. A bit unclean to put it in map_archdef.
885 map_archdef["#"..name] = "sizeof("..ctype..")"
886 -- Add new type and emit shortcut define.
887 local num = ctypenum + 1
888 map_type[name] = {
889 ctype = ctype,
890 ctypefmt = format("Dt%X(%%s)", num),
891 reg = reg,
893 wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
894 ctypenum = num
896 map_op[".type_2"] = map_op[".type_3"]
898 -- Dump type definitions.
899 local function dumptypes(out, lvl)
900 local t = {}
901 for name in pairs(map_type) do t[#t+1] = name end
902 sort(t)
903 out:write("Type definitions:\n")
904 for _,name in ipairs(t) do
905 local tp = map_type[name]
906 local reg = tp.reg or ""
907 out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
909 out:write("\n")
912 ------------------------------------------------------------------------------
914 -- Set the current section.
915 function _M.section(num)
916 waction("SECTION", num)
917 wflush(true) -- SECTION is a terminal action.
920 ------------------------------------------------------------------------------
922 -- Dump architecture description.
923 function _M.dumparch(out)
924 out:write(format("DynASM %s version %s, released %s\n\n",
925 _info.arch, _info.version, _info.release))
926 dumpactions(out)
929 -- Dump all user defined elements.
930 function _M.dumpdef(out, lvl)
931 dumptypes(out, lvl)
932 dumpglobals(out, lvl)
933 dumpexterns(out, lvl)
936 ------------------------------------------------------------------------------
938 -- Pass callbacks from/to the DynASM core.
939 function _M.passcb(wl, we, wf, ww)
940 wline, werror, wfatal, wwarn = wl, we, wf, ww
941 return wflush
944 -- Setup the arch-specific module.
945 function _M.setup(arch, opt)
946 g_arch, g_opt = arch, opt
949 -- Merge the core maps and the arch-specific maps.
950 function _M.mergemaps(map_coreop, map_def)
951 setmetatable(map_op, { __index = map_coreop })
952 setmetatable(map_def, { __index = map_archdef })
953 return map_op, map_def
956 return _M
958 ------------------------------------------------------------------------------