cgcpu.pas 85 KB

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