powerpc.inc 29 KB

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