cgcpu.pas 76 KB

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