cgcpu.pas 80 KB

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