arm.inc 32 KB

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