cgcpu.pas 69 KB

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