arm.inc 31 KB

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