cgcpu.pas 77 KB

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