arm.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. {$ifdef wince}
  25. function _controlfp(new: DWORD; mask: DWORD): DWORD; cdecl; external 'coredll';
  26. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  27. Procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  28. begin
  29. softfloat_exception_flags:=0;
  30. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  31. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  32. { FPU precision 64 bit, rounding to nearest, affine infinity }
  33. _controlfp($000C0003, $030F031F);
  34. end;
  35. {$endif wince}
  36. {****************************************************************************
  37. stack frame related stuff
  38. ****************************************************************************}
  39. {$IFNDEF INTERNAL_BACKTRACE}
  40. {$define FPC_SYSTEM_HAS_GET_FRAME}
  41. function get_frame:pointer;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  42. asm
  43. mov r0,r11
  44. end ['R0'];
  45. {$ENDIF not INTERNAL_BACKTRACE}
  46. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  47. function get_caller_addr(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  48. asm
  49. movs r0,r0
  50. beq .Lg_a_null
  51. ldr r0,[r0,#-4]
  52. .Lg_a_null:
  53. end ['R0'];
  54. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  55. function get_caller_frame(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  56. asm
  57. movs r0,r0
  58. beq .Lgnf_null
  59. ldr r0,[r0,#-12]
  60. .Lgnf_null:
  61. end ['R0'];
  62. {$define FPC_SYSTEM_HAS_SPTR}
  63. Function Sptr : pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  64. asm
  65. mov r0,sp
  66. end ['R0'];
  67. {$define FPC_SYSTEM_HAS_FILLCHAR}
  68. Procedure FillChar(var x;count:longint;value:byte);assembler;nostackframe;
  69. asm
  70. // less than 0?
  71. cmp r1,#0
  72. movlt pc,lr
  73. mov r3,r0
  74. cmp r1,#8 // at least 8 bytes to do?
  75. blt .LFillchar2
  76. orr r2,r2,r2,lsl #8
  77. orr r2,r2,r2,lsl #16
  78. .LFillchar0:
  79. tst r3,#3 // aligned yet?
  80. strneb r2,[r3],#1
  81. subne r1,r1,#1
  82. bne .LFillchar0
  83. mov ip,r2
  84. .LFillchar1:
  85. cmp r1,#8 // 8 bytes still to do?
  86. blt .LFillchar2
  87. stmia r3!,{r2,ip}
  88. sub r1,r1,#8
  89. cmp r1,#8 // 8 bytes still to do?
  90. blt .LFillchar2
  91. stmia r3!,{r2,ip}
  92. sub r1,r1,#8
  93. cmp r1,#8 // 8 bytes still to do?
  94. blt .LFillchar2
  95. stmia r3!,{r2,ip}
  96. sub r1,r1,#8
  97. cmp r1,#8 // 8 bytes still to do?
  98. stmgeia r3!,{r2,ip}
  99. subge r1,r1,#8
  100. bge .LFillchar1
  101. .LFillchar2:
  102. movs r1,r1 // anything left?
  103. moveq pc,lr
  104. rsb r1,r1,#7
  105. add pc,pc,r1,lsl #2
  106. mov r0,r0
  107. strb r2,[r3],#1
  108. strb r2,[r3],#1
  109. strb r2,[r3],#1
  110. strb r2,[r3],#1
  111. strb r2,[r3],#1
  112. strb r2,[r3],#1
  113. strb r2,[r3],#1
  114. mov pc,lr
  115. end;
  116. (*
  117. {$define FPC_SYSTEM_HAS_MOVE}
  118. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  119. asm
  120. // count <=0 ?
  121. cmp r2,#0
  122. movle pc,lr
  123. // overlap?
  124. cmp r2,r1
  125. bls .Lnoverlap
  126. add r3,r0,r2
  127. cmp r3,r2
  128. bls .Lnooverlap
  129. // overlap, copy backward
  130. .Loverlapped
  131. subs r2,r2,#1
  132. ldrb r3,[r0,r2]
  133. strb r3,[r1,r2]
  134. bne .Loverlapped
  135. mov pc,lr
  136. .Lnooverlap:
  137. // less then 16 bytes to copy?
  138. cmp r2,#16
  139. // yes, the forget about the whole optimizations
  140. // and do a bytewise copy
  141. blt .Lbyteloop
  142. // both uneven aligned?
  143. eor r3,r0,r1
  144. tst r3,#1
  145. bne .Ldifferentaligned
  146. // yes, then align
  147. // alignment to 4 byte boundries is enough
  148. ldrb ip,[r0],#1
  149. sub r2,r2,#1
  150. stb ip,[r1],#1
  151. tst r3,#2
  152. bne .Ldifferentaligned
  153. ldrh ip,[r0],#2
  154. sub r2,r2,#2
  155. sth ip,[r1],#2
  156. .Ldifferentaligned
  157. // qword aligned?
  158. orrs r3,r0,r1
  159. tst r3,#7
  160. bne .Ldwordloop
  161. .Lqwordloop:
  162. sub r2,r2,#8
  163. ldmia r0!,{r3,ip}
  164. stmia r1!,{r3,ip}
  165. .Lbyteloop:
  166. ldrb r3,[r0],#1
  167. strb r3,[r1],#1
  168. subs r2,r2,#1
  169. bne .Lbyteloop
  170. mov pc,lr
  171. end;
  172. *)
  173. var
  174. fpc_system_lock: longint; export name 'fpc_system_lock';
  175. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  176. asm
  177. // lock
  178. ldr r3, .Lfpc_system_lock
  179. mov r1, #1
  180. .Lloop:
  181. swp r2, r1, [r3]
  182. cmp r2, #0
  183. bne .Lloop
  184. // do the job
  185. ldr r1, [r0]
  186. sub r1, r1, #1
  187. str r1, [r0]
  188. mov r0, r1
  189. // unlock and return
  190. str r2, [r3]
  191. mov pc, lr
  192. .Lfpc_system_lock:
  193. .long fpc_system_lock
  194. end;
  195. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  196. asm
  197. // lock
  198. ldr r3, .Lfpc_system_lock
  199. mov r1, #1
  200. .Lloop:
  201. swp r2, r1, [r3]
  202. cmp r2, #0
  203. bne .Lloop
  204. // do the job
  205. ldr r1, [r0]
  206. add r1, r1, #1
  207. str r1, [r0]
  208. mov r0, r1
  209. // unlock and return
  210. str r2, [r3]
  211. mov pc, lr
  212. .Lfpc_system_lock:
  213. .long fpc_system_lock
  214. end;
  215. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  216. asm
  217. swp r1, r1, [r0]
  218. mov r0,r1
  219. end;
  220. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  221. asm
  222. // lock
  223. ldr r3, .Lfpc_system_lock
  224. mov r2, #1
  225. .Lloop:
  226. swp r2, r2, [r3]
  227. cmp r2, #0
  228. bne .Lloop
  229. // do the job
  230. ldr r2, [r0]
  231. add r1, r1, r2
  232. str r1, [r0]
  233. mov r0, r2
  234. // unlock and return
  235. mov r2, #0
  236. str r2, [r3]
  237. mov pc, lr
  238. .Lfpc_system_lock:
  239. .long fpc_system_lock
  240. end;
  241. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  242. asm
  243. // lock
  244. ldr r12, .Lfpc_system_lock
  245. mov r3, #1
  246. .Lloop:
  247. swp r3, r3, [r12]
  248. cmp r3, #0
  249. bne .Lloop
  250. // do the job
  251. ldr r3, [r0]
  252. cmp r3, r2
  253. streq r1, [r0]
  254. mov r0, r3
  255. // unlock and return
  256. mov r3, #0
  257. str r3, [r12]
  258. mov pc, lr
  259. .Lfpc_system_lock:
  260. .long fpc_system_lock
  261. end;
  262. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  263. function declocked(var l: longint) : boolean; inline;
  264. begin
  265. Result:=InterLockedDecrement(l) = 0;
  266. end;
  267. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  268. procedure inclocked(var l: longint); inline;
  269. begin
  270. InterLockedIncrement(l);
  271. end;