powerpc.inc 38 KB

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