cgcpu.pas 75 KB

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