cgcpu.pas 69 KB

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