arm.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team.
  4. Processor dependent implementation for the system unit for
  5. ARM
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$asmmode gas}
  13. procedure fpc_cpuinit;
  14. begin
  15. {$if not(defined(wince)) and not(defined(gba)) and not(defined(nds))}
  16. asm
  17. rfs r0
  18. and r0,r0,#0xffe0ffff
  19. orr r0,r0,#0x00020000
  20. wfs r0
  21. end;
  22. {$endif}
  23. end;
  24. {****************************************************************************
  25. stack frame related stuff
  26. ****************************************************************************}
  27. {$IFNDEF INTERNAL_BACKTRACE}
  28. {$define FPC_SYSTEM_HAS_GET_FRAME}
  29. function get_frame:pointer;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  30. asm
  31. mov r0,r11
  32. end ['R0'];
  33. {$ENDIF not INTERNAL_BACKTRACE}
  34. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  35. function get_caller_addr(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  36. asm
  37. movs r0,r0
  38. beq .Lg_a_null
  39. ldr r0,[r0,#-4]
  40. .Lg_a_null:
  41. end ['R0'];
  42. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  43. function get_caller_frame(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  44. asm
  45. movs r0,r0
  46. beq .Lgnf_null
  47. ldr r0,[r0,#-12]
  48. .Lgnf_null:
  49. end ['R0'];
  50. {$define FPC_SYSTEM_HAS_SPTR}
  51. Function Sptr : pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  52. asm
  53. mov r0,sp
  54. end ['R0'];
  55. {$define FPC_SYSTEM_HAS_FILLCHAR}
  56. Procedure FillChar(var x;count:longint;value:byte);assembler;nostackframe;
  57. asm
  58. // less than 0?
  59. cmp r1,#0
  60. movlt pc,lr
  61. mov r3,r0
  62. cmp r1,#8 // at least 8 bytes to do?
  63. blt .LFillchar2
  64. orr r2,r2,r2,lsl #8
  65. orr r2,r2,r2,lsl #16
  66. .LFillchar0:
  67. tst r3,#3 // aligned yet?
  68. strneb r2,[r3],#1
  69. subne r1,r1,#1
  70. bne .LFillchar0
  71. mov ip,r2
  72. .LFillchar1:
  73. cmp r1,#8 // 8 bytes still to do?
  74. blt .LFillchar2
  75. stmia r3!,{r2,ip}
  76. sub r1,r1,#8
  77. cmp r1,#8 // 8 bytes still to do?
  78. blt .LFillchar2
  79. stmia r3!,{r2,ip}
  80. sub r1,r1,#8
  81. cmp r1,#8 // 8 bytes still to do?
  82. blt .LFillchar2
  83. stmia r3!,{r2,ip}
  84. sub r1,r1,#8
  85. cmp r1,#8 // 8 bytes still to do?
  86. stmgeia r3!,{r2,ip}
  87. subge r1,r1,#8
  88. bge .LFillchar1
  89. .LFillchar2:
  90. movs r1,r1 // anything left?
  91. moveq pc,lr
  92. rsb r1,r1,#7
  93. add pc,pc,r1,lsl #2
  94. mov r0,r0
  95. strb r2,[r3],#1
  96. strb r2,[r3],#1
  97. strb r2,[r3],#1
  98. strb r2,[r3],#1
  99. strb r2,[r3],#1
  100. strb r2,[r3],#1
  101. strb r2,[r3],#1
  102. mov pc,lr
  103. end;
  104. (*
  105. {$define FPC_SYSTEM_HAS_MOVE}
  106. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  107. asm
  108. // count <=0 ?
  109. cmp r2,#0
  110. movle pc,lr
  111. // overlap?
  112. cmp r2,r1
  113. bls .Lnoverlap
  114. add r3,r0,r2
  115. cmp r3,r2
  116. bls .Lnooverlap
  117. // overlap, copy backward
  118. .Loverlapped
  119. subs r2,r2,#1
  120. ldrb r3,[r0,r2]
  121. strb r3,[r1,r2]
  122. bne .Loverlapped
  123. mov pc,lr
  124. .Lnooverlap:
  125. // less then 16 bytes to copy?
  126. cmp r2,#16
  127. // yes, the forget about the whole optimizations
  128. // and do a bytewise copy
  129. blt .Lbyteloop
  130. // both uneven aligned?
  131. eor r3,r0,r1
  132. tst r3,#1
  133. bne .Ldifferentaligned
  134. // yes, then align
  135. // alignment to 4 byte boundries is enough
  136. ldrb ip,[r0],#1
  137. sub r2,r2,#1
  138. stb ip,[r1],#1
  139. tst r3,#2
  140. bne .Ldifferentaligned
  141. ldrh ip,[r0],#2
  142. sub r2,r2,#2
  143. sth ip,[r1],#2
  144. .Ldifferentaligned
  145. // qword aligned?
  146. orrs r3,r0,r1
  147. tst r3,#7
  148. bne .Ldwordloop
  149. .Lqwordloop:
  150. sub r2,r2,#8
  151. ldmia r0!,{r3,ip}
  152. stmia r1!,{r3,ip}
  153. .Lbyteloop:
  154. ldrb r3,[r0],#1
  155. strb r3,[r1],#1
  156. subs r2,r2,#1
  157. bne .Lbyteloop
  158. mov pc,lr
  159. end;
  160. *)
  161. var
  162. fpc_system_lock: longint; export name 'fpc_system_lock';
  163. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  164. asm
  165. // lock
  166. ldr r3, .Lfpc_system_lock
  167. mov r1, #1
  168. .Lloop:
  169. swp r2, r1, [r3]
  170. cmp r2, #0
  171. bne .Lloop
  172. // do the job
  173. ldr r1, [r0]
  174. sub r1, r1, #1
  175. str r1, [r0]
  176. mov r0, r1
  177. // unlock and return
  178. str r2, [r3]
  179. mov pc, lr
  180. .Lfpc_system_lock:
  181. .long fpc_system_lock
  182. end;
  183. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  184. asm
  185. // lock
  186. ldr r3, .Lfpc_system_lock
  187. mov r1, #1
  188. .Lloop:
  189. swp r2, r1, [r3]
  190. cmp r2, #0
  191. bne .Lloop
  192. // do the job
  193. ldr r1, [r0]
  194. add r1, r1, #1
  195. str r1, [r0]
  196. mov r0, r1
  197. // unlock and return
  198. str r2, [r3]
  199. mov pc, lr
  200. .Lfpc_system_lock:
  201. .long fpc_system_lock
  202. end;
  203. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  204. asm
  205. swp r1, r1, [r0]
  206. mov r0,r1
  207. end;
  208. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  209. asm
  210. // lock
  211. ldr r3, .Lfpc_system_lock
  212. mov r2, #1
  213. .Lloop:
  214. swp r2, r2, [r3]
  215. cmp r2, #0
  216. bne .Lloop
  217. // do the job
  218. ldr r2, [r0]
  219. add r1, r1, r2
  220. str r1, [r0]
  221. mov r0, r2
  222. // unlock and return
  223. mov r2, #0
  224. str r2, [r3]
  225. mov pc, lr
  226. .Lfpc_system_lock:
  227. .long fpc_system_lock
  228. end;
  229. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  230. asm
  231. // lock
  232. ldr r12, .Lfpc_system_lock
  233. mov r3, #1
  234. .Lloop:
  235. swp r3, r3, [r12]
  236. cmp r3, #0
  237. bne .Lloop
  238. // do the job
  239. ldr r3, [r0]
  240. cmp r3, r2
  241. streq r1, [r0]
  242. mov r0, r3
  243. // unlock and return
  244. mov r3, #0
  245. str r3, [r12]
  246. mov pc, lr
  247. .Lfpc_system_lock:
  248. .long fpc_system_lock
  249. end;
  250. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  251. function declocked(var l: longint) : boolean; inline;
  252. begin
  253. Result:=InterLockedDecrement(l) = 0;
  254. end;
  255. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  256. procedure inclocked(var l: longint); inline;
  257. begin
  258. InterLockedIncrement(l);
  259. end;