arm.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. const
  14. cpu_has_edsp : boolean = false;
  15. in_edsp_test : boolean = false;
  16. var
  17. moveproc : pointer;
  18. procedure fpc_cpuinit;
  19. begin
  20. {$if not(defined(wince)) and not(defined(gba)) and not(defined(nds))}
  21. asm
  22. rfs r0
  23. and r0,r0,#0xffe0ffff
  24. orr r0,r0,#0x00020000
  25. wfs r0
  26. end;
  27. {$endif}
  28. end;
  29. {$ifdef wince}
  30. function _controlfp(new: DWORD; mask: DWORD): DWORD; cdecl; external 'coredll';
  31. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  32. Procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  33. begin
  34. softfloat_exception_flags:=0;
  35. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  36. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  37. { FPU precision 64 bit, rounding to nearest, affine infinity }
  38. _controlfp($000C0003, $030F031F);
  39. end;
  40. {$endif wince}
  41. {****************************************************************************
  42. stack frame related stuff
  43. ****************************************************************************}
  44. {$IFNDEF INTERNAL_BACKTRACE}
  45. {$define FPC_SYSTEM_HAS_GET_FRAME}
  46. function get_frame:pointer;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  47. asm
  48. mov r0,r11
  49. end ['R0'];
  50. {$ENDIF not INTERNAL_BACKTRACE}
  51. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  52. function get_caller_addr(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  53. asm
  54. movs r0,r0
  55. beq .Lg_a_null
  56. ldr r0,[r0,#-4]
  57. .Lg_a_null:
  58. end ['R0'];
  59. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  60. function get_caller_frame(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  61. asm
  62. movs r0,r0
  63. beq .Lgnf_null
  64. ldr r0,[r0,#-12]
  65. .Lgnf_null:
  66. end ['R0'];
  67. {$define FPC_SYSTEM_HAS_SPTR}
  68. Function Sptr : pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
  69. asm
  70. mov r0,sp
  71. end ['R0'];
  72. {$define FPC_SYSTEM_HAS_FILLCHAR}
  73. Procedure FillChar(var x;count:longint;value:byte);assembler;nostackframe;
  74. asm
  75. // less than 0?
  76. cmp r1,#0
  77. movlt pc,lr
  78. mov r3,r0
  79. cmp r1,#8 // at least 8 bytes to do?
  80. blt .LFillchar2
  81. orr r2,r2,r2,lsl #8
  82. orr r2,r2,r2,lsl #16
  83. .LFillchar0:
  84. tst r3,#3 // aligned yet?
  85. strneb r2,[r3],#1
  86. subne r1,r1,#1
  87. bne .LFillchar0
  88. mov ip,r2
  89. .LFillchar1:
  90. cmp r1,#8 // 8 bytes still to do?
  91. blt .LFillchar2
  92. stmia r3!,{r2,ip}
  93. sub r1,r1,#8
  94. cmp r1,#8 // 8 bytes still to do?
  95. blt .LFillchar2
  96. stmia r3!,{r2,ip}
  97. sub r1,r1,#8
  98. cmp r1,#8 // 8 bytes still to do?
  99. blt .LFillchar2
  100. stmia r3!,{r2,ip}
  101. sub r1,r1,#8
  102. cmp r1,#8 // 8 bytes still to do?
  103. stmgeia r3!,{r2,ip}
  104. subge r1,r1,#8
  105. bge .LFillchar1
  106. .LFillchar2:
  107. movs r1,r1 // anything left?
  108. moveq pc,lr
  109. rsb r1,r1,#7
  110. add pc,pc,r1,lsl #2
  111. mov r0,r0
  112. strb r2,[r3],#1
  113. strb r2,[r3],#1
  114. strb r2,[r3],#1
  115. strb r2,[r3],#1
  116. strb r2,[r3],#1
  117. strb r2,[r3],#1
  118. strb r2,[r3],#1
  119. mov pc,lr
  120. end;
  121. {$ifndef FPC_SYSTEM_HAS_MOVE}
  122. {$define FPC_SYSTEM_HAS_MOVE}
  123. {$define FPC_SYSTEM_FPC_MOVE}
  124. procedure Move_pld(const source;var dest;count:longint);assembler;nostackframe;
  125. asm
  126. pld [r0]
  127. pld [r1]
  128. // count <=0 ?
  129. cmp r2,#0
  130. movle pc,lr
  131. // overlap?
  132. cmp r1,r0
  133. bls .Lnooverlap
  134. add r3,r0,r2
  135. cmp r3,r1
  136. bls .Lnooverlap
  137. // overlap, copy backward
  138. .Loverlapped:
  139. subs r2,r2,#1
  140. ldrb r3,[r0,r2]
  141. strb r3,[r1,r2]
  142. bne .Loverlapped
  143. mov pc,lr
  144. .Lnooverlap:
  145. // less then 16 bytes to copy?
  146. cmp r2,#8
  147. // yes, the forget about the whole optimizations
  148. // and do a bytewise copy
  149. blt .Lbyteloop
  150. // both aligned?
  151. orr r3,r0,r1
  152. tst r3,#3
  153. bne .Lbyteloop
  154. (*
  155. // yes, then align
  156. // alignment to 4 byte boundries is enough
  157. ldrb ip,[r0],#1
  158. sub r2,r2,#1
  159. stb ip,[r1],#1
  160. tst r3,#2
  161. bne .Ldifferentaligned
  162. ldrh ip,[r0],#2
  163. sub r2,r2,#2
  164. sth ip,[r1],#2
  165. .Ldifferentaligned
  166. // qword aligned?
  167. orrs r3,r0,r1
  168. tst r3,#7
  169. bne .Ldwordloop
  170. *)
  171. pld [r0,#32]
  172. pld [r1,#32]
  173. .Ldwordloop:
  174. sub r2,r2,#4
  175. ldr r3,[r0],#4
  176. // preload
  177. pld [r0,#64]
  178. pld [r1,#64]
  179. cmp r2,#4
  180. str r3,[r1],#4
  181. bcs .Ldwordloop
  182. cmp r2,#0
  183. moveq pc,lr
  184. .Lbyteloop:
  185. subs r2,r2,#1
  186. ldrb r3,[r0],#1
  187. strb r3,[r1],#1
  188. bne .Lbyteloop
  189. mov pc,lr
  190. end;
  191. procedure Move_blended(const source;var dest;count:longint);assembler;nostackframe;
  192. asm
  193. // count <=0 ?
  194. cmp r2,#0
  195. movle pc,lr
  196. // overlap?
  197. cmp r1,r0
  198. bls .Lnooverlap
  199. add r3,r0,r2
  200. cmp r3,r1
  201. bls .Lnooverlap
  202. // overlap, copy backward
  203. .Loverlapped:
  204. subs r2,r2,#1
  205. ldrb r3,[r0,r2]
  206. strb r3,[r1,r2]
  207. bne .Loverlapped
  208. mov pc,lr
  209. .Lnooverlap:
  210. // less then 16 bytes to copy?
  211. cmp r2,#8
  212. // yes, the forget about the whole optimizations
  213. // and do a bytewise copy
  214. blt .Lbyteloop
  215. // both aligned?
  216. orr r3,r0,r1
  217. tst r3,#3
  218. bne .Lbyteloop
  219. (*
  220. // yes, then align
  221. // alignment to 4 byte boundries is enough
  222. ldrb ip,[r0],#1
  223. sub r2,r2,#1
  224. stb ip,[r1],#1
  225. tst r3,#2
  226. bne .Ldifferentaligned
  227. ldrh ip,[r0],#2
  228. sub r2,r2,#2
  229. sth ip,[r1],#2
  230. .Ldifferentaligned
  231. // qword aligned?
  232. orrs r3,r0,r1
  233. tst r3,#7
  234. bne .Ldwordloop
  235. *)
  236. .Ldwordloop:
  237. sub r2,r2,#4
  238. ldr r3,[r0],#4
  239. cmp r2,#4
  240. str r3,[r1],#4
  241. bcs .Ldwordloop
  242. cmp r2,#0
  243. moveq pc,lr
  244. .Lbyteloop:
  245. subs r2,r2,#1
  246. ldrb r3,[r0],#1
  247. strb r3,[r1],#1
  248. bne .Lbyteloop
  249. mov pc,lr
  250. end;
  251. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  252. asm
  253. ldr ip,.Lmoveproc
  254. ldr pc,[ip]
  255. .Lmoveproc:
  256. .long moveproc
  257. end;
  258. {$endif FPC_SYSTEM_HAS_MOVE}
  259. var
  260. fpc_system_lock: longint; export name 'fpc_system_lock';
  261. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  262. asm
  263. // lock
  264. ldr r3, .Lfpc_system_lock
  265. mov r1, #1
  266. .Lloop:
  267. swp r2, r1, [r3]
  268. cmp r2, #0
  269. bne .Lloop
  270. // do the job
  271. ldr r1, [r0]
  272. sub r1, r1, #1
  273. str r1, [r0]
  274. mov r0, r1
  275. // unlock and return
  276. str r2, [r3]
  277. mov pc, lr
  278. .Lfpc_system_lock:
  279. .long fpc_system_lock
  280. end;
  281. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  282. asm
  283. // lock
  284. ldr r3, .Lfpc_system_lock
  285. mov r1, #1
  286. .Lloop:
  287. swp r2, r1, [r3]
  288. cmp r2, #0
  289. bne .Lloop
  290. // do the job
  291. ldr r1, [r0]
  292. add r1, r1, #1
  293. str r1, [r0]
  294. mov r0, r1
  295. // unlock and return
  296. str r2, [r3]
  297. mov pc, lr
  298. .Lfpc_system_lock:
  299. .long fpc_system_lock
  300. end;
  301. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  302. asm
  303. swp r1, r1, [r0]
  304. mov r0,r1
  305. end;
  306. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  307. asm
  308. // lock
  309. ldr r3, .Lfpc_system_lock
  310. mov r2, #1
  311. .Lloop:
  312. swp r2, r2, [r3]
  313. cmp r2, #0
  314. bne .Lloop
  315. // do the job
  316. ldr r2, [r0]
  317. add r1, r1, r2
  318. str r1, [r0]
  319. mov r0, r2
  320. // unlock and return
  321. mov r2, #0
  322. str r2, [r3]
  323. mov pc, lr
  324. .Lfpc_system_lock:
  325. .long fpc_system_lock
  326. end;
  327. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  328. asm
  329. // lock
  330. ldr r12, .Lfpc_system_lock
  331. mov r3, #1
  332. .Lloop:
  333. swp r3, r3, [r12]
  334. cmp r3, #0
  335. bne .Lloop
  336. // do the job
  337. ldr r3, [r0]
  338. cmp r3, r2
  339. streq r1, [r0]
  340. mov r0, r3
  341. // unlock and return
  342. mov r3, #0
  343. str r3, [r12]
  344. mov pc, lr
  345. .Lfpc_system_lock:
  346. .long fpc_system_lock
  347. end;
  348. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  349. function declocked(var l: longint) : boolean; inline;
  350. begin
  351. Result:=InterLockedDecrement(l) = 0;
  352. end;
  353. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  354. procedure inclocked(var l: longint); inline;
  355. begin
  356. InterLockedIncrement(l);
  357. end;
  358. procedure fpc_cpucodeinit;
  359. begin
  360. cpu_has_edsp:=true;
  361. in_edsp_test:=true;
  362. asm
  363. pld [r0]
  364. end;
  365. in_edsp_test:=false;
  366. {$ifdef FPC_SYSTEM_FPC_MOVE}
  367. if cpu_has_edsp then
  368. moveproc:=@move_pld
  369. else
  370. moveproc:=@move_blended;
  371. {$endif FPC_SYSTEM_FPC_MOVE}
  372. end;