cgcpu.pas 77 KB

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