cgcpu.pas 66 KB

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