cgcpu.pas 69 KB

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