arm.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  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. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  122. {$ifndef FPC_SYSTEM_HAS_MOVE}
  123. {$define FPC_SYSTEM_HAS_MOVE}
  124. {$define FPC_SYSTEM_FPC_MOVE}
  125. procedure Move_pld(const source;var dest;count:longint);assembler;nostackframe;
  126. asm
  127. pld [r0]
  128. pld [r1]
  129. // count <=0 ?
  130. cmp r2,#0
  131. movle pc,lr
  132. // overlap?
  133. cmp r1,r0
  134. bls .Lnooverlap
  135. add r3,r0,r2
  136. cmp r3,r1
  137. bls .Lnooverlap
  138. // overlap, copy backward
  139. .Loverlapped:
  140. subs r2,r2,#1
  141. ldrb r3,[r0,r2]
  142. strb r3,[r1,r2]
  143. bne .Loverlapped
  144. mov pc,lr
  145. .Lnooverlap:
  146. // less then 16 bytes to copy?
  147. cmp r2,#8
  148. // yes, the forget about the whole optimizations
  149. // and do a bytewise copy
  150. blt .Lbyteloop
  151. // both aligned?
  152. orr r3,r0,r1
  153. tst r3,#3
  154. bne .Lbyteloop
  155. (*
  156. // yes, then align
  157. // alignment to 4 byte boundries is enough
  158. ldrb ip,[r0],#1
  159. sub r2,r2,#1
  160. stb ip,[r1],#1
  161. tst r3,#2
  162. bne .Ldifferentaligned
  163. ldrh ip,[r0],#2
  164. sub r2,r2,#2
  165. sth ip,[r1],#2
  166. .Ldifferentaligned
  167. // qword aligned?
  168. orrs r3,r0,r1
  169. tst r3,#7
  170. bne .Ldwordloop
  171. *)
  172. pld [r0,#32]
  173. pld [r1,#32]
  174. .Ldwordloop:
  175. sub r2,r2,#4
  176. ldr r3,[r0],#4
  177. // preload
  178. pld [r0,#64]
  179. pld [r1,#64]
  180. cmp r2,#4
  181. str r3,[r1],#4
  182. bcs .Ldwordloop
  183. cmp r2,#0
  184. moveq pc,lr
  185. .Lbyteloop:
  186. subs r2,r2,#1
  187. ldrb r3,[r0],#1
  188. strb r3,[r1],#1
  189. bne .Lbyteloop
  190. mov pc,lr
  191. end;
  192. procedure Move_blended(const source;var dest;count:longint);assembler;nostackframe;
  193. asm
  194. // count <=0 ?
  195. cmp r2,#0
  196. movle pc,lr
  197. // overlap?
  198. cmp r1,r0
  199. bls .Lnooverlap
  200. add r3,r0,r2
  201. cmp r3,r1
  202. bls .Lnooverlap
  203. // overlap, copy backward
  204. .Loverlapped:
  205. subs r2,r2,#1
  206. ldrb r3,[r0,r2]
  207. strb r3,[r1,r2]
  208. bne .Loverlapped
  209. mov pc,lr
  210. .Lnooverlap:
  211. // less then 16 bytes to copy?
  212. cmp r2,#8
  213. // yes, the forget about the whole optimizations
  214. // and do a bytewise copy
  215. blt .Lbyteloop
  216. // both aligned?
  217. orr r3,r0,r1
  218. tst r3,#3
  219. bne .Lbyteloop
  220. (*
  221. // yes, then align
  222. // alignment to 4 byte boundries is enough
  223. ldrb ip,[r0],#1
  224. sub r2,r2,#1
  225. stb ip,[r1],#1
  226. tst r3,#2
  227. bne .Ldifferentaligned
  228. ldrh ip,[r0],#2
  229. sub r2,r2,#2
  230. sth ip,[r1],#2
  231. .Ldifferentaligned
  232. // qword aligned?
  233. orrs r3,r0,r1
  234. tst r3,#7
  235. bne .Ldwordloop
  236. *)
  237. .Ldwordloop:
  238. sub r2,r2,#4
  239. ldr r3,[r0],#4
  240. cmp r2,#4
  241. str r3,[r1],#4
  242. bcs .Ldwordloop
  243. cmp r2,#0
  244. moveq pc,lr
  245. .Lbyteloop:
  246. subs r2,r2,#1
  247. ldrb r3,[r0],#1
  248. strb r3,[r1],#1
  249. bne .Lbyteloop
  250. mov pc,lr
  251. end;
  252. const
  253. moveproc : pointer = @move_blended;
  254. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  255. asm
  256. ldr ip,.Lmoveproc
  257. ldr pc,[ip]
  258. .Lmoveproc:
  259. .long moveproc
  260. end;
  261. {$endif FPC_SYSTEM_HAS_MOVE}
  262. var
  263. fpc_system_lock: longint; export name 'fpc_system_lock';
  264. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  265. asm
  266. // lock
  267. ldr r3, .Lfpc_system_lock
  268. mov r1, #1
  269. .Lloop:
  270. swp r2, r1, [r3]
  271. cmp r2, #0
  272. bne .Lloop
  273. // do the job
  274. ldr r1, [r0]
  275. sub r1, r1, #1
  276. str r1, [r0]
  277. mov r0, r1
  278. // unlock and return
  279. str r2, [r3]
  280. mov pc, lr
  281. .Lfpc_system_lock:
  282. .long fpc_system_lock
  283. end;
  284. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  285. asm
  286. // lock
  287. ldr r3, .Lfpc_system_lock
  288. mov r1, #1
  289. .Lloop:
  290. swp r2, r1, [r3]
  291. cmp r2, #0
  292. bne .Lloop
  293. // do the job
  294. ldr r1, [r0]
  295. add r1, r1, #1
  296. str r1, [r0]
  297. mov r0, r1
  298. // unlock and return
  299. str r2, [r3]
  300. mov pc, lr
  301. .Lfpc_system_lock:
  302. .long fpc_system_lock
  303. end;
  304. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  305. asm
  306. swp r1, r1, [r0]
  307. mov r0,r1
  308. end;
  309. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  310. asm
  311. // lock
  312. ldr r3, .Lfpc_system_lock
  313. mov r2, #1
  314. .Lloop:
  315. swp r2, r2, [r3]
  316. cmp r2, #0
  317. bne .Lloop
  318. // do the job
  319. ldr r2, [r0]
  320. add r1, r1, r2
  321. str r1, [r0]
  322. mov r0, r2
  323. // unlock and return
  324. mov r2, #0
  325. str r2, [r3]
  326. mov pc, lr
  327. .Lfpc_system_lock:
  328. .long fpc_system_lock
  329. end;
  330. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  331. asm
  332. // lock
  333. ldr r12, .Lfpc_system_lock
  334. mov r3, #1
  335. .Lloop:
  336. swp r3, r3, [r12]
  337. cmp r3, #0
  338. bne .Lloop
  339. // do the job
  340. ldr r3, [r0]
  341. cmp r3, r2
  342. streq r1, [r0]
  343. mov r0, r3
  344. // unlock and return
  345. mov r3, #0
  346. str r3, [r12]
  347. mov pc, lr
  348. .Lfpc_system_lock:
  349. .long fpc_system_lock
  350. end;
  351. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  352. function declocked(var l: longint) : boolean; inline;
  353. begin
  354. Result:=InterLockedDecrement(l) = 0;
  355. end;
  356. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  357. procedure inclocked(var l: longint); inline;
  358. begin
  359. InterLockedIncrement(l);
  360. end;
  361. procedure fpc_cpucodeinit;
  362. begin
  363. cpu_has_edsp:=true;
  364. in_edsp_test:=true;
  365. asm
  366. pld [r0]
  367. end;
  368. in_edsp_test:=false;
  369. {$ifdef FPC_SYSTEM_FPC_MOVE}
  370. if cpu_has_edsp then
  371. moveproc:=@move_pld
  372. else
  373. moveproc:=@move_blended;
  374. {$endif FPC_SYSTEM_FPC_MOVE}
  375. end;