cgcpu.pas 69 KB

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