arm.inc 8.6 KB

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