asm-macros.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Macro used to simplify coding multi-line assembler.
  4. * Some of the bit test macro can simplify down to one line
  5. * depending on the mask value.
  6. *
  7. * Copyright (C) 2004 Microtronix Datacom Ltd.
  8. *
  9. * All rights reserved.
  10. */
  11. #ifndef _ASM_NIOS2_ASMMACROS_H
  12. #define _ASM_NIOS2_ASMMACROS_H
  13. /*
  14. * ANDs reg2 with mask and places the result in reg1.
  15. *
  16. * You cannnot use the same register for reg1 & reg2.
  17. */
  18. .macro ANDI32 reg1, reg2, mask
  19. .if \mask & 0xffff
  20. .if \mask & 0xffff0000
  21. movhi \reg1, %hi(\mask)
  22. movui \reg1, %lo(\mask)
  23. and \reg1, \reg1, \reg2
  24. .else
  25. andi \reg1, \reg2, %lo(\mask)
  26. .endif
  27. .else
  28. andhi \reg1, \reg2, %hi(\mask)
  29. .endif
  30. .endm
  31. /*
  32. * ORs reg2 with mask and places the result in reg1.
  33. *
  34. * It is safe to use the same register for reg1 & reg2.
  35. */
  36. .macro ORI32 reg1, reg2, mask
  37. .if \mask & 0xffff
  38. .if \mask & 0xffff0000
  39. orhi \reg1, \reg2, %hi(\mask)
  40. ori \reg1, \reg2, %lo(\mask)
  41. .else
  42. ori \reg1, \reg2, %lo(\mask)
  43. .endif
  44. .else
  45. orhi \reg1, \reg2, %hi(\mask)
  46. .endif
  47. .endm
  48. /*
  49. * XORs reg2 with mask and places the result in reg1.
  50. *
  51. * It is safe to use the same register for reg1 & reg2.
  52. */
  53. .macro XORI32 reg1, reg2, mask
  54. .if \mask & 0xffff
  55. .if \mask & 0xffff0000
  56. xorhi \reg1, \reg2, %hi(\mask)
  57. xori \reg1, \reg1, %lo(\mask)
  58. .else
  59. xori \reg1, \reg2, %lo(\mask)
  60. .endif
  61. .else
  62. xorhi \reg1, \reg2, %hi(\mask)
  63. .endif
  64. .endm
  65. /*
  66. * This is a support macro for BTBZ & BTBNZ. It checks
  67. * the bit to make sure it is valid 32 value.
  68. *
  69. * It is safe to use the same register for reg1 & reg2.
  70. */
  71. .macro BT reg1, reg2, bit
  72. .if \bit > 31
  73. .err
  74. .else
  75. .if \bit < 16
  76. andi \reg1, \reg2, (1 << \bit)
  77. .else
  78. andhi \reg1, \reg2, (1 << (\bit - 16))
  79. .endif
  80. .endif
  81. .endm
  82. /*
  83. * Tests the bit in reg2 and branches to label if the
  84. * bit is zero. The result of the bit test is stored in reg1.
  85. *
  86. * It is safe to use the same register for reg1 & reg2.
  87. */
  88. .macro BTBZ reg1, reg2, bit, label
  89. BT \reg1, \reg2, \bit
  90. beq \reg1, r0, \label
  91. .endm
  92. /*
  93. * Tests the bit in reg2 and branches to label if the
  94. * bit is non-zero. The result of the bit test is stored in reg1.
  95. *
  96. * It is safe to use the same register for reg1 & reg2.
  97. */
  98. .macro BTBNZ reg1, reg2, bit, label
  99. BT \reg1, \reg2, \bit
  100. bne \reg1, r0, \label
  101. .endm
  102. /*
  103. * Tests the bit in reg2 and then compliments the bit in reg2.
  104. * The result of the bit test is stored in reg1.
  105. *
  106. * It is NOT safe to use the same register for reg1 & reg2.
  107. */
  108. .macro BTC reg1, reg2, bit
  109. .if \bit > 31
  110. .err
  111. .else
  112. .if \bit < 16
  113. andi \reg1, \reg2, (1 << \bit)
  114. xori \reg2, \reg2, (1 << \bit)
  115. .else
  116. andhi \reg1, \reg2, (1 << (\bit - 16))
  117. xorhi \reg2, \reg2, (1 << (\bit - 16))
  118. .endif
  119. .endif
  120. .endm
  121. /*
  122. * Tests the bit in reg2 and then sets the bit in reg2.
  123. * The result of the bit test is stored in reg1.
  124. *
  125. * It is NOT safe to use the same register for reg1 & reg2.
  126. */
  127. .macro BTS reg1, reg2, bit
  128. .if \bit > 31
  129. .err
  130. .else
  131. .if \bit < 16
  132. andi \reg1, \reg2, (1 << \bit)
  133. ori \reg2, \reg2, (1 << \bit)
  134. .else
  135. andhi \reg1, \reg2, (1 << (\bit - 16))
  136. orhi \reg2, \reg2, (1 << (\bit - 16))
  137. .endif
  138. .endif
  139. .endm
  140. /*
  141. * Tests the bit in reg2 and then resets the bit in reg2.
  142. * The result of the bit test is stored in reg1.
  143. *
  144. * It is NOT safe to use the same register for reg1 & reg2.
  145. */
  146. .macro BTR reg1, reg2, bit
  147. .if \bit > 31
  148. .err
  149. .else
  150. .if \bit < 16
  151. andi \reg1, \reg2, (1 << \bit)
  152. andi \reg2, \reg2, %lo(~(1 << \bit))
  153. .else
  154. andhi \reg1, \reg2, (1 << (\bit - 16))
  155. andhi \reg2, \reg2, %lo(~(1 << (\bit - 16)))
  156. .endif
  157. .endif
  158. .endm
  159. /*
  160. * Tests the bit in reg2 and then compliments the bit in reg2.
  161. * The result of the bit test is stored in reg1. If the
  162. * original bit was zero it branches to label.
  163. *
  164. * It is NOT safe to use the same register for reg1 & reg2.
  165. */
  166. .macro BTCBZ reg1, reg2, bit, label
  167. BTC \reg1, \reg2, \bit
  168. beq \reg1, r0, \label
  169. .endm
  170. /*
  171. * Tests the bit in reg2 and then compliments the bit in reg2.
  172. * The result of the bit test is stored in reg1. If the
  173. * original bit was non-zero it branches to label.
  174. *
  175. * It is NOT safe to use the same register for reg1 & reg2.
  176. */
  177. .macro BTCBNZ reg1, reg2, bit, label
  178. BTC \reg1, \reg2, \bit
  179. bne \reg1, r0, \label
  180. .endm
  181. /*
  182. * Tests the bit in reg2 and then sets the bit in reg2.
  183. * The result of the bit test is stored in reg1. If the
  184. * original bit was zero it branches to label.
  185. *
  186. * It is NOT safe to use the same register for reg1 & reg2.
  187. */
  188. .macro BTSBZ reg1, reg2, bit, label
  189. BTS \reg1, \reg2, \bit
  190. beq \reg1, r0, \label
  191. .endm
  192. /*
  193. * Tests the bit in reg2 and then sets the bit in reg2.
  194. * The result of the bit test is stored in reg1. If the
  195. * original bit was non-zero it branches to label.
  196. *
  197. * It is NOT safe to use the same register for reg1 & reg2.
  198. */
  199. .macro BTSBNZ reg1, reg2, bit, label
  200. BTS \reg1, \reg2, \bit
  201. bne \reg1, r0, \label
  202. .endm
  203. /*
  204. * Tests the bit in reg2 and then resets the bit in reg2.
  205. * The result of the bit test is stored in reg1. If the
  206. * original bit was zero it branches to label.
  207. *
  208. * It is NOT safe to use the same register for reg1 & reg2.
  209. */
  210. .macro BTRBZ reg1, reg2, bit, label
  211. BTR \reg1, \reg2, \bit
  212. bne \reg1, r0, \label
  213. .endm
  214. /*
  215. * Tests the bit in reg2 and then resets the bit in reg2.
  216. * The result of the bit test is stored in reg1. If the
  217. * original bit was non-zero it branches to label.
  218. *
  219. * It is NOT safe to use the same register for reg1 & reg2.
  220. */
  221. .macro BTRBNZ reg1, reg2, bit, label
  222. BTR \reg1, \reg2, \bit
  223. bne \reg1, r0, \label
  224. .endm
  225. /*
  226. * Tests the bits in mask against reg2 stores the result in reg1.
  227. * If the all the bits in the mask are zero it branches to label.
  228. *
  229. * It is safe to use the same register for reg1 & reg2.
  230. */
  231. .macro TSTBZ reg1, reg2, mask, label
  232. ANDI32 \reg1, \reg2, \mask
  233. beq \reg1, r0, \label
  234. .endm
  235. /*
  236. * Tests the bits in mask against reg2 stores the result in reg1.
  237. * If the any of the bits in the mask are 1 it branches to label.
  238. *
  239. * It is safe to use the same register for reg1 & reg2.
  240. */
  241. .macro TSTBNZ reg1, reg2, mask, label
  242. ANDI32 \reg1, \reg2, \mask
  243. bne \reg1, r0, \label
  244. .endm
  245. /*
  246. * Pushes reg onto the stack.
  247. */
  248. .macro PUSH reg
  249. addi sp, sp, -4
  250. stw \reg, 0(sp)
  251. .endm
  252. /*
  253. * Pops the top of the stack into reg.
  254. */
  255. .macro POP reg
  256. ldw \reg, 0(sp)
  257. addi sp, sp, 4
  258. .endm
  259. #endif /* _ASM_NIOS2_ASMMACROS_H */