cgcpu.pas 69 KB

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