asm-macros.h 6.7 KB

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