cgcpu.pas 72 KB

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