cgcpu.pas 72 KB

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