diff options
Diffstat (limited to 'dynasm/dasm_arm64.lua')
| -rw-r--r-- | dynasm/dasm_arm64.lua | 1248 |
1 files changed, 1248 insertions, 0 deletions
diff --git a/dynasm/dasm_arm64.lua b/dynasm/dasm_arm64.lua new file mode 100644 index 00000000..5d68accc --- /dev/null +++ b/dynasm/dasm_arm64.lua | |||
| @@ -0,0 +1,1248 @@ | |||
| 1 | ------------------------------------------------------------------------------ | ||
| 2 | -- DynASM ARM64 module. | ||
| 3 | -- | ||
| 4 | -- Copyright (C) 2005-2026 Mike Pall. All rights reserved. | ||
| 5 | -- See dynasm.lua for full copyright notice. | ||
| 6 | ------------------------------------------------------------------------------ | ||
| 7 | |||
| 8 | -- Module information: | ||
| 9 | local _info = { | ||
| 10 | arch = "arm", | ||
| 11 | description = "DynASM ARM64 module", | ||
| 12 | version = "1.5.0", | ||
| 13 | vernum = 10500, | ||
| 14 | release = "2021-05-02", | ||
| 15 | author = "Mike Pall", | ||
| 16 | license = "MIT", | ||
| 17 | } | ||
| 18 | |||
| 19 | -- Exported glue functions for the arch-specific module. | ||
| 20 | local _M = { _info = _info } | ||
| 21 | |||
| 22 | -- Cache library functions. | ||
| 23 | local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs | ||
| 24 | local assert, setmetatable, rawget = assert, setmetatable, rawget | ||
| 25 | local _s = string | ||
| 26 | local format, byte, char = _s.format, _s.byte, _s.char | ||
| 27 | local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub | ||
| 28 | local concat, sort, insert = table.concat, table.sort, table.insert | ||
| 29 | local bit = bit or require("bit") | ||
| 30 | local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift | ||
| 31 | local ror, tohex, tobit = bit.ror, bit.tohex, bit.tobit | ||
| 32 | |||
| 33 | -- Inherited tables and callbacks. | ||
| 34 | local g_opt, g_arch | ||
| 35 | local wline, werror, wfatal, wwarn | ||
| 36 | |||
| 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", "REL_A", | ||
| 43 | "IMM", "IMM6", "IMM12", "IMM13W", "IMM13X", "IMML", "IMMV", | ||
| 44 | "VREG", | ||
| 45 | } | ||
| 46 | |||
| 47 | -- Maximum number of section buffer positions for dasm_put(). | ||
| 48 | -- CHECK: Keep this in sync with the C code! | ||
| 49 | local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. | ||
| 50 | |||
| 51 | -- Action name -> action number. | ||
| 52 | local map_action = {} | ||
| 53 | for n,name in ipairs(action_names) do | ||
| 54 | map_action[name] = n-1 | ||
| 55 | end | ||
| 56 | |||
| 57 | -- Action list buffer. | ||
| 58 | local actlist = {} | ||
| 59 | |||
| 60 | -- Argument list for next dasm_put(). Start with offset 0 into action list. | ||
| 61 | local actargs = { 0 } | ||
| 62 | |||
| 63 | -- Current number of section buffer positions for dasm_put(). | ||
| 64 | local secpos = 1 | ||
| 65 | |||
| 66 | ------------------------------------------------------------------------------ | ||
| 67 | |||
| 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 | ||
| 77 | |||
| 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 | ||
| 88 | |||
| 89 | ------------------------------------------------------------------------------ | ||
| 90 | |||
| 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 | ||
| 96 | |||
| 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(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 | ||
| 103 | end | ||
| 104 | |||
| 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. | ||
| 112 | end | ||
| 113 | |||
| 114 | -- Put escaped word. | ||
| 115 | local function wputw(n) | ||
| 116 | if n <= 0x000fffff then waction("ESC") end | ||
| 117 | wputxw(n) | ||
| 118 | end | ||
| 119 | |||
| 120 | -- Reserve position for word. | ||
| 121 | local function wpos() | ||
| 122 | local pos = #actlist+1 | ||
| 123 | actlist[pos] = "" | ||
| 124 | return pos | ||
| 125 | end | ||
| 126 | |||
| 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 | if n <= 0x000fffff then | ||
| 131 | insert(actlist, pos+1, n) | ||
| 132 | n = map_action.ESC * 0x10000 | ||
| 133 | end | ||
| 134 | actlist[pos] = n | ||
| 135 | end | ||
| 136 | |||
| 137 | ------------------------------------------------------------------------------ | ||
| 138 | |||
| 139 | -- Global label name -> global label number. With auto assignment on 1st use. | ||
| 140 | local next_global = 20 | ||
| 141 | local map_global = setmetatable({}, { __index = function(t, name) | ||
| 142 | if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end | ||
| 143 | local n = next_global | ||
| 144 | if n > 2047 then werror("too many global labels") end | ||
| 145 | next_global = n + 1 | ||
| 146 | t[name] = n | ||
| 147 | return n | ||
| 148 | end}) | ||
| 149 | |||
| 150 | -- Dump global labels. | ||
| 151 | local function dumpglobals(out, lvl) | ||
| 152 | local t = {} | ||
| 153 | for name, n in pairs(map_global) do t[n] = name end | ||
| 154 | out:write("Global labels:\n") | ||
| 155 | for i=20,next_global-1 do | ||
| 156 | out:write(format(" %s\n", t[i])) | ||
| 157 | end | ||
| 158 | out:write("\n") | ||
| 159 | end | ||
| 160 | |||
| 161 | -- Write global label enum. | ||
| 162 | local function writeglobals(out, prefix) | ||
| 163 | local t = {} | ||
| 164 | for name, n in pairs(map_global) do t[n] = name end | ||
| 165 | out:write("enum {\n") | ||
| 166 | for i=20,next_global-1 do | ||
| 167 | out:write(" ", prefix, t[i], ",\n") | ||
| 168 | end | ||
| 169 | out:write(" ", prefix, "_MAX\n};\n") | ||
| 170 | end | ||
| 171 | |||
| 172 | -- Write global label names. | ||
| 173 | local function writeglobalnames(out, name) | ||
| 174 | local t = {} | ||
| 175 | for name, n in pairs(map_global) do t[n] = name end | ||
| 176 | out:write("static const char *const ", name, "[] = {\n") | ||
| 177 | for i=20,next_global-1 do | ||
| 178 | out:write(" \"", t[i], "\",\n") | ||
| 179 | end | ||
| 180 | out:write(" (const char *)0\n};\n") | ||
| 181 | end | ||
| 182 | |||
| 183 | ------------------------------------------------------------------------------ | ||
| 184 | |||
| 185 | -- Extern label name -> extern label number. With auto assignment on 1st use. | ||
| 186 | local next_extern = 0 | ||
| 187 | local map_extern_ = {} | ||
| 188 | local map_extern = setmetatable({}, { __index = function(t, name) | ||
| 189 | -- No restrictions on the name for now. | ||
| 190 | local n = next_extern | ||
| 191 | if n > 2047 then werror("too many extern labels") end | ||
| 192 | next_extern = n + 1 | ||
| 193 | t[name] = n | ||
| 194 | map_extern_[n] = name | ||
| 195 | return n | ||
| 196 | end}) | ||
| 197 | |||
| 198 | -- Dump extern labels. | ||
| 199 | local function dumpexterns(out, lvl) | ||
| 200 | out:write("Extern labels:\n") | ||
| 201 | for i=0,next_extern-1 do | ||
| 202 | out:write(format(" %s\n", map_extern_[i])) | ||
| 203 | end | ||
| 204 | out:write("\n") | ||
| 205 | end | ||
| 206 | |||
| 207 | -- Write extern label names. | ||
| 208 | local function writeexternnames(out, name) | ||
| 209 | out:write("static const char *const ", name, "[] = {\n") | ||
| 210 | for i=0,next_extern-1 do | ||
| 211 | out:write(" \"", map_extern_[i], "\",\n") | ||
| 212 | end | ||
| 213 | out:write(" (const char *)0\n};\n") | ||
| 214 | end | ||
| 215 | |||
| 216 | ------------------------------------------------------------------------------ | ||
| 217 | |||
| 218 | -- Arch-specific maps. | ||
| 219 | |||
| 220 | -- Ext. register name -> int. name. | ||
| 221 | local map_archdef = { xzr = "@x31", wzr = "@w31", lr = "x30", } | ||
| 222 | |||
| 223 | -- Int. register name -> ext. name. | ||
| 224 | local map_reg_rev = { ["@x31"] = "xzr", ["@w31"] = "wzr", x30 = "lr", } | ||
| 225 | |||
| 226 | local map_type = {} -- Type name -> { ctype, reg } | ||
| 227 | local ctypenum = 0 -- Type number (for Dt... macros). | ||
| 228 | |||
| 229 | -- Reverse defines for registers. | ||
| 230 | function _M.revdef(s) | ||
| 231 | return map_reg_rev[s] or s | ||
| 232 | end | ||
| 233 | |||
| 234 | local map_shift = { lsl = 0, lsr = 1, asr = 2, } | ||
| 235 | |||
| 236 | local map_extend = { | ||
| 237 | uxtb = 0, uxth = 1, uxtw = 2, uxtx = 3, | ||
| 238 | sxtb = 4, sxth = 5, sxtw = 6, sxtx = 7, | ||
| 239 | } | ||
| 240 | |||
| 241 | local map_cond = { | ||
| 242 | eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7, | ||
| 243 | hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14, | ||
| 244 | hs = 2, lo = 3, | ||
| 245 | } | ||
| 246 | |||
| 247 | local map_bti = { | ||
| 248 | c = 0x40, j = 0x80, jc = 0xc0, | ||
| 249 | } | ||
| 250 | |||
| 251 | ------------------------------------------------------------------------------ | ||
| 252 | |||
| 253 | local parse_reg_type | ||
| 254 | |||
| 255 | local function parse_reg(expr, shift, no_vreg) | ||
| 256 | if not expr then werror("expected register name") end | ||
| 257 | local tname, ovreg = match(expr, "^([%w_]+):(@?%l%d+)$") | ||
| 258 | if not tname then | ||
| 259 | tname, ovreg = match(expr, "^([%w_]+):(R[xwqdshb]%b())$") | ||
| 260 | end | ||
| 261 | local tp = map_type[tname or expr] | ||
| 262 | if tp then | ||
| 263 | local reg = ovreg or tp.reg | ||
| 264 | if not reg then | ||
| 265 | werror("type `"..(tname or expr).."' needs a register override") | ||
| 266 | end | ||
| 267 | expr = reg | ||
| 268 | end | ||
| 269 | local ok31, rt, r = match(expr, "^(@?)([xwqdshb])([123]?[0-9])$") | ||
| 270 | if r then | ||
| 271 | r = tonumber(r) | ||
| 272 | if r <= 30 or (r == 31 and ok31 ~= "" or (rt ~= "w" and rt ~= "x")) then | ||
| 273 | if not parse_reg_type then | ||
| 274 | parse_reg_type = rt | ||
| 275 | elseif parse_reg_type ~= rt then | ||
| 276 | werror("register size mismatch") | ||
| 277 | end | ||
| 278 | return shl(r, shift), tp | ||
| 279 | end | ||
| 280 | end | ||
| 281 | local vrt, vreg = match(expr, "^R([xwqdshb])(%b())$") | ||
| 282 | if vreg then | ||
| 283 | if not parse_reg_type then | ||
| 284 | parse_reg_type = vrt | ||
| 285 | elseif parse_reg_type ~= vrt then | ||
| 286 | werror("register size mismatch") | ||
| 287 | end | ||
| 288 | if not no_vreg then waction("VREG", shift, vreg) end | ||
| 289 | return 0 | ||
| 290 | end | ||
| 291 | werror("bad register name `"..expr.."'") | ||
| 292 | end | ||
| 293 | |||
| 294 | local function parse_reg_base(expr) | ||
| 295 | if expr == "sp" then return 0x3e0 end | ||
| 296 | local base, tp = parse_reg(expr, 5) | ||
| 297 | if parse_reg_type ~= "x" then werror("bad register type") end | ||
| 298 | parse_reg_type = false | ||
| 299 | return base, tp | ||
| 300 | end | ||
| 301 | |||
| 302 | local parse_ctx = {} | ||
| 303 | |||
| 304 | local loadenv = setfenv and function(s) | ||
| 305 | local code = loadstring(s, "") | ||
| 306 | if code then setfenv(code, parse_ctx) end | ||
| 307 | return code | ||
| 308 | end or function(s) | ||
| 309 | return load(s, "", nil, parse_ctx) | ||
| 310 | end | ||
| 311 | |||
| 312 | -- Try to parse simple arithmetic, too, since some basic ops are aliases. | ||
| 313 | local function parse_number(n) | ||
| 314 | local x = tonumber(n) | ||
| 315 | if x then return x end | ||
| 316 | local code = loadenv("return "..n) | ||
| 317 | if code then | ||
| 318 | local ok, y = pcall(code) | ||
| 319 | if ok and type(y) == "number" then return y end | ||
| 320 | end | ||
| 321 | return nil | ||
| 322 | end | ||
| 323 | |||
| 324 | local function parse_imm(imm, bits, shift, scale, signed) | ||
| 325 | imm = match(imm, "^#(.*)$") | ||
| 326 | if not imm then werror("expected immediate operand") end | ||
| 327 | local n = parse_number(imm) | ||
| 328 | if n then | ||
| 329 | local m = sar(n, scale) | ||
| 330 | if shl(m, scale) == n then | ||
| 331 | if signed then | ||
| 332 | local s = sar(m, bits-1) | ||
| 333 | if s == 0 then return shl(m, shift) | ||
| 334 | elseif s == -1 then return shl(m + shl(1, bits), shift) end | ||
| 335 | else | ||
| 336 | if sar(m, bits) == 0 then return shl(m, shift) end | ||
| 337 | end | ||
| 338 | end | ||
| 339 | werror("out of range immediate `"..imm.."'") | ||
| 340 | else | ||
| 341 | waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) | ||
| 342 | return 0 | ||
| 343 | end | ||
| 344 | end | ||
| 345 | |||
| 346 | local function parse_imm12(imm) | ||
| 347 | imm = match(imm, "^#(.*)$") | ||
| 348 | if not imm then werror("expected immediate operand") end | ||
| 349 | local n = parse_number(imm) | ||
| 350 | if n then | ||
| 351 | if shr(n, 12) == 0 then | ||
| 352 | return shl(n, 10) | ||
| 353 | elseif band(n, 0xff000fff) == 0 then | ||
| 354 | return shr(n, 2) + 0x00400000 | ||
| 355 | end | ||
| 356 | werror("out of range immediate `"..imm.."'") | ||
| 357 | else | ||
| 358 | waction("IMM12", 0, imm) | ||
| 359 | return 0 | ||
| 360 | end | ||
| 361 | end | ||
| 362 | |||
| 363 | local function parse_imm13(imm) | ||
| 364 | imm = match(imm, "^#(.*)$") | ||
| 365 | if not imm then werror("expected immediate operand") end | ||
| 366 | local n = parse_number(imm) | ||
| 367 | local r64 = parse_reg_type == "x" | ||
| 368 | if n and n % 1 == 0 and n >= 0 and n <= 0xffffffff then | ||
| 369 | local inv = false | ||
| 370 | if band(n, 1) == 1 then n = bit.bnot(n); inv = true end | ||
| 371 | local t = {} | ||
| 372 | for i=1,32 do t[i] = band(n, 1); n = shr(n, 1) end | ||
| 373 | local b = table.concat(t) | ||
| 374 | b = b..(r64 and (inv and "1" or "0"):rep(32) or b) | ||
| 375 | local p0, p1, p0a, p1a = b:match("^(0+)(1+)(0*)(1*)") | ||
| 376 | if p0 then | ||
| 377 | local w = p1a == "" and (r64 and 64 or 32) or #p1+#p0a | ||
| 378 | if band(w, w-1) == 0 and b == b:sub(1, w):rep(64/w) then | ||
| 379 | local s = band(-2*w, 0x3f) - 1 | ||
| 380 | if w == 64 then s = s + 0x1000 end | ||
| 381 | if inv then | ||
| 382 | return shl(w-#p1-#p0, 16) + shl(s+w-#p1, 10) | ||
| 383 | else | ||
| 384 | return shl(w-#p0, 16) + shl(s+#p1, 10) | ||
| 385 | end | ||
| 386 | end | ||
| 387 | end | ||
| 388 | werror("out of range immediate `"..imm.."'") | ||
| 389 | elseif r64 then | ||
| 390 | waction("IMM13X", 0, format("(unsigned int)(%s)", imm)) | ||
| 391 | actargs[#actargs+1] = format("(unsigned int)((unsigned long long)(%s)>>32)", imm) | ||
| 392 | return 0 | ||
| 393 | else | ||
| 394 | waction("IMM13W", 0, imm) | ||
| 395 | return 0 | ||
| 396 | end | ||
| 397 | end | ||
| 398 | |||
| 399 | local function parse_imm6(imm) | ||
| 400 | imm = match(imm, "^#(.*)$") | ||
| 401 | if not imm then werror("expected immediate operand") end | ||
| 402 | local n = parse_number(imm) | ||
| 403 | if n then | ||
| 404 | if n >= 0 and n <= 63 then | ||
| 405 | return shl(band(n, 0x1f), 19) + (n >= 32 and 0x80000000 or 0) | ||
| 406 | end | ||
| 407 | werror("out of range immediate `"..imm.."'") | ||
| 408 | else | ||
| 409 | waction("IMM6", 0, imm) | ||
| 410 | return 0 | ||
| 411 | end | ||
| 412 | end | ||
| 413 | |||
| 414 | local function parse_imm_load(imm, scale) | ||
| 415 | local n = parse_number(imm) | ||
| 416 | if n then | ||
| 417 | local m = sar(n, scale) | ||
| 418 | if shl(m, scale) == n and m >= 0 and m < 0x1000 then | ||
| 419 | return shl(m, 10) + 0x01000000 -- Scaled, unsigned 12 bit offset. | ||
| 420 | elseif n >= -256 and n < 256 then | ||
| 421 | return shl(band(n, 511), 12) -- Unscaled, signed 9 bit offset. | ||
| 422 | end | ||
| 423 | werror("out of range immediate `"..imm.."'") | ||
| 424 | else | ||
| 425 | waction("IMML", scale, imm) | ||
| 426 | return 0 | ||
| 427 | end | ||
| 428 | end | ||
| 429 | |||
| 430 | local function parse_fpimm(imm) | ||
| 431 | imm = match(imm, "^#(.*)$") | ||
| 432 | if not imm then werror("expected immediate operand") end | ||
| 433 | local n = parse_number(imm) | ||
| 434 | if n then | ||
| 435 | local m, e = math.frexp(n) | ||
| 436 | local s, e2 = 0, band(e-2, 7) | ||
| 437 | if m < 0 then m = -m; s = 0x00100000 end | ||
| 438 | m = m*32-16 | ||
| 439 | if m % 1 == 0 and m >= 0 and m <= 15 and sar(shl(e2, 29), 29)+2 == e then | ||
| 440 | return s + shl(e2, 17) + shl(m, 13) | ||
| 441 | end | ||
| 442 | werror("out of range immediate `"..imm.."'") | ||
| 443 | else | ||
| 444 | werror("NYI fpimm action") | ||
| 445 | end | ||
| 446 | end | ||
| 447 | |||
| 448 | local function parse_shift(expr) | ||
| 449 | local s, s2 = match(expr, "^(%S+)%s*(.*)$") | ||
| 450 | s = map_shift[s] | ||
| 451 | if not s then werror("expected shift operand") end | ||
| 452 | return parse_imm(s2, 6, 10, 0, false) + shl(s, 22) | ||
| 453 | end | ||
| 454 | |||
| 455 | local function parse_lslx16(expr) | ||
| 456 | local n = match(expr, "^lsl%s*#(%d+)$") | ||
| 457 | n = tonumber(n) | ||
| 458 | if not n then werror("expected shift operand") end | ||
| 459 | if band(n, parse_reg_type == "x" and 0xffffffcf or 0xffffffef) ~= 0 then | ||
| 460 | werror("bad shift amount") | ||
| 461 | end | ||
| 462 | return shl(n, 17) | ||
| 463 | end | ||
| 464 | |||
| 465 | local function parse_extend(expr) | ||
| 466 | local s, s2 = match(expr, "^(%S+)%s*(.*)$") | ||
| 467 | if s == "lsl" then | ||
| 468 | s = parse_reg_type == "x" and 3 or 2 | ||
| 469 | else | ||
| 470 | s = map_extend[s] | ||
| 471 | end | ||
| 472 | if not s then werror("expected extend operand") end | ||
| 473 | return (s2 == "" and 0 or parse_imm(s2, 3, 10, 0, false)) + shl(s, 13) | ||
| 474 | end | ||
| 475 | |||
| 476 | local function parse_cond(expr, inv) | ||
| 477 | local c = map_cond[expr] | ||
| 478 | if not c then werror("expected condition operand") end | ||
| 479 | return shl(bit.bxor(c, inv), 12) | ||
| 480 | end | ||
| 481 | |||
| 482 | local function parse_map(expr, map) | ||
| 483 | local x = map[expr] | ||
| 484 | if not x then werror("bad operand") end | ||
| 485 | return x | ||
| 486 | end | ||
| 487 | |||
| 488 | local function parse_load(params, nparams, n, op) | ||
| 489 | if params[n+2] then werror("too many operands") end | ||
| 490 | local scale = shr(op, 30) | ||
| 491 | local pn, p2 = params[n], params[n+1] | ||
| 492 | local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") | ||
| 493 | if not p1 then | ||
| 494 | if not p2 then | ||
| 495 | local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") | ||
| 496 | if reg and tailr ~= "" then | ||
| 497 | local base, tp = parse_reg_base(reg) | ||
| 498 | if tp then | ||
| 499 | waction("IMML", scale, format(tp.ctypefmt, tailr)) | ||
| 500 | return op + base | ||
| 501 | end | ||
| 502 | end | ||
| 503 | end | ||
| 504 | werror("expected address operand") | ||
| 505 | end | ||
| 506 | if p2 then | ||
| 507 | if wb == "!" then werror("bad use of '!'") end | ||
| 508 | op = op + parse_reg_base(p1) + parse_imm(p2, 9, 12, 0, true) + 0x400 | ||
| 509 | elseif wb == "!" then | ||
| 510 | local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$") | ||
| 511 | if not p1a then werror("bad use of '!'") end | ||
| 512 | op = op + parse_reg_base(p1a) + parse_imm(p2a, 9, 12, 0, true) + 0xc00 | ||
| 513 | else | ||
| 514 | local p1a, p2a = match(p1, "^([^,%s]*)%s*(.*)$") | ||
| 515 | op = op + parse_reg_base(p1a) | ||
| 516 | if p2a ~= "" then | ||
| 517 | local imm = match(p2a, "^,%s*#(.*)$") | ||
| 518 | if imm then | ||
| 519 | op = op + parse_imm_load(imm, scale) | ||
| 520 | else | ||
| 521 | local p2b, p3b, p3s = match(p2a, "^,%s*([^,%s]*)%s*,?%s*(%S*)%s*(.*)$") | ||
| 522 | op = op + parse_reg(p2b, 16) + 0x00200800 | ||
| 523 | if parse_reg_type ~= "x" and parse_reg_type ~= "w" then | ||
| 524 | werror("bad index register type") | ||
| 525 | end | ||
| 526 | if p3b == "" then | ||
| 527 | if parse_reg_type ~= "x" then werror("bad index register type") end | ||
| 528 | op = op + 0x6000 | ||
| 529 | else | ||
| 530 | if p3s == "" or p3s == "#0" then | ||
| 531 | elseif p3s == "#"..scale then | ||
| 532 | op = op + 0x1000 | ||
| 533 | else | ||
| 534 | werror("bad scale") | ||
| 535 | end | ||
| 536 | if parse_reg_type == "x" then | ||
| 537 | if p3b == "lsl" and p3s ~= "" then op = op + 0x6000 | ||
| 538 | elseif p3b == "sxtx" then op = op + 0xe000 | ||
| 539 | else | ||
| 540 | werror("bad extend/shift specifier") | ||
| 541 | end | ||
| 542 | else | ||
| 543 | if p3b == "uxtw" then op = op + 0x4000 | ||
| 544 | elseif p3b == "sxtw" then op = op + 0xc000 | ||
| 545 | else | ||
| 546 | werror("bad extend/shift specifier") | ||
| 547 | end | ||
| 548 | end | ||
| 549 | end | ||
| 550 | end | ||
| 551 | else | ||
| 552 | if wb == "!" then werror("bad use of '!'") end | ||
| 553 | op = op + 0x01000000 | ||
| 554 | end | ||
| 555 | end | ||
| 556 | return op | ||
| 557 | end | ||
| 558 | |||
| 559 | local function parse_load_pair(params, nparams, n, op) | ||
| 560 | if params[n+2] then werror("too many operands") end | ||
| 561 | local pn, p2 = params[n], params[n+1] | ||
| 562 | local scale = 2 + shr(op, 31 - band(shr(op, 26), 1)) | ||
| 563 | local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") | ||
| 564 | if not p1 then | ||
| 565 | if not p2 then | ||
| 566 | local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") | ||
| 567 | if reg and tailr ~= "" then | ||
| 568 | local base, tp = parse_reg_base(reg) | ||
| 569 | if tp then | ||
| 570 | waction("IMM", 32768+7*32+15+scale*1024, format(tp.ctypefmt, tailr)) | ||
| 571 | return op + base + 0x01000000 | ||
| 572 | end | ||
| 573 | end | ||
| 574 | end | ||
| 575 | werror("expected address operand") | ||
| 576 | end | ||
| 577 | if p2 then | ||
| 578 | if wb == "!" then werror("bad use of '!'") end | ||
| 579 | op = op + 0x00800000 | ||
| 580 | else | ||
| 581 | local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$") | ||
| 582 | if p1a then p1, p2 = p1a, p2a else p2 = "#0" end | ||
| 583 | op = op + (wb == "!" and 0x01800000 or 0x01000000) | ||
| 584 | end | ||
| 585 | return op + parse_reg_base(p1) + parse_imm(p2, 7, 15, scale, true) | ||
| 586 | end | ||
| 587 | |||
| 588 | local function parse_label(label, def) | ||
| 589 | local prefix = label:sub(1, 2) | ||
| 590 | -- =>label (pc label reference) | ||
| 591 | if prefix == "=>" then | ||
| 592 | return "PC", 0, label:sub(3) | ||
| 593 | end | ||
| 594 | -- ->name (global label reference) | ||
| 595 | if prefix == "->" then | ||
| 596 | return "LG", map_global[label:sub(3)] | ||
| 597 | end | ||
| 598 | if def then | ||
| 599 | -- [1-9] (local label definition) | ||
| 600 | if match(label, "^[1-9]$") then | ||
| 601 | return "LG", 10+tonumber(label) | ||
| 602 | end | ||
| 603 | else | ||
| 604 | -- [<>][1-9] (local label reference) | ||
| 605 | local dir, lnum = match(label, "^([<>])([1-9])$") | ||
| 606 | if dir then -- Fwd: 1-9, Bkwd: 11-19. | ||
| 607 | return "LG", lnum + (dir == ">" and 0 or 10) | ||
| 608 | end | ||
| 609 | -- extern label (extern label reference) | ||
| 610 | local extname = match(label, "^extern%s+(%S+)$") | ||
| 611 | if extname then | ||
| 612 | return "EXT", map_extern[extname] | ||
| 613 | end | ||
| 614 | -- &expr (pointer) | ||
| 615 | if label:sub(1, 1) == "&" then | ||
| 616 | return "A", 0, format("(ptrdiff_t)(%s)", label:sub(2)) | ||
| 617 | end | ||
| 618 | end | ||
| 619 | end | ||
| 620 | |||
| 621 | local function branch_type(op) | ||
| 622 | if band(op, 0x7c000000) == 0x14000000 then return 0 -- B, BL | ||
| 623 | elseif shr(op, 24) == 0x54 or band(op, 0x7e000000) == 0x34000000 or | ||
| 624 | band(op, 0x3b000000) == 0x18000000 then | ||
| 625 | return 0x800 -- B.cond, CBZ, CBNZ, LDR* literal | ||
| 626 | elseif band(op, 0x7e000000) == 0x36000000 then return 0x1000 -- TBZ, TBNZ | ||
| 627 | elseif band(op, 0x9f000000) == 0x10000000 then return 0x2000 -- ADR | ||
| 628 | elseif band(op, 0x9f000000) == band(0x90000000) then return 0x3000 -- ADRP | ||
| 629 | else | ||
| 630 | assert(false, "unknown branch type") | ||
| 631 | end | ||
| 632 | end | ||
| 633 | |||
| 634 | ------------------------------------------------------------------------------ | ||
| 635 | |||
| 636 | local map_op, op_template | ||
| 637 | |||
| 638 | local function op_alias(opname, f) | ||
| 639 | return function(params, nparams) | ||
| 640 | if not params then return "-> "..opname:sub(1, -3) end | ||
| 641 | f(params, nparams) | ||
| 642 | op_template(params, map_op[opname], nparams) | ||
| 643 | end | ||
| 644 | end | ||
| 645 | |||
| 646 | local function alias_bfx(p) | ||
| 647 | p[4] = "#("..p[3]:sub(2)..")+("..p[4]:sub(2)..")-1" | ||
| 648 | end | ||
| 649 | |||
| 650 | local function alias_bfiz(p) | ||
| 651 | parse_reg(p[1], 0, true) | ||
| 652 | if parse_reg_type == "w" then | ||
| 653 | p[3] = "#(32-("..p[3]:sub(2).."))%32" | ||
| 654 | p[4] = "#("..p[4]:sub(2)..")-1" | ||
| 655 | else | ||
| 656 | p[3] = "#(64-("..p[3]:sub(2).."))%64" | ||
| 657 | p[4] = "#("..p[4]:sub(2)..")-1" | ||
| 658 | end | ||
| 659 | end | ||
| 660 | |||
| 661 | local alias_lslimm = op_alias("ubfm_4", function(p) | ||
| 662 | parse_reg(p[1], 0, true) | ||
| 663 | local sh = p[3]:sub(2) | ||
| 664 | if parse_reg_type == "w" then | ||
| 665 | p[3] = "#(32-("..sh.."))%32" | ||
| 666 | p[4] = "#31-("..sh..")" | ||
| 667 | else | ||
| 668 | p[3] = "#(64-("..sh.."))%64" | ||
| 669 | p[4] = "#63-("..sh..")" | ||
| 670 | end | ||
| 671 | end) | ||
| 672 | |||
| 673 | -- Template strings for ARM instructions. | ||
| 674 | map_op = { | ||
| 675 | -- Basic data processing instructions. | ||
| 676 | add_3 = "0b000000DNMg|11000000pDpNIg|8b206000pDpNMx", | ||
| 677 | add_4 = "0b000000DNMSg|0b200000DNMXg|8b200000pDpNMXx|8b200000pDpNxMwX", | ||
| 678 | adds_3 = "2b000000DNMg|31000000DpNIg|ab206000DpNMx", | ||
| 679 | adds_4 = "2b000000DNMSg|2b200000DNMXg|ab200000DpNMXx|ab200000DpNxMwX", | ||
| 680 | cmn_2 = "2b00001fNMg|3100001fpNIg|ab20601fpNMx", | ||
| 681 | cmn_3 = "2b00001fNMSg|2b20001fNMXg|ab20001fpNMXx|ab20001fpNxMwX", | ||
| 682 | |||
| 683 | sub_3 = "4b000000DNMg|51000000pDpNIg|cb206000pDpNMx", | ||
| 684 | sub_4 = "4b000000DNMSg|4b200000DNMXg|cb200000pDpNMXx|cb200000pDpNxMwX", | ||
| 685 | subs_3 = "6b000000DNMg|71000000DpNIg|eb206000DpNMx", | ||
| 686 | subs_4 = "6b000000DNMSg|6b200000DNMXg|eb200000DpNMXx|eb200000DpNxMwX", | ||
| 687 | cmp_2 = "6b00001fNMg|7100001fpNIg|eb20601fpNMx", | ||
| 688 | cmp_3 = "6b00001fNMSg|6b20001fNMXg|eb20001fpNMXx|eb20001fpNxMwX", | ||
| 689 | |||
| 690 | neg_2 = "4b0003e0DMg", | ||
| 691 | neg_3 = "4b0003e0DMSg", | ||
| 692 | negs_2 = "6b0003e0DMg", | ||
| 693 | negs_3 = "6b0003e0DMSg", | ||
| 694 | |||
| 695 | adc_3 = "1a000000DNMg", | ||
| 696 | adcs_3 = "3a000000DNMg", | ||
| 697 | sbc_3 = "5a000000DNMg", | ||
| 698 | sbcs_3 = "7a000000DNMg", | ||
| 699 | ngc_2 = "5a0003e0DMg", | ||
| 700 | ngcs_2 = "7a0003e0DMg", | ||
| 701 | |||
| 702 | and_3 = "0a000000DNMg|12000000pDNig", | ||
| 703 | and_4 = "0a000000DNMSg", | ||
| 704 | orr_3 = "2a000000DNMg|32000000pDNig", | ||
| 705 | orr_4 = "2a000000DNMSg", | ||
| 706 | eor_3 = "4a000000DNMg|52000000pDNig", | ||
| 707 | eor_4 = "4a000000DNMSg", | ||
| 708 | ands_3 = "6a000000DNMg|72000000DNig", | ||
| 709 | ands_4 = "6a000000DNMSg", | ||
| 710 | tst_2 = "6a00001fNMg|7200001fNig", | ||
| 711 | tst_3 = "6a00001fNMSg", | ||
| 712 | |||
| 713 | bic_3 = "0a200000DNMg", | ||
| 714 | bic_4 = "0a200000DNMSg", | ||
| 715 | orn_3 = "2a200000DNMg", | ||
| 716 | orn_4 = "2a200000DNMSg", | ||
| 717 | eon_3 = "4a200000DNMg", | ||
| 718 | eon_4 = "4a200000DNMSg", | ||
| 719 | bics_3 = "6a200000DNMg", | ||
| 720 | bics_4 = "6a200000DNMSg", | ||
| 721 | |||
| 722 | movn_2 = "12800000DWg", | ||
| 723 | movn_3 = "12800000DWRg", | ||
| 724 | movz_2 = "52800000DWg", | ||
| 725 | movz_3 = "52800000DWRg", | ||
| 726 | movk_2 = "72800000DWg", | ||
| 727 | movk_3 = "72800000DWRg", | ||
| 728 | |||
| 729 | -- TODO: this doesn't cover all valid immediates for mov reg, #imm. | ||
| 730 | mov_2 = "2a0003e0DMg|52800000DW|320003e0pDig|11000000pDpNg", | ||
| 731 | mov_3 = "2a0003e0DMSg", | ||
| 732 | mvn_2 = "2a2003e0DMg", | ||
| 733 | mvn_3 = "2a2003e0DMSg", | ||
| 734 | |||
| 735 | adr_2 = "10000000DBx", | ||
| 736 | adrp_2 = "90000000DBx", | ||
| 737 | |||
| 738 | csel_4 = "1a800000DNMCg", | ||
| 739 | csinc_4 = "1a800400DNMCg", | ||
| 740 | csinv_4 = "5a800000DNMCg", | ||
| 741 | csneg_4 = "5a800400DNMCg", | ||
| 742 | cset_2 = "1a9f07e0Dcg", | ||
| 743 | csetm_2 = "5a9f03e0Dcg", | ||
| 744 | cinc_3 = "1a800400DNmcg", | ||
| 745 | cinv_3 = "5a800000DNmcg", | ||
| 746 | cneg_3 = "5a800400DNmcg", | ||
| 747 | |||
| 748 | ccmn_4 = "3a400000NMVCg|3a400800N5VCg", | ||
| 749 | ccmp_4 = "7a400000NMVCg|7a400800N5VCg", | ||
| 750 | |||
| 751 | madd_4 = "1b000000DNMAg", | ||
| 752 | msub_4 = "1b008000DNMAg", | ||
| 753 | mul_3 = "1b007c00DNMg", | ||
| 754 | mneg_3 = "1b00fc00DNMg", | ||
| 755 | |||
| 756 | smaddl_4 = "9b200000DxNMwAx", | ||
| 757 | smsubl_4 = "9b208000DxNMwAx", | ||
| 758 | smull_3 = "9b207c00DxNMw", | ||
| 759 | smnegl_3 = "9b20fc00DxNMw", | ||
| 760 | smulh_3 = "9b407c00DNMx", | ||
| 761 | umaddl_4 = "9ba00000DxNMwAx", | ||
| 762 | umsubl_4 = "9ba08000DxNMwAx", | ||
| 763 | umull_3 = "9ba07c00DxNMw", | ||
| 764 | umnegl_3 = "9ba0fc00DxNMw", | ||
| 765 | umulh_3 = "9bc07c00DNMx", | ||
| 766 | |||
| 767 | udiv_3 = "1ac00800DNMg", | ||
| 768 | sdiv_3 = "1ac00c00DNMg", | ||
| 769 | |||
| 770 | -- Bit operations. | ||
| 771 | sbfm_4 = "13000000DN12w|93400000DN12x", | ||
| 772 | bfm_4 = "33000000DN12w|b3400000DN12x", | ||
| 773 | ubfm_4 = "53000000DN12w|d3400000DN12x", | ||
| 774 | extr_4 = "13800000DNM2w|93c00000DNM2x", | ||
| 775 | |||
| 776 | sxtb_2 = "13001c00DNw|93401c00DNx", | ||
| 777 | sxth_2 = "13003c00DNw|93403c00DNx", | ||
| 778 | sxtw_2 = "93407c00DxNw", | ||
| 779 | uxtb_2 = "53001c00DNw", | ||
| 780 | uxth_2 = "53003c00DNw", | ||
| 781 | |||
| 782 | sbfx_4 = op_alias("sbfm_4", alias_bfx), | ||
| 783 | bfxil_4 = op_alias("bfm_4", alias_bfx), | ||
| 784 | ubfx_4 = op_alias("ubfm_4", alias_bfx), | ||
| 785 | sbfiz_4 = op_alias("sbfm_4", alias_bfiz), | ||
| 786 | bfi_4 = op_alias("bfm_4", alias_bfiz), | ||
| 787 | ubfiz_4 = op_alias("ubfm_4", alias_bfiz), | ||
| 788 | |||
| 789 | lsl_3 = function(params, nparams) | ||
| 790 | if params and params[3]:byte() == 35 then | ||
| 791 | return alias_lslimm(params, nparams) | ||
| 792 | else | ||
| 793 | return op_template(params, "1ac02000DNMg", nparams) | ||
| 794 | end | ||
| 795 | end, | ||
| 796 | lsr_3 = "1ac02400DNMg|53007c00DN1w|d340fc00DN1x", | ||
| 797 | asr_3 = "1ac02800DNMg|13007c00DN1w|9340fc00DN1x", | ||
| 798 | ror_3 = "1ac02c00DNMg|13800000DNm2w|93c00000DNm2x", | ||
| 799 | |||
| 800 | clz_2 = "5ac01000DNg", | ||
| 801 | cls_2 = "5ac01400DNg", | ||
| 802 | rbit_2 = "5ac00000DNg", | ||
| 803 | rev_2 = "5ac00800DNw|dac00c00DNx", | ||
| 804 | rev16_2 = "5ac00400DNg", | ||
| 805 | rev32_2 = "dac00800DNx", | ||
| 806 | |||
| 807 | -- Loads and stores. | ||
| 808 | ["strb_*"] = "38000000DwL", | ||
| 809 | ["ldrb_*"] = "38400000DwL", | ||
| 810 | ["ldrsb_*"] = "38c00000DwL|38800000DxL", | ||
| 811 | ["strh_*"] = "78000000DwL", | ||
| 812 | ["ldrh_*"] = "78400000DwL", | ||
| 813 | ["ldrsh_*"] = "78c00000DwL|78800000DxL", | ||
| 814 | ["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL", | ||
| 815 | ["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL", | ||
| 816 | ["ldrsw_*"] = "98000000DxB|b8800000DxL", | ||
| 817 | -- NOTE: ldur etc. are handled by ldr et al. | ||
| 818 | |||
| 819 | ["stp_*"] = "28000000DAwP|a8000000DAxP|2c000000DAsP|6c000000DAdP|ac000000DAqP", | ||
| 820 | ["ldp_*"] = "28400000DAwP|a8400000DAxP|2c400000DAsP|6c400000DAdP|ac400000DAqP", | ||
| 821 | ["ldpsw_*"] = "68400000DAxP", | ||
| 822 | |||
| 823 | -- Branches. | ||
| 824 | b_1 = "14000000B", | ||
| 825 | bl_1 = "94000000B", | ||
| 826 | blr_1 = "d63f0000Nx", | ||
| 827 | br_1 = "d61f0000Nx", | ||
| 828 | ret_0 = "d65f03c0", | ||
| 829 | ret_1 = "d65f0000Nx", | ||
| 830 | -- b.cond is added below. | ||
| 831 | cbz_2 = "34000000DBg", | ||
| 832 | cbnz_2 = "35000000DBg", | ||
| 833 | tbz_3 = "36000000DTBw|36000000DTBx", | ||
| 834 | tbnz_3 = "37000000DTBw|37000000DTBx", | ||
| 835 | |||
| 836 | -- Branch Target Identification. | ||
| 837 | bti_1 = "d503241ft", | ||
| 838 | |||
| 839 | -- ARM64e: Pointer authentication codes (PAC). | ||
| 840 | blraaz_1 = "d63f081fNx", | ||
| 841 | blrabz_1 = "d63f0c1fNx", | ||
| 842 | braa_2 = "d71f0800NDx", | ||
| 843 | brab_2 = "d71f0c00NDx", | ||
| 844 | braaz_1 = "d61f081fNx", | ||
| 845 | brabz_1 = "d61f0c1fNx", | ||
| 846 | paciasp_0 = "d503233f", | ||
| 847 | pacibsp_0 = "d503237f", | ||
| 848 | autiasp_0 = "d50323bf", | ||
| 849 | autibsp_0 = "d50323ff", | ||
| 850 | retaa_0 = "d65f0bff", | ||
| 851 | retab_0 = "d65f0fff", | ||
| 852 | |||
| 853 | -- Miscellaneous instructions. | ||
| 854 | -- TODO: hlt, hvc, smc, svc, eret, dcps[123], drps, mrs, msr | ||
| 855 | -- TODO: sys, sysl, ic, dc, at, tlbi | ||
| 856 | -- TODO: hint, yield, wfe, wfi, sev, sevl | ||
| 857 | -- TODO: clrex, dsb, dmb, isb | ||
| 858 | nop_0 = "d503201f", | ||
| 859 | brk_0 = "d4200000", | ||
| 860 | brk_1 = "d4200000W", | ||
| 861 | |||
| 862 | -- Floating point instructions. | ||
| 863 | fmov_2 = "1e204000DNf|1e260000DwNs|1e270000DsNw|9e660000DxNd|9e670000DdNx|1e201000DFf", | ||
| 864 | fabs_2 = "1e20c000DNf", | ||
| 865 | fneg_2 = "1e214000DNf", | ||
| 866 | fsqrt_2 = "1e21c000DNf", | ||
| 867 | |||
| 868 | fcvt_2 = "1e22c000DdNs|1e624000DsNd", | ||
| 869 | |||
| 870 | -- TODO: half-precision and fixed-point conversions. | ||
| 871 | fcvtas_2 = "1e240000DwNs|9e240000DxNs|1e640000DwNd|9e640000DxNd", | ||
| 872 | fcvtau_2 = "1e250000DwNs|9e250000DxNs|1e650000DwNd|9e650000DxNd", | ||
| 873 | fcvtms_2 = "1e300000DwNs|9e300000DxNs|1e700000DwNd|9e700000DxNd", | ||
| 874 | fcvtmu_2 = "1e310000DwNs|9e310000DxNs|1e710000DwNd|9e710000DxNd", | ||
| 875 | fcvtns_2 = "1e200000DwNs|9e200000DxNs|1e600000DwNd|9e600000DxNd", | ||
| 876 | fcvtnu_2 = "1e210000DwNs|9e210000DxNs|1e610000DwNd|9e610000DxNd", | ||
| 877 | fcvtps_2 = "1e280000DwNs|9e280000DxNs|1e680000DwNd|9e680000DxNd", | ||
| 878 | fcvtpu_2 = "1e290000DwNs|9e290000DxNs|1e690000DwNd|9e690000DxNd", | ||
| 879 | fcvtzs_2 = "1e380000DwNs|9e380000DxNs|1e780000DwNd|9e780000DxNd", | ||
| 880 | fcvtzu_2 = "1e390000DwNs|9e390000DxNs|1e790000DwNd|9e790000DxNd", | ||
| 881 | |||
| 882 | scvtf_2 = "1e220000DsNw|9e220000DsNx|1e620000DdNw|9e620000DdNx", | ||
| 883 | ucvtf_2 = "1e230000DsNw|9e230000DsNx|1e630000DdNw|9e630000DdNx", | ||
| 884 | |||
| 885 | frintn_2 = "1e244000DNf", | ||
| 886 | frintp_2 = "1e24c000DNf", | ||
| 887 | frintm_2 = "1e254000DNf", | ||
| 888 | frintz_2 = "1e25c000DNf", | ||
| 889 | frinta_2 = "1e264000DNf", | ||
| 890 | frintx_2 = "1e274000DNf", | ||
| 891 | frinti_2 = "1e27c000DNf", | ||
| 892 | |||
| 893 | fadd_3 = "1e202800DNMf", | ||
| 894 | fsub_3 = "1e203800DNMf", | ||
| 895 | fmul_3 = "1e200800DNMf", | ||
| 896 | fnmul_3 = "1e208800DNMf", | ||
| 897 | fdiv_3 = "1e201800DNMf", | ||
| 898 | |||
| 899 | fmadd_4 = "1f000000DNMAf", | ||
| 900 | fmsub_4 = "1f008000DNMAf", | ||
| 901 | fnmadd_4 = "1f200000DNMAf", | ||
| 902 | fnmsub_4 = "1f208000DNMAf", | ||
| 903 | |||
| 904 | fmax_3 = "1e204800DNMf", | ||
| 905 | fmaxnm_3 = "1e206800DNMf", | ||
| 906 | fmin_3 = "1e205800DNMf", | ||
| 907 | fminnm_3 = "1e207800DNMf", | ||
| 908 | |||
| 909 | fcmp_2 = "1e202000NMf|1e202008NZf", | ||
| 910 | fcmpe_2 = "1e202010NMf|1e202018NZf", | ||
| 911 | |||
| 912 | fccmp_4 = "1e200400NMVCf", | ||
| 913 | fccmpe_4 = "1e200410NMVCf", | ||
| 914 | |||
| 915 | fcsel_4 = "1e200c00DNMCf", | ||
| 916 | |||
| 917 | -- TODO: crc32*, aes*, sha*, pmull | ||
| 918 | -- TODO: SIMD instructions. | ||
| 919 | } | ||
| 920 | |||
| 921 | for cond,c in pairs(map_cond) do | ||
| 922 | map_op["b"..cond.."_1"] = tohex(0x54000000+c).."B" | ||
| 923 | end | ||
| 924 | |||
| 925 | ------------------------------------------------------------------------------ | ||
| 926 | |||
| 927 | -- Handle opcodes defined with template strings. | ||
| 928 | local function parse_template(params, template, nparams, pos) | ||
| 929 | local op = tonumber(template:sub(1, 8), 16) | ||
| 930 | local n = 1 | ||
| 931 | local rtt = {} | ||
| 932 | |||
| 933 | parse_reg_type = false | ||
| 934 | |||
| 935 | -- Process each character. | ||
| 936 | for p in gmatch(template:sub(9), ".") do | ||
| 937 | local q = params[n] | ||
| 938 | if p == "D" then | ||
| 939 | op = op + parse_reg(q, 0); n = n + 1 | ||
| 940 | elseif p == "N" then | ||
| 941 | op = op + parse_reg(q, 5); n = n + 1 | ||
| 942 | elseif p == "M" then | ||
| 943 | op = op + parse_reg(q, 16); n = n + 1 | ||
| 944 | elseif p == "A" then | ||
| 945 | op = op + parse_reg(q, 10); n = n + 1 | ||
| 946 | elseif p == "m" then | ||
| 947 | op = op + parse_reg(params[n-1], 16) | ||
| 948 | |||
| 949 | elseif p == "p" then | ||
| 950 | if q == "sp" then params[n] = "@x31" end | ||
| 951 | elseif p == "g" then | ||
| 952 | if parse_reg_type == "x" then | ||
| 953 | op = op + 0x80000000 | ||
| 954 | elseif parse_reg_type ~= "w" then | ||
| 955 | werror("bad register type") | ||
| 956 | end | ||
| 957 | parse_reg_type = false | ||
| 958 | elseif p == "f" then | ||
| 959 | if parse_reg_type == "d" then | ||
| 960 | op = op + 0x00400000 | ||
| 961 | elseif parse_reg_type ~= "s" then | ||
| 962 | werror("bad register type") | ||
| 963 | end | ||
| 964 | parse_reg_type = false | ||
| 965 | elseif p == "x" or p == "w" or p == "d" or p == "s" or p == "q" then | ||
| 966 | if parse_reg_type ~= p then | ||
| 967 | werror("register size mismatch") | ||
| 968 | end | ||
| 969 | parse_reg_type = false | ||
| 970 | |||
| 971 | elseif p == "L" then | ||
| 972 | op = parse_load(params, nparams, n, op) | ||
| 973 | elseif p == "P" then | ||
| 974 | op = parse_load_pair(params, nparams, n, op) | ||
| 975 | |||
| 976 | elseif p == "B" then | ||
| 977 | local mode, v, s = parse_label(q, false); n = n + 1 | ||
| 978 | if not mode then werror("bad label `"..q.."'") end | ||
| 979 | local m = branch_type(op) | ||
| 980 | if mode == "A" then | ||
| 981 | waction("REL_"..mode, v+m, format("(unsigned int)(%s)", s)) | ||
| 982 | actargs[#actargs+1] = format("(unsigned int)((%s)>>32)", s) | ||
| 983 | else | ||
| 984 | waction("REL_"..mode, v+m, s, 1) | ||
| 985 | end | ||
| 986 | |||
| 987 | elseif p == "I" then | ||
| 988 | op = op + parse_imm12(q); n = n + 1 | ||
| 989 | elseif p == "i" then | ||
| 990 | op = op + parse_imm13(q); n = n + 1 | ||
| 991 | elseif p == "W" then | ||
| 992 | op = op + parse_imm(q, 16, 5, 0, false); n = n + 1 | ||
| 993 | elseif p == "T" then | ||
| 994 | op = op + parse_imm6(q); n = n + 1 | ||
| 995 | elseif p == "1" then | ||
| 996 | op = op + parse_imm(q, 6, 16, 0, false); n = n + 1 | ||
| 997 | elseif p == "2" then | ||
| 998 | op = op + parse_imm(q, 6, 10, 0, false); n = n + 1 | ||
| 999 | elseif p == "5" then | ||
| 1000 | op = op + parse_imm(q, 5, 16, 0, false); n = n + 1 | ||
| 1001 | elseif p == "V" then | ||
| 1002 | op = op + parse_imm(q, 4, 0, 0, false); n = n + 1 | ||
| 1003 | elseif p == "F" then | ||
| 1004 | op = op + parse_fpimm(q); n = n + 1 | ||
| 1005 | elseif p == "Z" then | ||
| 1006 | if q ~= "#0" and q ~= "#0.0" then werror("expected zero immediate") end | ||
| 1007 | n = n + 1 | ||
| 1008 | |||
| 1009 | elseif p == "S" then | ||
| 1010 | op = op + parse_shift(q); n = n + 1 | ||
| 1011 | elseif p == "X" then | ||
| 1012 | op = op + parse_extend(q); n = n + 1 | ||
| 1013 | elseif p == "R" then | ||
| 1014 | op = op + parse_lslx16(q); n = n + 1 | ||
| 1015 | elseif p == "C" then | ||
| 1016 | op = op + parse_cond(q, 0); n = n + 1 | ||
| 1017 | elseif p == "c" then | ||
| 1018 | op = op + parse_cond(q, 1); n = n + 1 | ||
| 1019 | elseif p == "t" then | ||
| 1020 | op = op + parse_map(q, map_bti); n = n + 1 | ||
| 1021 | |||
| 1022 | else | ||
| 1023 | assert(false) | ||
| 1024 | end | ||
| 1025 | end | ||
| 1026 | wputpos(pos, op) | ||
| 1027 | end | ||
| 1028 | |||
| 1029 | function op_template(params, template, nparams) | ||
| 1030 | if not params then return template:gsub("%x%x%x%x%x%x%x%x", "") end | ||
| 1031 | |||
| 1032 | -- Limit number of section buffer positions used by a single dasm_put(). | ||
| 1033 | -- A single opcode needs a maximum of 4 positions. | ||
| 1034 | if secpos+4 > maxsecpos then wflush() end | ||
| 1035 | local pos = wpos() | ||
| 1036 | local lpos, apos, spos = #actlist, #actargs, secpos | ||
| 1037 | |||
| 1038 | local ok, err | ||
| 1039 | for t in gmatch(template, "[^|]+") do | ||
| 1040 | ok, err = pcall(parse_template, params, t, nparams, pos) | ||
| 1041 | if ok then return end | ||
| 1042 | secpos = spos | ||
| 1043 | actlist[lpos+1] = nil | ||
| 1044 | actlist[lpos+2] = nil | ||
| 1045 | actlist[lpos+3] = nil | ||
| 1046 | actlist[lpos+4] = nil | ||
| 1047 | actargs[apos+1] = nil | ||
| 1048 | actargs[apos+2] = nil | ||
| 1049 | actargs[apos+3] = nil | ||
| 1050 | actargs[apos+4] = nil | ||
| 1051 | end | ||
| 1052 | error(err, 0) | ||
| 1053 | end | ||
| 1054 | |||
| 1055 | map_op[".template__"] = op_template | ||
| 1056 | |||
| 1057 | ------------------------------------------------------------------------------ | ||
| 1058 | |||
| 1059 | -- Pseudo-opcode to mark the position where the action list is to be emitted. | ||
| 1060 | map_op[".actionlist_1"] = function(params) | ||
| 1061 | if not params then return "cvar" end | ||
| 1062 | local name = params[1] -- No syntax check. You get to keep the pieces. | ||
| 1063 | wline(function(out) writeactions(out, name) end) | ||
| 1064 | end | ||
| 1065 | |||
| 1066 | -- Pseudo-opcode to mark the position where the global enum is to be emitted. | ||
| 1067 | map_op[".globals_1"] = function(params) | ||
| 1068 | if not params then return "prefix" end | ||
| 1069 | local prefix = params[1] -- No syntax check. You get to keep the pieces. | ||
| 1070 | wline(function(out) writeglobals(out, prefix) end) | ||
| 1071 | end | ||
| 1072 | |||
| 1073 | -- Pseudo-opcode to mark the position where the global names are to be emitted. | ||
| 1074 | map_op[".globalnames_1"] = function(params) | ||
| 1075 | if not params then return "cvar" end | ||
| 1076 | local name = params[1] -- No syntax check. You get to keep the pieces. | ||
| 1077 | wline(function(out) writeglobalnames(out, name) end) | ||
| 1078 | end | ||
| 1079 | |||
| 1080 | -- Pseudo-opcode to mark the position where the extern names are to be emitted. | ||
| 1081 | map_op[".externnames_1"] = function(params) | ||
| 1082 | if not params then return "cvar" end | ||
| 1083 | local name = params[1] -- No syntax check. You get to keep the pieces. | ||
| 1084 | wline(function(out) writeexternnames(out, name) end) | ||
| 1085 | end | ||
| 1086 | |||
| 1087 | ------------------------------------------------------------------------------ | ||
| 1088 | |||
| 1089 | -- Label pseudo-opcode (converted from trailing colon form). | ||
| 1090 | map_op[".label_1"] = function(params) | ||
| 1091 | if not params then return "[1-9] | ->global | =>pcexpr" end | ||
| 1092 | if secpos+1 > maxsecpos then wflush() end | ||
| 1093 | local mode, n, s = parse_label(params[1], true) | ||
| 1094 | if not mode or mode == "EXT" then werror("bad label definition") end | ||
| 1095 | waction("LABEL_"..mode, n, s, 1) | ||
| 1096 | end | ||
| 1097 | |||
| 1098 | ------------------------------------------------------------------------------ | ||
| 1099 | |||
| 1100 | -- Pseudo-opcodes for data storage. | ||
| 1101 | local function op_data(params) | ||
| 1102 | if not params then return "imm..." end | ||
| 1103 | local sz = params.op == ".long" and 4 or 8 | ||
| 1104 | for _,p in ipairs(params) do | ||
| 1105 | local imm = parse_number(p) | ||
| 1106 | if imm then | ||
| 1107 | local n = tobit(imm) | ||
| 1108 | if n == imm or (n < 0 and n + 2^32 == imm) then | ||
| 1109 | wputw(n < 0 and n + 2^32 or n) | ||
| 1110 | if sz == 8 then | ||
| 1111 | wputw(imm < 0 and 0xffffffff or 0) | ||
| 1112 | end | ||
| 1113 | elseif sz == 4 then | ||
| 1114 | werror("bad immediate `"..p.."'") | ||
| 1115 | else | ||
| 1116 | imm = nil | ||
| 1117 | end | ||
| 1118 | end | ||
| 1119 | if not imm then | ||
| 1120 | local mode, v, s = parse_label(p, false) | ||
| 1121 | if sz == 4 then | ||
| 1122 | if mode then werror("label does not fit into .long") end | ||
| 1123 | waction("IMMV", 0, p) | ||
| 1124 | elseif mode and mode ~= "A" then | ||
| 1125 | waction("REL_"..mode, v+0x8000, s, 1) | ||
| 1126 | else | ||
| 1127 | if mode == "A" then p = s end | ||
| 1128 | waction("IMMV", 0, format("(unsigned int)(%s)", p)) | ||
| 1129 | waction("IMMV", 0, format("(unsigned int)((unsigned long long)(%s)>>32)", p)) | ||
| 1130 | end | ||
| 1131 | end | ||
| 1132 | if secpos+2 > maxsecpos then wflush() end | ||
| 1133 | end | ||
| 1134 | end | ||
| 1135 | map_op[".long_*"] = op_data | ||
| 1136 | map_op[".quad_*"] = op_data | ||
| 1137 | map_op[".addr_*"] = op_data | ||
| 1138 | |||
| 1139 | -- Alignment pseudo-opcode. | ||
| 1140 | map_op[".align_1"] = function(params) | ||
| 1141 | if not params then return "numpow2" end | ||
| 1142 | if secpos+1 > maxsecpos then wflush() end | ||
| 1143 | local align = tonumber(params[1]) | ||
| 1144 | if align then | ||
| 1145 | local x = align | ||
| 1146 | -- Must be a power of 2 in the range (2 ... 256). | ||
| 1147 | for i=1,8 do | ||
| 1148 | x = x / 2 | ||
| 1149 | if x == 1 then | ||
| 1150 | waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. | ||
| 1151 | return | ||
| 1152 | end | ||
| 1153 | end | ||
| 1154 | end | ||
| 1155 | werror("bad alignment") | ||
| 1156 | end | ||
| 1157 | |||
| 1158 | ------------------------------------------------------------------------------ | ||
| 1159 | |||
| 1160 | -- Pseudo-opcode for (primitive) type definitions (map to C types). | ||
| 1161 | map_op[".type_3"] = function(params, nparams) | ||
| 1162 | if not params then | ||
| 1163 | return nparams == 2 and "name, ctype" or "name, ctype, reg" | ||
| 1164 | end | ||
| 1165 | local name, ctype, reg = params[1], params[2], params[3] | ||
| 1166 | if not match(name, "^[%a_][%w_]*$") then | ||
| 1167 | werror("bad type name `"..name.."'") | ||
| 1168 | end | ||
| 1169 | local tp = map_type[name] | ||
| 1170 | if tp then | ||
| 1171 | werror("duplicate type `"..name.."'") | ||
| 1172 | end | ||
| 1173 | -- Add #type to defines. A bit unclean to put it in map_archdef. | ||
| 1174 | map_archdef["#"..name] = "sizeof("..ctype..")" | ||
| 1175 | -- Add new type and emit shortcut define. | ||
| 1176 | local num = ctypenum + 1 | ||
| 1177 | map_type[name] = { | ||
| 1178 | ctype = ctype, | ||
| 1179 | ctypefmt = format("Dt%X(%%s)", num), | ||
| 1180 | reg = reg, | ||
| 1181 | } | ||
| 1182 | wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) | ||
| 1183 | ctypenum = num | ||
| 1184 | end | ||
| 1185 | map_op[".type_2"] = map_op[".type_3"] | ||
| 1186 | |||
| 1187 | -- Dump type definitions. | ||
| 1188 | local function dumptypes(out, lvl) | ||
| 1189 | local t = {} | ||
| 1190 | for name in pairs(map_type) do t[#t+1] = name end | ||
| 1191 | sort(t) | ||
| 1192 | out:write("Type definitions:\n") | ||
| 1193 | for _,name in ipairs(t) do | ||
| 1194 | local tp = map_type[name] | ||
| 1195 | local reg = tp.reg or "" | ||
| 1196 | out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) | ||
| 1197 | end | ||
| 1198 | out:write("\n") | ||
| 1199 | end | ||
| 1200 | |||
| 1201 | ------------------------------------------------------------------------------ | ||
| 1202 | |||
| 1203 | -- Set the current section. | ||
| 1204 | function _M.section(num) | ||
| 1205 | waction("SECTION", num) | ||
| 1206 | wflush(true) -- SECTION is a terminal action. | ||
| 1207 | end | ||
| 1208 | |||
| 1209 | ------------------------------------------------------------------------------ | ||
| 1210 | |||
| 1211 | -- Dump architecture description. | ||
| 1212 | function _M.dumparch(out) | ||
| 1213 | out:write(format("DynASM %s version %s, released %s\n\n", | ||
| 1214 | _info.arch, _info.version, _info.release)) | ||
| 1215 | dumpactions(out) | ||
| 1216 | end | ||
| 1217 | |||
| 1218 | -- Dump all user defined elements. | ||
| 1219 | function _M.dumpdef(out, lvl) | ||
| 1220 | dumptypes(out, lvl) | ||
| 1221 | dumpglobals(out, lvl) | ||
| 1222 | dumpexterns(out, lvl) | ||
| 1223 | end | ||
| 1224 | |||
| 1225 | ------------------------------------------------------------------------------ | ||
| 1226 | |||
| 1227 | -- Pass callbacks from/to the DynASM core. | ||
| 1228 | function _M.passcb(wl, we, wf, ww) | ||
| 1229 | wline, werror, wfatal, wwarn = wl, we, wf, ww | ||
| 1230 | return wflush | ||
| 1231 | end | ||
| 1232 | |||
| 1233 | -- Setup the arch-specific module. | ||
| 1234 | function _M.setup(arch, opt) | ||
| 1235 | g_arch, g_opt = arch, opt | ||
| 1236 | end | ||
| 1237 | |||
| 1238 | -- Merge the core maps and the arch-specific maps. | ||
| 1239 | function _M.mergemaps(map_coreop, map_def) | ||
| 1240 | setmetatable(map_op, { __index = map_coreop }) | ||
| 1241 | setmetatable(map_def, { __index = map_archdef }) | ||
| 1242 | return map_op, map_def | ||
| 1243 | end | ||
| 1244 | |||
| 1245 | return _M | ||
| 1246 | |||
| 1247 | ------------------------------------------------------------------------------ | ||
| 1248 | |||
