arm.inc 33 KB

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