powerpc64.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  450. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  451. assembler; nostackframe;
  452. {$else FPC_STRTOSHORTSTRINGPROC}
  453. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  454. assembler; nostackframe;
  455. {$define FPC_STRPASPROC}
  456. {$endif FPC_STRTOSHORTSTRINGPROC}
  457. {$include strpas.inc}
  458. {$undef FPC_STRPASPROC}
  459. {$endif FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  460. (*
  461. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  462. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  463. function fpc_pchar_length(p:pchar):longint;assembler;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif} nostackframe;
  464. {$include strlen.inc}
  465. {$endif FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  466. *)
  467. {$define FPC_SYSTEM_HAS_GET_FRAME}
  468. function get_frame:pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  469. asm
  470. { all abi's I know use r1 as stack pointer }
  471. mr r3, r1
  472. end;
  473. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  474. function get_caller_addr(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  475. asm
  476. cmpldi r3,0
  477. beq .Lcaller_addr_frame_null
  478. ld r3, 0(r3)
  479. cmpldi r3,0
  480. beq .Lcaller_addr_frame_null
  481. ld r3, 16(r3)
  482. .Lcaller_addr_frame_null:
  483. end;
  484. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  485. function get_caller_frame(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  486. asm
  487. cmpldi r3,0
  488. beq .Lcaller_frame_null
  489. ld r3, 0(r3)
  490. .Lcaller_frame_null:
  491. end;
  492. {$define FPC_SYSTEM_HAS_SPTR}
  493. Function Sptr : Pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif} nostackframe;
  494. asm
  495. mr r3,r1
  496. end;
  497. {****************************************************************************
  498. Str()
  499. ****************************************************************************}
  500. { int_str: generic implementation is used for now }
  501. {****************************************************************************
  502. Multithreading
  503. ****************************************************************************}
  504. { do a thread save inc/dec }
  505. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  506. function declocked(var l : longint) : boolean;assembler;nostackframe;
  507. { input: address of l in r3 }
  508. { output: boolean indicating whether l is zero after decrementing }
  509. asm
  510. .LDecLockedLoop:
  511. lwarx r10,0,r3
  512. subi r10,r10,1
  513. stwcx. r10,0,r3
  514. bne- .LDecLockedLoop
  515. cntlzd r3,r10
  516. srdi r3,r3,6
  517. end;
  518. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  519. procedure inclocked(var l : longint);assembler;nostackframe;
  520. asm
  521. .LIncLockedLoop:
  522. lwarx r10,0,r3
  523. addi r10,r10,1
  524. stwcx. r10,0,r3
  525. bne- .LIncLockedLoop
  526. end;
  527. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  528. function declocked(var l : int64) : boolean;assembler;nostackframe;
  529. { input: address of l in r3 }
  530. { output: boolean indicating whether l is zero after decrementing }
  531. asm
  532. .LDecLockedLoop:
  533. ldarx r10,0,r3
  534. subi r10,r10,1
  535. stdcx. r10,0,r3
  536. bne- .LDecLockedLoop
  537. cntlzd r3,r10
  538. srdi r3,r3,6
  539. end;
  540. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  541. procedure inclocked(var l : int64);assembler;nostackframe;
  542. asm
  543. .LIncLockedLoop:
  544. ldarx r10,0,r3
  545. addi r10,r10,1
  546. stdcx. r10,0,r3
  547. bne- .LIncLockedLoop
  548. end;
  549. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  550. { input: address of target in r3 }
  551. { output: target-1 in r3 }
  552. { side-effect: target := target-1 }
  553. asm
  554. .LInterLockedDecLoop:
  555. lwarx r10,0,r3
  556. subi r10,r10,1
  557. stwcx. r10,0,r3
  558. bne .LInterLockedDecLoop
  559. mr r3,r10
  560. end;
  561. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  562. { input: address of target in r3 }
  563. { output: target+1 in r3 }
  564. { side-effect: target := target+1 }
  565. asm
  566. .LInterLockedIncLoop:
  567. lwarx r10,0,r3
  568. addi r10,r10,1
  569. stwcx. r10,0,r3
  570. bne .LInterLockedIncLoop
  571. mr r3,r10
  572. end;
  573. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  574. { input: address of target in r3, source in r4 }
  575. { output: target in r3 }
  576. { side-effect: target := source }
  577. asm
  578. .LInterLockedXchgLoop:
  579. lwarx r10,0,r3
  580. stwcx. r4,0,r3
  581. bne .LInterLockedXchgLoop
  582. mr r3,r10
  583. end;
  584. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  585. { input: address of target in r3, source in r4 }
  586. { output: target in r3 }
  587. { side-effect: target := target+source }
  588. asm
  589. .LInterLockedXchgAddLoop:
  590. lwarx r10,0,r3
  591. add r10,r10,r4
  592. stwcx. r10,0,r3
  593. bne .LInterLockedXchgAddLoop
  594. sub r3,r10,r4
  595. end;
  596. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  597. { input: address of target in r3, newvalue in r4, comparand in r5 }
  598. { output: value stored in target before entry of the function }
  599. { side-effect: NewValue stored in target if (target = comparand) }
  600. asm
  601. .LInterlockedCompareExchangeLoop:
  602. lwarx r10,0,r3
  603. // lwarx performs an unsigned load -> sign extend since arguments are
  604. // longints
  605. extsw r10,r10
  606. sub r9,r10,r5
  607. addic r9,r9,-1
  608. subfe r9,r9,r9
  609. and r8,r4,r9
  610. andc r7,r10,r9
  611. or r6,r7,r8
  612. stwcx. r6,0,r3
  613. bne .LInterlockedCompareExchangeLoop
  614. mr r3, r10
  615. end;
  616. function InterLockedDecrement64(var Target: Int64) : Int64; assembler; nostackframe;
  617. { input: address of target in r3 }
  618. { output: target-1 in r3 }
  619. { side-effect: target := target-1 }
  620. asm
  621. .LInterLockedDecLoop:
  622. ldarx r10,0,r3
  623. subi r10,r10,1
  624. stdcx. r10,0,r3
  625. bne .LInterLockedDecLoop
  626. mr r3,r10
  627. end;
  628. function InterLockedIncrement64(var Target: Int64) : Int64; assembler; nostackframe;
  629. { input: address of target in r3 }
  630. { output: target+1 in r3 }
  631. { side-effect: target := target+1 }
  632. asm
  633. .LInterLockedIncLoop:
  634. ldarx r10,0,r3
  635. addi r10,r10,1
  636. stdcx. r10,0,r3
  637. bne .LInterLockedIncLoop
  638. mr r3,r10
  639. end;
  640. function InterLockedExchange64(var Target: Int64; Source : Int64) : Int64; assembler; nostackframe;
  641. { input: address of target in r3, source in r4 }
  642. { output: target in r3 }
  643. { side-effect: target := source }
  644. asm
  645. .LInterLockedXchgLoop:
  646. ldarx r10,0,r3
  647. stdcx. r4,0,r3
  648. bne .LInterLockedXchgLoop
  649. mr r3,r10
  650. end;
  651. function InterLockedExchangeAdd64(var Target: Int64; Source : Int64) : Int64; assembler; nostackframe;
  652. { input: address of target in r3, source in r4 }
  653. { output: target in r3 }
  654. { side-effect: target := target+source }
  655. asm
  656. .LInterLockedXchgAddLoop:
  657. ldarx r10,0,r3
  658. add r10,r10,r4
  659. stdcx. r10,0,r3
  660. bne .LInterLockedXchgAddLoop
  661. sub r3,r10,r4
  662. end;
  663. function InterlockedCompareExchange64(var Target: Int64; NewValue: Int64; Comperand: Int64): Int64; assembler; nostackframe;
  664. { input: address of target in r3, newvalue in r4, comparand in r5 }
  665. { output: value stored in target before entry of the function }
  666. { side-effect: NewValue stored in target if (target = comparand) }
  667. asm
  668. .LInterlockedCompareExchangeLoop:
  669. ldarx r10,0,r3
  670. sub r9,r10,r5
  671. addic r9,r9,-1
  672. subfe r9,r9,r9
  673. and r8,r4,r9
  674. andc r7,r10,r9
  675. or r6,r7,r8
  676. stdcx. r6,0,r3
  677. bne .LInterlockedCompareExchangeLoop
  678. mr r3, r10
  679. end;
  680. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  681. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  682. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  683. asm
  684. isync
  685. end;
  686. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  687. begin
  688. { reads imply barrier on earlier reads depended on }
  689. end;
  690. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  691. asm
  692. isync
  693. {$ifdef FPC_HAS_LWSYNC}
  694. lwsync
  695. {$endif}
  696. end;
  697. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  698. asm
  699. {$ifdef FPC_HAS_LWSYNC}
  700. lwsync
  701. {$endif}
  702. end;
  703. {$endif}