cgcpu.pas 79 KB

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