powerpc.inc 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2000-2001 by the Free Pascal development team.
  5. Portions Copyright (c) 2000 by Casey Duncan ([email protected])
  6. Processor dependent implementation for the system unit for
  7. PowerPC
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. {****************************************************************************
  15. PowerPC specific stuff
  16. ****************************************************************************}
  17. { This function is never called directly, it's a dummy to hold the register save/
  18. load subroutines
  19. }
  20. {$ifndef MACOS}
  21. procedure saverestorereg;assembler;
  22. asm
  23. { exit }
  24. .global _restfpr_14_x
  25. _restfpr_14_x: lfd f14, -144(r11)
  26. .global _restfpr_15_x
  27. _restfpr_15_x: lfd f15, -136(r11)
  28. .global _restfpr_16_x
  29. _restfpr_16_x: lfd f16, -128(r11)
  30. .global _restfpr_17_x
  31. _restfpr_17_x: lfd f17, -120(r11)
  32. .global _restfpr_18_x
  33. _restfpr_18_x: lfd f18, -112(r11)
  34. .global _restfpr_19_x
  35. _restfpr_19_x: lfd f19, -104(r11)
  36. .global _restfpr_20_x
  37. _restfpr_20_x: lfd f20, -96(r11)
  38. .global _restfpr_21_x
  39. _restfpr_21_x: lfd f21, -88(r11)
  40. .global _restfpr_22_x
  41. _restfpr_22_x: lfd f22, -80(r11)
  42. .global _restfpr_23_x
  43. _restfpr_23_x: lfd f23, -72(r11)
  44. .global _restfpr_24_x
  45. _restfpr_24_x: lfd f24, -64(r11)
  46. .global _restfpr_25_x
  47. _restfpr_25_x: lfd f25, -56(r11)
  48. .global _restfpr_26_x
  49. _restfpr_26_x: lfd f26, -48(r11)
  50. .global _restfpr_27_x
  51. _restfpr_27_x: lfd f27, -40(r11)
  52. .global _restfpr_28_x
  53. _restfpr_28_x: lfd f28, -32(r11)
  54. .global _restfpr_29_x
  55. _restfpr_29_x: lfd f29, -24(r11)
  56. .global _restfpr_30_x
  57. _restfpr_30_x: lfd f30, -16(r11)
  58. .global _restfpr_31_x
  59. _restfpr_31_x: lwz r0, 4(r11)
  60. lfd f31, -8(r11)
  61. mtlr r0
  62. ori r1, r11, 0
  63. blr
  64. { exit with restoring lr }
  65. .global _restfpr_14_l
  66. _restfpr_14_l: lfd f14, -144(r11)
  67. .global _restfpr_15_l
  68. _restfpr_15_l: lfd f15, -136(r11)
  69. .global _restfpr_16_l
  70. _restfpr_16_l: lfd f16, -128(r11)
  71. .global _restfpr_17_l
  72. _restfpr_17_l: lfd f17, -120(r11)
  73. .global _restfpr_18_l
  74. _restfpr_18_l: lfd f18, -112(r11)
  75. .global _restfpr_19_l
  76. _restfpr_19_l: lfd f19, -104(r11)
  77. .global _restfpr_20_l
  78. _restfpr_20_l: lfd f20, -96(r11)
  79. .global _restfpr_21_l
  80. _restfpr_21_l: lfd f21, -88(r11)
  81. .global _restfpr_22_l
  82. _restfpr_22_l: lfd f22, -80(r11)
  83. .global _restfpr_23_l
  84. _restfpr_23_l: lfd f23, -72(r11)
  85. .global _restfpr_24_l
  86. _restfpr_24_l: lfd f24, -64(r11)
  87. .global _restfpr_25_l
  88. _restfpr_25_l: lfd f25, -56(r11)
  89. .global _restfpr_26_l
  90. _restfpr_26_l: lfd f26, -48(r11)
  91. .global _restfpr_27_l
  92. _restfpr_27_l: lfd f27, -40(r11)
  93. .global _restfpr_28_l
  94. _restfpr_28_l: lfd f28, -32(r11)
  95. .global _restfpr_29_l
  96. _restfpr_29_l: lfd f29, -24(r11)
  97. .global _restfpr_30_l
  98. _restfpr_30_l: lfd f30, -16(r11)
  99. .global _restfpr_31_l
  100. _restfpr_31_l: lwz r0, 4(r11)
  101. lfd f31, -8(r11)
  102. mtlr r0
  103. ori r1, r11, 0
  104. blr
  105. end;
  106. {$endif MACOS}
  107. {****************************************************************************
  108. Move / Fill
  109. ****************************************************************************}
  110. {$define FPC_SYSTEM_HAS_MOVE}
  111. procedure Move(const source;var dest;count:longint);assembler;
  112. asm
  113. { count <= 0 ? }
  114. cmpwi cr0,r5,0
  115. { check if we have to do the move backwards because of overlap }
  116. sub r10,r4,r3
  117. { carry := boolean(dest-source < count) = boolean(overlap) }
  118. subc r10,r10,r5
  119. { count < 15 ? (to decide whether we will move dwords or bytes }
  120. cmpwi cr1,r5,15
  121. { if overlap, then r10 := -1 else r10 := 0 }
  122. subfe r10,r10,r10
  123. { count < 39 ? (32 + max. alignment (7) }
  124. cmpwi cr7,r5,39
  125. { if count <= 0, stop }
  126. ble cr0,LMoveDone
  127. { load the begin of the source in the data cache }
  128. dcbt 0,r3
  129. { and the dest as well }
  130. dcbst 0,r4
  131. { if overlap, then r0 := count else r0 := 0 }
  132. and r0,r5,r10
  133. { if overlap, then point source and dest to the end }
  134. add r3,r3,r0
  135. add r4,r4,r0
  136. { if overlap, then r0 := 6, else r6 := -1 }
  137. not r6,r10
  138. { if overlap, then r10 := -2, else r10 := 0 }
  139. slwi r10,r10,1
  140. { if overlap, then r10 := -1, else r10 := 1 }
  141. addi r10,r10,1
  142. { if count < 15, copy everything byte by byte }
  143. blt cr1,LMoveBytes
  144. { if no overlap, then source/dest += -1, otherwise they stay }
  145. { After the next instruction, r3/r4 + r10 = next position to }
  146. { load/store from/to }
  147. add r3,r3,r6
  148. add r4,r4,r6
  149. { otherwise, guarantee 4 byte alignment for dest for starters }
  150. LMove4ByteAlignLoop:
  151. lbzux r0,r3,r10
  152. stbux r0,r4,r10
  153. { is dest now 4 aligned? }
  154. andi. r0,r4,3
  155. subi r5,r5,1
  156. { while not aligned, continue }
  157. bne cr0,LMove4ByteAlignLoop
  158. { check for 8 byte alignment }
  159. andi. r0,r4,7
  160. { we are going to copy one byte again (the one at the newly }
  161. { aligned address), so increase count byte 1 }
  162. addi r5,r5,1
  163. { count div 4 for number of dwords to copy }
  164. srwi r0,r5,2
  165. { if 11 <= count < 39, copy using dwords }
  166. blt cr7,LMoveDWords
  167. { multiply the update count with 4 }
  168. slwi r10,r10,2
  169. slwi r6,r6,2
  170. { and adapt the source and dest }
  171. add r3,r3,r6
  172. add r4,r4,r6
  173. beq cr0,L8BytesAligned
  174. { count >= 39 -> align to 8 byte boundary and then use the FPU }
  175. { since we're already at 4 byte alignment, use dword store }
  176. lwzux r0,r3,r10
  177. stwux r0,r4,r10
  178. subi r5,r5,4
  179. L8BytesAligned:
  180. { count div 32 ( >= 1, since count was >=39 }
  181. srwi r0,r5,5
  182. { remainder }
  183. andi. r5,r5,31
  184. { to decide if we will do some dword stores (instead of only }
  185. { byte stores) afterwards or not }
  186. cmpwi cr1,r5,11
  187. mtctr r0
  188. { r0 := count div 4, will be moved to ctr when copying dwords }
  189. srwi r0,r5,2
  190. { adjust the update count: it will now be 8 or -8 depending on overlap }
  191. slwi r10,r10,1
  192. { adjust source and dest pointers: because of the above loop, dest is now }
  193. { aligned to 8 bytes. So if we add r6 we will still have an 8 bytes }
  194. { aligned address) }
  195. add r3,r3,r6
  196. add r4,r4,r6
  197. slwi r6,r6,1
  198. LMove32ByteLoop:
  199. lfdux f0,r3,r10
  200. lfdux f1,r3,r10
  201. lfdux f2,r3,r10
  202. lfdux f3,r3,r10
  203. stfdux f0,r4,r10
  204. stfdux f1,r4,r10
  205. stfdux f2,r4,r10
  206. stfdux f3,r4,r10
  207. bdnz LMove32ByteLoop
  208. { cr0*4+eq is true if "count and 31" = 0 }
  209. beq cr0,LMoveDone
  210. { make r10 again -1 or 1, but first adjust source/dest pointers }
  211. sub r3,r3,r6
  212. sub r4,r4,r6
  213. srawi r10,r10,3
  214. srawi r6,r6,3
  215. { cr1 contains whether count <= 11 }
  216. ble cr1,LMoveBytes
  217. LMoveDWords:
  218. mtctr r0
  219. andi. r5,r5,3
  220. { r10 * 4 }
  221. slwi r10,r10,2
  222. slwi r6,r6,2
  223. add r3,r3,r6
  224. add r4,r4,r6
  225. LMoveDWordsLoop:
  226. lwzux r0,r3,r10
  227. stwux r0,r4,r10
  228. bdnz LMoveDWordsLoop
  229. beq cr0,LMoveDone
  230. { make r10 again -1 or 1 }
  231. sub r3,r3,r6
  232. sub r4,r4,r6
  233. srawi r10,r10,2
  234. srawi r6,r6,2
  235. LMoveBytes:
  236. add r3,r3,r6
  237. add r4,r4,r6
  238. mtctr r5
  239. LMoveBytesLoop:
  240. lbzux r0,r3,r10
  241. stbux r0,r4,r10
  242. bdnz LMoveBytesLoop
  243. LMoveDone:
  244. end ['R0','R3','R4','R5','R10','F0','F11','F12','F13','CTR','CR0','CR1','CR7'];
  245. {$define FPC_SYSTEM_HAS_FILLCHAR}
  246. Procedure FillChar(var x;count:longint;value:byte);assembler;
  247. { input: x in r3, count in r4, value in r5 }
  248. {$ifndef ABI_AIX}
  249. { in the AIX ABI, we can use te red zone for temp storage, otherwise we have }
  250. { to explicitely allocate room }
  251. var
  252. temp : packed record
  253. case byte of
  254. 0: (l1,l2: longint);
  255. 1: (d: double);
  256. end;
  257. {$endif ABI_AIX}
  258. asm
  259. { no bytes? }
  260. cmpwi cr6,r4,0
  261. { less than 15 bytes? }
  262. cmpwi cr7,r4,15
  263. { less than 63 bytes? }
  264. cmpwi cr1,r4,63
  265. { fill r5 with ValueValueValueValue }
  266. rlwimi r5,r5,8,16,23
  267. { setup for aligning x to multiple of 4}
  268. rlwinm r10,r3,0,31-2+1,31
  269. rlwimi r5,r5,16,0,15
  270. ble cr6,LFillCharDone
  271. { get the start of the data in the cache (and mark it as "will be }
  272. { modified") }
  273. dcbst 0,r3
  274. subfic r10,r10,4
  275. blt cr7,LFillCharVerySmall
  276. { just store 4 bytes instead of using a loop to align (there are }
  277. { plenty of other instructions now to keep the processor busy }
  278. { while it handles the (possibly unaligned) store) }
  279. stw r5,0(r3)
  280. { r3 := align(r3,4) }
  281. add r3,r3,r10
  282. { decrease count with number of bytes already stored }
  283. sub r4,r4,r10
  284. blt cr1,LFillCharSmall
  285. { if we have to fill with 0 (which happens a lot), we can simply use }
  286. { dcbz for the most part, which is very fast, so make a special case }
  287. { for that }
  288. cmplwi cr1,r5,0
  289. { align to a multiple of 32 (and immediately check whether we aren't }
  290. { already 32 byte aligned) }
  291. rlwinm. r10,r3,0,31-5+1,31
  292. { setup r3 for using update forms of store instructions }
  293. subi r3,r3,4
  294. { get number of bytes to store }
  295. subfic r10,r10,32
  296. { if already 32byte aligned, skip align loop }
  297. beq L32ByteAlignLoopDone
  298. { substract from the total count }
  299. sub r4,r4,r10
  300. L32ByteAlignLoop:
  301. { we were already aligned to 4 byres, so this will count down to }
  302. { exactly 0 }
  303. subic. r10,r10,4
  304. stwu r5,4(r3)
  305. bne L32ByteAlignLoop
  306. L32ByteAlignLoopDone:
  307. { get the amount of 32 byte blocks }
  308. srwi r10,r4,5
  309. { and keep the rest in r4 (recording whether there is any rest) }
  310. rlwinm. r4,r4,0,31-5+1,31
  311. { move to ctr }
  312. mtctr r10
  313. { check how many rest there is (to decide whether we'll use }
  314. { FillCharSmall or FillCharVerySmall) }
  315. cmpl cr7,r4,11
  316. { if filling with zero, only use dcbz }
  317. bne cr1, LFillCharNoZero
  318. { make r3 point again to the actual store position }
  319. addi r3,r3,4
  320. LFillCharDCBZLoop:
  321. dcbz 0,r3
  322. addi r3,r3,32
  323. bdnz LFillCharDCBZLoop
  324. { if there was no rest, we're finished }
  325. beq LFillCharDone
  326. b LFillCharVerySmall
  327. LFillCharNoZero:
  328. {$ifdef ABI_AIX}
  329. stw r5,0(sp)
  330. stw r5,4(sp)
  331. lfd f0,0(sp)
  332. {$else ABI_AIX}
  333. stw r5,temp
  334. stw r5,4+temp
  335. lfd f0,temp
  336. {$endif ABI_AIX}
  337. { make r3 point to address-8, so we're able to use fp double stores }
  338. { with update (it's already -4 now) }
  339. subi r3,r3,4
  340. { load r10 with 8, so that dcbz uses the correct address }
  341. li r10, 8
  342. LFillChar32ByteLoop:
  343. dcbz r3,r10
  344. stfdu f0,8(r3)
  345. stfdu f0,8(r3)
  346. stfdu f0,8(r3)
  347. stfdu f0,8(r3)
  348. bdnz LFillChar32ByteLoop
  349. { if there was no rest, we're finished }
  350. beq LFillCharDone
  351. { make r3 point again to the actual next byte that must be written }
  352. addi r3,r3,8
  353. b LFillCharVerySmall
  354. LFillCharSmall:
  355. { when we arrive here, we're already 4 byte aligned }
  356. { get count div 4 to store dwords }
  357. srwi r10,r4,2
  358. { get ready for use of update stores }
  359. subi r3,r3,4
  360. mtctr r10
  361. rlwinm. r4,r4,0,31-2+1,31
  362. LFillCharSmallLoop:
  363. stwu r5,4(r3)
  364. bdnz LFillCharSmallLoop
  365. { if nothing left, stop }
  366. beq LFillCharDone
  367. { get ready to store bytes }
  368. addi r3,r3,4
  369. LFillCharVerySmall:
  370. mtctr r4
  371. subi r3,r3,1
  372. LFillCharVerySmallLoop:
  373. stbu r5,1(r3)
  374. bdnz LFillCharVerySmallLoop
  375. LFillCharDone:
  376. end;
  377. {$define FPC_SYSTEM_HAS_FILLDWORD}
  378. procedure filldword(var x;count : longint;value : dword);
  379. assembler;
  380. asm
  381. { registers:
  382. r3 x
  383. r4 count
  384. r5 value
  385. }
  386. cmpwi cr0,r3,0
  387. mtctr r4
  388. subi r3,r3,4
  389. ble LFillDWordEnd //if count<=0 Then Exit
  390. LFillDWordLoop:
  391. stwu r5,4(r3)
  392. bdnz LFillDWordLoop
  393. LFillDWordEnd:
  394. end ['R3','R4','R5','CTR'];
  395. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  396. function IndexByte(const buf;len:longint;b:byte):longint; assembler;
  397. { input: r3 = buf, r4 = len, r5 = b }
  398. { output: r3 = position of b in buf (-1 if not found) }
  399. asm
  400. { load the begin of the buffer in the data cache }
  401. dcbt 0,r3
  402. cmplwi r4,0
  403. mtctr r4
  404. subi r10,r3,1
  405. mr r0,r3
  406. { assume not found }
  407. li r3,-1
  408. ble LIndexByteDone
  409. LIndexByteLoop:
  410. lbzu r9,1(r10)
  411. cmplw r9,r5
  412. bdnzf cr0*4+eq,LIndexByteLoop
  413. { r3 still contains -1 here }
  414. bne LIndexByteDone
  415. sub r3,r10,r0
  416. LIndexByteDone:
  417. end ['R0','R3','R9','R10','CR0','CTR'];
  418. {$define FPC_SYSTEM_HAS_INDEXWORD}
  419. function IndexWord(const buf;len:longint;b:word):longint; assembler;
  420. { input: r3 = buf, r4 = len, r5 = b }
  421. { output: r3 = position of b in buf (-1 if not found) }
  422. asm
  423. { load the begin of the buffer in the data cache }
  424. dcbt 0,r3
  425. cmplwi r4,0
  426. mtctr r4
  427. subi r10,r3,2
  428. mr r0,r3
  429. { assume not found }
  430. li r3,-1
  431. ble LIndexWordDone
  432. LIndexWordLoop:
  433. lhzu r9,2(r10)
  434. cmplw r9,r5
  435. bdnzf cr0*4+eq,LIndexWordLoop
  436. { r3 still contains -1 here }
  437. bne LIndexWordDone
  438. sub r3,r10,r0
  439. LIndexWordDone:
  440. end ['R0','R3','R9','R10','CR0','CTR'];
  441. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  442. function IndexDWord(const buf;len:longint;b:DWord):longint; assembler;
  443. { input: r3 = buf, r4 = len, r5 = b }
  444. { output: r3 = position of b in buf (-1 if not found) }
  445. asm
  446. { load the begin of the buffer in the data cache }
  447. dcbt 0,r3
  448. cmplwi r4,0
  449. mtctr r4
  450. subi r10,r3,4
  451. mr r0,r3
  452. { assume not found }
  453. li r3,-1
  454. ble LIndexDWordDone
  455. LIndexDWordLoop:
  456. lwzu r9,4(r10)
  457. cmplw r9,r5
  458. bdnzf cr0*4+eq, LIndexDWordLoop
  459. { r3 still contains -1 here }
  460. bne LIndexDWordDone
  461. sub r3,r10,r0
  462. LIndexDWordDone:
  463. end ['R0','R3','R9','R10','CR0','CTR'];
  464. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  465. function CompareByte(const buf1,buf2;len:longint):longint; assembler;
  466. { input: r3 = buf1, r4 = buf2, r5 = len }
  467. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  468. { note: almost direct copy of strlcomp() from strings.inc }
  469. asm
  470. { load the begin of the first buffer in the data cache }
  471. dcbt 0,r3
  472. { use r0 instead of r3 for buf1 since r3 contains result }
  473. cmplwi r5,0
  474. mtctr r5
  475. subi r11,r3,1
  476. subi r4,r4,1
  477. li r3,0
  478. ble LCompByteDone
  479. LCompByteLoop:
  480. { load next chars }
  481. lbzu r9,1(r11)
  482. lbzu r10,1(r4)
  483. { calculate difference }
  484. sub. r3,r9,r10
  485. { if chars not equal or at the end, we're ready }
  486. bdnzt cr0*4+eq, LCompByteLoop
  487. LCompByteDone:
  488. end ['R0','R3','R4','R9','R10','R11','CR0','CTR'];
  489. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  490. function CompareWord(const buf1,buf2;len:longint):longint; assembler;
  491. { input: r3 = buf1, r4 = buf2, r5 = len }
  492. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  493. { note: almost direct copy of strlcomp() from strings.inc }
  494. asm
  495. { load the begin of the first buffer in the data cache }
  496. dcbt 0,r3
  497. { use r0 instead of r3 for buf1 since r3 contains result }
  498. cmplwi r5,0
  499. mtctr r5
  500. subi r11,r3,2
  501. subi r4,r4,2
  502. li r3,0
  503. ble LCompWordDone
  504. LCompWordLoop:
  505. { load next chars }
  506. lhzu r9,2(r11)
  507. lhzu r10,2(r4)
  508. { calculate difference }
  509. sub. r3,r9,r10
  510. { if chars not equal or at the end, we're ready }
  511. bdnzt cr0*4+eq, LCompWordLoop
  512. LCompWordDone:
  513. end ['R0','R3','R4','R9','R10','R11','CR0','CTR'];
  514. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  515. function CompareDWord(const buf1,buf2;len:longint):longint; assembler;
  516. { input: r3 = buf1, r4 = buf2, r5 = len }
  517. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  518. { note: almost direct copy of strlcomp() from strings.inc }
  519. asm
  520. { load the begin of the first buffer in the data cache }
  521. dcbt 0,r3
  522. { use r0 instead of r3 for buf1 since r3 contains result }
  523. cmplwi r5,0
  524. mtctr r5
  525. subi r11,r3,4
  526. subi r4,r4,4
  527. li r3,0
  528. ble LCompDWordDone
  529. LCompDWordLoop:
  530. { load next chars }
  531. lwzu r9,4(r11)
  532. lwzu r10,4(r4)
  533. { calculate difference }
  534. sub. r3,r9,r10
  535. { if chars not equal or at the end, we're ready }
  536. bdnzt cr0*4+eq, LCompDWordLoop
  537. LCompDWordDone:
  538. end ['R0','R3','R4','R9','R10','R11','CR0','CTR'];
  539. {$define FPC_SYSTEM_HAS_INDEXCHAR0}
  540. function IndexChar0(const buf;len:longint;b:Char):longint; assembler;
  541. { input: r3 = buf, r4 = len, r5 = b }
  542. { output: r3 = position of found position (-1 if not found) }
  543. asm
  544. { load the begin of the buffer in the data cache }
  545. dcbt 0,r3
  546. { length = 0? }
  547. cmplwi r4,0
  548. mtctr r4
  549. subi r9,r3,1
  550. subi r0,r3,1
  551. { assume not found }
  552. li r3,-1
  553. { if yes, do nothing }
  554. ble LIndexChar0Done
  555. LIndexChar0Loop:
  556. lbzu r10,1(r9)
  557. cmplwi cr1,r10,0
  558. cmplw r10,r5
  559. beq cr1,LIndexChar0Done
  560. bdnzf cr0*4+eq, LIndexChar0Loop
  561. bne LIndexChar0Done
  562. sub r3,r9,r0
  563. LIndexChar0Done:
  564. end ['R0','R3','R4','R9','R10','CR0','CTR'];
  565. {****************************************************************************
  566. String
  567. ****************************************************************************}
  568. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  569. function fpc_shortstr_to_shortstr(len:longint; const sstr: shortstring): shortstring; [public,alias: 'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  570. assembler;
  571. { input: r3: pointer to result, r4: len, r5: sstr }
  572. asm
  573. { load length source }
  574. lbz r10,0(r5)
  575. { load the begin of the dest buffer in the data cache }
  576. dcbtst 0,r3
  577. { put min(length(sstr),len) in r4 }
  578. subfc r7,r10,r4 { r0 := r4 - r10 }
  579. subfe r4,r4,r4 { if r3 >= r4 then r3' := 0 else r3' := -1 }
  580. and r7,r7,r4 { if r3 >= r4 then r3' := 0 else r3' := r3-r10 }
  581. add r4,r10,r7 { if r3 >= r4 then r3' := r10 else r3' := r3 }
  582. cmplwi r4,0
  583. { put length in ctr }
  584. mtctr r4
  585. stb r4,0(r3)
  586. beq LShortStrCopyDone
  587. LShortStrCopyLoop:
  588. lbzu r0,1(r5)
  589. stbu r0,1(r3)
  590. bdnz LShortStrCopyLoop
  591. LShortStrCopyDone:
  592. end ['R0','R3','R4','R5','R6','R7','R10','CR0','CTR'];
  593. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  594. {$ifdef interncopy}
  595. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN'];
  596. {$else}
  597. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY'];
  598. {$endif}
  599. assembler;
  600. { input: r3: len, r4: sstr, r5: dstr }
  601. asm
  602. { load length source }
  603. lbz r10,0(r4)
  604. { load the begin of the dest buffer in the data cache }
  605. dcbtst 0,r5
  606. { put min(length(sstr),len) in r3 }
  607. subc r0,r3,r10 { r0 := r3 - r10 }
  608. subfe r3,r3,r3 { if r3 >= r4 then r3' := 0 else r3' := -1 }
  609. and r3,r0,r3 { if r3 >= r4 then r3' := 0 else r3' := r3-r10 }
  610. add r3,r3,r10 { if r3 >= r4 then r3' := r10 else r3' := r3 }
  611. cmplwi r3,0
  612. { put length in ctr }
  613. mtctr r3
  614. stb r3,0(r5)
  615. beq LShortStrCopyDone2
  616. LShortStrCopyLoop2:
  617. lbzu r0,1(r4)
  618. stbu r0,1(r5)
  619. bdnz LShortStrCopyLoop2
  620. LShortStrCopyDone2:
  621. end ['R0','R3','R4','R5','R10','CR0','CTR'];
  622. (*
  623. !!! fast version like for the i386, but not called this way currently (JM)
  624. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  625. function fpc_shortstr_concat(const s1,s2: shortstring): shortstring; compilerproc;
  626. { expects that results (r3) contains a pointer to the current string and s1 }
  627. { (r4) a pointer to the one that has to be concatenated }
  628. assembler;
  629. asm
  630. { load length s1 }
  631. lbz r9, 0(r4)
  632. { load length result }
  633. lbz r10, 0(r3)
  634. { length 0? }
  635. cmplwi r10,0
  636. { go to last current character of result }
  637. add r4,r9,r4
  638. { calculate min(length(s1),255-length(result)) }
  639. subfic r9,r9,255
  640. subc r8,r9,r10 { r8 := r9 - r10 }
  641. subfe r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  642. and r9,r8,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r8 }
  643. add r9,r9,r10 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  644. { and concatenate }
  645. mtctr r9
  646. beq LShortStrConcatDone
  647. LShortStrConcatLoop:
  648. lbzu r10,1(r4)
  649. stbu r10,1(r3)
  650. bdnz LShortStrConcatLoop
  651. LShortStrConcatDone:
  652. end ['R3','R4','R8','R9','R10','CTR'];
  653. *)
  654. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  655. function fpc_shortstr_compare(const dstr,sstr:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  656. assembler;
  657. asm
  658. { load length sstr }
  659. lbz r9,0(r4)
  660. { load length dstr }
  661. lbz r10,0(r3)
  662. { save their difference for later and }
  663. { calculate min(length(sstr),length(dstr)) }
  664. subfc r7,r10,r9 { r0 := r9 - r10 }
  665. subfe r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  666. and r7,r7,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r8 }
  667. add r9,r10,r7 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  668. { first compare dwords (length/4) }
  669. srwi. r5,r9,2
  670. { keep length mod 4 for the ends }
  671. rlwinm r9,r9,0,30,31
  672. { already check whether length mod 4 = 0 }
  673. cmplwi cr1,r9,0
  674. { so we can load r3 with 0, in case the strings both have length 0 }
  675. mr r8,r3
  676. li r3, 0
  677. { length div 4 in ctr for loop }
  678. mtctr r5
  679. { if length < 3, goto byte comparing }
  680. beq LShortStrCompare1
  681. { setup for use of update forms of load/store with dwords }
  682. subi r4,r4,3
  683. subi r8,r8,3
  684. LShortStrCompare4Loop:
  685. lwzu r3,4(r4)
  686. lwzu r10,4(r8)
  687. sub. r3,r3,r10
  688. bdnzt cr0+eq,LShortStrCompare4Loop
  689. { r3 contains result if we stopped because of "ne" flag }
  690. bne LShortStrCompareDone
  691. { setup for use of update forms of load/store with bytes }
  692. addi r4,r4,3
  693. addi r8,r8,3
  694. LShortStrCompare1:
  695. { if comparelen mod 4 = 0, skip this and return the difference in }
  696. { lengths }
  697. beq cr1,LShortStrCompareLen
  698. mtctr r9
  699. LShortStrCompare1Loop:
  700. lbzu r3,1(r4)
  701. lbzu r10,1(r8)
  702. sub. r3,r3,r10
  703. bdnzt cr0+eq,LShortStrCompare1Loop
  704. bne LShortStrCompareDone
  705. LShortStrCompareLen:
  706. { also return result in flags, maybe we can use this in the CG }
  707. mr. r3,r3
  708. LShortStrCompareDone:
  709. end ['R0','R3','R4','R5','R6','R7','R8','R9','R10','CR0','CR1','CTR'];
  710. {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  711. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  712. assembler;
  713. {$include strpas.inc}
  714. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  715. function fpc_pchar_length(p:pchar):longint;assembler;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  716. {$include strlen.inc}
  717. {$define FPC_SYSTEM_HAS_GET_FRAME}
  718. function get_frame:pointer;assembler;
  719. asm
  720. { all abi's I know use r1 as stack pointer }
  721. mr r3, r1
  722. end ['R3'];
  723. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  724. function get_caller_addr(framebp:pointer):pointer;assembler;
  725. asm
  726. cmplwi r3,0
  727. beq Lcaller_addr_frame_null
  728. lwz r3,0(r3)
  729. cmplwi r3,0
  730. beq Lcaller_addr_frame_null
  731. lwz r3,4(r3)
  732. Lcaller_addr_frame_null:
  733. // !!!!!!! depends on ABI !!!!!!!!
  734. end ['R3'];
  735. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  736. function get_caller_frame(framebp:pointer):pointer;assembler;
  737. asm
  738. cmplwi r3,0
  739. beq Lcaller_frame_null
  740. lwz r3,0(r3)
  741. Lcaller_frame_null:
  742. end ['R3'];
  743. {$define FPC_SYSTEM_HAS_ABS_LONGINT}
  744. function abs(l:longint):longint; assembler;[internconst:in_const_abs];
  745. asm
  746. srawi r0,r3,31
  747. add r3,r0,r3
  748. xor r3,r3,r0
  749. end ['R0','R3'];
  750. {****************************************************************************
  751. Math
  752. ****************************************************************************}
  753. {$define FPC_SYSTEM_HAS_ODD_LONGINT}
  754. function odd(l:longint):boolean;assembler;[internconst:in_const_odd];
  755. asm
  756. rlwinm r3,r3,0,31,31
  757. end ['R3'];
  758. {$define FPC_SYSTEM_HAS_SQR_LONGINT}
  759. function sqr(l:longint):longint;assembler;[internconst:in_const_sqr];
  760. asm
  761. mullw r3,r3,r3
  762. end ['R3'];
  763. {$define FPC_SYSTEM_HAS_SPTR}
  764. Function Sptr : Longint;assembler;
  765. asm
  766. mr r3,r1
  767. end ['R3'];
  768. {****************************************************************************
  769. Str()
  770. ****************************************************************************}
  771. { int_str: generic implementation is used for now }
  772. {****************************************************************************
  773. Multithreading
  774. ****************************************************************************}
  775. { do a thread save inc/dec }
  776. {$define FPC_SYSTEM_HAS_DECLOCKED}
  777. function declocked(var l : longint) : boolean;assembler;
  778. { input: address of l in r3 }
  779. { output: boolean indicating whether l is zero after decrementing }
  780. asm
  781. LDecLockedLoop:
  782. lwarx r10,0,r3
  783. subi r10,r10,1
  784. stwcx. r10,0,r3
  785. bne- LDecLockedLoop
  786. cntlzw r3,r10
  787. srwi r3,r3,5
  788. end ['R3','R10'];
  789. {$define FPC_SYSTEM_HAS_INCLOCKED}
  790. procedure inclocked(var l : longint);assembler;
  791. asm
  792. LIncLockedLoop:
  793. lwarx r10,0,r3
  794. addi r10,r10,1
  795. stwcx. r10,0,r3
  796. bne- LDecLockedLoop
  797. end ['R3','R10'];
  798. {
  799. $Log$
  800. Revision 1.43 2003-05-12 19:39:33 jonas
  801. * fixed final fillchar error (tfillchr passes now)
  802. Revision 1.42 2003/05/12 19:00:50 jonas
  803. * fixed bug in fillchar
  804. Revision 1.41 2003/05/10 20:33:39 jonas
  805. * fixed get_caller_frame and get_caller_addr
  806. Revision 1.40 2003/05/10 17:33:06 jonas
  807. * final (? :) fix to move, passes new tests/test/tmove test
  808. Revision 1.39 2003/05/02 19:03:25 jonas
  809. * fixed some bugs in move()
  810. Revision 1.38 2003/04/27 16:24:44 jonas
  811. - disabled fpc_shortstr_concat because it's called differently than that
  812. routine is declared
  813. Revision 1.37 2003/04/26 20:37:17 jonas
  814. * fixed and re-enabled routines commented out by Florian :)
  815. Revision 1.36 2003/04/26 17:46:49 florian
  816. * commented out not working routines
  817. * reactivated assembler fillchar
  818. Revision 1.35 2003/04/26 17:35:15 jonas
  819. * fixed FillChar
  820. Revision 1.34 2003/04/26 12:05:10 florian
  821. * removed object/class helpers, the compiler uses the generic ones
  822. Revision 1.33 2003/04/26 11:55:52 florian
  823. * fixed newlines
  824. Revision 1.32 2003/04/23 21:04:48 florian
  825. * fixed fpc_shortstr_to_shortstr
  826. Revision 1.31 2003/03/17 14:30:11 peter
  827. * changed address parameter/return values to pointer instead
  828. of longint
  829. Revision 1.30 2003/03/12 19:21:29 jonas
  830. + implemented get_frame()
  831. * fixed bug in IndexDWord()
  832. Revision 1.29 2003/01/09 20:14:35 florian
  833. * fixed helper declarations
  834. Revision 1.28 2003/01/09 13:38:56 florian
  835. * dec/inclocked got defines
  836. Revision 1.27 2002/11/07 15:23:13 jonas
  837. * always use code that was between 'ifdef mt', since that define is
  838. deprecated now
  839. Revision 1.26 2002/11/01 13:27:55 jonas
  840. * changed "dcbtst r0,x" to "dcbtst 0,x"
  841. Revision 1.25 2002/10/23 15:26:00 olle
  842. * excluded saverestorereg for target macos
  843. Revision 1.24 2002/10/20 13:40:55 jonas
  844. * move/fill*/index*/comp* routines immediately exit if length is negative
  845. Revision 1.23 2002/10/17 10:12:50 jonas
  846. * fixed return value of declocked()
  847. Revision 1.22 2002/10/05 14:20:16 peter
  848. * fpc_pchar_length compilerproc and strlen alias
  849. Revision 1.21 2002/10/02 18:21:52 peter
  850. * Copy() changed to internal function calling compilerprocs
  851. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  852. new copy functions
  853. Revision 1.20 2002/09/10 21:30:34 jonas
  854. * disabled powerpc-specific fpc_shortstr_concat for now, it was
  855. completely wrong
  856. Revision 1.19 2002/09/10 17:47:20 jonas
  857. * fixed bug with concatting 0-length shortstrings
  858. Revision 1.18 2002/09/07 16:01:26 peter
  859. * old logs removed and tabs fixed
  860. Revision 1.17 2002/08/31 21:29:57 florian
  861. * several PC related fixes
  862. Revision 1.16 2002/08/31 16:08:36 florian
  863. * fixed undefined labels
  864. Revision 1.15 2002/08/31 13:11:11 florian
  865. * several fixes for Linux/PPC compilation
  866. Revision 1.14 2002/08/18 22:11:10 florian
  867. * fixed remaining assembler errors
  868. Revision 1.13 2002/08/18 21:37:48 florian
  869. * several errors in inline assembler fixed
  870. Revision 1.12 2002/08/10 17:14:36 jonas
  871. * various fixes, mostly changing the names of the modifies registers to
  872. upper case since that seems to be required by the compiler
  873. Revision 1.11 2002/07/30 17:29:53 florian
  874. + dummy setjmp and longjmp added
  875. + dummy implemtation of the destructor helper
  876. Revision 1.10 2002/07/28 21:39:29 florian
  877. * made abs a compiler proc if it is generic
  878. Revision 1.9 2002/07/28 20:43:49 florian
  879. * several fixes for linux/powerpc
  880. * several fixes to MT
  881. Revision 1.8 2002/07/26 15:45:56 florian
  882. * changed multi threading define: it's MT instead of MTRTL
  883. }