gnu.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package x86asm
  5. import (
  6. "fmt"
  7. "strings"
  8. )
  9. // GNUSyntax returns the GNU assembler syntax for the instruction, as defined by GNU binutils.
  10. // This general form is often called “AT&T syntax” as a reference to AT&T System V Unix.
  11. func GNUSyntax(inst Inst, pc uint64, symname SymLookup) string {
  12. // Rewrite instruction to mimic GNU peculiarities.
  13. // Note that inst has been passed by value and contains
  14. // no pointers, so any changes we make here are local
  15. // and will not propagate back out to the caller.
  16. if symname == nil {
  17. symname = func(uint64) (string, uint64) { return "", 0 }
  18. }
  19. // Adjust opcode [sic].
  20. switch inst.Op {
  21. case FDIV, FDIVR, FSUB, FSUBR, FDIVP, FDIVRP, FSUBP, FSUBRP:
  22. // DC E0, DC F0: libopcodes swaps FSUBR/FSUB and FDIVR/FDIV, at least
  23. // if you believe the Intel manual is correct (the encoding is irregular as given;
  24. // libopcodes uses the more regular expected encoding).
  25. // TODO(rsc): Test to ensure Intel manuals are correct and report to libopcodes maintainers?
  26. // NOTE: iant thinks this is deliberate, but we can't find the history.
  27. _, reg1 := inst.Args[0].(Reg)
  28. _, reg2 := inst.Args[1].(Reg)
  29. if reg1 && reg2 && (inst.Opcode>>24 == 0xDC || inst.Opcode>>24 == 0xDE) {
  30. switch inst.Op {
  31. case FDIV:
  32. inst.Op = FDIVR
  33. case FDIVR:
  34. inst.Op = FDIV
  35. case FSUB:
  36. inst.Op = FSUBR
  37. case FSUBR:
  38. inst.Op = FSUB
  39. case FDIVP:
  40. inst.Op = FDIVRP
  41. case FDIVRP:
  42. inst.Op = FDIVP
  43. case FSUBP:
  44. inst.Op = FSUBRP
  45. case FSUBRP:
  46. inst.Op = FSUBP
  47. }
  48. }
  49. case MOVNTSD:
  50. // MOVNTSD is F2 0F 2B /r.
  51. // MOVNTSS is F3 0F 2B /r (supposedly; not in manuals).
  52. // Usually inner prefixes win for display,
  53. // so that F3 F2 0F 2B 11 is REP MOVNTSD
  54. // and F2 F3 0F 2B 11 is REPN MOVNTSS.
  55. // Libopcodes always prefers MOVNTSS regardless of prefix order.
  56. if countPrefix(&inst, 0xF3) > 0 {
  57. found := false
  58. for i := len(inst.Prefix) - 1; i >= 0; i-- {
  59. switch inst.Prefix[i] & 0xFF {
  60. case 0xF3:
  61. if !found {
  62. found = true
  63. inst.Prefix[i] |= PrefixImplicit
  64. }
  65. case 0xF2:
  66. inst.Prefix[i] &^= PrefixImplicit
  67. }
  68. }
  69. inst.Op = MOVNTSS
  70. }
  71. }
  72. // Add implicit arguments.
  73. switch inst.Op {
  74. case MONITOR:
  75. inst.Args[0] = EDX
  76. inst.Args[1] = ECX
  77. inst.Args[2] = EAX
  78. if inst.AddrSize == 16 {
  79. inst.Args[2] = AX
  80. }
  81. case MWAIT:
  82. if inst.Mode == 64 {
  83. inst.Args[0] = RCX
  84. inst.Args[1] = RAX
  85. } else {
  86. inst.Args[0] = ECX
  87. inst.Args[1] = EAX
  88. }
  89. }
  90. // Adjust which prefixes will be displayed.
  91. // The rule is to display all the prefixes not implied by
  92. // the usual instruction display, that is, all the prefixes
  93. // except the ones with PrefixImplicit set.
  94. // However, of course, there are exceptions to the rule.
  95. switch inst.Op {
  96. case CRC32:
  97. // CRC32 has a mandatory F2 prefix.
  98. // If there are multiple F2s and no F3s, the extra F2s do not print.
  99. // (And Decode has already marked them implicit.)
  100. // However, if there is an F3 anywhere, then the extra F2s do print.
  101. // If there are multiple F2 prefixes *and* an (ignored) F3,
  102. // then libopcodes prints the extra F2s as REPNs.
  103. if countPrefix(&inst, 0xF2) > 1 {
  104. unmarkImplicit(&inst, 0xF2)
  105. markLastImplicit(&inst, 0xF2)
  106. }
  107. // An unused data size override should probably be shown,
  108. // to distinguish DATA16 CRC32B from plain CRC32B,
  109. // but libopcodes always treats the final override as implicit
  110. // and the others as explicit.
  111. unmarkImplicit(&inst, PrefixDataSize)
  112. markLastImplicit(&inst, PrefixDataSize)
  113. case CVTSI2SD, CVTSI2SS:
  114. if !isMem(inst.Args[1]) {
  115. markLastImplicit(&inst, PrefixDataSize)
  116. }
  117. case CVTSD2SI, CVTSS2SI, CVTTSD2SI, CVTTSS2SI,
  118. ENTER, FLDENV, FNSAVE, FNSTENV, FRSTOR, LGDT, LIDT, LRET,
  119. POP, PUSH, RET, SGDT, SIDT, SYSRET, XBEGIN:
  120. markLastImplicit(&inst, PrefixDataSize)
  121. case LOOP, LOOPE, LOOPNE, MONITOR:
  122. markLastImplicit(&inst, PrefixAddrSize)
  123. case MOV:
  124. // The 16-bit and 32-bit forms of MOV Sreg, dst and MOV src, Sreg
  125. // cannot be distinguished when src or dst refers to memory, because
  126. // Sreg is always a 16-bit value, even when we're doing a 32-bit
  127. // instruction. Because the instruction tables distinguished these two,
  128. // any operand size prefix has been marked as used (to decide which
  129. // branch to take). Unmark it, so that it will show up in disassembly,
  130. // so that the reader can tell the size of memory operand.
  131. // up with the same arguments
  132. dst, _ := inst.Args[0].(Reg)
  133. src, _ := inst.Args[1].(Reg)
  134. if ES <= src && src <= GS && isMem(inst.Args[0]) || ES <= dst && dst <= GS && isMem(inst.Args[1]) {
  135. unmarkImplicit(&inst, PrefixDataSize)
  136. }
  137. case MOVDQU:
  138. if countPrefix(&inst, 0xF3) > 1 {
  139. unmarkImplicit(&inst, 0xF3)
  140. markLastImplicit(&inst, 0xF3)
  141. }
  142. case MOVQ2DQ:
  143. markLastImplicit(&inst, PrefixDataSize)
  144. case SLDT, SMSW, STR, FXRSTOR, XRSTOR, XSAVE, XSAVEOPT, CMPXCHG8B:
  145. if isMem(inst.Args[0]) {
  146. unmarkImplicit(&inst, PrefixDataSize)
  147. }
  148. case SYSEXIT:
  149. unmarkImplicit(&inst, PrefixDataSize)
  150. }
  151. if isCondJmp[inst.Op] || isLoop[inst.Op] || inst.Op == JCXZ || inst.Op == JECXZ || inst.Op == JRCXZ {
  152. if countPrefix(&inst, PrefixCS) > 0 && countPrefix(&inst, PrefixDS) > 0 {
  153. for i, p := range inst.Prefix {
  154. switch p & 0xFFF {
  155. case PrefixPN, PrefixPT:
  156. inst.Prefix[i] &= 0xF0FF // cut interpretation bits, producing original segment prefix
  157. }
  158. }
  159. }
  160. }
  161. // XACQUIRE/XRELEASE adjustment.
  162. if inst.Op == MOV {
  163. // MOV into memory is a candidate for turning REP into XRELEASE.
  164. // However, if the REP is followed by a REPN, that REPN blocks the
  165. // conversion.
  166. haveREPN := false
  167. for i := len(inst.Prefix) - 1; i >= 0; i-- {
  168. switch inst.Prefix[i] &^ PrefixIgnored {
  169. case PrefixREPN:
  170. haveREPN = true
  171. case PrefixXRELEASE:
  172. if haveREPN {
  173. inst.Prefix[i] = PrefixREP
  174. }
  175. }
  176. }
  177. }
  178. // We only format the final F2/F3 as XRELEASE/XACQUIRE.
  179. haveXA := false
  180. haveXR := false
  181. for i := len(inst.Prefix) - 1; i >= 0; i-- {
  182. switch inst.Prefix[i] &^ PrefixIgnored {
  183. case PrefixXRELEASE:
  184. if !haveXR {
  185. haveXR = true
  186. } else {
  187. inst.Prefix[i] = PrefixREP
  188. }
  189. case PrefixXACQUIRE:
  190. if !haveXA {
  191. haveXA = true
  192. } else {
  193. inst.Prefix[i] = PrefixREPN
  194. }
  195. }
  196. }
  197. // Determine opcode.
  198. op := strings.ToLower(inst.Op.String())
  199. if alt := gnuOp[inst.Op]; alt != "" {
  200. op = alt
  201. }
  202. // Determine opcode suffix.
  203. // Libopcodes omits the suffix if the width of the operation
  204. // can be inferred from a register arguments. For example,
  205. // add $1, %ebx has no suffix because you can tell from the
  206. // 32-bit register destination that it is a 32-bit add,
  207. // but in addl $1, (%ebx), the destination is memory, so the
  208. // size is not evident without the l suffix.
  209. needSuffix := true
  210. SuffixLoop:
  211. for i, a := range inst.Args {
  212. if a == nil {
  213. break
  214. }
  215. switch a := a.(type) {
  216. case Reg:
  217. switch inst.Op {
  218. case MOVSX, MOVZX:
  219. continue
  220. case SHL, SHR, RCL, RCR, ROL, ROR, SAR:
  221. if i == 1 {
  222. // shift count does not tell us operand size
  223. continue
  224. }
  225. case CRC32:
  226. // The source argument does tell us operand size,
  227. // but libopcodes still always puts a suffix on crc32.
  228. continue
  229. case PUSH, POP:
  230. // Even though segment registers are 16-bit, push and pop
  231. // can save/restore them from 32-bit slots, so they
  232. // do not imply operand size.
  233. if ES <= a && a <= GS {
  234. continue
  235. }
  236. case CVTSI2SD, CVTSI2SS:
  237. // The integer register argument takes priority.
  238. if X0 <= a && a <= X15 {
  239. continue
  240. }
  241. }
  242. if AL <= a && a <= R15 || ES <= a && a <= GS || X0 <= a && a <= X15 || M0 <= a && a <= M7 {
  243. needSuffix = false
  244. break SuffixLoop
  245. }
  246. }
  247. }
  248. if needSuffix {
  249. switch inst.Op {
  250. case CMPXCHG8B, FLDCW, FNSTCW, FNSTSW, LDMXCSR, LLDT, LMSW, LTR, PCLMULQDQ,
  251. SETA, SETAE, SETB, SETBE, SETE, SETG, SETGE, SETL, SETLE, SETNE, SETNO, SETNP, SETNS, SETO, SETP, SETS,
  252. SLDT, SMSW, STMXCSR, STR, VERR, VERW:
  253. // For various reasons, libopcodes emits no suffix for these instructions.
  254. case CRC32:
  255. op += byteSizeSuffix(argBytes(&inst, inst.Args[1]))
  256. case LGDT, LIDT, SGDT, SIDT:
  257. op += byteSizeSuffix(inst.DataSize / 8)
  258. case MOVZX, MOVSX:
  259. // Integer size conversions get two suffixes.
  260. op = op[:4] + byteSizeSuffix(argBytes(&inst, inst.Args[1])) + byteSizeSuffix(argBytes(&inst, inst.Args[0]))
  261. case LOOP, LOOPE, LOOPNE:
  262. // Add w suffix to indicate use of CX register instead of ECX.
  263. if inst.AddrSize == 16 {
  264. op += "w"
  265. }
  266. case CALL, ENTER, JMP, LCALL, LEAVE, LJMP, LRET, RET, SYSRET, XBEGIN:
  267. // Add w suffix to indicate use of 16-bit target.
  268. // Exclude JMP rel8.
  269. if inst.Opcode>>24 == 0xEB {
  270. break
  271. }
  272. if inst.DataSize == 16 && inst.Mode != 16 {
  273. markLastImplicit(&inst, PrefixDataSize)
  274. op += "w"
  275. } else if inst.Mode == 64 {
  276. op += "q"
  277. }
  278. case FRSTOR, FNSAVE, FNSTENV, FLDENV:
  279. // Add s suffix to indicate shortened FPU state (I guess).
  280. if inst.DataSize == 16 {
  281. op += "s"
  282. }
  283. case PUSH, POP:
  284. if markLastImplicit(&inst, PrefixDataSize) {
  285. op += byteSizeSuffix(inst.DataSize / 8)
  286. } else if inst.Mode == 64 {
  287. op += "q"
  288. } else {
  289. op += byteSizeSuffix(inst.MemBytes)
  290. }
  291. default:
  292. if isFloat(inst.Op) {
  293. // I can't explain any of this, but it's what libopcodes does.
  294. switch inst.MemBytes {
  295. default:
  296. if (inst.Op == FLD || inst.Op == FSTP) && isMem(inst.Args[0]) {
  297. op += "t"
  298. }
  299. case 4:
  300. if isFloatInt(inst.Op) {
  301. op += "l"
  302. } else {
  303. op += "s"
  304. }
  305. case 8:
  306. if isFloatInt(inst.Op) {
  307. op += "ll"
  308. } else {
  309. op += "l"
  310. }
  311. }
  312. break
  313. }
  314. op += byteSizeSuffix(inst.MemBytes)
  315. }
  316. }
  317. // Adjust special case opcodes.
  318. switch inst.Op {
  319. case 0:
  320. if inst.Prefix[0] != 0 {
  321. return strings.ToLower(inst.Prefix[0].String())
  322. }
  323. case INT:
  324. if inst.Opcode>>24 == 0xCC {
  325. inst.Args[0] = nil
  326. op = "int3"
  327. }
  328. case CMPPS, CMPPD, CMPSD_XMM, CMPSS:
  329. imm, ok := inst.Args[2].(Imm)
  330. if ok && 0 <= imm && imm < 8 {
  331. inst.Args[2] = nil
  332. op = cmppsOps[imm] + op[3:]
  333. }
  334. case PCLMULQDQ:
  335. imm, ok := inst.Args[2].(Imm)
  336. if ok && imm&^0x11 == 0 {
  337. inst.Args[2] = nil
  338. op = pclmulqOps[(imm&0x10)>>3|(imm&1)]
  339. }
  340. case XLATB:
  341. if markLastImplicit(&inst, PrefixAddrSize) {
  342. op = "xlat" // not xlatb
  343. }
  344. }
  345. // Build list of argument strings.
  346. var (
  347. usedPrefixes bool // segment prefixes consumed by Mem formatting
  348. args []string // formatted arguments
  349. )
  350. for i, a := range inst.Args {
  351. if a == nil {
  352. break
  353. }
  354. switch inst.Op {
  355. case MOVSB, MOVSW, MOVSD, MOVSQ, OUTSB, OUTSW, OUTSD:
  356. if i == 0 {
  357. usedPrefixes = true // disable use of prefixes for first argument
  358. } else {
  359. usedPrefixes = false
  360. }
  361. }
  362. if a == Imm(1) && (inst.Opcode>>24)&^1 == 0xD0 {
  363. continue
  364. }
  365. args = append(args, gnuArg(&inst, pc, symname, a, &usedPrefixes))
  366. }
  367. // The default is to print the arguments in reverse Intel order.
  368. // A few instructions inhibit this behavior.
  369. switch inst.Op {
  370. case BOUND, LCALL, ENTER, LJMP:
  371. // no reverse
  372. default:
  373. // reverse args
  374. for i, j := 0, len(args)-1; i < j; i, j = i+1, j-1 {
  375. args[i], args[j] = args[j], args[i]
  376. }
  377. }
  378. // Build prefix string.
  379. // Must be after argument formatting, which can turn off segment prefixes.
  380. var (
  381. prefix = "" // output string
  382. numAddr = 0
  383. numData = 0
  384. implicitData = false
  385. )
  386. for _, p := range inst.Prefix {
  387. if p&0xFF == PrefixDataSize && p&PrefixImplicit != 0 {
  388. implicitData = true
  389. }
  390. }
  391. for _, p := range inst.Prefix {
  392. if p == 0 || p.IsVEX() {
  393. break
  394. }
  395. if p&PrefixImplicit != 0 {
  396. continue
  397. }
  398. switch p &^ (PrefixIgnored | PrefixInvalid) {
  399. default:
  400. if p.IsREX() {
  401. if p&0xFF == PrefixREX {
  402. prefix += "rex "
  403. } else {
  404. prefix += "rex." + p.String()[4:] + " "
  405. }
  406. break
  407. }
  408. prefix += strings.ToLower(p.String()) + " "
  409. case PrefixPN:
  410. op += ",pn"
  411. continue
  412. case PrefixPT:
  413. op += ",pt"
  414. continue
  415. case PrefixAddrSize, PrefixAddr16, PrefixAddr32:
  416. // For unknown reasons, if the addr16 prefix is repeated,
  417. // libopcodes displays all but the last as addr32, even though
  418. // the addressing form used in a memory reference is clearly
  419. // still 16-bit.
  420. n := 32
  421. if inst.Mode == 32 {
  422. n = 16
  423. }
  424. numAddr++
  425. if countPrefix(&inst, PrefixAddrSize) > numAddr {
  426. n = inst.Mode
  427. }
  428. prefix += fmt.Sprintf("addr%d ", n)
  429. continue
  430. case PrefixData16, PrefixData32:
  431. if implicitData && countPrefix(&inst, PrefixDataSize) > 1 {
  432. // Similar to the addr32 logic above, but it only kicks in
  433. // when something used the data size prefix (one is implicit).
  434. n := 16
  435. if inst.Mode == 16 {
  436. n = 32
  437. }
  438. numData++
  439. if countPrefix(&inst, PrefixDataSize) > numData {
  440. if inst.Mode == 16 {
  441. n = 16
  442. } else {
  443. n = 32
  444. }
  445. }
  446. prefix += fmt.Sprintf("data%d ", n)
  447. continue
  448. }
  449. prefix += strings.ToLower(p.String()) + " "
  450. }
  451. }
  452. // Finally! Put it all together.
  453. text := prefix + op
  454. if args != nil {
  455. text += " "
  456. // Indirect call/jmp gets a star to distinguish from direct jump address.
  457. if (inst.Op == CALL || inst.Op == JMP || inst.Op == LJMP || inst.Op == LCALL) && (isMem(inst.Args[0]) || isReg(inst.Args[0])) {
  458. text += "*"
  459. }
  460. text += strings.Join(args, ",")
  461. }
  462. return text
  463. }
  464. // gnuArg returns the GNU syntax for the argument x from the instruction inst.
  465. // If *usedPrefixes is false and x is a Mem, then the formatting
  466. // includes any segment prefixes and sets *usedPrefixes to true.
  467. func gnuArg(inst *Inst, pc uint64, symname SymLookup, x Arg, usedPrefixes *bool) string {
  468. if x == nil {
  469. return "<nil>"
  470. }
  471. switch x := x.(type) {
  472. case Reg:
  473. switch inst.Op {
  474. case CVTSI2SS, CVTSI2SD, CVTSS2SI, CVTSD2SI, CVTTSD2SI, CVTTSS2SI:
  475. if inst.DataSize == 16 && EAX <= x && x <= R15L {
  476. x -= EAX - AX
  477. }
  478. case IN, INSB, INSW, INSD, OUT, OUTSB, OUTSW, OUTSD:
  479. // DX is the port, but libopcodes prints it as if it were a memory reference.
  480. if x == DX {
  481. return "(%dx)"
  482. }
  483. case VMOVDQA, VMOVDQU, VMOVNTDQA, VMOVNTDQ:
  484. return strings.Replace(gccRegName[x], "xmm", "ymm", -1)
  485. }
  486. return gccRegName[x]
  487. case Mem:
  488. if s, disp := memArgToSymbol(x, pc, inst.Len, symname); s != "" {
  489. suffix := ""
  490. if disp != 0 {
  491. suffix = fmt.Sprintf("%+d", disp)
  492. }
  493. return fmt.Sprintf("%s%s", s, suffix)
  494. }
  495. seg := ""
  496. var haveCS, haveDS, haveES, haveFS, haveGS, haveSS bool
  497. switch x.Segment {
  498. case CS:
  499. haveCS = true
  500. case DS:
  501. haveDS = true
  502. case ES:
  503. haveES = true
  504. case FS:
  505. haveFS = true
  506. case GS:
  507. haveGS = true
  508. case SS:
  509. haveSS = true
  510. }
  511. switch inst.Op {
  512. case INSB, INSW, INSD, STOSB, STOSW, STOSD, STOSQ, SCASB, SCASW, SCASD, SCASQ:
  513. // These do not accept segment prefixes, at least in the GNU rendering.
  514. default:
  515. if *usedPrefixes {
  516. break
  517. }
  518. for i := len(inst.Prefix) - 1; i >= 0; i-- {
  519. p := inst.Prefix[i] &^ PrefixIgnored
  520. if p == 0 {
  521. continue
  522. }
  523. switch p {
  524. case PrefixCS:
  525. if !haveCS {
  526. haveCS = true
  527. inst.Prefix[i] |= PrefixImplicit
  528. }
  529. case PrefixDS:
  530. if !haveDS {
  531. haveDS = true
  532. inst.Prefix[i] |= PrefixImplicit
  533. }
  534. case PrefixES:
  535. if !haveES {
  536. haveES = true
  537. inst.Prefix[i] |= PrefixImplicit
  538. }
  539. case PrefixFS:
  540. if !haveFS {
  541. haveFS = true
  542. inst.Prefix[i] |= PrefixImplicit
  543. }
  544. case PrefixGS:
  545. if !haveGS {
  546. haveGS = true
  547. inst.Prefix[i] |= PrefixImplicit
  548. }
  549. case PrefixSS:
  550. if !haveSS {
  551. haveSS = true
  552. inst.Prefix[i] |= PrefixImplicit
  553. }
  554. }
  555. }
  556. *usedPrefixes = true
  557. }
  558. if haveCS {
  559. seg += "%cs:"
  560. }
  561. if haveDS {
  562. seg += "%ds:"
  563. }
  564. if haveSS {
  565. seg += "%ss:"
  566. }
  567. if haveES {
  568. seg += "%es:"
  569. }
  570. if haveFS {
  571. seg += "%fs:"
  572. }
  573. if haveGS {
  574. seg += "%gs:"
  575. }
  576. disp := ""
  577. if x.Disp != 0 {
  578. disp = fmt.Sprintf("%#x", x.Disp)
  579. }
  580. if x.Scale == 0 || x.Index == 0 && x.Scale == 1 && (x.Base == ESP || x.Base == RSP || x.Base == 0 && inst.Mode == 64) {
  581. if x.Base == 0 {
  582. return seg + disp
  583. }
  584. return fmt.Sprintf("%s%s(%s)", seg, disp, gccRegName[x.Base])
  585. }
  586. base := gccRegName[x.Base]
  587. if x.Base == 0 {
  588. base = ""
  589. }
  590. index := gccRegName[x.Index]
  591. if x.Index == 0 {
  592. if inst.AddrSize == 64 {
  593. index = "%riz"
  594. } else {
  595. index = "%eiz"
  596. }
  597. }
  598. if AX <= x.Base && x.Base <= DI {
  599. // 16-bit addressing - no scale
  600. return fmt.Sprintf("%s%s(%s,%s)", seg, disp, base, index)
  601. }
  602. return fmt.Sprintf("%s%s(%s,%s,%d)", seg, disp, base, index, x.Scale)
  603. case Rel:
  604. if pc == 0 {
  605. return fmt.Sprintf(".%+#x", int64(x))
  606. } else {
  607. addr := pc + uint64(inst.Len) + uint64(x)
  608. if s, base := symname(addr); s != "" && addr == base {
  609. return fmt.Sprintf("%s", s)
  610. } else {
  611. addr := pc + uint64(inst.Len) + uint64(x)
  612. return fmt.Sprintf("%#x", addr)
  613. }
  614. }
  615. case Imm:
  616. if (inst.Op == MOV || inst.Op == PUSH) && inst.DataSize == 32 { // See comment in plan9x.go.
  617. if s, base := symname(uint64(x)); s != "" {
  618. suffix := ""
  619. if uint64(x) != base {
  620. suffix = fmt.Sprintf("%+d", uint64(x)-base)
  621. }
  622. return fmt.Sprintf("$%s%s", s, suffix)
  623. }
  624. }
  625. if inst.Mode == 32 {
  626. return fmt.Sprintf("$%#x", uint32(x))
  627. }
  628. return fmt.Sprintf("$%#x", int64(x))
  629. }
  630. return x.String()
  631. }
  632. var gccRegName = [...]string{
  633. 0: "REG0",
  634. AL: "%al",
  635. CL: "%cl",
  636. BL: "%bl",
  637. DL: "%dl",
  638. AH: "%ah",
  639. CH: "%ch",
  640. BH: "%bh",
  641. DH: "%dh",
  642. SPB: "%spl",
  643. BPB: "%bpl",
  644. SIB: "%sil",
  645. DIB: "%dil",
  646. R8B: "%r8b",
  647. R9B: "%r9b",
  648. R10B: "%r10b",
  649. R11B: "%r11b",
  650. R12B: "%r12b",
  651. R13B: "%r13b",
  652. R14B: "%r14b",
  653. R15B: "%r15b",
  654. AX: "%ax",
  655. CX: "%cx",
  656. BX: "%bx",
  657. DX: "%dx",
  658. SP: "%sp",
  659. BP: "%bp",
  660. SI: "%si",
  661. DI: "%di",
  662. R8W: "%r8w",
  663. R9W: "%r9w",
  664. R10W: "%r10w",
  665. R11W: "%r11w",
  666. R12W: "%r12w",
  667. R13W: "%r13w",
  668. R14W: "%r14w",
  669. R15W: "%r15w",
  670. EAX: "%eax",
  671. ECX: "%ecx",
  672. EDX: "%edx",
  673. EBX: "%ebx",
  674. ESP: "%esp",
  675. EBP: "%ebp",
  676. ESI: "%esi",
  677. EDI: "%edi",
  678. R8L: "%r8d",
  679. R9L: "%r9d",
  680. R10L: "%r10d",
  681. R11L: "%r11d",
  682. R12L: "%r12d",
  683. R13L: "%r13d",
  684. R14L: "%r14d",
  685. R15L: "%r15d",
  686. RAX: "%rax",
  687. RCX: "%rcx",
  688. RDX: "%rdx",
  689. RBX: "%rbx",
  690. RSP: "%rsp",
  691. RBP: "%rbp",
  692. RSI: "%rsi",
  693. RDI: "%rdi",
  694. R8: "%r8",
  695. R9: "%r9",
  696. R10: "%r10",
  697. R11: "%r11",
  698. R12: "%r12",
  699. R13: "%r13",
  700. R14: "%r14",
  701. R15: "%r15",
  702. IP: "%ip",
  703. EIP: "%eip",
  704. RIP: "%rip",
  705. F0: "%st",
  706. F1: "%st(1)",
  707. F2: "%st(2)",
  708. F3: "%st(3)",
  709. F4: "%st(4)",
  710. F5: "%st(5)",
  711. F6: "%st(6)",
  712. F7: "%st(7)",
  713. M0: "%mm0",
  714. M1: "%mm1",
  715. M2: "%mm2",
  716. M3: "%mm3",
  717. M4: "%mm4",
  718. M5: "%mm5",
  719. M6: "%mm6",
  720. M7: "%mm7",
  721. X0: "%xmm0",
  722. X1: "%xmm1",
  723. X2: "%xmm2",
  724. X3: "%xmm3",
  725. X4: "%xmm4",
  726. X5: "%xmm5",
  727. X6: "%xmm6",
  728. X7: "%xmm7",
  729. X8: "%xmm8",
  730. X9: "%xmm9",
  731. X10: "%xmm10",
  732. X11: "%xmm11",
  733. X12: "%xmm12",
  734. X13: "%xmm13",
  735. X14: "%xmm14",
  736. X15: "%xmm15",
  737. CS: "%cs",
  738. SS: "%ss",
  739. DS: "%ds",
  740. ES: "%es",
  741. FS: "%fs",
  742. GS: "%gs",
  743. GDTR: "%gdtr",
  744. IDTR: "%idtr",
  745. LDTR: "%ldtr",
  746. MSW: "%msw",
  747. TASK: "%task",
  748. CR0: "%cr0",
  749. CR1: "%cr1",
  750. CR2: "%cr2",
  751. CR3: "%cr3",
  752. CR4: "%cr4",
  753. CR5: "%cr5",
  754. CR6: "%cr6",
  755. CR7: "%cr7",
  756. CR8: "%cr8",
  757. CR9: "%cr9",
  758. CR10: "%cr10",
  759. CR11: "%cr11",
  760. CR12: "%cr12",
  761. CR13: "%cr13",
  762. CR14: "%cr14",
  763. CR15: "%cr15",
  764. DR0: "%db0",
  765. DR1: "%db1",
  766. DR2: "%db2",
  767. DR3: "%db3",
  768. DR4: "%db4",
  769. DR5: "%db5",
  770. DR6: "%db6",
  771. DR7: "%db7",
  772. TR0: "%tr0",
  773. TR1: "%tr1",
  774. TR2: "%tr2",
  775. TR3: "%tr3",
  776. TR4: "%tr4",
  777. TR5: "%tr5",
  778. TR6: "%tr6",
  779. TR7: "%tr7",
  780. }
  781. var gnuOp = map[Op]string{
  782. CBW: "cbtw",
  783. CDQ: "cltd",
  784. CMPSD: "cmpsl",
  785. CMPSD_XMM: "cmpsd",
  786. CWD: "cwtd",
  787. CWDE: "cwtl",
  788. CQO: "cqto",
  789. INSD: "insl",
  790. IRET: "iretw",
  791. IRETD: "iret",
  792. IRETQ: "iretq",
  793. LODSB: "lods",
  794. LODSD: "lods",
  795. LODSQ: "lods",
  796. LODSW: "lods",
  797. MOVSD: "movsl",
  798. MOVSD_XMM: "movsd",
  799. OUTSD: "outsl",
  800. POPA: "popaw",
  801. POPAD: "popa",
  802. POPF: "popfw",
  803. POPFD: "popf",
  804. PUSHA: "pushaw",
  805. PUSHAD: "pusha",
  806. PUSHF: "pushfw",
  807. PUSHFD: "pushf",
  808. SCASB: "scas",
  809. SCASD: "scas",
  810. SCASQ: "scas",
  811. SCASW: "scas",
  812. STOSB: "stos",
  813. STOSD: "stos",
  814. STOSQ: "stos",
  815. STOSW: "stos",
  816. XLATB: "xlat",
  817. }
  818. var cmppsOps = []string{
  819. "cmpeq",
  820. "cmplt",
  821. "cmple",
  822. "cmpunord",
  823. "cmpneq",
  824. "cmpnlt",
  825. "cmpnle",
  826. "cmpord",
  827. }
  828. var pclmulqOps = []string{
  829. "pclmullqlqdq",
  830. "pclmulhqlqdq",
  831. "pclmullqhqdq",
  832. "pclmulhqhqdq",
  833. }
  834. func countPrefix(inst *Inst, target Prefix) int {
  835. n := 0
  836. for _, p := range inst.Prefix {
  837. if p&0xFF == target&0xFF {
  838. n++
  839. }
  840. }
  841. return n
  842. }
  843. func markLastImplicit(inst *Inst, prefix Prefix) bool {
  844. for i := len(inst.Prefix) - 1; i >= 0; i-- {
  845. p := inst.Prefix[i]
  846. if p&0xFF == prefix {
  847. inst.Prefix[i] |= PrefixImplicit
  848. return true
  849. }
  850. }
  851. return false
  852. }
  853. func unmarkImplicit(inst *Inst, prefix Prefix) {
  854. for i := len(inst.Prefix) - 1; i >= 0; i-- {
  855. p := inst.Prefix[i]
  856. if p&0xFF == prefix {
  857. inst.Prefix[i] &^= PrefixImplicit
  858. }
  859. }
  860. }
  861. func byteSizeSuffix(b int) string {
  862. switch b {
  863. case 1:
  864. return "b"
  865. case 2:
  866. return "w"
  867. case 4:
  868. return "l"
  869. case 8:
  870. return "q"
  871. }
  872. return ""
  873. }
  874. func argBytes(inst *Inst, arg Arg) int {
  875. if isMem(arg) {
  876. return inst.MemBytes
  877. }
  878. return regBytes(arg)
  879. }
  880. func isFloat(op Op) bool {
  881. switch op {
  882. case FADD, FCOM, FCOMP, FDIV, FDIVR, FIADD, FICOM, FICOMP, FIDIV, FIDIVR, FILD, FIMUL, FIST, FISTP, FISTTP, FISUB, FISUBR, FLD, FMUL, FST, FSTP, FSUB, FSUBR:
  883. return true
  884. }
  885. return false
  886. }
  887. func isFloatInt(op Op) bool {
  888. switch op {
  889. case FIADD, FICOM, FICOMP, FIDIV, FIDIVR, FILD, FIMUL, FIST, FISTP, FISTTP, FISUB, FISUBR:
  890. return true
  891. }
  892. return false
  893. }