arm.inc 34 KB

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