cgcpu.pas 79 KB

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