cpu.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
  2. // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
  3. // Portions Copyright © 1997-1999 Vita Nuova Limited
  4. // Portions Copyright © 2000-2008 Vita Nuova Holdings Limited (www.vitanuova.com)
  5. // Portions Copyright © 2004,2006 Bruce Ellis
  6. // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
  7. // Revisions Copyright © 2000-2008 Lucent Technologies Inc. and others
  8. // Portions Copyright © 2009 The Go Authors. All rights reserved.
  9. // Portions Copyright © 2019 The Go Authors. All rights reserved.
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. package riscv
  29. import "github.com/twitchyliquid64/golang-asm/obj"
  30. //go:generate go run ../stringer.go -i $GOFILE -o anames.go -p riscv
  31. const (
  32. // Base register numberings.
  33. REG_X0 = obj.RBaseRISCV + iota
  34. REG_X1
  35. REG_X2
  36. REG_X3
  37. REG_X4
  38. REG_X5
  39. REG_X6
  40. REG_X7
  41. REG_X8
  42. REG_X9
  43. REG_X10
  44. REG_X11
  45. REG_X12
  46. REG_X13
  47. REG_X14
  48. REG_X15
  49. REG_X16
  50. REG_X17
  51. REG_X18
  52. REG_X19
  53. REG_X20
  54. REG_X21
  55. REG_X22
  56. REG_X23
  57. REG_X24
  58. REG_X25
  59. REG_X26
  60. REG_X27
  61. REG_X28
  62. REG_X29
  63. REG_X30
  64. REG_X31
  65. // FP register numberings.
  66. REG_F0
  67. REG_F1
  68. REG_F2
  69. REG_F3
  70. REG_F4
  71. REG_F5
  72. REG_F6
  73. REG_F7
  74. REG_F8
  75. REG_F9
  76. REG_F10
  77. REG_F11
  78. REG_F12
  79. REG_F13
  80. REG_F14
  81. REG_F15
  82. REG_F16
  83. REG_F17
  84. REG_F18
  85. REG_F19
  86. REG_F20
  87. REG_F21
  88. REG_F22
  89. REG_F23
  90. REG_F24
  91. REG_F25
  92. REG_F26
  93. REG_F27
  94. REG_F28
  95. REG_F29
  96. REG_F30
  97. REG_F31
  98. // This marks the end of the register numbering.
  99. REG_END
  100. // General registers reassigned to ABI names.
  101. REG_ZERO = REG_X0
  102. REG_RA = REG_X1 // aka REG_LR
  103. REG_SP = REG_X2
  104. REG_GP = REG_X3 // aka REG_SB
  105. REG_TP = REG_X4 // aka REG_G
  106. REG_T0 = REG_X5
  107. REG_T1 = REG_X6
  108. REG_T2 = REG_X7
  109. REG_S0 = REG_X8
  110. REG_S1 = REG_X9
  111. REG_A0 = REG_X10
  112. REG_A1 = REG_X11
  113. REG_A2 = REG_X12
  114. REG_A3 = REG_X13
  115. REG_A4 = REG_X14
  116. REG_A5 = REG_X15
  117. REG_A6 = REG_X16
  118. REG_A7 = REG_X17
  119. REG_S2 = REG_X18
  120. REG_S3 = REG_X19
  121. REG_S4 = REG_X20 // aka REG_CTXT
  122. REG_S5 = REG_X21
  123. REG_S6 = REG_X22
  124. REG_S7 = REG_X23
  125. REG_S8 = REG_X24
  126. REG_S9 = REG_X25
  127. REG_S10 = REG_X26
  128. REG_S11 = REG_X27
  129. REG_T3 = REG_X28
  130. REG_T4 = REG_X29
  131. REG_T5 = REG_X30
  132. REG_T6 = REG_X31 // aka REG_TMP
  133. // Go runtime register names.
  134. REG_G = REG_TP // G pointer.
  135. REG_CTXT = REG_S4 // Context for closures.
  136. REG_LR = REG_RA // Link register.
  137. REG_TMP = REG_T6 // Reserved for assembler use.
  138. // ABI names for floating point registers.
  139. REG_FT0 = REG_F0
  140. REG_FT1 = REG_F1
  141. REG_FT2 = REG_F2
  142. REG_FT3 = REG_F3
  143. REG_FT4 = REG_F4
  144. REG_FT5 = REG_F5
  145. REG_FT6 = REG_F6
  146. REG_FT7 = REG_F7
  147. REG_FS0 = REG_F8
  148. REG_FS1 = REG_F9
  149. REG_FA0 = REG_F10
  150. REG_FA1 = REG_F11
  151. REG_FA2 = REG_F12
  152. REG_FA3 = REG_F13
  153. REG_FA4 = REG_F14
  154. REG_FA5 = REG_F15
  155. REG_FA6 = REG_F16
  156. REG_FA7 = REG_F17
  157. REG_FS2 = REG_F18
  158. REG_FS3 = REG_F19
  159. REG_FS4 = REG_F20
  160. REG_FS5 = REG_F21
  161. REG_FS6 = REG_F22
  162. REG_FS7 = REG_F23
  163. REG_FS8 = REG_F24
  164. REG_FS9 = REG_F25
  165. REG_FS10 = REG_F26
  166. REG_FS11 = REG_F27
  167. REG_FT8 = REG_F28
  168. REG_FT9 = REG_F29
  169. REG_FT10 = REG_F30
  170. REG_FT11 = REG_F31
  171. // Names generated by the SSA compiler.
  172. REGSP = REG_SP
  173. REGG = REG_G
  174. )
  175. // https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#dwarf-register-numbers
  176. var RISCV64DWARFRegisters = map[int16]int16{
  177. // Integer Registers.
  178. REG_X0: 0,
  179. REG_X1: 1,
  180. REG_X2: 2,
  181. REG_X3: 3,
  182. REG_X4: 4,
  183. REG_X5: 5,
  184. REG_X6: 6,
  185. REG_X7: 7,
  186. REG_X8: 8,
  187. REG_X9: 9,
  188. REG_X10: 10,
  189. REG_X11: 11,
  190. REG_X12: 12,
  191. REG_X13: 13,
  192. REG_X14: 14,
  193. REG_X15: 15,
  194. REG_X16: 16,
  195. REG_X17: 17,
  196. REG_X18: 18,
  197. REG_X19: 19,
  198. REG_X20: 20,
  199. REG_X21: 21,
  200. REG_X22: 22,
  201. REG_X23: 23,
  202. REG_X24: 24,
  203. REG_X25: 25,
  204. REG_X26: 26,
  205. REG_X27: 27,
  206. REG_X28: 28,
  207. REG_X29: 29,
  208. REG_X30: 30,
  209. REG_X31: 31,
  210. // Floating-Point Registers.
  211. REG_F0: 32,
  212. REG_F1: 33,
  213. REG_F2: 34,
  214. REG_F3: 35,
  215. REG_F4: 36,
  216. REG_F5: 37,
  217. REG_F6: 38,
  218. REG_F7: 39,
  219. REG_F8: 40,
  220. REG_F9: 41,
  221. REG_F10: 42,
  222. REG_F11: 43,
  223. REG_F12: 44,
  224. REG_F13: 45,
  225. REG_F14: 46,
  226. REG_F15: 47,
  227. REG_F16: 48,
  228. REG_F17: 49,
  229. REG_F18: 50,
  230. REG_F19: 51,
  231. REG_F20: 52,
  232. REG_F21: 53,
  233. REG_F22: 54,
  234. REG_F23: 55,
  235. REG_F24: 56,
  236. REG_F25: 57,
  237. REG_F26: 58,
  238. REG_F27: 59,
  239. REG_F28: 60,
  240. REG_F29: 61,
  241. REG_F30: 62,
  242. REG_F31: 63,
  243. }
  244. // Prog.Mark flags.
  245. const (
  246. // NEED_PCREL_ITYPE_RELOC is set on AUIPC instructions to indicate that
  247. // it is the first instruction in an AUIPC + I-type pair that needs a
  248. // R_RISCV_PCREL_ITYPE relocation.
  249. NEED_PCREL_ITYPE_RELOC = 1 << 0
  250. // NEED_PCREL_STYPE_RELOC is set on AUIPC instructions to indicate that
  251. // it is the first instruction in an AUIPC + S-type pair that needs a
  252. // R_RISCV_PCREL_STYPE relocation.
  253. NEED_PCREL_STYPE_RELOC = 1 << 1
  254. )
  255. // RISC-V mnemonics, as defined in the "opcodes" and "opcodes-pseudo" files
  256. // from:
  257. //
  258. // https://github.com/riscv/riscv-opcodes
  259. //
  260. // As well as some pseudo-mnemonics (e.g. MOV) used only in the assembler.
  261. //
  262. // See also "The RISC-V Instruction Set Manual" at:
  263. //
  264. // https://riscv.org/specifications/
  265. //
  266. // If you modify this table, you MUST run 'go generate' to regenerate anames.go!
  267. const (
  268. // Unprivileged ISA (Document Version 20190608-Base-Ratified)
  269. // 2.4: Integer Computational Instructions
  270. AADDI = obj.ABaseRISCV + obj.A_ARCHSPECIFIC + iota
  271. ASLTI
  272. ASLTIU
  273. AANDI
  274. AORI
  275. AXORI
  276. ASLLI
  277. ASRLI
  278. ASRAI
  279. ALUI
  280. AAUIPC
  281. AADD
  282. ASLT
  283. ASLTU
  284. AAND
  285. AOR
  286. AXOR
  287. ASLL
  288. ASRL
  289. ASUB
  290. ASRA
  291. // The SLL/SRL/SRA instructions differ slightly between RV32 and RV64,
  292. // hence there are pseudo-opcodes for the RV32 specific versions.
  293. ASLLIRV32
  294. ASRLIRV32
  295. ASRAIRV32
  296. // 2.5: Control Transfer Instructions
  297. AJAL
  298. AJALR
  299. ABEQ
  300. ABNE
  301. ABLT
  302. ABLTU
  303. ABGE
  304. ABGEU
  305. // 2.6: Load and Store Instructions
  306. ALW
  307. ALWU
  308. ALH
  309. ALHU
  310. ALB
  311. ALBU
  312. ASW
  313. ASH
  314. ASB
  315. // 2.7: Memory Ordering Instructions
  316. AFENCE
  317. AFENCEI
  318. AFENCETSO
  319. // 5.2: Integer Computational Instructions (RV64I)
  320. AADDIW
  321. ASLLIW
  322. ASRLIW
  323. ASRAIW
  324. AADDW
  325. ASLLW
  326. ASRLW
  327. ASUBW
  328. ASRAW
  329. // 5.3: Load and Store Instructions (RV64I)
  330. ALD
  331. ASD
  332. // 7.1: Multiplication Operations
  333. AMUL
  334. AMULH
  335. AMULHU
  336. AMULHSU
  337. AMULW
  338. ADIV
  339. ADIVU
  340. AREM
  341. AREMU
  342. ADIVW
  343. ADIVUW
  344. AREMW
  345. AREMUW
  346. // 8.2: Load-Reserved/Store-Conditional Instructions
  347. ALRD
  348. ASCD
  349. ALRW
  350. ASCW
  351. // 8.3: Atomic Memory Operations
  352. AAMOSWAPD
  353. AAMOADDD
  354. AAMOANDD
  355. AAMOORD
  356. AAMOXORD
  357. AAMOMAXD
  358. AAMOMAXUD
  359. AAMOMIND
  360. AAMOMINUD
  361. AAMOSWAPW
  362. AAMOADDW
  363. AAMOANDW
  364. AAMOORW
  365. AAMOXORW
  366. AAMOMAXW
  367. AAMOMAXUW
  368. AAMOMINW
  369. AAMOMINUW
  370. // 10.1: Base Counters and Timers
  371. ARDCYCLE
  372. ARDCYCLEH
  373. ARDTIME
  374. ARDTIMEH
  375. ARDINSTRET
  376. ARDINSTRETH
  377. // 11.2: Floating-Point Control and Status Register
  378. AFRCSR
  379. AFSCSR
  380. AFRRM
  381. AFSRM
  382. AFRFLAGS
  383. AFSFLAGS
  384. AFSRMI
  385. AFSFLAGSI
  386. // 11.5: Single-Precision Load and Store Instructions
  387. AFLW
  388. AFSW
  389. // 11.6: Single-Precision Floating-Point Computational Instructions
  390. AFADDS
  391. AFSUBS
  392. AFMULS
  393. AFDIVS
  394. AFMINS
  395. AFMAXS
  396. AFSQRTS
  397. AFMADDS
  398. AFMSUBS
  399. AFNMADDS
  400. AFNMSUBS
  401. // 11.7: Single-Precision Floating-Point Conversion and Move Instructions
  402. AFCVTWS
  403. AFCVTLS
  404. AFCVTSW
  405. AFCVTSL
  406. AFCVTWUS
  407. AFCVTLUS
  408. AFCVTSWU
  409. AFCVTSLU
  410. AFSGNJS
  411. AFSGNJNS
  412. AFSGNJXS
  413. AFMVXS
  414. AFMVSX
  415. AFMVXW
  416. AFMVWX
  417. // 11.8: Single-Precision Floating-Point Compare Instructions
  418. AFEQS
  419. AFLTS
  420. AFLES
  421. // 11.9: Single-Precision Floating-Point Classify Instruction
  422. AFCLASSS
  423. // 12.3: Double-Precision Load and Store Instructions
  424. AFLD
  425. AFSD
  426. // 12.4: Double-Precision Floating-Point Computational Instructions
  427. AFADDD
  428. AFSUBD
  429. AFMULD
  430. AFDIVD
  431. AFMIND
  432. AFMAXD
  433. AFSQRTD
  434. AFMADDD
  435. AFMSUBD
  436. AFNMADDD
  437. AFNMSUBD
  438. // 12.5: Double-Precision Floating-Point Conversion and Move Instructions
  439. AFCVTWD
  440. AFCVTLD
  441. AFCVTDW
  442. AFCVTDL
  443. AFCVTWUD
  444. AFCVTLUD
  445. AFCVTDWU
  446. AFCVTDLU
  447. AFCVTSD
  448. AFCVTDS
  449. AFSGNJD
  450. AFSGNJND
  451. AFSGNJXD
  452. AFMVXD
  453. AFMVDX
  454. // 12.6: Double-Precision Floating-Point Compare Instructions
  455. AFEQD
  456. AFLTD
  457. AFLED
  458. // 12.7: Double-Precision Floating-Point Classify Instruction
  459. AFCLASSD
  460. // 13.1 Quad-Precision Load and Store Instructions
  461. AFLQ
  462. AFSQ
  463. // 13.2: Quad-Precision Computational Instructions
  464. AFADDQ
  465. AFSUBQ
  466. AFMULQ
  467. AFDIVQ
  468. AFMINQ
  469. AFMAXQ
  470. AFSQRTQ
  471. AFMADDQ
  472. AFMSUBQ
  473. AFNMADDQ
  474. AFNMSUBQ
  475. // 13.3 Quad-Precision Convert and Move Instructions
  476. AFCVTWQ
  477. AFCVTLQ
  478. AFCVTSQ
  479. AFCVTDQ
  480. AFCVTQW
  481. AFCVTQL
  482. AFCVTQS
  483. AFCVTQD
  484. AFCVTWUQ
  485. AFCVTLUQ
  486. AFCVTQWU
  487. AFCVTQLU
  488. AFSGNJQ
  489. AFSGNJNQ
  490. AFSGNJXQ
  491. AFMVXQ
  492. AFMVQX
  493. // 13.4 Quad-Precision Floating-Point Compare Instructions
  494. AFEQQ
  495. AFLEQ
  496. AFLTQ
  497. // 13.5 Quad-Precision Floating-Point Classify Instruction
  498. AFCLASSQ
  499. // Privileged ISA (Version 20190608-Priv-MSU-Ratified)
  500. // 3.1.9: Instructions to Access CSRs
  501. ACSRRW
  502. ACSRRS
  503. ACSRRC
  504. ACSRRWI
  505. ACSRRSI
  506. ACSRRCI
  507. // 3.2.1: Environment Call and Breakpoint
  508. AECALL
  509. ASCALL
  510. AEBREAK
  511. ASBREAK
  512. // 3.2.2: Trap-Return Instructions
  513. AMRET
  514. ASRET
  515. AURET
  516. ADRET
  517. // 3.2.3: Wait for Interrupt
  518. AWFI
  519. // 4.2.1: Supervisor Memory-Management Fence Instruction
  520. ASFENCEVMA
  521. // Hypervisor Memory-Management Instructions
  522. AHFENCEGVMA
  523. AHFENCEVVMA
  524. // The escape hatch. Inserts a single 32-bit word.
  525. AWORD
  526. // Pseudo-instructions. These get translated by the assembler into other
  527. // instructions, based on their operands.
  528. ABEQZ
  529. ABGEZ
  530. ABGT
  531. ABGTU
  532. ABGTZ
  533. ABLE
  534. ABLEU
  535. ABLEZ
  536. ABLTZ
  537. ABNEZ
  538. AFNEGD
  539. AFNEGS
  540. AFNED
  541. AFNES
  542. AMOV
  543. AMOVB
  544. AMOVBU
  545. AMOVF
  546. AMOVD
  547. AMOVH
  548. AMOVHU
  549. AMOVW
  550. AMOVWU
  551. ANEG
  552. ANEGW
  553. ANOT
  554. ASEQZ
  555. ASNEZ
  556. // End marker
  557. ALAST
  558. )
  559. // All unary instructions which write to their arguments (as opposed to reading
  560. // from them) go here. The assembly parser uses this information to populate
  561. // its AST in a semantically reasonable way.
  562. //
  563. // Any instructions not listed here are assumed to either be non-unary or to read
  564. // from its argument.
  565. var unaryDst = map[obj.As]bool{
  566. ARDCYCLE: true,
  567. ARDCYCLEH: true,
  568. ARDTIME: true,
  569. ARDTIMEH: true,
  570. ARDINSTRET: true,
  571. ARDINSTRETH: true,
  572. }
  573. // Instruction encoding masks.
  574. const (
  575. // ITypeImmMask is a mask including only the immediate portion of
  576. // I-type instructions.
  577. ITypeImmMask = 0xfff00000
  578. // STypeImmMask is a mask including only the immediate portion of
  579. // S-type instructions.
  580. STypeImmMask = 0xfe000f80
  581. // UTypeImmMask is a mask including only the immediate portion of
  582. // U-type instructions.
  583. UTypeImmMask = 0xfffff000
  584. // UJTypeImmMask is a mask including only the immediate portion of
  585. // UJ-type instructions.
  586. UJTypeImmMask = UTypeImmMask
  587. )