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