arm.inc 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 divided}
  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. vmrs 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. vmsr 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. procedure fpc_shortstr_to_shortstr(out res:shortstring;const sstr:shortstring);assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  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. {$ifdef CPUARM_HAS_BX}
  442. bx lr
  443. {$else}
  444. mov pc,lr
  445. {$endif}
  446. .L01010101:
  447. .long 0x01010101
  448. end;
  449. {$endif}
  450. {$ifndef darwin}
  451. {$define FPC_SYSTEM_HAS_ANSISTR_DECR_REF}
  452. Procedure fpc_ansistr_decr_ref (Var S : Pointer); [Public,Alias:'FPC_ANSISTR_DECR_REF'];assembler;nostackframe; compilerproc;
  453. asm
  454. ldr r1, [r0]
  455. // On return the pointer will always be set to zero, so utilize the delay slots
  456. mov r2, #0
  457. str r2, [r0]
  458. // Check for a zero string
  459. cmp r1, #0
  460. // Load reference counter
  461. ldrne r2, [r1, #-8]
  462. {$ifdef CPUARM_HAS_BX}
  463. bxeq lr
  464. {$else}
  465. moveq pc,lr
  466. {$endif}
  467. // Check for a constant string
  468. cmp r2, #0
  469. {$ifdef CPUARM_HAS_BX}
  470. bxlt lr
  471. {$else}
  472. movlt pc,lr
  473. {$endif}
  474. stmfd sp!, {r1, lr}
  475. sub r0, r1, #8
  476. {$ifdef CPUARM_HAS_BLX}
  477. blx InterLockedDecrement
  478. {$else}
  479. bl InterLockedDecrement
  480. {$endif}
  481. // InterLockedDecrement is a nice guy and sets the z flag for us
  482. // if the reference count dropped to 0
  483. ldmnefd sp!, {r1, pc}
  484. ldmfd sp!, {r0, lr}
  485. // We currently can not use constant symbols in ARM-Assembly
  486. // but we need to stay backward compatible with 2.6
  487. sub r0, r0, #12
  488. // Jump without a link, so freemem directly returns to our caller
  489. b FPC_FREEMEM
  490. end;
  491. {$endif not darwin}
  492. var
  493. fpc_system_lock: longint; export name 'fpc_system_lock';
  494. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  495. asm
  496. {$ifdef CPUARM_HAS_LDREX}
  497. .Lloop:
  498. ldrex r1, [r0]
  499. sub r1, r1, #1
  500. strex r2, r1, [r0]
  501. cmp r2, #0
  502. bne .Lloop
  503. movs r0, r1
  504. bx lr
  505. {$else}
  506. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  507. stmfd r13!, {lr}
  508. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  509. .Latomic_dec_loop:
  510. ldr r0, [r2] // Load the current value
  511. // We expect this to work without looping most of the time
  512. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  513. // loop here again, we have to reload the value. Normaly this just fills the
  514. // load stall-cycles from the above ldr so in reality we'll not get any additional
  515. // delays because of this
  516. // Don't use ldr to load r3 to avoid cacheline trashing
  517. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  518. // the kuser_cmpxchg entry point
  519. mvn r3, #0x0000f000
  520. sub r3, r3, #0x3F
  521. sub r1, r0, #1 // Decrement value
  522. {$ifdef CPUARM_HAS_BLX}
  523. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  524. {$else}
  525. mov lr, pc
  526. {$ifdef CPUARM_HAS_BX}
  527. bx r3
  528. {$else}
  529. mov pc, r3
  530. {$endif}
  531. {$endif}
  532. // MOVS sets the Z flag when the result reaches zero, this can be used later on
  533. // The C-Flag will not be modified by this because we're not doing any shifting
  534. movcss r0, r1 // We expect that to work most of the time so keep it pipeline friendly
  535. ldmcsfd r13!, {pc}
  536. b .Latomic_dec_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. sub r1, r1, #1
  548. str r1, [r0]
  549. movs r0, r1
  550. // unlock and return
  551. str r2, [r3]
  552. {$ifdef CPUARM_HAS_BX}
  553. bx lr
  554. {$else}
  555. mov pc,lr
  556. {$endif}
  557. .Lfpc_system_lock:
  558. .long fpc_system_lock
  559. {$endif}
  560. {$endif}
  561. end;
  562. {$ifndef darwin}
  563. {$define FPC_SYSTEM_HAS_ANSISTR_INCR_REF}
  564. Procedure fpc_ansistr_incr_ref (S : Pointer); [Public,Alias:'FPC_ANSISTR_INCR_REF'];assembler;nostackframe; compilerproc;
  565. asm
  566. // Null string?
  567. cmp r0, #0
  568. // Load reference counter
  569. ldrne r1, [r0, #-8]
  570. // pointer to counter, calculate here for delay slot utilization
  571. subne r0, r0, #8
  572. {$ifdef CPUARM_HAS_BX}
  573. bxeq lr
  574. {$else}
  575. moveq pc,lr
  576. {$endif}
  577. // Check for a constant string
  578. cmp r1, #0
  579. // Tailcall
  580. // Hopefully the linker will place InterLockedIncrement as layed out here
  581. bge InterLockedIncrement
  582. // Freepascal will generate a proper return here, save some cachespace
  583. end;
  584. {$endif not darwin}
  585. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  586. asm
  587. {$ifdef CPUARM_HAS_LDREX}
  588. .Lloop:
  589. ldrex r1, [r0]
  590. add r1, r1, #1
  591. strex r2, r1, [r0]
  592. cmp r2, #0
  593. bne .Lloop
  594. mov r0, r1
  595. bx lr
  596. {$else}
  597. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  598. stmfd r13!, {lr}
  599. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  600. .Latomic_inc_loop:
  601. ldr r0, [r2] // Load the current value
  602. // We expect this to work without looping most of the time
  603. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  604. // loop here again, we have to reload the value. Normaly this just fills the
  605. // load stall-cycles from the above ldr so in reality we'll not get any additional
  606. // delays because of this
  607. // Don't use ldr to load r3 to avoid cacheline trashing
  608. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  609. // the kuser_cmpxchg entry point
  610. mvn r3, #0x0000f000
  611. sub r3, r3, #0x3F
  612. add r1, r0, #1 // Increment value
  613. {$ifdef CPUARM_HAS_BLX}
  614. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  615. {$else}
  616. mov lr, pc
  617. {$ifdef CPUARM_HAS_BX}
  618. bx r3
  619. {$else}
  620. mov pc, r3
  621. {$endif}
  622. {$endif}
  623. movcs r0, r1 // We expect that to work most of the time so keep it pipeline friendly
  624. ldmcsfd r13!, {pc}
  625. b .Latomic_inc_loop // kuser_cmpxchg sets C flag on error
  626. {$else}
  627. // lock
  628. ldr r3, .Lfpc_system_lock
  629. mov r1, #1
  630. .Lloop:
  631. swp r2, r1, [r3]
  632. cmp r2, #0
  633. bne .Lloop
  634. // do the job
  635. ldr r1, [r0]
  636. add r1, r1, #1
  637. str r1, [r0]
  638. mov r0, r1
  639. // unlock and return
  640. str r2, [r3]
  641. {$ifdef CPUARM_HAS_BX}
  642. bx lr
  643. {$else}
  644. mov pc,lr
  645. {$endif}
  646. .Lfpc_system_lock:
  647. .long fpc_system_lock
  648. {$endif}
  649. {$endif}
  650. end;
  651. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  652. asm
  653. {$ifdef CPUARM_HAS_LDREX}
  654. // swp is deprecated on ARMv6 and above
  655. .Lloop:
  656. ldrex r2, [r0]
  657. strex r3, r1, [r0]
  658. cmp r3, #0
  659. bne .Lloop
  660. mov r0, r2
  661. bx lr
  662. {$else}
  663. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  664. stmfd r13!, {r4, lr}
  665. mov r2, r0 // kuser_cmpxchg does not clobber r2 (and r1) by definition
  666. .Latomic_add_loop:
  667. ldr r0, [r2] // Load the current value
  668. // We expect this to work without looping most of the time
  669. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  670. // loop here again, we have to reload the value. Normaly this just fills the
  671. // load stall-cycles from the above ldr so in reality we'll not get any additional
  672. // delays because of this
  673. // Don't use ldr to load r3 to avoid cacheline trashing
  674. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  675. // the kuser_cmpxchg entry point
  676. mvn r3, #0x0000f000
  677. sub r3, r3, #0x3F
  678. mov r4, r0 // save the current value because kuser_cmpxchg clobbers r0
  679. {$ifdef CPUARM_HAS_BLX}
  680. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  681. {$else}
  682. mov lr, pc
  683. {$ifdef CPUARM_HAS_BX}
  684. bx r3
  685. {$else}
  686. mov pc, r3
  687. {$endif}
  688. {$endif}
  689. // restore the original value if needed
  690. movcs r0, r4
  691. ldmcsfd r13!, {r4, pc}
  692. b .Latomic_add_loop // kuser_cmpxchg failed, loop back
  693. {$else}
  694. // lock
  695. ldr r3, .Lfpc_system_lock
  696. mov r2, #1
  697. .Lloop:
  698. swp r2, r2, [r3]
  699. cmp r2, #0
  700. bne .Lloop
  701. // do the job
  702. ldr r2, [r0]
  703. str r1, [r0]
  704. mov r0, r2
  705. // unlock and return
  706. mov r2, #0
  707. str r2, [r3]
  708. {$ifdef CPUARM_HAS_BX}
  709. bx lr
  710. {$else}
  711. mov pc,lr
  712. {$endif}
  713. .Lfpc_system_lock:
  714. .long fpc_system_lock
  715. {$endif}
  716. {$endif}
  717. end;
  718. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  719. asm
  720. {$ifdef CPUARM_HAS_LDREX}
  721. .Lloop:
  722. ldrex r2, [r0]
  723. add r12, r1, r2
  724. strex r3, r12, [r0]
  725. cmp r3, #0
  726. bne .Lloop
  727. mov r0, r2
  728. bx lr
  729. {$else}
  730. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  731. stmfd r13!, {r4, lr}
  732. mov r2, r0 // kuser_cmpxchg does not clobber r2 by definition
  733. mov r4, r1 // Save addend
  734. .Latomic_add_loop:
  735. ldr r0, [r2] // Load the current value
  736. // We expect this to work without looping most of the time
  737. // R3 gets clobbered in kuser_cmpxchg so in the unlikely case that we have to
  738. // loop here again, we have to reload the value. Normaly this just fills the
  739. // load stall-cycles from the above ldr so in reality we'll not get any additional
  740. // delays because of this
  741. // Don't use ldr to load r3 to avoid cacheline trashing
  742. // Load 0xffff0fff into r3 and substract to 0xffff0fc0,
  743. // the kuser_cmpxchg entry point
  744. mvn r3, #0x0000f000
  745. sub r3, r3, #0x3F
  746. add r1, r0, r4 // Add to value
  747. {$ifdef CPUARM_HAS_BLX}
  748. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  749. {$else}
  750. mov lr, pc
  751. {$ifdef CPUARM_HAS_BX}
  752. bx r3
  753. {$else}
  754. mov pc, r3
  755. {$endif}
  756. {$endif}
  757. // r1 does not get clobbered, so just get back the original value
  758. // Otherwise we would have to allocate one more register and store the
  759. // temporary value
  760. subcs r0, r1, r4
  761. ldmcsfd r13!, {r4, pc}
  762. b .Latomic_add_loop // kuser_cmpxchg failed, loop back
  763. {$else}
  764. // lock
  765. ldr r3, .Lfpc_system_lock
  766. mov r2, #1
  767. .Lloop:
  768. swp r2, r2, [r3]
  769. cmp r2, #0
  770. bne .Lloop
  771. // do the job
  772. ldr r2, [r0]
  773. add r1, r1, r2
  774. str r1, [r0]
  775. mov r0, r2
  776. // unlock and return
  777. mov r2, #0
  778. str r2, [r3]
  779. {$ifdef CPUARM_HAS_BX}
  780. bx lr
  781. {$else}
  782. mov pc,lr
  783. {$endif}
  784. .Lfpc_system_lock:
  785. .long fpc_system_lock
  786. {$endif}
  787. {$endif}
  788. end;
  789. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  790. asm
  791. {$ifdef CPUARM_HAS_LDREX}
  792. .Lloop:
  793. ldrex r3, [r0]
  794. mov r12, #0
  795. cmp r3, r2
  796. strexeq r12, r1, [r0]
  797. cmp r12, #0
  798. bne .Lloop
  799. mov r0, r3
  800. bx lr
  801. {$else}
  802. {$ifdef SYSTEM_HAS_KUSER_CMPXCHG}
  803. stmfd r13!, {r4, lr}
  804. mvn r3, #0x0000f000
  805. sub r3, r3, #0x3F
  806. mov r4, r2 // Swap parameters around
  807. mov r2, r0
  808. mov r0, r4 // Use r4 because we'll need the new value for later
  809. // r1 and r2 will not be clobbered by kuser_cmpxchg
  810. // If we have to loop, r0 will be set to the original Comperand
  811. .Linterlocked_compare_exchange_loop:
  812. {$ifdef CPUARM_HAS_BLX}
  813. blx r3 // Call kuser_cmpxchg, sets C-Flag on success
  814. {$else}
  815. mov lr, pc
  816. {$ifdef CPUARM_HAS_BX}
  817. bx r3
  818. {$else}
  819. mov pc, r3
  820. {$endif}
  821. {$endif}
  822. movcs r0, r4 // Return the previous value on success
  823. ldmcsfd r13!, {r4, pc}
  824. // The error case is a bit tricky, kuser_cmpxchg does not return the current value
  825. // So we may need to loop to avoid race conditions
  826. // The loop case is HIGHLY unlikely, it would require that we got rescheduled between
  827. // calling kuser_cmpxchg and the ldr. While beeing rescheduled another process/thread
  828. // would have the set the value to our comperand
  829. ldr r0, [r2] // Load the currently set value
  830. cmp r0, r4 // Return if Comperand != current value, otherwise loop again
  831. ldmnefd r13!, {r4, pc}
  832. // If we need to loop here, we have to
  833. b .Linterlocked_compare_exchange_loop
  834. {$else}
  835. // lock
  836. ldr r12, .Lfpc_system_lock
  837. mov r3, #1
  838. .Lloop:
  839. swp r3, r3, [r12]
  840. cmp r3, #0
  841. bne .Lloop
  842. // do the job
  843. ldr r3, [r0]
  844. cmp r3, r2
  845. streq r1, [r0]
  846. mov r0, r3
  847. // unlock and return
  848. mov r3, #0
  849. str r3, [r12]
  850. {$ifdef CPUARM_HAS_BX}
  851. bx lr
  852. {$else}
  853. mov pc,lr
  854. {$endif}
  855. .Lfpc_system_lock:
  856. .long fpc_system_lock
  857. {$endif}
  858. {$endif}
  859. end;
  860. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  861. function declocked(var l: longint) : boolean; inline;
  862. begin
  863. Result:=InterLockedDecrement(l) = 0;
  864. end;
  865. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  866. procedure inclocked(var l: longint); inline;
  867. begin
  868. InterLockedIncrement(l);
  869. end;
  870. procedure fpc_cpucodeinit;
  871. begin
  872. {$ifdef FPC_SYSTEM_FPC_MOVE}
  873. {$ifndef CPUARM_HAS_EDSP}
  874. cpu_has_edsp:=true;
  875. in_edsp_test:=true;
  876. asm
  877. bic r0,sp,#7
  878. ldrd r0,r1,[r0]
  879. end;
  880. in_edsp_test:=false;
  881. if cpu_has_edsp then
  882. moveproc:=@move_pld
  883. else
  884. moveproc:=@move_blended;
  885. {$else CPUARM_HAS_EDSP}
  886. cpu_has_edsp:=true;
  887. {$endif CPUARM_HAS_EDSP}
  888. {$endif FPC_SYSTEM_FPC_MOVE}
  889. end;
  890. {$define FPC_SYSTEM_HAS_SWAPENDIAN}
  891. { SwapEndian(<16 Bit>) being inlined is faster than using assembler }
  892. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  893. begin
  894. { the extra Word type cast is necessary because the "AValue shr 8" }
  895. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  896. { the sign bits from the upper 16 bits are shifted in rather than }
  897. { zeroes. }
  898. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  899. end;
  900. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  901. begin
  902. Result := Word((AValue shr 8) or (AValue shl 8));
  903. end;
  904. (*
  905. This is kept for reference. Thats what the compiler COULD generate in these cases.
  906. But FPC currently does not support inlining of asm-functions, so the whole call-overhead
  907. is bigger than the gain of the optimized function.
  908. function AsmSwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif};assembler;nostackframe;
  909. asm
  910. // We're starting with 4321
  911. {$if defined(CPUARM_HAS_REV)}
  912. rev r0, r0 // Reverse byteorder r0 = 1234
  913. mov r0, r0, shr #16 // Shift down to 16bits r0 = 0012
  914. {$else}
  915. mov r0, r0, shl #16 // Shift to make that 2100
  916. mov r0, r0, ror #24 // Rotate to 1002
  917. orr r0, r0, r0 shr #16 // Shift and combine into 0012
  918. {$endif}
  919. end;
  920. *)
  921. {
  922. These used to be an assembler-function, but with newer improvements to the compiler this
  923. generates a perfect 4 cycle code sequence and can be inlined.
  924. }
  925. function SwapEndian(const AValue: LongWord): LongWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  926. begin
  927. Result:= AValue xor rordword(AValue,16);
  928. Result:= Result and $FF00FFFF;
  929. Result:= (Result shr 8) xor rordword(AValue,8);
  930. end;
  931. function SwapEndian(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  932. begin
  933. Result:=LongInt(SwapEndian(DWord(AValue)));
  934. end;
  935. {
  936. Currently freepascal will not generate a good assembler sequence for
  937. Result:=(SwapEndian(longword(lo(AValue))) shl 32) or
  938. (SwapEndian(longword(hi(AValue))));
  939. So we keep an assembly version for now
  940. }
  941. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  942. asm
  943. // fpc >2.6.0 adds the "rev" instruction in the internal assembler
  944. {$if defined(CPUARM_HAS_REV)}
  945. rev r2, r0
  946. rev r0, r1
  947. mov r1, r2
  948. {$else}
  949. mov ip, r1
  950. // We're starting with r0 = $87654321
  951. eor r1, r0, r0, ror #16 // r1 = $C444C444
  952. bic r1, r1, #16711680 // r1 = r1 and $ff00ffff = $C400C444
  953. mov r0, r0, ror #8 // r0 = $21876543
  954. eor r1, r0, r1, lsr #8 // r1 = $21436587
  955. eor r0, ip, ip, ror #16
  956. bic r0, r0, #16711680
  957. mov ip, ip, ror #8
  958. eor r0, ip, r0, lsr #8
  959. {$endif}
  960. end;
  961. function SwapEndian(const AValue: QWord): QWord; {$ifdef SYSTEMINLINE}inline;{$endif}
  962. begin
  963. Result:=QWord(SwapEndian(Int64(AValue)));
  964. end;
  965. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  966. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  967. { Generic read/readwrite barrier code. }
  968. procedure barrier; assembler; nostackframe;
  969. asm
  970. // manually encode the instructions to avoid bootstrap and -march external
  971. // assembler settings
  972. {$ifdef CPUARM_HAS_DMB}
  973. .long 0xf57ff05f // dmb sy
  974. {$else}
  975. {$ifdef CPUARMV6}
  976. mov r0, #0
  977. .long 0xee070fba // mcr 15, 0, r0, cr7, cr10, {5}
  978. {$endif}
  979. {$endif}
  980. end;
  981. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  982. begin
  983. barrier;
  984. end;
  985. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  986. begin
  987. { reads imply barrier on earlier reads depended on; not required on ARM }
  988. end;
  989. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  990. begin
  991. barrier;
  992. end;
  993. procedure WriteBarrier; assembler; nostackframe;
  994. asm
  995. // specialize the write barrier because according to ARM, implementations for
  996. // "dmb st" may be more optimal than the more generic "dmb sy"
  997. {$ifdef CPUARM_HAS_DMB}
  998. .long 0xf57ff05e // dmb st
  999. {$else}
  1000. {$ifdef CPUARMV6}
  1001. mov r0, #0
  1002. .long 0xee070fba // mcr 15, 0, r0, cr7, cr10, {5}
  1003. {$endif}
  1004. {$endif}
  1005. end;
  1006. {$endif}
  1007. {include hand-optimized assembler division code}
  1008. {$i divide.inc}