powerpc.inc 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000-2006 by the Free Pascal development team.
  4. Portions Copyright (c) 2000 by Casey Duncan ([email protected])
  5. Processor dependent implementation for the system unit for
  6. PowerPC
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$IFNDEF LINUX}
  14. {$IFNDEF MORPHOS}
  15. {$IFNDEF AMIGAOS4}
  16. {$IFNDEF AIX}
  17. {$DEFINE USE_DCBZ}
  18. {$ENDIF AIX}
  19. {$ENDIF AMIGAOS4}
  20. {$ENDIF MORPHOS}
  21. {$ENDIF LINUX}
  22. {****************************************************************************
  23. PowerPC specific stuff
  24. ****************************************************************************}
  25. {
  26. const
  27. ppc_fpu_overflow = (1 shl (32-3));
  28. ppc_fpu_underflow = (1 shl (32-4));
  29. ppc_fpu_divbyzero = (1 shl (32-5));
  30. ppc_fpu_inexact = (1 shl (32-6));
  31. ppc_fpu_invalid_snan = (1 shl (32-7));
  32. }
  33. {$ifndef FPUNONE}
  34. procedure fpc_enable_ppc_fpu_exceptions;
  35. assembler; nostackframe;
  36. asm
  37. { clear all "exception happened" flags we care about}
  38. mtfsfi 0,0
  39. mtfsfi 1,0
  40. mtfsfi 2,0
  41. mtfsfi 3,0
  42. mtfsb0 21
  43. mtfsb0 22
  44. mtfsb0 23
  45. { enable invalid operations and division by zero exceptions. }
  46. { No overflow/underflow, since those give some spurious }
  47. { exceptions }
  48. mtfsfi 6,9
  49. end;
  50. procedure fpc_cpuinit;
  51. begin
  52. { don't let libraries influence the FPU cw set by the host program }
  53. if not IsLibrary then
  54. fpc_enable_ppc_fpu_exceptions;
  55. end;
  56. function fpc_get_ppc_fpscr: cardinal;
  57. assembler;
  58. var
  59. temp: record a,b:longint; end;
  60. asm
  61. mffs f0
  62. stfd f0,temp
  63. lwz r3,temp.b
  64. { clear all exception flags }
  65. {
  66. rlwinm r4,r3,0,16,31
  67. stw r4,temp.b
  68. lfd f0,temp
  69. a_mtfsf f0
  70. }
  71. end;
  72. { This function is never called directly, it's a dummy to hold the register save/
  73. load subroutines
  74. }
  75. {$ifndef MACOS}
  76. label
  77. _restfpr_14_x,
  78. _restfpr_15_x,
  79. _restfpr_16_x,
  80. _restfpr_17_x,
  81. _restfpr_18_x,
  82. _restfpr_19_x,
  83. _restfpr_20_x,
  84. _restfpr_21_x,
  85. _restfpr_22_x,
  86. _restfpr_23_x,
  87. _restfpr_24_x,
  88. _restfpr_25_x,
  89. _restfpr_26_x,
  90. _restfpr_27_x,
  91. _restfpr_28_x,
  92. _restfpr_29_x,
  93. _restfpr_30_x,
  94. _restfpr_31_x,
  95. _restfpr_14_l,
  96. _restfpr_15_l,
  97. _restfpr_16_l,
  98. _restfpr_17_l,
  99. _restfpr_18_l,
  100. _restfpr_19_l,
  101. _restfpr_20_l,
  102. _restfpr_21_l,
  103. _restfpr_22_l,
  104. _restfpr_23_l,
  105. _restfpr_24_l,
  106. _restfpr_25_l,
  107. _restfpr_26_l,
  108. _restfpr_27_l,
  109. _restfpr_28_l,
  110. _restfpr_29_l,
  111. _restfpr_30_l,
  112. _restfpr_31_l;
  113. procedure saverestorereg;assembler; nostackframe;
  114. asm
  115. { exit }
  116. .globl _restfpr_14_x
  117. _restfpr_14_x: lfd f14, -144(r11)
  118. .globl _restfpr_15_x
  119. _restfpr_15_x: lfd f15, -136(r11)
  120. .globl _restfpr_16_x
  121. _restfpr_16_x: lfd f16, -128(r11)
  122. .globl _restfpr_17_x
  123. _restfpr_17_x: lfd f17, -120(r11)
  124. .globl _restfpr_18_x
  125. _restfpr_18_x: lfd f18, -112(r11)
  126. .globl _restfpr_19_x
  127. _restfpr_19_x: lfd f19, -104(r11)
  128. .globl _restfpr_20_x
  129. _restfpr_20_x: lfd f20, -96(r11)
  130. .globl _restfpr_21_x
  131. _restfpr_21_x: lfd f21, -88(r11)
  132. .globl _restfpr_22_x
  133. _restfpr_22_x: lfd f22, -80(r11)
  134. .globl _restfpr_23_x
  135. _restfpr_23_x: lfd f23, -72(r11)
  136. .globl _restfpr_24_x
  137. _restfpr_24_x: lfd f24, -64(r11)
  138. .globl _restfpr_25_x
  139. _restfpr_25_x: lfd f25, -56(r11)
  140. .globl _restfpr_26_x
  141. _restfpr_26_x: lfd f26, -48(r11)
  142. .globl _restfpr_27_x
  143. _restfpr_27_x: lfd f27, -40(r11)
  144. .globl _restfpr_28_x
  145. _restfpr_28_x: lfd f28, -32(r11)
  146. .globl _restfpr_29_x
  147. _restfpr_29_x: lfd f29, -24(r11)
  148. .globl _restfpr_30_x
  149. _restfpr_30_x: lfd f30, -16(r11)
  150. .globl _restfpr_31_x
  151. _restfpr_31_x: lwz r0, 4(r11)
  152. lfd f31, -8(r11)
  153. mtlr r0
  154. ori r1, r11, 0
  155. blr
  156. { exit with restoring lr }
  157. .globl _restfpr_14_l
  158. _restfpr_14_l: lfd f14, -144(r11)
  159. .globl _restfpr_15_l
  160. _restfpr_15_l: lfd f15, -136(r11)
  161. .globl _restfpr_16_l
  162. _restfpr_16_l: lfd f16, -128(r11)
  163. .globl _restfpr_17_l
  164. _restfpr_17_l: lfd f17, -120(r11)
  165. .globl _restfpr_18_l
  166. _restfpr_18_l: lfd f18, -112(r11)
  167. .globl _restfpr_19_l
  168. _restfpr_19_l: lfd f19, -104(r11)
  169. .globl _restfpr_20_l
  170. _restfpr_20_l: lfd f20, -96(r11)
  171. .globl _restfpr_21_l
  172. _restfpr_21_l: lfd f21, -88(r11)
  173. .globl _restfpr_22_l
  174. _restfpr_22_l: lfd f22, -80(r11)
  175. .globl _restfpr_23_l
  176. _restfpr_23_l: lfd f23, -72(r11)
  177. .globl _restfpr_24_l
  178. _restfpr_24_l: lfd f24, -64(r11)
  179. .globl _restfpr_25_l
  180. _restfpr_25_l: lfd f25, -56(r11)
  181. .globl _restfpr_26_l
  182. _restfpr_26_l: lfd f26, -48(r11)
  183. .globl _restfpr_27_l
  184. _restfpr_27_l: lfd f27, -40(r11)
  185. .globl _restfpr_28_l
  186. _restfpr_28_l: lfd f28, -32(r11)
  187. .globl _restfpr_29_l
  188. _restfpr_29_l: lfd f29, -24(r11)
  189. .globl _restfpr_30_l
  190. _restfpr_30_l: lfd f30, -16(r11)
  191. .globl _restfpr_31_l
  192. _restfpr_31_l: lwz r0, 4(r11)
  193. lfd f31, -8(r11)
  194. mtlr r0
  195. ori r1, r11, 0
  196. blr
  197. end;
  198. {$endif MACOS}
  199. {$else}
  200. procedure fpc_cpuinit;
  201. begin
  202. end;
  203. {$endif}
  204. {****************************************************************************
  205. Move / Fill
  206. ****************************************************************************}
  207. {$ifndef FPC_SYSTEM_HAS_MOVE}
  208. {$define FPC_SYSTEM_HAS_MOVE}
  209. procedure Move(const source;var dest;count:longint);[public, alias: 'FPC_MOVE'];assembler; nostackframe;
  210. asm
  211. { count <= 0 ? }
  212. cmpwi cr0,r5,0
  213. { check if we have to do the move backwards because of overlap }
  214. sub r10,r4,r3
  215. { carry := boolean(dest-source < count) = boolean(overlap) }
  216. subc r10,r10,r5
  217. { count < 15 ? (to decide whether we will move dwords or bytes }
  218. cmpwi cr1,r5,15
  219. { if overlap, then r10 := -1 else r10 := 0 }
  220. subfe r10,r10,r10
  221. { count < 63 ? (32 + max. alignment (31) }
  222. { change to 64, because for overlap, we do not re-increment r5,
  223. which could then lead to a counter being zero at start and thus
  224. running forever }
  225. cmpwi cr7,r5,64
  226. { if count <= 0, stop }
  227. ble cr0,.LMoveDone
  228. { load the begin of the source in the data cache }
  229. dcbt 0,r3
  230. { and the dest as well }
  231. dcbtst 0,r4
  232. { if overlap, then r0 := count else r0 := 0 }
  233. and r0,r5,r10
  234. { if overlap, then point source and dest to the end }
  235. add r3,r3,r0
  236. add r4,r4,r0
  237. { if overlap, then r6 := 0, else r6 := -1 }
  238. not r6,r10
  239. { if overlap, then r10 := -2, else r10 := 0 }
  240. slwi r10,r10,1
  241. { if overlap, then r10 := -1, else r10 := 1 }
  242. addi r10,r10,1
  243. { if count < 15, copy everything byte by byte }
  244. blt cr1,.LMoveBytes
  245. { if no overlap, then source/dest += -1, otherwise they stay }
  246. { After the next instruction, r3/r4 + r10 = next position to }
  247. { load/store from/to }
  248. add r3,r3,r6
  249. add r4,r4,r6
  250. { otherwise, guarantee 4 byte alignment for dest for starters }
  251. .LMove4ByteAlignLoop:
  252. lbzux r0,r3,r10
  253. stbux r0,r4,r10
  254. { is dest now 4 aligned? }
  255. andi. r0,r4,3
  256. subi r5,r5,1
  257. { while not aligned, continue }
  258. bne cr0,.LMove4ByteAlignLoop
  259. {$ifndef ppc603}
  260. { check for 32 byte alignment }
  261. andi. r7,r4,31
  262. {$endif non ppc603}
  263. { we are going to copy one byte again (the one at the newly }
  264. { aligned address), so increase count byte 1 }
  265. { This is only true if there is no overlap, thus }
  266. { use r5:=r5-r6; which does what is needed. }
  267. sub r5,r5,r6
  268. { count div 4 for number of dwords to copy }
  269. srwi r0,r5,2
  270. { if 11 <= count < 63, copy using dwords }
  271. blt cr7,.LMoveDWords
  272. {$ifndef ppc603}
  273. { # of dwords to copy to reach 32 byte alignment (*4) }
  274. { (depends on forward/backward copy) }
  275. { if forward copy, r6 = -1 -> r8 := 32 }
  276. { if backward copy, r6 = 0 -> r8 := 0 }
  277. rlwinm r8,r6,0,31-6+1,31-6+1
  278. { if forward copy, we have to copy 32 - unaligned count bytes }
  279. { if backward copy unaligned count bytes }
  280. sub r7,r8,r7
  281. { if backward copy, the calculated value is now negate -> }
  282. { make it positive again }
  283. not r8, r6
  284. add r7, r7, r8
  285. xor r7, r7, r8
  286. {$endif not ppc603}
  287. { multiply the update count with 4 }
  288. slwi r10,r10,2
  289. slwi r6,r6,2
  290. { and adapt the source and dest }
  291. add r3,r3,r6
  292. add r4,r4,r6
  293. {$ifndef ppc603}
  294. beq cr0,.LMove32BytesAligned
  295. .L32BytesAlignMoveLoop:
  296. { count >= 39 -> align to 8 byte boundary and then use the FPU }
  297. { since we're already at 4 byte alignment, use dword store }
  298. subic. r7,r7,4
  299. lwzux r0,r3,r10
  300. subi r5,r5,4
  301. stwux r0,r4,r10
  302. bne .L32BytesAlignMoveLoop
  303. .LMove32BytesAligned:
  304. { count div 32 ( >= 1, since count was >=63 }
  305. srwi r0,r5,5
  306. { remainder }
  307. andi. r5,r5,31
  308. { to decide if we will do some dword stores (instead of only }
  309. { byte stores) afterwards or not }
  310. {$else not ppc603}
  311. srwi r0,r5,4
  312. andi. r5,r5,15
  313. {$endif not ppc603}
  314. cmpwi cr1,r5,11
  315. mtctr r0
  316. { r0 := count div 4, will be moved to ctr when copying dwords }
  317. srwi r0,r5,2
  318. {$if not defined(ppc603) and not defined(FPUNONE)}
  319. { adjust the update count: it will now be 8 or -8 depending on overlap }
  320. slwi r10,r10,1
  321. { adjust source and dest pointers: because of the above loop, dest is now }
  322. { aligned to 8 bytes. So if we add r6 we will still have an 8 bytes }
  323. { aligned address) }
  324. add r3,r3,r6
  325. add r4,r4,r6
  326. slwi r6,r6,1
  327. {$IFDEF USE_DCBZ}
  328. { the dcbz offset must give a 32 byte aligned address when added }
  329. { to the current dest address and its address must point to the }
  330. { bytes that will be overwritten in the current iteration. In case }
  331. { of a forward loop, the dest address has currently an offset of }
  332. { -8 compared to the bytes that will be overwritten (and r6 = -8). }
  333. { In case of a backward of a loop, the dest address currently has }
  334. { an offset of +32 compared to the bytes that will be overwritten }
  335. { (and r6 = 0). So the forward dcbz offset must become +8 and the }
  336. { backward -32 -> (-r6 * 5) - 32 gives the correct offset }
  337. slwi r7,r6,2
  338. add r7,r7,r6
  339. neg r7,r7
  340. subi r7,r7,32
  341. {$ENDIF USE_DCBZ}
  342. .LMove32ByteDcbz:
  343. lfdux f0,r3,r10
  344. lfdux f1,r3,r10
  345. lfdux f2,r3,r10
  346. lfdux f3,r3,r10
  347. {$IFDEF USE_DCBZ}
  348. { must be done only now, in case source and dest are less than }
  349. { 32 bytes apart! }
  350. dcbz r4,r7
  351. {$ENDIF USE_DCBZ}
  352. stfdux f0,r4,r10
  353. stfdux f1,r4,r10
  354. stfdux f2,r4,r10
  355. stfdux f3,r4,r10
  356. bdnz .LMove32ByteDcbz
  357. .LMove32ByteLoopDone:
  358. {$else not ppc603}
  359. .LMove16ByteLoop:
  360. lwzux r11,r3,r10
  361. lwzux r7,r3,r10
  362. lwzux r8,r3,r10
  363. lwzux r9,r3,r10
  364. stwux r11,r4,r10
  365. stwux r7,r4,r10
  366. stwux r8,r4,r10
  367. stwux r9,r4,r10
  368. bdnz .LMove16ByteLoop
  369. {$endif not ppc603}
  370. { cr0*4+eq is true if "count and 31" = 0 }
  371. beq cr0,.LMoveDone
  372. { make r10 again -1 or 1, but first adjust source/dest pointers }
  373. sub r3,r3,r6
  374. sub r4,r4,r6
  375. {$ifndef ppc603}
  376. srawi r10,r10,3
  377. srawi r6,r6,3
  378. {$else not ppc603}
  379. srawi r10,r10,2
  380. srawi r6,r6,2
  381. {$endif not ppc603}
  382. { cr1 contains whether count <= 11 }
  383. ble cr1,.LMoveBytes
  384. .LMoveDWords:
  385. mtctr r0
  386. andi. r5,r5,3
  387. { r10 * 4 }
  388. slwi r10,r10,2
  389. slwi r6,r6,2
  390. add r3,r3,r6
  391. add r4,r4,r6
  392. .LMoveDWordsLoop:
  393. lwzux r0,r3,r10
  394. stwux r0,r4,r10
  395. bdnz .LMoveDWordsLoop
  396. beq cr0,.LMoveDone
  397. { make r10 again -1 or 1 }
  398. sub r3,r3,r6
  399. sub r4,r4,r6
  400. srawi r10,r10,2
  401. srawi r6,r6,2
  402. .LMoveBytes:
  403. add r3,r3,r6
  404. add r4,r4,r6
  405. mtctr r5
  406. .LMoveBytesLoop:
  407. lbzux r0,r3,r10
  408. stbux r0,r4,r10
  409. bdnz .LMoveBytesLoop
  410. .LMoveDone:
  411. end;
  412. {$endif FPC_SYSTEM_HAS_MOVE}
  413. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  414. {$define FPC_SYSTEM_HAS_FILLCHAR}
  415. Procedure FillChar(var x;count:longint;value:byte);assembler;
  416. { input: x in r3, count in r4, value in r5 }
  417. {$ifndef FPC_ABI_AIX}
  418. { in the AIX ABI, we can use te red zone for temp storage, otherwise we have }
  419. { to explicitely allocate room }
  420. var
  421. temp : packed record
  422. case byte of
  423. 0: (l1,l2: longint);
  424. {$ifndef FPUNONE}
  425. 1: (d: double);
  426. {$endif}
  427. end;
  428. {$endif FPC_ABI_AIX}
  429. asm
  430. { no bytes? }
  431. cmpwi cr6,r4,0
  432. { less than 15 bytes? }
  433. cmpwi cr7,r4,15
  434. { less than 64 bytes? }
  435. cmpwi cr1,r4,64
  436. { fill r5 with ValueValueValueValue }
  437. rlwimi r5,r5,8,16,23
  438. { setup for aligning x to multiple of 4}
  439. rlwinm r10,r3,0,31-2+1,31
  440. rlwimi r5,r5,16,0,15
  441. ble cr6,.LFillCharDone
  442. { get the start of the data in the cache (and mark it as "will be }
  443. { modified") }
  444. dcbtst 0,r3
  445. subfic r10,r10,4
  446. blt cr7,.LFillCharVerySmall
  447. { just store 4 bytes instead of using a loop to align (there are }
  448. { plenty of other instructions now to keep the processor busy }
  449. { while it handles the (possibly unaligned) store) }
  450. stw r5,0(r3)
  451. { r3 := align(r3,4) }
  452. add r3,r3,r10
  453. { decrease count with number of bytes already stored }
  454. sub r4,r4,r10
  455. {$IFNDEF FPUNONE}
  456. blt cr1,.LFillCharSmall
  457. {$IFDEF USE_DCBZ}
  458. { if we have to fill with 0 (which happens a lot), we can simply use }
  459. { dcbz for the most part, which is very fast, so make a special case }
  460. { for that }
  461. cmplwi cr1,r5,0
  462. {$ENDIF}
  463. { align to a multiple of 32 (and immediately check whether we aren't }
  464. { already 32 byte aligned) }
  465. rlwinm. r10,r3,0,31-5+1,31
  466. { setup r3 for using update forms of store instructions }
  467. subi r3,r3,4
  468. { get number of bytes to store }
  469. subfic r10,r10,32
  470. { if already 32byte aligned, skip align loop }
  471. beq .L32ByteAlignLoopDone
  472. { substract from the total count }
  473. sub r4,r4,r10
  474. .L32ByteAlignLoop:
  475. { we were already aligned to 4 byres, so this will count down to }
  476. { exactly 0 }
  477. subic. r10,r10,4
  478. stwu r5,4(r3)
  479. bne .L32ByteAlignLoop
  480. .L32ByteAlignLoopDone:
  481. { get the amount of 32 byte blocks }
  482. srwi r10,r4,5
  483. { and keep the rest in r4 (recording whether there is any rest) }
  484. rlwinm. r4,r4,0,31-5+1,31
  485. { move to ctr }
  486. mtctr r10
  487. { check how many rest there is (to decide whether we'll use }
  488. { FillCharSmall or FillCharVerySmall) }
  489. cmplwi cr7,r4,11
  490. {$IFDEF USE_DCBZ}
  491. { if filling with zero, only use dcbz }
  492. bne cr1, .LFillCharNoZero
  493. { make r3 point again to the actual store position }
  494. addi r3,r3,4
  495. .LFillCharDCBZLoop:
  496. dcbz 0,r3
  497. addi r3,r3,32
  498. bdnz .LFillCharDCBZLoop
  499. { if there was no rest, we're finished }
  500. beq .LFillCharDone
  501. b .LFillCharVerySmall
  502. {$ENDIF USE_DCBZ}
  503. .LFillCharNoZero:
  504. {$ifdef FPC_ABI_AIX}
  505. stw r5,-4(r1)
  506. stw r5,-8(r1)
  507. lfd f0,-8(r1)
  508. {$else FPC_ABI_AIX}
  509. stw r5,temp
  510. stw r5,temp+4
  511. lfd f0,temp
  512. {$endif FPC_ABI_AIX}
  513. { make r3 point to address-8, so we're able to use fp double stores }
  514. { with update (it's already -4 now) }
  515. subi r3,r3,4
  516. {$IFDEF USE_DCBZ}
  517. { load r10 with 8, so that dcbz uses the correct address }
  518. li r10, 8
  519. {$ENDIF}
  520. .LFillChar32ByteLoop:
  521. {$IFDEF USE_DCBZ}
  522. dcbz r3,r10
  523. {$ENDIF USE_DCBZ}
  524. stfdu f0,8(r3)
  525. stfdu f0,8(r3)
  526. stfdu f0,8(r3)
  527. stfdu f0,8(r3)
  528. bdnz .LFillChar32ByteLoop
  529. { if there was no rest, we're finished }
  530. beq .LFillCharDone
  531. { make r3 point again to the actual next byte that must be written }
  532. addi r3,r3,8
  533. b .LFillCharVerySmall
  534. .LFillCharSmall:
  535. {$ENDIF FPUNONE}
  536. { when we arrive here, we're already 4 byte aligned }
  537. { get count div 4 to store dwords }
  538. srwi r10,r4,2
  539. { get ready for use of update stores }
  540. subi r3,r3,4
  541. mtctr r10
  542. rlwinm. r4,r4,0,31-2+1,31
  543. .LFillCharSmallLoop:
  544. stwu r5,4(r3)
  545. bdnz .LFillCharSmallLoop
  546. { if nothing left, stop }
  547. beq .LFillCharDone
  548. { get ready to store bytes }
  549. addi r3,r3,4
  550. .LFillCharVerySmall:
  551. mtctr r4
  552. subi r3,r3,1
  553. .LFillCharVerySmallLoop:
  554. stbu r5,1(r3)
  555. bdnz .LFillCharVerySmallLoop
  556. .LFillCharDone:
  557. end;
  558. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  559. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  560. {$define FPC_SYSTEM_HAS_FILLDWORD}
  561. procedure filldword(var x;count : longint;value : dword);
  562. assembler; nostackframe;
  563. asm
  564. { registers:
  565. r3 x
  566. r4 count
  567. r5 value
  568. }
  569. cmpwi cr0,r4,0
  570. mtctr r4
  571. subi r3,r3,4
  572. ble .LFillDWordEnd //if count<=0 Then Exit
  573. .LFillDWordLoop:
  574. stwu r5,4(r3)
  575. bdnz .LFillDWordLoop
  576. .LFillDWordEnd:
  577. end;
  578. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  579. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  580. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  581. function IndexByte(const buf;len:longint;b:byte):longint; assembler; nostackframe;
  582. { input: r3 = buf, r4 = len, r5 = b }
  583. { output: r3 = position of b in buf (-1 if not found) }
  584. asm
  585. { load the begin of the buffer in the data cache }
  586. dcbt 0,r3
  587. cmplwi r4,0
  588. mtctr r4
  589. subi r10,r3,1
  590. mr r0,r3
  591. { assume not found }
  592. li r3,-1
  593. ble .LIndexByteDone
  594. .LIndexByteLoop:
  595. lbzu r9,1(r10)
  596. cmplw r9,r5
  597. bdnzf cr0*4+eq,.LIndexByteLoop
  598. { r3 still contains -1 here }
  599. bne .LIndexByteDone
  600. sub r3,r10,r0
  601. .LIndexByteDone:
  602. end;
  603. {$endif FPC_SYSTEM_HAS_INDEXBYTE}
  604. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  605. {$define FPC_SYSTEM_HAS_INDEXWORD}
  606. function IndexWord(const buf;len:longint;b:word):longint; assembler; nostackframe;
  607. { input: r3 = buf, r4 = len, r5 = b }
  608. { output: r3 = position of b in buf (-1 if not found) }
  609. asm
  610. { load the begin of the buffer in the data cache }
  611. dcbt 0,r3
  612. cmplwi r4,0
  613. mtctr r4
  614. subi r10,r3,2
  615. mr r0,r3
  616. { assume not found }
  617. li r3,-1
  618. ble .LIndexWordDone
  619. .LIndexWordLoop:
  620. lhzu r9,2(r10)
  621. cmplw r9,r5
  622. bdnzf cr0*4+eq,.LIndexWordLoop
  623. { r3 still contains -1 here }
  624. bne .LIndexWordDone
  625. sub r3,r10,r0
  626. srawi r3,r3,1
  627. .LIndexWordDone:
  628. end;
  629. {$endif FPC_SYSTEM_HAS_INDEXWORD}
  630. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  631. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  632. function IndexDWord(const buf;len:longint;b:DWord):longint; assembler; nostackframe;
  633. { input: r3 = buf, r4 = len, r5 = b }
  634. { output: r3 = position of b in buf (-1 if not found) }
  635. asm
  636. { load the begin of the buffer in the data cache }
  637. dcbt 0,r3
  638. cmplwi r4,0
  639. mtctr r4
  640. subi r10,r3,4
  641. mr r0,r3
  642. { assume not found }
  643. li r3,-1
  644. ble .LIndexDWordDone
  645. .LIndexDWordLoop:
  646. lwzu r9,4(r10)
  647. cmplw r9,r5
  648. bdnzf cr0*4+eq, .LIndexDWordLoop
  649. { r3 still contains -1 here }
  650. bne .LIndexDWordDone
  651. sub r3,r10,r0
  652. srawi r3,r3,2
  653. .LIndexDWordDone:
  654. end;
  655. {$endif FPC_SYSTEM_HAS_INDEXDWORD}
  656. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  657. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  658. function CompareByte(const buf1,buf2;len:longint):longint; assembler; nostackframe;
  659. { input: r3 = buf1, r4 = buf2, r5 = len }
  660. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  661. { note: almost direct copy of strlcomp() from strings.inc }
  662. asm
  663. { load the begin of the first buffer in the data cache }
  664. dcbt 0,r3
  665. { use r0 instead of r3 for buf1 since r3 contains result }
  666. cmplwi r5,0
  667. mtctr r5
  668. subi r11,r3,1
  669. subi r4,r4,1
  670. li r3,0
  671. ble .LCompByteDone
  672. .LCompByteLoop:
  673. { load next chars }
  674. lbzu r9,1(r11)
  675. lbzu r10,1(r4)
  676. { calculate difference }
  677. sub. r3,r9,r10
  678. { if chars not equal or at the end, we're ready }
  679. bdnzt cr0*4+eq, .LCompByteLoop
  680. .LCompByteDone:
  681. end;
  682. {$endif FPC_SYSTEM_HAS_COMPAREBYTE}
  683. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  684. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  685. function CompareWord(const buf1,buf2;len:longint):longint; assembler; nostackframe;
  686. { input: r3 = buf1, r4 = buf2, r5 = len }
  687. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  688. { note: almost direct copy of strlcomp() from strings.inc }
  689. asm
  690. { load the begin of the first buffer in the data cache }
  691. dcbt 0,r3
  692. { use r0 instead of r3 for buf1 since r3 contains result }
  693. cmplwi r5,0
  694. mtctr r5
  695. subi r11,r3,2
  696. subi r4,r4,2
  697. li r3,0
  698. ble .LCompWordDone
  699. .LCompWordLoop:
  700. { load next chars }
  701. lhzu r9,2(r11)
  702. lhzu r10,2(r4)
  703. { calculate difference }
  704. sub. r3,r9,r10
  705. { if chars not equal or at the end, we're ready }
  706. bdnzt cr0*4+eq, .LCompWordLoop
  707. .LCompWordDone:
  708. end;
  709. {$endif FPC_SYSTEM_HAS_COMPAREWORD}
  710. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  711. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  712. function CompareDWord(const buf1,buf2;len:longint):longint; assembler; nostackframe;
  713. { input: r3 = buf1, r4 = buf2, r5 = len }
  714. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  715. { note: almost direct copy of strlcomp() from strings.inc }
  716. asm
  717. { load the begin of the first buffer in the data cache }
  718. dcbt 0,r3
  719. { use r0 instead of r3 for buf1 since r3 contains result }
  720. cmplwi r5,0
  721. mtctr r5
  722. subi r11,r3,4
  723. subi r4,r4,4
  724. li r3,0
  725. ble .LCompDWordDone
  726. .LCompDWordLoop:
  727. { load next chars }
  728. lwzu r9,4(r11)
  729. lwzu r10,4(r4)
  730. { calculate difference }
  731. sub. r0,r9,r10
  732. { if chars not equal or at the end, we're ready }
  733. bdnzt cr0*4+eq, .LCompDWordLoop
  734. .LCompDWordDone:
  735. cmplw cr1,r9,r10
  736. beq .Ldone
  737. { since these were two dwords, we have to perform an additional }
  738. { unsigned comparison and set the result accordingly }
  739. bgt cr1,.Lpos
  740. li r3,-2
  741. .Lpos:
  742. addi r3,r3,1
  743. .Ldone:
  744. end;
  745. {$endif FPC_SYSTEM_HAS_COMPAREDWORD}
  746. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  747. {$define FPC_SYSTEM_HAS_INDEXCHAR0}
  748. function IndexChar0(const buf;len:longint;b:Char):longint; assembler; nostackframe;
  749. { input: r3 = buf, r4 = len, r5 = b }
  750. { output: r3 = position of found position (-1 if not found) }
  751. asm
  752. { load the begin of the buffer in the data cache }
  753. dcbt 0,r3
  754. { length = 0? }
  755. cmplwi r4,0
  756. mtctr r4
  757. subi r9,r3,1
  758. subi r0,r3,1
  759. { assume not found }
  760. li r3,-1
  761. { if yes, do nothing }
  762. ble .LIndexChar0Done
  763. .LIndexChar0Loop:
  764. lbzu r10,1(r9)
  765. cmplwi cr1,r10,0
  766. cmplw r10,r5
  767. beq cr1,.LIndexChar0Done
  768. bdnzf cr0*4+eq, .LIndexChar0Loop
  769. bne .LIndexChar0Done
  770. sub r3,r9,r0
  771. .LIndexChar0Done:
  772. end;
  773. {$endif FPC_SYSTEM_HAS_INDEXCHAR0}
  774. {****************************************************************************
  775. String
  776. ****************************************************************************}
  777. {$ifndef STR_CONCAT_PROCS}
  778. (*
  779. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  780. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  781. function fpc_shortstr_concat(const s1, s2: shortstring): shortstring; compilerproc; [public, alias: 'FPC_SHORTSTR_CONCAT'];
  782. { expects that (r3) contains a pointer to the result r4 to s1, r5 to s2 }
  783. assembler;
  784. asm
  785. { load length s1 }
  786. lbz r6, 0(r4)
  787. { load length s2 }
  788. lbz r10, 0(r5)
  789. { length 0 for s1? }
  790. cmplwi cr7,r6,0
  791. { length 255 for s1? }
  792. subfic. r7,r6,255
  793. { length 0 for s2? }
  794. cmplwi cr1,r10,0
  795. { calculate min(length(s2),255-length(s1)) }
  796. subc r8,r7,r10 { r8 := r7 - r10 }
  797. cror 4*6+2,4*1+2,4*7+2
  798. subfe r7,r7,r7 { if r7 >= r10 then r7' := 0 else r7' := -1 }
  799. mtctr r6
  800. and r7,r8,r7 { if r7 >= r10 then r7' := 0 else r7' := r7-r10 }
  801. add r7,r7,r10 { if r7 >= r10 then r7' := r10 else r7' := r7 }
  802. mr r9,r3
  803. { calculate length of final string }
  804. add r8,r7,r6
  805. stb r8,0(r3)
  806. beq cr7, .Lcopys1loopDone
  807. .Lcopys1loop:
  808. lbzu r0,1(r4)
  809. stbu r0,1(r9)
  810. bdnz .Lcopys1loop
  811. .Lcopys1loopDone:
  812. mtctr r7
  813. beq cr6, .LconcatDone
  814. .Lcopys2loop:
  815. lbzu r0,1(r5)
  816. stbu r0,1(r9)
  817. bdnz .Lcopys2loop
  818. end;
  819. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  820. *)
  821. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  822. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  823. procedure fpc_shortstr_append_shortstr(var s1: shortstring; const s2: shortstring); compilerproc;
  824. { expects that results (r3) contains a pointer to the current string s1, r4 }
  825. { high(s1) and (r5) a pointer to the one that has to be concatenated }
  826. assembler; nostackframe;
  827. asm
  828. { load length s1 }
  829. lbz r6, 0(r3)
  830. { load length s2 }
  831. lbz r10, 0(r5)
  832. { length 0? }
  833. cmplw cr1,r6,r4
  834. cmplwi r10,0
  835. { calculate min(length(s2),high(result)-length(result)) }
  836. sub r9,r4,r6
  837. subc r8,r9,r10 { r8 := r9 - r10 }
  838. cror 4*7+2,4*0+2,4*1+2
  839. subfe r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  840. and r9,r8,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r10 }
  841. add r9,r9,r10 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  842. { calculate new length }
  843. add r10,r6,r9
  844. { load value to copy in ctr }
  845. mtctr r9
  846. { store new length }
  847. stb r10,0(r3)
  848. { go to last current character of result }
  849. add r3,r6,r3
  850. { if nothing to do, exit }
  851. beq cr7, .LShortStrAppendDone
  852. { and concatenate }
  853. .LShortStrAppendLoop:
  854. lbzu r10,1(r5)
  855. stbu r10,1(r3)
  856. bdnz .LShortStrAppendLoop
  857. .LShortStrAppendDone:
  858. end;
  859. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  860. {$endif STR_CONCAT_PROCS}
  861. (*
  862. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  863. function fpc_shortstr_compare(const dstr,sstr:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  864. assembler;
  865. asm
  866. { load length sstr }
  867. lbz r9,0(r4)
  868. { load length dstr }
  869. lbz r10,0(r3)
  870. { save their difference for later and }
  871. { calculate min(length(sstr),length(dstr)) }
  872. subfc r7,r10,r9 { r0 := r9 - r10 }
  873. subfe r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  874. and r7,r7,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r8 }
  875. add r9,r10,r7 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  876. { first compare dwords (length/4) }
  877. srwi. r5,r9,2
  878. { keep length mod 4 for the ends }
  879. rlwinm r9,r9,0,30,31
  880. { already check whether length mod 4 = 0 }
  881. cmplwi cr1,r9,0
  882. { so we can load r3 with 0, in case the strings both have length 0 }
  883. mr r8,r3
  884. li r3, 0
  885. { length div 4 in ctr for loop }
  886. mtctr r5
  887. { if length < 3, goto byte comparing }
  888. beq LShortStrCompare1
  889. { setup for use of update forms of load/store with dwords }
  890. subi r4,r4,3
  891. subi r8,r8,3
  892. LShortStrCompare4Loop:
  893. lwzu r3,4(r4)
  894. lwzu r10,4(r8)
  895. sub. r3,r3,r10
  896. bdnzt cr0+eq,LShortStrCompare4Loop
  897. { r3 contains result if we stopped because of "ne" flag }
  898. bne LShortStrCompareDone
  899. { setup for use of update forms of load/store with bytes }
  900. addi r4,r4,3
  901. addi r8,r8,3
  902. LShortStrCompare1:
  903. { if comparelen mod 4 = 0, skip this and return the difference in }
  904. { lengths }
  905. beq cr1,LShortStrCompareLen
  906. mtctr r9
  907. LShortStrCompare1Loop:
  908. lbzu r3,1(r4)
  909. lbzu r10,1(r8)
  910. sub. r3,r3,r10
  911. bdnzt cr0+eq,LShortStrCompare1Loop
  912. bne LShortStrCompareDone
  913. LShortStrCompareLen:
  914. { also return result in flags, maybe we can use this in the CG }
  915. mr. r3,r3
  916. LShortStrCompareDone:
  917. end;
  918. *)
  919. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  920. {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  921. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);assembler;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc; nostackframe;
  922. {
  923. r3: result address
  924. r4: high(result)
  925. r5: p (source)
  926. }
  927. asm
  928. { nil? }
  929. mr r8, p
  930. cmplwi p, 0
  931. { load the begin of the string in the data cache }
  932. dcbt 0, p
  933. { maxlength }
  934. mr r10,r4
  935. mtctr r10
  936. { at LStrPasDone, we set the length of the result to 255 - r10 - r4 }
  937. { = 255 - 255 - 0 if the soure = nil -> perfect :) }
  938. beq .LStrPasDone
  939. { save address for at the end and use r7 in loop }
  940. mr r7,r3
  941. { no "subi r7,r7,1" because the first byte = length byte }
  942. subi r8,r8,1
  943. .LStrPasLoop:
  944. lbzu r10,1(r8)
  945. cmplwi cr0,r10,0
  946. stbu r10,1(r7)
  947. bdnzf cr0*4+eq, .LStrPasLoop
  948. { if we stopped because of a terminating #0, decrease the length by 1 }
  949. cntlzw r4,r10
  950. { get remaining count for length }
  951. mfctr r10
  952. { if r10 was zero (-> stopped because of zero byte), then r4 will be 32 }
  953. { (32 leading zero bits) -> shr 5 = 1, otherwise this will be zero }
  954. srwi r4,r4,5
  955. .LStrPasDone:
  956. subfic r10,r10,255
  957. sub r10,r10,r4
  958. { store length }
  959. stb r10,0(r3)
  960. end;
  961. {$endif FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  962. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  963. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  964. function fpc_pchar_length(p:pchar):sizeint;assembler;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc; nostackframe;
  965. {$include strlen.inc}
  966. {$endif FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  967. {$ifndef INTERNAL_BACKTRACE}
  968. {$define FPC_SYSTEM_HAS_GET_FRAME}
  969. function get_frame:pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  970. asm
  971. { all abi's I know use r1 as stack pointer }
  972. mr r3, r1
  973. end;
  974. {$endif INTERNAL_BACKTRACE}
  975. {NOTE: On MACOS, 68000 code might call powerpc code, through the MixedMode manager,
  976. (even in the OS in system 9). The pointer to the switching stack frame is then
  977. indicated by the first bit set to 1. This is checked below.}
  978. {Both routines below assumes that framebp is a valid framepointer or nil.}
  979. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  980. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  981. asm
  982. cmplwi r3,0
  983. beq .Lcaller_addr_invalid
  984. lwz r3,0(r3)
  985. cmplwi r3,0
  986. beq .Lcaller_addr_invalid
  987. {$ifdef MACOS}
  988. rlwinm r4,r3,0,31,31
  989. cmpwi r4,0
  990. bne cr0,.Lcaller_addr_invalid
  991. {$endif MACOS}
  992. {$ifdef FPC_ABI_AIX}
  993. lwz r3,8(r3)
  994. {$else FPC_ABI_AIX}
  995. lwz r3,4(r3)
  996. {$endif FPC_ABI_AIX}
  997. blr
  998. .Lcaller_addr_invalid:
  999. li r3,0
  1000. end;
  1001. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1002. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  1003. asm
  1004. cmplwi r3,0
  1005. beq .Lcaller_frame_invalid
  1006. lwz r3,0(r3)
  1007. {$ifdef MACOS}
  1008. rlwinm r4,r3,0,31,31
  1009. cmpwi r4,0
  1010. bne cr0,.Lcaller_frame_invalid
  1011. {$endif MACOS}
  1012. blr
  1013. .Lcaller_frame_invalid:
  1014. li r3,0
  1015. end;
  1016. {$define FPC_SYSTEM_HAS_SPTR}
  1017. Function Sptr : Pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  1018. asm
  1019. mr r3,r1
  1020. end;
  1021. {****************************************************************************
  1022. Str()
  1023. ****************************************************************************}
  1024. { int_str: generic implementation is used for now }
  1025. {****************************************************************************
  1026. Multithreading
  1027. ****************************************************************************}
  1028. { do a thread save inc/dec }
  1029. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1030. function declocked(var l : longint) : boolean;assembler;nostackframe;
  1031. { input: address of l in r3 }
  1032. { output: boolean indicating whether l is zero after decrementing }
  1033. asm
  1034. .LDecLockedLoop:
  1035. lwarx r10,0,r3
  1036. subi r10,r10,1
  1037. stwcx. r10,0,r3
  1038. bne- .LDecLockedLoop
  1039. cntlzw r3,r10
  1040. srwi r3,r3,5
  1041. end;
  1042. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1043. procedure inclocked(var l : longint);assembler;nostackframe;
  1044. asm
  1045. .LIncLockedLoop:
  1046. lwarx r10,0,r3
  1047. addi r10,r10,1
  1048. stwcx. r10,0,r3
  1049. bne- .LIncLockedLoop
  1050. end;
  1051. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  1052. { input: address of target in r3 }
  1053. { output: target-1 in r3 }
  1054. { side-effect: target := target-1 }
  1055. asm
  1056. .LInterLockedDecLoop:
  1057. lwarx r10,0,r3
  1058. subi r10,r10,1
  1059. stwcx. r10,0,r3
  1060. bne .LInterLockedDecLoop
  1061. mr r3,r10
  1062. end;
  1063. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  1064. { input: address of target in r3 }
  1065. { output: target+1 in r3 }
  1066. { side-effect: target := target+1 }
  1067. asm
  1068. .LInterLockedIncLoop:
  1069. lwarx r10,0,r3
  1070. addi r10,r10,1
  1071. stwcx. r10,0,r3
  1072. bne .LInterLockedIncLoop
  1073. mr r3,r10
  1074. end;
  1075. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  1076. { input: address of target in r3, source in r4 }
  1077. { output: target in r3 }
  1078. { side-effect: target := source }
  1079. asm
  1080. .LInterLockedXchgLoop:
  1081. lwarx r10,0,r3
  1082. stwcx. r4,0,r3
  1083. bne .LInterLockedXchgLoop
  1084. mr r3,r10
  1085. end;
  1086. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  1087. asm
  1088. .LInterLockedXchgAddLoop:
  1089. lwarx r10,0,r3
  1090. add r10,r10,r4
  1091. stwcx. r10,0,r3
  1092. bne .LInterLockedXchgAddLoop
  1093. sub r3,r10,r4
  1094. end;
  1095. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  1096. { input: address of target in r3, newvalue in r4, comparand in r5 }
  1097. { output: value stored in target before entry of the function }
  1098. { side-effect: NewValue stored in target if (target = comparand) }
  1099. asm
  1100. .LInterlockedCompareExchangeLoop:
  1101. lwarx r10,0,r3
  1102. sub r9,r10,r5
  1103. addic r9,r9,-1
  1104. subfe r9,r9,r9
  1105. and r8,r4,r9
  1106. andc r7,r10,r9
  1107. or r6,r7,r8
  1108. stwcx. r6,0,r3
  1109. bne .LInterlockedCompareExchangeLoop
  1110. mr r3, r10
  1111. end;
  1112. {$IFDEF MORPHOS}
  1113. { this is only required for MorphOS }
  1114. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  1115. procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  1116. var tmp: array[0..1] of dword;
  1117. begin
  1118. asm
  1119. { setting fpu to round to nearest mode }
  1120. li r3,0
  1121. stw r3,8(r1)
  1122. stw r3,12(r1)
  1123. lfd f1,8(r1)
  1124. mtfsf 7,f1
  1125. end;
  1126. { powerpc might use softfloat code }
  1127. softfloat_exception_flags:=[];
  1128. softfloat_exception_mask:=[float_flag_underflow, float_flag_inexact, float_flag_denormal];
  1129. end;
  1130. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  1131. procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  1132. begin
  1133. softfloat_exception_flags:=[];
  1134. end;
  1135. {$ENDIF}
  1136. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1137. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  1138. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  1139. asm
  1140. isync
  1141. end;
  1142. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1143. begin
  1144. { reads imply barrier on earlier reads depended on }
  1145. end;
  1146. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  1147. asm
  1148. isync
  1149. eieio
  1150. end;
  1151. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  1152. asm
  1153. eieio
  1154. end;
  1155. {$endif}