powerpc.inc 32 KB

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