cgcpu.pas 80 KB

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