powerpc64.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000-2001 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. PowerPC64
  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. {****************************************************************************
  14. PowerPC specific stuff
  15. ****************************************************************************}
  16. procedure fpc_enable_ppc_fpu_exceptions; assembler; nostackframe;
  17. asm
  18. { clear all "exception happened" flags we care about}
  19. mtfsfi 0,0
  20. mtfsfi 1,0
  21. mtfsfi 2,0
  22. mtfsfi 3,0
  23. {$ifdef fpc_mtfsb0_corrected}
  24. mtfsb0 21
  25. mtfsb0 22
  26. mtfsb0 23
  27. {$endif fpc_mtfsb0_corrected}
  28. { enable invalid operations and division by zero exceptions. }
  29. { No overflow/underflow, since those give some spurious }
  30. { exceptions }
  31. mtfsfi 6,9
  32. end;
  33. procedure fpc_cpuinit;
  34. begin
  35. { don't let libraries influence the FPU cw set by the host program }
  36. if not IsLibrary then
  37. fpc_enable_ppc_fpu_exceptions;
  38. end;
  39. {****************************************************************************
  40. Move / Fill
  41. ****************************************************************************}
  42. {$ifndef FPC_SYSTEM_HAS_MOVE}
  43. {$define FPC_SYSTEM_HAS_MOVE}
  44. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  45. type
  46. bytearray = array [0..high(sizeint)-1] of byte;
  47. var
  48. i:longint;
  49. begin
  50. if count <= 0 then exit;
  51. Dec(count);
  52. if (@dest > @source) then
  53. begin
  54. for i:=count downto 0 do
  55. bytearray(dest)[i]:=bytearray(source)[i];
  56. end
  57. else
  58. begin
  59. for i:=0 to count do
  60. bytearray(dest)[i]:=bytearray(source)[i];
  61. end;
  62. end;
  63. {$endif FPC_SYSTEM_HAS_MOVE}
  64. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  65. {$define FPC_SYSTEM_HAS_FILLCHAR}
  66. Procedure FillChar(var x;count:SizeInt;value:byte);
  67. type
  68. longintarray = array [0..high(sizeint) div 4-1] of longint;
  69. bytearray = array [0..high(sizeint)-1] of byte;
  70. var
  71. i,v : longint;
  72. begin
  73. if count <= 0 then exit;
  74. v := 0;
  75. { aligned? }
  76. if (PtrUInt(@x) mod sizeof(PtrUInt))<>0 then
  77. for i:=0 to count-1 do
  78. bytearray(x)[i]:=value
  79. else begin
  80. v:=(value shl 8) or (value and $FF);
  81. v:=(v shl 16) or (v and $ffff);
  82. for i:=0 to (count div 4)-1 do
  83. longintarray(x)[i]:=v;
  84. for i:=(count div 4)*4 to count-1 do
  85. bytearray(x)[i]:=value;
  86. end;
  87. end;
  88. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  89. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  90. {$define FPC_SYSTEM_HAS_FILLDWORD}
  91. procedure filldword(var x;count : SizeInt;value : dword); assembler; nostackframe;
  92. asm
  93. cmpdi cr0,r4,0
  94. mtctr r4
  95. subi r3,r3,4
  96. ble .LFillDWordEnd //if count<=0 Then Exit
  97. .LFillDWordLoop:
  98. stwu r5,4(r3)
  99. bdnz .LFillDWordLoop
  100. .LFillDWordEnd:
  101. end;
  102. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  103. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  104. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  105. function IndexByte(const buf;len:SizeInt;b:byte):int64; assembler; nostackframe;
  106. { input: r3 = buf, r4 = len, r5 = b }
  107. { output: r3 = position of b in buf (-1 if not found) }
  108. asm
  109. { load the begin of the buffer in the data cache }
  110. dcbt 0,r3
  111. cmpldi r4,0
  112. mtctr r4
  113. subi r10,r3,1
  114. mr r0,r3
  115. { assume not found }
  116. li r3,-1
  117. ble .LIndexByteDone
  118. .LIndexByteLoop:
  119. lbzu r9,1(r10)
  120. cmpld r9,r5
  121. bdnzf cr0*4+eq,.LIndexByteLoop
  122. { r3 still contains -1 here }
  123. bne .LIndexByteDone
  124. sub r3,r10,r0
  125. .LIndexByteDone:
  126. end;
  127. {$endif FPC_SYSTEM_HAS_INDEXBYTE}
  128. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  129. {$define FPC_SYSTEM_HAS_INDEXWORD}
  130. function IndexWord(const buf;len:SizeInt;b:word):int64; assembler; nostackframe;
  131. { input: r3 = buf, r4 = len, r5 = b }
  132. { output: r3 = position of b in buf (-1 if not found) }
  133. asm
  134. { load the begin of the buffer in the data cache }
  135. dcbt 0,r3
  136. cmpldi r4,0
  137. mtctr r4
  138. subi r10,r3,2
  139. mr r0,r3
  140. { assume not found }
  141. li r3,-1
  142. ble .LIndexWordDone
  143. .LIndexWordLoop:
  144. lhzu r9,2(r10)
  145. cmpld r9,r5
  146. bdnzf cr0*4+eq,.LIndexWordLoop
  147. { r3 still contains -1 here }
  148. bne .LIndexWordDone
  149. sub r3,r10,r0
  150. sradi r3,r3,1
  151. .LIndexWordDone:
  152. end;
  153. {$endif FPC_SYSTEM_HAS_INDEXWORD}
  154. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  155. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  156. function IndexDWord(const buf;len:SizeInt;b:DWord):int64; assembler; nostackframe;
  157. { input: r3 = buf, r4 = len, r5 = b }
  158. { output: r3 = position of b in buf (-1 if not found) }
  159. asm
  160. { load the begin of the buffer in the data cache }
  161. dcbt 0,r3
  162. cmpldi r4,0
  163. mtctr r4
  164. subi r10,r3,4
  165. mr r0,r3
  166. { assume not found }
  167. li r3,-1
  168. ble .LIndexDWordDone
  169. .LIndexDWordLoop:
  170. lwzu r9,4(r10)
  171. cmpld r9,r5
  172. bdnzf cr0*4+eq, .LIndexDWordLoop
  173. { r3 still contains -1 here }
  174. bne .LIndexDWordDone
  175. sub r3,r10,r0
  176. sradi r3,r3,2
  177. .LIndexDWordDone:
  178. end;
  179. {$endif FPC_SYSTEM_HAS_INDEXDWORD}
  180. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  181. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  182. function CompareByte(const buf1,buf2;len:SizeInt):int64; assembler; nostackframe;
  183. { input: r3 = buf1, r4 = buf2, r5 = len }
  184. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  185. { note: almost direct copy of strlcomp() from strings.inc }
  186. asm
  187. { load the begin of the first buffer in the data cache }
  188. dcbt 0,r3
  189. { use r0 instead of r3 for buf1 since r3 contains result }
  190. cmpldi r5,0
  191. mtctr r5
  192. subi r11,r3,1
  193. subi r4,r4,1
  194. li r3,0
  195. ble .LCompByteDone
  196. .LCompByteLoop:
  197. { load next chars }
  198. lbzu r9,1(r11)
  199. lbzu r10,1(r4)
  200. { calculate difference }
  201. sub. r3,r9,r10
  202. { if chars not equal or at the end, we're ready }
  203. bdnzt cr0*4+eq, .LCompByteLoop
  204. .LCompByteDone:
  205. end;
  206. {$endif FPC_SYSTEM_HAS_COMPAREBYTE}
  207. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  208. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  209. function CompareWord(const buf1,buf2;len:SizeInt):int64; assembler; nostackframe;
  210. { input: r3 = buf1, r4 = buf2, r5 = len }
  211. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  212. { note: almost direct copy of strlcomp() from strings.inc }
  213. asm
  214. { load the begin of the first buffer in the data cache }
  215. dcbt 0,r3
  216. { use r0 instead of r3 for buf1 since r3 contains result }
  217. cmpldi r5,0
  218. mtctr r5
  219. subi r11,r3,2
  220. subi r4,r4,2
  221. li r3,0
  222. ble .LCompWordDone
  223. .LCompWordLoop:
  224. { load next chars }
  225. lhzu r9,2(r11)
  226. lhzu r10,2(r4)
  227. { calculate difference }
  228. sub. r3,r9,r10
  229. { if chars not equal or at the end, we're ready }
  230. bdnzt cr0*4+eq, .LCompWordLoop
  231. .LCompWordDone:
  232. end;
  233. {$endif FPC_SYSTEM_HAS_COMPAREWORD}
  234. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  235. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  236. function CompareDWord(const buf1,buf2;len:SizeInt):int64; assembler; nostackframe;
  237. { input: r3 = buf1, r4 = buf2, r5 = len }
  238. { output: r3 = 0 if equal, < 0 if buf1 < str2, > 0 if buf1 > str2 }
  239. { note: almost direct copy of strlcomp() from strings.inc }
  240. asm
  241. { load the begin of the first buffer in the data cache }
  242. dcbt 0,r3
  243. { use r0 instead of r3 for buf1 since r3 contains result }
  244. cmpldi r5,0
  245. mtctr r5
  246. subi r11,r3,4
  247. subi r4,r4,4
  248. li r3,0
  249. ble .LCompDWordDone
  250. .LCompDWordLoop:
  251. { load next chars }
  252. lwzu r9,4(r11)
  253. lwzu r10,4(r4)
  254. { calculate difference }
  255. sub. r3,r9,r10
  256. { if chars not equal or at the end, we're ready }
  257. bdnzt cr0*4+eq, .LCompDWordLoop
  258. .LCompDWordDone:
  259. cmpld cr1,r9,r10
  260. beq .Ldone
  261. { since these were two dwords, we have to perform an additional }
  262. { unsigned comparison and set the result accordingly }
  263. bgt cr1,.Lpos
  264. li r3,-2
  265. .Lpos:
  266. addi r3,r3,1
  267. .Ldone:
  268. end;
  269. {$endif FPC_SYSTEM_HAS_COMPAREDWORD}
  270. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  271. {$define FPC_SYSTEM_HAS_INDEXCHAR0}
  272. function IndexChar0(const buf;len:SizeInt;b:Char):int64; assembler; nostackframe;
  273. { input: r3 = buf, r4 = len, r5 = b }
  274. { output: r3 = position of found position (-1 if not found) }
  275. asm
  276. { load the begin of the buffer in the data cache }
  277. dcbt 0,r3
  278. { length = 0? }
  279. cmpldi r4,0
  280. mtctr r4
  281. subi r9,r3,1
  282. subi r0,r3,1
  283. { assume not found }
  284. li r3,-1
  285. { if yes, do nothing }
  286. ble .LIndexChar0Done
  287. .LIndexChar0Loop:
  288. lbzu r10,1(r9)
  289. cmpldi cr1,r10,0
  290. cmpld r10,r5
  291. beq cr1,.LIndexChar0Done
  292. bdnzf cr0*4+eq, .LIndexChar0Loop
  293. bne .LIndexChar0Done
  294. sub r3,r9,r0
  295. .LIndexChar0Done:
  296. end;
  297. {$endif FPC_SYSTEM_HAS_INDEXCHAR0}
  298. {****************************************************************************
  299. String
  300. ****************************************************************************}
  301. {$ifndef STR_CONCAT_PROCS}
  302. (*
  303. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  304. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  305. function fpc_shortstr_concat(const s1, s2: shortstring): shortstring; compilerproc; [public, alias: 'FPC_SHORTSTR_CONCAT'];
  306. { expects that (r3) contains a pointer to the result r4 to s1, r5 to s2 }
  307. assembler;
  308. asm
  309. { load length s1 }
  310. lbz r6, 0(r4)
  311. { load length s2 }
  312. lbz r10, 0(r5)
  313. { length 0 for s1? }
  314. cmpldi cr7,r6,0
  315. { length 255 for s1? }
  316. subfic. r7,r6,255
  317. { length 0 for s2? }
  318. cmpldi cr1,r10,0
  319. { calculate min(length(s2),255-length(s1)) }
  320. subc r8,r7,r10 { r8 := r7 - r10 }
  321. cror 4*6+2,4*1+2,4*7+2
  322. subfe r7,r7,r7 { if r7 >= r10 then r7' := 0 else r7' := -1 }
  323. mtctr r6
  324. and r7,r8,r7 { if r7 >= r10 then r7' := 0 else r7' := r7-r10 }
  325. add r7,r7,r10 { if r7 >= r10 then r7' := r10 else r7' := r7 }
  326. mr r9,r3
  327. { calculate length of final string }
  328. add r8,r7,r6
  329. stb r8,0(r3)
  330. beq cr7, .Lcopys1loopDone
  331. .Lcopys1loop:
  332. lbzu r0,1(r4)
  333. stbu r0,1(r9)
  334. bdnz .Lcopys1loop
  335. .Lcopys1loopDone:
  336. mtctr r7
  337. beq cr6, .LconcatDone
  338. .Lcopys2loop:
  339. lbzu r0,1(r5)
  340. stbu r0,1(r9)
  341. bdnz .Lcopys2loop
  342. .LconcatDone:
  343. end;
  344. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  345. *)
  346. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  347. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  348. procedure fpc_shortstr_append_shortstr(var s1: shortstring; const s2: shortstring); compilerproc;
  349. { expects that results (r3) contains a pointer to the current string s1, r4 }
  350. { high(s1) and (r5) a pointer to the one that has to be concatenated }
  351. assembler; nostackframe;
  352. asm
  353. { load length s1 }
  354. lbz r6, 0(r3)
  355. { load length s2 }
  356. lbz r10, 0(r5)
  357. { length 0? }
  358. cmpld cr1,r6,r4
  359. cmpldi r10,0
  360. { calculate min(length(s2),high(result)-length(result)) }
  361. sub r9,r4,r6
  362. subc r8,r9,r10 { r8 := r9 - r10 }
  363. cror 4*7+2,4*0+2,4*1+2
  364. subfe r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  365. and r9,r8,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r10 }
  366. add r9,r9,r10 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  367. { calculate new length }
  368. add r10,r6,r9
  369. { load value to copy in ctr }
  370. mtctr r9
  371. { store new length }
  372. stb r10,0(r3)
  373. { go to last current character of result }
  374. add r3,r6,r3
  375. { if nothing to do, exit }
  376. beq cr7, .LShortStrAppendDone
  377. { and concatenate }
  378. .LShortStrAppendLoop:
  379. lbzu r10,1(r5)
  380. stbu r10,1(r3)
  381. bdnz .LShortStrAppendLoop
  382. .LShortStrAppendDone:
  383. end;
  384. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  385. {$endif STR_CONCAT_PROCS}
  386. (*
  387. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  388. function fpc_shortstr_compare(const dstr, sstr:shortstring): SizeInt; [public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  389. assembler;
  390. { TODO: improve, because the main compare loop does an unaligned access everytime.. :(
  391. TODO: needs some additional opcodes not yet known to the compiler :( }
  392. asm
  393. { load length sstr }
  394. lbz r9,0(r4)
  395. { load length dstr }
  396. lbz r10,0(r3)
  397. { save their difference for later and }
  398. { calculate min(length(sstr),length(dstr)) }
  399. subfc r7,r10,r9 { r0 := r9 - r10 }
  400. subfe r9,r9,r9 { if r9 >= r10 then r9' := 0 else r9' := -1 }
  401. and r7,r7,r9 { if r9 >= r10 then r9' := 0 else r9' := r9-r8 }
  402. add r9,r10,r7 { if r9 >= r10 then r9' := r10 else r9' := r9 }
  403. { first compare qwords (length/4) }
  404. srdi. r5,r9,3
  405. { keep length mod 8 for the ends; note that the value in r9 <= 255
  406. so we can use rlwinm safely }
  407. rlwinm r9,r9,0,29,31
  408. { already check whether length mod 8 = 0 }
  409. cmpldi cr1,r9,0
  410. { so we can load r3 with 0, in case the strings both have length 0 }
  411. mr r8,r3
  412. li r3, 0
  413. { length div 8 in ctr for loop }
  414. mtctr r5
  415. { if length < 7, goto byte comparing }
  416. beq .LShortStrCompare1
  417. { setup for use of update forms of load/store with qwords }
  418. subi r4,r4,7
  419. subi r8,r8,7
  420. .LShortStrCompare4Loop:
  421. ldu r3,8(r4)
  422. ldu r10,8(r8)
  423. sub. r3,r3,r10
  424. bdnzt cr0+eq,.LShortStrCompare4Loop
  425. { r3 contains result if we stopped because of "ne" flag }
  426. bne .LShortStrCompareDone
  427. { setup for use of update forms of load/store with bytes }
  428. addi r4,r4,7
  429. addi r8,r8,7
  430. .LShortStrCompare1:
  431. { if comparelen mod 4 = 0, skip this and return the difference in }
  432. { lengths }
  433. beq cr1,.LShortStrCompareLen
  434. mtctr r9
  435. .LShortStrCompare1Loop:
  436. lbzu r3,1(r4)
  437. lbzu r10,1(r8)
  438. sub. r3,r3,r10
  439. bdnzt cr0+eq,.LShortStrCompare1Loop
  440. bne .LShortStrCompareDone
  441. .LShortStrCompareLen:
  442. { also return result in flags, maybe we can use this in the CG }
  443. mr. r3,r3
  444. .LShortStrCompareDone:
  445. end;
  446. *)
  447. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  448. {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  449. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  450. assembler; nostackframe;
  451. {
  452. r3: result address
  453. r4: high(result)
  454. r5: p (source)
  455. }
  456. asm
  457. { nil? }
  458. mr r8, p
  459. cmpldi p, 0
  460. { load the begin of the string in the data cache }
  461. dcbt 0, p
  462. { maxlength }
  463. mr r10,r4
  464. mtctr r10
  465. { at LStrPasDone, we set the length of the result to 255 - r10 - r4 }
  466. bne .LStrPasStart
  467. { put zero into r10 }
  468. mr r10, r8
  469. b .LStrPasDone
  470. .LStrPasStart:
  471. { save address for at the end and use r7 in loop }
  472. mr r7,r3
  473. { no "subi r7,r7,1" because the first byte = length byte }
  474. subi r8,r8,1
  475. .LStrPasLoop:
  476. lbzu r10,1(r8)
  477. cmplwi cr0,r10,0
  478. stbu r10,1(r7)
  479. bdnzf cr0*4+eq, .LStrPasLoop
  480. { if we stopped because of a terminating #0, decrease the length by 1 }
  481. cntlzd r4,r10
  482. { get remaining count for length }
  483. mfctr r10
  484. { if r10 was zero (-> stopped because of zero byte), then r4 will be 64 }
  485. { (64 leading zero bits) -> shr 6 = 1, otherwise this will be zero }
  486. srdi r4,r4,6
  487. subfic r10,r10,255
  488. sub r10,r10,r4
  489. { store length }
  490. .LStrPasDone:
  491. stb r10,0(r3)
  492. end;
  493. {$endif FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  494. (*
  495. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  496. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  497. function fpc_pchar_length(p:pchar):longint;assembler;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif} nostackframe;
  498. {$include strlen.inc}
  499. {$endif FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  500. *)
  501. {$define FPC_SYSTEM_HAS_GET_FRAME}
  502. function get_frame:pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  503. asm
  504. { all abi's I know use r1 as stack pointer }
  505. mr r3, r1
  506. end;
  507. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  508. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  509. asm
  510. cmpldi r3,0
  511. beq .Lcaller_addr_frame_null
  512. ld r3, 0(r3)
  513. cmpldi r3,0
  514. beq .Lcaller_addr_frame_null
  515. ld r3, 16(r3)
  516. .Lcaller_addr_frame_null:
  517. end;
  518. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  519. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  520. asm
  521. cmpldi r3,0
  522. beq .Lcaller_frame_null
  523. ld r3, 0(r3)
  524. .Lcaller_frame_null:
  525. end;
  526. {$define FPC_SYSTEM_HAS_SPTR}
  527. Function Sptr : Pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  528. asm
  529. mr r3,r1
  530. end;
  531. {****************************************************************************
  532. Str()
  533. ****************************************************************************}
  534. { int_str: generic implementation is used for now }
  535. {****************************************************************************
  536. Multithreading
  537. ****************************************************************************}
  538. { do a thread save inc/dec }
  539. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  540. function declocked(var l : longint) : boolean;assembler;nostackframe;
  541. { input: address of l in r3 }
  542. { output: boolean indicating whether l is zero after decrementing }
  543. asm
  544. .LDecLockedLoop:
  545. lwarx r10,0,r3
  546. subi r10,r10,1
  547. stwcx. r10,0,r3
  548. bne- .LDecLockedLoop
  549. cntlzd r3,r10
  550. srdi r3,r3,6
  551. end;
  552. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  553. procedure inclocked(var l : longint);assembler;nostackframe;
  554. asm
  555. .LIncLockedLoop:
  556. lwarx r10,0,r3
  557. addi r10,r10,1
  558. stwcx. r10,0,r3
  559. bne- .LIncLockedLoop
  560. end;
  561. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  562. function declocked(var l : int64) : boolean;assembler;nostackframe;
  563. { input: address of l in r3 }
  564. { output: boolean indicating whether l is zero after decrementing }
  565. asm
  566. .LDecLockedLoop:
  567. ldarx r10,0,r3
  568. subi r10,r10,1
  569. stdcx. r10,0,r3
  570. bne- .LDecLockedLoop
  571. cntlzd r3,r10
  572. srdi r3,r3,6
  573. end;
  574. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  575. procedure inclocked(var l : int64);assembler;nostackframe;
  576. asm
  577. .LIncLockedLoop:
  578. ldarx r10,0,r3
  579. addi r10,r10,1
  580. stdcx. r10,0,r3
  581. bne- .LIncLockedLoop
  582. end;
  583. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  584. { input: address of target in r3 }
  585. { output: target-1 in r3 }
  586. { side-effect: target := target-1 }
  587. asm
  588. .LInterLockedDecLoop:
  589. lwarx r10,0,r3
  590. subi r10,r10,1
  591. stwcx. r10,0,r3
  592. bne .LInterLockedDecLoop
  593. mr r3,r10
  594. end;
  595. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  596. { input: address of target in r3 }
  597. { output: target+1 in r3 }
  598. { side-effect: target := target+1 }
  599. asm
  600. .LInterLockedIncLoop:
  601. lwarx r10,0,r3
  602. addi r10,r10,1
  603. stwcx. r10,0,r3
  604. bne .LInterLockedIncLoop
  605. mr r3,r10
  606. end;
  607. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  608. { input: address of target in r3, source in r4 }
  609. { output: target in r3 }
  610. { side-effect: target := source }
  611. asm
  612. .LInterLockedXchgLoop:
  613. lwarx r10,0,r3
  614. stwcx. r4,0,r3
  615. bne .LInterLockedXchgLoop
  616. mr r3,r10
  617. end;
  618. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  619. { input: address of target in r3, source in r4 }
  620. { output: target in r3 }
  621. { side-effect: target := target+source }
  622. asm
  623. .LInterLockedXchgAddLoop:
  624. lwarx r10,0,r3
  625. add r10,r10,r4
  626. stwcx. r10,0,r3
  627. bne .LInterLockedXchgAddLoop
  628. sub r3,r10,r4
  629. end;
  630. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  631. { input: address of target in r3, newvalue in r4, comparand in r5 }
  632. { output: value stored in target before entry of the function }
  633. { side-effect: NewValue stored in target if (target = comparand) }
  634. asm
  635. .LInterlockedCompareExchangeLoop:
  636. lwarx r10,0,r3
  637. // lwarx performs an unsigned load -> sign extend since arguments are
  638. // longints
  639. extsw r10,r10
  640. sub r9,r10,r5
  641. addic r9,r9,-1
  642. subfe r9,r9,r9
  643. and r8,r4,r9
  644. andc r7,r10,r9
  645. or r6,r7,r8
  646. stwcx. r6,0,r3
  647. bne .LInterlockedCompareExchangeLoop
  648. mr r3, r10
  649. end;
  650. function InterLockedDecrement64(var Target: Int64) : Int64; assembler; nostackframe;
  651. { input: address of target in r3 }
  652. { output: target-1 in r3 }
  653. { side-effect: target := target-1 }
  654. asm
  655. .LInterLockedDecLoop:
  656. ldarx r10,0,r3
  657. subi r10,r10,1
  658. stdcx. r10,0,r3
  659. bne .LInterLockedDecLoop
  660. mr r3,r10
  661. end;
  662. function InterLockedIncrement64(var Target: Int64) : Int64; assembler; nostackframe;
  663. { input: address of target in r3 }
  664. { output: target+1 in r3 }
  665. { side-effect: target := target+1 }
  666. asm
  667. .LInterLockedIncLoop:
  668. ldarx r10,0,r3
  669. addi r10,r10,1
  670. stdcx. r10,0,r3
  671. bne .LInterLockedIncLoop
  672. mr r3,r10
  673. end;
  674. function InterLockedExchange64(var Target: Int64; Source : Int64) : Int64; assembler; nostackframe;
  675. { input: address of target in r3, source in r4 }
  676. { output: target in r3 }
  677. { side-effect: target := source }
  678. asm
  679. .LInterLockedXchgLoop:
  680. ldarx r10,0,r3
  681. stdcx. r4,0,r3
  682. bne .LInterLockedXchgLoop
  683. mr r3,r10
  684. end;
  685. function InterLockedExchangeAdd64(var Target: Int64; Source : Int64) : Int64; assembler; nostackframe;
  686. { input: address of target in r3, source in r4 }
  687. { output: target in r3 }
  688. { side-effect: target := target+source }
  689. asm
  690. .LInterLockedXchgAddLoop:
  691. ldarx r10,0,r3
  692. add r10,r10,r4
  693. stdcx. r10,0,r3
  694. bne .LInterLockedXchgAddLoop
  695. sub r3,r10,r4
  696. end;
  697. function InterlockedCompareExchange64(var Target: Int64; NewValue: Int64; Comperand: Int64): Int64; assembler; nostackframe;
  698. { input: address of target in r3, newvalue in r4, comparand in r5 }
  699. { output: value stored in target before entry of the function }
  700. { side-effect: NewValue stored in target if (target = comparand) }
  701. asm
  702. .LInterlockedCompareExchangeLoop:
  703. ldarx r10,0,r3
  704. sub r9,r10,r5
  705. addic r9,r9,-1
  706. subfe r9,r9,r9
  707. and r8,r4,r9
  708. andc r7,r10,r9
  709. or r6,r7,r8
  710. stdcx. r6,0,r3
  711. bne .LInterlockedCompareExchangeLoop
  712. mr r3, r10
  713. end;
  714. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  715. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  716. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  717. asm
  718. isync
  719. end;
  720. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  721. begin
  722. { reads imply barrier on earlier reads depended on }
  723. end;
  724. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  725. asm
  726. isync
  727. {$ifdef FPC_HAS_LWSYNC}
  728. lwsync
  729. {$endif}
  730. end;
  731. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  732. asm
  733. {$ifdef FPC_HAS_LWSYNC}
  734. lwsync
  735. {$endif}
  736. end;
  737. {$endif}