powerpc64.inc 23 KB

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