arm.inc 36 KB

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