powerpc.inc 38 KB

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