cgcpu.pas 75 KB

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