cgcpu.pas 80 KB

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