cgcpu.pas 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  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] := trgcpu.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 < tcgsize2size[OS_INT]) then
  418. a_op_const_reg(list, OP_SHL, OS_INT,
  419. (tcgsize2size[OS_INT] - sizeleft) * tcgsize2size[OS_INT],
  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. { unaligned 64 bit accesses are much slower than unaligned }
  645. { 32 bit accesses because they cause a hardware exception }
  646. { (which isn't handled by linux, so there you even get a }
  647. { crash) }
  648. if (ref.alignment<>0) and
  649. (fromsize in [OS_64,OS_S64]) and
  650. (ref.alignment<4) then
  651. begin
  652. if (ref2.base<>NR_NO) and
  653. (ref2.index<>NR_NO) then
  654. begin
  655. tmpreg:=getintregister(list,OS_64);
  656. a_op_reg_reg_reg(list,OP_SHR,OS_64,ref2.base,ref2.index,tmpreg);
  657. ref2.base:=tmpreg;
  658. ref2.index:=NR_NO;
  659. end;
  660. tmpreg:=getintregister(list,OS_32);
  661. a_load_ref_reg(list,OS_32,OS_32,ref2,tmpreg);
  662. inc(ref2.offset,4);
  663. a_load_ref_reg(list,OS_32,OS_32,ref2,reg);
  664. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, reg, tmpreg, 32, 0));
  665. exit;
  666. end;
  667. op := loadinstr[fromsize, ref2.index <> NR_NO, false];
  668. { there is no LWAU instruction, simulate using ADDI and LWA }
  669. if (op = A_NOP) then begin
  670. list.concat(taicpu.op_reg_reg_const(A_ADDI, reg, reg, ref2.offset));
  671. ref2.offset := 0;
  672. op := A_LWA;
  673. end;
  674. a_load_store(list, op, reg, ref2);
  675. { sign extend shortint if necessary, since there is no
  676. load instruction that does that automatically (JM) }
  677. if (fromsize = OS_S8) then
  678. begin
  679. list.concat(taicpu.op_reg_reg(A_EXTSB, reg, reg));
  680. if (tosize in [OS_16,OS_32]) then
  681. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  682. end
  683. else if (fromsize = OS_S16) and
  684. (tosize = OS_32) then
  685. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  686. end;
  687. procedure tcgppc.a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize;
  688. reg1, reg2: tregister);
  689. var
  690. instr: TAiCpu;
  691. bytesize : byte;
  692. begin
  693. {$ifdef extdebug}
  694. list.concat(tai_comment.create(strpnew('a_load_reg_reg from : ' + cgsize2string(fromsize) + ' to ' + cgsize2string(tosize))));
  695. {$endif}
  696. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  697. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  698. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  699. ( is_signed_cgsize(fromsize) and (not is_signed_cgsize(tosize)) and
  700. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> tcgsize2size[OS_INT]) ) then begin
  701. case tosize of
  702. OS_S8:
  703. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  704. OS_S16:
  705. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  706. OS_S32:
  707. instr := taicpu.op_reg_reg(A_EXTSW,reg2,reg1);
  708. OS_8, OS_16, OS_32:
  709. instr := taicpu.op_reg_reg_const_const(A_RLDICL, reg2, reg1, 0, (8-tcgsize2size[tosize])*8);
  710. OS_S64, OS_64:
  711. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  712. end;
  713. end else
  714. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  715. list.concat(instr);
  716. rg[R_INTREGISTER].add_move_instruction(instr);
  717. end;
  718. procedure tcgppc.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  719. begin
  720. {$ifdef extdebug}
  721. list.concat(tai_comment.create(strpnew('a_load_subsetreg_reg subsetregsize = ' + cgsize2string(sreg.subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + intToStr(sreg.startbit) + ' tosize = ' + cgsize2string(tosize))));
  722. {$endif}
  723. { do the extraction if required and then extend the sign correctly. (The latter is actually required only for signed subsets
  724. and if that subset is not >= the tosize). }
  725. if (sreg.startbit <> 0) or
  726. (sreg.bitlen <> tcgsize2size[subsetsize]*8) then begin
  727. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, destreg, sreg.subsetreg, (64 - sreg.startbit) and 63, 64 - sreg.bitlen));
  728. if (subsetsize in [OS_S8..OS_S128]) then
  729. if ((sreg.bitlen mod 8) = 0) then begin
  730. a_load_reg_reg(list, tcgsize2unsigned[subsetsize], subsetsize, destreg, destreg);
  731. a_load_reg_reg(list, subsetsize, tosize, destreg, destreg);
  732. end else begin
  733. a_op_const_reg(list,OP_SHL,OS_INT,64-sreg.bitlen,destreg);
  734. a_op_const_reg(list,OP_SAR,OS_INT,64-sreg.bitlen,destreg);
  735. end;
  736. end else begin
  737. a_load_reg_reg(list, tcgsize2unsigned[sreg.subsetregsize], subsetsize, sreg.subsetreg, destreg);
  738. a_load_reg_reg(list, subsetsize, tosize, destreg, destreg);
  739. end;
  740. end;
  741. procedure tcgppc.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  742. begin
  743. {$ifdef extdebug}
  744. list.concat(tai_comment.create(strpnew('a_load_reg_subsetreg fromsize = ' + cgsize2string(fromsize) + ' subsetregsize = ' + cgsize2string(sreg.subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + IntToStr(sreg.startbit))));
  745. {$endif}
  746. if (slopt in [SL_SETZERO,SL_SETMAX]) then
  747. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  748. else if (sreg.bitlen <> sizeof(aint)*8) then
  749. { simply use the INSRDI instruction }
  750. list.concat(taicpu.op_reg_reg_const_const(A_INSRDI, sreg.subsetreg, fromreg, sreg.bitlen, (64 - (sreg.startbit + sreg.bitlen)) and 63))
  751. else
  752. a_load_reg_reg(list, fromsize, subsetsize, fromreg, sreg.subsetreg);
  753. end;
  754. procedure tcgppc.a_load_const_subsetreg(list: TAsmlist; subsetsize: tcgsize;
  755. a: aint; const sreg: tsubsetregister);
  756. var
  757. tmpreg : TRegister;
  758. begin
  759. {$ifdef extdebug}
  760. list.concat(tai_comment.create(strpnew('a_load_const_subsetreg subsetregsize = ' + cgsize2string(sreg.subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + intToStr(sreg.startbit) + ' a = ' + intToStr(a))));
  761. {$endif}
  762. { loading the constant into the lowest bits of a temp register and then inserting is
  763. better than loading some usually large constants and do some masking and shifting on ppc64 }
  764. tmpreg := getintregister(list,subsetsize);
  765. a_load_const_reg(list,subsetsize,a,tmpreg);
  766. a_load_reg_subsetreg(list, subsetsize, subsetsize, tmpreg, sreg);
  767. end;
  768. procedure tcgppc.a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  769. aint; reg: TRegister);
  770. begin
  771. a_op_const_reg_reg(list, op, size, a, reg, reg);
  772. end;
  773. procedure tcgppc.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  774. dst: TRegister);
  775. begin
  776. a_op_reg_reg_reg(list, op, size, src, dst, dst);
  777. end;
  778. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  779. size: tcgsize; a: aint; src, dst: tregister);
  780. var
  781. useReg : boolean;
  782. procedure do_lo_hi(loOp, hiOp : TAsmOp);
  783. begin
  784. { Optimization for logical ops (excluding AND), trying to do this as efficiently
  785. as possible by only generating code for the affected halfwords. Note that all
  786. the instructions handled here must have "X op 0 = X" for every halfword. }
  787. usereg := false;
  788. if (aword(a) > high(dword)) then begin
  789. usereg := true;
  790. end else begin
  791. if (word(a) <> 0) then begin
  792. list.concat(taicpu.op_reg_reg_const(loOp, dst, src, word(a)));
  793. if (word(a shr 16) <> 0) then
  794. list.concat(taicpu.op_reg_reg_const(hiOp, dst, dst, word(a shr 16)));
  795. end else if (word(a shr 16) <> 0) then
  796. list.concat(taicpu.op_reg_reg_const(hiOp, dst, src, word(a shr 16)));
  797. end;
  798. end;
  799. procedure do_lo_hi_and;
  800. begin
  801. { optimization logical and with immediate: only use "andi." for 16 bit
  802. ands, otherwise use register method. Doing this for 32 bit constants
  803. would not give any advantage to the register method (via useReg := true),
  804. requiring a scratch register and three instructions. }
  805. usereg := false;
  806. if (aword(a) > high(word)) then
  807. usereg := true
  808. else
  809. list.concat(taicpu.op_reg_reg_const(A_ANDI_, dst, src, word(a)));
  810. end;
  811. procedure do_constant_div(list : TAsmList; size : TCgSize; a : aint; src, dst : TRegister;
  812. signed : boolean);
  813. const
  814. negops : array[boolean] of tasmop = (A_NEG, A_NEGO);
  815. var
  816. magic, shift : int64;
  817. u_magic : qword;
  818. u_shift : byte;
  819. u_add : boolean;
  820. power : byte;
  821. isNegPower : boolean;
  822. divreg : tregister;
  823. begin
  824. if (a = 0) then begin
  825. internalerror(2005061701);
  826. end else if (a = 1) then begin
  827. cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, src, dst);
  828. end else if (a = -1) and (signed) then begin
  829. { note: only in the signed case possible..., may overflow }
  830. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(negops[cs_check_overflow in current_settings.localswitches], dst, src));
  831. end else if (ispowerof2(a, power, isNegPower)) then begin
  832. if (signed) then begin
  833. { From "The PowerPC Compiler Writer's Guide", pg. 52ff }
  834. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, power,
  835. src, dst);
  836. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_ADDZE, dst, dst));
  837. if (isNegPower) then
  838. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  839. end else begin
  840. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, power, src, dst)
  841. end;
  842. end else begin
  843. { replace division by multiplication, both implementations }
  844. { from "The PowerPC Compiler Writer's Guide" pg. 53ff }
  845. divreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  846. if (signed) then begin
  847. getmagic_signedN(sizeof(aInt)*8, a, magic, shift);
  848. { load magic value }
  849. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, magic, divreg);
  850. { multiply }
  851. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHD, dst, src, divreg));
  852. { add/subtract numerator }
  853. if (a > 0) and (magic < 0) then begin
  854. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, src, dst, dst);
  855. end else if (a < 0) and (magic > 0) then begin
  856. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, src, dst, dst);
  857. end;
  858. { shift shift places to the right (arithmetic) }
  859. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, shift, dst, dst);
  860. { extract and add sign bit }
  861. if (a >= 0) then begin
  862. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 63, src, divreg);
  863. end else begin
  864. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 63, dst, divreg);
  865. end;
  866. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, dst, divreg, dst);
  867. end else begin
  868. getmagic_unsignedN(sizeof(aWord)*8, a, u_magic, u_add, u_shift);
  869. { load magic in divreg }
  870. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, aint(u_magic), divreg);
  871. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHDU, dst, src, divreg));
  872. if (u_add) then begin
  873. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, dst, src, divreg);
  874. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 1, divreg, divreg);
  875. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, divreg, dst, divreg);
  876. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, u_shift-1, divreg, dst);
  877. end else begin
  878. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, u_shift, dst, dst);
  879. end;
  880. end;
  881. end;
  882. end;
  883. var
  884. scratchreg: tregister;
  885. shift : byte;
  886. shiftmask : longint;
  887. isneg : boolean;
  888. begin
  889. { subtraction is the same as addition with negative constant }
  890. if op = OP_SUB then begin
  891. a_op_const_reg_reg(list, OP_ADD, size, -a, src, dst);
  892. exit;
  893. end;
  894. {$IFDEF EXTDEBUG}
  895. list.concat(tai_comment.create(strpnew('a_op_const_reg_reg ' + cgop2string(op))));
  896. {$ENDIF EXTDEBUG}
  897. { This case includes some peephole optimizations for the various operations,
  898. (e.g. AND, OR, XOR, ..) - can't this be done at some higher level,
  899. independent of architecture? }
  900. { assume that we do not need a scratch register for the operation }
  901. useReg := false;
  902. case (op) of
  903. OP_DIV, OP_IDIV:
  904. if (cs_opt_level1 in current_settings.optimizerswitches) then
  905. do_constant_div(list, size, a, src, dst, op = OP_IDIV)
  906. else
  907. usereg := true;
  908. OP_IMUL, OP_MUL:
  909. { idea: factorize constant multiplicands and use adds/shifts with few factors;
  910. however, even a 64 bit multiply is already quite fast on PPC64 }
  911. if (a = 0) then
  912. a_load_const_reg(list, size, 0, dst)
  913. else if (a = -1) then
  914. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst))
  915. else if (a = 1) then
  916. a_load_reg_reg(list, OS_INT, OS_INT, src, dst)
  917. else if ispowerof2(a, shift, isneg) then begin
  918. list.concat(taicpu.op_reg_reg_const(A_SLDI, dst, src, shift));
  919. if (isneg) then
  920. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  921. end else if (a >= low(smallint)) and (a <= high(smallint)) then
  922. list.concat(taicpu.op_reg_reg_const(A_MULLI, dst, src,
  923. smallint(a)))
  924. else
  925. usereg := true;
  926. OP_ADD:
  927. if (a = 0) then
  928. a_load_reg_reg(list, size, size, src, dst)
  929. else if (a >= low(smallint)) and (a <= high(smallint)) then
  930. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst, src, smallint(a)))
  931. else
  932. useReg := true;
  933. OP_OR:
  934. if (a = 0) then
  935. a_load_reg_reg(list, size, size, src, dst)
  936. else if (a = -1) then
  937. a_load_const_reg(list, size, -1, dst)
  938. else
  939. do_lo_hi(A_ORI, A_ORIS);
  940. OP_AND:
  941. if (a = 0) then
  942. a_load_const_reg(list, size, 0, dst)
  943. else if (a = -1) then
  944. a_load_reg_reg(list, size, size, src, dst)
  945. else
  946. do_lo_hi_and;
  947. OP_XOR:
  948. if (a = 0) then
  949. a_load_reg_reg(list, size, size, src, dst)
  950. else if (a = -1) then
  951. list.concat(taicpu.op_reg_reg(A_NOT, dst, src))
  952. else
  953. do_lo_hi(A_XORI, A_XORIS);
  954. OP_SHL, OP_SHR, OP_SAR:
  955. begin
  956. if (size in [OS_64, OS_S64]) then
  957. shift := 6
  958. else
  959. shift := 5;
  960. shiftmask := (1 shl shift)-1;
  961. if (a and shiftmask) <> 0 then begin
  962. list.concat(taicpu.op_reg_reg_const(
  963. TShiftOpCG2AsmOpConst[size in [OS_64, OS_S64], op], dst, src, a and shiftmask));
  964. end else
  965. a_load_reg_reg(list, size, size, src, dst);
  966. if ((a shr shift) <> 0) then
  967. internalError(68991);
  968. end
  969. else
  970. internalerror(200109091);
  971. end;
  972. { if all else failed, load the constant in a register and then
  973. perform the operation }
  974. if (useReg) then begin
  975. scratchreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  976. a_load_const_reg(list, size, a, scratchreg);
  977. a_op_reg_reg_reg(list, op, size, scratchreg, src, dst);
  978. end else
  979. maybeadjustresult(list, op, size, dst);
  980. end;
  981. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  982. size: tcgsize; src1, src2, dst: tregister);
  983. const
  984. op_reg_reg_opcg2asmop32: array[TOpCG] of tasmop =
  985. (A_NONE, A_MR, A_ADD, A_AND, A_DIVWU, A_DIVW, A_MULLW, A_MULLW, A_NEG, A_NOT, A_OR,
  986. A_SRAW, A_SLW, A_SRW, A_SUB, A_XOR);
  987. op_reg_reg_opcg2asmop64: array[TOpCG] of tasmop =
  988. (A_NONE, A_MR, A_ADD, A_AND, A_DIVDU, A_DIVD, A_MULLD, A_MULLD, A_NEG, A_NOT, A_OR,
  989. A_SRAD, A_SLD, A_SRD, A_SUB, A_XOR);
  990. begin
  991. case op of
  992. OP_NEG, OP_NOT:
  993. begin
  994. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src1));
  995. if (op = OP_NOT) and not (size in [OS_64, OS_S64]) then
  996. { zero/sign extend result again, fromsize is not important here }
  997. a_load_reg_reg(list, OS_S64, size, dst, dst)
  998. end;
  999. else
  1000. if (size in [OS_64, OS_S64]) then begin
  1001. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src2,
  1002. src1));
  1003. end else begin
  1004. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop32[op], dst, src2,
  1005. src1));
  1006. maybeadjustresult(list, op, size, dst);
  1007. end;
  1008. end;
  1009. end;
  1010. {*************** compare instructructions ****************}
  1011. procedure tcgppc.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1012. cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1013. const
  1014. { unsigned useconst 32bit-op }
  1015. cmpop_table : array[boolean, boolean, boolean] of TAsmOp = (
  1016. ((A_CMPD, A_CMPW), (A_CMPDI, A_CMPWI)),
  1017. ((A_CMPLD, A_CMPLW), (A_CMPLDI, A_CMPLWI))
  1018. );
  1019. var
  1020. tmpreg : TRegister;
  1021. signed, useconst : boolean;
  1022. opsize : TCgSize;
  1023. op : TAsmOp;
  1024. begin
  1025. {$IFDEF EXTDEBUG}
  1026. list.concat(tai_comment.create(strpnew('a_cmp_const_reg_label ' + cgsize2string(size) + ' ' + booltostr(cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE]) + ' ' + inttostr(a) )));
  1027. {$ENDIF EXTDEBUG}
  1028. signed := cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE];
  1029. { in the following case, we generate more efficient code when
  1030. signed is true }
  1031. if (cmp_op in [OC_EQ, OC_NE]) and
  1032. (aword(a) > $FFFF) then
  1033. signed := true;
  1034. opsize := size;
  1035. { do we need to change the operand size because ppc64 only supports 32 and
  1036. 64 bit compares? }
  1037. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then begin
  1038. if (signed) then
  1039. opsize := OS_S32
  1040. else
  1041. opsize := OS_32;
  1042. a_load_reg_reg(current_asmdata.CurrAsmList, size, opsize, reg, reg);
  1043. end;
  1044. { can we use immediate compares? }
  1045. useconst := (signed and ( (a >= low(smallint)) and (a <= high(smallint)))) or
  1046. ((not signed) and (aword(a) <= $FFFF));
  1047. op := cmpop_table[not signed, useconst, opsize in [OS_32, OS_S32]];
  1048. if (useconst) then begin
  1049. list.concat(taicpu.op_reg_reg_const(op, NR_CR0, reg, a));
  1050. end else begin
  1051. tmpreg := getintregister(current_asmdata.CurrAsmList, OS_INT);
  1052. a_load_const_reg(current_asmdata.CurrAsmList, opsize, a, tmpreg);
  1053. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg, tmpreg));
  1054. end;
  1055. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  1056. end;
  1057. procedure tcgppc.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize;
  1058. cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1059. var
  1060. op: tasmop;
  1061. begin
  1062. {$IFDEF extdebug}
  1063. list.concat(tai_comment.create(strpnew('a_cmp_reg_reg_label, size ' + cgsize2string(size) + ' op ' + inttostr(ord(cmp_op)))));
  1064. {$ENDIF extdebug}
  1065. {$note Commented out below check because of compiler weirdness}
  1066. {
  1067. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then
  1068. internalerror(200606041);
  1069. }
  1070. if cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE] then
  1071. if (size in [OS_64, OS_S64]) then
  1072. op := A_CMPD
  1073. else
  1074. op := A_CMPW
  1075. else
  1076. if (size in [OS_64, OS_S64]) then
  1077. op := A_CMPLD
  1078. else
  1079. op := A_CMPLW;
  1080. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg2, reg1));
  1081. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  1082. end;
  1083. procedure tcgppc.a_jmp_name_direct(list : TAsmList; s : string; prependDot : boolean);
  1084. var
  1085. p: taicpu;
  1086. begin
  1087. if (prependDot) then
  1088. s := '.' + s;
  1089. p := taicpu.op_sym(A_B, current_asmdata.RefAsmSymbol(s));
  1090. p.is_jmp := true;
  1091. list.concat(p)
  1092. end;
  1093. procedure tcgppc.a_jmp_name(list: TAsmList; const s: string);
  1094. var
  1095. p: taicpu;
  1096. begin
  1097. if (target_info.system = system_powerpc64_darwin) then
  1098. begin
  1099. p := taicpu.op_sym(A_B,get_darwin_call_stub(s));
  1100. p.is_jmp := true;
  1101. list.concat(p)
  1102. end
  1103. else
  1104. a_jmp_name_direct(list, s, true);
  1105. end;
  1106. procedure tcgppc.a_jmp_always(list: TAsmList; l: tasmlabel);
  1107. begin
  1108. a_jmp(list, A_B, C_None, 0, l);
  1109. end;
  1110. procedure tcgppc.a_jmp_flags(list: TAsmList; const f: TResFlags; l:
  1111. tasmlabel);
  1112. var
  1113. c: tasmcond;
  1114. begin
  1115. c := flags_to_cond(f);
  1116. a_jmp(list, A_BC, c.cond, c.cr - RS_CR0, l);
  1117. end;
  1118. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f:
  1119. TResFlags; reg: TRegister);
  1120. var
  1121. testbit: byte;
  1122. bitvalue: boolean;
  1123. begin
  1124. { get the bit to extract from the conditional register + its requested value (0 or 1) }
  1125. testbit := ((f.cr - RS_CR0) * 4);
  1126. case f.flag of
  1127. F_EQ, F_NE:
  1128. begin
  1129. inc(testbit, 2);
  1130. bitvalue := f.flag = F_EQ;
  1131. end;
  1132. F_LT, F_GE:
  1133. begin
  1134. bitvalue := f.flag = F_LT;
  1135. end;
  1136. F_GT, F_LE:
  1137. begin
  1138. inc(testbit);
  1139. bitvalue := f.flag = F_GT;
  1140. end;
  1141. else
  1142. internalerror(200112261);
  1143. end;
  1144. { load the conditional register in the destination reg }
  1145. list.concat(taicpu.op_reg(A_MFCR, reg));
  1146. { we will move the bit that has to be tested to bit 0 by rotating left }
  1147. testbit := (testbit + 1) and 31;
  1148. { extract bit }
  1149. list.concat(taicpu.op_reg_reg_const_const_const(
  1150. A_RLWINM,reg,reg,testbit,31,31));
  1151. { if we need the inverse, xor with 1 }
  1152. if not bitvalue then
  1153. list.concat(taicpu.op_reg_reg_const(A_XORI, reg, reg, 1));
  1154. end;
  1155. { *********** entry/exit code and address loading ************ }
  1156. procedure tcgppc.g_save_registers(list: TAsmList);
  1157. begin
  1158. { this work is done in g_proc_entry; additionally it is not safe
  1159. to use it because it is called at some weird time }
  1160. end;
  1161. procedure tcgppc.g_restore_registers(list: TAsmList);
  1162. begin
  1163. { this work is done in g_proc_exit; mainly because it is not safe to
  1164. put the register restore code here because it is called at some weird time }
  1165. end;
  1166. procedure tcgppc.calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  1167. var
  1168. reg : TSuperRegister;
  1169. begin
  1170. fprcount := 0;
  1171. firstfpr := RS_F31;
  1172. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1173. for reg := RS_F14 to RS_F31 do
  1174. if reg in rg[R_FPUREGISTER].used_in_proc then begin
  1175. fprcount := ord(RS_F31)-ord(reg)+1;
  1176. firstfpr := reg;
  1177. break;
  1178. end;
  1179. end;
  1180. procedure tcgppc.calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  1181. var
  1182. reg : TSuperRegister;
  1183. begin
  1184. gprcount := 0;
  1185. firstgpr := RS_R31;
  1186. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1187. for reg := RS_R14 to RS_R31 do
  1188. if reg in rg[R_INTREGISTER].used_in_proc then begin
  1189. gprcount := ord(RS_R31)-ord(reg)+1;
  1190. firstgpr := reg;
  1191. break;
  1192. end;
  1193. end;
  1194. procedure tcgppc.profilecode_savepara(para : tparavarsym; list : TAsmList);
  1195. begin
  1196. case (para.paraloc[calleeside].location^.loc) of
  1197. LOC_REGISTER, LOC_CREGISTER:
  1198. a_load_reg_ref(list, OS_INT, para.paraloc[calleeside].Location^.size,
  1199. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  1200. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1201. a_loadfpu_reg_ref(list, para.paraloc[calleeside].Location^.size,
  1202. para.paraloc[calleeside].Location^.size,
  1203. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  1204. LOC_MMREGISTER, LOC_CMMREGISTER:
  1205. { not supported }
  1206. internalerror(2006041801);
  1207. end;
  1208. end;
  1209. procedure tcgppc.profilecode_restorepara(para : tparavarsym; list : TAsmList);
  1210. begin
  1211. case (para.paraloc[calleeside].Location^.loc) of
  1212. LOC_REGISTER, LOC_CREGISTER:
  1213. a_load_ref_reg(list, para.paraloc[calleeside].Location^.size, OS_INT,
  1214. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  1215. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1216. a_loadfpu_ref_reg(list, para.paraloc[calleeside].Location^.size,
  1217. para.paraloc[calleeside].Location^.size,
  1218. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  1219. LOC_MMREGISTER, LOC_CMMREGISTER:
  1220. { not supported }
  1221. internalerror(2006041802);
  1222. end;
  1223. end;
  1224. procedure tcgppc.g_profilecode(list: TAsmList);
  1225. begin
  1226. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_savepara), list);
  1227. a_call_name_direct(list, '_mcount', false, true);
  1228. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_restorepara), list);
  1229. end;
  1230. { Generates the entry code of a procedure/function.
  1231. This procedure may be called before, as well as after g_return_from_proc
  1232. is called. localsize is the sum of the size necessary for local variables
  1233. and the maximum possible combined size of ALL the parameters of a procedure
  1234. called by the current one
  1235. IMPORTANT: registers are not to be allocated through the register
  1236. allocator here, because the register colouring has already occured !!
  1237. }
  1238. procedure tcgppc.g_proc_entry(list: TAsmList; localsize: longint;
  1239. nostackframe: boolean);
  1240. var
  1241. firstregfpu, firstreggpr: TSuperRegister;
  1242. needslinkreg: boolean;
  1243. fprcount, gprcount : aint;
  1244. { Save standard registers, both FPR and GPR; does not support VMX/Altivec }
  1245. procedure save_standard_registers;
  1246. var
  1247. regcount : TSuperRegister;
  1248. href : TReference;
  1249. mayNeedLRStore : boolean;
  1250. begin
  1251. { there are two ways to do this: manually, by generating a few "std" instructions,
  1252. or via the restore helper functions. The latter are selected by the -Og switch,
  1253. i.e. "optimize for size" }
  1254. if (cs_opt_size in current_settings.optimizerswitches) and
  1255. (target_info.system <> system_powerpc64_darwin) then begin
  1256. mayNeedLRStore := false;
  1257. if ((fprcount > 0) and (gprcount > 0)) then begin
  1258. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1259. a_call_name_direct(list, '_savegpr1_' + intToStr(32-gprcount), false, false, false);
  1260. a_call_name_direct(list, '_savefpr_' + intToStr(32-fprcount), false, false, false);
  1261. end else if (gprcount > 0) then
  1262. a_call_name_direct(list, '_savegpr0_' + intToStr(32-gprcount), false, false, false)
  1263. else if (fprcount > 0) then
  1264. a_call_name_direct(list, '_savefpr_' + intToStr(32-fprcount), false, false, false)
  1265. else
  1266. mayNeedLRStore := true;
  1267. end else begin
  1268. { save registers, FPU first, then GPR }
  1269. reference_reset_base(href, NR_STACK_POINTER_REG, -8);
  1270. if (fprcount > 0) then
  1271. for regcount := RS_F31 downto firstregfpu do begin
  1272. a_loadfpu_reg_ref(list, OS_FLOAT, OS_FLOAT, newreg(R_FPUREGISTER,
  1273. regcount, R_SUBNONE), href);
  1274. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1275. end;
  1276. if (gprcount > 0) then
  1277. for regcount := RS_R31 downto firstreggpr do begin
  1278. a_load_reg_ref(list, OS_INT, OS_INT, newreg(R_INTREGISTER, regcount,
  1279. R_SUBNONE), href);
  1280. dec(href.offset, tcgsize2size[OS_INT]);
  1281. end;
  1282. { VMX registers not supported by FPC atm }
  1283. { in this branch we always need to store LR ourselves}
  1284. mayNeedLRStore := true;
  1285. end;
  1286. { we may need to store R0 (=LR) ourselves }
  1287. if ((cs_profile in init_settings.moduleswitches) or (mayNeedLRStore)) and (needslinkreg) then begin
  1288. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_ELF);
  1289. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  1290. end;
  1291. end;
  1292. var
  1293. href: treference;
  1294. begin
  1295. calcFirstUsedFPR(firstregfpu, fprcount);
  1296. calcFirstUsedGPR(firstreggpr, gprcount);
  1297. { calculate real stack frame size }
  1298. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1299. gprcount, fprcount);
  1300. { determine whether we need to save the link register }
  1301. needslinkreg :=
  1302. not(nostackframe) and
  1303. (save_lr_in_prologue or
  1304. ((cs_opt_size in current_settings.optimizerswitches) and
  1305. ((fprcount > 0) or
  1306. (gprcount > 0))));
  1307. a_reg_alloc(list, NR_STACK_POINTER_REG);
  1308. a_reg_alloc(list, NR_R0);
  1309. { move link register to r0 }
  1310. if (needslinkreg) then
  1311. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  1312. save_standard_registers;
  1313. { save old stack frame pointer }
  1314. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then begin
  1315. a_reg_alloc(list, NR_OLD_STACK_POINTER_REG);
  1316. list.concat(taicpu.op_reg_reg(A_MR, NR_OLD_STACK_POINTER_REG, NR_STACK_POINTER_REG));
  1317. end;
  1318. { create stack frame }
  1319. if (not nostackframe) and (localsize > 0) and
  1320. tppcprocinfo(current_procinfo).needstackframe then begin
  1321. if (localsize <= high(smallint)) then begin
  1322. reference_reset_base(href, NR_STACK_POINTER_REG, -localsize);
  1323. a_load_store(list, A_STDU, NR_STACK_POINTER_REG, href);
  1324. end else begin
  1325. reference_reset_base(href, NR_NO, -localsize);
  1326. { Use R0 for loading the constant (which is definitely > 32k when entering
  1327. this branch).
  1328. Inlined at this position because it must not use temp registers because
  1329. register allocations have already been done }
  1330. { Code template:
  1331. lis r0,ofs@highest
  1332. ori r0,r0,ofs@higher
  1333. sldi r0,r0,32
  1334. oris r0,r0,ofs@h
  1335. ori r0,r0,ofs@l
  1336. }
  1337. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1338. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1339. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1340. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1341. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1342. list.concat(taicpu.op_reg_reg_reg(A_STDUX, NR_R1, NR_R1, NR_R0));
  1343. end;
  1344. end;
  1345. { CR register not used by FPC atm }
  1346. { keep R1 allocated??? }
  1347. a_reg_dealloc(list, NR_R0);
  1348. end;
  1349. { Generates the exit code for a method.
  1350. This procedure may be called before, as well as after g_stackframe_entry
  1351. is called.
  1352. IMPORTANT: registers are not to be allocated through the register
  1353. allocator here, because the register colouring has already occured !!
  1354. }
  1355. procedure tcgppc.g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  1356. boolean);
  1357. var
  1358. firstregfpu, firstreggpr: TSuperRegister;
  1359. needslinkreg : boolean;
  1360. fprcount, gprcount: aint;
  1361. { Restore standard registers, both FPR and GPR; does not support VMX/Altivec }
  1362. procedure restore_standard_registers;
  1363. var
  1364. { flag indicating whether we need to manually add the exit code (e.g. blr instruction)
  1365. or not }
  1366. needsExitCode : Boolean;
  1367. href : treference;
  1368. regcount : TSuperRegister;
  1369. begin
  1370. { there are two ways to do this: manually, by generating a few "ld" instructions,
  1371. or via the restore helper functions. The latter are selected by the -Og switch,
  1372. i.e. "optimize for size" }
  1373. if (cs_opt_size in current_settings.optimizerswitches) then begin
  1374. needsExitCode := false;
  1375. if ((fprcount > 0) and (gprcount > 0)) then begin
  1376. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1377. a_call_name_direct(list, '_restgpr1_' + intToStr(32-gprcount), false, false, false);
  1378. a_jmp_name_direct(list, '_restfpr_' + intToStr(32-fprcount), false);
  1379. end else if (gprcount > 0) then
  1380. a_jmp_name_direct(list, '_restgpr0_' + intToStr(32-gprcount), false)
  1381. else if (fprcount > 0) then
  1382. a_jmp_name_direct(list, '_restfpr_' + intToStr(32-fprcount), false)
  1383. else
  1384. needsExitCode := true;
  1385. end else begin
  1386. needsExitCode := true;
  1387. { restore registers, FPU first, GPR next }
  1388. reference_reset_base(href, NR_STACK_POINTER_REG, -tcgsize2size[OS_FLOAT]);
  1389. if (fprcount > 0) then
  1390. for regcount := RS_F31 downto firstregfpu do begin
  1391. a_loadfpu_ref_reg(list, OS_FLOAT, OS_FLOAT, href, newreg(R_FPUREGISTER, regcount,
  1392. R_SUBNONE));
  1393. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1394. end;
  1395. if (gprcount > 0) then
  1396. for regcount := RS_R31 downto firstreggpr do begin
  1397. a_load_ref_reg(list, OS_INT, OS_INT, href, newreg(R_INTREGISTER, regcount,
  1398. R_SUBNONE));
  1399. dec(href.offset, tcgsize2size[OS_INT]);
  1400. end;
  1401. { VMX not supported by FPC atm }
  1402. end;
  1403. if (needsExitCode) then begin
  1404. { restore LR (if needed) }
  1405. if (needslinkreg) then begin
  1406. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_ELF);
  1407. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1408. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1409. end;
  1410. { generate return instruction }
  1411. list.concat(taicpu.op_none(A_BLR));
  1412. end;
  1413. end;
  1414. var
  1415. href: treference;
  1416. localsize : aint;
  1417. begin
  1418. calcFirstUsedFPR(firstregfpu, fprcount);
  1419. calcFirstUsedGPR(firstreggpr, gprcount);
  1420. { determine whether we need to restore the link register }
  1421. needslinkreg :=
  1422. not(nostackframe) and
  1423. (((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1424. ((pi_do_call in current_procinfo.flags) or (cs_profile in init_settings.moduleswitches))) or
  1425. ((cs_opt_size in current_settings.optimizerswitches) and ((fprcount > 0) or (gprcount > 0))) or
  1426. ([cs_lineinfo, cs_debuginfo] * current_settings.moduleswitches <> []));
  1427. { calculate stack frame }
  1428. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1429. gprcount, fprcount);
  1430. { CR register not supported }
  1431. { restore stack pointer }
  1432. if (not nostackframe) and (localsize > 0) and
  1433. tppcprocinfo(current_procinfo).needstackframe then begin
  1434. if (localsize <= high(smallint)) then begin
  1435. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1436. end else begin
  1437. reference_reset_base(href, NR_NO, localsize);
  1438. { use R0 for loading the constant (which is definitely > 32k when entering
  1439. this branch)
  1440. Inlined because it must not use temp registers because register allocations
  1441. have already been done
  1442. }
  1443. { Code template:
  1444. lis r0,ofs@highest
  1445. ori r0,ofs@higher
  1446. sldi r0,r0,32
  1447. oris r0,r0,ofs@h
  1448. ori r0,r0,ofs@l
  1449. }
  1450. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1451. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1452. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1453. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1454. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1455. list.concat(taicpu.op_reg_reg_reg(A_ADD, NR_R1, NR_R1, NR_R0));
  1456. end;
  1457. end;
  1458. restore_standard_registers;
  1459. end;
  1460. procedure tcgppc.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  1461. tregister);
  1462. var
  1463. ref2, tmpref: treference;
  1464. { register used to construct address }
  1465. tempreg : TRegister;
  1466. begin
  1467. if (target_info.system = system_powerpc64_darwin) then
  1468. begin
  1469. inherited a_loadaddr_ref_reg(list,ref,r);
  1470. exit;
  1471. end;
  1472. ref2 := ref;
  1473. fixref(list, ref2);
  1474. { load a symbol }
  1475. if (assigned(ref2.symbol) or (hasLargeOffset(ref2))) then begin
  1476. { add the symbol's value to the base of the reference, and if the }
  1477. { reference doesn't have a base, create one }
  1478. reference_reset(tmpref);
  1479. tmpref.offset := ref2.offset;
  1480. tmpref.symbol := ref2.symbol;
  1481. tmpref.relsymbol := ref2.relsymbol;
  1482. { load 64 bit reference into r. If the reference already has a base register,
  1483. first load the 64 bit value into a temp register, then add it to the result
  1484. register rD }
  1485. if (ref2.base <> NR_NO) then begin
  1486. { already have a base register, so allocate a new one }
  1487. tempreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1488. end else begin
  1489. tempreg := r;
  1490. end;
  1491. { code for loading a reference from a symbol into a register rD }
  1492. (*
  1493. lis rX,SYM@highest
  1494. ori rX,SYM@higher
  1495. sldi rX,rX,32
  1496. oris rX,rX,SYM@h
  1497. ori rX,rX,SYM@l
  1498. *)
  1499. {$IFDEF EXTDEBUG}
  1500. list.concat(tai_comment.create(strpnew('loadaddr_ref_reg ')));
  1501. {$ENDIF EXTDEBUG}
  1502. if (assigned(tmpref.symbol)) then begin
  1503. tmpref.refaddr := addr_highest;
  1504. list.concat(taicpu.op_reg_ref(A_LIS, tempreg, tmpref));
  1505. tmpref.refaddr := addr_higher;
  1506. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1507. list.concat(taicpu.op_reg_reg_const(A_SLDI, tempreg, tempreg, 32));
  1508. tmpref.refaddr := addr_high;
  1509. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tempreg, tempreg, tmpref));
  1510. tmpref.refaddr := addr_low;
  1511. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1512. end else
  1513. a_load_const_reg(list, OS_ADDR, tmpref.offset, tempreg);
  1514. { if there's already a base register, add the temp register contents to
  1515. the base register }
  1516. if (ref2.base <> NR_NO) then begin
  1517. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, tempreg, ref2.base));
  1518. end;
  1519. end else if (ref2.offset <> 0) then begin
  1520. { no symbol, but offset <> 0 }
  1521. if (ref2.base <> NR_NO) then begin
  1522. a_op_const_reg_reg(list, OP_ADD, OS_64, ref2.offset, ref2.base, r)
  1523. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never
  1524. occurs, so now only ref.offset has to be loaded }
  1525. end else begin
  1526. a_load_const_reg(list, OS_64, ref2.offset, r);
  1527. end;
  1528. end else if (ref2.index <> NR_NO) then begin
  1529. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, ref2.base, ref2.index))
  1530. end else if (ref2.base <> NR_NO) and
  1531. (r <> ref2.base) then begin
  1532. a_load_reg_reg(list, OS_ADDR, OS_ADDR, ref2.base, r)
  1533. end else begin
  1534. list.concat(taicpu.op_reg_const(A_LI, r, 0));
  1535. end;
  1536. end;
  1537. { ************* concatcopy ************ }
  1538. procedure tcgppc.g_concatcopy(list: TAsmList; const source, dest: treference;
  1539. len: aint);
  1540. var
  1541. countreg, tempreg:TRegister;
  1542. src, dst: TReference;
  1543. lab: tasmlabel;
  1544. count, count2, step: longint;
  1545. size: tcgsize;
  1546. begin
  1547. {$IFDEF extdebug}
  1548. if len > high(aint) then
  1549. internalerror(2002072704);
  1550. list.concat(tai_comment.create(strpnew('g_concatcopy1 ' + inttostr(len) + ' bytes left ')));
  1551. {$ENDIF extdebug}
  1552. { if the references are equal, exit, there is no need to copy anything }
  1553. if references_equal(source, dest) or
  1554. (len=0) then
  1555. exit;
  1556. { make sure short loads are handled as optimally as possible;
  1557. note that the data here never overlaps, so we can do a forward
  1558. copy at all times.
  1559. NOTE: maybe use some scratch registers to pair load/store instructions
  1560. }
  1561. if (len <= 8) then begin
  1562. src := source; dst := dest;
  1563. {$IFDEF extdebug}
  1564. list.concat(tai_comment.create(strpnew('g_concatcopy3 ' + inttostr(src.offset) + ' ' + inttostr(dst.offset))));
  1565. {$ENDIF extdebug}
  1566. while (len <> 0) do begin
  1567. if (len = 8) then begin
  1568. a_load_ref_ref(list, OS_64, OS_64, src, dst);
  1569. dec(len, 8);
  1570. end else if (len >= 4) then begin
  1571. a_load_ref_ref(list, OS_32, OS_32, src, dst);
  1572. inc(src.offset, 4); inc(dst.offset, 4);
  1573. dec(len, 4);
  1574. end else if (len >= 2) then begin
  1575. a_load_ref_ref(list, OS_16, OS_16, src, dst);
  1576. inc(src.offset, 2); inc(dst.offset, 2);
  1577. dec(len, 2);
  1578. end else begin
  1579. a_load_ref_ref(list, OS_8, OS_8, src, dst);
  1580. inc(src.offset, 1); inc(dst.offset, 1);
  1581. dec(len, 1);
  1582. end;
  1583. end;
  1584. exit;
  1585. end;
  1586. {$IFDEF extdebug}
  1587. list.concat(tai_comment.create(strpnew('g_concatcopy2 ' + inttostr(len) + ' bytes left ')));
  1588. {$ENDIF extdebug}
  1589. if not(source.alignment in [1,2]) and
  1590. not(dest.alignment in [1,2]) then
  1591. begin
  1592. count:=len div 8;
  1593. step:=8;
  1594. size:=OS_64;
  1595. end
  1596. else
  1597. begin
  1598. count:=len div 4;
  1599. step:=4;
  1600. size:=OS_32;
  1601. end;
  1602. tempreg:=getintregister(list,size);
  1603. reference_reset(src);
  1604. reference_reset(dst);
  1605. { load the address of source into src.base }
  1606. if (count > 4) or
  1607. not issimpleref(source) or
  1608. ((source.index <> NR_NO) and
  1609. ((source.offset + len) > high(smallint))) then begin
  1610. src.base := getaddressregister(list);
  1611. a_loadaddr_ref_reg(list, source, src.base);
  1612. end else begin
  1613. src := source;
  1614. end;
  1615. { load the address of dest into dst.base }
  1616. if (count > 4) or
  1617. not issimpleref(dest) or
  1618. ((dest.index <> NR_NO) and
  1619. ((dest.offset + len) > high(smallint))) then begin
  1620. dst.base := getaddressregister(list);
  1621. a_loadaddr_ref_reg(list, dest, dst.base);
  1622. end else begin
  1623. dst := dest;
  1624. end;
  1625. { generate a loop }
  1626. if count > 4 then begin
  1627. { the offsets are zero after the a_loadaddress_ref_reg and just
  1628. have to be set to step. I put an Inc there so debugging may be
  1629. easier (should offset be different from zero here, it will be
  1630. easy to notice in the generated assembler }
  1631. inc(dst.offset, step);
  1632. inc(src.offset, step);
  1633. list.concat(taicpu.op_reg_reg_const(A_SUBI, src.base, src.base, step));
  1634. list.concat(taicpu.op_reg_reg_const(A_SUBI, dst.base, dst.base, step));
  1635. countreg := getintregister(list, OS_INT);
  1636. a_load_const_reg(list, OS_INT, count, countreg);
  1637. current_asmdata.getjumplabel(lab);
  1638. a_label(list, lab);
  1639. list.concat(taicpu.op_reg_reg_const(A_SUBIC_, countreg, countreg, 1));
  1640. if (size=OS_64) then
  1641. begin
  1642. list.concat(taicpu.op_reg_ref(A_LDU, tempreg, src));
  1643. list.concat(taicpu.op_reg_ref(A_STDU, tempreg, dst));
  1644. end
  1645. else
  1646. begin
  1647. list.concat(taicpu.op_reg_ref(A_LWZU, tempreg, src));
  1648. list.concat(taicpu.op_reg_ref(A_STWU, tempreg, dst));
  1649. end;
  1650. a_jmp(list, A_BC, C_NE, 0, lab);
  1651. a_reg_sync(list,src.base);
  1652. a_reg_sync(list,dst.base);
  1653. a_reg_sync(list,countreg);
  1654. len := len mod step;
  1655. count := 0;
  1656. end;
  1657. { unrolled loop }
  1658. if count > 0 then begin
  1659. for count2 := 1 to count do begin
  1660. a_load_ref_reg(list, size, size, src, tempreg);
  1661. a_load_reg_ref(list, size, size, tempreg, dst);
  1662. inc(src.offset, step);
  1663. inc(dst.offset, step);
  1664. end;
  1665. len := len mod step;
  1666. end;
  1667. if (len and 4) <> 0 then begin
  1668. a_load_ref_reg(list, OS_32, OS_32, src, tempreg);
  1669. a_load_reg_ref(list, OS_32, OS_32, tempreg, dst);
  1670. inc(src.offset, 4);
  1671. inc(dst.offset, 4);
  1672. end;
  1673. { copy the leftovers }
  1674. if (len and 2) <> 0 then begin
  1675. a_load_ref_reg(list, OS_16, OS_16, src, tempreg);
  1676. a_load_reg_ref(list, OS_16, OS_16, tempreg, dst);
  1677. inc(src.offset, 2);
  1678. inc(dst.offset, 2);
  1679. end;
  1680. if (len and 1) <> 0 then begin
  1681. a_load_ref_reg(list, OS_8, OS_8, src, tempreg);
  1682. a_load_reg_ref(list, OS_8, OS_8, tempreg, dst);
  1683. end;
  1684. end;
  1685. procedure tcgppc.g_external_wrapper(list: TAsmList; pd: TProcDef; const externalname: string);
  1686. var
  1687. href : treference;
  1688. begin
  1689. if (target_info.system <> system_powerpc64_linux) then begin
  1690. inherited;
  1691. exit;
  1692. end;
  1693. { for ppc64/linux emit correct code which sets up a stack frame and then calls the
  1694. external method normally to ensure that the GOT/TOC will be loaded correctly if
  1695. required.
  1696. It's not really advantageous to use cg methods here because they are too specialized.
  1697. I.e. the resulting code sequence looks as follows:
  1698. mflr r0
  1699. std r0, 16(r1)
  1700. stdu r1, -112(r1)
  1701. bl <external_method>
  1702. nop
  1703. addi r1, r1, 112
  1704. ld r0, 16(r1)
  1705. mtlr r0
  1706. blr
  1707. }
  1708. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  1709. reference_reset_base(href, NR_STACK_POINTER_REG, 16);
  1710. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  1711. reference_reset_base(href, NR_STACK_POINTER_REG, -MINIMUM_STACKFRAME_SIZE);
  1712. list.concat(taicpu.op_reg_ref(A_STDU, NR_STACK_POINTER_REG, href));
  1713. list.concat(taicpu.op_sym(A_BL, current_asmdata.RefAsmSymbol(externalname)));
  1714. list.concat(taicpu.op_none(A_NOP));
  1715. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, MINIMUM_STACKFRAME_SIZE));
  1716. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_ELF);
  1717. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1718. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1719. list.concat(taicpu.op_none(A_BLR));
  1720. end;
  1721. {***************** This is private property, keep out! :) *****************}
  1722. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  1723. const
  1724. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1725. begin
  1726. {$IFDEF EXTDEBUG}
  1727. list.concat(tai_comment.create(strpnew('maybeadjustresult op = ' + cgop2string(op) + ' size = ' + cgsize2string(size))));
  1728. {$ENDIF EXTDEBUG}
  1729. if (op in overflowops) and (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32]) then
  1730. a_load_reg_reg(list, OS_64, size, dst, dst);
  1731. end;
  1732. function tcgppc.issimpleref(const ref: treference): boolean;
  1733. begin
  1734. if (ref.base = NR_NO) and
  1735. (ref.index <> NR_NO) then
  1736. internalerror(200208101);
  1737. result :=
  1738. not (assigned(ref.symbol)) and
  1739. (((ref.index = NR_NO) and
  1740. (ref.offset >= low(smallint)) and
  1741. (ref.offset <= high(smallint))) or
  1742. ((ref.index <> NR_NO) and
  1743. (ref.offset = 0)));
  1744. end;
  1745. procedure tcgppc.a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  1746. ref: treference);
  1747. procedure maybefixup64bitoffset;
  1748. var
  1749. tmpreg: tregister;
  1750. begin
  1751. { for some instructions we need to check that the offset is divisible by at
  1752. least four. If not, add the bytes which are "off" to the base register and
  1753. adjust the offset accordingly }
  1754. case op of
  1755. A_LD, A_LDU, A_STD, A_STDU, A_LWA :
  1756. if ((ref.offset mod 4) <> 0) then begin
  1757. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1758. if (ref.base <> NR_NO) then begin
  1759. a_op_const_reg_reg(list, OP_ADD, OS_ADDR, ref.offset mod 4, ref.base, tmpreg);
  1760. ref.base := tmpreg;
  1761. end else begin
  1762. list.concat(taicpu.op_reg_const(A_LI, tmpreg, ref.offset mod 4));
  1763. ref.base := tmpreg;
  1764. end;
  1765. ref.offset := (ref.offset div 4) * 4;
  1766. end;
  1767. end;
  1768. end;
  1769. var
  1770. tmpreg, tmpreg2: tregister;
  1771. tmpref: treference;
  1772. largeOffset: Boolean;
  1773. begin
  1774. if (target_info.system = system_powerpc64_darwin) then
  1775. begin
  1776. { darwin/ppc64 works with 32 bit relocatable symbol addresses }
  1777. maybefixup64bitoffset;
  1778. inherited a_load_store(list,op,reg,ref);
  1779. exit
  1780. end;
  1781. { at this point there must not be a combination of values in the ref treference
  1782. which is not possible to directly map to instructions of the PowerPC architecture }
  1783. if (ref.index <> NR_NO) and ((ref.offset <> 0) or (assigned(ref.symbol))) then
  1784. internalerror(200310131);
  1785. { if this is a PIC'ed address, handle it and exit }
  1786. if (ref.refaddr = addr_pic) then begin
  1787. if (ref.offset <> 0) then
  1788. internalerror(2006010501);
  1789. if (ref.index <> NR_NO) then
  1790. internalerror(2006010502);
  1791. if (not assigned(ref.symbol)) then
  1792. internalerror(200601050);
  1793. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1794. exit;
  1795. end;
  1796. maybefixup64bitoffset;
  1797. {$IFDEF EXTDEBUG}
  1798. list.concat(tai_comment.create(strpnew('a_load_store1 ' + BoolToStr(ref.refaddr = addr_pic))));
  1799. {$ENDIF EXTDEBUG}
  1800. { if we have to load/store from a symbol or large addresses, use a temporary register
  1801. containing the address }
  1802. if (assigned(ref.symbol) or (hasLargeOffset(ref))) then begin
  1803. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1804. if (hasLargeOffset(ref) and (ref.base = NR_NO)) then begin
  1805. ref.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1806. a_load_const_reg(list, OS_ADDR, ref.offset, ref.base);
  1807. ref.offset := 0;
  1808. end;
  1809. reference_reset(tmpref);
  1810. tmpref.symbol := ref.symbol;
  1811. tmpref.relsymbol := ref.relsymbol;
  1812. tmpref.offset := ref.offset;
  1813. if (ref.base <> NR_NO) then begin
  1814. { As long as the TOC isn't working we try to achieve highest speed (in this
  1815. case by allowing instructions execute in parallel) as possible at the cost
  1816. of using another temporary register. So the code template when there is
  1817. a base register and an offset is the following:
  1818. lis rT1, SYM+offs@highest
  1819. ori rT1, rT1, SYM+offs@higher
  1820. lis rT2, SYM+offs@hi
  1821. ori rT2, SYM+offs@lo
  1822. rldimi rT2, rT1, 32
  1823. <op>X reg, base, rT2
  1824. }
  1825. tmpreg2 := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1826. if (assigned(tmpref.symbol)) then begin
  1827. tmpref.refaddr := addr_highest;
  1828. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1829. tmpref.refaddr := addr_higher;
  1830. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1831. tmpref.refaddr := addr_high;
  1832. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg2, tmpref));
  1833. tmpref.refaddr := addr_low;
  1834. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg2, tmpreg2, tmpref));
  1835. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, tmpreg2, tmpreg, 32, 0));
  1836. end else
  1837. a_load_const_reg(list, OS_ADDR, tmpref.offset, tmpreg2);
  1838. reference_reset(tmpref);
  1839. tmpref.base := ref.base;
  1840. tmpref.index := tmpreg2;
  1841. case op of
  1842. { the code generator doesn't generate update instructions anyway, so
  1843. error out on those instructions }
  1844. A_LBZ : op := A_LBZX;
  1845. A_LHZ : op := A_LHZX;
  1846. A_LWZ : op := A_LWZX;
  1847. A_LD : op := A_LDX;
  1848. A_LHA : op := A_LHAX;
  1849. A_LWA : op := A_LWAX;
  1850. A_LFS : op := A_LFSX;
  1851. A_LFD : op := A_LFDX;
  1852. A_STB : op := A_STBX;
  1853. A_STH : op := A_STHX;
  1854. A_STW : op := A_STWX;
  1855. A_STD : op := A_STDX;
  1856. A_STFS : op := A_STFSX;
  1857. A_STFD : op := A_STFDX;
  1858. else
  1859. { unknown load/store opcode }
  1860. internalerror(2005101302);
  1861. end;
  1862. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1863. end else begin
  1864. { when accessing value from a reference without a base register, use the
  1865. following code template:
  1866. lis rT,SYM+offs@highesta
  1867. ori rT,SYM+offs@highera
  1868. sldi rT,rT,32
  1869. oris rT,rT,SYM+offs@ha
  1870. ld rD,SYM+offs@l(rT)
  1871. }
  1872. tmpref.refaddr := addr_highesta;
  1873. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1874. tmpref.refaddr := addr_highera;
  1875. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1876. list.concat(taicpu.op_reg_reg_const(A_SLDI, tmpreg, tmpreg, 32));
  1877. tmpref.refaddr := addr_higha;
  1878. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tmpreg, tmpreg, tmpref));
  1879. tmpref.base := tmpreg;
  1880. tmpref.refaddr := addr_low;
  1881. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1882. end;
  1883. end else begin
  1884. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1885. end;
  1886. end;
  1887. procedure tcgppc.loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  1888. var
  1889. l: tasmsymbol;
  1890. ref: treference;
  1891. symname : string;
  1892. begin
  1893. maybe_new_object_file(current_asmdata.asmlists[al_picdata]);
  1894. symname := '_$' + current_asmdata.name + '$toc$' + hexstr(a, sizeof(a)*2);
  1895. l:=current_asmdata.getasmsymbol(symname);
  1896. if not(assigned(l)) then begin
  1897. l:=current_asmdata.DefineAsmSymbol(symname,AB_GLOBAL, AT_DATA);
  1898. current_asmdata.asmlists[al_picdata].concat(tai_section.create(sec_toc, '.toc', 8));
  1899. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create_global(l,0));
  1900. current_asmdata.asmlists[al_picdata].concat(tai_directive.create(asd_toc_entry, symname + '[TC], ' + inttostr(a)));
  1901. end;
  1902. reference_reset_symbol(ref,l,0);
  1903. ref.base := NR_R2;
  1904. ref.refaddr := addr_no;
  1905. {$IFDEF EXTDEBUG}
  1906. list.concat(tai_comment.create(strpnew('loading value from TOC reference for ' + symname)));
  1907. {$ENDIF EXTDEBUG}
  1908. cg.a_load_ref_reg(list, OS_INT, OS_INT, ref, reg);
  1909. end;
  1910. begin
  1911. cg := tcgppc.create;
  1912. end.