arm.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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) and not defined(FPUVFPV3_D16)}
  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. {$ifndef darwin}
  88. mov r0,r11
  89. {$else}
  90. mov r0,r7
  91. {$endif}
  92. end;
  93. {$ENDIF not INTERNAL_BACKTRACE}
  94. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  95. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler;nostackframe;
  96. asm
  97. cmp r0,#0
  98. {$ifndef darwin}
  99. ldrne r0,[r0,#-4]
  100. {$else}
  101. ldrne r0,[r0,#4]
  102. {$endif}
  103. end;
  104. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  105. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler;nostackframe;
  106. asm
  107. cmp r0,#0
  108. {$ifndef darwin}
  109. ldrne r0,[r0,#-12]
  110. {$else}
  111. ldrne r0,[r0]
  112. {$endif}
  113. end;
  114. {$define FPC_SYSTEM_HAS_SPTR}
  115. Function Sptr : pointer;assembler;nostackframe;
  116. asm
  117. mov r0,sp
  118. end;
  119. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  120. {$define FPC_SYSTEM_HAS_FILLCHAR}
  121. Procedure FillChar(var x;count:longint;value:byte);assembler;nostackframe;
  122. asm
  123. // less than 0?
  124. cmp r1,#0
  125. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  126. movle pc,lr
  127. {$else}
  128. bxle lr
  129. {$endif}
  130. mov r3,r0
  131. orr r2,r2,r2,lsl #8
  132. orr r2,r2,r2,lsl #16
  133. tst r3, #3 // Aligned?
  134. bne .LFillchar_do_align
  135. .LFillchar_is_aligned:
  136. subs r1,r1,#8
  137. bmi .LFillchar_less_than_8bytes
  138. mov ip,r2
  139. .LFillchar_at_least_8bytes:
  140. // Do 16 bytes per loop
  141. // More unrolling is uncessary, as we'll just stall on the write buffers
  142. stmia r3!,{r2,ip}
  143. subs r1,r1,#8
  144. stmplia r3!,{r2,ip}
  145. subpls r1,r1,#8
  146. bpl .LFillchar_at_least_8bytes
  147. .LFillchar_less_than_8bytes:
  148. // Do the rest
  149. adds r1, r1, #8
  150. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  151. moveq pc,lr
  152. {$else}
  153. bxeq lr
  154. {$endif}
  155. tst r1, #4
  156. strne r2,[r3],#4
  157. tst r1, #2
  158. strneh r2,[r3],#2
  159. tst r1, #1
  160. strneb r2,[r3],#1
  161. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  162. mov pc,lr
  163. {$else}
  164. bx lr
  165. {$endif}
  166. // Special case for unaligned start
  167. // We make a maximum of 3 loops here
  168. .LFillchar_do_align:
  169. strb r2,[r3],#1
  170. subs r1, r1, #1
  171. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  172. moveq pc,lr
  173. {$else}
  174. bxeq lr
  175. {$endif}
  176. tst r3,#3
  177. bne .LFillchar_do_align
  178. b .LFillchar_is_aligned
  179. end;
  180. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  181. {$ifndef FPC_SYSTEM_HAS_MOVE}
  182. {$define FPC_SYSTEM_HAS_MOVE}
  183. procedure Move_pld(const source;var dest;count:longint);assembler;nostackframe;
  184. asm
  185. pld [r0]
  186. // count <=0 ?
  187. cmp r2,#0
  188. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  189. movle pc,lr
  190. {$else}
  191. bxle lr
  192. {$endif}
  193. // overlap?
  194. subs r3, r1, r0 // if (dest > source) and
  195. cmphi r2, r3 // (count > dest - src) then
  196. bhi .Loverlapped // DoReverseByteCopy;
  197. cmp r2,#8 // if (count < 8) then
  198. blt .Lbyteloop // DoForwardByteCopy;
  199. // Any way to avoid the above jump and fuse the next two instructions?
  200. tst r0, #3 // if (source and 3) <> 0 or
  201. tsteq r1, #3 // (dest and 3) <> 0 then
  202. bne .Lbyteloop // DoForwardByteCopy;
  203. pld [r0,#32]
  204. .Ldwordloop:
  205. ldmia r0!, {r3, ip}
  206. // preload
  207. pld [r0,#64]
  208. sub r2,r2,#8
  209. cmp r2, #8
  210. stmia r1!, {r3, ip}
  211. bge .Ldwordloop
  212. cmp r2,#0
  213. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  214. moveq pc,lr
  215. {$else}
  216. bxeq lr
  217. {$endif}
  218. .Lbyteloop:
  219. subs r2,r2,#1
  220. ldrb r3,[r0],#1
  221. strb r3,[r1],#1
  222. bne .Lbyteloop
  223. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  224. mov pc,lr
  225. {$else}
  226. bx lr
  227. {$endif}
  228. .Loverlapped:
  229. subs r2,r2,#1
  230. ldrb r3,[r0,r2]
  231. strb r3,[r1,r2]
  232. bne .Loverlapped
  233. end;
  234. procedure Move_blended(const source;var dest;count:longint);assembler;nostackframe;
  235. asm
  236. // count <=0 ?
  237. cmp r2,#0
  238. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  239. movle pc,lr
  240. {$else}
  241. bxle lr
  242. {$endif}
  243. // overlap?
  244. subs r3, r1, r0 // if (dest > source) and
  245. cmphi r2, r3 // (count > dest - src) then
  246. bhi .Loverlapped // DoReverseByteCopy;
  247. cmp r2,#8 // if (count < 8) then
  248. blt .Lbyteloop // DoForwardByteCopy;
  249. // Any way to avoid the above jump and fuse the next two instructions?
  250. tst r0, #3 // if (source and 3) <> 0 or
  251. tsteq r1, #3 // (dest and 3) <> 0 then
  252. bne .Lbyteloop // DoForwardByteCopy;
  253. .Ldwordloop:
  254. ldmia r0!, {r3, ip}
  255. sub r2,r2,#8
  256. cmp r2, #8
  257. stmia r1!, {r3, ip}
  258. bge .Ldwordloop
  259. cmp r2,#0
  260. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  261. moveq pc,lr
  262. {$else}
  263. bxeq lr
  264. {$endif}
  265. .Lbyteloop:
  266. subs r2,r2,#1
  267. ldrb r3,[r0],#1
  268. strb r3,[r1],#1
  269. bne .Lbyteloop
  270. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  271. mov pc,lr
  272. {$else}
  273. bx lr
  274. {$endif}
  275. .Loverlapped:
  276. subs r2,r2,#1
  277. ldrb r3,[r0,r2]
  278. strb r3,[r1,r2]
  279. bne .Loverlapped
  280. end;
  281. const
  282. moveproc : pointer = @move_blended;
  283. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  284. asm
  285. ldr ip,.Lmoveproc
  286. ldr pc,[ip]
  287. .Lmoveproc:
  288. .long moveproc
  289. end;
  290. {$endif FPC_SYSTEM_HAS_MOVE}
  291. {****************************************************************************
  292. String
  293. ****************************************************************************}
  294. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  295. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  296. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  297. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring):shortstring;assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  298. {$else}
  299. procedure fpc_shortstr_to_shortstr(out res:shortstring;const sstr:shortstring);assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  300. {$endif}
  301. {r0: __RESULT
  302. r1: len
  303. r2: sstr}
  304. asm
  305. ldrb r12,[r2],#1
  306. cmp r12,r1
  307. movgt r12,r1
  308. strb r12,[r0],#1
  309. cmp r12,#6 (* 6 seems to be the break even point. *)
  310. blt .LStartTailCopy
  311. (* Align destination on 32bits. This is the only place where unrolling
  312. really seems to help, since in the common case, sstr is aligned on
  313. 32 bits, therefore in the common case we need to copy 3 bytes to
  314. align, i.e. in the case of a loop, you wouldn't branch out early.*)
  315. rsb r3,r0,#0
  316. ands r3,r3,#3
  317. sub r12,r12,r3
  318. ldrneb r1,[r2],#1
  319. strneb r1,[r0],#1
  320. subnes r3,r3,#1
  321. ldrneb r1,[r2],#1
  322. strneb r1,[r0],#1
  323. subnes r3,r3,#1
  324. ldrneb r1,[r2],#1
  325. strneb r1,[r0],#1
  326. subnes r3,r3,#1
  327. .LDoneAlign:
  328. (* Destination should be aligned now, but source might not be aligned,
  329. if this is the case, do a byte-per-byte copy. *)
  330. tst r2,#3
  331. bne .LStartTailCopy
  332. (* Start the main copy, 32 bit at a time. *)
  333. movs r3,r12,lsr #2
  334. and r12,r12,#3
  335. beq .LStartTailCopy
  336. .LNext4bytes:
  337. (* Unrolling this loop would save a little bit of time for long strings
  338. (>20 chars), but alas, it hurts for short strings and they are the
  339. common case.*)
  340. ldrne r1,[r2],#4
  341. strne r1,[r0],#4
  342. subnes r3,r3,#1
  343. bne .LNext4bytes
  344. .LStartTailCopy:
  345. (* Do remaining bytes. *)
  346. cmp r12,#0
  347. beq .LDoneTail
  348. .LNextChar3:
  349. ldrb r1,[r2],#1
  350. strb r1,[r0],#1
  351. subs r12,r12,#1
  352. bne .LNextChar3
  353. .LDoneTail:
  354. end;
  355. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);assembler;nostackframe;[public,alias:'FPC_SHORTSTR_ASSIGN'];compilerproc;
  356. {r0: len
  357. r1: sstr
  358. r2: dstr}
  359. asm
  360. ldrb r12,[r1],#1
  361. cmp r12,r0
  362. movgt r12,r0
  363. strb r12,[r2],#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,r2,#0
  371. ands r3,r3,#3
  372. sub r12,r12,r3
  373. ldrneb r0,[r1],#1
  374. strneb r0,[r2],#1
  375. subnes r3,r3,#1
  376. ldrneb r0,[r1],#1
  377. strneb r0,[r2],#1
  378. subnes r3,r3,#1
  379. ldrneb r0,[r1],#1
  380. strneb r0,[r2],#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 r1,#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 r0,[r1],#4
  396. strne r0,[r2],#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 r0,[r1],#1
  405. strb r0,[r2],#1
  406. subs r12,r12,#1
  407. bne .LNextChar3
  408. .LDoneTail:
  409. end;
  410. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  411. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  412. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  413. function fpc_Pchar_length(p:Pchar):sizeint;assembler;nostackframe;[public,alias:'FPC_PCHAR_LENGTH'];compilerproc;
  414. asm
  415. cmp r0,#0
  416. mov r1,r0
  417. beq .Ldone
  418. .Lnextchar:
  419. (*Are we aligned?*)
  420. tst r1,#3
  421. bne .Ltest_unaligned (*No, do byte per byte.*)
  422. ldr r3,.L01010101
  423. .Ltest_aligned:
  424. (*Aligned, load 4 bytes at a time.*)
  425. ldr r12,[r1],#4
  426. (*Check wether r12 contains a 0 byte.*)
  427. sub r2,r12,r3
  428. mvn r12,r12
  429. and r2,r2,r12
  430. ands r2,r2,r3,lsl #7 (*r3 lsl 7 = $80808080*)
  431. beq .Ltest_aligned (*No 0 byte, repeat.*)
  432. sub r1,r1,#4
  433. .Ltest_unaligned:
  434. ldrb r12,[r1],#1
  435. cmp r12,#1 (*r12<1 same as r12=0, but result in carry flag*)
  436. bcs .Lnextchar
  437. (*Dirty trick: we need to subtract 1 extra because we have counted the
  438. terminating 0, due to the known carry flag sbc can do this.*)
  439. sbc r0,r1,r0
  440. .Ldone:
  441. {$if defined(cpuarmv3) or defined(cpuarmv4)}
  442. mov pc,lr
  443. {$else}
  444. bx lr
  445. {$endif}
  446. .L01010101:
  447. .long 0x01010101
  448. end;
  449. {$endif}
  450. var
  451. fpc_system_lock: longint; export name 'fpc_system_lock';
  452. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  453. asm
  454. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  455. .Lloop:
  456. ldrex r1, [r0]
  457. sub r1, r1, #1
  458. strex r2, r1, [r0]
  459. cmp r2, #0
  460. bne .Lloop
  461. mov r0, r1
  462. bx lr
  463. {$else}
  464. {$if defined(LINUX) and defined(CPUARMEL)}
  465. stmfd r13!, {lr}
  466. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  467. .Latomic_dec_loop:
  468. ldr r0, [r2] // Load the current value
  469. // We expect this to work without looping most of the time
  470. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  471. // loop here again, we have to reload the value. Normaly this just fills the
  472. // load stall-cycles from the above ldr so in reality we'll not get any additional
  473. // delays because of this
  474. // Don't use ldr to load r3 to avoid cacheline trashing
  475. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  476. // the kuser_cmpxchg entry point
  477. mvn r3, #0x0000f000
  478. sub r3, r3, #0x3F
  479. sub r1, r0, #1 // Decrement value
  480. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  481. movcs r0, r1 // We expect that to work most of the time so keep it pipeline friendly
  482. ldmcsfd r13!, {pc}
  483. b .Latomic_dec_loop // kuser_cmpxchg sets C flag on error
  484. {$else}
  485. // lock
  486. ldr r3, .Lfpc_system_lock
  487. mov r1, #1
  488. .Lloop:
  489. swp r2, r1, [r3]
  490. cmp r2, #0
  491. bne .Lloop
  492. // do the job
  493. ldr r1, [r0]
  494. sub r1, r1, #1
  495. str r1, [r0]
  496. mov r0, r1
  497. // unlock and return
  498. str r2, [r3]
  499. bx lr
  500. .Lfpc_system_lock:
  501. .long fpc_system_lock
  502. {$endif}
  503. {$endif}
  504. end;
  505. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  506. asm
  507. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  508. .Lloop:
  509. ldrex r1, [r0]
  510. add r1, r1, #1
  511. strex r2, r1, [r0]
  512. cmp r2, #0
  513. bne .Lloop
  514. mov r0, r1
  515. bx lr
  516. {$else}
  517. {$if defined(LINUX) and defined(CPUARMEL)}
  518. stmfd r13!, {lr}
  519. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  520. .Latomic_inc_loop:
  521. ldr r0, [r2] // Load the current value
  522. // We expect this to work without looping most of the time
  523. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  524. // loop here again, we have to reload the value. Normaly this just fills the
  525. // load stall-cycles from the above ldr so in reality we'll not get any additional
  526. // delays because of this
  527. // Don't use ldr to load r3 to avoid cacheline trashing
  528. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  529. // the kuser_cmpxchg entry point
  530. mvn r3, #0x0000f000
  531. sub r3, r3, #0x3F
  532. add r1, r0, #1 // Increment value
  533. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  534. movcs r0, r1 // We expect that to work most of the time so keep it pipeline friendly
  535. ldmcsfd r13!, {pc}
  536. b .Latomic_inc_loop // kuser_cmpxchg sets C flag on error
  537. {$else}
  538. // lock
  539. ldr r3, .Lfpc_system_lock
  540. mov r1, #1
  541. .Lloop:
  542. swp r2, r1, [r3]
  543. cmp r2, #0
  544. bne .Lloop
  545. // do the job
  546. ldr r1, [r0]
  547. add r1, r1, #1
  548. str r1, [r0]
  549. mov r0, r1
  550. // unlock and return
  551. str r2, [r3]
  552. bx lr
  553. .Lfpc_system_lock:
  554. .long fpc_system_lock
  555. {$endif}
  556. {$endif}
  557. end;
  558. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  559. asm
  560. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  561. // swp is deprecated on ARMv6 and above
  562. .Lloop:
  563. ldrex r2, [r0]
  564. strex r3, r1, [r0]
  565. cmp r3, #0
  566. bne .Lloop
  567. mov r0, r2
  568. bx lr
  569. {$else}
  570. swp r1, r1, [r0]
  571. mov r0,r1
  572. {$endif}
  573. end;
  574. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  575. asm
  576. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  577. .Lloop:
  578. ldrex r2, [r0]
  579. add r12, r1, r2
  580. strex r3, r12, [r0]
  581. cmp r3, #0
  582. bne .Lloop
  583. mov r0, r2
  584. bx lr
  585. {$else}
  586. {$if defined(LINUX) and defined(CPUARMEL)}
  587. stmfd r13!, {r4, lr}
  588. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  589. mov r4, r1 // Save addend
  590. .Latomic_add_loop:
  591. ldr r0, [r2] // Load the current value
  592. // We expect this to work without looping most of the time
  593. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  594. // loop here again, we have to reload the value. Normaly this just fills the
  595. // load stall-cycles from the above ldr so in reality we'll not get any additional
  596. // delays because of this
  597. // Don't use ldr to load r3 to avoid cacheline trashing
  598. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  599. // the kuser_cmpxchg entry point
  600. mvn r3, #0x0000f000
  601. sub r3, r3, #0x3F
  602. add r1, r0, r4 // Add to value
  603. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  604. // r1 does not get clobbered, so just get back the original value
  605. // Otherwise we would have to allocate one more register and store the
  606. // temporary value
  607. subcs r0, r1, r4
  608. ldmcsfd r13!, {r4, pc}
  609. b .Latomic_add_loop // kuser_cmpxchg failed, loop back
  610. {$else}
  611. // lock
  612. ldr r3, .Lfpc_system_lock
  613. mov r2, #1
  614. .Lloop:
  615. swp r2, r2, [r3]
  616. cmp r2, #0
  617. bne .Lloop
  618. // do the job
  619. ldr r2, [r0]
  620. add r1, r1, r2
  621. str r1, [r0]
  622. mov r0, r2
  623. // unlock and return
  624. mov r2, #0
  625. str r2, [r3]
  626. bx lr
  627. .Lfpc_system_lock:
  628. .long fpc_system_lock
  629. {$endif}
  630. {$endif}
  631. end;
  632. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  633. asm
  634. {$if defined(cpuarmv6) or defined(cpuarmv7m) or defined(cpucortexm3)}
  635. .Lloop:
  636. ldrex r3, [r0]
  637. mov r12, #0
  638. cmp r3, r2
  639. strexeq r12, r1, [r0]
  640. cmp r12, #0
  641. bne .Lloop
  642. mov r0, r3
  643. bx lr
  644. {$else}
  645. {$if defined(LINUX) and defined(CPUARMEL)}
  646. stmfd r13!, {r4, lr}
  647. mvn r3, #0x0000f000
  648. sub r3, r3, #0x3F
  649. mov r4, r2 // Swap parameters around
  650. mov r2, r0
  651. mov r0, r4 // Use r4 because we'll need the new value for later
  652. // r1 and r2 will not be clobbered by kuser_cmpxchg
  653. // If we have to loop, r0 will be set to the original Comperand
  654. .Linterlocked_compare_exchange_loop:
  655. blx r3 // Call kuser_cmpxchg sets C-Flag on success
  656. movcs r0, r4 // Return the previous value on success
  657. ldmcsfd r13!, {r4, pc}
  658. // The error case is a bit tricky, kuser_cmpxchg does not return the current value
  659. // So we may need to loop to avoid race conditions
  660. // The loop case is HIGHLY unlikely, it would require that we got rescheduled between
  661. // calling kuser_cmpxchg and the ldr. While beeing rescheduled another process/thread
  662. // would have the set the value to our comperand
  663. ldr r0, [r2] // Load the currently set value
  664. cmp r0, r4 // Return if Comperand != current value, otherwise loop again
  665. ldmnefd r13!, {r4, pc}
  666. // If we need to loop here, we have to
  667. b .Linterlocked_compare_exchange_loop
  668. {$else}
  669. // lock
  670. ldr r12, .Lfpc_system_lock
  671. mov r3, #1
  672. .Lloop:
  673. swp r3, r3, [r12]
  674. cmp r3, #0
  675. bne .Lloop
  676. // do the job
  677. ldr r3, [r0]
  678. cmp r3, r2
  679. streq r1, [r0]
  680. mov r0, r3
  681. // unlock and return
  682. mov r3, #0
  683. str r3, [r12]
  684. bx lr
  685. .Lfpc_system_lock:
  686. .long fpc_system_lock
  687. {$endif}
  688. {$endif}
  689. end;
  690. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  691. function declocked(var l: longint) : boolean; inline;
  692. begin
  693. Result:=InterLockedDecrement(l) = 0;
  694. end;
  695. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  696. procedure inclocked(var l: longint); inline;
  697. begin
  698. InterLockedIncrement(l);
  699. end;
  700. procedure fpc_cpucodeinit;
  701. begin
  702. {$ifdef FPC_SYSTEM_FPC_MOVE}
  703. cpu_has_edsp:=true;
  704. in_edsp_test:=true;
  705. asm
  706. bic r0,sp,#7
  707. ldrd r0,[r0]
  708. end;
  709. in_edsp_test:=false;
  710. if cpu_has_edsp then
  711. moveproc:=@move_pld
  712. else
  713. moveproc:=@move_blended;
  714. {$endif FPC_SYSTEM_FPC_MOVE}
  715. end;
  716. {$define FPC_SYSTEM_HAS_SWAPENDIAN}
  717. { SwapEndian(<16 Bit>) being inlined is faster than using assembler }
  718. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  719. begin
  720. { the extra Word type cast is necessary because the "AValue shr 8" }
  721. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  722. { the sign bits from the upper 16 bits are shifted in rather than }
  723. { zeroes. }
  724. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  725. end;
  726. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  727. begin
  728. Result := Word((AValue shr 8) or (AValue shl 8));
  729. end;
  730. (*
  731. This is kept for reference. Thats what the compiler COULD generate in these cases.
  732. But FPC currently does not support inlining of asm-functions, so the whole call-overhead
  733. is bigger than the gain of the optimized function.
  734. function AsmSwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif};assembler;nostackframe;
  735. asm
  736. // We're starting with 4321
  737. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5) or defined(VER2_6)}
  738. mov r0, r0, shl #16 // Shift to make that 2100
  739. mov r0, r0, ror #24 // Rotate to 1002
  740. orr r0, r0, r0 shr #16 // Shift and combine into 0012
  741. {$else}
  742. rev r0, r0 // Reverse byteorder r0 = 1234
  743. mov r0, r0, shr #16 // Shift down to 16bits r0 = 0012
  744. {$endif}
  745. end;
  746. *)
  747. {
  748. These used to be an assembler-function, but with newer improvements to the compiler this
  749. generates a perfect 4 cycle code sequence and can be inlined.
  750. }
  751. function SwapEndian(const AValue: LongWord): LongWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  752. begin
  753. Result:= AValue xor rordword(AValue,16);
  754. Result:= Result and $FF00FFFF;
  755. Result:= (Result shr 8) xor rordword(AValue,8);
  756. end;
  757. function SwapEndian(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  758. begin
  759. Result:=LongInt(SwapEndian(DWord(AValue)));
  760. end;
  761. {
  762. Currently freepascal will not generate a good assembler sequence for
  763. Result:=(SwapEndian(longword(lo(AValue))) shl 32) or
  764. (SwapEndian(longword(hi(AValue))));
  765. So we keep an assembly version for now
  766. }
  767. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  768. asm
  769. {$if defined(cpuarmv3) or defined(cpuarmv4) or defined(cpuarmv5) or defined(VER2_6)}
  770. mov ip, r1
  771. // We're starting with r0 = $87654321
  772. eor r1, r0, r0, ror #16 // r1 = $C444C444
  773. bic r1, r1, #16711680 // r1 = r1 and $ff00ffff = $C400C444
  774. mov r0, r0, ror #8 // r0 = $21876543
  775. eor r1, r0, r1, lsr #8 // r1 = $21436587
  776. eor r0, ip, ip, ror #16
  777. bic r0, r0, #16711680
  778. mov ip, ip, ror #8
  779. eor r0, ip, r0, lsr #8
  780. {$else}
  781. rev r2, r0
  782. rev r0, r1
  783. mov r1, r2
  784. {$endif}
  785. end;
  786. function SwapEndian(const AValue: QWord): QWord; {$ifdef SYSTEMINLINE}inline;{$endif}
  787. begin
  788. Result:=QWord(SwapEndian(Int64(AValue)));
  789. end;
  790. {include hand-optimized assembler division code}
  791. {$i divide.inc}