thumb2.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. {$asmmode gas}
  13. {$ifndef FPC_SYSTEM_HAS_MOVE}
  14. {$define FPC_SYSTEM_FPC_MOVE}
  15. {$endif FPC_SYSTEM_HAS_MOVE}
  16. {$ifdef FPC_SYSTEM_FPC_MOVE}
  17. const
  18. cpu_has_edsp : boolean = false;
  19. in_edsp_test : boolean = false;
  20. {$endif FPC_SYSTEM_FPC_MOVE}
  21. {$if not(defined(wince)) and not(defined(gba)) and not(defined(nds)) and not(defined(FPUSOFT)) and not(defined(FPULIBGCC))}
  22. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  23. Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  24. begin
  25. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  26. asm
  27. rfs r0
  28. and r0,r0,#0xffe0ffff
  29. orr r0,r0,#0x00070000
  30. wfs r0
  31. end;
  32. end;
  33. {$endif}
  34. procedure fpc_cpuinit;
  35. begin
  36. SysInitFPU;
  37. end;
  38. {$ifdef wince}
  39. function _controlfp(new: DWORD; mask: DWORD): DWORD; cdecl; external 'coredll';
  40. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  41. Procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  42. begin
  43. softfloat_exception_flags:=0;
  44. end;
  45. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  46. Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  47. begin
  48. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  49. { Enable FPU exceptions, but disable INEXACT, UNDERFLOW, DENORMAL }
  50. { FPU precision 64 bit, rounding to nearest, affine infinity }
  51. _controlfp($000C0003, $030F031F);
  52. end;
  53. {$endif wince}
  54. {****************************************************************************
  55. stack frame related stuff
  56. ****************************************************************************}
  57. {$IFNDEF INTERNAL_BACKTRACE}
  58. {$define FPC_SYSTEM_HAS_GET_FRAME}
  59. function get_frame:pointer;assembler;nostackframe;
  60. asm
  61. mov r0,r11
  62. end;
  63. {$ENDIF not INTERNAL_BACKTRACE}
  64. {
  65. Stack frame on Thumb2:
  66. LR <- FP
  67. Old FP
  68. }
  69. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  70. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler;
  71. asm
  72. movs r0,r0
  73. beq .Lg_a_null
  74. ldr r0,[r0]
  75. .Lg_a_null:
  76. end;
  77. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  78. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler;
  79. asm
  80. movs r0,r0
  81. beq .Lgnf_null
  82. ldr r0,[r0,#-4]
  83. .Lgnf_null:
  84. end;
  85. {$define FPC_SYSTEM_HAS_SPTR}
  86. Function Sptr : pointer;assembler;
  87. asm
  88. mov r0,sp
  89. end;
  90. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  91. {$define FPC_SYSTEM_HAS_FILLCHAR}
  92. Procedure FillChar(var x;count:longint;value:byte);assembler;nostackframe;
  93. asm
  94. // less than 0?
  95. cmp r1,#0
  96. it lt
  97. movlt pc,lr
  98. mov r3,r0
  99. cmp r1,#8 // at least 8 bytes to do?
  100. blt .LFillchar2
  101. orr r2,r2,r2,lsl #8
  102. orr r2,r2,r2,lsl #16
  103. .LFillchar0:
  104. tst r3,#3 // aligned yet?
  105. itt ne
  106. strneb r2,[r3],#1
  107. subne r1,r1,#1
  108. bne .LFillchar0
  109. mov ip,r2
  110. .LFillchar1:
  111. cmp r1,#8 // 8 bytes still to do?
  112. blt .LFillchar2
  113. stmia r3!,{r2,ip}
  114. sub r1,r1,#8
  115. cmp r1,#8 // 8 bytes still to do?
  116. blt .LFillchar2
  117. stmia r3!,{r2,ip}
  118. sub r1,r1,#8
  119. cmp r1,#8 // 8 bytes still to do?
  120. blt .LFillchar2
  121. stmia r3!,{r2,ip}
  122. sub r1,r1,#8
  123. cmp r1,#8 // 8 bytes still to do?
  124. itt ge
  125. stmgeia r3!,{r2,ip}
  126. subge r1,r1,#8
  127. bge .LFillchar1
  128. .LFillchar2:
  129. movs r1,r1 // anything left?
  130. it eq
  131. moveq pc,lr
  132. rsb r1,r1,#7
  133. mov r1,r1,lsl #2
  134. add pc,r1
  135. mov r0,r0
  136. strb r2,[r3],#1
  137. strb r2,[r3],#1
  138. strb r2,[r3],#1
  139. strb r2,[r3],#1
  140. strb r2,[r3],#1
  141. strb r2,[r3],#1
  142. strb r2,[r3],#1
  143. mov pc,lr
  144. end;
  145. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  146. {$ifndef FPC_SYSTEM_HAS_MOVE}
  147. {$define FPC_SYSTEM_HAS_MOVE}
  148. procedure Move_pld(const source;var dest;count:longint);assembler;nostackframe;
  149. asm
  150. pld [r0]
  151. pld [r1]
  152. // count <=0 ?
  153. cmp r2,#0
  154. it le
  155. movle pc,lr
  156. // overlap?
  157. cmp r1,r0
  158. bls .Lnooverlap
  159. add r3,r0,r2
  160. cmp r3,r1
  161. bls .Lnooverlap
  162. // overlap, copy backward
  163. .Loverlapped:
  164. subs r2,r2,#1
  165. ldrb r3,[r0,r2]
  166. strb r3,[r1,r2]
  167. bne .Loverlapped
  168. mov pc,lr
  169. .Lnooverlap:
  170. // less then 16 bytes to copy?
  171. cmp r2,#8
  172. // yes, the forget about the whole optimizations
  173. // and do a bytewise copy
  174. blt .Lbyteloop
  175. // both aligned?
  176. orr r3,r0,r1
  177. tst r3,#3
  178. bne .Lbyteloop
  179. (*
  180. // yes, then align
  181. // alignment to 4 byte boundries is enough
  182. ldrb ip,[r0],#1
  183. sub r2,r2,#1
  184. stb ip,[r1],#1
  185. tst r3,#2
  186. bne .Ldifferentaligned
  187. ldrh ip,[r0],#2
  188. sub r2,r2,#2
  189. sth ip,[r1],#2
  190. .Ldifferentaligned
  191. // qword aligned?
  192. orrs r3,r0,r1
  193. tst r3,#7
  194. bne .Ldwordloop
  195. *)
  196. pld [r0,#32]
  197. pld [r1,#32]
  198. .Ldwordloop:
  199. sub r2,r2,#4
  200. ldr r3,[r0],#4
  201. // preload
  202. pld [r0,#64]
  203. pld [r1,#64]
  204. cmp r2,#4
  205. str r3,[r1],#4
  206. bcs .Ldwordloop
  207. cmp r2,#0
  208. it eq
  209. moveq pc,lr
  210. .Lbyteloop:
  211. subs r2,r2,#1
  212. ldrb r3,[r0],#1
  213. strb r3,[r1],#1
  214. bne .Lbyteloop
  215. mov pc,lr
  216. end;
  217. procedure Move_blended(const source;var dest;count:longint);assembler;nostackframe;
  218. asm
  219. // count <=0 ?
  220. cmp r2,#0
  221. it le
  222. movle pc,lr
  223. // overlap?
  224. cmp r1,r0
  225. bls .Lnooverlap
  226. add r3,r0,r2
  227. cmp r3,r1
  228. bls .Lnooverlap
  229. // overlap, copy backward
  230. .Loverlapped:
  231. subs r2,r2,#1
  232. ldrb r3,[r0,r2]
  233. strb r3,[r1,r2]
  234. bne .Loverlapped
  235. mov pc,lr
  236. .Lnooverlap:
  237. // less then 16 bytes to copy?
  238. cmp r2,#8
  239. // yes, the forget about the whole optimizations
  240. // and do a bytewise copy
  241. blt .Lbyteloop
  242. // both aligned?
  243. orr r3,r0,r1
  244. tst r3,#3
  245. bne .Lbyteloop
  246. (*
  247. // yes, then align
  248. // alignment to 4 byte boundries is enough
  249. ldrb ip,[r0],#1
  250. sub r2,r2,#1
  251. stb ip,[r1],#1
  252. tst r3,#2
  253. bne .Ldifferentaligned
  254. ldrh ip,[r0],#2
  255. sub r2,r2,#2
  256. sth ip,[r1],#2
  257. .Ldifferentaligned
  258. // qword aligned?
  259. orrs r3,r0,r1
  260. tst r3,#7
  261. bne .Ldwordloop
  262. *)
  263. .Ldwordloop:
  264. sub r2,r2,#4
  265. ldr r3,[r0],#4
  266. cmp r2,#4
  267. str r3,[r1],#4
  268. bcs .Ldwordloop
  269. cmp r2,#0
  270. it eq
  271. moveq pc,lr
  272. .Lbyteloop:
  273. subs r2,r2,#1
  274. ldrb r3,[r0],#1
  275. strb r3,[r1],#1
  276. bne .Lbyteloop
  277. mov pc,lr
  278. end;
  279. const
  280. moveproc : pointer = @move_blended;
  281. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  282. asm
  283. ldr ip,.Lmoveproc
  284. ldr pc,[ip]
  285. .Lmoveproc:
  286. .long moveproc
  287. end;
  288. {$endif FPC_SYSTEM_HAS_MOVE}
  289. {****************************************************************************
  290. String
  291. ****************************************************************************}
  292. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  293. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  294. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  295. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring):shortstring;assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  296. {$else}
  297. procedure fpc_shortstr_to_shortstr(out res:shortstring;const sstr:shortstring);assembler;nostackframe;[public,alias: 'FPC_SHORTSTR_TO_SHORTSTR'];compilerproc;
  298. {$endif}
  299. {r0: __RESULT
  300. r1: len
  301. r2: sstr}
  302. asm
  303. ldrb r12,[r2],#1
  304. cmp r12,r1
  305. it gt
  306. movgt r12,r1
  307. strb r12,[r0],#1
  308. cmp r12,#6 (* 6 seems to be the break even point. *)
  309. blt .LStartTailCopy
  310. (* Align destination on 32bits. This is the only place where unrolling
  311. really seems to help, since in the common case, sstr is aligned on
  312. 32 bits, therefore in the common case we need to copy 3 bytes to
  313. align, i.e. in the case of a loop, you wouldn't branch out early.*)
  314. rsb r3,r0,#0
  315. ands r3,r3,#3
  316. sub r12,r12,r3
  317. itttt ne
  318. ldrneb r1,[r2],#1
  319. strneb r1,[r0],#1
  320. subnes r3,r3,#1
  321. ldrneb r1,[r2],#1
  322. itttt ne
  323. strneb r1,[r0],#1
  324. subnes r3,r3,#1
  325. ldrneb r1,[r2],#1
  326. strneb r1,[r0],#1
  327. it ne
  328. subnes r3,r3,#1
  329. .LDoneAlign:
  330. (* Destination should be aligned now, but source might not be aligned,
  331. if this is the case, do a byte-per-byte copy. *)
  332. tst r2,#3
  333. bne .LStartTailCopy
  334. (* Start the main copy, 32 bit at a time. *)
  335. movs r3,r12,lsr #2
  336. and r12,r12,#3
  337. beq .LStartTailCopy
  338. .LNext4bytes:
  339. (* Unrolling this loop would save a little bit of time for long strings
  340. (>20 chars), but alas, it hurts for short strings and they are the
  341. common case.*)
  342. ittt ne
  343. ldrne r1,[r2],#4
  344. strne r1,[r0],#4
  345. subnes r3,r3,#1
  346. bne .LNext4bytes
  347. .LStartTailCopy:
  348. (* Do remaining bytes. *)
  349. cmp r12,#0
  350. beq .LDoneTail
  351. .LNextChar3:
  352. ldrb r1,[r2],#1
  353. strb r1,[r0],#1
  354. subs r12,r12,#1
  355. bne .LNextChar3
  356. .LDoneTail:
  357. end;
  358. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);assembler;nostackframe;[public,alias:'FPC_SHORTSTR_ASSIGN'];compilerproc;
  359. {r0: len
  360. r1: sstr
  361. r2: dstr}
  362. asm
  363. ldrb r12,[r1],#1
  364. cmp r12,r0
  365. it gt
  366. movgt r12,r0
  367. strb r12,[r2],#1
  368. cmp r12,#6 (* 6 seems to be the break even point. *)
  369. blt .LStartTailCopy
  370. (* Align destination on 32bits. This is the only place where unrolling
  371. really seems to help, since in the common case, sstr is aligned on
  372. 32 bits, therefore in the common case we need to copy 3 bytes to
  373. align, i.e. in the case of a loop, you wouldn't branch out early.*)
  374. rsb r3,r2,#0
  375. ands r3,r3,#3
  376. sub r12,r12,r3
  377. itttt ne
  378. ldrneb r0,[r1],#1
  379. strneb r0,[r2],#1
  380. subnes r3,r3,#1
  381. ldrneb r0,[r1],#1
  382. itttt ne
  383. strneb r0,[r2],#1
  384. subnes r3,r3,#1
  385. ldrneb r0,[r1],#1
  386. strneb r0,[r2],#1
  387. it ne
  388. subnes r3,r3,#1
  389. .LDoneAlign:
  390. (* Destination should be aligned now, but source might not be aligned,
  391. if this is the case, do a byte-per-byte copy. *)
  392. tst r1,#3
  393. bne .LStartTailCopy
  394. (* Start the main copy, 32 bit at a time. *)
  395. movs r3,r12,lsr #2
  396. and r12,r12,#3
  397. beq .LStartTailCopy
  398. .LNext4bytes:
  399. (* Unrolling this loop would save a little bit of time for long strings
  400. (>20 chars), but alas, it hurts for short strings and they are the
  401. common case.*)
  402. ittt ne
  403. ldrne r0,[r1],#4
  404. strne r0,[r2],#4
  405. subnes r3,r3,#1
  406. bne .LNext4bytes
  407. .LStartTailCopy:
  408. (* Do remaining bytes. *)
  409. cmp r12,#0
  410. beq .LDoneTail
  411. .LNextChar3:
  412. ldrb r0,[r1],#1
  413. strb r0,[r2],#1
  414. subs r12,r12,#1
  415. bne .LNextChar3
  416. .LDoneTail:
  417. end;
  418. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  419. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  420. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  421. function fpc_Pchar_length(p:Pchar):sizeint;assembler;nostackframe;[public,alias:'FPC_PCHAR_LENGTH'];compilerproc;
  422. asm
  423. cmp r0,#0
  424. mov r1,r0
  425. beq .Ldone
  426. .Lnextchar:
  427. (*Are we aligned?*)
  428. tst r1,#3
  429. bne .Ltest_unaligned (*No, do byte per byte.*)
  430. ldr r3,.L01010101
  431. .Ltest_aligned:
  432. (*Aligned, load 4 bytes at a time.*)
  433. ldr r12,[r1],#4
  434. (*Check wether r12 contains a 0 byte.*)
  435. sub r2,r12,r3
  436. mvn r12,r12
  437. and r2,r2,r12
  438. ands r2,r2,r3,lsl #7 (*r3 lsl 7 = $80808080*)
  439. beq .Ltest_aligned (*No 0 byte, repeat.*)
  440. sub r1,r1,#4
  441. .Ltest_unaligned:
  442. ldrb r12,[r1],#1
  443. cmp r12,#1 (*r12<1 same as r12=0, but result in carry flag*)
  444. bcs .Lnextchar
  445. (*Dirty trick: we need to subtract 1 extra because we have counted the
  446. terminating 0, due to the known carry flag sbc can do this.*)
  447. sbc r0,r1,r0
  448. .Ldone:
  449. mov pc,lr
  450. .L01010101:
  451. .long 0x01010101
  452. end;
  453. {$endif}
  454. var
  455. fpc_system_lock: longint; export name 'fpc_system_lock';
  456. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  457. asm
  458. // lock
  459. ldr r3, .Lfpc_system_lock
  460. mov r1, #1
  461. .Lloop:
  462. ldrex r2, [r3]
  463. cmp r2, #0
  464. itt eq
  465. strexeq r2, r1, [r3]
  466. cmpeq r2, #0
  467. bne .Lloop
  468. // do the job
  469. ldr r1, [r0]
  470. sub r1, r1, #1
  471. str r1, [r0]
  472. mov r0, r1
  473. // unlock and return
  474. str r2, [r3]
  475. mov pc, lr
  476. .Lfpc_system_lock:
  477. .long fpc_system_lock
  478. end;
  479. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  480. asm
  481. // lock
  482. ldr r3, .Lfpc_system_lock
  483. mov r1, #1
  484. .Lloop:
  485. ldrex r2, [r3]
  486. cmp r2, #0
  487. itt eq
  488. strexeq r2, r1, [r3]
  489. cmpeq r2, #0
  490. bne .Lloop
  491. // do the job
  492. ldr r1, [r0]
  493. add r1, r1, #1
  494. str r1, [r0]
  495. mov r0, r1
  496. // unlock and return
  497. str r2, [r3]
  498. mov pc, lr
  499. .Lfpc_system_lock:
  500. .long fpc_system_lock
  501. end;
  502. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  503. asm
  504. // lock
  505. ldr r3, .Lfpc_system_lock
  506. mov r2, #1
  507. .Lloop:
  508. ldrex r2, [r3]
  509. cmp r2, #0
  510. itt eq
  511. strexeq r2, r12, [r3]
  512. cmpeq r2, #0
  513. bne .Lloop
  514. // do the job
  515. ldr r2, [r0]
  516. str r1, [r0]
  517. mov r0, r2
  518. // unlock and return
  519. mov r2, #0
  520. str r2, [r3]
  521. mov pc, lr
  522. .Lfpc_system_lock:
  523. .long fpc_system_lock
  524. end;
  525. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  526. asm
  527. // lock
  528. ldr r3, .Lfpc_system_lock
  529. mov r2, #1
  530. .Lloop:
  531. ldrex r2, [r3]
  532. cmp r2, #0
  533. itt eq
  534. strexeq r2, r12, [r3]
  535. cmpeq r2, #0
  536. bne .Lloop
  537. // do the job
  538. ldr r2, [r0]
  539. add r1, r1, r2
  540. str r1, [r0]
  541. mov r0, r2
  542. // unlock and return
  543. mov r2, #0
  544. str r2, [r3]
  545. mov pc, lr
  546. .Lfpc_system_lock:
  547. .long fpc_system_lock
  548. end;
  549. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  550. asm
  551. // lock
  552. ldr r12, .Lfpc_system_lock
  553. mov r3, #1
  554. .Lloop:
  555. ldrex r2, [r12]
  556. cmp r2, #0
  557. itt eq
  558. strexeq r2, r1, [r12]
  559. cmpeq r2, #0
  560. bne .Lloop
  561. // do the job
  562. ldr r3, [r0]
  563. cmp r3, r2
  564. it eq
  565. streq r1, [r0]
  566. mov r0, r3
  567. // unlock and return
  568. mov r3, #0
  569. str r3, [r12]
  570. mov pc, lr
  571. .Lfpc_system_lock:
  572. .long fpc_system_lock
  573. end;
  574. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  575. function declocked(var l: longint) : boolean; inline;
  576. begin
  577. Result:=InterLockedDecrement(l) = 0;
  578. end;
  579. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  580. procedure inclocked(var l: longint); inline;
  581. begin
  582. InterLockedIncrement(l);
  583. end;
  584. procedure fpc_cpucodeinit;
  585. begin
  586. {$ifdef FPC_SYSTEM_FPC_MOVE}
  587. cpu_has_edsp:=true;
  588. in_edsp_test:=true;
  589. asm
  590. mov r1,sp
  591. bic r0,r1,#7
  592. ldrd r0,[r0]
  593. end;
  594. in_edsp_test:=false;
  595. if cpu_has_edsp then
  596. moveproc:=@move_pld
  597. else
  598. moveproc:=@move_blended;
  599. {$endif FPC_SYSTEM_FPC_MOVE}
  600. end;