powerpc.inc 32 KB

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