cgcpu.pas 70 KB

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