arm.inc 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. end;
  70. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  71. Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  72. begin
  73. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  74. { FPU precision 64 bit, rounding to nearest, affine infinity }
  75. _controlfp($000C0003, $030F031F);
  76. end;
  77. {$endif wince}
  78. {****************************************************************************
  79. stack frame related stuff
  80. ****************************************************************************}
  81. {$IFNDEF INTERNAL_BACKTRACE}
  82. {$define FPC_SYSTEM_HAS_GET_FRAME}
  83. function get_frame:pointer;assembler;nostackframe;
  84. asm
  85. {$ifndef darwin}
  86. mov r0,r11
  87. {$else}
  88. mov r0,r7
  89. {$endif}
  90. end;
  91. {$ENDIF not INTERNAL_BACKTRACE}
  92. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  93. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler;nostackframe;
  94. asm
  95. cmp r0,#0
  96. {$ifndef darwin}
  97. ldrne r0,[r0,#-4]
  98. {$else}
  99. ldrne r0,[r0,#4]
  100. {$endif}
  101. end;
  102. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  103. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler;nostackframe;
  104. asm
  105. cmp r0,#0
  106. {$ifndef darwin}
  107. ldrne r0,[r0,#-12]
  108. {$else}
  109. ldrne r0,[r0]
  110. {$endif}
  111. end;
  112. {$define FPC_SYSTEM_HAS_SPTR}
  113. Function Sptr : pointer;assembler;nostackframe;
  114. asm
  115. mov r0,sp
  116. end;
  117. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  118. {$define FPC_SYSTEM_HAS_FILLCHAR}
  119. Procedure FillChar(var x;count:longint;value:byte);assembler;nostackframe;
  120. asm
  121. // less than 0?
  122. cmp r1,#0
  123. {$ifdef CPUARM_HAS_BX}
  124. bxle lr
  125. {$else}
  126. movle pc,lr
  127. {$endif}
  128. mov r3,r0
  129. orr r2,r2,r2,lsl #8
  130. orr r2,r2,r2,lsl #16
  131. tst r3, #3 // Aligned?
  132. bne .LFillchar_do_align
  133. .LFillchar_is_aligned:
  134. subs r1,r1,#8
  135. bmi .LFillchar_less_than_8bytes
  136. mov ip,r2
  137. .LFillchar_at_least_8bytes:
  138. // Do 16 bytes per loop
  139. // More unrolling is uncessary, as we'll just stall on the write buffers
  140. stmia r3!,{r2,ip}
  141. subs r1,r1,#8
  142. stmplia r3!,{r2,ip}
  143. subpls r1,r1,#8
  144. bpl .LFillchar_at_least_8bytes
  145. .LFillchar_less_than_8bytes:
  146. // Do the rest
  147. adds r1, r1, #8
  148. {$ifdef CPUARM_HAS_BX}
  149. bxeq lr
  150. {$else}
  151. moveq pc,lr
  152. {$endif}
  153. tst r1, #4
  154. strne r2,[r3],#4
  155. tst r1, #2
  156. strneh r2,[r3],#2
  157. tst r1, #1
  158. strneb r2,[r3],#1
  159. {$ifdef CPUARM_HAS_BX}
  160. bx lr
  161. {$else}
  162. mov pc,lr
  163. {$endif}
  164. // Special case for unaligned start
  165. // We make a maximum of 3 loops here
  166. .LFillchar_do_align:
  167. strb r2,[r3],#1
  168. subs r1, r1, #1
  169. {$ifdef CPUARM_HAS_BX}
  170. bxeq lr
  171. {$else}
  172. moveq pc,lr
  173. {$endif}
  174. tst r3,#3
  175. bne .LFillchar_do_align
  176. b .LFillchar_is_aligned
  177. end;
  178. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  179. {$ifndef FPC_SYSTEM_HAS_MOVE}
  180. {$define FPC_SYSTEM_HAS_MOVE}
  181. {$ifdef CPUARM_HAS_EDSP}
  182. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  183. {$else CPUARM_HAS_EDSP}
  184. procedure Move_pld(const source;var dest;count:longint);assembler;nostackframe;
  185. {$endif CPUARM_HAS_EDSP}
  186. asm
  187. pld [r0]
  188. // count <=0 ?
  189. cmp r2,#0
  190. {$ifdef CPUARM_HAS_BX}
  191. bxle lr
  192. {$else}
  193. movle pc,lr
  194. {$endif}
  195. // overlap?
  196. subs r3, r1, r0 // if (dest > source) and
  197. cmphi r2, r3 // (count > dest - src) then
  198. bhi .Loverlapped // DoReverseByteCopy;
  199. cmp r2,#8 // if (count < 8) then
  200. blt .Lbyteloop // DoForwardByteCopy;
  201. // Any way to avoid the above jump and fuse the next two instructions?
  202. tst r0, #3 // if (source and 3) <> 0 or
  203. tsteq r1, #3 // (dest and 3) <> 0 then
  204. bne .Lbyteloop // DoForwardByteCopy;
  205. pld [r0,#32]
  206. .Ldwordloop:
  207. ldmia r0!, {r3, ip}
  208. // preload
  209. pld [r0,#64]
  210. sub r2,r2,#8
  211. cmp r2, #8
  212. stmia r1!, {r3, ip}
  213. bge .Ldwordloop
  214. cmp r2,#0
  215. {$ifdef CPUARM_HAS_BX}
  216. bxeq lr
  217. {$else}
  218. moveq pc,lr
  219. {$endif}
  220. .Lbyteloop:
  221. subs r2,r2,#1
  222. ldrb r3,[r0],#1
  223. strb r3,[r1],#1
  224. bne .Lbyteloop
  225. {$ifdef CPUARM_HAS_BX}
  226. bx lr
  227. {$else}
  228. mov pc,lr
  229. {$endif}
  230. .Loverlapped:
  231. subs r2,r2,#1
  232. ldrb r3,[r0,r2]
  233. strb r3,[r1,r2]
  234. bne .Loverlapped
  235. end;
  236. {$ifndef CPUARM_HAS_EDSP}
  237. procedure Move_blended(const source;var dest;count:longint);assembler;nostackframe;
  238. asm
  239. // count <=0 ?
  240. cmp r2,#0
  241. {$ifdef CPUARM_HAS_BX}
  242. bxle lr
  243. {$else}
  244. movle pc,lr
  245. {$endif}
  246. // overlap?
  247. subs r3, r1, r0 // if (dest > source) and
  248. cmphi r2, r3 // (count > dest - src) then
  249. bhi .Loverlapped // DoReverseByteCopy;
  250. cmp r2,#8 // if (count < 8) then
  251. blt .Lbyteloop // DoForwardByteCopy;
  252. // Any way to avoid the above jump and fuse the next two instructions?
  253. tst r0, #3 // if (source and 3) <> 0 or
  254. tsteq r1, #3 // (dest and 3) <> 0 then
  255. bne .Lbyteloop // DoForwardByteCopy;
  256. .Ldwordloop:
  257. ldmia r0!, {r3, ip}
  258. sub r2,r2,#8
  259. cmp r2, #8
  260. stmia r1!, {r3, ip}
  261. bge .Ldwordloop
  262. cmp r2,#0
  263. {$ifdef CPUARM_HAS_BX}
  264. bxeq lr
  265. {$else}
  266. moveq pc,lr
  267. {$endif}
  268. .Lbyteloop:
  269. subs r2,r2,#1
  270. ldrb r3,[r0],#1
  271. strb r3,[r1],#1
  272. bne .Lbyteloop
  273. {$ifdef CPUARM_HAS_BX}
  274. bx lr
  275. {$else}
  276. mov pc,lr
  277. {$endif}
  278. .Loverlapped:
  279. subs r2,r2,#1
  280. ldrb r3,[r0,r2]
  281. strb r3,[r1,r2]
  282. bne .Loverlapped
  283. end;
  284. const
  285. moveproc : pointer = @move_blended;
  286. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  287. asm
  288. ldr ip,.Lmoveproc
  289. ldr pc,[ip]
  290. .Lmoveproc:
  291. .long moveproc
  292. end;
  293. {$endif CPUARM_HAS_EDSP}
  294. {$endif FPC_SYSTEM_HAS_MOVE}
  295. {****************************************************************************
  296. String
  297. ****************************************************************************}
  298. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  299. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  300. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  301. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring):shortstring;assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  302. {$else}
  303. procedure fpc_shortstr_to_shortstr(out res:shortstring;const sstr:shortstring);assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  304. {$endif}
  305. {r0: __RESULT
  306. r1: len
  307. r2: sstr}
  308. asm
  309. ldrb r12,[r2],#1
  310. cmp r12,r1
  311. movgt r12,r1
  312. strb r12,[r0],#1
  313. cmp r12,#6 (* 6 seems to be the break even point. *)
  314. blt .LStartTailCopy
  315. (* Align destination on 32bits. This is the only place where unrolling
  316. really seems to help, since in the common case, sstr is aligned on
  317. 32 bits, therefore in the common case we need to copy 3 bytes to
  318. align, i.e. in the case of a loop, you wouldn't branch out early.*)
  319. rsb r3,r0,#0
  320. ands r3,r3,#3
  321. sub r12,r12,r3
  322. ldrneb r1,[r2],#1
  323. strneb r1,[r0],#1
  324. subnes r3,r3,#1
  325. ldrneb r1,[r2],#1
  326. strneb r1,[r0],#1
  327. subnes r3,r3,#1
  328. ldrneb r1,[r2],#1
  329. strneb r1,[r0],#1
  330. subnes r3,r3,#1
  331. .LDoneAlign:
  332. (* Destination should be aligned now, but source might not be aligned,
  333. if this is the case, do a byte-per-byte copy. *)
  334. tst r2,#3
  335. bne .LStartTailCopy
  336. (* Start the main copy, 32 bit at a time. *)
  337. movs r3,r12,lsr #2
  338. and r12,r12,#3
  339. beq .LStartTailCopy
  340. .LNext4bytes:
  341. (* Unrolling this loop would save a little bit of time for long strings
  342. (>20 chars), but alas, it hurts for short strings and they are the
  343. common case.*)
  344. ldrne r1,[r2],#4
  345. strne r1,[r0],#4
  346. subnes r3,r3,#1
  347. bne .LNext4bytes
  348. .LStartTailCopy:
  349. (* Do remaining bytes. *)
  350. cmp r12,#0
  351. beq .LDoneTail
  352. .LNextChar3:
  353. ldrb r1,[r2],#1
  354. strb r1,[r0],#1
  355. subs r12,r12,#1
  356. bne .LNextChar3
  357. .LDoneTail:
  358. end;
  359. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);assembler;nostackframe;[public,alias:'FPC_SHORTSTR_ASSIGN'];compilerproc;
  360. {r0: len
  361. r1: sstr
  362. r2: dstr}
  363. asm
  364. ldrb r12,[r1],#1
  365. cmp r12,r0
  366. movgt r12,r0
  367. strb r12,[r2],#1
  368. cmp r12,#6 (* 6 seems to be the break even point. *)
  369. blt .LStartTailCopy
  370. (* Align destination on 32bits. This is the only place where unrolling
  371. really seems to help, since in the common case, sstr is aligned on
  372. 32 bits, therefore in the common case we need to copy 3 bytes to
  373. align, i.e. in the case of a loop, you wouldn't branch out early.*)
  374. rsb r3,r2,#0
  375. ands r3,r3,#3
  376. sub r12,r12,r3
  377. ldrneb r0,[r1],#1
  378. strneb r0,[r2],#1
  379. subnes r3,r3,#1
  380. ldrneb r0,[r1],#1
  381. strneb r0,[r2],#1
  382. subnes r3,r3,#1
  383. ldrneb r0,[r1],#1
  384. strneb r0,[r2],#1
  385. subnes r3,r3,#1
  386. .LDoneAlign:
  387. (* Destination should be aligned now, but source might not be aligned,
  388. if this is the case, do a byte-per-byte copy. *)
  389. tst r1,#3
  390. bne .LStartTailCopy
  391. (* Start the main copy, 32 bit at a time. *)
  392. movs r3,r12,lsr #2
  393. and r12,r12,#3
  394. beq .LStartTailCopy
  395. .LNext4bytes:
  396. (* Unrolling this loop would save a little bit of time for long strings
  397. (>20 chars), but alas, it hurts for short strings and they are the
  398. common case.*)
  399. ldrne r0,[r1],#4
  400. strne r0,[r2],#4
  401. subnes r3,r3,#1
  402. bne .LNext4bytes
  403. .LStartTailCopy:
  404. (* Do remaining bytes. *)
  405. cmp r12,#0
  406. beq .LDoneTail
  407. .LNextChar3:
  408. ldrb r0,[r1],#1
  409. strb r0,[r2],#1
  410. subs r12,r12,#1
  411. bne .LNextChar3
  412. .LDoneTail:
  413. end;
  414. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  415. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  416. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  417. function fpc_Pchar_length(p:Pchar):sizeint;assembler;nostackframe;[public,alias:'FPC_PCHAR_LENGTH'];compilerproc;
  418. asm
  419. cmp r0,#0
  420. mov r1,r0
  421. beq .Ldone
  422. .Lnextchar:
  423. (*Are we aligned?*)
  424. tst r1,#3
  425. bne .Ltest_unaligned (*No, do byte per byte.*)
  426. ldr r3,.L01010101
  427. .Ltest_aligned:
  428. (*Aligned, load 4 bytes at a time.*)
  429. ldr r12,[r1],#4
  430. (*Check wether r12 contains a 0 byte.*)
  431. sub r2,r12,r3
  432. mvn r12,r12
  433. and r2,r2,r12
  434. ands r2,r2,r3,lsl #7 (*r3 lsl 7 = $80808080*)
  435. beq .Ltest_aligned (*No 0 byte, repeat.*)
  436. sub r1,r1,#4
  437. .Ltest_unaligned:
  438. ldrb r12,[r1],#1
  439. cmp r12,#1 (*r12<1 same as r12=0, but result in carry flag*)
  440. bcs .Lnextchar
  441. (*Dirty trick: we need to subtract 1 extra because we have counted the
  442. terminating 0, due to the known carry flag sbc can do this.*)
  443. sbc r0,r1,r0
  444. .Ldone:
  445. {$ifdef CPUARM_HAS_BX}
  446. bx lr
  447. {$else}
  448. mov pc,lr
  449. {$endif}
  450. .L01010101:
  451. .long 0x01010101
  452. end;
  453. {$endif}
  454. {$ifndef darwin}
  455. {$define FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  456. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [Public,Alias:'FPC_ANSISTR_DECR_REF'];assembler;nostackframe; compilerproc;
  457. asm
  458. ldr r1, [r0]
  459. // On return the pointer will always be set to zero, so utilize the delay slots
  460. mov r2, #0
  461. str r2, [r0]
  462. // Check for a zero string
  463. cmp r1, #0
  464. // Load reference counter
  465. ldrne r2, [r1, #-8]
  466. {$ifdef CPUARM_HAS_BX}
  467. bxeq lr
  468. {$else}
  469. moveq pc,lr
  470. {$endif}
  471. // Check for a constant string
  472. cmp r2, #0
  473. {$ifdef CPUARM_HAS_BX}
  474. bxlt lr
  475. {$else}
  476. movlt pc,lr
  477. {$endif}
  478. stmfd sp!, {r1, lr}
  479. sub r0, r1, #8
  480. {$ifdef CPUARM_HAS_BLX}
  481. blx InterLockedDecrement
  482. {$else}
  483. bl InterLockedDecrement
  484. {$endif}
  485. // InterLockedDecrement is a nice guy and sets the z flag for us
  486. // if the reference count dropped to 0
  487. ldmnefd sp!, {r1, pc}
  488. ldmfd sp!, {r0, lr}
  489. // We currently can not use constant symbols in ARM-Assembly
  490. // but we need to stay backward compatible with 2.6
  491. sub r0, r0, #12
  492. // Jump without a link, so freemem directly returns to our caller
  493. b FPC_FREEMEM
  494. end;
  495. {$endif not darwin}
  496. var
  497. fpc_system_lock: longint; export name 'fpc_system_lock';
  498. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  499. asm
  500. {$ifdef CPUARM_HAS_LDREX}
  501. .Lloop:
  502. ldrex r1, [r0]
  503. sub r1, r1, #1
  504. strex r2, r1, [r0]
  505. cmp r2, #0
  506. bne .Lloop
  507. movs r0, r1
  508. bx lr
  509. {$else}
  510. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  511. stmfd r13!, {lr}
  512. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  513. .Latomic_dec_loop:
  514. ldr r0, [r2] // Load the current value
  515. // We expect this to work without looping most of the time
  516. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  517. // loop here again, we have to reload the value. Normaly this just fills the
  518. // load stall-cycles from the above ldr so in reality we'll not get any additional
  519. // delays because of this
  520. // Don't use ldr to load r3 to avoid cacheline trashing
  521. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  522. // the kuser_cmpxchg entry point
  523. mvn r3, #0x0000f000
  524. sub r3, r3, #0x3F
  525. sub r1, r0, #1 // Decrement value
  526. {$ifdef CPUARM_HAS_BLX}
  527. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  528. {$else}
  529. mov lr, pc
  530. {$ifdef CPUARM_HAS_BX}
  531. bx r3
  532. {$else}
  533. mov pc, r3
  534. {$endif}
  535. {$endif}
  536. // MOVS sets the Z flag when the result reaches zero, this can be used later on
  537. // The C-Flag will not be modified by this because we're not doing any shifting
  538. movcss r0, r1 // We expect that to work most of the time so keep it pipeline friendly
  539. ldmcsfd r13!, {pc}
  540. b .Latomic_dec_loop // kuser_cmpxchg sets C flag on error
  541. {$else}
  542. // lock
  543. ldr r3, .Lfpc_system_lock
  544. mov r1, #1
  545. .Lloop:
  546. swp r2, r1, [r3]
  547. cmp r2, #0
  548. bne .Lloop
  549. // do the job
  550. ldr r1, [r0]
  551. sub r1, r1, #1
  552. str r1, [r0]
  553. movs r0, r1
  554. // unlock and return
  555. str r2, [r3]
  556. {$ifdef CPUARM_HAS_BX}
  557. bx lr
  558. {$else}
  559. mov pc,lr
  560. {$endif}
  561. .Lfpc_system_lock:
  562. .long fpc_system_lock
  563. {$endif}
  564. {$endif}
  565. end;
  566. {$ifndef darwin}
  567. {$define FPC_SYSTEM_HAS_ANSISTR_INCR_REF}
  568. Procedure fpc_ansistr_incr_ref (S : Pointer); [Public,Alias:'FPC_ANSISTR_INCR_REF'];assembler;nostackframe; compilerproc;
  569. asm
  570. // Null string?
  571. cmp r0, #0
  572. // Load reference counter
  573. ldrne r1, [r0, #-8]
  574. // pointer to counter, calculate here for delay slot utilization
  575. subne r0, r0, #8
  576. {$ifdef CPUARM_HAS_BX}
  577. bxeq lr
  578. {$else}
  579. moveq pc,lr
  580. {$endif}
  581. // Check for a constant string
  582. cmp r1, #0
  583. // Tailcall
  584. // Hopefully the linker will place InterLockedIncrement as layed out here
  585. bge InterLockedIncrement
  586. // Freepascal will generate a proper return here, save some cachespace
  587. end;
  588. {$endif not darwin}
  589. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  590. asm
  591. {$ifdef CPUARM_HAS_LDREX}
  592. .Lloop:
  593. ldrex r1, [r0]
  594. add r1, r1, #1
  595. strex r2, r1, [r0]
  596. cmp r2, #0
  597. bne .Lloop
  598. mov r0, r1
  599. bx lr
  600. {$else}
  601. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  602. stmfd r13!, {lr}
  603. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  604. .Latomic_inc_loop:
  605. ldr r0, [r2] // Load the current value
  606. // We expect this to work without looping most of the time
  607. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  608. // loop here again, we have to reload the value. Normaly this just fills the
  609. // load stall-cycles from the above ldr so in reality we'll not get any additional
  610. // delays because of this
  611. // Don't use ldr to load r3 to avoid cacheline trashing
  612. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  613. // the kuser_cmpxchg entry point
  614. mvn r3, #0x0000f000
  615. sub r3, r3, #0x3F
  616. add r1, r0, #1 // Increment value
  617. {$ifdef CPUARM_HAS_BLX}
  618. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  619. {$else}
  620. mov lr, pc
  621. {$ifdef CPUARM_HAS_BX}
  622. bx r3
  623. {$else}
  624. mov pc, r3
  625. {$endif}
  626. {$endif}
  627. movcs r0, r1 // We expect that to work most of the time so keep it pipeline friendly
  628. ldmcsfd r13!, {pc}
  629. b .Latomic_inc_loop // kuser_cmpxchg sets C flag on error
  630. {$else}
  631. // lock
  632. ldr r3, .Lfpc_system_lock
  633. mov r1, #1
  634. .Lloop:
  635. swp r2, r1, [r3]
  636. cmp r2, #0
  637. bne .Lloop
  638. // do the job
  639. ldr r1, [r0]
  640. add r1, r1, #1
  641. str r1, [r0]
  642. mov r0, r1
  643. // unlock and return
  644. str r2, [r3]
  645. {$ifdef CPUARM_HAS_BX}
  646. bx lr
  647. {$else}
  648. mov pc,lr
  649. {$endif}
  650. .Lfpc_system_lock:
  651. .long fpc_system_lock
  652. {$endif}
  653. {$endif}
  654. end;
  655. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  656. asm
  657. {$ifdef CPUARM_HAS_LDREX}
  658. // swp is deprecated on ARMv6 and above
  659. .Lloop:
  660. ldrex r2, [r0]
  661. strex r3, r1, [r0]
  662. cmp r3, #0
  663. bne .Lloop
  664. mov r0, r2
  665. bx lr
  666. {$else}
  667. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  668. stmfd r13!, {r4, lr}
  669. mov r2, r0 // kuser_cmpxchg does not clobber r2 (and r1) by definition
  670. .Latomic_add_loop:
  671. ldr r0, [r2] // Load the current value
  672. // We expect this to work without looping most of the time
  673. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  674. // loop here again, we have to reload the value. Normaly this just fills the
  675. // load stall-cycles from the above ldr so in reality we'll not get any additional
  676. // delays because of this
  677. // Don't use ldr to load r3 to avoid cacheline trashing
  678. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  679. // the kuser_cmpxchg entry point
  680. mvn r3, #0x0000f000
  681. sub r3, r3, #0x3F
  682. mov r4, r0 // save the current value because kuser_cmpxchg clobbers r0
  683. {$ifdef CPUARM_HAS_BLX}
  684. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  685. {$else}
  686. mov lr, pc
  687. {$ifdef CPUARM_HAS_BX}
  688. bx r3
  689. {$else}
  690. mov pc, r3
  691. {$endif}
  692. {$endif}
  693. // restore the original value if needed
  694. movcs r0, r4
  695. ldmcsfd r13!, {r4, pc}
  696. b .Latomic_add_loop // kuser_cmpxchg failed, loop back
  697. {$else}
  698. // lock
  699. ldr r3, .Lfpc_system_lock
  700. mov r2, #1
  701. .Lloop:
  702. swp r2, r2, [r3]
  703. cmp r2, #0
  704. bne .Lloop
  705. // do the job
  706. ldr r2, [r0]
  707. str r1, [r0]
  708. mov r0, r2
  709. // unlock and return
  710. mov r2, #0
  711. str r2, [r3]
  712. {$ifdef CPUARM_HAS_BX}
  713. bx lr
  714. {$else}
  715. mov pc,lr
  716. {$endif}
  717. .Lfpc_system_lock:
  718. .long fpc_system_lock
  719. {$endif}
  720. {$endif}
  721. end;
  722. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  723. asm
  724. {$ifdef CPUARM_HAS_LDREX}
  725. .Lloop:
  726. ldrex r2, [r0]
  727. add r12, r1, r2
  728. strex r3, r12, [r0]
  729. cmp r3, #0
  730. bne .Lloop
  731. mov r0, r2
  732. bx lr
  733. {$else}
  734. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  735. stmfd r13!, {r4, lr}
  736. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  737. mov r4, r1 // Save addend
  738. .Latomic_add_loop:
  739. ldr r0, [r2] // Load the current value
  740. // We expect this to work without looping most of the time
  741. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  742. // loop here again, we have to reload the value. Normaly this just fills the
  743. // load stall-cycles from the above ldr so in reality we'll not get any additional
  744. // delays because of this
  745. // Don't use ldr to load r3 to avoid cacheline trashing
  746. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  747. // the kuser_cmpxchg entry point
  748. mvn r3, #0x0000f000
  749. sub r3, r3, #0x3F
  750. add r1, r0, r4 // Add to value
  751. {$ifdef CPUARM_HAS_BLX}
  752. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  753. {$else}
  754. mov lr, pc
  755. {$ifdef CPUARM_HAS_BX}
  756. bx r3
  757. {$else}
  758. mov pc, r3
  759. {$endif}
  760. {$endif}
  761. // r1 does not get clobbered, so just get back the original value
  762. // Otherwise we would have to allocate one more register and store the
  763. // temporary value
  764. subcs r0, r1, r4
  765. ldmcsfd r13!, {r4, pc}
  766. b .Latomic_add_loop // kuser_cmpxchg failed, loop back
  767. {$else}
  768. // lock
  769. ldr r3, .Lfpc_system_lock
  770. mov r2, #1
  771. .Lloop:
  772. swp r2, r2, [r3]
  773. cmp r2, #0
  774. bne .Lloop
  775. // do the job
  776. ldr r2, [r0]
  777. add r1, r1, r2
  778. str r1, [r0]
  779. mov r0, r2
  780. // unlock and return
  781. mov r2, #0
  782. str r2, [r3]
  783. {$ifdef CPUARM_HAS_BX}
  784. bx lr
  785. {$else}
  786. mov pc,lr
  787. {$endif}
  788. .Lfpc_system_lock:
  789. .long fpc_system_lock
  790. {$endif}
  791. {$endif}
  792. end;
  793. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  794. asm
  795. {$ifdef CPUARM_HAS_LDREX}
  796. .Lloop:
  797. ldrex r3, [r0]
  798. mov r12, #0
  799. cmp r3, r2
  800. strexeq r12, r1, [r0]
  801. cmp r12, #0
  802. bne .Lloop
  803. mov r0, r3
  804. bx lr
  805. {$else}
  806. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  807. stmfd r13!, {r4, lr}
  808. mvn r3, #0x0000f000
  809. sub r3, r3, #0x3F
  810. mov r4, r2 // Swap parameters around
  811. mov r2, r0
  812. mov r0, r4 // Use r4 because we'll need the new value for later
  813. // r1 and r2 will not be clobbered by kuser_cmpxchg
  814. // If we have to loop, r0 will be set to the original Comperand
  815. .Linterlocked_compare_exchange_loop:
  816. {$ifdef CPUARM_HAS_BLX}
  817. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  818. {$else}
  819. mov lr, pc
  820. {$ifdef CPUARM_HAS_BX}
  821. bx r3
  822. {$else}
  823. mov pc, r3
  824. {$endif}
  825. {$endif}
  826. movcs r0, r4 // Return the previous value on success
  827. ldmcsfd r13!, {r4, pc}
  828. // The error case is a bit tricky, kuser_cmpxchg does not return the current value
  829. // So we may need to loop to avoid race conditions
  830. // The loop case is HIGHLY unlikely, it would require that we got rescheduled between
  831. // calling kuser_cmpxchg and the ldr. While beeing rescheduled another process/thread
  832. // would have the set the value to our comperand
  833. ldr r0, [r2] // Load the currently set value
  834. cmp r0, r4 // Return if Comperand != current value, otherwise loop again
  835. ldmnefd r13!, {r4, pc}
  836. // If we need to loop here, we have to
  837. b .Linterlocked_compare_exchange_loop
  838. {$else}
  839. // lock
  840. ldr r12, .Lfpc_system_lock
  841. mov r3, #1
  842. .Lloop:
  843. swp r3, r3, [r12]
  844. cmp r3, #0
  845. bne .Lloop
  846. // do the job
  847. ldr r3, [r0]
  848. cmp r3, r2
  849. streq r1, [r0]
  850. mov r0, r3
  851. // unlock and return
  852. mov r3, #0
  853. str r3, [r12]
  854. {$ifdef CPUARM_HAS_BX}
  855. bx lr
  856. {$else}
  857. mov pc,lr
  858. {$endif}
  859. .Lfpc_system_lock:
  860. .long fpc_system_lock
  861. {$endif}
  862. {$endif}
  863. end;
  864. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  865. function declocked(var l: longint) : boolean; inline;
  866. begin
  867. Result:=InterLockedDecrement(l) = 0;
  868. end;
  869. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  870. procedure inclocked(var l: longint); inline;
  871. begin
  872. InterLockedIncrement(l);
  873. end;
  874. procedure fpc_cpucodeinit;
  875. begin
  876. {$ifdef FPC_SYSTEM_FPC_MOVE}
  877. {$ifndef CPUARM_HAS_EDSP}
  878. cpu_has_edsp:=true;
  879. in_edsp_test:=true;
  880. asm
  881. bic r0,sp,#7
  882. ldrd r0,[r0]
  883. end;
  884. in_edsp_test:=false;
  885. if cpu_has_edsp then
  886. moveproc:=@move_pld
  887. else
  888. moveproc:=@move_blended;
  889. {$else CPUARM_HAS_EDSP}
  890. cpu_has_edsp:=true;
  891. {$endif CPUARM_HAS_EDSP}
  892. {$endif FPC_SYSTEM_FPC_MOVE}
  893. end;
  894. {$define FPC_SYSTEM_HAS_SWAPENDIAN}
  895. { SwapEndian(<16 Bit>) being inlined is faster than using assembler }
  896. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  897. begin
  898. { the extra Word type cast is necessary because the "AValue shr 8" }
  899. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  900. { the sign bits from the upper 16 bits are shifted in rather than }
  901. { zeroes. }
  902. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  903. end;
  904. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  905. begin
  906. Result := Word((AValue shr 8) or (AValue shl 8));
  907. end;
  908. (*
  909. This is kept for reference. Thats what the compiler COULD generate in these cases.
  910. But FPC currently does not support inlining of asm-functions, so the whole call-overhead
  911. is bigger than the gain of the optimized function.
  912. function AsmSwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif};assembler;nostackframe;
  913. asm
  914. // We're starting with 4321
  915. {$if defined(CPUARM_HAS_REV)}
  916. rev r0, r0 // Reverse byteorder r0 = 1234
  917. mov r0, r0, shr #16 // Shift down to 16bits r0 = 0012
  918. {$else}
  919. mov r0, r0, shl #16 // Shift to make that 2100
  920. mov r0, r0, ror #24 // Rotate to 1002
  921. orr r0, r0, r0 shr #16 // Shift and combine into 0012
  922. {$endif}
  923. end;
  924. *)
  925. {
  926. These used to be an assembler-function, but with newer improvements to the compiler this
  927. generates a perfect 4 cycle code sequence and can be inlined.
  928. }
  929. function SwapEndian(const AValue: LongWord): LongWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  930. begin
  931. Result:= AValue xor rordword(AValue,16);
  932. Result:= Result and $FF00FFFF;
  933. Result:= (Result shr 8) xor rordword(AValue,8);
  934. end;
  935. function SwapEndian(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  936. begin
  937. Result:=LongInt(SwapEndian(DWord(AValue)));
  938. end;
  939. {
  940. Currently freepascal will not generate a good assembler sequence for
  941. Result:=(SwapEndian(longword(lo(AValue))) shl 32) or
  942. (SwapEndian(longword(hi(AValue))));
  943. So we keep an assembly version for now
  944. }
  945. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  946. asm
  947. // fpc >2.6.0 adds the "rev" instruction in the internal assembler
  948. {$if defined(CPUARM_HAS_REV)}
  949. rev r2, r0
  950. rev r0, r1
  951. mov r1, r2
  952. {$else}
  953. mov ip, r1
  954. // We're starting with r0 = $87654321
  955. eor r1, r0, r0, ror #16 // r1 = $C444C444
  956. bic r1, r1, #16711680 // r1 = r1 and $ff00ffff = $C400C444
  957. mov r0, r0, ror #8 // r0 = $21876543
  958. eor r1, r0, r1, lsr #8 // r1 = $21436587
  959. eor r0, ip, ip, ror #16
  960. bic r0, r0, #16711680
  961. mov ip, ip, ror #8
  962. eor r0, ip, r0, lsr #8
  963. {$endif}
  964. end;
  965. function SwapEndian(const AValue: QWord): QWord; {$ifdef SYSTEMINLINE}inline;{$endif}
  966. begin
  967. Result:=QWord(SwapEndian(Int64(AValue)));
  968. end;
  969. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  970. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  971. { Generic read/readwrite barrier code. }
  972. procedure barrier; assembler; nostackframe;
  973. asm
  974. // manually encode the instructions to avoid bootstrap and -march external
  975. // assembler settings
  976. {$ifdef CPUARM_HAS_DMB}
  977. .long 0xf57ff05f // dmb sy
  978. {$else}
  979. {$ifdef CPUARMV6}
  980. mov r0, #0
  981. .long 0xee070fba // mcr 15, 0, r0, cr7, cr10, {5}
  982. {$endif}
  983. {$endif}
  984. end;
  985. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  986. begin
  987. barrier;
  988. end;
  989. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  990. begin
  991. { reads imply barrier on earlier reads depended on; not required on ARM }
  992. end;
  993. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  994. begin
  995. barrier;
  996. end;
  997. procedure WriteBarrier; assembler; nostackframe;
  998. asm
  999. // specialize the write barrier because according to ARM, implementations for
  1000. // "dmb st" may be more optimal than the more generic "dmb sy"
  1001. {$ifdef CPUARM_HAS_DMB}
  1002. .long 0xf57ff05e // dmb st
  1003. {$else}
  1004. {$ifdef CPUARMV6}
  1005. mov r0, #0
  1006. .long 0xee070fba // mcr 15, 0, r0, cr7, cr10, {5}
  1007. {$endif}
  1008. {$endif}
  1009. end;
  1010. {$endif}
  1011. {include hand-optimized assembler division code}
  1012. { $i divide.inc}