2
0

cgcpu.pas 78 KB

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