sysv.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* -----------------------------------------------------------------------
  2. sysv.S
  3. CSKY Foreign Function Interface
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. ``Software''), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. #define LIBFFI_ASM
  23. #include <fficonfig.h>
  24. #include <ffi.h>
  25. .macro CSKY_FUNC_START name
  26. .text
  27. .align 2
  28. .globl \name
  29. .type \name, @function
  30. \name:
  31. .endm
  32. #ifdef __CSKYABIV2__
  33. /*
  34. * a0: fn
  35. * a1: &ecif
  36. * a2: cif->bytes
  37. * a3: fig->flags
  38. * sp+0: ecif.rvalue
  39. */
  40. CSKY_FUNC_START ffi_call_SYSV
  41. /* Save registers */
  42. .cfi_startproc
  43. subi sp, 28
  44. .cfi_def_cfa_offset 28
  45. stw a0, (sp, 0x0)
  46. .cfi_offset 0, -28
  47. stw a1, (sp, 0x4)
  48. .cfi_offset 1, -24
  49. stw a2, (sp, 0x8)
  50. .cfi_offset 2, -20
  51. stw a3, (sp, 0xC)
  52. .cfi_offset 3, -16
  53. stw l0, (sp, 0x10)
  54. .cfi_offset 4, -12
  55. stw l1, (sp, 0x14)
  56. .cfi_offset 5, -8
  57. stw lr, (sp, 0x18)
  58. .cfi_offset 15, -4
  59. mov l0, sp
  60. .cfi_def_cfa_register 4
  61. /* Make room for all of the new args. */
  62. subu sp, sp, a2
  63. /* Place all of the ffi_prep_args in position */
  64. mov a0, sp
  65. /* a1 already set */
  66. /* Call ffi_prep_args(stack, &ecif) */
  67. jsri ffi_prep_args
  68. /* move first 4 parameters in registers */
  69. ldw a0, (sp, 0x0)
  70. ldw a1, (sp, 0x4)
  71. ldw a2, (sp, 0x8)
  72. ldw a3, (sp, 0xC)
  73. /* and adjust stack */
  74. subu lr, l0, sp /* cif->bytes == l0 - sp */
  75. cmphsi lr, 16
  76. movi l1, 16
  77. movt lr, l1
  78. addu sp, sp, lr
  79. ldw l1, (l0, 0) /* load fn() in advance */
  80. /* call (fn) (...) */
  81. jsr l1
  82. /* Remove the space we pushed for the args */
  83. mov sp, l0
  84. /* Load r2 with the pointer to storage for the return value */
  85. ldw a2, (sp, 0x1C)
  86. /* Load r3 with the return type code */
  87. ldw a3, (sp, 0xC)
  88. /* If the return value pointer is NULL, assume no return value. */
  89. cmpnei a2, 0
  90. bf .Lepilogue
  91. cmpnei a3, FFI_TYPE_STRUCT
  92. bf .Lepilogue
  93. /* return INT64 */
  94. cmpnei a3, FFI_TYPE_SINT64
  95. bt .Lretint
  96. /* stw a0, (a2, 0x0) at .Lretint */
  97. stw a1, (a2, 0x4)
  98. .Lretint:
  99. /* return INT */
  100. stw a0, (a2, 0x0)
  101. .Lepilogue:
  102. ldw a0, (sp, 0x0)
  103. ldw a1, (sp, 0x4)
  104. ldw a2, (sp, 0x8)
  105. ldw a3, (sp, 0xC)
  106. ldw l0, (sp, 0x10)
  107. ldw l1, (sp, 0x14)
  108. ldw lr, (sp, 0x18)
  109. addi sp, sp, 28
  110. rts
  111. .cfi_endproc
  112. .size ffi_call_SYSV, .-ffi_call_SYSV
  113. /*
  114. * unsigned int FFI_HIDDEN
  115. * ffi_closure_SYSV_inner (closure, respp, args)
  116. * ffi_closure *closure;
  117. * void **respp;
  118. * void *args;
  119. */
  120. CSKY_FUNC_START ffi_closure_SYSV
  121. .cfi_startproc
  122. mov a2, sp
  123. addi a1, sp, 16
  124. subi sp, sp, 24
  125. .cfi_def_cfa_offset 40
  126. stw a1, (sp, 0x10)
  127. .cfi_offset 1, -24
  128. stw lr, (sp, 0x14)
  129. .cfi_offset 15, -20
  130. stw sp, (sp, 0x8)
  131. addi a1, sp, 8
  132. jsri ffi_closure_SYSV_inner
  133. ldw a0, (sp, 0x0)
  134. /*
  135. * if FFI_TYPE_SINT64, need a1.
  136. * if FFI_TYPE_INT, ignore a1.
  137. */
  138. ldw a1, (sp, 0x4)
  139. ldw lr, (sp, 0x14)
  140. addi sp, sp, 40
  141. rts
  142. .cfi_endproc
  143. .size ffi_closure_SYSV, .-ffi_closure_SYSV
  144. CSKY_FUNC_START ffi_csky_trampoline
  145. subi sp, sp, 16
  146. stw a0, (sp, 0x0)
  147. stw a1, (sp, 0x4)
  148. stw a2, (sp, 0x8)
  149. stw a3, (sp, 0xC)
  150. lrw a0, [.Lctx]
  151. lrw a1, [.Lfun]
  152. jmp a1
  153. .Lctx:
  154. mov a0, a0
  155. mov a0, a0
  156. .Lfun:
  157. .size ffi_csky_trampoline, .-ffi_csky_trampoline
  158. CSKY_FUNC_START ffi_csky_cacheflush
  159. mov t0, r7
  160. movi r7, 123
  161. trap 0
  162. mov r7, t0
  163. rts
  164. .size ffi_csky_cacheflush, .-ffi_csky_cacheflush
  165. #else /* !__CSKYABIV2__ */
  166. /*
  167. * a0: fn
  168. * a1: &ecif
  169. * a2: cif->bytes
  170. * a3: fig->flags
  171. * a4: ecif.rvalue
  172. */
  173. CSKY_FUNC_START ffi_call_SYSV
  174. /* Save registers */
  175. .cfi_startproc
  176. subi sp, 32
  177. subi sp, 8
  178. .cfi_def_cfa_offset 40
  179. stw a0, (sp, 0x0)
  180. .cfi_offset 2, -40
  181. stw a1, (sp, 0x4)
  182. .cfi_offset 3, -36
  183. stw a2, (sp, 0x8)
  184. .cfi_offset 4, -32
  185. stw a3, (sp, 0xC)
  186. .cfi_offset 5, -28
  187. stw a4, (sp, 0x10)
  188. .cfi_offset 6, -24
  189. stw a5, (sp, 0x14)
  190. .cfi_offset 7, -20
  191. stw l0, (sp, 0x18)
  192. .cfi_offset 8, -16
  193. stw l1, (sp, 0x1C)
  194. .cfi_offset 9, -12
  195. stw lr, (sp, 0x20)
  196. .cfi_offset 15, -8
  197. mov l0, sp
  198. .cfi_def_cfa_register 8
  199. /* Make room for all of the new args. */
  200. subu sp, sp, a2
  201. /* Place all of the ffi_prep_args in position */
  202. mov a0, sp
  203. /* a1 already set */
  204. /* Call ffi_prep_args(stack, &ecif) */
  205. jsri ffi_prep_args
  206. /* move first 4 parameters in registers */
  207. ldw a0, (sp, 0x0)
  208. ldw a1, (sp, 0x4)
  209. ldw a2, (sp, 0x8)
  210. ldw a3, (sp, 0xC)
  211. ldw a4, (sp, 0x10)
  212. ldw a5, (sp, 0x14)
  213. /* and adjust stack */
  214. mov lr, l0
  215. subu lr, sp /* cif->bytes == l0 - sp */
  216. movi l1, 24
  217. cmphs lr, l1
  218. movt lr, l1
  219. addu sp, sp, lr
  220. ldw l1, (l0, 0) /* load fn() in advance */
  221. /* call (fn) (...) */
  222. jsr l1
  223. /* Remove the space we pushed for the args */
  224. mov sp, l0
  225. /* Load r2 with the pointer to storage for the return value */
  226. ldw a2, (sp, 0x10)
  227. /* Load r3 with the return type code */
  228. ldw a3, (sp, 0xC)
  229. /* If the return value pointer is NULL, assume no return value. */
  230. cmpnei a2, 0
  231. bf .Lepilogue
  232. cmpnei a3, FFI_TYPE_STRUCT
  233. bf .Lepilogue
  234. /* return INT64 */
  235. cmpnei a3, FFI_TYPE_SINT64
  236. bt .Lretint
  237. /* stw a0, (a2, 0x0) at .Lretint */
  238. stw a1, (a2, 0x4)
  239. .Lretint:
  240. /* return INT */
  241. stw a0, (a2, 0x0)
  242. .Lepilogue:
  243. ldw a0, (sp, 0x0)
  244. ldw a1, (sp, 0x4)
  245. ldw a2, (sp, 0x8)
  246. ldw a3, (sp, 0xC)
  247. ldw a4, (sp, 0x10)
  248. ldw a5, (sp, 0x14)
  249. ldw l0, (sp, 0x18)
  250. ldw l1, (sp, 0x1C)
  251. ldw lr, (sp, 0x20)
  252. addi sp, sp, 32
  253. addi sp, sp, 8
  254. rts
  255. .cfi_endproc
  256. .size ffi_call_SYSV, .-ffi_call_SYSV
  257. /*
  258. * unsigned int FFI_HIDDEN
  259. * ffi_closure_SYSV_inner (closure, respp, args)
  260. * ffi_closure *closure;
  261. * void **respp;
  262. * void *args;
  263. */
  264. CSKY_FUNC_START ffi_closure_SYSV
  265. .cfi_startproc
  266. mov a2, sp
  267. mov a1, sp
  268. addi a1, 24
  269. subi sp, sp, 24
  270. .cfi_def_cfa_offset 48
  271. stw a1, (sp, 0x10)
  272. .cfi_offset 3, -32
  273. stw lr, (sp, 0x14)
  274. .cfi_offset 15, -28
  275. stw sp, (sp, 0x8)
  276. mov a1, sp
  277. addi a1, 8
  278. jsri ffi_closure_SYSV_inner
  279. ldw a0, (sp, 0x0)
  280. /*
  281. * if FFI_TYPE_SINT64, need a1.
  282. * if FFI_TYPE_INT, ignore a1.
  283. */
  284. ldw a1, (sp, 0x4)
  285. ldw lr, (sp, 0x14)
  286. addi sp, sp, 24
  287. addi sp, sp, 24
  288. rts
  289. .cfi_endproc
  290. .size ffi_closure_SYSV, .-ffi_closure_SYSV
  291. CSKY_FUNC_START ffi_csky_trampoline
  292. subi sp, 24
  293. stw a0, (sp, 0x0)
  294. stw a1, (sp, 0x4)
  295. stw a2, (sp, 0x8)
  296. stw a3, (sp, 0xC)
  297. stw a4, (sp, 0x10)
  298. stw a5, (sp, 0x14)
  299. lrw a0, [.Lctx]
  300. lrw a1, [.Lfun]
  301. jmp a1
  302. .Lctx:
  303. mov a0, a0
  304. mov a0, a0
  305. .Lfun:
  306. .size ffi_csky_trampoline, .-ffi_csky_trampoline
  307. CSKY_FUNC_START ffi_csky_cacheflush
  308. lrw r1, 123
  309. trap 0
  310. rts
  311. .size ffi_csky_cacheflush, .-ffi_csky_cacheflush
  312. #endif /* __CSKYABIV2__ */