cgcpu.pas 68 KB

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