cgcpu.pas 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. globtype, symtype, symdef, symsym,
  22. cgbase, cgobj,cgppc,
  23. aasmbase, aasmcpu, aasmtai,aasmdata,
  24. cpubase, cpuinfo, cgutils, rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcgppcgen)
  28. procedure init_register_allocators; override;
  29. procedure done_register_allocators; override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_load_ref_cgpara(list: TAsmList; size: tcgsize; const r: treference;
  36. const paraloc: tcgpara); override;
  37. procedure a_call_name(list: TAsmList; const s: string; weak: boolean); override;
  38. procedure a_call_reg(list: TAsmList; reg: tregister); override;
  39. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  40. aint; reg: TRegister); override;
  41. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  42. dst: TRegister); override;
  43. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  44. size: tcgsize; a: aint; src, dst: tregister); override;
  45. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  46. size: tcgsize; src1, src2, dst: tregister); override;
  47. { move instructions }
  48. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: aint; reg:
  49. tregister); override;
  50. { loads the memory pointed to by ref into register reg }
  51. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const
  52. Ref: treference; reg: tregister); override;
  53. procedure a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1,
  54. reg2: tregister); override;
  55. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); override;
  56. procedure a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize; a: aint; const sreg: tsubsetregister); override;
  57. { comparison operations }
  58. procedure a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op:
  59. topcmp; a: aint; reg: tregister;
  60. l: tasmlabel); override;
  61. procedure a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op:
  62. topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  63. procedure a_jmp_name(list: TAsmList; const s: string); override;
  64. procedure a_jmp_always(list: TAsmList; l: tasmlabel); override;
  65. procedure a_jmp_flags(list: TAsmList; const f: TResFlags; l: tasmlabel);
  66. override;
  67. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags;
  68. reg: TRegister); override;
  69. { need to override this for ppc64 to avoid calling CG methods which allocate
  70. registers during creation of the interface wrappers to subtract ioffset from
  71. the self pointer. But register allocation does not take place for them (which
  72. would probably be the generic fix) so we need to have a specialized method
  73. that uses the R11 scratch register in these cases.
  74. At the same time this allows > 32 bit offsets as well.
  75. }
  76. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);override;
  77. procedure g_profilecode(list: TAsmList); override;
  78. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe:
  79. boolean); override;
  80. procedure g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  81. boolean); override;
  82. procedure g_save_registers(list: TAsmList); override;
  83. procedure g_restore_registers(list: TAsmList); override;
  84. procedure a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  85. tregister); override;
  86. procedure g_concatcopy(list: TAsmList; const source, dest: treference;
  87. len: aint); override;
  88. private
  89. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  90. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  91. { returns whether a reference can be used immediately in a powerpc }
  92. { instruction }
  93. function issimpleref(const ref: treference): boolean;
  94. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  95. procedure a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  96. ref: treference); override;
  97. { returns the lowest numbered FP register in use, and the number of used FP registers
  98. for the current procedure }
  99. procedure calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  100. { returns the lowest numbered GP register in use, and the number of used GP registers
  101. for the current procedure }
  102. procedure calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  103. { generates code to call a method with the given string name. The boolean options
  104. control code generation. If prependDot is true, a single dot character is prepended to
  105. the string, if addNOP is true a single NOP instruction is added after the call, and
  106. if includeCall is true, the method is marked as having a call, not if false. This
  107. option is particularly useful to prevent generation of a larger stack frame for the
  108. register save and restore helper functions. }
  109. procedure a_call_name_direct(list: TAsmList; s: string; weak: boolean; prependDot : boolean;
  110. addNOP : boolean; includeCall : boolean = true);
  111. procedure a_jmp_name_direct(list : TAsmList; s : string; prependDot : boolean);
  112. { emits code to store the given value a into the TOC (if not already in there), and load it from there
  113. as well }
  114. procedure loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  115. procedure profilecode_savepara(para : tparavarsym; list : TAsmList);
  116. procedure profilecode_restorepara(para : tparavarsym; list : TAsmList);
  117. end;
  118. procedure create_codegen;
  119. const
  120. TShiftOpCG2AsmOpConst : array[boolean, OP_SAR..OP_SHR] of TAsmOp = (
  121. (A_SRAWI, A_SLWI, A_SRWI), (A_SRADI, A_SLDI, A_SRDI)
  122. );
  123. implementation
  124. uses
  125. sysutils, cclasses,
  126. globals, verbose, systems, cutils,
  127. symconst, fmodule,
  128. rgobj, tgobj, cpupi, procinfo, paramgr, cpupara;
  129. function is_signed_cgsize(const size : TCgSize) : Boolean;
  130. begin
  131. case size of
  132. OS_S8,OS_S16,OS_S32,OS_S64 : result := true;
  133. OS_8,OS_16,OS_32,OS_64 : result := false;
  134. else
  135. internalerror(2006050701);
  136. end;
  137. end;
  138. {$push}
  139. {$r-}
  140. {$q-}
  141. { helper function which calculate "magic" values for replacement of unsigned
  142. division by constant operation by multiplication. See the PowerPC compiler
  143. developer manual for more information }
  144. procedure getmagic_unsignedN(const N : byte; const d : aWord;
  145. out magic_m : aWord; out magic_add : boolean; out magic_shift : byte);
  146. var
  147. p : aInt;
  148. nc, delta, q1, r1, q2, r2, two_N_minus_1 : aWord;
  149. begin
  150. assert(d > 0);
  151. two_N_minus_1 := aWord(1) shl (N-1);
  152. magic_add := false;
  153. {$push}
  154. {$warnings off }
  155. nc := aWord(-1) - (-d) mod d;
  156. {$pop}
  157. p := N-1; { initialize p }
  158. q1 := two_N_minus_1 div nc; { initialize q1 = 2p/nc }
  159. r1 := two_N_minus_1 - q1*nc; { initialize r1 = rem(2p,nc) }
  160. q2 := (two_N_minus_1-1) div d; { initialize q2 = (2p-1)/d }
  161. r2 := (two_N_minus_1-1) - q2*d; { initialize r2 = rem((2p-1),d) }
  162. repeat
  163. inc(p);
  164. if (r1 >= (nc - r1)) then begin
  165. q1 := 2 * q1 + 1; { update q1 }
  166. r1 := 2*r1 - nc; { update r1 }
  167. end else begin
  168. q1 := 2*q1; { update q1 }
  169. r1 := 2*r1; { update r1 }
  170. end;
  171. if ((r2 + 1) >= (d - r2)) then begin
  172. if (q2 >= (two_N_minus_1-1)) then
  173. magic_add := true;
  174. q2 := 2*q2 + 1; { update q2 }
  175. r2 := 2*r2 + 1 - d; { update r2 }
  176. end else begin
  177. if (q2 >= two_N_minus_1) then
  178. magic_add := true;
  179. q2 := 2*q2; { update q2 }
  180. r2 := 2*r2 + 1; { update r2 }
  181. end;
  182. delta := d - 1 - r2;
  183. until not ((p < (2*N)) and ((q1 < delta) or ((q1 = delta) and (r1 = 0))));
  184. magic_m := q2 + 1; { resulting magic number }
  185. magic_shift := p - N; { resulting shift }
  186. end;
  187. { helper function which calculate "magic" values for replacement of signed
  188. division by constant operation by multiplication. See the PowerPC compiler
  189. developer manual for more information }
  190. procedure getmagic_signedN(const N : byte; const d : aInt;
  191. out magic_m : aInt; out magic_s : aInt);
  192. var
  193. p : aInt;
  194. ad, anc, delta, q1, r1, q2, r2, t : aWord;
  195. two_N_minus_1 : aWord;
  196. begin
  197. assert((d < -1) or (d > 1));
  198. two_N_minus_1 := aWord(1) shl (N-1);
  199. ad := abs(d);
  200. t := two_N_minus_1 + (aWord(d) shr (N-1));
  201. anc := t - 1 - t mod ad; { absolute value of nc }
  202. p := (N-1); { initialize p }
  203. q1 := two_N_minus_1 div anc; { initialize q1 = 2p/abs(nc) }
  204. r1 := two_N_minus_1 - q1*anc; { initialize r1 = rem(2p,abs(nc)) }
  205. q2 := two_N_minus_1 div ad; { initialize q2 = 2p/abs(d) }
  206. r2 := two_N_minus_1 - q2*ad; { initialize r2 = rem(2p,abs(d)) }
  207. repeat
  208. inc(p);
  209. q1 := 2*q1; { update q1 = 2p/abs(nc) }
  210. r1 := 2*r1; { update r1 = rem(2p/abs(nc)) }
  211. if (r1 >= anc) then begin { must be unsigned comparison }
  212. inc(q1);
  213. dec(r1, anc);
  214. end;
  215. q2 := 2*q2; { update q2 = 2p/abs(d) }
  216. r2 := 2*r2; { update r2 = rem(2p/abs(d)) }
  217. if (r2 >= ad) then begin { must be unsigned comparison }
  218. inc(q2);
  219. dec(r2, ad);
  220. end;
  221. delta := ad - r2;
  222. until not ((q1 < delta) or ((q1 = delta) and (r1 = 0)));
  223. magic_m := q2 + 1;
  224. if (d < 0) then begin
  225. magic_m := -magic_m; { resulting magic number }
  226. end;
  227. magic_s := p - N; { resulting shift }
  228. end;
  229. {$pop}
  230. { finds positive and negative powers of two of the given value, returning the
  231. power and whether it's a negative power or not in addition to the actual result
  232. of the function }
  233. function ispowerof2(value : aInt; out power : byte; out neg : boolean) : boolean;
  234. var
  235. i : longint;
  236. hl : aInt;
  237. begin
  238. neg := false;
  239. { also try to find negative power of two's by negating if the
  240. value is negative. low(aInt) is special because it can not be
  241. negated. Simply return the appropriate values for it }
  242. if (value < 0) then begin
  243. neg := true;
  244. if (value = low(aInt)) then begin
  245. power := sizeof(aInt)*8-1;
  246. result := true;
  247. exit;
  248. end;
  249. value := -value;
  250. end;
  251. if ((value and (value-1)) <> 0) then begin
  252. result := false;
  253. exit;
  254. end;
  255. hl := 1;
  256. for i := 0 to (sizeof(aInt)*8-1) do begin
  257. if (hl = value) then begin
  258. result := true;
  259. power := i;
  260. exit;
  261. end;
  262. hl := hl shl 1;
  263. end;
  264. end;
  265. { returns the number of instruction required to load the given integer into a register.
  266. This is basically a stripped down version of a_load_const_reg, increasing a counter
  267. instead of emitting instructions. }
  268. function getInstructionLength(a : aint) : longint;
  269. function get32bitlength(a : longint; var length : longint) : boolean; inline;
  270. var
  271. is_half_signed : byte;
  272. begin
  273. { if the lower 16 bits are zero, do a single LIS }
  274. if (smallint(a) = 0) and ((a shr 16) <> 0) then begin
  275. inc(length);
  276. get32bitlength := longint(a) < 0;
  277. end else begin
  278. is_half_signed := ord(smallint(lo(a)) < 0);
  279. inc(length);
  280. if smallint(hi(a) + is_half_signed) <> 0 then
  281. inc(length);
  282. get32bitlength := (smallint(a) < 0) or (a < 0);
  283. end;
  284. end;
  285. var
  286. extendssign : boolean;
  287. begin
  288. result := 0;
  289. if (lo(a) = 0) and (hi(a) <> 0) then begin
  290. get32bitlength(hi(a), result);
  291. inc(result);
  292. end else begin
  293. extendssign := get32bitlength(lo(a), result);
  294. if (extendssign) and (hi(a) = 0) then
  295. inc(result)
  296. else if (not
  297. ((extendssign and (longint(hi(a)) = -1)) or
  298. ((not extendssign) and (hi(a)=0)))
  299. ) then begin
  300. get32bitlength(hi(a), result);
  301. inc(result);
  302. end;
  303. end;
  304. end;
  305. procedure tcgppc.init_register_allocators;
  306. begin
  307. inherited init_register_allocators;
  308. if (target_info.system <> system_powerpc64_darwin) then
  309. // r13 is tls, do not use, r2 is not available
  310. rg[R_INTREGISTER] := trgintcpu.create(R_INTREGISTER, R_SUBWHOLE,
  311. [{$ifdef user0} RS_R0, {$endif} RS_R3, RS_R4, RS_R5, RS_R6, RS_R7, RS_R8,
  312. RS_R9, RS_R10, RS_R11, RS_R12, RS_R31, RS_R30, RS_R29,
  313. RS_R28, RS_R27, RS_R26, RS_R25, RS_R24, RS_R23, RS_R22,
  314. RS_R21, RS_R20, RS_R19, RS_R18, RS_R17, RS_R16, RS_R15,
  315. RS_R14], first_int_imreg, [])
  316. else
  317. { special for darwin/ppc64: r2 available volatile, r13 = tls }
  318. rg[R_INTREGISTER] := trgintcpu.create(R_INTREGISTER, R_SUBWHOLE,
  319. [{$ifdef user0} RS_R0, {$endif} RS_R2, RS_R3, RS_R4, RS_R5, RS_R6, RS_R7, RS_R8,
  320. RS_R9, RS_R10, RS_R11, RS_R12, RS_R31, RS_R30, RS_R29,
  321. RS_R28, RS_R27, RS_R26, RS_R25, RS_R24, RS_R23, RS_R22,
  322. RS_R21, RS_R20, RS_R19, RS_R18, RS_R17, RS_R16, RS_R15,
  323. RS_R14], first_int_imreg, []);
  324. rg[R_FPUREGISTER] := trgcpu.create(R_FPUREGISTER, R_SUBNONE,
  325. [RS_F0, RS_F1, RS_F2, RS_F3, RS_F4, RS_F5, RS_F6, RS_F7, RS_F8, RS_F9,
  326. RS_F10, RS_F11, RS_F12, RS_F13, RS_F31, RS_F30, RS_F29, RS_F28, RS_F27,
  327. RS_F26, RS_F25, RS_F24, RS_F23, RS_F22, RS_F21, RS_F20, RS_F19, RS_F18,
  328. RS_F17, RS_F16, RS_F15, RS_F14], first_fpu_imreg, []);
  329. { TODO: FIX ME}
  330. rg[R_MMREGISTER] := trgcpu.create(R_MMREGISTER, R_SUBNONE,
  331. [RS_M0, RS_M1, RS_M2], first_mm_imreg, []);
  332. end;
  333. procedure tcgppc.done_register_allocators;
  334. begin
  335. rg[R_INTREGISTER].free;
  336. rg[R_FPUREGISTER].free;
  337. rg[R_MMREGISTER].free;
  338. inherited done_register_allocators;
  339. end;
  340. procedure tcgppc.a_load_ref_cgpara(list: TAsmList; size: tcgsize; const r:
  341. treference; const paraloc: tcgpara);
  342. var
  343. tmpref, ref: treference;
  344. location: pcgparalocation;
  345. sizeleft: aint;
  346. adjusttail : boolean;
  347. begin
  348. location := paraloc.location;
  349. tmpref := r;
  350. sizeleft := paraloc.intsize;
  351. adjusttail := false;
  352. while assigned(location) do begin
  353. paramanager.allocparaloc(list,location);
  354. case location^.loc of
  355. LOC_REGISTER, LOC_CREGISTER:
  356. begin
  357. if not(size in [OS_NO,OS_128,OS_S128]) then
  358. a_load_ref_reg(list, size, location^.size, tmpref,
  359. location^.register)
  360. else begin
  361. { load non-integral sized memory location into register. This
  362. memory location be 1-sizeleft byte sized.
  363. Always assume that this memory area is properly aligned, eg. start
  364. loading the larger quantities for "odd" quantities first }
  365. case sizeleft of
  366. 1,2,4,8 :
  367. a_load_ref_reg(list, int_cgsize(sizeleft), location^.size, tmpref,
  368. location^.register);
  369. 3 : begin
  370. a_reg_alloc(list, NR_R12);
  371. a_load_ref_reg(list, OS_16, location^.size, tmpref,
  372. NR_R12);
  373. inc(tmpref.offset, tcgsize2size[OS_16]);
  374. a_load_ref_reg(list, OS_8, location^.size, tmpref,
  375. location^.register);
  376. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R12, 8, 40));
  377. a_reg_dealloc(list, NR_R12);
  378. end;
  379. 5 : begin
  380. a_reg_alloc(list, NR_R12);
  381. a_load_ref_reg(list, OS_32, location^.size, tmpref, NR_R12);
  382. inc(tmpref.offset, tcgsize2size[OS_32]);
  383. a_load_ref_reg(list, OS_8, location^.size, tmpref, location^.register);
  384. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R12, 8, 24));
  385. a_reg_dealloc(list, NR_R12);
  386. end;
  387. 6 : begin
  388. a_reg_alloc(list, NR_R12);
  389. a_load_ref_reg(list, OS_32, location^.size, tmpref, NR_R12);
  390. inc(tmpref.offset, tcgsize2size[OS_32]);
  391. a_load_ref_reg(list, OS_16, location^.size, tmpref, location^.register);
  392. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R12, 16, 16));
  393. a_reg_dealloc(list, NR_R12);
  394. end;
  395. 7 : begin
  396. a_reg_alloc(list, NR_R12);
  397. a_reg_alloc(list, NR_R0);
  398. a_load_ref_reg(list, OS_32, location^.size, tmpref, NR_R12);
  399. inc(tmpref.offset, tcgsize2size[OS_32]);
  400. a_load_ref_reg(list, OS_16, location^.size, tmpref, NR_R0);
  401. inc(tmpref.offset, tcgsize2size[OS_16]);
  402. a_load_ref_reg(list, OS_8, location^.size, tmpref, location^.register);
  403. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, NR_R0, NR_R12, 16, 16));
  404. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, location^.register, NR_R0, 8, 8));
  405. a_reg_dealloc(list, NR_R0);
  406. a_reg_dealloc(list, NR_R12);
  407. end;
  408. else begin
  409. { still > 8 bytes to load, so load data single register now }
  410. a_load_ref_reg(list, location^.size, location^.size, tmpref,
  411. location^.register);
  412. { the block is > 8 bytes, so we have to store any bytes not
  413. a multiple of the register size beginning with the MSB }
  414. adjusttail := true;
  415. end;
  416. end;
  417. if (adjusttail) and (sizeleft < sizeof(pint)) then
  418. a_op_const_reg(list, OP_SHL, OS_INT,
  419. (sizeof(pint) - sizeleft) * sizeof(pint),
  420. location^.register);
  421. end;
  422. end;
  423. LOC_REFERENCE:
  424. begin
  425. reference_reset_base(ref, location^.reference.index,
  426. location^.reference.offset,paraloc.alignment);
  427. g_concatcopy(list, tmpref, ref, sizeleft);
  428. if assigned(location^.next) then
  429. internalerror(2005010710);
  430. end;
  431. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  432. case location^.size of
  433. OS_F32, OS_F64:
  434. a_loadfpu_ref_reg(list, location^.size, location^.size, tmpref, location^.register);
  435. else
  436. internalerror(2002072801);
  437. end;
  438. LOC_VOID:
  439. { nothing to do }
  440. ;
  441. else
  442. internalerror(2002081103);
  443. end;
  444. inc(tmpref.offset, tcgsize2size[location^.size]);
  445. dec(sizeleft, tcgsize2size[location^.size]);
  446. location := location^.next;
  447. end;
  448. end;
  449. { calling a procedure by name }
  450. procedure tcgppc.a_call_name(list: TAsmList; const s: string; weak: boolean);
  451. begin
  452. if (target_info.system <> system_powerpc64_darwin) then
  453. a_call_name_direct(list, s, weak, false, true)
  454. else
  455. begin
  456. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s,weak)));
  457. include(current_procinfo.flags,pi_do_call);
  458. end;
  459. end;
  460. procedure tcgppc.a_call_name_direct(list: TAsmList; s: string; weak: boolean; prependDot : boolean; addNOP : boolean; includeCall : boolean);
  461. begin
  462. if (prependDot) then
  463. s := '.' + s;
  464. if not(weak) then
  465. list.concat(taicpu.op_sym(A_BL, current_asmdata.RefAsmSymbol(s)))
  466. else
  467. list.concat(taicpu.op_sym(A_BL, current_asmdata.WeakRefAsmSymbol(s)));
  468. if (addNOP) then
  469. list.concat(taicpu.op_none(A_NOP));
  470. if (includeCall) and
  471. assigned(current_procinfo) then
  472. include(current_procinfo.flags, pi_do_call);
  473. end;
  474. { calling a procedure by address }
  475. procedure tcgppc.a_call_reg(list: TAsmList; reg: tregister);
  476. var
  477. tmpref: treference;
  478. tempreg : TRegister;
  479. begin
  480. if (target_info.abi<>abi_powerpc_sysv) then
  481. inherited a_call_reg(list,reg)
  482. else if (not (cs_opt_size in current_settings.optimizerswitches)) then begin
  483. tempreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  484. { load actual function entry (reg contains the reference to the function descriptor)
  485. into tempreg }
  486. reference_reset_base(tmpref, reg, 0, sizeof(pint));
  487. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, tempreg);
  488. { save TOC pointer in stackframe }
  489. reference_reset_base(tmpref, NR_STACK_POINTER_REG, LA_RTOC_SYSV, 8);
  490. a_load_reg_ref(list, OS_ADDR, OS_ADDR, NR_RTOC, tmpref);
  491. { move actual function pointer to CTR register }
  492. list.concat(taicpu.op_reg(A_MTCTR, tempreg));
  493. { load new TOC pointer from function descriptor into RTOC register }
  494. reference_reset_base(tmpref, reg, tcgsize2size[OS_ADDR], 8);
  495. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  496. { load new environment pointer from function descriptor into R11 register }
  497. reference_reset_base(tmpref, reg, 2*tcgsize2size[OS_ADDR], 8);
  498. a_reg_alloc(list, NR_R11);
  499. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_R11);
  500. { call function }
  501. list.concat(taicpu.op_none(A_BCTRL));
  502. a_reg_dealloc(list, NR_R11);
  503. end else begin
  504. { call ptrgl helper routine which expects the pointer to the function descriptor
  505. in R11 }
  506. a_reg_alloc(list, NR_R11);
  507. a_load_reg_reg(list, OS_ADDR, OS_ADDR, reg, NR_R11);
  508. a_call_name_direct(list, '.ptrgl', false, false, false);
  509. a_reg_dealloc(list, NR_R11);
  510. end;
  511. { we need to load the old RTOC from stackframe because we changed it}
  512. reference_reset_base(tmpref, NR_STACK_POINTER_REG, LA_RTOC_SYSV, 8);
  513. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  514. include(current_procinfo.flags, pi_do_call);
  515. end;
  516. {********************** load instructions ********************}
  517. procedure tcgppc.a_load_const_reg(list: TAsmList; size: TCGSize; a: aint;
  518. reg: TRegister);
  519. { loads a 32 bit constant into the given register, using an optimal instruction sequence.
  520. This is either LIS, LI or LI+ADDIS.
  521. Returns true if during these operations the upper 32 bits were filled with 1 bits (e.g.
  522. sign extension was performed) }
  523. function load32bitconstant(list : TAsmList; size : TCGSize; a : longint;
  524. reg : TRegister) : boolean;
  525. var
  526. is_half_signed : byte;
  527. begin
  528. { if the lower 16 bits are zero, do a single LIS }
  529. if (smallint(a) = 0) and ((a shr 16) <> 0) then begin
  530. list.concat(taicpu.op_reg_const(A_LIS, reg, smallint(hi(a))));
  531. load32bitconstant := longint(a) < 0;
  532. end else begin
  533. is_half_signed := ord(smallint(lo(a)) < 0);
  534. list.concat(taicpu.op_reg_const(A_LI, reg, smallint(a and $ffff)));
  535. if smallint(hi(a) + is_half_signed) <> 0 then begin
  536. list.concat(taicpu.op_reg_reg_const(A_ADDIS, reg, reg, smallint(hi(a) + is_half_signed)));
  537. end;
  538. load32bitconstant := (smallint(a) < 0) or (a < 0);
  539. end;
  540. end;
  541. { loads a 32 bit constant into R0, using an optimal instruction sequence.
  542. This is either LIS, LI or LI+ORIS.
  543. Returns true if during these operations the upper 32 bits were filled with 1 bits (e.g.
  544. sign extension was performed) }
  545. function load32bitconstantR0(list : TAsmList; size : TCGSize; a : longint) : boolean;
  546. begin
  547. { if it's a value we can load with a single LI, do it }
  548. if (a >= low(smallint)) and (a <= high(smallint)) then begin
  549. list.concat(taicpu.op_reg_const(A_LI, NR_R0, smallint(a)));
  550. end else begin
  551. { if the lower 16 bits are zero, do a single LIS }
  552. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, smallint(a shr 16)));
  553. if (smallint(a) <> 0) then begin
  554. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(a)));
  555. end;
  556. end;
  557. load32bitconstantR0 := a < 0;
  558. end;
  559. { emits the code to load a constant by emitting various instructions into the output
  560. code}
  561. procedure loadConstantNormal(list: TAsmList; size : TCgSize; a: aint; reg: TRegister);
  562. var
  563. extendssign : boolean;
  564. instr : taicpu;
  565. begin
  566. if (lo(a) = 0) and (hi(a) <> 0) then begin
  567. { load only upper 32 bits, and shift }
  568. load32bitconstant(list, size, longint(hi(a)), reg);
  569. list.concat(taicpu.op_reg_reg_const(A_SLDI, reg, reg, 32));
  570. end else begin
  571. { load lower 32 bits }
  572. extendssign := load32bitconstant(list, size, longint(lo(a)), reg);
  573. if (extendssign) and (hi(a) = 0) then
  574. { if upper 32 bits are zero, but loading the lower 32 bit resulted in automatic
  575. sign extension, clear those bits }
  576. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, reg, reg, 0, 32))
  577. else if (not
  578. ((extendssign and (longint(hi(a)) = -1)) or
  579. ((not extendssign) and (hi(a)=0)))
  580. ) then begin
  581. { only load the upper 32 bits, if the automatic sign extension is not okay,
  582. that is, _not_ if
  583. - loading the lower 32 bits resulted in -1 in the upper 32 bits, and the upper
  584. 32 bits should contain -1
  585. - loading the lower 32 bits resulted in 0 in the upper 32 bits, and the upper
  586. 32 bits should contain 0 }
  587. a_reg_alloc(list, NR_R0);
  588. load32bitconstantR0(list, size, longint(hi(a)));
  589. { combine both registers }
  590. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, reg, NR_R0, 32, 0));
  591. a_reg_dealloc(list, NR_R0);
  592. end;
  593. end;
  594. end;
  595. {$IFDEF EXTDEBUG}
  596. var
  597. astring : string;
  598. {$ENDIF EXTDEBUG}
  599. begin
  600. {$IFDEF EXTDEBUG}
  601. astring := 'a_load_const_reg ' + inttostr(hi(a)) + ' ' + inttostr(lo(a)) + ' ' + inttostr(ord(size)) + ' ' + inttostr(tcgsize2size[size]) + ' ' + hexstr(a, 16);
  602. list.concat(tai_comment.create(strpnew(astring)));
  603. {$ENDIF EXTDEBUG}
  604. if not (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  605. internalerror(2002090902);
  606. { if PIC or basic optimizations are enabled, and the number of instructions which would be
  607. required to load the value is greater than 2, store (and later load) the value from there }
  608. // if (((cs_opt_peephole in current_settings.optimizerswitches) or (cs_create_pic in current_settings.moduleswitches)) and
  609. // (getInstructionLength(a) > 2)) then
  610. // loadConstantPIC(list, size, a, reg)
  611. // else
  612. loadConstantNormal(list, size, a, reg);
  613. end;
  614. procedure tcgppc.a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize;
  615. const ref: treference; reg: tregister);
  616. const
  617. LoadInstr: array[OS_8..OS_S64, boolean, boolean] of TAsmOp =
  618. { indexed? updating? }
  619. (((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  620. ((A_LHZ, A_LHZU), (A_LHZX, A_LHZUX)),
  621. ((A_LWZ, A_LWZU), (A_LWZX, A_LWZUX)),
  622. ((A_LD, A_LDU), (A_LDX, A_LDUX)),
  623. { 128bit stuff too }
  624. ((A_NONE, A_NONE), (A_NONE, A_NONE)),
  625. { there's no load-byte-with-sign-extend :( }
  626. ((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  627. ((A_LHA, A_LHAU), (A_LHAX, A_LHAUX)),
  628. { there's no load-word-arithmetic-indexed with update, simulate it in code :( }
  629. ((A_LWA, A_NOP), (A_LWAX, A_LWAUX)),
  630. ((A_LD, A_LDU), (A_LDX, A_LDUX))
  631. );
  632. var
  633. op: tasmop;
  634. ref2: treference;
  635. tmpreg: tregister;
  636. begin
  637. {$IFDEF EXTDEBUG}
  638. list.concat(tai_comment.create(strpnew('a_load_ref_reg ' + ref2string(ref))));
  639. {$ENDIF EXTDEBUG}
  640. if not (fromsize in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  641. internalerror(2002090904);
  642. { the caller is expected to have adjusted the reference already
  643. in this case }
  644. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  645. fromsize := tosize;
  646. ref2 := ref;
  647. fixref(list, ref2);
  648. op := loadinstr[fromsize, ref2.index <> NR_NO, false];
  649. { there is no LWAU instruction, simulate using ADDI and LWA }
  650. if (op = A_NOP) then begin
  651. list.concat(taicpu.op_reg_reg_const(A_ADDI, reg, reg, ref2.offset));
  652. ref2.offset := 0;
  653. op := A_LWA;
  654. end;
  655. a_load_store(list, op, reg, ref2);
  656. { sign extend shortint if necessary (because there is
  657. no load instruction to sign extend an 8 bit value automatically)
  658. and mask out extra sign bits when loading from a smaller
  659. signed to a larger unsigned type (where it matters) }
  660. if (fromsize = OS_S8) then begin
  661. a_load_reg_reg(list, OS_8, OS_S8, reg, reg);
  662. a_load_reg_reg(list, OS_S8, tosize, reg, reg);
  663. end else if (fromsize = OS_S16) and (tosize = OS_32) then
  664. a_load_reg_reg(list, fromsize, tosize, reg, reg);
  665. end;
  666. procedure tcgppc.a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize;
  667. reg1, reg2: tregister);
  668. var
  669. instr: TAiCpu;
  670. bytesize : byte;
  671. begin
  672. {$ifdef extdebug}
  673. list.concat(tai_comment.create(strpnew('a_load_reg_reg from : ' + cgsize2string(fromsize) + ' to ' + cgsize2string(tosize))));
  674. {$endif}
  675. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  676. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  677. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  678. ( is_signed_cgsize(fromsize) and (not is_signed_cgsize(tosize)) and
  679. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> sizeof(pint)) ) then begin
  680. case tosize of
  681. OS_S8:
  682. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  683. OS_S16:
  684. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  685. OS_S32:
  686. instr := taicpu.op_reg_reg(A_EXTSW,reg2,reg1);
  687. OS_8, OS_16, OS_32:
  688. instr := taicpu.op_reg_reg_const_const(A_RLDICL, reg2, reg1, 0, (8-tcgsize2size[tosize])*8);
  689. OS_S64, OS_64:
  690. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  691. end;
  692. end else
  693. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  694. list.concat(instr);
  695. rg[R_INTREGISTER].add_move_instruction(instr);
  696. end;
  697. procedure tcgppc.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  698. begin
  699. {$ifdef extdebug}
  700. list.concat(tai_comment.create(strpnew('a_load_subsetreg_reg subsetregsize = ' + cgsize2string(sreg.subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + intToStr(sreg.startbit) + ' tosize = ' + cgsize2string(tosize))));
  701. {$endif}
  702. { do the extraction if required and then extend the sign correctly. (The latter is actually required only for signed subsets
  703. and if that subset is not >= the tosize). }
  704. if (sreg.startbit <> 0) or
  705. (sreg.bitlen <> tcgsize2size[subsetsize]*8) then begin
  706. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, destreg, sreg.subsetreg, (64 - sreg.startbit) and 63, 64 - sreg.bitlen));
  707. if (subsetsize in [OS_S8..OS_S128]) then
  708. if ((sreg.bitlen mod 8) = 0) then begin
  709. a_load_reg_reg(list, tcgsize2unsigned[subsetsize], subsetsize, destreg, destreg);
  710. a_load_reg_reg(list, subsetsize, tosize, destreg, destreg);
  711. end else begin
  712. a_op_const_reg(list,OP_SHL,OS_INT,64-sreg.bitlen,destreg);
  713. a_op_const_reg(list,OP_SAR,OS_INT,64-sreg.bitlen,destreg);
  714. end;
  715. end else begin
  716. a_load_reg_reg(list, tcgsize2unsigned[sreg.subsetregsize], subsetsize, sreg.subsetreg, destreg);
  717. a_load_reg_reg(list, subsetsize, tosize, destreg, destreg);
  718. end;
  719. end;
  720. procedure tcgppc.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  721. begin
  722. {$ifdef extdebug}
  723. list.concat(tai_comment.create(strpnew('a_load_reg_subsetreg fromsize = ' + cgsize2string(fromsize) + ' subsetregsize = ' + cgsize2string(sreg.subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + IntToStr(sreg.startbit))));
  724. {$endif}
  725. if (slopt in [SL_SETZERO,SL_SETMAX]) then
  726. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  727. else if (sreg.bitlen <> sizeof(aint)*8) then
  728. { simply use the INSRDI instruction }
  729. list.concat(taicpu.op_reg_reg_const_const(A_INSRDI, sreg.subsetreg, fromreg, sreg.bitlen, (64 - (sreg.startbit + sreg.bitlen)) and 63))
  730. else
  731. a_load_reg_reg(list, fromsize, subsetsize, fromreg, sreg.subsetreg);
  732. end;
  733. procedure tcgppc.a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize;
  734. a: aint; const sreg: tsubsetregister);
  735. var
  736. tmpreg : TRegister;
  737. begin
  738. {$ifdef extdebug}
  739. list.concat(tai_comment.create(strpnew('a_load_const_subsetreg subsetregsize = ' + cgsize2string(sreg.subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + intToStr(sreg.startbit) + ' a = ' + intToStr(a))));
  740. {$endif}
  741. { loading the constant into the lowest bits of a temp register and then inserting is
  742. better than loading some usually large constants and do some masking and shifting on ppc64 }
  743. tmpreg := getintregister(list,subsetsize);
  744. a_load_const_reg(list,subsetsize,a,tmpreg);
  745. a_load_reg_subsetreg(list, subsetsize, subsetsize, tmpreg, sreg);
  746. end;
  747. procedure tcgppc.a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  748. aint; reg: TRegister);
  749. begin
  750. a_op_const_reg_reg(list, op, size, a, reg, reg);
  751. end;
  752. procedure tcgppc.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  753. dst: TRegister);
  754. begin
  755. a_op_reg_reg_reg(list, op, size, src, dst, dst);
  756. end;
  757. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  758. size: tcgsize; a: aint; src, dst: tregister);
  759. var
  760. useReg : boolean;
  761. procedure do_lo_hi(loOp, hiOp : TAsmOp);
  762. begin
  763. { Optimization for logical ops (excluding AND), trying to do this as efficiently
  764. as possible by only generating code for the affected halfwords. Note that all
  765. the instructions handled here must have "X op 0 = X" for every halfword. }
  766. usereg := false;
  767. if (aword(a) > high(dword)) then begin
  768. usereg := true;
  769. end else begin
  770. if (word(a) <> 0) then begin
  771. list.concat(taicpu.op_reg_reg_const(loOp, dst, src, word(a)));
  772. if (word(a shr 16) <> 0) then
  773. list.concat(taicpu.op_reg_reg_const(hiOp, dst, dst, word(a shr 16)));
  774. end else if (word(a shr 16) <> 0) then
  775. list.concat(taicpu.op_reg_reg_const(hiOp, dst, src, word(a shr 16)));
  776. end;
  777. end;
  778. procedure do_lo_hi_and;
  779. begin
  780. { optimization logical and with immediate: only use "andi." for 16 bit
  781. ands, otherwise use register method. Doing this for 32 bit constants
  782. would not give any advantage to the register method (via useReg := true),
  783. requiring a scratch register and three instructions. }
  784. usereg := false;
  785. if (aword(a) > high(word)) then
  786. usereg := true
  787. else
  788. list.concat(taicpu.op_reg_reg_const(A_ANDI_, dst, src, word(a)));
  789. end;
  790. procedure do_constant_div(list : TAsmList; size : TCgSize; a : aint; src, dst : TRegister;
  791. signed : boolean);
  792. const
  793. negops : array[boolean] of tasmop = (A_NEG, A_NEGO);
  794. var
  795. magic, shift : int64;
  796. u_magic : qword;
  797. u_shift : byte;
  798. u_add : boolean;
  799. power : byte;
  800. isNegPower : boolean;
  801. divreg : tregister;
  802. begin
  803. if (a = 0) then begin
  804. internalerror(2005061701);
  805. end else if (a = 1) then begin
  806. cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, src, dst);
  807. end else if (a = -1) and (signed) then begin
  808. { note: only in the signed case possible..., may overflow }
  809. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(negops[cs_check_overflow in current_settings.localswitches], dst, src));
  810. end else if (ispowerof2(a, power, isNegPower)) then begin
  811. if (signed) then begin
  812. { From "The PowerPC Compiler Writer's Guide", pg. 52ff }
  813. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, power,
  814. src, dst);
  815. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_ADDZE, dst, dst));
  816. if (isNegPower) then
  817. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  818. end else begin
  819. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, power, src, dst)
  820. end;
  821. end else begin
  822. { replace division by multiplication, both implementations }
  823. { from "The PowerPC Compiler Writer's Guide" pg. 53ff }
  824. divreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  825. if (signed) then begin
  826. getmagic_signedN(sizeof(aInt)*8, a, magic, shift);
  827. { load magic value }
  828. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, magic, divreg);
  829. { multiply }
  830. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHD, dst, src, divreg));
  831. { add/subtract numerator }
  832. if (a > 0) and (magic < 0) then begin
  833. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, src, dst, dst);
  834. end else if (a < 0) and (magic > 0) then begin
  835. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, src, dst, dst);
  836. end;
  837. { shift shift places to the right (arithmetic) }
  838. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, shift, dst, dst);
  839. { extract and add sign bit }
  840. if (a >= 0) then begin
  841. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 63, src, divreg);
  842. end else begin
  843. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 63, dst, divreg);
  844. end;
  845. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, dst, divreg, dst);
  846. end else begin
  847. getmagic_unsignedN(sizeof(aWord)*8, a, u_magic, u_add, u_shift);
  848. { load magic in divreg }
  849. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, aint(u_magic), divreg);
  850. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHDU, dst, src, divreg));
  851. if (u_add) then begin
  852. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, dst, src, divreg);
  853. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 1, divreg, divreg);
  854. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, divreg, dst, divreg);
  855. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, u_shift-1, divreg, dst);
  856. end else begin
  857. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, u_shift, dst, dst);
  858. end;
  859. end;
  860. end;
  861. end;
  862. var
  863. scratchreg: tregister;
  864. shift : byte;
  865. shiftmask : longint;
  866. isneg : boolean;
  867. begin
  868. { subtraction is the same as addition with negative constant }
  869. if op = OP_SUB then begin
  870. a_op_const_reg_reg(list, OP_ADD, size, -a, src, dst);
  871. exit;
  872. end;
  873. {$IFDEF EXTDEBUG}
  874. list.concat(tai_comment.create(strpnew('a_op_const_reg_reg ' + cgop2string(op))));
  875. {$ENDIF EXTDEBUG}
  876. { This case includes some peephole optimizations for the various operations,
  877. (e.g. AND, OR, XOR, ..) - can't this be done at some higher level,
  878. independent of architecture? }
  879. { assume that we do not need a scratch register for the operation }
  880. useReg := false;
  881. case (op) of
  882. OP_DIV, OP_IDIV:
  883. if (cs_opt_level1 in current_settings.optimizerswitches) then
  884. do_constant_div(list, size, a, src, dst, op = OP_IDIV)
  885. else
  886. usereg := true;
  887. OP_IMUL, OP_MUL:
  888. { idea: factorize constant multiplicands and use adds/shifts with few factors;
  889. however, even a 64 bit multiply is already quite fast on PPC64 }
  890. if (a = 0) then
  891. a_load_const_reg(list, size, 0, dst)
  892. else if (a = -1) then
  893. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst))
  894. else if (a = 1) then
  895. a_load_reg_reg(list, OS_INT, OS_INT, src, dst)
  896. else if ispowerof2(a, shift, isneg) then begin
  897. list.concat(taicpu.op_reg_reg_const(A_SLDI, dst, src, shift));
  898. if (isneg) then
  899. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  900. end else if (a >= low(smallint)) and (a <= high(smallint)) then
  901. list.concat(taicpu.op_reg_reg_const(A_MULLI, dst, src,
  902. smallint(a)))
  903. else
  904. usereg := true;
  905. OP_ADD:
  906. if (a = 0) then
  907. a_load_reg_reg(list, size, size, src, dst)
  908. else if (a >= low(smallint)) and (a <= high(smallint)) then
  909. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst, src, smallint(a)))
  910. else
  911. useReg := true;
  912. OP_OR:
  913. if (a = 0) then
  914. a_load_reg_reg(list, size, size, src, dst)
  915. else if (a = -1) then
  916. a_load_const_reg(list, size, -1, dst)
  917. else
  918. do_lo_hi(A_ORI, A_ORIS);
  919. OP_AND:
  920. if (a = 0) then
  921. a_load_const_reg(list, size, 0, dst)
  922. else if (a = -1) then
  923. a_load_reg_reg(list, size, size, src, dst)
  924. else
  925. do_lo_hi_and;
  926. OP_XOR:
  927. if (a = 0) then
  928. a_load_reg_reg(list, size, size, src, dst)
  929. else if (a = -1) then
  930. list.concat(taicpu.op_reg_reg(A_NOT, dst, src))
  931. else
  932. do_lo_hi(A_XORI, A_XORIS);
  933. OP_ROL:
  934. begin
  935. if (size in [OS_64, OS_S64]) then begin
  936. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, dst, src, a and 63, 0));
  937. end else if (size in [OS_32, OS_S32]) then begin
  938. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, a and 31, 0, 31));
  939. end else begin
  940. internalerror(2008091303);
  941. end;
  942. end;
  943. OP_ROR:
  944. begin
  945. if (size in [OS_64, OS_S64]) then begin
  946. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, dst, src, ((64 - a) and 63), 0));
  947. end else if (size in [OS_32, OS_S32]) then begin
  948. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, (32 - a) and 31, 0, 31));
  949. end else begin
  950. internalerror(2008091304);
  951. end;
  952. end;
  953. OP_SHL, OP_SHR, OP_SAR:
  954. begin
  955. if (size in [OS_64, OS_S64]) then
  956. shift := 6
  957. else
  958. shift := 5;
  959. shiftmask := (1 shl shift)-1;
  960. if (a and shiftmask) <> 0 then begin
  961. list.concat(taicpu.op_reg_reg_const(
  962. TShiftOpCG2AsmOpConst[size in [OS_64, OS_S64], op], dst, src, a and shiftmask));
  963. end else
  964. a_load_reg_reg(list, size, size, src, dst);
  965. if ((a shr shift) <> 0) then
  966. internalError(68991);
  967. end
  968. else
  969. internalerror(200109091);
  970. end;
  971. { if all else failed, load the constant in a register and then
  972. perform the operation }
  973. if (useReg) then begin
  974. scratchreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  975. a_load_const_reg(list, size, a, scratchreg);
  976. a_op_reg_reg_reg(list, op, size, scratchreg, src, dst);
  977. end else
  978. maybeadjustresult(list, op, size, dst);
  979. end;
  980. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  981. size: tcgsize; src1, src2, dst: tregister);
  982. const
  983. op_reg_reg_opcg2asmop32: array[TOpCG] of tasmop =
  984. (A_NONE, A_MR, A_ADD, A_AND, A_DIVWU, A_DIVW, A_MULLW, A_MULLW, A_NEG, A_NOT, A_OR,
  985. A_SRAW, A_SLW, A_SRW, A_SUB, A_XOR, A_NONE, A_NONE);
  986. op_reg_reg_opcg2asmop64: array[TOpCG] of tasmop =
  987. (A_NONE, A_MR, A_ADD, A_AND, A_DIVDU, A_DIVD, A_MULLD, A_MULLD, A_NEG, A_NOT, A_OR,
  988. A_SRAD, A_SLD, A_SRD, A_SUB, A_XOR, A_NONE, A_NONE);
  989. var
  990. tmpreg : TRegister;
  991. begin
  992. case op of
  993. OP_NEG, OP_NOT:
  994. begin
  995. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src1));
  996. if (op = OP_NOT) and not (size in [OS_64, OS_S64]) then
  997. { zero/sign extend result again, fromsize is not important here }
  998. a_load_reg_reg(list, OS_S64, size, dst, dst)
  999. end;
  1000. OP_ROL:
  1001. begin
  1002. if (size in [OS_64, OS_S64]) then begin
  1003. list.concat(taicpu.op_reg_reg_reg_const(A_RLDCL, dst, src2, src1, 0));
  1004. end else if (size in [OS_32, OS_S32]) then begin
  1005. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, src1, 0, 31));
  1006. end else begin
  1007. internalerror(2008091301);
  1008. end;
  1009. end;
  1010. OP_ROR:
  1011. begin
  1012. tmpreg := getintregister(current_asmdata.CurrAsmList, OS_INT);
  1013. list.concat(taicpu.op_reg_reg(A_NEG, tmpreg, src1));
  1014. if (size in [OS_64, OS_S64]) then begin
  1015. list.concat(taicpu.op_reg_reg_reg_const(A_RLDCL, dst, src2, tmpreg, 0));
  1016. end else if (size in [OS_32, OS_S32]) then begin
  1017. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, tmpreg, 0, 31));
  1018. end else begin
  1019. internalerror(2008091302);
  1020. end;
  1021. end;
  1022. else
  1023. if (size in [OS_64, OS_S64]) then begin
  1024. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src2,
  1025. src1));
  1026. end else begin
  1027. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop32[op], dst, src2,
  1028. src1));
  1029. maybeadjustresult(list, op, size, dst);
  1030. end;
  1031. end;
  1032. end;
  1033. {*************** compare instructructions ****************}
  1034. procedure tcgppc.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1035. cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1036. const
  1037. { unsigned useconst 32bit-op }
  1038. cmpop_table : array[boolean, boolean, boolean] of TAsmOp = (
  1039. ((A_CMPD, A_CMPW), (A_CMPDI, A_CMPWI)),
  1040. ((A_CMPLD, A_CMPLW), (A_CMPLDI, A_CMPLWI))
  1041. );
  1042. var
  1043. tmpreg : TRegister;
  1044. signed, useconst : boolean;
  1045. opsize : TCgSize;
  1046. op : TAsmOp;
  1047. begin
  1048. {$IFDEF EXTDEBUG}
  1049. list.concat(tai_comment.create(strpnew('a_cmp_const_reg_label ' + cgsize2string(size) + ' ' + booltostr(cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE]) + ' ' + inttostr(a) )));
  1050. {$ENDIF EXTDEBUG}
  1051. signed := cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE];
  1052. { in the following case, we generate more efficient code when
  1053. signed is true }
  1054. if (cmp_op in [OC_EQ, OC_NE]) and
  1055. (aword(a) > $FFFF) then
  1056. signed := true;
  1057. opsize := size;
  1058. { do we need to change the operand size because ppc64 only supports 32 and
  1059. 64 bit compares? }
  1060. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then begin
  1061. if (signed) then
  1062. opsize := OS_S32
  1063. else
  1064. opsize := OS_32;
  1065. a_load_reg_reg(current_asmdata.CurrAsmList, size, opsize, reg, reg);
  1066. end;
  1067. { can we use immediate compares? }
  1068. useconst := (signed and ( (a >= low(smallint)) and (a <= high(smallint)))) or
  1069. ((not signed) and (aword(a) <= $FFFF));
  1070. op := cmpop_table[not signed, useconst, opsize in [OS_32, OS_S32]];
  1071. if (useconst) then begin
  1072. list.concat(taicpu.op_reg_reg_const(op, NR_CR0, reg, a));
  1073. end else begin
  1074. tmpreg := getintregister(current_asmdata.CurrAsmList, OS_INT);
  1075. a_load_const_reg(current_asmdata.CurrAsmList, opsize, a, tmpreg);
  1076. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg, tmpreg));
  1077. end;
  1078. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  1079. end;
  1080. procedure tcgppc.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize;
  1081. cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1082. var
  1083. op: tasmop;
  1084. begin
  1085. {$IFDEF extdebug}
  1086. list.concat(tai_comment.create(strpnew('a_cmp_reg_reg_label, size ' + cgsize2string(size) + ' op ' + inttostr(ord(cmp_op)))));
  1087. {$ENDIF extdebug}
  1088. {$note Commented out below check because of compiler weirdness}
  1089. {
  1090. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then
  1091. internalerror(200606041);
  1092. }
  1093. if cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE] then
  1094. if (size in [OS_64, OS_S64]) then
  1095. op := A_CMPD
  1096. else
  1097. op := A_CMPW
  1098. else
  1099. if (size in [OS_64, OS_S64]) then
  1100. op := A_CMPLD
  1101. else
  1102. op := A_CMPLW;
  1103. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg2, reg1));
  1104. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  1105. end;
  1106. procedure tcgppc.a_jmp_name_direct(list : TAsmList; s : string; prependDot : boolean);
  1107. var
  1108. p: taicpu;
  1109. begin
  1110. if (prependDot) then
  1111. s := '.' + s;
  1112. p := taicpu.op_sym(A_B, current_asmdata.RefAsmSymbol(s));
  1113. p.is_jmp := true;
  1114. list.concat(p)
  1115. end;
  1116. procedure tcgppc.a_jmp_name(list: TAsmList; const s: string);
  1117. var
  1118. p: taicpu;
  1119. begin
  1120. if (target_info.system = system_powerpc64_darwin) then
  1121. begin
  1122. p := taicpu.op_sym(A_B,get_darwin_call_stub(s,false));
  1123. p.is_jmp := true;
  1124. list.concat(p)
  1125. end
  1126. else
  1127. a_jmp_name_direct(list, s, true);
  1128. end;
  1129. procedure tcgppc.a_jmp_always(list: TAsmList; l: tasmlabel);
  1130. begin
  1131. a_jmp(list, A_B, C_None, 0, l);
  1132. end;
  1133. procedure tcgppc.a_jmp_flags(list: TAsmList; const f: TResFlags; l:
  1134. tasmlabel);
  1135. var
  1136. c: tasmcond;
  1137. begin
  1138. c := flags_to_cond(f);
  1139. a_jmp(list, A_BC, c.cond, c.cr - RS_CR0, l);
  1140. end;
  1141. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f:
  1142. TResFlags; reg: TRegister);
  1143. var
  1144. testbit: byte;
  1145. bitvalue: boolean;
  1146. begin
  1147. { get the bit to extract from the conditional register + its requested value (0 or 1) }
  1148. testbit := ((f.cr - RS_CR0) * 4);
  1149. case f.flag of
  1150. F_EQ, F_NE:
  1151. begin
  1152. inc(testbit, 2);
  1153. bitvalue := f.flag = F_EQ;
  1154. end;
  1155. F_LT, F_GE:
  1156. begin
  1157. bitvalue := f.flag = F_LT;
  1158. end;
  1159. F_GT, F_LE:
  1160. begin
  1161. inc(testbit);
  1162. bitvalue := f.flag = F_GT;
  1163. end;
  1164. else
  1165. internalerror(200112261);
  1166. end;
  1167. { load the conditional register in the destination reg }
  1168. list.concat(taicpu.op_reg(A_MFCR, reg));
  1169. { we will move the bit that has to be tested to bit 0 by rotating left }
  1170. testbit := (testbit + 1) and 31;
  1171. { extract bit }
  1172. list.concat(taicpu.op_reg_reg_const_const_const(
  1173. A_RLWINM,reg,reg,testbit,31,31));
  1174. { if we need the inverse, xor with 1 }
  1175. if not bitvalue then
  1176. list.concat(taicpu.op_reg_reg_const(A_XORI, reg, reg, 1));
  1177. end;
  1178. { *********** entry/exit code and address loading ************ }
  1179. procedure tcgppc.g_save_registers(list: TAsmList);
  1180. begin
  1181. { this work is done in g_proc_entry; additionally it is not safe
  1182. to use it because it is called at some weird time }
  1183. end;
  1184. procedure tcgppc.g_restore_registers(list: TAsmList);
  1185. begin
  1186. { this work is done in g_proc_exit; mainly because it is not safe to
  1187. put the register restore code here because it is called at some weird time }
  1188. end;
  1189. procedure tcgppc.calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  1190. var
  1191. reg : TSuperRegister;
  1192. begin
  1193. fprcount := 0;
  1194. firstfpr := RS_F31;
  1195. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1196. for reg := RS_F14 to RS_F31 do
  1197. if reg in rg[R_FPUREGISTER].used_in_proc then begin
  1198. fprcount := ord(RS_F31)-ord(reg)+1;
  1199. firstfpr := reg;
  1200. break;
  1201. end;
  1202. end;
  1203. procedure tcgppc.calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  1204. var
  1205. reg : TSuperRegister;
  1206. begin
  1207. gprcount := 0;
  1208. firstgpr := RS_R31;
  1209. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1210. for reg := RS_R14 to RS_R31 do
  1211. if reg in rg[R_INTREGISTER].used_in_proc then begin
  1212. gprcount := ord(RS_R31)-ord(reg)+1;
  1213. firstgpr := reg;
  1214. break;
  1215. end;
  1216. end;
  1217. procedure tcgppc.profilecode_savepara(para : tparavarsym; list : TAsmList);
  1218. begin
  1219. case (para.paraloc[calleeside].location^.loc) of
  1220. LOC_REGISTER, LOC_CREGISTER:
  1221. a_load_reg_ref(list, OS_INT, para.paraloc[calleeside].Location^.size,
  1222. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  1223. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1224. a_loadfpu_reg_ref(list, para.paraloc[calleeside].Location^.size,
  1225. para.paraloc[calleeside].Location^.size,
  1226. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  1227. LOC_MMREGISTER, LOC_CMMREGISTER:
  1228. { not supported }
  1229. internalerror(2006041801);
  1230. end;
  1231. end;
  1232. procedure tcgppc.profilecode_restorepara(para : tparavarsym; list : TAsmList);
  1233. begin
  1234. case (para.paraloc[calleeside].Location^.loc) of
  1235. LOC_REGISTER, LOC_CREGISTER:
  1236. a_load_ref_reg(list, para.paraloc[calleeside].Location^.size, OS_INT,
  1237. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  1238. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1239. a_loadfpu_ref_reg(list, para.paraloc[calleeside].Location^.size,
  1240. para.paraloc[calleeside].Location^.size,
  1241. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  1242. LOC_MMREGISTER, LOC_CMMREGISTER:
  1243. { not supported }
  1244. internalerror(2006041802);
  1245. end;
  1246. end;
  1247. procedure tcgppc.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);
  1248. var
  1249. hsym : tsym;
  1250. href : treference;
  1251. paraloc : Pcgparalocation;
  1252. begin
  1253. if ((ioffset >= low(smallint)) and (ioffset < high(smallint))) then begin
  1254. { the original method can handle this }
  1255. inherited g_adjust_self_value(list, procdef, ioffset);
  1256. exit;
  1257. end;
  1258. { calculate the parameter info for the procdef }
  1259. procdef.init_paraloc_info(callerside);
  1260. hsym:=tsym(procdef.parast.Find('self'));
  1261. if not(assigned(hsym) and
  1262. (hsym.typ=paravarsym)) then
  1263. internalerror(2010103101);
  1264. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1265. while paraloc<>nil do
  1266. with paraloc^ do begin
  1267. case loc of
  1268. LOC_REGISTER:
  1269. begin
  1270. a_load_const_reg(list, size, ioffset, NR_R11);
  1271. a_op_reg_reg(list, OP_SUB, size, NR_R11, register);
  1272. end else
  1273. internalerror(2010103102);
  1274. end;
  1275. paraloc:=next;
  1276. end;
  1277. end;
  1278. procedure tcgppc.g_profilecode(list: TAsmList);
  1279. begin
  1280. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_savepara), list);
  1281. a_call_name_direct(list, '_mcount', false, false, true);
  1282. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_restorepara), list);
  1283. end;
  1284. { Generates the entry code of a procedure/function.
  1285. This procedure may be called before, as well as after g_return_from_proc
  1286. is called. localsize is the sum of the size necessary for local variables
  1287. and the maximum possible combined size of ALL the parameters of a procedure
  1288. called by the current one
  1289. IMPORTANT: registers are not to be allocated through the register
  1290. allocator here, because the register colouring has already occured !!
  1291. }
  1292. procedure tcgppc.g_proc_entry(list: TAsmList; localsize: longint;
  1293. nostackframe: boolean);
  1294. var
  1295. firstregfpu, firstreggpr: TSuperRegister;
  1296. needslinkreg: boolean;
  1297. fprcount, gprcount : aint;
  1298. { Save standard registers, both FPR and GPR; does not support VMX/Altivec }
  1299. procedure save_standard_registers;
  1300. var
  1301. regcount : TSuperRegister;
  1302. href : TReference;
  1303. mayNeedLRStore : boolean;
  1304. begin
  1305. { there are two ways to do this: manually, by generating a few "std" instructions,
  1306. or via the restore helper functions. The latter are selected by the -Og switch,
  1307. i.e. "optimize for size" }
  1308. if (cs_opt_size in current_settings.optimizerswitches) and
  1309. (target_info.system <> system_powerpc64_darwin) then begin
  1310. mayNeedLRStore := false;
  1311. if ((fprcount > 0) and (gprcount > 0)) then begin
  1312. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1313. a_call_name_direct(list, '_savegpr1_' + intToStr(32-gprcount), false, false, false, false);
  1314. a_call_name_direct(list, '_savefpr_' + intToStr(32-fprcount), false, false, false, false);
  1315. end else if (gprcount > 0) then
  1316. a_call_name_direct(list, '_savegpr0_' + intToStr(32-gprcount), false, false, false, false)
  1317. else if (fprcount > 0) then
  1318. a_call_name_direct(list, '_savefpr_' + intToStr(32-fprcount), false, false, false, false)
  1319. else
  1320. mayNeedLRStore := true;
  1321. end else begin
  1322. { save registers, FPU first, then GPR }
  1323. reference_reset_base(href, NR_STACK_POINTER_REG, -8, 8);
  1324. if (fprcount > 0) then
  1325. for regcount := RS_F31 downto firstregfpu do begin
  1326. a_loadfpu_reg_ref(list, OS_FLOAT, OS_FLOAT, newreg(R_FPUREGISTER,
  1327. regcount, R_SUBNONE), href);
  1328. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1329. end;
  1330. if (gprcount > 0) then
  1331. for regcount := RS_R31 downto firstreggpr do begin
  1332. a_load_reg_ref(list, OS_INT, OS_INT, newreg(R_INTREGISTER, regcount,
  1333. R_SUBNONE), href);
  1334. dec(href.offset, sizeof(pint));
  1335. end;
  1336. { VMX registers not supported by FPC atm }
  1337. { in this branch we always need to store LR ourselves}
  1338. mayNeedLRStore := true;
  1339. end;
  1340. { we may need to store R0 (=LR) ourselves }
  1341. if ((cs_profile in init_settings.moduleswitches) or (mayNeedLRStore)) and (needslinkreg) then begin
  1342. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, 8);
  1343. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  1344. end;
  1345. end;
  1346. var
  1347. href: treference;
  1348. begin
  1349. calcFirstUsedFPR(firstregfpu, fprcount);
  1350. calcFirstUsedGPR(firstreggpr, gprcount);
  1351. { calculate real stack frame size }
  1352. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1353. gprcount, fprcount);
  1354. { determine whether we need to save the link register }
  1355. needslinkreg :=
  1356. not(nostackframe) and
  1357. (save_lr_in_prologue or
  1358. ((cs_opt_size in current_settings.optimizerswitches) and
  1359. ((fprcount > 0) or
  1360. (gprcount > 0))));
  1361. a_reg_alloc(list, NR_STACK_POINTER_REG);
  1362. a_reg_alloc(list, NR_R0);
  1363. { move link register to r0 }
  1364. if (needslinkreg) then
  1365. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  1366. save_standard_registers;
  1367. { save old stack frame pointer }
  1368. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then begin
  1369. a_reg_alloc(list, NR_OLD_STACK_POINTER_REG);
  1370. list.concat(taicpu.op_reg_reg(A_MR, NR_OLD_STACK_POINTER_REG, NR_STACK_POINTER_REG));
  1371. end;
  1372. { create stack frame }
  1373. if (not nostackframe) and (localsize > 0) and
  1374. tppcprocinfo(current_procinfo).needstackframe then begin
  1375. if (localsize <= high(smallint)) then begin
  1376. reference_reset_base(href, NR_STACK_POINTER_REG, -localsize, 8);
  1377. a_load_store(list, A_STDU, NR_STACK_POINTER_REG, href);
  1378. end else begin
  1379. reference_reset_base(href, NR_NO, -localsize, 8);
  1380. { Use R0 for loading the constant (which is definitely > 32k when entering
  1381. this branch).
  1382. Inlined at this position because it must not use temp registers because
  1383. register allocations have already been done }
  1384. { Code template:
  1385. lis r0,ofs@highest
  1386. ori r0,r0,ofs@higher
  1387. sldi r0,r0,32
  1388. oris r0,r0,ofs@h
  1389. ori r0,r0,ofs@l
  1390. }
  1391. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1392. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1393. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1394. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1395. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1396. list.concat(taicpu.op_reg_reg_reg(A_STDUX, NR_R1, NR_R1, NR_R0));
  1397. end;
  1398. end;
  1399. { CR register not used by FPC atm }
  1400. { keep R1 allocated??? }
  1401. a_reg_dealloc(list, NR_R0);
  1402. end;
  1403. { Generates the exit code for a method.
  1404. This procedure may be called before, as well as after g_stackframe_entry
  1405. is called.
  1406. IMPORTANT: registers are not to be allocated through the register
  1407. allocator here, because the register colouring has already occured !!
  1408. }
  1409. procedure tcgppc.g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  1410. boolean);
  1411. var
  1412. firstregfpu, firstreggpr: TSuperRegister;
  1413. needslinkreg : boolean;
  1414. fprcount, gprcount: aint;
  1415. { Restore standard registers, both FPR and GPR; does not support VMX/Altivec }
  1416. procedure restore_standard_registers;
  1417. var
  1418. { flag indicating whether we need to manually add the exit code (e.g. blr instruction)
  1419. or not }
  1420. needsExitCode : Boolean;
  1421. href : treference;
  1422. regcount : TSuperRegister;
  1423. begin
  1424. { there are two ways to do this: manually, by generating a few "ld" instructions,
  1425. or via the restore helper functions. The latter are selected by the -Og switch,
  1426. i.e. "optimize for size" }
  1427. if (cs_opt_size in current_settings.optimizerswitches) then begin
  1428. needsExitCode := false;
  1429. if ((fprcount > 0) and (gprcount > 0)) then begin
  1430. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1431. a_call_name_direct(list, '_restgpr1_' + intToStr(32-gprcount), false, false, false, false);
  1432. a_jmp_name_direct(list, '_restfpr_' + intToStr(32-fprcount), false);
  1433. end else if (gprcount > 0) then
  1434. a_jmp_name_direct(list, '_restgpr0_' + intToStr(32-gprcount), false)
  1435. else if (fprcount > 0) then
  1436. a_jmp_name_direct(list, '_restfpr_' + intToStr(32-fprcount), false)
  1437. else
  1438. needsExitCode := true;
  1439. end else begin
  1440. needsExitCode := true;
  1441. { restore registers, FPU first, GPR next }
  1442. reference_reset_base(href, NR_STACK_POINTER_REG, -tcgsize2size[OS_FLOAT], 8);
  1443. if (fprcount > 0) then
  1444. for regcount := RS_F31 downto firstregfpu do begin
  1445. a_loadfpu_ref_reg(list, OS_FLOAT, OS_FLOAT, href, newreg(R_FPUREGISTER, regcount,
  1446. R_SUBNONE));
  1447. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1448. end;
  1449. if (gprcount > 0) then
  1450. for regcount := RS_R31 downto firstreggpr do begin
  1451. a_load_ref_reg(list, OS_INT, OS_INT, href, newreg(R_INTREGISTER, regcount,
  1452. R_SUBNONE));
  1453. dec(href.offset, sizeof(pint));
  1454. end;
  1455. { VMX not supported by FPC atm }
  1456. end;
  1457. if (needsExitCode) then begin
  1458. { restore LR (if needed) }
  1459. if (needslinkreg) then begin
  1460. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, 8);
  1461. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1462. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1463. end;
  1464. { generate return instruction }
  1465. list.concat(taicpu.op_none(A_BLR));
  1466. end;
  1467. end;
  1468. var
  1469. href: treference;
  1470. localsize : aint;
  1471. begin
  1472. calcFirstUsedFPR(firstregfpu, fprcount);
  1473. calcFirstUsedGPR(firstreggpr, gprcount);
  1474. { determine whether we need to restore the link register }
  1475. needslinkreg :=
  1476. not(nostackframe) and
  1477. (((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1478. ((pi_do_call in current_procinfo.flags) or (cs_profile in init_settings.moduleswitches))) or
  1479. ((cs_opt_size in current_settings.optimizerswitches) and ((fprcount > 0) or (gprcount > 0))) or
  1480. ([cs_lineinfo, cs_debuginfo] * current_settings.moduleswitches <> []));
  1481. { calculate stack frame }
  1482. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1483. gprcount, fprcount);
  1484. { CR register not supported }
  1485. { restore stack pointer }
  1486. if (not nostackframe) and (localsize > 0) and
  1487. tppcprocinfo(current_procinfo).needstackframe then begin
  1488. if (localsize <= high(smallint)) then begin
  1489. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1490. end else begin
  1491. reference_reset_base(href, NR_NO, localsize, 8);
  1492. { use R0 for loading the constant (which is definitely > 32k when entering
  1493. this branch)
  1494. Inlined because it must not use temp registers because register allocations
  1495. have already been done
  1496. }
  1497. { Code template:
  1498. lis r0,ofs@highest
  1499. ori r0,ofs@higher
  1500. sldi r0,r0,32
  1501. oris r0,r0,ofs@h
  1502. ori r0,r0,ofs@l
  1503. }
  1504. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1505. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1506. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1507. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1508. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1509. list.concat(taicpu.op_reg_reg_reg(A_ADD, NR_R1, NR_R1, NR_R0));
  1510. end;
  1511. end;
  1512. restore_standard_registers;
  1513. end;
  1514. procedure tcgppc.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  1515. tregister);
  1516. var
  1517. ref2, tmpref: treference;
  1518. { register used to construct address }
  1519. tempreg : TRegister;
  1520. begin
  1521. if (target_info.system = system_powerpc64_darwin) then
  1522. begin
  1523. inherited a_loadaddr_ref_reg(list,ref,r);
  1524. exit;
  1525. end;
  1526. ref2 := ref;
  1527. fixref(list, ref2);
  1528. { load a symbol }
  1529. if (assigned(ref2.symbol) or (hasLargeOffset(ref2))) then begin
  1530. { add the symbol's value to the base of the reference, and if the }
  1531. { reference doesn't have a base, create one }
  1532. reference_reset(tmpref, ref2.alignment);
  1533. tmpref.offset := ref2.offset;
  1534. tmpref.symbol := ref2.symbol;
  1535. tmpref.relsymbol := ref2.relsymbol;
  1536. { load 64 bit reference into r. If the reference already has a base register,
  1537. first load the 64 bit value into a temp register, then add it to the result
  1538. register rD }
  1539. if (ref2.base <> NR_NO) then begin
  1540. { already have a base register, so allocate a new one }
  1541. tempreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1542. end else begin
  1543. tempreg := r;
  1544. end;
  1545. { code for loading a reference from a symbol into a register rD }
  1546. (*
  1547. lis rX,SYM@highest
  1548. ori rX,SYM@higher
  1549. sldi rX,rX,32
  1550. oris rX,rX,SYM@h
  1551. ori rX,rX,SYM@l
  1552. *)
  1553. {$IFDEF EXTDEBUG}
  1554. list.concat(tai_comment.create(strpnew('loadaddr_ref_reg ')));
  1555. {$ENDIF EXTDEBUG}
  1556. if (assigned(tmpref.symbol)) then begin
  1557. tmpref.refaddr := addr_highest;
  1558. list.concat(taicpu.op_reg_ref(A_LIS, tempreg, tmpref));
  1559. tmpref.refaddr := addr_higher;
  1560. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1561. list.concat(taicpu.op_reg_reg_const(A_SLDI, tempreg, tempreg, 32));
  1562. tmpref.refaddr := addr_high;
  1563. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tempreg, tempreg, tmpref));
  1564. tmpref.refaddr := addr_low;
  1565. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1566. end else
  1567. a_load_const_reg(list, OS_ADDR, tmpref.offset, tempreg);
  1568. { if there's already a base register, add the temp register contents to
  1569. the base register }
  1570. if (ref2.base <> NR_NO) then begin
  1571. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, tempreg, ref2.base));
  1572. end;
  1573. end else if (ref2.offset <> 0) then begin
  1574. { no symbol, but offset <> 0 }
  1575. if (ref2.base <> NR_NO) then begin
  1576. a_op_const_reg_reg(list, OP_ADD, OS_64, ref2.offset, ref2.base, r)
  1577. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never
  1578. occurs, so now only ref.offset has to be loaded }
  1579. end else begin
  1580. a_load_const_reg(list, OS_64, ref2.offset, r);
  1581. end;
  1582. end else if (ref2.index <> NR_NO) then begin
  1583. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, ref2.base, ref2.index))
  1584. end else if (ref2.base <> NR_NO) and
  1585. (r <> ref2.base) then begin
  1586. a_load_reg_reg(list, OS_ADDR, OS_ADDR, ref2.base, r)
  1587. end else begin
  1588. list.concat(taicpu.op_reg_const(A_LI, r, 0));
  1589. end;
  1590. end;
  1591. { ************* concatcopy ************ }
  1592. procedure tcgppc.g_concatcopy(list: TAsmList; const source, dest: treference;
  1593. len: aint);
  1594. var
  1595. countreg, tempreg:TRegister;
  1596. src, dst: TReference;
  1597. lab: tasmlabel;
  1598. count, count2, step: longint;
  1599. size: tcgsize;
  1600. begin
  1601. {$IFDEF extdebug}
  1602. if len > high(aint) then
  1603. internalerror(2002072704);
  1604. list.concat(tai_comment.create(strpnew('g_concatcopy1 ' + inttostr(len) + ' bytes left ')));
  1605. {$ENDIF extdebug}
  1606. { if the references are equal, exit, there is no need to copy anything }
  1607. if references_equal(source, dest) or
  1608. (len=0) then
  1609. exit;
  1610. { make sure short loads are handled as optimally as possible;
  1611. note that the data here never overlaps, so we can do a forward
  1612. copy at all times.
  1613. NOTE: maybe use some scratch registers to pair load/store instructions
  1614. }
  1615. if (len <= 8) then begin
  1616. src := source; dst := dest;
  1617. {$IFDEF extdebug}
  1618. list.concat(tai_comment.create(strpnew('g_concatcopy3 ' + inttostr(src.offset) + ' ' + inttostr(dst.offset))));
  1619. {$ENDIF extdebug}
  1620. while (len <> 0) do begin
  1621. if (len = 8) then begin
  1622. a_load_ref_ref(list, OS_64, OS_64, src, dst);
  1623. dec(len, 8);
  1624. end else if (len >= 4) then begin
  1625. a_load_ref_ref(list, OS_32, OS_32, src, dst);
  1626. inc(src.offset, 4); inc(dst.offset, 4);
  1627. dec(len, 4);
  1628. end else if (len >= 2) then begin
  1629. a_load_ref_ref(list, OS_16, OS_16, src, dst);
  1630. inc(src.offset, 2); inc(dst.offset, 2);
  1631. dec(len, 2);
  1632. end else begin
  1633. a_load_ref_ref(list, OS_8, OS_8, src, dst);
  1634. inc(src.offset, 1); inc(dst.offset, 1);
  1635. dec(len, 1);
  1636. end;
  1637. end;
  1638. exit;
  1639. end;
  1640. {$IFDEF extdebug}
  1641. list.concat(tai_comment.create(strpnew('g_concatcopy2 ' + inttostr(len) + ' bytes left ')));
  1642. {$ENDIF extdebug}
  1643. if not(source.alignment in [1,2]) and
  1644. not(dest.alignment in [1,2]) then
  1645. begin
  1646. count:=len div 8;
  1647. step:=8;
  1648. size:=OS_64;
  1649. end
  1650. else
  1651. begin
  1652. count:=len div 4;
  1653. step:=4;
  1654. size:=OS_32;
  1655. end;
  1656. tempreg:=getintregister(list,size);
  1657. reference_reset(src,source.alignment);
  1658. reference_reset(dst,dest.alignment);
  1659. { load the address of source into src.base }
  1660. if (count > 4) or
  1661. not issimpleref(source) or
  1662. ((source.index <> NR_NO) and
  1663. ((source.offset + len) > high(smallint))) then begin
  1664. src.base := getaddressregister(list);
  1665. a_loadaddr_ref_reg(list, source, src.base);
  1666. end else begin
  1667. src := source;
  1668. end;
  1669. { load the address of dest into dst.base }
  1670. if (count > 4) or
  1671. not issimpleref(dest) or
  1672. ((dest.index <> NR_NO) and
  1673. ((dest.offset + len) > high(smallint))) then begin
  1674. dst.base := getaddressregister(list);
  1675. a_loadaddr_ref_reg(list, dest, dst.base);
  1676. end else begin
  1677. dst := dest;
  1678. end;
  1679. { generate a loop }
  1680. if count > 4 then begin
  1681. { the offsets are zero after the a_loadaddress_ref_reg and just
  1682. have to be set to step. I put an Inc there so debugging may be
  1683. easier (should offset be different from zero here, it will be
  1684. easy to notice in the generated assembler }
  1685. inc(dst.offset, step);
  1686. inc(src.offset, step);
  1687. list.concat(taicpu.op_reg_reg_const(A_SUBI, src.base, src.base, step));
  1688. list.concat(taicpu.op_reg_reg_const(A_SUBI, dst.base, dst.base, step));
  1689. countreg := getintregister(list, OS_INT);
  1690. a_load_const_reg(list, OS_INT, count, countreg);
  1691. current_asmdata.getjumplabel(lab);
  1692. a_label(list, lab);
  1693. list.concat(taicpu.op_reg_reg_const(A_SUBIC_, countreg, countreg, 1));
  1694. if (size=OS_64) then
  1695. begin
  1696. list.concat(taicpu.op_reg_ref(A_LDU, tempreg, src));
  1697. list.concat(taicpu.op_reg_ref(A_STDU, tempreg, dst));
  1698. end
  1699. else
  1700. begin
  1701. list.concat(taicpu.op_reg_ref(A_LWZU, tempreg, src));
  1702. list.concat(taicpu.op_reg_ref(A_STWU, tempreg, dst));
  1703. end;
  1704. a_jmp(list, A_BC, C_NE, 0, lab);
  1705. a_reg_sync(list,src.base);
  1706. a_reg_sync(list,dst.base);
  1707. a_reg_sync(list,countreg);
  1708. len := len mod step;
  1709. count := 0;
  1710. end;
  1711. { unrolled loop }
  1712. if count > 0 then begin
  1713. for count2 := 1 to count do begin
  1714. a_load_ref_reg(list, size, size, src, tempreg);
  1715. a_load_reg_ref(list, size, size, tempreg, dst);
  1716. inc(src.offset, step);
  1717. inc(dst.offset, step);
  1718. end;
  1719. len := len mod step;
  1720. end;
  1721. if (len and 4) <> 0 then begin
  1722. a_load_ref_reg(list, OS_32, OS_32, src, tempreg);
  1723. a_load_reg_ref(list, OS_32, OS_32, tempreg, dst);
  1724. inc(src.offset, 4);
  1725. inc(dst.offset, 4);
  1726. end;
  1727. { copy the leftovers }
  1728. if (len and 2) <> 0 then begin
  1729. a_load_ref_reg(list, OS_16, OS_16, src, tempreg);
  1730. a_load_reg_ref(list, OS_16, OS_16, tempreg, dst);
  1731. inc(src.offset, 2);
  1732. inc(dst.offset, 2);
  1733. end;
  1734. if (len and 1) <> 0 then begin
  1735. a_load_ref_reg(list, OS_8, OS_8, src, tempreg);
  1736. a_load_reg_ref(list, OS_8, OS_8, tempreg, dst);
  1737. end;
  1738. end;
  1739. {***************** This is private property, keep out! :) *****************}
  1740. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  1741. const
  1742. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1743. begin
  1744. {$IFDEF EXTDEBUG}
  1745. list.concat(tai_comment.create(strpnew('maybeadjustresult op = ' + cgop2string(op) + ' size = ' + cgsize2string(size))));
  1746. {$ENDIF EXTDEBUG}
  1747. if (op in overflowops) and (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32]) then
  1748. a_load_reg_reg(list, OS_64, size, dst, dst);
  1749. end;
  1750. function tcgppc.issimpleref(const ref: treference): boolean;
  1751. begin
  1752. if (ref.base = NR_NO) and
  1753. (ref.index <> NR_NO) then
  1754. internalerror(200208101);
  1755. result :=
  1756. not (assigned(ref.symbol)) and
  1757. (((ref.index = NR_NO) and
  1758. (ref.offset >= low(smallint)) and
  1759. (ref.offset <= high(smallint))) or
  1760. ((ref.index <> NR_NO) and
  1761. (ref.offset = 0)));
  1762. end;
  1763. procedure tcgppc.a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  1764. ref: treference);
  1765. procedure maybefixup64bitoffset;
  1766. var
  1767. tmpreg: tregister;
  1768. begin
  1769. { for some instructions we need to check that the offset is divisible by at
  1770. least four. If not, add the bytes which are "off" to the base register and
  1771. adjust the offset accordingly }
  1772. case op of
  1773. A_LD, A_LDU, A_STD, A_STDU, A_LWA :
  1774. if ((ref.offset mod 4) <> 0) then begin
  1775. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1776. if (ref.base <> NR_NO) then begin
  1777. a_op_const_reg_reg(list, OP_ADD, OS_ADDR, ref.offset mod 4, ref.base, tmpreg);
  1778. ref.base := tmpreg;
  1779. end else begin
  1780. list.concat(taicpu.op_reg_const(A_LI, tmpreg, ref.offset mod 4));
  1781. ref.base := tmpreg;
  1782. end;
  1783. ref.offset := (ref.offset div 4) * 4;
  1784. end;
  1785. end;
  1786. end;
  1787. var
  1788. tmpreg, tmpreg2: tregister;
  1789. tmpref: treference;
  1790. largeOffset: Boolean;
  1791. begin
  1792. if (target_info.system = system_powerpc64_darwin) then
  1793. begin
  1794. { darwin/ppc64 works with 32 bit relocatable symbol addresses }
  1795. maybefixup64bitoffset;
  1796. inherited a_load_store(list,op,reg,ref);
  1797. exit
  1798. end;
  1799. { at this point there must not be a combination of values in the ref treference
  1800. which is not possible to directly map to instructions of the PowerPC architecture }
  1801. if (ref.index <> NR_NO) and ((ref.offset <> 0) or (assigned(ref.symbol))) then
  1802. internalerror(200310131);
  1803. { if this is a PIC'ed address, handle it and exit }
  1804. if (ref.refaddr = addr_pic) then begin
  1805. if (ref.offset <> 0) then
  1806. internalerror(2006010501);
  1807. if (ref.index <> NR_NO) then
  1808. internalerror(2006010502);
  1809. if (not assigned(ref.symbol)) then
  1810. internalerror(200601050);
  1811. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1812. exit;
  1813. end;
  1814. maybefixup64bitoffset;
  1815. {$IFDEF EXTDEBUG}
  1816. list.concat(tai_comment.create(strpnew('a_load_store1 ' + BoolToStr(ref.refaddr = addr_pic))));
  1817. {$ENDIF EXTDEBUG}
  1818. { if we have to load/store from a symbol or large addresses, use a temporary register
  1819. containing the address }
  1820. if (assigned(ref.symbol) or (hasLargeOffset(ref))) then begin
  1821. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1822. if (hasLargeOffset(ref) and (ref.base = NR_NO)) then begin
  1823. ref.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1824. a_load_const_reg(list, OS_ADDR, ref.offset, ref.base);
  1825. ref.offset := 0;
  1826. end;
  1827. reference_reset(tmpref, ref.alignment);
  1828. tmpref.symbol := ref.symbol;
  1829. tmpref.relsymbol := ref.relsymbol;
  1830. tmpref.offset := ref.offset;
  1831. if (ref.base <> NR_NO) then begin
  1832. { As long as the TOC isn't working we try to achieve highest speed (in this
  1833. case by allowing instructions execute in parallel) as possible at the cost
  1834. of using another temporary register. So the code template when there is
  1835. a base register and an offset is the following:
  1836. lis rT1, SYM+offs@highest
  1837. ori rT1, rT1, SYM+offs@higher
  1838. lis rT2, SYM+offs@hi
  1839. ori rT2, SYM+offs@lo
  1840. rldimi rT2, rT1, 32
  1841. <op>X reg, base, rT2
  1842. }
  1843. tmpreg2 := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1844. if (assigned(tmpref.symbol)) then begin
  1845. tmpref.refaddr := addr_highest;
  1846. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1847. tmpref.refaddr := addr_higher;
  1848. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1849. tmpref.refaddr := addr_high;
  1850. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg2, tmpref));
  1851. tmpref.refaddr := addr_low;
  1852. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg2, tmpreg2, tmpref));
  1853. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, tmpreg2, tmpreg, 32, 0));
  1854. end else
  1855. a_load_const_reg(list, OS_ADDR, tmpref.offset, tmpreg2);
  1856. reference_reset(tmpref, ref.alignment);
  1857. tmpref.base := ref.base;
  1858. tmpref.index := tmpreg2;
  1859. case op of
  1860. { the code generator doesn't generate update instructions anyway, so
  1861. error out on those instructions }
  1862. A_LBZ : op := A_LBZX;
  1863. A_LHZ : op := A_LHZX;
  1864. A_LWZ : op := A_LWZX;
  1865. A_LD : op := A_LDX;
  1866. A_LHA : op := A_LHAX;
  1867. A_LWA : op := A_LWAX;
  1868. A_LFS : op := A_LFSX;
  1869. A_LFD : op := A_LFDX;
  1870. A_STB : op := A_STBX;
  1871. A_STH : op := A_STHX;
  1872. A_STW : op := A_STWX;
  1873. A_STD : op := A_STDX;
  1874. A_STFS : op := A_STFSX;
  1875. A_STFD : op := A_STFDX;
  1876. else
  1877. { unknown load/store opcode }
  1878. internalerror(2005101302);
  1879. end;
  1880. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1881. end else begin
  1882. { when accessing value from a reference without a base register, use the
  1883. following code template:
  1884. lis rT,SYM+offs@highesta
  1885. ori rT,SYM+offs@highera
  1886. sldi rT,rT,32
  1887. oris rT,rT,SYM+offs@ha
  1888. ld rD,SYM+offs@l(rT)
  1889. }
  1890. tmpref.refaddr := addr_highesta;
  1891. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1892. tmpref.refaddr := addr_highera;
  1893. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1894. list.concat(taicpu.op_reg_reg_const(A_SLDI, tmpreg, tmpreg, 32));
  1895. tmpref.refaddr := addr_higha;
  1896. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tmpreg, tmpreg, tmpref));
  1897. tmpref.base := tmpreg;
  1898. tmpref.refaddr := addr_low;
  1899. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1900. end;
  1901. end else begin
  1902. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1903. end;
  1904. end;
  1905. procedure tcgppc.loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  1906. var
  1907. l: tasmsymbol;
  1908. ref: treference;
  1909. symname : string;
  1910. begin
  1911. maybe_new_object_file(current_asmdata.asmlists[al_picdata]);
  1912. symname := '_$' + current_asmdata.name + '$toc$' + hexstr(a, sizeof(a)*2);
  1913. l:=current_asmdata.getasmsymbol(symname);
  1914. if not(assigned(l)) then begin
  1915. l:=current_asmdata.DefineAsmSymbol(symname,AB_GLOBAL, AT_DATA);
  1916. new_section(current_asmdata.asmlists[al_picdata],sec_toc, '.toc', 8);
  1917. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create_global(l,0));
  1918. current_asmdata.asmlists[al_picdata].concat(tai_directive.create(asd_toc_entry, symname + '[TC], ' + inttostr(a)));
  1919. end;
  1920. reference_reset_symbol(ref,l,0, 8);
  1921. ref.base := NR_R2;
  1922. ref.refaddr := addr_no;
  1923. {$IFDEF EXTDEBUG}
  1924. list.concat(tai_comment.create(strpnew('loading value from TOC reference for ' + symname)));
  1925. {$ENDIF EXTDEBUG}
  1926. cg.a_load_ref_reg(list, OS_INT, OS_INT, ref, reg);
  1927. end;
  1928. procedure create_codegen;
  1929. begin
  1930. cg := tcgppc.create;
  1931. end;
  1932. end.