cgcpu.pas 66 KB

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