arm.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. {$ifndef FPC_SYSTEM_HAS_MOVE}
  14. {$define FPC_SYSTEM_FPC_MOVE}
  15. {$endif FPC_SYSTEM_HAS_MOVE}
  16. {$ifdef FPC_SYSTEM_FPC_MOVE}
  17. const
  18. cpu_has_edsp : boolean = false;
  19. in_edsp_test : boolean = false;
  20. {$endif FPC_SYSTEM_FPC_MOVE}
  21. {$if not(defined(wince)) and not(defined(gba)) and not(defined(nds)) and not(defined(FPUSOFT)) and not(defined(FPULIBGCC))}
  22. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  23. {$if not defined(darwin) and not defined(FPUVFPV2) and not defined(FPUVFPV3)}
  24. Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  25. begin
  26. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  27. asm
  28. rfs r0
  29. and r0,r0,#0xffe0ffff
  30. orr r0,r0,#0x00070000
  31. wfs r0
  32. end;
  33. end;
  34. {$else}
  35. Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  36. begin
  37. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  38. asm
  39. fmrx r0,fpscr
  40. // set "round to nearest" mode
  41. and r0,r0,#0xff3fffff
  42. // mask "exception happened" and overflow flags
  43. and r0,r0,#0xffffff20
  44. // mask exception flags
  45. and r0,r0,#0xffff40ff
  46. {$ifndef darwin}
  47. // Floating point exceptions cause kernel panics on iPhoneOS 2.2.1...
  48. // disable flush-to-zero mode (IEEE math compliant)
  49. and r0,r0,#0xfeffffff
  50. // enable invalid operation, div-by-zero and overflow exceptions
  51. orr r0,r0,#0x00000700
  52. {$endif}
  53. fmxr fpscr,r0
  54. end;
  55. end;
  56. {$endif}
  57. {$endif}
  58. procedure fpc_cpuinit;
  59. begin
  60. { don't let libraries influence the FPU cw set by the host program }
  61. if not IsLibrary then
  62. SysInitFPU;
  63. end;
  64. {$ifdef wince}
  65. function _controlfp(new: DWORD; mask: DWORD): DWORD; cdecl; external 'coredll';
  66. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  67. Procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  68. begin
  69. softfloat_exception_flags:=0;
  70. end;
  71. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  72. Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  73. begin
  74. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  75. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  76. { FPU precision 64 bit, rounding to nearest, affine infinity }
  77. _controlfp($000C0003, $030F031F);
  78. end;
  79. {$endif wince}
  80. {****************************************************************************
  81. stack frame related stuff
  82. ****************************************************************************}
  83. {$IFNDEF INTERNAL_BACKTRACE}
  84. {$define FPC_SYSTEM_HAS_GET_FRAME}
  85. function get_frame:pointer;assembler;nostackframe;
  86. asm
  87. mov r0,r11
  88. end;
  89. {$ENDIF not INTERNAL_BACKTRACE}
  90. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  91. function get_caller_addr(framebp:pointer):pointer;assembler;
  92. asm
  93. movs r0,r0
  94. beq .Lg_a_null
  95. ldr r0,[r0,#-4]
  96. .Lg_a_null:
  97. end;
  98. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  99. function get_caller_frame(framebp:pointer):pointer;assembler;
  100. asm
  101. movs r0,r0
  102. beq .Lgnf_null
  103. // see comments in arm/cgcpu.pas, g_proc_entry
  104. ldr r0,[r0,#-12]
  105. .Lgnf_null:
  106. end;
  107. {$define FPC_SYSTEM_HAS_SPTR}
  108. Function Sptr : pointer;assembler;
  109. asm
  110. mov r0,sp
  111. end;
  112. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  113. {$define FPC_SYSTEM_HAS_FILLCHAR}
  114. Procedure FillChar(var x;count:longint;value:byte);assembler;nostackframe;
  115. asm
  116. // less than 0?
  117. cmp r1,#0
  118. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  119. movlt pc,lr
  120. {$else}
  121. bxlt lr
  122. {$endif}
  123. mov r3,r0
  124. cmp r1,#8 // at least 8 bytes to do?
  125. blt .LFillchar2
  126. orr r2,r2,r2,lsl #8
  127. orr r2,r2,r2,lsl #16
  128. .LFillchar0:
  129. tst r3,#3 // aligned yet?
  130. strneb r2,[r3],#1
  131. subne r1,r1,#1
  132. bne .LFillchar0
  133. mov ip,r2
  134. .LFillchar1:
  135. cmp r1,#8 // 8 bytes still to do?
  136. blt .LFillchar2
  137. stmia r3!,{r2,ip}
  138. sub r1,r1,#8
  139. cmp r1,#8 // 8 bytes still to do?
  140. blt .LFillchar2
  141. stmia r3!,{r2,ip}
  142. sub r1,r1,#8
  143. cmp r1,#8 // 8 bytes still to do?
  144. blt .LFillchar2
  145. stmia r3!,{r2,ip}
  146. sub r1,r1,#8
  147. cmp r1,#8 // 8 bytes still to do?
  148. stmgeia r3!,{r2,ip}
  149. subge r1,r1,#8
  150. bge .LFillchar1
  151. .LFillchar2:
  152. movs r1,r1 // anything left?
  153. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  154. moveq pc,lr
  155. {$else}
  156. bxeq lr
  157. {$endif}
  158. rsb r1,r1,#7
  159. add pc,pc,r1,lsl #2
  160. mov r0,r0
  161. strb r2,[r3],#1
  162. strb r2,[r3],#1
  163. strb r2,[r3],#1
  164. strb r2,[r3],#1
  165. strb r2,[r3],#1
  166. strb r2,[r3],#1
  167. strb r2,[r3],#1
  168. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  169. mov pc,lr
  170. {$else}
  171. bx lr
  172. {$endif}
  173. end;
  174. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  175. {$ifndef FPC_SYSTEM_HAS_MOVE}
  176. {$define FPC_SYSTEM_HAS_MOVE}
  177. procedure Move_pld(const source;var dest;count:longint);assembler;nostackframe;
  178. asm
  179. pld [r0]
  180. pld [r1]
  181. // count <=0 ?
  182. cmp r2,#0
  183. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  184. movle pc,lr
  185. {$else}
  186. bxle lr
  187. {$endif}
  188. // overlap?
  189. cmp r1,r0
  190. bls .Lnooverlap
  191. add r3,r0,r2
  192. cmp r3,r1
  193. bls .Lnooverlap
  194. // overlap, copy backward
  195. .Loverlapped:
  196. subs r2,r2,#1
  197. ldrb r3,[r0,r2]
  198. strb r3,[r1,r2]
  199. bne .Loverlapped
  200. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  201. mov pc,lr
  202. {$else}
  203. bx lr
  204. {$endif}
  205. .Lnooverlap:
  206. // less then 16 bytes to copy?
  207. cmp r2,#8
  208. // yes, the forget about the whole optimizations
  209. // and do a bytewise copy
  210. blt .Lbyteloop
  211. // both aligned?
  212. orr r3,r0,r1
  213. tst r3,#3
  214. bne .Lbyteloop
  215. (*
  216. // yes, then align
  217. // alignment to 4 byte boundries is enough
  218. ldrb ip,[r0],#1
  219. sub r2,r2,#1
  220. stb ip,[r1],#1
  221. tst r3,#2
  222. bne .Ldifferentaligned
  223. ldrh ip,[r0],#2
  224. sub r2,r2,#2
  225. sth ip,[r1],#2
  226. .Ldifferentaligned
  227. // qword aligned?
  228. orrs r3,r0,r1
  229. tst r3,#7
  230. bne .Ldwordloop
  231. *)
  232. pld [r0,#32]
  233. pld [r1,#32]
  234. .Ldwordloop:
  235. sub r2,r2,#4
  236. ldr r3,[r0],#4
  237. // preload
  238. pld [r0,#64]
  239. pld [r1,#64]
  240. cmp r2,#4
  241. str r3,[r1],#4
  242. bcs .Ldwordloop
  243. cmp r2,#0
  244. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  245. moveq pc,lr
  246. {$else}
  247. bxeq lr
  248. {$endif}
  249. .Lbyteloop:
  250. subs r2,r2,#1
  251. ldrb r3,[r0],#1
  252. strb r3,[r1],#1
  253. bne .Lbyteloop
  254. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  255. mov pc,lr
  256. {$else}
  257. bx lr
  258. {$endif}
  259. end;
  260. procedure Move_blended(const source;var dest;count:longint);assembler;nostackframe;
  261. asm
  262. // count <=0 ?
  263. cmp r2,#0
  264. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  265. movle pc,lr
  266. {$else}
  267. bxle lr
  268. {$endif}
  269. // overlap?
  270. cmp r1,r0
  271. bls .Lnooverlap
  272. add r3,r0,r2
  273. cmp r3,r1
  274. bls .Lnooverlap
  275. // overlap, copy backward
  276. .Loverlapped:
  277. subs r2,r2,#1
  278. ldrb r3,[r0,r2]
  279. strb r3,[r1,r2]
  280. bne .Loverlapped
  281. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  282. mov pc,lr
  283. {$else}
  284. bx lr
  285. {$endif}
  286. .Lnooverlap:
  287. // less then 16 bytes to copy?
  288. cmp r2,#8
  289. // yes, the forget about the whole optimizations
  290. // and do a bytewise copy
  291. blt .Lbyteloop
  292. // both aligned?
  293. orr r3,r0,r1
  294. tst r3,#3
  295. bne .Lbyteloop
  296. (*
  297. // yes, then align
  298. // alignment to 4 byte boundries is enough
  299. ldrb ip,[r0],#1
  300. sub r2,r2,#1
  301. stb ip,[r1],#1
  302. tst r3,#2
  303. bne .Ldifferentaligned
  304. ldrh ip,[r0],#2
  305. sub r2,r2,#2
  306. sth ip,[r1],#2
  307. .Ldifferentaligned
  308. // qword aligned?
  309. orrs r3,r0,r1
  310. tst r3,#7
  311. bne .Ldwordloop
  312. *)
  313. .Ldwordloop:
  314. sub r2,r2,#4
  315. ldr r3,[r0],#4
  316. cmp r2,#4
  317. str r3,[r1],#4
  318. bcs .Ldwordloop
  319. cmp r2,#0
  320. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  321. moveq pc,lr
  322. {$else}
  323. bxeq lr
  324. {$endif}
  325. .Lbyteloop:
  326. subs r2,r2,#1
  327. ldrb r3,[r0],#1
  328. strb r3,[r1],#1
  329. bne .Lbyteloop
  330. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  331. mov pc,lr
  332. {$else}
  333. bx lr
  334. {$endif}
  335. end;
  336. const
  337. moveproc : pointer = @move_blended;
  338. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  339. asm
  340. ldr ip,.Lmoveproc
  341. ldr pc,[ip]
  342. .Lmoveproc:
  343. .long moveproc
  344. end;
  345. {$endif FPC_SYSTEM_HAS_MOVE}
  346. {****************************************************************************
  347. String
  348. ****************************************************************************}
  349. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  350. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  351. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  352. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring):shortstring;assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  353. {$else}
  354. procedure fpc_shortstr_to_shortstr(out res:shortstring;const sstr:shortstring);assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  355. {$endif}
  356. {r0: __RESULT
  357. r1: len
  358. r2: sstr}
  359. asm
  360. ldrb r12,[r2],#1
  361. cmp r12,r1
  362. movgt r12,r1
  363. strb r12,[r0],#1
  364. cmp r12,#6 (* 6 seems to be the break even point. *)
  365. blt .LStartTailCopy
  366. (* Align destination on 32bits. This is the only place where unrolling
  367. really seems to help, since in the common case, sstr is aligned on
  368. 32 bits, therefore in the common case we need to copy 3 bytes to
  369. align, i.e. in the case of a loop, you wouldn't branch out early.*)
  370. rsb r3,r0,#0
  371. ands r3,r3,#3
  372. sub r12,r12,r3
  373. ldrneb r1,[r2],#1
  374. strneb r1,[r0],#1
  375. subnes r3,r3,#1
  376. ldrneb r1,[r2],#1
  377. strneb r1,[r0],#1
  378. subnes r3,r3,#1
  379. ldrneb r1,[r2],#1
  380. strneb r1,[r0],#1
  381. subnes r3,r3,#1
  382. .LDoneAlign:
  383. (* Destination should be aligned now, but source might not be aligned,
  384. if this is the case, do a byte-per-byte copy. *)
  385. tst r2,#3
  386. bne .LStartTailCopy
  387. (* Start the main copy, 32 bit at a time. *)
  388. movs r3,r12,lsr #2
  389. and r12,r12,#3
  390. beq .LStartTailCopy
  391. .LNext4bytes:
  392. (* Unrolling this loop would save a little bit of time for long strings
  393. (>20 chars), but alas, it hurts for short strings and they are the
  394. common case.*)
  395. ldrne r1,[r2],#4
  396. strne r1,[r0],#4
  397. subnes r3,r3,#1
  398. bne .LNext4bytes
  399. .LStartTailCopy:
  400. (* Do remaining bytes. *)
  401. cmp r12,#0
  402. beq .LDoneTail
  403. .LNextChar3:
  404. ldrb r1,[r2],#1
  405. strb r1,[r0],#1
  406. subs r12,r12,#1
  407. bne .LNextChar3
  408. .LDoneTail:
  409. end;
  410. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);assembler;nostackframe;[public,alias:'FPC_SHORTSTR_ASSIGN'];compilerproc;
  411. {r0: len
  412. r1: sstr
  413. r2: dstr}
  414. asm
  415. ldrb r12,[r1],#1
  416. cmp r12,r0
  417. movgt r12,r0
  418. strb r12,[r2],#1
  419. cmp r12,#6 (* 6 seems to be the break even point. *)
  420. blt .LStartTailCopy
  421. (* Align destination on 32bits. This is the only place where unrolling
  422. really seems to help, since in the common case, sstr is aligned on
  423. 32 bits, therefore in the common case we need to copy 3 bytes to
  424. align, i.e. in the case of a loop, you wouldn't branch out early.*)
  425. rsb r3,r2,#0
  426. ands r3,r3,#3
  427. sub r12,r12,r3
  428. ldrneb r0,[r1],#1
  429. strneb r0,[r2],#1
  430. subnes r3,r3,#1
  431. ldrneb r0,[r1],#1
  432. strneb r0,[r2],#1
  433. subnes r3,r3,#1
  434. ldrneb r0,[r1],#1
  435. strneb r0,[r2],#1
  436. subnes r3,r3,#1
  437. .LDoneAlign:
  438. (* Destination should be aligned now, but source might not be aligned,
  439. if this is the case, do a byte-per-byte copy. *)
  440. tst r1,#3
  441. bne .LStartTailCopy
  442. (* Start the main copy, 32 bit at a time. *)
  443. movs r3,r12,lsr #2
  444. and r12,r12,#3
  445. beq .LStartTailCopy
  446. .LNext4bytes:
  447. (* Unrolling this loop would save a little bit of time for long strings
  448. (>20 chars), but alas, it hurts for short strings and they are the
  449. common case.*)
  450. ldrne r0,[r1],#4
  451. strne r0,[r2],#4
  452. subnes r3,r3,#1
  453. bne .LNext4bytes
  454. .LStartTailCopy:
  455. (* Do remaining bytes. *)
  456. cmp r12,#0
  457. beq .LDoneTail
  458. .LNextChar3:
  459. ldrb r0,[r1],#1
  460. strb r0,[r2],#1
  461. subs r12,r12,#1
  462. bne .LNextChar3
  463. .LDoneTail:
  464. end;
  465. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  466. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  467. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  468. function fpc_Pchar_length(p:Pchar):longint;assembler;nostackframe;[public,alias:'FPC_PCHAR_LENGTH'];compilerproc;
  469. asm
  470. cmp r0,#0
  471. mov r1,r0
  472. beq .Ldone
  473. .Lnextchar:
  474. (*Are we aligned?*)
  475. tst r1,#3
  476. bne .Ltest_unaligned (*No, do byte per byte.*)
  477. ldr r3,.L01010101
  478. .Ltest_aligned:
  479. (*Aligned, load 4 bytes at a time.*)
  480. ldr r12,[r1],#4
  481. (*Check wether r12 contains a 0 byte.*)
  482. sub r2,r12,r3
  483. mvn r12,r12
  484. and r2,r2,r12
  485. ands r2,r2,r3,lsl #7 (*r3 lsl 7 = $80808080*)
  486. beq .Ltest_aligned (*No 0 byte, repeat.*)
  487. sub r1,r1,#4
  488. .Ltest_unaligned:
  489. ldrb r12,[r1],#1
  490. cmp r12,#1 (*r12<1 same as r12=0, but result in carry flag*)
  491. bcs .Lnextchar
  492. (*Dirty trick: we need to subtract 1 extra because we have counted the
  493. terminating 0, due to the known carry flag sbc can do this.*)
  494. sbc r0,r1,r0
  495. .Ldone:
  496. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5)}
  497. mov pc,lr
  498. {$else}
  499. bx lr
  500. {$endif}
  501. .L01010101:
  502. .long 0x01010101
  503. end;
  504. {$endif}
  505. var
  506. fpc_system_lock: longint; export name 'fpc_system_lock';
  507. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  508. asm
  509. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  510. .Lloop:
  511. ldrex r1, [r0]
  512. sub r1, r1, #1
  513. strex r2, r1, [r0]
  514. cmp r2, #0
  515. bne .Lloop
  516. mov r0, r1
  517. bx lr
  518. {$else}
  519. // lock
  520. ldr r3, .Lfpc_system_lock
  521. mov r1, #1
  522. .Lloop:
  523. swp r2, r1, [r3]
  524. cmp r2, #0
  525. bne .Lloop
  526. // do the job
  527. ldr r1, [r0]
  528. sub r1, r1, #1
  529. str r1, [r0]
  530. mov r0, r1
  531. // unlock and return
  532. str r2, [r3]
  533. bx lr
  534. .Lfpc_system_lock:
  535. .long fpc_system_lock
  536. {$endif}
  537. end;
  538. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  539. asm
  540. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  541. .Lloop:
  542. ldrex r1, [r0]
  543. add r1, r1, #1
  544. strex r2, r1, [r0]
  545. cmp r2, #0
  546. bne .Lloop
  547. mov r0, r1
  548. bx lr
  549. {$else}
  550. // lock
  551. ldr r3, .Lfpc_system_lock
  552. mov r1, #1
  553. .Lloop:
  554. swp r2, r1, [r3]
  555. cmp r2, #0
  556. bne .Lloop
  557. // do the job
  558. ldr r1, [r0]
  559. add r1, r1, #1
  560. str r1, [r0]
  561. mov r0, r1
  562. // unlock and return
  563. str r2, [r3]
  564. bx lr
  565. .Lfpc_system_lock:
  566. .long fpc_system_lock
  567. {$endif}
  568. end;
  569. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  570. asm
  571. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  572. // swp is deprecated on ARMv6 and above
  573. .Lloop:
  574. ldrex r2, [r0]
  575. strex r3, r1, [r0]
  576. cmp r3, #0
  577. bne .Lloop
  578. mov r0, r2
  579. bx lr
  580. {$else}
  581. swp r1, r1, [r0]
  582. mov r0,r1
  583. {$endif}
  584. end;
  585. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  586. asm
  587. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  588. .Lloop:
  589. ldrex r2, [r0]
  590. add r12, r1, r2
  591. strex r3, r12, [r0]
  592. cmp r3, #0
  593. bne .Lloop
  594. mov r0, r2
  595. bx lr
  596. {$else}
  597. // lock
  598. ldr r3, .Lfpc_system_lock
  599. mov r2, #1
  600. .Lloop:
  601. swp r2, r2, [r3]
  602. cmp r2, #0
  603. bne .Lloop
  604. // do the job
  605. ldr r2, [r0]
  606. add r1, r1, r2
  607. str r1, [r0]
  608. mov r0, r2
  609. // unlock and return
  610. mov r2, #0
  611. str r2, [r3]
  612. bx lr
  613. .Lfpc_system_lock:
  614. .long fpc_system_lock
  615. {$endif}
  616. end;
  617. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  618. asm
  619. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  620. .Lloop:
  621. ldrex r3, [r0]
  622. mov r12, #0
  623. cmp r3, r2
  624. strexeq r12, r1, [r0]
  625. cmp r12, #0
  626. bne .Lloop
  627. mov r0, r3
  628. bx lr
  629. {$else}
  630. // lock
  631. ldr r12, .Lfpc_system_lock
  632. mov r3, #1
  633. .Lloop:
  634. swp r3, r3, [r12]
  635. cmp r3, #0
  636. bne .Lloop
  637. // do the job
  638. ldr r3, [r0]
  639. cmp r3, r2
  640. streq r1, [r0]
  641. mov r0, r3
  642. // unlock and return
  643. mov r3, #0
  644. str r3, [r12]
  645. bx lr
  646. .Lfpc_system_lock:
  647. .long fpc_system_lock
  648. {$endif}
  649. end;
  650. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  651. function declocked(var l: longint) : boolean; inline;
  652. begin
  653. Result:=InterLockedDecrement(l) = 0;
  654. end;
  655. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  656. procedure inclocked(var l: longint); inline;
  657. begin
  658. InterLockedIncrement(l);
  659. end;
  660. procedure fpc_cpucodeinit;
  661. begin
  662. {$ifdef FPC_SYSTEM_FPC_MOVE}
  663. cpu_has_edsp:=true;
  664. in_edsp_test:=true;
  665. asm
  666. bic r0,sp,#7
  667. ldrd r0,[r0]
  668. end;
  669. in_edsp_test:=false;
  670. if cpu_has_edsp then
  671. moveproc:=@move_pld
  672. else
  673. moveproc:=@move_blended;
  674. {$endif FPC_SYSTEM_FPC_MOVE}
  675. end;
  676. {include hand-optimized assembler division code}
  677. {$i divide.inc}