cgcpu.pas 71 KB

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