powerpc.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 f13,r3,r10
  102. lfdux f12,r3,r10
  103. lfdux f11,r3,r10
  104. lfdux f0,r3,r10
  105. stfdux f13,r4,r10
  106. stfdux f12,r4,r10
  107. stfdux f11,r4,r10
  108. stfdux f0,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_FILLWORD}
  277. procedure fillword(var x;count : longint;value : word);
  278. begin
  279. { registers:
  280. r3 x
  281. r4 count
  282. r5 value
  283. r13 value.value
  284. r14 ptr to dest word
  285. r15 increment 1
  286. r16 increment 2
  287. r17 scratch
  288. r18 scratch
  289. f1 value.value.value.value
  290. }
  291. asm
  292. cmpwi cr0,r3,0
  293. andi r17,r4,$3
  294. srwi r18,r4,1 //r18:=count div 2
  295. mr r13,r3
  296. li r14,4
  297. ble .FillWordEnd //if count<=0 Then Exit
  298. .FillWordLoop:
  299. stwux r5,r13,r14
  300. bdnz .FillWordLoop
  301. .FillWordEnd:
  302. end [r13,r14,ctr]
  303. end;
  304. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  305. function IndexByte(var buf;len:longint;b:byte):longint; assembler;
  306. { input: r3 = buf, r4 = len, r5 = b }
  307. { output: r3 = position of b in buf (-1 if not found) }
  308. asm
  309. { load the begin of the buffer in the data cache }
  310. dcbt 0,r3
  311. cmplwi r4,0
  312. mtctr r4
  313. subi r10,r3,1
  314. mr r0,r3
  315. { assume not found }
  316. li r3,-1
  317. beq LIndexByteDone
  318. LIndexByteLoop:
  319. lbzu r9,1(r10)
  320. cmplw r9,r5
  321. bdnzf cr0*4+eq,LIndexByteLoop
  322. { r3 still contains -1 here }
  323. bne LIndexByteDone
  324. sub r3,r10,r0
  325. LIndexByteDone:
  326. end ['r0','r3','r9','r10','cr0','ctr'];
  327. {$define FPC_SYSTEM_HAS_INDEXWORD}
  328. function IndexWord(var buf;len:longint;b:word):longint; assembler;
  329. { input: r3 = buf, r4 = len, r5 = b }
  330. { output: r3 = position of b in buf (-1 if not found) }
  331. asm
  332. { load the begin of the buffer in the data cache }
  333. dcbt 0,r3
  334. cmplwi r4,0
  335. mtctr r4
  336. subi r10,r3,2
  337. mr r0,r3
  338. { assume not found }
  339. li r3,-1
  340. beq LIndexWordDone
  341. LIndexWordLoop:
  342. lhzu r9,2(r10)
  343. cmplw r9,r5
  344. bdnzf cr0*4+eq,LIndexWordLoop
  345. { r3 still contains -1 here }
  346. bne LIndexWordDone
  347. sub r3,r10,r0
  348. LIndexWordDone:
  349. end ['r0','r3','r9','r10','cr0','ctr'];
  350. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  351. function IndexDWord(var buf;len:longint;b:DWord):longint; assembler;
  352. { input: r3 = buf, r4 = len, r5 = b }
  353. { output: r3 = position of b in buf (-1 if not found) }
  354. asm
  355. { load the begin of the buffer in the data cache }
  356. dcbt 0,r3
  357. cmplwi r4,0
  358. mtctr r4
  359. subi r10,r3,4
  360. mr r0,r3
  361. { assume not found }
  362. li r3,-1
  363. beq LIndexDWordDone
  364. LIndexDWordLoop:
  365. lwzu r9,4(r30)
  366. cmplw r9,r5
  367. bdnzf cr0*4+eq, LIndexDWordLoop
  368. { r3 still contains -1 here }
  369. bne LIndexDWordDone
  370. sub r3,r10,r0
  371. LIndexDWordDone:
  372. end ['r0','r3','r9','r10','cr0','ctr'];
  373. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  374. function CompareByte(var buf1,buf2;len:longint):longint; assembler;
  375. { input: r3 = buf1, r4 = buf2, r5 = len }
  376. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  377. { note: almost direct copy of strlcomp() from strings.inc }
  378. asm
  379. { load the begin of the first buffer in the data cache }
  380. dcbt 0,r3
  381. { use r0 instead of r3 for buf1 since r3 contains result }
  382. cmplwi r5,0
  383. mtctr r5
  384. subi r0,r3,1
  385. subi r4,r4,1
  386. li r3,0
  387. beq LCompByteDone
  388. LCompByteLoop:
  389. { load next chars }
  390. lbzu r9,1(r0)
  391. lbzu r10,1(r4)
  392. { calculate difference }
  393. sub. r3,r9,r10
  394. { if chars not equal or at the end, we're ready }
  395. bdnzt cr0*4+eq, LCompByteLoop
  396. LCompByteDone:
  397. end ['r0','r3','r4','r9','r10','cr0','ctr'];
  398. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  399. function CompareWord(var buf1,buf2;len:longint):longint; assembler;
  400. { input: r3 = buf1, r4 = buf2, r5 = len }
  401. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  402. { note: almost direct copy of strlcomp() from strings.inc }
  403. asm
  404. { load the begin of the first buffer in the data cache }
  405. dcbt 0,r3
  406. { use r0 instead of r3 for buf1 since r3 contains result }
  407. cmplwi r5,0
  408. mtctr r5
  409. subi r0,r3,2
  410. subi r4,r4,2
  411. li r3,0
  412. beq LCompWordDone
  413. LCompWordLoop:
  414. { load next chars }
  415. lhzu r9,2(r0)
  416. lhzu r10,2(r4)
  417. { calculate difference }
  418. sub. r3,r9,r10
  419. { if chars not equal or at the end, we're ready }
  420. bdnzt cr0*4+eq, LCompWordLoop
  421. LCompWordDone:
  422. end ['r0','r3','r4','r9','r10','cr0','ctr'];
  423. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  424. function CompareDWord(var buf1,buf2;len:longint):longint; assembler;
  425. { input: r3 = buf1, r4 = buf2, r5 = len }
  426. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  427. { note: almost direct copy of strlcomp() from strings.inc }
  428. asm
  429. { load the begin of the first buffer in the data cache }
  430. dcbt 0,r3
  431. { use r0 instead of r3 for buf1 since r3 contains result }
  432. cmplwi r5,0
  433. mtctr r5
  434. subi r0,r3,4
  435. subi r4,r4,4
  436. li r3,0
  437. beq LCompDWordDone
  438. LCompDWordLoop:
  439. { load next chars }
  440. lwzu r9,4(r0)
  441. lwzu r10,4(r4)
  442. { calculate difference }
  443. sub. r3,r9,r10
  444. { if chars not equal or at the end, we're ready }
  445. bdnzt cr0*4+eq, LCompDWordLoop
  446. LCompDWordDone:
  447. end ['r0','r3','r4','r9','r10','cr0','ctr'];
  448. {$define FPC_SYSTEM_HAS_INDEXCHAR0}
  449. function IndexChar0(var buf;len:longint;b:Char):longint; assembler;
  450. { input: r3 = buf, r4 = len, r5 = b }
  451. { output: r3 = position of found position (-1 if not found) }
  452. asm
  453. { load the begin of the buffer in the data cache }
  454. dcbt 0,r3
  455. { length = 0? }
  456. cmplwi r4,0
  457. mtctr r4
  458. subi r9,r3,1
  459. mr r0,r9
  460. { assume not found }
  461. li r3,-1
  462. { if yes, do nothing }
  463. beq LIndexChar0Done
  464. subi r3,r3,1
  465. LIndexChar0Loop:
  466. lbzu r10,1(r9)
  467. cmplwi cr1,r10,0
  468. cmplw r10,r5
  469. beq cr1,LIndexChar0Done
  470. bdnzf cr0*4+eq, LIndexChar0Loop
  471. bne LIndexChar0Done
  472. sub r3,r9,r0
  473. LIndexChar0Done:
  474. end ['r0','r3','r4','r9','r10','cr0','ctr'];
  475. {****************************************************************************
  476. Object Helpers
  477. ****************************************************************************}
  478. {define FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  479. (*
  480. use generic implementation for now
  481. procedure fpc_help_constructor; assembler; [public,alias:'FPC_HELP_CONSTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  482. *)
  483. {$define FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  484. procedure fpc_help_fail;assembler;[public,alias:'FPC_HELP_FAIL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  485. assembler
  486. asm
  487. !!!!!!!!!!!
  488. end;
  489. {$define FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  490. (*
  491. use generic implementation for now
  492. procedure fpc_help_destructor;assembler;[public,alias:'FPC_HELP_DESTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  493. *)
  494. {$define FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  495. procedure fpc_new_class;assembler;[public,alias:'FPC_NEW_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  496. assembler;
  497. asm
  498. !!!!!!!!!!!
  499. end;
  500. {$define FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  501. procedure fpc_dispose_class;assembler;[public,alias:'FPC_DISPOSE_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  502. assembler;
  503. asm
  504. !!!!!!!!!!!
  505. end;
  506. {$define FPC_SYSTEM_HAS_FPC_HELP_FAIL_CLASS}
  507. procedure fpc_help_fail_class;assembler;[public,alias:'FPC_HELP_FAIL_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  508. { a non zero class must allways be disposed
  509. VMT is allways at pos 0 }
  510. assembler;
  511. asm
  512. !!!!!!!!!!!
  513. end;
  514. {define FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  515. { we want the stack for debugging !! PM }
  516. (*
  517. use generic implementation for now
  518. procedure fpc_check_object(obj : pointer);[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  519. *)
  520. {define FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  521. (*
  522. use generic implementation for now
  523. procedure fpc_check_object_ext;assembler;[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  524. *)
  525. {****************************************************************************
  526. String
  527. ****************************************************************************}
  528. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  529. function fpc_shortstr_to_shortstr(len:longint; const sstr: shortstring): shortstring; [public,alias: 'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  530. assembler;
  531. { input: r3: pointer to result, r4: len, r5: sstr }
  532. asm
  533. { load length source }
  534. lbz r10,0(r5)
  535. { load the begin of the dest buffer in the data cache }
  536. dcbtst r0,r3
  537. { put min(length(sstr),len) in r3 }
  538. subc r0,r4,r10 { r0 := r3 - r10 }
  539. subme r4,r4,r4 { if r3 >= r4 then r3' := 0 else r3' := -1 }
  540. and r4,r0,r4 { if r3 >= r4 then r3' := 0 else r3' := r3-r10 }
  541. add r4,r4,r10 { if r3 >= r4 then r3' := r10 else r3' := r3 }
  542. cmplwi r4,0
  543. { put length in ctr }
  544. mtctr r4
  545. stb r4,0(r3)
  546. beq LShortStrCopyDone
  547. LShortStrCopyLoop:
  548. lbzu r0,1(r5)
  549. stbu r0,1(r3)
  550. bdnz LShortStrCopyLoop
  551. end ['r0','r3','r4','r5','r10','cr0','ctr'];
  552. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  553. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY'];
  554. assembler;
  555. { input: r3: len, r4: sstr, r5: dstr }
  556. asm
  557. { load length source }
  558. lbz r10,0(r4)
  559. { load the begin of the dest buffer in the data cache }
  560. dcbtst r0,r5
  561. { put min(length(sstr),len) in r3 }
  562. subc r0,r3,r10 { r0 := r3 - r10 }
  563. subme r3,r3,r3 { if r3 >= r4 then r3' := 0 else r3' := -1 }
  564. and r3,r0,r3 { if r3 >= r4 then r3' := 0 else r3' := r3-r10 }
  565. add r3,r3,r10 { if r3 >= r4 then r3' := r10 else r3' := r3 }
  566. cmplwi r3,0
  567. { put length in ctr }
  568. mtctr r3
  569. stb r3,0(r5)
  570. beq LShortStrCopyDone
  571. LShortStrCopyLoop:
  572. lbzu r0,1(r4)
  573. stbu r0,1(r5)
  574. bdnz LShortStrCopyLoop
  575. end ['r0','r3','r4','r5','r10','cr0','ctr'];
  576. function fpc_shortstr_concat(const s1: shortstring): shortstring; compilerproc;
  577. { expects that results (r3) contains a pointer to the current string and s1 }
  578. { (r4) a pointer to the one that has to be concatenated }
  579. assembler;
  580. asm
  581. { load length s1 }
  582. lbz r9, 0(r4)
  583. { load length result }
  584. lbz r10, 0(r3)
  585. { go to last current character of result }
  586. add r4,r9,r4
  587. { calculate min(length(s1),255-length(result)) }
  588. subfic r9,r9,255
  589. subc r8,r9,r10 { r8 := r9 - r10 }
  590. subme r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  591. and r9,r8,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r8 }
  592. add r9,r9,r10 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  593. { and concatenate }
  594. mtctr r9
  595. LShortStrConcatLoop:
  596. lbzu r10,1(r4)
  597. stbu r10,1(r3)
  598. bdnz LShortStrConcatLoop
  599. end ['r3','r4','r8','r9','r10','ctr'];
  600. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  601. function fpc_shortstr_compare(const dstr,sstr:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  602. assembler;
  603. asm
  604. { load length sstr }
  605. lbz r9,0(r4)
  606. { load length dstr }
  607. lbz r10,0(r3)
  608. { save their difference for later and }
  609. { calculate min(length(sstr),length(dstr)) }
  610. subc r0,r9,r10 { r0 := r9 - r10 }
  611. subme r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  612. and r9,r0,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r8 }
  613. add r9,r9,r10 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  614. { first compare dwords (length/4) }
  615. srwi. r8,r9,2
  616. { keep length mod 4 for the ends }
  617. rlwinm r9,r9,0,30,31
  618. { already check whether length mod 4 = 0 }
  619. cmplwi cr1,r9,0
  620. { length div 4 in ctr for loop }
  621. mtctr r8
  622. { if length < 3, goto byte comparing }
  623. beq LShortStrCompare1
  624. { setup for use of update forms of load/store with dwords }
  625. subi r4,r4,3
  626. subi r8,r3,3
  627. LShortStrCompare4Loop:
  628. lwzu r3,4(r4)
  629. lwzu r10,4(r8)
  630. sub. r3,r3,r10
  631. bdnzt cr0+eq,LShortStrCompare4Loop
  632. { r3 contains result if we stopped because of "ne" flag }
  633. bne LShortStrCompareDone
  634. { setup for use of update forms of load/store with bytes }
  635. addi r4,r4,3
  636. addi r8,r8,3
  637. LShortStrCompare1:
  638. { if comparelen mod 4 = 0, skip this and return the difference in }
  639. { lengths }
  640. beq cr1,LShortStrCompareLen
  641. LShortStrCompare1Loop:
  642. lbzu r3,1(r4)
  643. lbzu r10,1(r8)
  644. sub. r3,r3,r10
  645. bdnzt cr0+eq,LShortStrCompare4Loop
  646. bne LShortStrCompareDone
  647. LShortStrCompareLen:
  648. { also return result in flags, maybe we can use this in the CG }
  649. mr. r3,r0
  650. LShortStrCompareDone:
  651. end ['r0','r3','r4','r8','r9','r10','cr0','cr1','ctr'];
  652. {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  653. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  654. assembler;
  655. {$include strpas.inc}
  656. {$define FPC_SYSTEM_HAS_STRLEN}
  657. function strlen(p:pchar):longint;assembler;
  658. {$include strlen.inc}
  659. {$define FPC_SYSTEM_HAS_GET_FRAME}
  660. function get_frame:longint;assembler;
  661. asm
  662. !!!!!!! depends on ABI !!!!!!!!
  663. end ['r3'];
  664. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  665. function get_caller_addr(framebp:longint):longint;assembler;
  666. asm
  667. !!!!!!! depends on ABI !!!!!!!!
  668. end ['r3'];
  669. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  670. function get_caller_frame(framebp:longint):longint;assembler;
  671. asm
  672. !!!!!!! depends on ABI !!!!!!!!
  673. end ['r3'];
  674. {$define FPC_SYSTEM_HAS_ABS_LONGINT}
  675. function abs(l:longint):longint; assembler;[internconst:in_const_abs];
  676. asm
  677. srawi r0,r3,31
  678. add r3,r0,r3
  679. xor r3,r3,r0
  680. end ['r0','r3'];
  681. {****************************************************************************
  682. Math
  683. ****************************************************************************}
  684. {$define FPC_SYSTEM_HAS_ODD_LONGINT}
  685. function odd(l:longint):boolean;assembler;[internconst:in_const_odd];
  686. asm
  687. rlwinm r3,r3,0,31,31
  688. end ['r3'];
  689. {$define FPC_SYSTEM_HAS_SQR_LONGINT}
  690. function sqr(l:longint):longint;assembler;[internconst:in_const_sqr];
  691. asm
  692. mullw r3,r3,r3
  693. end ['r3'];
  694. {$define FPC_SYSTEM_HAS_SPTR}
  695. Function Sptr : Longint;assembler;
  696. asm
  697. mr r3,sp
  698. end ['r3'];
  699. {****************************************************************************
  700. Str()
  701. ****************************************************************************}
  702. { int_str: generic implementation is used for now }
  703. {****************************************************************************
  704. Multithreading
  705. ****************************************************************************}
  706. { do a thread save inc/dec }
  707. function declocked(var l : longint) : boolean;assembler;
  708. { input: address of l in r3 }
  709. { output: boolean indicating whether l is zero after decrementing }
  710. asm
  711. LDecLockedLoop:
  712. {$ifdef MTRTL}
  713. lwarx r10,0,r3
  714. subi r10,r10,1
  715. stwcx. r10,0,r3
  716. bne- LDecLockedLoop
  717. {$else MTRTL}
  718. lwzx r10,0,r3
  719. subi r10,r10,1
  720. stw r10,0,r3
  721. {$endif MTRTL}
  722. mr. r3,r10
  723. end ['r3','r10'];
  724. procedure inclocked(var l : longint);assembler;
  725. LIncLockedLoop:
  726. {$ifdef MTRTL}
  727. lwarx r10,0,r3
  728. addi r10,r10,1
  729. stwcx. r10,0,r3
  730. bne- LDecLockedLoop
  731. {$else MTRTL}
  732. lwzx r10,0,r3
  733. addi r10,r10,1
  734. stw r10,0,r3
  735. {$endif MTRTL}
  736. end ['r3','r10'];
  737. {
  738. $Log$
  739. Revision 1.7 2001-09-28 13:28:49 jonas
  740. * small changes to move (different count values trigger the selection of
  741. moving bytes instead dwords/doubles and move dcbt instruction)
  742. + implemented fillchar (untested)
  743. Revision 1.6 2001/09/27 15:30:29 jonas
  744. * conversion to compilerproc and to structure used by i386 rtl
  745. * some bugfixes
  746. * powerpc.inc is almost complete (only fillchar/word/dword, get_frame etc
  747. and the class helpers are still needed
  748. - removed unnecessary register saving in set.inc (thanks to compilerproc)
  749. * use registers reserved for parameters as much as possible instead of
  750. those reserved for local vars (since those have to be saved by the
  751. called anyway, while the ones for local vars have to be saved by the
  752. callee)
  753. Revision 1.5 2001/07/07 12:46:12 jonas
  754. * some small bugfixes and cache optimizations
  755. Revision 1.4 2001/03/03 13:53:36 jonas
  756. * fixed small bug in move
  757. Revision 1.3 2001/03/02 13:24:10 jonas
  758. + new, complete implementation of move procedure (including support for
  759. overlapping regions)
  760. Revision 1.2 2001/02/11 17:59:46 jonas
  761. * implemented several more procedures
  762. Revision 1.1 2000/07/27 07:32:12 jonas
  763. + initial version by Casey Duncan (not yet thoroughly debugged or complete)
  764. }