arm.inc 28 KB

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