cgcpu.pas 73 KB

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