cgcpu.pas 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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. usesfpr,usesgpr : boolean;
  685. begin
  686. { CR and LR only have to be saved in case they are modified by the current }
  687. { procedure, but currently this isn't checked, so save them always }
  688. { following is the entry code as described in "Altivec Programming }
  689. { Interface Manual", bar the saving of AltiVec registers }
  690. a_reg_alloc(list,NR_STACK_POINTER_REG);
  691. usesgpr := false;
  692. usesfpr := false;
  693. firstregint := RS_NO;
  694. firstregfpu := RS_NO;
  695. if not(po_assembler in current_procinfo.procdef.procoptions) then
  696. begin
  697. { save link register? }
  698. if save_lr_in_prologue then
  699. begin
  700. a_reg_alloc(list,NR_R0);
  701. { save return address... }
  702. { warning: if this is no longer done via r0, or if r0 is }
  703. { added to the usable registers, adapt tcgppcgen.g_profilecode }
  704. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  705. { ... in caller's frame }
  706. case target_info.abi of
  707. abi_powerpc_aix,
  708. abi_powerpc_darwin:
  709. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX,ctempposinvalid,4,[]);
  710. abi_powerpc_sysv:
  711. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV,ctempposinvalid,4,[]);
  712. end;
  713. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  714. if not(cs_profile in current_settings.moduleswitches) then
  715. a_reg_dealloc(list,NR_R0);
  716. end;
  717. (*
  718. { save the CR if necessary in callers frame. }
  719. if target_info.abi in [abi_powerpc_aix,abi_powerpc_darwin] then
  720. if false then { Not needed at the moment. }
  721. begin
  722. a_reg_alloc(list,NR_R0);
  723. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  724. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX,ctempposinvalid,4,[]);
  725. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  726. a_reg_dealloc(list,NR_R0);
  727. end;
  728. *)
  729. firstregfpu := tcpuprocinfo(current_procinfo).get_first_save_fpu_reg;
  730. firstregint := tcpuprocinfo(current_procinfo).get_first_save_int_reg;
  731. usesgpr := firstregint <> 32;
  732. usesfpr := firstregfpu <> 32;
  733. if tcpuprocinfo(current_procinfo).needs_frame_pointer then
  734. list.concat(taicpu.op_reg_reg(A_MR,NR_OLD_STACK_POINTER_REG,NR_STACK_POINTER_REG));
  735. end;
  736. if usesfpr then
  737. begin
  738. reference_reset_base(href,NR_R1,-8,ctempposinvalid,8,[]);
  739. for regcounter:=firstregfpu to RS_F31 do
  740. begin
  741. a_loadfpu_reg_ref(list,OS_F64,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  742. dec(href.offset,8);
  743. end;
  744. { compute start of gpr save area }
  745. inc(href.offset,4);
  746. end
  747. else
  748. { compute start of gpr save area }
  749. reference_reset_base(href,NR_R1,-4,ctempposinvalid,4,[]);
  750. { save gprs and fetch GOT pointer }
  751. if usesgpr then
  752. begin
  753. if (firstregint <= RS_R22) or
  754. ((cs_opt_size in current_settings.optimizerswitches) and
  755. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  756. (firstregint <= RS_R29)) then
  757. begin
  758. { TODO: TODO: 64 bit support }
  759. dec(href.offset,(RS_R31-firstregint)*sizeof(pint));
  760. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  761. end
  762. else
  763. for regcounter:=firstregint to RS_R31 do
  764. begin
  765. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  766. dec(href.offset,4);
  767. end;
  768. end;
  769. { done in ncgutil because it may only be released after the parameters }
  770. { have been moved to their final resting place }
  771. { if (tcpuprocinfo(current_procinfo).needs_frame_pointer) then }
  772. { a_reg_dealloc(list,NR_R12); }
  773. if (not nostackframe) and
  774. tcpuprocinfo(current_procinfo).needstackframe and
  775. (localsize <> 0) then
  776. begin
  777. if (localsize <= high(smallint)) then
  778. begin
  779. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize,ctempposinvalid,8,[]);
  780. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  781. end
  782. else
  783. begin
  784. reference_reset_base(href,NR_STACK_POINTER_REG,0,ctempposinvalid,4,[]);
  785. { can't use getregisterint here, the register colouring }
  786. { is already done when we get here }
  787. { R12 may hold previous stack pointer, R11 may be in }
  788. { use as got => use R0 (but then we can't use }
  789. { a_load_const_reg) }
  790. href.index := NR_R0;
  791. a_reg_alloc(list,href.index);
  792. list.concat(taicpu.op_reg_const(A_LI,NR_R0,smallint((-localsize) and $ffff)));
  793. if (smallint((-localsize) and $ffff) < 0) then
  794. { upper 16 bits are now $ffff -> xor with inverse }
  795. list.concat(taicpu.op_reg_reg_const(A_XORIS,NR_R0,NR_R0,word(not(((-localsize) shr 16) and $ffff))))
  796. else
  797. list.concat(taicpu.op_reg_reg_const(A_ORIS,NR_R0,NR_R0,word(((-localsize) shr 16) and $ffff)));
  798. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  799. a_reg_dealloc(list,href.index);
  800. end;
  801. end;
  802. { save current RTOC for restoration after calls if necessary }
  803. if (pi_do_call in current_procinfo.flags) and
  804. (target_info.abi in abis_ppc_toc) then
  805. begin
  806. reference_reset_base(href,NR_STACK_POINTER_REG,get_rtoc_offset,ctempposinvalid,target_info.stackalign,[]);
  807. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_RTOC,href);
  808. end;
  809. { save the CR if/when we ever start using caller-save portions of that
  810. register}
  811. { a_reg_alloc(list,R_0);
  812. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  813. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  814. new_reference(STACK_POINTER_REG,LA_CR)));
  815. a_reg_dealloc(list,R_0);
  816. }
  817. { now comes the AltiVec context save, not yet implemented !!! }
  818. end;
  819. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  820. { This procedure may be called before, as well as after g_stackframe_entry }
  821. { is called. NOTE registers are not to be allocated through the register }
  822. { allocator here, because the register colouring has already occurred !! }
  823. var
  824. regcounter,firstregfpu,firstregint: TsuperRegister;
  825. href : treference;
  826. usesfpr,usesgpr,genret : boolean;
  827. localsize: tcgint;
  828. begin
  829. { AltiVec context restore, not yet implemented !!! }
  830. firstregint:=RS_NO;
  831. firstregfpu:=RS_NO;
  832. usesfpr:=false;
  833. usesgpr:=false;
  834. if not (po_assembler in current_procinfo.procdef.procoptions) then
  835. begin
  836. firstregfpu := tcpuprocinfo(current_procinfo).get_first_save_fpu_reg;
  837. firstregint := tcpuprocinfo(current_procinfo).get_first_save_int_reg;
  838. usesgpr := firstregint <> 32;
  839. usesfpr := firstregfpu <> 32;
  840. end;
  841. localsize:= tcpuprocinfo(current_procinfo).calc_stackframe_size;
  842. { adjust r1 }
  843. { (register allocator is no longer valid at this time and an add of 0 }
  844. { is translated into a move, which is then registered with the register }
  845. { allocator, causing a crash }
  846. if (not nostackframe) and
  847. tcpuprocinfo(current_procinfo).needstackframe and
  848. (localsize <> 0) then
  849. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  850. { no return (blr) generated yet }
  851. genret:=true;
  852. if usesfpr then
  853. begin
  854. reference_reset_base(href,NR_R1,-8,ctempposinvalid,8,[]);
  855. for regcounter := firstregfpu to RS_F31 do
  856. begin
  857. a_loadfpu_ref_reg(list,OS_F64,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  858. dec(href.offset,8);
  859. end;
  860. inc(href.offset,4);
  861. end
  862. else
  863. reference_reset_base(href,NR_R1,-4,ctempposinvalid,4,[]);
  864. if (usesgpr) then
  865. begin
  866. if (firstregint <= RS_R22) or
  867. ((cs_opt_size in current_settings.optimizerswitches) and
  868. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  869. (firstregint <= RS_R29)) then
  870. begin
  871. { TODO: TODO: 64 bit support }
  872. dec(href.offset,(RS_R31-firstregint)*sizeof(pint));
  873. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  874. end
  875. else
  876. for regcounter:=firstregint to RS_R31 do
  877. begin
  878. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  879. dec(href.offset,4);
  880. end;
  881. end;
  882. (*
  883. { restore fprs and return }
  884. if usesfpr then
  885. begin
  886. { address of fpr save area to r11 }
  887. r:=NR_R12;
  888. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  889. {
  890. if (pi_do_call in current_procinfo.flags) then
  891. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x',AT_FUNCTION))
  892. else
  893. { leaf node => lr haven't to be restored }
  894. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  895. genret:=false;
  896. }
  897. end;
  898. *)
  899. { if we didn't generate the return code, we've to do it now }
  900. if genret then
  901. begin
  902. { load link register? }
  903. if not (po_assembler in current_procinfo.procdef.procoptions) then
  904. begin
  905. if (pi_do_call in current_procinfo.flags) then
  906. begin
  907. case target_info.abi of
  908. abi_powerpc_aix,
  909. abi_powerpc_darwin:
  910. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX,ctempposinvalid,4,[]);
  911. abi_powerpc_sysv:
  912. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV,ctempposinvalid,4,[]);
  913. end;
  914. a_reg_alloc(list,NR_R0);
  915. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  916. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  917. a_reg_dealloc(list,NR_R0);
  918. end;
  919. (*
  920. { restore the CR if necessary from callers frame}
  921. if target_info.abi in [abi_powerpc_aix,abi_powerpc_darwin] then
  922. if false then { Not needed at the moment. }
  923. begin
  924. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX,ctempposinvalid,4,[]);
  925. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  926. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  927. a_reg_dealloc(list,NR_R0);
  928. end;
  929. *)
  930. end;
  931. list.concat(taicpu.op_none(A_BLR));
  932. end;
  933. end;
  934. function tcgppc.save_regs(list : TAsmList):longint;
  935. {Generates code which saves used non-volatile registers in
  936. the save area right below the address the stackpointer point to.
  937. Returns the actual used save area size.}
  938. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  939. usesfpr,usesgpr: boolean;
  940. href : treference;
  941. offset: tcgint;
  942. regcounter2, firstfpureg: Tsuperregister;
  943. begin
  944. usesfpr:=false;
  945. firstreggpr:=RS_NO;
  946. firstregfpu:=RS_NO;
  947. if not (po_assembler in current_procinfo.procdef.procoptions) then
  948. begin
  949. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  950. case target_info.abi of
  951. abi_powerpc_aix,
  952. abi_powerpc_darwin:
  953. firstfpureg := RS_F14;
  954. abi_powerpc_sysv:
  955. firstfpureg := RS_F9;
  956. else
  957. internalerror(2003122903);
  958. end;
  959. for regcounter:=firstfpureg to RS_F31 do
  960. begin
  961. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  962. begin
  963. usesfpr:=true;
  964. firstregfpu:=regcounter;
  965. break;
  966. end;
  967. end;
  968. end;
  969. usesgpr:=false;
  970. if not (po_assembler in current_procinfo.procdef.procoptions) then
  971. for regcounter2:=RS_R13 to RS_R31 do
  972. begin
  973. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  974. begin
  975. usesgpr:=true;
  976. firstreggpr:=regcounter2;
  977. break;
  978. end;
  979. end;
  980. offset:= 0;
  981. { save floating-point registers }
  982. if usesfpr then
  983. for regcounter := firstregfpu to RS_F31 do
  984. begin
  985. offset:= offset - 8;
  986. reference_reset_base(href, NR_STACK_POINTER_REG, offset, ctempposinvalid, 8, []);
  987. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  988. end;
  989. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  990. { save gprs in gpr save area }
  991. if usesgpr then
  992. if firstreggpr < RS_R30 then
  993. begin
  994. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  995. reference_reset_base(href,NR_STACK_POINTER_REG,offset,ctempposinvalid,4,[]);
  996. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  997. {STMW stores multiple registers}
  998. end
  999. else
  1000. begin
  1001. for regcounter := firstreggpr to RS_R31 do
  1002. begin
  1003. offset:= offset - 4;
  1004. reference_reset_base(href, NR_STACK_POINTER_REG, offset, ctempposinvalid, 4, []);
  1005. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1006. end;
  1007. end;
  1008. { now comes the AltiVec context save, not yet implemented !!! }
  1009. save_regs:= -offset;
  1010. end;
  1011. procedure tcgppc.restore_regs(list : TAsmList);
  1012. {Generates code which restores used non-volatile registers from
  1013. the save area right below the address the stackpointer point to.}
  1014. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1015. usesfpr,usesgpr: boolean;
  1016. href : treference;
  1017. offset: integer;
  1018. regcounter2, firstfpureg: Tsuperregister;
  1019. begin
  1020. usesfpr:=false;
  1021. firstreggpr:=RS_NO;
  1022. firstregfpu:=RS_NO;
  1023. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1024. begin
  1025. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1026. case target_info.abi of
  1027. abi_powerpc_aix,
  1028. abi_powerpc_darwin:
  1029. firstfpureg := RS_F14;
  1030. abi_powerpc_sysv:
  1031. firstfpureg := RS_F9;
  1032. else
  1033. internalerror(2003122903);
  1034. end;
  1035. for regcounter:=firstfpureg to RS_F31 do
  1036. begin
  1037. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1038. begin
  1039. usesfpr:=true;
  1040. firstregfpu:=regcounter;
  1041. break;
  1042. end;
  1043. end;
  1044. end;
  1045. usesgpr:=false;
  1046. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1047. for regcounter2:=RS_R13 to RS_R31 do
  1048. begin
  1049. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1050. begin
  1051. usesgpr:=true;
  1052. firstreggpr:=regcounter2;
  1053. break;
  1054. end;
  1055. end;
  1056. offset:= 0;
  1057. { restore fp registers }
  1058. if usesfpr then
  1059. for regcounter := firstregfpu to RS_F31 do
  1060. begin
  1061. offset:= offset - 8;
  1062. reference_reset_base(href, NR_STACK_POINTER_REG, offset, ctempposinvalid, 8, []);
  1063. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1064. end;
  1065. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1066. { restore gprs }
  1067. if usesgpr then
  1068. if firstreggpr < RS_R30 then
  1069. begin
  1070. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1071. reference_reset_base(href,NR_STACK_POINTER_REG,offset,ctempposinvalid,4,[]); //-220
  1072. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1073. {LMW loads multiple registers}
  1074. end
  1075. else
  1076. begin
  1077. for regcounter := firstreggpr to RS_R31 do
  1078. begin
  1079. offset:= offset - 4;
  1080. reference_reset_base(href, NR_STACK_POINTER_REG, offset, ctempposinvalid, 4, []);
  1081. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1082. end;
  1083. end;
  1084. { now comes the AltiVec context restore, not yet implemented !!! }
  1085. end;
  1086. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1087. (* NOT IN USE *)
  1088. { generated the entry code of a procedure/function. Note: localsize is the }
  1089. { sum of the size necessary for local variables and the maximum possible }
  1090. { combined size of ALL the parameters of a procedure called by the current }
  1091. { one }
  1092. const
  1093. macosLinkageAreaSize = 24;
  1094. var
  1095. href : treference;
  1096. registerSaveAreaSize : longint;
  1097. begin
  1098. if (localsize mod 8) <> 0 then
  1099. internalerror(58991);
  1100. { CR and LR only have to be saved in case they are modified by the current }
  1101. { procedure, but currently this isn't checked, so save them always }
  1102. { following is the entry code as described in "Altivec Programming }
  1103. { Interface Manual", bar the saving of AltiVec registers }
  1104. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1105. a_reg_alloc(list,NR_R0);
  1106. { save return address in callers frame}
  1107. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1108. { ... in caller's frame }
  1109. reference_reset_base(href,NR_STACK_POINTER_REG,8,ctempposinvalid,8,[]);
  1110. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1111. a_reg_dealloc(list,NR_R0);
  1112. { save non-volatile registers in callers frame}
  1113. registerSaveAreaSize:= save_regs(list);
  1114. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1115. a_reg_alloc(list,NR_R0);
  1116. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1117. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX,ctempposinvalid,4,[]);
  1118. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1119. a_reg_dealloc(list,NR_R0);
  1120. (*
  1121. { save pointer to incoming arguments }
  1122. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1123. *)
  1124. (*
  1125. a_reg_alloc(list,R_12);
  1126. { 0 or 8 based on SP alignment }
  1127. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1128. R_12,STACK_POINTER_REG,0,28,28));
  1129. { add in stack length }
  1130. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1131. -localsize));
  1132. { establish new alignment }
  1133. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1134. a_reg_dealloc(list,R_12);
  1135. *)
  1136. { allocate stack frame }
  1137. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1138. inc(localsize,tg.lasttemp);
  1139. localsize:=align(localsize,16);
  1140. //tcpuprocinfo(current_procinfo).localsize:=localsize;
  1141. if (localsize <> 0) then
  1142. begin
  1143. if (localsize <= high(smallint)) then
  1144. begin
  1145. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize,ctempposinvalid,8,[]);
  1146. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1147. end
  1148. else
  1149. begin
  1150. reference_reset_base(href,NR_STACK_POINTER_REG,0,ctempposinvalid,8,[]);
  1151. href.index := NR_R11;
  1152. a_reg_alloc(list,href.index);
  1153. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1154. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1155. a_reg_dealloc(list,href.index);
  1156. end;
  1157. end;
  1158. { save current RTOC for restoration after calls if necessary }
  1159. if pi_do_call in current_procinfo.flags then
  1160. begin
  1161. reference_reset_base(href,NR_STACK_POINTER_REG,get_rtoc_offset,ctempposinvalid,target_info.stackalign,[]);
  1162. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_RTOC,href);
  1163. end;
  1164. end;
  1165. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : tcgint);
  1166. (* NOT IN USE *)
  1167. var
  1168. href : treference;
  1169. begin
  1170. a_reg_alloc(list,NR_R0);
  1171. { restore stack pointer }
  1172. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP,ctempposinvalid,4,[]);
  1173. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1174. (*
  1175. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1176. *)
  1177. { restore the CR if necessary from callers frame
  1178. ( !!! always done currently ) }
  1179. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX,ctempposinvalid,4,[]);
  1180. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1181. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1182. a_reg_dealloc(list,NR_R0);
  1183. (*
  1184. { restore return address from callers frame }
  1185. reference_reset_base(href,STACK_POINTER_REG,8,ctempposinvalid,8,[]);
  1186. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1187. *)
  1188. { restore non-volatile registers from callers frame }
  1189. restore_regs(list);
  1190. (*
  1191. { return to caller }
  1192. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1193. list.concat(taicpu.op_none(A_BLR));
  1194. *)
  1195. { restore return address from callers frame }
  1196. reference_reset_base(href,NR_STACK_POINTER_REG,8,ctempposinvalid,8,[]);
  1197. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1198. { return to caller }
  1199. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1200. list.concat(taicpu.op_none(A_BLR));
  1201. end;
  1202. { ************* concatcopy ************ }
  1203. {$ifdef use8byteconcatcopy}
  1204. const
  1205. maxmoveunit = 8;
  1206. {$else use8byteconcatcopy}
  1207. const
  1208. maxmoveunit = 4;
  1209. {$endif use8byteconcatcopy}
  1210. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1211. var
  1212. countreg: TRegister;
  1213. src, dst: TReference;
  1214. lab: tasmlabel;
  1215. count, count2: aint;
  1216. size: tcgsize;
  1217. copyreg: tregister;
  1218. begin
  1219. {$ifdef extdebug}
  1220. if len > high(longint) then
  1221. internalerror(2002072704);
  1222. {$endif extdebug}
  1223. if (references_equal(source,dest)) then
  1224. exit;
  1225. { make sure short loads are handled as optimally as possible }
  1226. if (len <= maxmoveunit) and
  1227. (byte(len) in [1,2,4,8]) then
  1228. begin
  1229. if len < 8 then
  1230. begin
  1231. size := int_cgsize(len);
  1232. a_load_ref_ref(list,size,size,source,dest);
  1233. end
  1234. else
  1235. begin
  1236. copyreg := getfpuregister(list,OS_F64);
  1237. a_loadfpu_ref_reg(list,OS_F64,OS_F64,source,copyreg);
  1238. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dest);
  1239. end;
  1240. exit;
  1241. end;
  1242. count := len div maxmoveunit;
  1243. reference_reset(src,source.alignment,source.volatility);
  1244. reference_reset(dst,dest.alignment,dest.volatility);
  1245. { load the address of source into src.base }
  1246. if (count > 4) or
  1247. not issimpleref(source) or
  1248. ((source.index <> NR_NO) and
  1249. ((source.offset + longint(len)) > high(smallint))) then
  1250. begin
  1251. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1252. a_loadaddr_ref_reg(list,source,src.base);
  1253. end
  1254. else
  1255. begin
  1256. src := source;
  1257. end;
  1258. { load the address of dest into dst.base }
  1259. if (count > 4) or
  1260. not issimpleref(dest) or
  1261. ((dest.index <> NR_NO) and
  1262. ((dest.offset + longint(len)) > high(smallint))) then
  1263. begin
  1264. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1265. a_loadaddr_ref_reg(list,dest,dst.base);
  1266. end
  1267. else
  1268. begin
  1269. dst := dest;
  1270. end;
  1271. {$ifdef use8byteconcatcopy}
  1272. if count > 4 then
  1273. { generate a loop }
  1274. begin
  1275. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1276. { have to be set to 8. I put an Inc there so debugging may be }
  1277. { easier (should offset be different from zero here, it will be }
  1278. { easy to notice in the generated assembler }
  1279. inc(dst.offset,8);
  1280. inc(src.offset,8);
  1281. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1282. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1283. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1284. a_load_const_reg(list,OS_32,count,countreg);
  1285. copyreg := getfpuregister(list,OS_F64);
  1286. a_reg_sync(list,copyreg);
  1287. current_asmdata.getjumplabel(lab);
  1288. a_label(list, lab);
  1289. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1290. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1291. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1292. a_jmp(list,A_BC,C_NE,0,lab);
  1293. a_reg_sync(list,copyreg);
  1294. len := len mod 8;
  1295. end;
  1296. count := len div 8;
  1297. if count > 0 then
  1298. { unrolled loop }
  1299. begin
  1300. copyreg := getfpuregister(list,OS_F64);
  1301. for count2 := 1 to count do
  1302. begin
  1303. a_loadfpu_ref_reg(list,OS_F64,OS_F64,src,copyreg);
  1304. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dst);
  1305. inc(src.offset,8);
  1306. inc(dst.offset,8);
  1307. end;
  1308. len := len mod 8;
  1309. end;
  1310. if (len and 4) <> 0 then
  1311. begin
  1312. a_reg_alloc(list,NR_R0);
  1313. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1314. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1315. inc(src.offset,4);
  1316. inc(dst.offset,4);
  1317. a_reg_dealloc(list,NR_R0);
  1318. end;
  1319. {$else use8byteconcatcopy}
  1320. if count > 4 then
  1321. { generate a loop }
  1322. begin
  1323. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1324. { have to be set to 4. I put an Inc there so debugging may be }
  1325. { easier (should offset be different from zero here, it will be }
  1326. { easy to notice in the generated assembler }
  1327. inc(dst.offset,4);
  1328. inc(src.offset,4);
  1329. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1330. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1331. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1332. a_load_const_reg(list,OS_32,count,countreg);
  1333. { explicitely allocate R_0 since it can be used safely here }
  1334. { (for holding date that's being copied) }
  1335. a_reg_alloc(list,NR_R0);
  1336. current_asmdata.getjumplabel(lab);
  1337. a_label(list, lab);
  1338. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1339. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1340. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1341. a_jmp(list,A_BC,C_NE,0,lab);
  1342. a_reg_dealloc(list,NR_R0);
  1343. len := len mod 4;
  1344. end;
  1345. count := len div 4;
  1346. if count > 0 then
  1347. { unrolled loop }
  1348. begin
  1349. a_reg_alloc(list,NR_R0);
  1350. for count2 := 1 to count do
  1351. begin
  1352. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1353. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1354. inc(src.offset,4);
  1355. inc(dst.offset,4);
  1356. end;
  1357. a_reg_dealloc(list,NR_R0);
  1358. len := len mod 4;
  1359. end;
  1360. {$endif use8byteconcatcopy}
  1361. { copy the leftovers }
  1362. if (len and 2) <> 0 then
  1363. begin
  1364. a_reg_alloc(list,NR_R0);
  1365. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1366. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1367. inc(src.offset,2);
  1368. inc(dst.offset,2);
  1369. a_reg_dealloc(list,NR_R0);
  1370. end;
  1371. if (len and 1) <> 0 then
  1372. begin
  1373. a_reg_alloc(list,NR_R0);
  1374. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1375. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1376. a_reg_dealloc(list,NR_R0);
  1377. end;
  1378. end;
  1379. {***************** This is private property, keep out! :) *****************}
  1380. function tcgppc.issimpleref(const ref: treference): boolean;
  1381. begin
  1382. if (ref.base = NR_NO) and
  1383. (ref.index <> NR_NO) then
  1384. internalerror(200208101);
  1385. result :=
  1386. not(assigned(ref.symbol)) and
  1387. (((ref.index = NR_NO) and
  1388. (ref.offset >= low(smallint)) and
  1389. (ref.offset <= high(smallint))) or
  1390. ((ref.index <> NR_NO) and
  1391. (ref.offset = 0)));
  1392. end;
  1393. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1394. { that's the case, we can use rlwinm to do an AND operation }
  1395. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1396. var
  1397. temp : longint;
  1398. testbit : aint;
  1399. compare: boolean;
  1400. begin
  1401. get_rlwi_const := false;
  1402. if (a = 0) or (a = -1) then
  1403. exit;
  1404. { start with the lowest bit }
  1405. testbit := 1;
  1406. { check its value }
  1407. compare := boolean(a and testbit);
  1408. { find out how long the run of bits with this value is }
  1409. { (it's impossible that all bits are 1 or 0, because in that case }
  1410. { this function wouldn't have been called) }
  1411. l1 := 31;
  1412. while (((a and testbit) <> 0) = compare) do
  1413. begin
  1414. testbit := testbit shl 1;
  1415. dec(l1);
  1416. end;
  1417. { check the length of the run of bits that comes next }
  1418. compare := not compare;
  1419. l2 := l1;
  1420. while (((a and testbit) <> 0) = compare) and
  1421. (l2 >= 0) do
  1422. begin
  1423. testbit := testbit shl 1;
  1424. dec(l2);
  1425. end;
  1426. { and finally the check whether the rest of the bits all have the }
  1427. { same value }
  1428. compare := not compare;
  1429. temp := l2;
  1430. if temp >= 0 then
  1431. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1432. exit;
  1433. { we have done "not(not(compare))", so compare is back to its }
  1434. { initial value. If the lowest bit was 0, a is of the form }
  1435. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1436. { because l2 now contains the position of the last zero of the }
  1437. { first run instead of that of the first 1) so switch l1 and l2 }
  1438. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1439. if not compare then
  1440. begin
  1441. temp := l1;
  1442. l1 := l2+1;
  1443. l2 := temp;
  1444. end
  1445. else
  1446. { otherwise, l1 currently contains the position of the last }
  1447. { zero instead of that of the first 1 of the second run -> +1 }
  1448. inc(l1);
  1449. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1450. l1 := l1 and 31;
  1451. l2 := l2 and 31;
  1452. get_rlwi_const := true;
  1453. end;
  1454. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1455. begin
  1456. case op of
  1457. OP_NOT:
  1458. begin
  1459. cg.a_op_reg_reg(list,OP_NOT,OS_32,regsrc.reglo,regdst.reglo);
  1460. cg.a_op_reg_reg(list,OP_NOT,OS_32,regsrc.reghi,regdst.reghi);
  1461. end;
  1462. OP_NEG:
  1463. begin
  1464. list.concat(taicpu.op_reg_reg_const(a_subfic,regdst.reglo,regsrc.reglo,0));
  1465. list.concat(taicpu.op_reg_reg(a_subfze,regdst.reghi,regsrc.reghi));
  1466. end;
  1467. else
  1468. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1469. end;
  1470. end;
  1471. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1472. begin
  1473. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1474. end;
  1475. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1476. begin
  1477. case op of
  1478. OP_AND,OP_OR,OP_XOR:
  1479. begin
  1480. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1481. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1482. end;
  1483. OP_ADD:
  1484. begin
  1485. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1486. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1487. end;
  1488. OP_SUB:
  1489. begin
  1490. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1491. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1492. end;
  1493. else
  1494. internalerror(2002072801);
  1495. end;
  1496. end;
  1497. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1498. const
  1499. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1500. (A_SUBIC,A_SUBC,A_ADDME));
  1501. var
  1502. tmpreg: tregister;
  1503. tmpreg64: tregister64;
  1504. issub: boolean;
  1505. begin
  1506. case op of
  1507. OP_AND,OP_OR,OP_XOR:
  1508. begin
  1509. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1510. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1511. regdst.reghi);
  1512. end;
  1513. OP_ADD, OP_SUB:
  1514. begin
  1515. if (value < 0) and
  1516. (value <> low(value)) then
  1517. begin
  1518. if op = OP_ADD then
  1519. op := OP_SUB
  1520. else
  1521. op := OP_ADD;
  1522. value := -value;
  1523. end;
  1524. if (longint(value) <> 0) then
  1525. begin
  1526. issub := op = OP_SUB;
  1527. if (value > 0) and
  1528. (value-ord(issub) <= 32767) then
  1529. begin
  1530. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1531. regdst.reglo,regsrc.reglo,longint(value)));
  1532. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1533. regdst.reghi,regsrc.reghi));
  1534. end
  1535. else if ((value shr 32) = 0) then
  1536. begin
  1537. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1538. cg.a_load_const_reg(list,OS_32,aint(value),tmpreg);
  1539. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1540. regdst.reglo,regsrc.reglo,tmpreg));
  1541. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1542. regdst.reghi,regsrc.reghi));
  1543. end
  1544. else
  1545. begin
  1546. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1547. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1548. a_load64_const_reg(list,value,tmpreg64);
  1549. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1550. end
  1551. end
  1552. else
  1553. begin
  1554. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1555. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1556. regdst.reghi);
  1557. end;
  1558. end;
  1559. else
  1560. internalerror(2002072802);
  1561. end;
  1562. end;
  1563. procedure create_codegen;
  1564. begin
  1565. cg := tcgppc.create;
  1566. cg64 :=tcg64fppc.create;
  1567. end;
  1568. end.