powerpc64.inc 23 KB

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