cgcpu.pas 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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,cgppc,
  23. aasmbase,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcgppcgen)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  36. procedure a_call_name(list : TAsmList;const s : string);override;
  37. procedure a_call_reg(list : TAsmList;reg: tregister); override;
  38. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  39. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  40. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  41. size: tcgsize; a: aint; src, dst: tregister); override;
  42. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  43. size: tcgsize; src1, src2, dst: tregister); override;
  44. { move instructions }
  45. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);override;
  46. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  47. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  48. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize: tcgsize;
  49. tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); override;
  50. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tcgsize; const fromsreg, tosreg: tsubsetregister); override;
  51. { comparison operations }
  52. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  53. l : tasmlabel);override;
  54. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_name(list : TAsmList;const s : string); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  59. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  60. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  61. procedure g_save_registers(list:TAsmList); override;
  62. procedure g_restore_registers(list:TAsmList); override;
  63. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  64. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  65. { that's the case, we can use rlwinm to do an AND operation }
  66. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  67. protected
  68. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  69. private
  70. (* NOT IN USE: *)
  71. procedure g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  72. (* NOT IN USE: *)
  73. procedure g_return_from_proc_mac(list : TAsmList;parasize : aint);
  74. { clear out potential overflow bits from 8 or 16 bit operations }
  75. { the upper 24/16 bits of a register after an operation }
  76. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  77. { returns whether a reference can be used immediately in a powerpc }
  78. { instruction }
  79. function issimpleref(const ref: treference): boolean;
  80. function save_regs(list : TAsmList):longint;
  81. procedure restore_regs(list : TAsmList);
  82. end;
  83. tcg64fppc = class(tcg64f32)
  84. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  85. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  86. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  87. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  88. end;
  89. const
  90. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDI,A_ANDI_,A_DIVWU,
  91. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  92. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI,A_NONE,A_NONE);
  93. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDIS,A_ANDIS_,
  94. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  95. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS,A_NONE,A_NONE);
  96. implementation
  97. uses
  98. globals,verbose,systems,cutils,
  99. symconst,symsym,fmodule,
  100. rgobj,tgobj,cpupi,procinfo,paramgr;
  101. procedure tcgppc.init_register_allocators;
  102. begin
  103. inherited init_register_allocators;
  104. if target_info.system=system_powerpc_darwin then
  105. begin
  106. {
  107. if pi_needs_got in current_procinfo.flags then
  108. begin
  109. current_procinfo.got:=NR_R31;
  110. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  111. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  112. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  113. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  114. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  115. RS_R14,RS_R13],first_int_imreg,[]);
  116. end
  117. else}
  118. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  119. [{$ifdef user0} RS_R0,{$endif} RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  120. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  121. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  122. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  123. RS_R14,RS_R13],first_int_imreg,[]);
  124. end
  125. else
  126. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  127. [{$ifdef user0} RS_R0,{$endif}RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  128. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  129. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  130. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  131. RS_R14,RS_R13],first_int_imreg,[]);
  132. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  133. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  134. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  135. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  136. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  137. { TODO: FIX ME}
  138. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  139. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  140. end;
  141. procedure tcgppc.done_register_allocators;
  142. begin
  143. rg[R_INTREGISTER].free;
  144. rg[R_FPUREGISTER].free;
  145. rg[R_MMREGISTER].free;
  146. inherited done_register_allocators;
  147. end;
  148. procedure tcgppc.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);
  149. var
  150. tmpref, ref: treference;
  151. location: pcgparalocation;
  152. sizeleft: aint;
  153. begin
  154. location := paraloc.location;
  155. tmpref := r;
  156. sizeleft := paraloc.intsize;
  157. while assigned(location) do
  158. begin
  159. case location^.loc of
  160. LOC_REGISTER,LOC_CREGISTER:
  161. begin
  162. {$ifndef cpu64bitaddr}
  163. if (sizeleft <> 3) then
  164. begin
  165. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  166. end
  167. else
  168. begin
  169. a_load_ref_reg(list,OS_16,OS_16,tmpref,location^.register);
  170. a_reg_alloc(list,NR_R0);
  171. inc(tmpref.offset,2);
  172. a_load_ref_reg(list,OS_8,OS_8,tmpref,newreg(R_INTREGISTER,RS_R0,R_SUBNONE));
  173. a_op_const_reg(list,OP_SHL,OS_INT,16,location^.register);
  174. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location^.register,newreg(R_INTREGISTER,RS_R0,R_SUBNONE),8,16,31-8));
  175. a_reg_dealloc(list,NR_R0);
  176. dec(tmpref.offset,2);
  177. end;
  178. {$else not cpu64bitaddr}
  179. {$error add 64 bit support for non power of 2 loads in a_param_ref}
  180. {$endif not cpu64bitaddr}
  181. end;
  182. LOC_REFERENCE:
  183. begin
  184. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  185. g_concatcopy(list,tmpref,ref,sizeleft);
  186. if assigned(location^.next) then
  187. internalerror(2005010710);
  188. end;
  189. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  190. case location^.size of
  191. OS_F32, OS_F64:
  192. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  193. else
  194. internalerror(2002072801);
  195. end;
  196. LOC_VOID:
  197. begin
  198. // nothing to do
  199. end;
  200. else
  201. internalerror(2002081103);
  202. end;
  203. inc(tmpref.offset,tcgsize2size[location^.size]);
  204. dec(sizeleft,tcgsize2size[location^.size]);
  205. location := location^.next;
  206. end;
  207. end;
  208. { calling a procedure by name }
  209. procedure tcgppc.a_call_name(list : TAsmList;const s : string);
  210. begin
  211. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  212. if it is a cross-TOC call. If so, it also replaces the NOP
  213. with some restore code.}
  214. if (target_info.system <> system_powerpc_darwin) then
  215. begin
  216. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s)));
  217. if target_info.system=system_powerpc_macos then
  218. list.concat(taicpu.op_none(A_NOP));
  219. end
  220. else
  221. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  222. {
  223. the compiler does not properly set this flag anymore in pass 1, and
  224. for now we only need it after pass 2 (I hope) (JM)
  225. if not(pi_do_call in current_procinfo.flags) then
  226. internalerror(2003060703);
  227. }
  228. include(current_procinfo.flags,pi_do_call);
  229. end;
  230. { calling a procedure by address }
  231. procedure tcgppc.a_call_reg(list : TAsmList;reg: tregister);
  232. var
  233. tmpreg : tregister;
  234. tmpref : treference;
  235. begin
  236. if target_info.system=system_powerpc_macos then
  237. begin
  238. {Generate instruction to load the procedure address from
  239. the transition vector.}
  240. //TODO: Support cross-TOC calls.
  241. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  242. reference_reset(tmpref);
  243. tmpref.offset := 0;
  244. //tmpref.symaddr := refs_full;
  245. tmpref.base:= reg;
  246. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  247. end
  248. else
  249. tmpreg:=reg;
  250. inherited a_call_reg(list,tmpreg);
  251. end;
  252. {********************** load instructions ********************}
  253. procedure tcgppc.a_load_const_reg(list : TAsmList; size: TCGSize; a : aint; reg : TRegister);
  254. begin
  255. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  256. internalerror(2002090902);
  257. if (a >= low(smallint)) and
  258. (a <= high(smallint)) then
  259. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  260. else if ((a and $ffff) <> 0) then
  261. begin
  262. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  263. if ((a shr 16) <> 0) or
  264. (smallint(a and $ffff) < 0) then
  265. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  266. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  267. end
  268. else
  269. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  270. end;
  271. procedure tcgppc.a_load_ref_reg(list : TAsmList; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  272. const
  273. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  274. { indexed? updating?}
  275. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  276. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  277. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  278. { 64bit stuff should be handled separately }
  279. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  280. { 128bit stuff too }
  281. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  282. { there's no load-byte-with-sign-extend :( }
  283. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  284. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  285. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  286. var
  287. op: tasmop;
  288. ref2: treference;
  289. begin
  290. { TODO: optimize/take into consideration fromsize/tosize. Will }
  291. { probably only matter for OS_S8 loads though }
  292. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  293. internalerror(2002090902);
  294. ref2 := ref;
  295. fixref(list,ref2);
  296. { the caller is expected to have adjusted the reference already }
  297. { in this case }
  298. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  299. fromsize := tosize;
  300. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  301. a_load_store(list,op,reg,ref2);
  302. { sign extend shortint if necessary (because there is
  303. no load instruction to sign extend an 8 bit value automatically)
  304. and mask out extra sign bits when loading from a smaller signed
  305. to a larger unsigned type }
  306. if fromsize = OS_S8 then
  307. begin
  308. a_load_reg_reg(list, OS_8, OS_S8, reg, reg);
  309. a_load_reg_reg(list, OS_S8, tosize, reg, reg);
  310. end;
  311. end;
  312. procedure tcgppc.a_load_reg_reg(list : TAsmList;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  313. var
  314. instr: taicpu;
  315. begin
  316. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  317. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  318. (fromsize <> tosize)) or
  319. { needs to mask out the sign in the top 16 bits }
  320. ((fromsize = OS_S8) and
  321. (tosize = OS_16)) then
  322. case tosize of
  323. OS_8:
  324. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  325. reg2,reg1,0,31-8+1,31);
  326. OS_S8:
  327. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  328. OS_16:
  329. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  330. reg2,reg1,0,31-16+1,31);
  331. OS_S16:
  332. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  333. OS_32,OS_S32:
  334. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  335. else internalerror(2002090901);
  336. end
  337. else
  338. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  339. list.concat(instr);
  340. rg[R_INTREGISTER].add_move_instruction(instr);
  341. end;
  342. procedure tcgppc.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  343. begin
  344. if (sreg.bitlen > 32) then
  345. internalerror(2008020701);
  346. if (sreg.bitlen <> 32) then
  347. begin
  348. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,destreg,
  349. sreg.subsetreg,(32-sreg.startbit) and 31,32-sreg.bitlen,31));
  350. { types with a negative lower bound are always a base type (8, 16, 32 bits) }
  351. if (subsetsize in [OS_S8..OS_S128]) then
  352. if ((sreg.bitlen mod 8) = 0) then
  353. begin
  354. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,destreg,destreg);
  355. a_load_reg_reg(list,subsetsize,tosize,destreg,destreg);
  356. end
  357. else
  358. begin
  359. a_op_const_reg(list,OP_SHL,OS_INT,32-sreg.bitlen,destreg);
  360. a_op_const_reg(list,OP_SAR,OS_INT,32-sreg.bitlen,destreg);
  361. end;
  362. end
  363. else
  364. a_load_reg_reg(list,subsetsize,tosize,sreg.subsetreg,destreg);
  365. end;
  366. procedure tcgppc.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  367. begin
  368. if (slopt in [SL_SETZERO,SL_SETMAX]) then
  369. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  370. else if (sreg.bitlen>32) then
  371. internalerror(2008020702)
  372. else if (sreg.bitlen <> 32) then
  373. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,sreg.subsetreg,fromreg,
  374. sreg.startbit,32-sreg.startbit-sreg.bitlen,31-sreg.startbit))
  375. else
  376. a_load_reg_reg(list,fromsize,subsetsize,fromreg,sreg.subsetreg);
  377. end;
  378. procedure tcgppc.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tcgsize; const fromsreg, tosreg: tsubsetregister);
  379. begin
  380. if (tosreg.bitlen>32) or (tosreg.startbit>31) then
  381. internalerror(2008020703);
  382. if (fromsreg.bitlen >= tosreg.bitlen) then
  383. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,tosreg.subsetreg, fromsreg.subsetreg,
  384. (tosreg.startbit-fromsreg.startbit) and 31,
  385. 32-tosreg.startbit-tosreg.bitlen,31-tosreg.startbit))
  386. else
  387. inherited a_load_subsetreg_subsetreg(list,fromsubsetsize,tosubsetsize,fromsreg,tosreg);
  388. end;
  389. procedure tcgppc.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  390. begin
  391. a_op_const_reg_reg(list,op,size,a,reg,reg);
  392. end;
  393. procedure tcgppc.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  394. begin
  395. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  396. end;
  397. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  398. const
  399. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  400. begin
  401. if (op in overflowops) and
  402. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  403. a_load_reg_reg(list,OS_32,size,dst,dst);
  404. end;
  405. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  406. size: tcgsize; a: aint; src, dst: tregister);
  407. var
  408. l1,l2: longint;
  409. oplo, ophi: tasmop;
  410. scratchreg: tregister;
  411. useReg, gotrlwi: boolean;
  412. procedure do_lo_hi;
  413. begin
  414. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  415. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  416. end;
  417. begin
  418. if (op = OP_MOVE) then
  419. internalerror(2006031401);
  420. if op = OP_SUB then
  421. begin
  422. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  423. exit;
  424. end;
  425. ophi := TOpCG2AsmOpConstHi[op];
  426. oplo := TOpCG2AsmOpConstLo[op];
  427. gotrlwi := get_rlwi_const(a,l1,l2);
  428. if (op in [OP_AND,OP_OR,OP_XOR]) then
  429. begin
  430. if (a = 0) then
  431. begin
  432. if op = OP_AND then
  433. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  434. else
  435. a_load_reg_reg(list,size,size,src,dst);
  436. exit;
  437. end
  438. else if (a = -1) then
  439. begin
  440. case op of
  441. OP_OR:
  442. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  443. OP_XOR:
  444. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  445. OP_AND:
  446. a_load_reg_reg(list,size,size,src,dst);
  447. end;
  448. exit;
  449. end
  450. else if (aword(a) <= high(word)) and
  451. ((op <> OP_AND) or
  452. not gotrlwi) then
  453. begin
  454. if ((size = OS_8) and
  455. (byte(a) <> a)) or
  456. ((size = OS_S8) and
  457. (shortint(a) <> a)) then
  458. internalerror(200604142);
  459. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  460. { and/or/xor -> cannot overflow in high 16 bits }
  461. exit;
  462. end;
  463. { all basic constant instructions also have a shifted form that }
  464. { works only on the highest 16bits, so if lo(a) is 0, we can }
  465. { use that one }
  466. if (word(a) = 0) and
  467. (not(op = OP_AND) or
  468. not gotrlwi) then
  469. begin
  470. if (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  471. internalerror(200604141);
  472. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  473. exit;
  474. end;
  475. end
  476. else if (op = OP_ADD) then
  477. if a = 0 then
  478. begin
  479. a_load_reg_reg(list,size,size,src,dst);
  480. exit
  481. end
  482. else if (a >= low(smallint)) and
  483. (a <= high(smallint)) then
  484. begin
  485. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  486. maybeadjustresult(list,op,size,dst);
  487. exit;
  488. end;
  489. { otherwise, the instructions we can generate depend on the }
  490. { operation }
  491. useReg := false;
  492. case op of
  493. OP_DIV,OP_IDIV:
  494. if (a = 0) then
  495. internalerror(200208103)
  496. else if (a = 1) then
  497. begin
  498. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  499. exit
  500. end
  501. else if ispowerof2(a,l1) then
  502. begin
  503. case op of
  504. OP_DIV:
  505. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  506. OP_IDIV:
  507. begin
  508. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  509. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  510. end;
  511. end;
  512. exit;
  513. end
  514. else
  515. usereg := true;
  516. OP_IMUL, OP_MUL:
  517. if (a = 0) then
  518. begin
  519. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  520. exit
  521. end
  522. else if (a = 1) then
  523. begin
  524. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  525. exit
  526. end
  527. else if ispowerof2(a,l1) then
  528. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  529. else if (longint(a) >= low(smallint)) and
  530. (longint(a) <= high(smallint)) then
  531. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  532. else
  533. usereg := true;
  534. OP_ADD:
  535. begin
  536. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  537. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  538. smallint((a shr 16) + ord(smallint(a) < 0))));
  539. end;
  540. OP_OR:
  541. { try to use rlwimi }
  542. if gotrlwi and
  543. (src = dst) then
  544. begin
  545. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  546. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  547. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  548. scratchreg,0,l1,l2));
  549. end
  550. else
  551. do_lo_hi;
  552. OP_AND:
  553. { try to use rlwinm }
  554. if gotrlwi then
  555. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  556. src,0,l1,l2))
  557. else
  558. useReg := true;
  559. OP_XOR:
  560. do_lo_hi;
  561. OP_SHL,OP_SHR,OP_SAR:
  562. begin
  563. if (a and 31) <> 0 Then
  564. list.concat(taicpu.op_reg_reg_const(
  565. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  566. else
  567. a_load_reg_reg(list,size,size,src,dst);
  568. if (a shr 5) <> 0 then
  569. internalError(68991);
  570. end;
  571. OP_ROL:
  572. begin
  573. if (not (size in [OS_32, OS_S32])) then begin
  574. internalerror(2008091307);
  575. end;
  576. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, a and 31, 0, 31));
  577. end;
  578. OP_ROR:
  579. begin
  580. if (not (size in [OS_32, OS_S32])) then begin
  581. internalerror(2008091308);
  582. end;
  583. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, (32 - a) and 31, 0, 31));
  584. end
  585. else
  586. internalerror(200109091);
  587. end;
  588. { if all else failed, load the constant in a register and then }
  589. { perform the operation }
  590. if useReg then
  591. begin
  592. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  593. a_load_const_reg(list,OS_32,a,scratchreg);
  594. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  595. end;
  596. maybeadjustresult(list,op,size,dst);
  597. end;
  598. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  599. size: tcgsize; src1, src2, dst: tregister);
  600. const
  601. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  602. (A_NONE,A_MR,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  603. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR,A_NONE,A_NONE);
  604. var
  605. tmpreg : TRegister;
  606. begin
  607. if (op = OP_MOVE) then
  608. internalerror(2006031402);
  609. case op of
  610. OP_NEG,OP_NOT:
  611. begin
  612. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  613. if (op = OP_NOT) and
  614. not(size in [OS_32,OS_S32]) then
  615. { zero/sign extend result again }
  616. a_load_reg_reg(list,OS_32,size,dst,dst);
  617. end;
  618. OP_ROL:
  619. begin
  620. if (not (size in [OS_32, OS_S32])) then begin
  621. internalerror(2008091305);
  622. end;
  623. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, src1, 0, 31));
  624. end;
  625. OP_ROR:
  626. begin
  627. if (not (size in [OS_32, OS_S32])) then begin
  628. internalerror(2008091306);
  629. end;
  630. tmpreg := getintregister(current_asmdata.CurrAsmList, OS_INT);
  631. list.concat(taicpu.op_reg_reg(A_NEG, tmpreg, src1));
  632. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, tmpreg, 0, 31));
  633. end;
  634. else
  635. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  636. end;
  637. maybeadjustresult(list,op,size,dst);
  638. end;
  639. {*************** compare instructructions ****************}
  640. procedure tcgppc.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  641. l : tasmlabel);
  642. var
  643. scratch_register: TRegister;
  644. signed: boolean;
  645. begin
  646. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE,OC_EQ,OC_NE];
  647. { in the following case, we generate more efficient code when }
  648. { signed is false }
  649. if (cmp_op in [OC_EQ,OC_NE]) and
  650. (aword(a) >= $8000) and
  651. (aword(a) <= $ffff) then
  652. signed := false;
  653. if signed then
  654. if (a >= low(smallint)) and (a <= high(smallint)) Then
  655. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  656. else
  657. begin
  658. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  659. a_load_const_reg(list,OS_32,a,scratch_register);
  660. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  661. end
  662. else
  663. if (aword(a) <= $ffff) then
  664. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  665. else
  666. begin
  667. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  668. a_load_const_reg(list,OS_32,a,scratch_register);
  669. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  670. end;
  671. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  672. end;
  673. procedure tcgppc.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  674. reg1,reg2 : tregister;l : tasmlabel);
  675. var
  676. op: tasmop;
  677. begin
  678. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  679. op := A_CMPW
  680. else
  681. op := A_CMPLW;
  682. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  683. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  684. end;
  685. procedure tcgppc.a_jmp_name(list : TAsmList;const s : string);
  686. var
  687. p : taicpu;
  688. begin
  689. if (target_info.system = system_powerpc_darwin) then
  690. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  691. else
  692. p := taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  693. p.is_jmp := true;
  694. list.concat(p)
  695. end;
  696. procedure tcgppc.a_jmp_always(list : TAsmList;l: tasmlabel);
  697. begin
  698. a_jmp(list,A_B,C_None,0,l);
  699. end;
  700. procedure tcgppc.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  701. var
  702. c: tasmcond;
  703. begin
  704. c := flags_to_cond(f);
  705. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  706. end;
  707. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  708. var
  709. testbit: byte;
  710. bitvalue: boolean;
  711. begin
  712. { get the bit to extract from the conditional register + its }
  713. { requested value (0 or 1) }
  714. testbit := ((f.cr-RS_CR0) * 4);
  715. case f.flag of
  716. F_EQ,F_NE:
  717. begin
  718. inc(testbit,2);
  719. bitvalue := f.flag = F_EQ;
  720. end;
  721. F_LT,F_GE:
  722. begin
  723. bitvalue := f.flag = F_LT;
  724. end;
  725. F_GT,F_LE:
  726. begin
  727. inc(testbit);
  728. bitvalue := f.flag = F_GT;
  729. end;
  730. else
  731. internalerror(200112261);
  732. end;
  733. { load the conditional register in the destination reg }
  734. list.concat(taicpu.op_reg(A_MFCR,reg));
  735. { we will move the bit that has to be tested to bit 0 by rotating }
  736. { left }
  737. testbit := (testbit + 1) and 31;
  738. { extract bit }
  739. list.concat(taicpu.op_reg_reg_const_const_const(
  740. A_RLWINM,reg,reg,testbit,31,31));
  741. { if we need the inverse, xor with 1 }
  742. if not bitvalue then
  743. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  744. end;
  745. (*
  746. procedure tcgppc.g_cond2reg(list: TAsmList; const f: TAsmCond; reg: TRegister);
  747. var
  748. testbit: byte;
  749. bitvalue: boolean;
  750. begin
  751. { get the bit to extract from the conditional register + its }
  752. { requested value (0 or 1) }
  753. case f.simple of
  754. false:
  755. begin
  756. { we don't generate this in the compiler }
  757. internalerror(200109062);
  758. end;
  759. true:
  760. case f.cond of
  761. C_None:
  762. internalerror(200109063);
  763. C_LT..C_NU:
  764. begin
  765. testbit := (ord(f.cr) - ord(R_CR0))*4;
  766. inc(testbit,AsmCondFlag2BI[f.cond]);
  767. bitvalue := AsmCondFlagTF[f.cond];
  768. end;
  769. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  770. begin
  771. testbit := f.crbit
  772. bitvalue := AsmCondFlagTF[f.cond];
  773. end;
  774. else
  775. internalerror(200109064);
  776. end;
  777. end;
  778. { load the conditional register in the destination reg }
  779. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  780. { we will move the bit that has to be tested to bit 31 -> rotate }
  781. { left by bitpos+1 (remember, this is big-endian!) }
  782. if bitpos <> 31 then
  783. inc(bitpos)
  784. else
  785. bitpos := 0;
  786. { extract bit }
  787. list.concat(taicpu.op_reg_reg_const_const_const(
  788. A_RLWINM,reg,reg,bitpos,31,31));
  789. { if we need the inverse, xor with 1 }
  790. if not bitvalue then
  791. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  792. end;
  793. *)
  794. { *********** entry/exit code and address loading ************ }
  795. procedure tcgppc.g_save_registers(list:TAsmList);
  796. begin
  797. { this work is done in g_proc_entry }
  798. end;
  799. procedure tcgppc.g_restore_registers(list:TAsmList);
  800. begin
  801. { this work is done in g_proc_exit }
  802. end;
  803. procedure tcgppc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  804. { generated the entry code of a procedure/function. Note: localsize is the }
  805. { sum of the size necessary for local variables and the maximum possible }
  806. { combined size of ALL the parameters of a procedure called by the current }
  807. { one. }
  808. { This procedure may be called before, as well as after g_return_from_proc }
  809. { is called. NOTE registers are not to be allocated through the register }
  810. { allocator here, because the register colouring has already occured !! }
  811. var regcounter,firstregfpu,firstregint: TSuperRegister;
  812. href : treference;
  813. usesfpr,usesgpr : boolean;
  814. begin
  815. { CR and LR only have to be saved in case they are modified by the current }
  816. { procedure, but currently this isn't checked, so save them always }
  817. { following is the entry code as described in "Altivec Programming }
  818. { Interface Manual", bar the saving of AltiVec registers }
  819. a_reg_alloc(list,NR_STACK_POINTER_REG);
  820. usesgpr := false;
  821. usesfpr := false;
  822. if not(po_assembler in current_procinfo.procdef.procoptions) then
  823. begin
  824. { save link register? }
  825. if save_lr_in_prologue then
  826. begin
  827. a_reg_alloc(list,NR_R0);
  828. { save return address... }
  829. { warning: if this is no longer done via r0, or if r0 is }
  830. { added to the usable registers, adapt tcgppcgen.g_profilecode }
  831. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  832. { ... in caller's frame }
  833. case target_info.abi of
  834. abi_powerpc_aix:
  835. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  836. abi_powerpc_sysv:
  837. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  838. end;
  839. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  840. if not(cs_profile in current_settings.moduleswitches) then
  841. a_reg_dealloc(list,NR_R0);
  842. end;
  843. (*
  844. { save the CR if necessary in callers frame. }
  845. if target_info.abi = abi_powerpc_aix then
  846. if false then { Not needed at the moment. }
  847. begin
  848. a_reg_alloc(list,NR_R0);
  849. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  850. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  851. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  852. a_reg_dealloc(list,NR_R0);
  853. end;
  854. *)
  855. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  856. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  857. usesgpr := firstregint <> 32;
  858. usesfpr := firstregfpu <> 32;
  859. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  860. begin
  861. a_reg_alloc(list,NR_R12);
  862. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  863. end;
  864. end;
  865. if usesfpr then
  866. begin
  867. reference_reset_base(href,NR_R1,-8);
  868. for regcounter:=firstregfpu to RS_F31 do
  869. begin
  870. a_loadfpu_reg_ref(list,OS_F64,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  871. dec(href.offset,8);
  872. end;
  873. { compute start of gpr save area }
  874. inc(href.offset,4);
  875. end
  876. else
  877. { compute start of gpr save area }
  878. reference_reset_base(href,NR_R1,-4);
  879. { save gprs and fetch GOT pointer }
  880. if usesgpr then
  881. begin
  882. if (firstregint <= RS_R22) or
  883. ((cs_opt_size in current_settings.optimizerswitches) and
  884. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  885. (firstregint <= RS_R29)) then
  886. begin
  887. { TODO: TODO: 64 bit support }
  888. dec(href.offset,(RS_R31-firstregint)*sizeof(pint));
  889. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  890. end
  891. else
  892. for regcounter:=firstregint to RS_R31 do
  893. begin
  894. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  895. dec(href.offset,4);
  896. end;
  897. end;
  898. { done in ncgutil because it may only be released after the parameters }
  899. { have been moved to their final resting place }
  900. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  901. { a_reg_dealloc(list,NR_R12); }
  902. if (not nostackframe) and
  903. tppcprocinfo(current_procinfo).needstackframe and
  904. (localsize <> 0) then
  905. begin
  906. if (localsize <= high(smallint)) then
  907. begin
  908. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  909. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  910. end
  911. else
  912. begin
  913. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  914. { can't use getregisterint here, the register colouring }
  915. { is already done when we get here }
  916. { R12 may hold previous stack pointer, R11 may be in }
  917. { use as got => use R0 (but then we can't use }
  918. { a_load_const_reg) }
  919. href.index := NR_R0;
  920. a_reg_alloc(list,href.index);
  921. list.concat(taicpu.op_reg_const(A_LI,NR_R0,smallint((-localsize) and $ffff)));
  922. if (smallint((-localsize) and $ffff) < 0) then
  923. { upper 16 bits are now $ffff -> xor with inverse }
  924. list.concat(taicpu.op_reg_reg_const(A_XORIS,NR_R0,NR_R0,word(not(((-localsize) shr 16) and $ffff))))
  925. else
  926. list.concat(taicpu.op_reg_reg_const(A_ORIS,NR_R0,NR_R0,word(((-localsize) shr 16) and $ffff)));
  927. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  928. a_reg_dealloc(list,href.index);
  929. end;
  930. end;
  931. { save the CR if necessary ( !!! never done currently ) }
  932. { still need to find out where this has to be done for SystemV
  933. a_reg_alloc(list,R_0);
  934. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  935. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  936. new_reference(STACK_POINTER_REG,LA_CR)));
  937. a_reg_dealloc(list,R_0);
  938. }
  939. { now comes the AltiVec context save, not yet implemented !!! }
  940. end;
  941. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  942. { This procedure may be called before, as well as after g_stackframe_entry }
  943. { is called. NOTE registers are not to be allocated through the register }
  944. { allocator here, because the register colouring has already occured !! }
  945. var
  946. regcounter,firstregfpu,firstregint: TsuperRegister;
  947. href : treference;
  948. usesfpr,usesgpr,genret : boolean;
  949. localsize: aint;
  950. begin
  951. { AltiVec context restore, not yet implemented !!! }
  952. usesfpr:=false;
  953. usesgpr:=false;
  954. if not (po_assembler in current_procinfo.procdef.procoptions) then
  955. begin
  956. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  957. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  958. usesgpr := firstregint <> 32;
  959. usesfpr := firstregfpu <> 32;
  960. end;
  961. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  962. { adjust r1 }
  963. { (register allocator is no longer valid at this time and an add of 0 }
  964. { is translated into a move, which is then registered with the register }
  965. { allocator, causing a crash }
  966. if (not nostackframe) and
  967. tppcprocinfo(current_procinfo).needstackframe and
  968. (localsize <> 0) then
  969. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  970. { no return (blr) generated yet }
  971. genret:=true;
  972. if usesfpr then
  973. begin
  974. reference_reset_base(href,NR_R1,-8);
  975. for regcounter := firstregfpu to RS_F31 do
  976. begin
  977. a_loadfpu_ref_reg(list,OS_F64,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  978. dec(href.offset,8);
  979. end;
  980. inc(href.offset,4);
  981. end
  982. else
  983. reference_reset_base(href,NR_R1,-4);
  984. if (usesgpr) then
  985. begin
  986. if (firstregint <= RS_R22) or
  987. ((cs_opt_size in current_settings.optimizerswitches) and
  988. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  989. (firstregint <= RS_R29)) then
  990. begin
  991. { TODO: TODO: 64 bit support }
  992. dec(href.offset,(RS_R31-firstregint)*sizeof(pint));
  993. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  994. end
  995. else
  996. for regcounter:=firstregint to RS_R31 do
  997. begin
  998. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  999. dec(href.offset,4);
  1000. end;
  1001. end;
  1002. (*
  1003. { restore fprs and return }
  1004. if usesfpr then
  1005. begin
  1006. { address of fpr save area to r11 }
  1007. r:=NR_R12;
  1008. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1009. {
  1010. if (pi_do_call in current_procinfo.flags) then
  1011. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x'))
  1012. else
  1013. { leaf node => lr haven't to be restored }
  1014. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  1015. genret:=false;
  1016. }
  1017. end;
  1018. *)
  1019. { if we didn't generate the return code, we've to do it now }
  1020. if genret then
  1021. begin
  1022. { load link register? }
  1023. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1024. begin
  1025. if (pi_do_call in current_procinfo.flags) then
  1026. begin
  1027. case target_info.abi of
  1028. abi_powerpc_aix:
  1029. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1030. abi_powerpc_sysv:
  1031. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1032. end;
  1033. a_reg_alloc(list,NR_R0);
  1034. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1035. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1036. a_reg_dealloc(list,NR_R0);
  1037. end;
  1038. (*
  1039. { restore the CR if necessary from callers frame}
  1040. if target_info.abi = abi_powerpc_aix then
  1041. if false then { Not needed at the moment. }
  1042. begin
  1043. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1044. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1045. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1046. a_reg_dealloc(list,NR_R0);
  1047. end;
  1048. *)
  1049. end;
  1050. list.concat(taicpu.op_none(A_BLR));
  1051. end;
  1052. end;
  1053. function tcgppc.save_regs(list : TAsmList):longint;
  1054. {Generates code which saves used non-volatile registers in
  1055. the save area right below the address the stackpointer point to.
  1056. Returns the actual used save area size.}
  1057. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1058. usesfpr,usesgpr: boolean;
  1059. href : treference;
  1060. offset: aint;
  1061. regcounter2, firstfpureg: Tsuperregister;
  1062. begin
  1063. usesfpr:=false;
  1064. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1065. begin
  1066. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1067. case target_info.abi of
  1068. abi_powerpc_aix:
  1069. firstfpureg := RS_F14;
  1070. abi_powerpc_sysv:
  1071. firstfpureg := RS_F9;
  1072. else
  1073. internalerror(2003122903);
  1074. end;
  1075. for regcounter:=firstfpureg to RS_F31 do
  1076. begin
  1077. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1078. begin
  1079. usesfpr:=true;
  1080. firstregfpu:=regcounter;
  1081. break;
  1082. end;
  1083. end;
  1084. end;
  1085. usesgpr:=false;
  1086. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1087. for regcounter2:=RS_R13 to RS_R31 do
  1088. begin
  1089. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1090. begin
  1091. usesgpr:=true;
  1092. firstreggpr:=regcounter2;
  1093. break;
  1094. end;
  1095. end;
  1096. offset:= 0;
  1097. { save floating-point registers }
  1098. if usesfpr then
  1099. for regcounter := firstregfpu to RS_F31 do
  1100. begin
  1101. offset:= offset - 8;
  1102. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1103. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1104. end;
  1105. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1106. { save gprs in gpr save area }
  1107. if usesgpr then
  1108. if firstreggpr < RS_R30 then
  1109. begin
  1110. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1111. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1112. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1113. {STMW stores multiple registers}
  1114. end
  1115. else
  1116. begin
  1117. for regcounter := firstreggpr to RS_R31 do
  1118. begin
  1119. offset:= offset - 4;
  1120. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1121. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1122. end;
  1123. end;
  1124. { now comes the AltiVec context save, not yet implemented !!! }
  1125. save_regs:= -offset;
  1126. end;
  1127. procedure tcgppc.restore_regs(list : TAsmList);
  1128. {Generates code which restores used non-volatile registers from
  1129. the save area right below the address the stackpointer point to.}
  1130. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1131. usesfpr,usesgpr: boolean;
  1132. href : treference;
  1133. offset: integer;
  1134. regcounter2, firstfpureg: Tsuperregister;
  1135. begin
  1136. usesfpr:=false;
  1137. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1138. begin
  1139. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1140. case target_info.abi of
  1141. abi_powerpc_aix:
  1142. firstfpureg := RS_F14;
  1143. abi_powerpc_sysv:
  1144. firstfpureg := RS_F9;
  1145. else
  1146. internalerror(2003122903);
  1147. end;
  1148. for regcounter:=firstfpureg to RS_F31 do
  1149. begin
  1150. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1151. begin
  1152. usesfpr:=true;
  1153. firstregfpu:=regcounter;
  1154. break;
  1155. end;
  1156. end;
  1157. end;
  1158. usesgpr:=false;
  1159. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1160. for regcounter2:=RS_R13 to RS_R31 do
  1161. begin
  1162. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1163. begin
  1164. usesgpr:=true;
  1165. firstreggpr:=regcounter2;
  1166. break;
  1167. end;
  1168. end;
  1169. offset:= 0;
  1170. { restore fp registers }
  1171. if usesfpr then
  1172. for regcounter := firstregfpu to RS_F31 do
  1173. begin
  1174. offset:= offset - 8;
  1175. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1176. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1177. end;
  1178. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1179. { restore gprs }
  1180. if usesgpr then
  1181. if firstreggpr < RS_R30 then
  1182. begin
  1183. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1184. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1185. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1186. {LMW loads multiple registers}
  1187. end
  1188. else
  1189. begin
  1190. for regcounter := firstreggpr to RS_R31 do
  1191. begin
  1192. offset:= offset - 4;
  1193. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1194. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1195. end;
  1196. end;
  1197. { now comes the AltiVec context restore, not yet implemented !!! }
  1198. end;
  1199. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1200. (* NOT IN USE *)
  1201. { generated the entry code of a procedure/function. Note: localsize is the }
  1202. { sum of the size necessary for local variables and the maximum possible }
  1203. { combined size of ALL the parameters of a procedure called by the current }
  1204. { one }
  1205. const
  1206. macosLinkageAreaSize = 24;
  1207. var
  1208. href : treference;
  1209. registerSaveAreaSize : longint;
  1210. begin
  1211. if (localsize mod 8) <> 0 then
  1212. internalerror(58991);
  1213. { CR and LR only have to be saved in case they are modified by the current }
  1214. { procedure, but currently this isn't checked, so save them always }
  1215. { following is the entry code as described in "Altivec Programming }
  1216. { Interface Manual", bar the saving of AltiVec registers }
  1217. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1218. a_reg_alloc(list,NR_R0);
  1219. { save return address in callers frame}
  1220. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1221. { ... in caller's frame }
  1222. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1223. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1224. a_reg_dealloc(list,NR_R0);
  1225. { save non-volatile registers in callers frame}
  1226. registerSaveAreaSize:= save_regs(list);
  1227. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1228. a_reg_alloc(list,NR_R0);
  1229. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1230. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1231. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1232. a_reg_dealloc(list,NR_R0);
  1233. (*
  1234. { save pointer to incoming arguments }
  1235. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1236. *)
  1237. (*
  1238. a_reg_alloc(list,R_12);
  1239. { 0 or 8 based on SP alignment }
  1240. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1241. R_12,STACK_POINTER_REG,0,28,28));
  1242. { add in stack length }
  1243. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1244. -localsize));
  1245. { establish new alignment }
  1246. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1247. a_reg_dealloc(list,R_12);
  1248. *)
  1249. { allocate stack frame }
  1250. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1251. inc(localsize,tg.lasttemp);
  1252. localsize:=align(localsize,16);
  1253. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1254. if (localsize <> 0) then
  1255. begin
  1256. if (localsize <= high(smallint)) then
  1257. begin
  1258. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1259. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1260. end
  1261. else
  1262. begin
  1263. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1264. href.index := NR_R11;
  1265. a_reg_alloc(list,href.index);
  1266. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1267. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1268. a_reg_dealloc(list,href.index);
  1269. end;
  1270. end;
  1271. end;
  1272. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : aint);
  1273. (* NOT IN USE *)
  1274. var
  1275. href : treference;
  1276. begin
  1277. a_reg_alloc(list,NR_R0);
  1278. { restore stack pointer }
  1279. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1280. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1281. (*
  1282. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1283. *)
  1284. { restore the CR if necessary from callers frame
  1285. ( !!! always done currently ) }
  1286. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1287. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1288. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1289. a_reg_dealloc(list,NR_R0);
  1290. (*
  1291. { restore return address from callers frame }
  1292. reference_reset_base(href,STACK_POINTER_REG,8);
  1293. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1294. *)
  1295. { restore non-volatile registers from callers frame }
  1296. restore_regs(list);
  1297. (*
  1298. { return to caller }
  1299. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1300. list.concat(taicpu.op_none(A_BLR));
  1301. *)
  1302. { restore return address from callers frame }
  1303. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1304. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1305. { return to caller }
  1306. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1307. list.concat(taicpu.op_none(A_BLR));
  1308. end;
  1309. { ************* concatcopy ************ }
  1310. {$ifdef use8byteconcatcopy}
  1311. const
  1312. maxmoveunit = 8;
  1313. {$else use8byteconcatcopy}
  1314. const
  1315. maxmoveunit = 4;
  1316. {$endif use8byteconcatcopy}
  1317. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1318. var
  1319. countreg: TRegister;
  1320. src, dst: TReference;
  1321. lab: tasmlabel;
  1322. count, count2: aint;
  1323. size: tcgsize;
  1324. copyreg: tregister;
  1325. begin
  1326. {$ifdef extdebug}
  1327. if len > high(longint) then
  1328. internalerror(2002072704);
  1329. {$endif extdebug}
  1330. if (references_equal(source,dest)) then
  1331. exit;
  1332. { make sure short loads are handled as optimally as possible }
  1333. if (len <= maxmoveunit) and
  1334. (byte(len) in [1,2,4,8]) then
  1335. begin
  1336. if len < 8 then
  1337. begin
  1338. size := int_cgsize(len);
  1339. a_load_ref_ref(list,size,size,source,dest);
  1340. end
  1341. else
  1342. begin
  1343. copyreg := getfpuregister(list,OS_F64);
  1344. a_loadfpu_ref_reg(list,OS_F64,OS_F64,source,copyreg);
  1345. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dest);
  1346. end;
  1347. exit;
  1348. end;
  1349. count := len div maxmoveunit;
  1350. reference_reset(src);
  1351. reference_reset(dst);
  1352. { load the address of source into src.base }
  1353. if (count > 4) or
  1354. not issimpleref(source) or
  1355. ((source.index <> NR_NO) and
  1356. ((source.offset + longint(len)) > high(smallint))) then
  1357. begin
  1358. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1359. a_loadaddr_ref_reg(list,source,src.base);
  1360. end
  1361. else
  1362. begin
  1363. src := source;
  1364. end;
  1365. { load the address of dest into dst.base }
  1366. if (count > 4) or
  1367. not issimpleref(dest) or
  1368. ((dest.index <> NR_NO) and
  1369. ((dest.offset + longint(len)) > high(smallint))) then
  1370. begin
  1371. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1372. a_loadaddr_ref_reg(list,dest,dst.base);
  1373. end
  1374. else
  1375. begin
  1376. dst := dest;
  1377. end;
  1378. {$ifdef use8byteconcatcopy}
  1379. if count > 4 then
  1380. { generate a loop }
  1381. begin
  1382. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1383. { have to be set to 8. I put an Inc there so debugging may be }
  1384. { easier (should offset be different from zero here, it will be }
  1385. { easy to notice in the generated assembler }
  1386. inc(dst.offset,8);
  1387. inc(src.offset,8);
  1388. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1389. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1390. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1391. a_load_const_reg(list,OS_32,count,countreg);
  1392. copyreg := getfpuregister(list,OS_F64);
  1393. a_reg_sync(list,copyreg);
  1394. current_asmdata.getjumplabel(lab);
  1395. a_label(list, lab);
  1396. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1397. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1398. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1399. a_jmp(list,A_BC,C_NE,0,lab);
  1400. a_reg_sync(list,copyreg);
  1401. len := len mod 8;
  1402. end;
  1403. count := len div 8;
  1404. if count > 0 then
  1405. { unrolled loop }
  1406. begin
  1407. copyreg := getfpuregister(list,OS_F64);
  1408. for count2 := 1 to count do
  1409. begin
  1410. a_loadfpu_ref_reg(list,OS_F64,OS_F64,src,copyreg);
  1411. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dst);
  1412. inc(src.offset,8);
  1413. inc(dst.offset,8);
  1414. end;
  1415. len := len mod 8;
  1416. end;
  1417. if (len and 4) <> 0 then
  1418. begin
  1419. a_reg_alloc(list,NR_R0);
  1420. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1421. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1422. inc(src.offset,4);
  1423. inc(dst.offset,4);
  1424. a_reg_dealloc(list,NR_R0);
  1425. end;
  1426. {$else use8byteconcatcopy}
  1427. if count > 4 then
  1428. { generate a loop }
  1429. begin
  1430. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1431. { have to be set to 4. I put an Inc there so debugging may be }
  1432. { easier (should offset be different from zero here, it will be }
  1433. { easy to notice in the generated assembler }
  1434. inc(dst.offset,4);
  1435. inc(src.offset,4);
  1436. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1437. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1438. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1439. a_load_const_reg(list,OS_32,count,countreg);
  1440. { explicitely allocate R_0 since it can be used safely here }
  1441. { (for holding date that's being copied) }
  1442. a_reg_alloc(list,NR_R0);
  1443. current_asmdata.getjumplabel(lab);
  1444. a_label(list, lab);
  1445. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1446. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1447. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1448. a_jmp(list,A_BC,C_NE,0,lab);
  1449. a_reg_dealloc(list,NR_R0);
  1450. len := len mod 4;
  1451. end;
  1452. count := len div 4;
  1453. if count > 0 then
  1454. { unrolled loop }
  1455. begin
  1456. a_reg_alloc(list,NR_R0);
  1457. for count2 := 1 to count do
  1458. begin
  1459. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1460. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1461. inc(src.offset,4);
  1462. inc(dst.offset,4);
  1463. end;
  1464. a_reg_dealloc(list,NR_R0);
  1465. len := len mod 4;
  1466. end;
  1467. {$endif use8byteconcatcopy}
  1468. { copy the leftovers }
  1469. if (len and 2) <> 0 then
  1470. begin
  1471. a_reg_alloc(list,NR_R0);
  1472. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1473. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1474. inc(src.offset,2);
  1475. inc(dst.offset,2);
  1476. a_reg_dealloc(list,NR_R0);
  1477. end;
  1478. if (len and 1) <> 0 then
  1479. begin
  1480. a_reg_alloc(list,NR_R0);
  1481. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1482. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1483. a_reg_dealloc(list,NR_R0);
  1484. end;
  1485. end;
  1486. {***************** This is private property, keep out! :) *****************}
  1487. function tcgppc.issimpleref(const ref: treference): boolean;
  1488. begin
  1489. if (ref.base = NR_NO) and
  1490. (ref.index <> NR_NO) then
  1491. internalerror(200208101);
  1492. result :=
  1493. not(assigned(ref.symbol)) and
  1494. (((ref.index = NR_NO) and
  1495. (ref.offset >= low(smallint)) and
  1496. (ref.offset <= high(smallint))) or
  1497. ((ref.index <> NR_NO) and
  1498. (ref.offset = 0)));
  1499. end;
  1500. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1501. { that's the case, we can use rlwinm to do an AND operation }
  1502. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1503. var
  1504. temp : longint;
  1505. testbit : aint;
  1506. compare: boolean;
  1507. begin
  1508. get_rlwi_const := false;
  1509. if (a = 0) or (a = -1) then
  1510. exit;
  1511. { start with the lowest bit }
  1512. testbit := 1;
  1513. { check its value }
  1514. compare := boolean(a and testbit);
  1515. { find out how long the run of bits with this value is }
  1516. { (it's impossible that all bits are 1 or 0, because in that case }
  1517. { this function wouldn't have been called) }
  1518. l1 := 31;
  1519. while (((a and testbit) <> 0) = compare) do
  1520. begin
  1521. testbit := testbit shl 1;
  1522. dec(l1);
  1523. end;
  1524. { check the length of the run of bits that comes next }
  1525. compare := not compare;
  1526. l2 := l1;
  1527. while (((a and testbit) <> 0) = compare) and
  1528. (l2 >= 0) do
  1529. begin
  1530. testbit := testbit shl 1;
  1531. dec(l2);
  1532. end;
  1533. { and finally the check whether the rest of the bits all have the }
  1534. { same value }
  1535. compare := not compare;
  1536. temp := l2;
  1537. if temp >= 0 then
  1538. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1539. exit;
  1540. { we have done "not(not(compare))", so compare is back to its }
  1541. { initial value. If the lowest bit was 0, a is of the form }
  1542. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1543. { because l2 now contains the position of the last zero of the }
  1544. { first run instead of that of the first 1) so switch l1 and l2 }
  1545. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1546. if not compare then
  1547. begin
  1548. temp := l1;
  1549. l1 := l2+1;
  1550. l2 := temp;
  1551. end
  1552. else
  1553. { otherwise, l1 currently contains the position of the last }
  1554. { zero instead of that of the first 1 of the second run -> +1 }
  1555. inc(l1);
  1556. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1557. l1 := l1 and 31;
  1558. l2 := l2 and 31;
  1559. get_rlwi_const := true;
  1560. end;
  1561. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1562. begin
  1563. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1564. end;
  1565. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1566. begin
  1567. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1568. end;
  1569. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1570. begin
  1571. case op of
  1572. OP_AND,OP_OR,OP_XOR:
  1573. begin
  1574. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1575. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1576. end;
  1577. OP_ADD:
  1578. begin
  1579. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1580. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1581. end;
  1582. OP_SUB:
  1583. begin
  1584. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1585. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1586. end;
  1587. else
  1588. internalerror(2002072801);
  1589. end;
  1590. end;
  1591. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1592. const
  1593. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1594. (A_SUBIC,A_SUBC,A_ADDME));
  1595. var
  1596. tmpreg: tregister;
  1597. tmpreg64: tregister64;
  1598. issub: boolean;
  1599. begin
  1600. case op of
  1601. OP_AND,OP_OR,OP_XOR:
  1602. begin
  1603. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1604. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1605. regdst.reghi);
  1606. end;
  1607. OP_ADD, OP_SUB:
  1608. begin
  1609. if (value < 0) and
  1610. (value <> low(value)) then
  1611. begin
  1612. if op = OP_ADD then
  1613. op := OP_SUB
  1614. else
  1615. op := OP_ADD;
  1616. value := -value;
  1617. end;
  1618. if (longint(value) <> 0) then
  1619. begin
  1620. issub := op = OP_SUB;
  1621. if (value > 0) and
  1622. (value-ord(issub) <= 32767) then
  1623. begin
  1624. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1625. regdst.reglo,regsrc.reglo,longint(value)));
  1626. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1627. regdst.reghi,regsrc.reghi));
  1628. end
  1629. else if ((value shr 32) = 0) then
  1630. begin
  1631. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1632. cg.a_load_const_reg(list,OS_32,aint(value),tmpreg);
  1633. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1634. regdst.reglo,regsrc.reglo,tmpreg));
  1635. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1636. regdst.reghi,regsrc.reghi));
  1637. end
  1638. else
  1639. begin
  1640. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1641. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1642. a_load64_const_reg(list,value,tmpreg64);
  1643. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1644. end
  1645. end
  1646. else
  1647. begin
  1648. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1649. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1650. regdst.reghi);
  1651. end;
  1652. end;
  1653. else
  1654. internalerror(2002072802);
  1655. end;
  1656. end;
  1657. begin
  1658. cg := tcgppc.create;
  1659. cg64 :=tcg64fppc.create;
  1660. end.