cgcpu.pas 68 KB

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