cgcpu.pas 81 KB

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