powerpc.inc 38 KB

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