cgcpu.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,cpuinfo,node,cg64f32,cginfo;
  25. type
  26. tcgppc = class(tcg)
  27. { passing parameters, per default the parameter is pushed }
  28. { nr gives the number of the parameter (enumerated from }
  29. { left to right), this allows to move the parameter to }
  30. { register, if the cpu supports register calling }
  31. { conventions }
  32. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  33. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  34. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  35. procedure a_call_name(list : taasmoutput;const s : string);override;
  36. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  37. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  38. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  39. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  40. size: tcgsize; a: aword; src, dst: tregister); override;
  41. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  42. size: tcgsize; src1, src2, dst: tregister); override;
  43. { move instructions }
  44. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  45. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  46. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  47. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);override;
  48. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister); override;
  49. { fpu move instructions }
  50. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  51. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  52. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  53. { comparison operations }
  54. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  55. l : tasmlabel);override;
  56. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  57. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  58. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  59. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  60. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  61. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  62. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  63. procedure g_restore_frame_pointer(list : taasmoutput);override;
  64. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  65. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  66. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  67. procedure g_overflowcheck(list: taasmoutput; const p: tnode); override;
  68. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  69. { that's the case, we can use rlwinm to do an AND operation }
  70. function get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  71. procedure g_save_standard_registers(list : taasmoutput);override;
  72. procedure g_restore_standard_registers(list : taasmoutput);override;
  73. procedure g_save_all_registers(list : taasmoutput);override;
  74. procedure g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);override;
  75. private
  76. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  77. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  78. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  79. { Make sure ref is a valid reference for the PowerPC and sets the }
  80. { base to the value of the index if (base = R_NO). }
  81. procedure fixref(list: taasmoutput; var ref: treference);
  82. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  83. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  84. ref: treference);
  85. { creates the correct branch instruction for a given combination }
  86. { of asmcondflags and destination addressing mode }
  87. procedure a_jmp(list: taasmoutput; op: tasmop;
  88. c: tasmcondflag; crval: longint; l: tasmlabel);
  89. end;
  90. tcg64fppc = class(tcg64f32)
  91. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  92. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  93. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  94. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  95. end;
  96. const
  97. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  98. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  99. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  100. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  101. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  102. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  103. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  104. C_LT,C_GE,C_LE,C_NE,C_LE,C_NG,C_GE,C_NL);
  105. implementation
  106. uses
  107. globtype,globals,verbose,systems,cutils,symconst,symdef,rgobj;
  108. { parameter passing... Still needs extra support from the processor }
  109. { independent code generator }
  110. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  111. var
  112. ref: treference;
  113. begin
  114. case locpara.loc of
  115. LOC_REGISTER:
  116. a_load_const_reg(list,size,a,locpara.register);
  117. LOC_REFERENCE:
  118. begin
  119. reference_reset(ref);
  120. ref.base:=locpara.reference.index;
  121. ref.offset:=locpara.reference.offset;
  122. a_load_const_ref(list,size,a,ref);
  123. end;
  124. else
  125. internalerror(2002081101);
  126. end;
  127. if locpara.sp_fixup<>0 then
  128. internalerror(2002081102);
  129. end;
  130. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  131. var
  132. ref: treference;
  133. tmpreg: tregister;
  134. begin
  135. case locpara.loc of
  136. LOC_REGISTER:
  137. a_load_ref_reg(list,size,r,locpara.register);
  138. LOC_REFERENCE:
  139. begin
  140. reference_reset(ref);
  141. ref.base:=locpara.reference.index;
  142. ref.offset:=locpara.reference.offset;
  143. tmpreg := get_scratch_reg_int(list);
  144. a_load_ref_reg(list,size,r,tmpreg);
  145. a_load_reg_ref(list,size,tmpreg,ref);
  146. free_scratch_reg(list,tmpreg);
  147. end;
  148. LOC_FPUREGISTER:
  149. case size of
  150. OS_32:
  151. a_loadfpu_ref_reg(list,OS_F32,r,locpara.register);
  152. OS_64:
  153. a_loadfpu_ref_reg(list,OS_F64,r,locpara.register);
  154. else
  155. internalerror(2002072801);
  156. end;
  157. else
  158. internalerror(2002081103);
  159. end;
  160. if locpara.sp_fixup<>0 then
  161. internalerror(2002081104);
  162. end;
  163. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  164. var
  165. ref: treference;
  166. tmpreg: tregister;
  167. begin
  168. {$ifdef para_sizes_known}
  169. if (nr <= max_param_regs_int) then
  170. a_loadaddr_ref_reg(list,size,r,param_regs_int[nr])
  171. else
  172. begin
  173. reset_reference(ref);
  174. ref.base := STACK_POINTER_REG;
  175. ref.offset := LinkageAreaSize+para_size_till_now;
  176. tmpreg := get_scratch_reg_address(list);
  177. a_loadaddr_ref_reg(list,size,r,tmpreg);
  178. a_load_reg_ref(list,size,tmpreg,ref);
  179. free_scratch_reg(list,tmpreg);
  180. end;
  181. {$endif para_sizes_known}
  182. end;
  183. { calling a code fragment by name }
  184. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  185. var
  186. href : treference;
  187. begin
  188. { save our RTOC register value. Only necessary when doing pointer based }
  189. { calls or cross TOC calls, but currently done always }
  190. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  191. list.concat(taicpu.op_reg_ref(A_STW,R_TOC,href));
  192. list.concat(taicpu.op_sym(A_BL,newasmsymbol(s)));
  193. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  194. list.concat(taicpu.op_reg_ref(A_LWZ,R_TOC,href));
  195. end;
  196. { calling a code fragment through a reference }
  197. procedure tcgppc.a_call_ref(list : taasmoutput;const ref : treference);
  198. begin
  199. {$warning FIX ME}
  200. end;
  201. {********************** load instructions ********************}
  202. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  203. begin
  204. if (longint(a) >= low(smallint)) and
  205. (longint(a) <= high(smallint)) then
  206. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  207. else if ((a and $ffff) <> 0) then
  208. begin
  209. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  210. if ((a shr 16) <> 0) then
  211. list.concat(taicpu.op_reg_const(A_ADDIS,reg,
  212. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  213. end
  214. else
  215. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  216. end;
  217. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  218. const
  219. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  220. { indexed? updating?}
  221. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  222. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  223. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  224. var
  225. op: TAsmOp;
  226. ref2: TReference;
  227. begin
  228. ref2 := ref;
  229. FixRef(list,ref2);
  230. if size in [OS_S8..OS_S16] then
  231. { storing is the same for signed and unsigned values }
  232. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  233. { 64 bit stuff should be handled separately }
  234. if size in [OS_64,OS_S64] then
  235. internalerror(200109236);
  236. op := storeinstr[tcgsize2unsigned[size],ref2.index<>R_NO,false];
  237. a_load_store(list,op,reg,ref2);
  238. End;
  239. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  240. const
  241. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  242. { indexed? updating?}
  243. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  244. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  245. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  246. { 64bit stuff should be handled separately }
  247. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  248. { there's no load-byte-with-sign-extend :( }
  249. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  250. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  251. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  252. var
  253. op: tasmop;
  254. tmpreg: tregister;
  255. ref2, tmpref: treference;
  256. begin
  257. ref2 := ref;
  258. fixref(list,ref2);
  259. op := loadinstr[size,ref2.index<>R_NO,false];
  260. a_load_store(list,op,reg,ref2);
  261. { sign extend shortint if necessary, since there is no }
  262. { load instruction that does that automatically (JM) }
  263. if size = OS_S8 then
  264. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  265. end;
  266. procedure tcgppc.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  267. begin
  268. if (reg1 <> reg2) or
  269. not(size in [OS_32,OS_S32]) then
  270. begin
  271. case size of
  272. OS_8:
  273. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  274. reg2,reg1,0,31-8+1,31));
  275. OS_S8:
  276. list.concat(taicpu.op_reg_reg(A_EXTSB,reg2,reg1));
  277. OS_16:
  278. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  279. reg2,reg1,0,31-16+1,31));
  280. OS_S16:
  281. list.concat(taicpu.op_reg_reg(A_EXTSH,reg2,reg1));
  282. OS_32,OS_S32:
  283. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  284. end;
  285. end;
  286. end;
  287. procedure tcgppc.a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);
  288. begin
  289. { can't use op_sym_ofs_reg because sym+ofs can be > 32767!! }
  290. internalerror(200112293);
  291. end;
  292. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  293. begin
  294. list.concat(taicpu.op_reg_reg(A_FMR,reg1,reg2));
  295. end;
  296. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  297. const
  298. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  299. { indexed? updating?}
  300. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  301. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  302. var
  303. op: tasmop;
  304. ref2: treference;
  305. begin
  306. { several functions call this procedure with OS_32 or OS_64 }
  307. { so this makes life easier (FK) }
  308. case size of
  309. OS_32,OS_F32:
  310. size:=OS_F32;
  311. OS_64,OS_F64:
  312. size:=OS_F64;
  313. else
  314. internalerror(200201121);
  315. end;
  316. ref2 := ref;
  317. fixref(list,ref2);
  318. op := fpuloadinstr[size,ref2.index <> R_NO,false];
  319. a_load_store(list,op,reg,ref2);
  320. end;
  321. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  322. const
  323. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  324. { indexed? updating?}
  325. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  326. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  327. var
  328. op: tasmop;
  329. ref2: treference;
  330. begin
  331. if not(size in [OS_F32,OS_F64]) then
  332. internalerror(200201122);
  333. ref2 := ref;
  334. fixref(list,ref2);
  335. op := fpustoreinstr[size,ref2.index <> R_NO,false];
  336. a_load_store(list,op,reg,ref2);
  337. end;
  338. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  339. var
  340. scratch_register: TRegister;
  341. begin
  342. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  343. end;
  344. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  345. begin
  346. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  347. end;
  348. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  349. size: tcgsize; a: aword; src, dst: tregister);
  350. var
  351. l1,l2: longint;
  352. oplo, ophi: tasmop;
  353. scratchreg: tregister;
  354. useReg, gotrlwi: boolean;
  355. procedure do_lo_hi;
  356. begin
  357. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  358. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  359. end;
  360. begin
  361. if op = OP_SUB then
  362. begin
  363. {$ifopt q+}
  364. {$q-}
  365. {$define overflowon}
  366. {$endif}
  367. a_op_const_reg_reg(list,OP_ADD,size,aword(-a),src,dst);
  368. {$ifdef overflowon}
  369. {$q+}
  370. {$undef overflowon}
  371. {$endif}
  372. exit;
  373. end;
  374. ophi := TOpCG2AsmOpConstHi[op];
  375. oplo := TOpCG2AsmOpConstLo[op];
  376. gotrlwi := get_rlwi_const(a,l1,l2);
  377. if (op in [OP_ADD,OP_AND,OP_OR,OP_XOR]) then
  378. begin
  379. if (a = 0) then
  380. begin
  381. if op = OP_AND then
  382. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  383. exit;
  384. end
  385. else if (a = high(aword)) and
  386. (op in [OP_AND,OP_OR,OP_XOR]) then
  387. begin
  388. case op of
  389. OP_OR:
  390. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  391. OP_XOR:
  392. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  393. end;
  394. exit;
  395. end
  396. else if (longint(a) >= 0) and
  397. (longint(a) <= high(word)) and
  398. ((op <> OP_AND) or
  399. not gotrlwi) then
  400. begin
  401. if (op = OP_ADD) then
  402. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)))
  403. else
  404. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  405. exit;
  406. end;
  407. { all basic constant instructions also have a shifted form that }
  408. { works only on the highest 16bits, so if lo(a) is 0, we can }
  409. { use that one }
  410. if (word(a) = 0) and
  411. (not(op = OP_AND) or
  412. not gotrlwi) then
  413. begin
  414. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  415. exit;
  416. end;
  417. end;
  418. { otherwise, the instructions we can generate depend on the }
  419. { operation }
  420. useReg := false;
  421. case op of
  422. OP_DIV,OP_IDIV:
  423. useReg := true;
  424. OP_IMUL, OP_MUL:
  425. if (longint(a) >= low(smallint)) and
  426. (longint(a) <= high(smallint)) then
  427. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  428. else
  429. usereg := true;
  430. OP_ADD:
  431. begin
  432. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  433. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  434. smallint((a shr 16) + ord(smallint(a) < 0))));
  435. end;
  436. OP_OR:
  437. { try to use rlwimi }
  438. if gotrlwi and
  439. (src = dst) then
  440. begin
  441. scratchreg := get_scratch_reg_int(list);
  442. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  443. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  444. scratchreg,0,l1,l2));
  445. free_scratch_reg(list,scratchreg);
  446. end
  447. else
  448. do_lo_hi;
  449. OP_AND:
  450. { try to use rlwinm }
  451. if gotrlwi then
  452. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  453. src,0,l1,l2))
  454. else
  455. useReg := true;
  456. OP_XOR:
  457. do_lo_hi;
  458. OP_SHL,OP_SHR,OP_SAR:
  459. begin
  460. if (a and 31) <> 0 Then
  461. list.concat(taicpu.op_reg_reg_const(
  462. TOpCG2AsmOpConstLo[Op],dst,src,a and 31));
  463. if (a shr 5) <> 0 then
  464. internalError(68991);
  465. end
  466. else
  467. internalerror(200109091);
  468. end;
  469. { if all else failed, load the constant in a register and then }
  470. { perform the operation }
  471. if useReg then
  472. begin
  473. scratchreg := get_scratch_reg_int(list);
  474. a_load_const_reg(list,OS_32,a,scratchreg);
  475. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  476. free_scratch_reg(list,scratchreg);
  477. end;
  478. end;
  479. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  480. size: tcgsize; src1, src2, dst: tregister);
  481. const
  482. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  483. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  484. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  485. begin
  486. case op of
  487. OP_NEG,OP_NOT:
  488. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  489. else
  490. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  491. end;
  492. end;
  493. {*************** compare instructructions ****************}
  494. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  495. l : tasmlabel);
  496. var
  497. p: taicpu;
  498. scratch_register: TRegister;
  499. signed: boolean;
  500. begin
  501. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  502. { in the following case, we generate more efficient code when }
  503. { signed is true }
  504. if (cmp_op in [OC_EQ,OC_NE]) and
  505. (a > $ffff) then
  506. signed := true;
  507. if signed then
  508. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  509. list.concat(taicpu.op_reg_reg_const(A_CMPWI,R_CR0,reg,longint(a)))
  510. else
  511. begin
  512. scratch_register := get_scratch_reg_int(list);
  513. a_load_const_reg(list,OS_32,a,scratch_register);
  514. list.concat(taicpu.op_reg_reg_reg(A_CMPW,R_CR0,reg,scratch_register));
  515. free_scratch_reg(list,scratch_register);
  516. end
  517. else
  518. if (a <= $ffff) then
  519. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,R_CR0,reg,a))
  520. else
  521. begin
  522. scratch_register := get_scratch_reg_int(list);
  523. a_load_const_reg(list,OS_32,a,scratch_register);
  524. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,R_CR0,reg,scratch_register));
  525. free_scratch_reg(list,scratch_register);
  526. end;
  527. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  528. end;
  529. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  530. reg1,reg2 : tregister;l : tasmlabel);
  531. var
  532. p: taicpu;
  533. op: tasmop;
  534. begin
  535. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  536. op := A_CMPW
  537. else op := A_CMPLW;
  538. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  539. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  540. end;
  541. procedure tcgppc.g_save_standard_registers(list : taasmoutput);
  542. begin
  543. {$warning FIX ME}
  544. end;
  545. procedure tcgppc.g_restore_standard_registers(list : taasmoutput);
  546. begin
  547. {$warning FIX ME}
  548. end;
  549. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  550. begin
  551. {$warning FIX ME}
  552. end;
  553. procedure tcgppc.g_restore_all_registers(list : taasmoutput;selfused,accused,acchiused:boolean);
  554. begin
  555. {$warning FIX ME}
  556. end;
  557. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  558. begin
  559. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  560. end;
  561. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  562. begin
  563. a_jmp(list,A_B,C_None,0,l);
  564. end;
  565. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  566. var
  567. c: tasmcond;
  568. begin
  569. c := flags_to_cond(f);
  570. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(R_CR0),l);
  571. end;
  572. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  573. var
  574. testbit: byte;
  575. bitvalue: boolean;
  576. begin
  577. { get the bit to extract from the conditional register + its }
  578. { requested value (0 or 1) }
  579. testbit := ((ord(f.cr)-ord(R_CR0)) * 4);
  580. case f.flag of
  581. F_EQ,F_NE:
  582. bitvalue := f.flag = F_EQ;
  583. F_LT,F_GE:
  584. begin
  585. inc(testbit);
  586. bitvalue := f.flag = F_LT;
  587. end;
  588. F_GT,F_LE:
  589. begin
  590. inc(testbit,2);
  591. bitvalue := f.flag = F_GT;
  592. end;
  593. else
  594. internalerror(200112261);
  595. end;
  596. { load the conditional register in the destination reg }
  597. list.concat(taicpu.op_reg(A_MFCR,reg));
  598. { we will move the bit that has to be tested to bit 0 by rotating }
  599. { left }
  600. testbit := (32 - testbit) and 31;
  601. { extract bit }
  602. list.concat(taicpu.op_reg_reg_const_const_const(
  603. A_RLWINM,reg,reg,testbit,31,31));
  604. { if we need the inverse, xor with 1 }
  605. if not bitvalue then
  606. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  607. end;
  608. (*
  609. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  610. var
  611. testbit: byte;
  612. bitvalue: boolean;
  613. begin
  614. { get the bit to extract from the conditional register + its }
  615. { requested value (0 or 1) }
  616. case f.simple of
  617. false:
  618. begin
  619. { we don't generate this in the compiler }
  620. internalerror(200109062);
  621. end;
  622. true:
  623. case f.cond of
  624. C_None:
  625. internalerror(200109063);
  626. C_LT..C_NU:
  627. begin
  628. testbit := (ord(f.cr) - ord(R_CR0))*4;
  629. inc(testbit,AsmCondFlag2BI[f.cond]);
  630. bitvalue := AsmCondFlagTF[f.cond];
  631. end;
  632. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  633. begin
  634. testbit := f.crbit
  635. bitvalue := AsmCondFlagTF[f.cond];
  636. end;
  637. else
  638. internalerror(200109064);
  639. end;
  640. end;
  641. { load the conditional register in the destination reg }
  642. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  643. { we will move the bit that has to be tested to bit 31 -> rotate }
  644. { left by bitpos+1 (remember, this is big-endian!) }
  645. if bitpos <> 31 then
  646. inc(bitpos)
  647. else
  648. bitpos := 0;
  649. { extract bit }
  650. list.concat(taicpu.op_reg_reg_const_const_const(
  651. A_RLWINM,reg,reg,bitpos,31,31));
  652. { if we need the inverse, xor with 1 }
  653. if not bitvalue then
  654. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  655. end;
  656. *)
  657. { *********** entry/exit code and address loading ************ }
  658. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  659. begin
  660. case target_info.system of
  661. system_powerpc_macos:
  662. g_stackframe_entry_mac(list,localsize);
  663. system_powerpc_linux:
  664. g_stackframe_entry_sysv(list,localsize)
  665. else
  666. internalerror(2204001);
  667. end;
  668. end;
  669. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  670. { generated the entry code of a procedure/function. Note: localsize is the }
  671. { sum of the size necessary for local variables and the maximum possible }
  672. { combined size of ALL the parameters of a procedure called by the current }
  673. { one }
  674. var regcounter: TRegister;
  675. href : treference;
  676. begin
  677. if (localsize mod 8) <> 0 then internalerror(58991);
  678. { CR and LR only have to be saved in case they are modified by the current }
  679. { procedure, but currently this isn't checked, so save them always }
  680. { following is the entry code as described in "Altivec Programming }
  681. { Interface Manual", bar the saving of AltiVec registers }
  682. a_reg_alloc(list,STACK_POINTER_REG);
  683. a_reg_alloc(list,R_0);
  684. { allocate registers containing reg parameters }
  685. for regcounter := R_3 to R_10 do
  686. a_reg_alloc(list,regcounter);
  687. { save return address... }
  688. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  689. { ... in caller's frame }
  690. reference_reset_base(href,STACK_POINTER_REG,4);
  691. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  692. a_reg_dealloc(list,R_0);
  693. a_reg_alloc(list,R_11);
  694. { save end of fpr save area }
  695. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER_REG,0));
  696. a_reg_alloc(list,R_12);
  697. { 0 or 8 based on SP alignment }
  698. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  699. R_12,STACK_POINTER_REG,0,28,28));
  700. { add in stack length }
  701. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  702. -localsize));
  703. { establish new alignment }
  704. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  705. a_reg_dealloc(list,R_12);
  706. { save floating-point registers }
  707. { !!! has to be optimized: only save registers that are used }
  708. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savefpr_14'),0));
  709. { compute end of gpr save area }
  710. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-144));
  711. { save gprs and fetch GOT pointer }
  712. { !!! has to be optimized: only save registers that are used }
  713. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savegpr_14_go'),0));
  714. a_reg_alloc(list,R_31);
  715. { place GOT ptr in r31 }
  716. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  717. { save the CR if necessary ( !!! always done currently ) }
  718. { still need to find out where this has to be done for SystemV
  719. a_reg_alloc(list,R_0);
  720. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  721. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  722. new_reference(STACK_POINTER_REG,LA_CR)));
  723. a_reg_dealloc(list,R_0); }
  724. { save pointer to incoming arguments }
  725. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_30,R_11,144));
  726. { now comes the AltiVec context save, not yet implemented !!! }
  727. end;
  728. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  729. { generated the entry code of a procedure/function. Note: localsize is the }
  730. { sum of the size necessary for local variables and the maximum possible }
  731. { combined size of ALL the parameters of a procedure called by the current }
  732. { one }
  733. var regcounter: TRegister;
  734. href : treference;
  735. begin
  736. if (localsize mod 8) <> 0 then internalerror(58991);
  737. { CR and LR only have to be saved in case they are modified by the current }
  738. { procedure, but currently this isn't checked, so save them always }
  739. { following is the entry code as described in "Altivec Programming }
  740. { Interface Manual", bar the saving of AltiVec registers }
  741. a_reg_alloc(list,STACK_POINTER_REG);
  742. a_reg_alloc(list,R_0);
  743. { allocate registers containing reg parameters }
  744. for regcounter := R_3 to R_10 do
  745. a_reg_alloc(list,regcounter);
  746. { save return address... }
  747. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  748. { ... in caller's frame }
  749. reference_reset_base(href,STACK_POINTER_REG,8);
  750. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  751. a_reg_dealloc(list,R_0);
  752. { save floating-point registers }
  753. { !!! has to be optimized: only save registers that are used }
  754. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savef14'),0));
  755. { save gprs in gpr save area }
  756. { !!! has to be optimized: only save registers that are used }
  757. reference_reset_base(href,STACK_POINTER_REG,-220);
  758. list.concat(taicpu.op_reg_ref(A_STMW,R_13,href));
  759. { save the CR if necessary ( !!! always done currently ) }
  760. a_reg_alloc(list,R_0);
  761. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  762. reference_reset_base(href,stack_pointer_reg,LA_CR);
  763. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  764. a_reg_dealloc(list,R_0);
  765. { save pointer to incoming arguments }
  766. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  767. a_reg_alloc(list,R_12);
  768. { 0 or 8 based on SP alignment }
  769. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  770. R_12,STACK_POINTER_REG,0,28,28));
  771. { add in stack length }
  772. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  773. -localsize));
  774. { establish new alignment }
  775. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  776. a_reg_dealloc(list,R_12);
  777. { now comes the AltiVec context save, not yet implemented !!! }
  778. end;
  779. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  780. begin
  781. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  782. end;
  783. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  784. begin
  785. case target_info.system of
  786. system_powerpc_macos:
  787. g_return_from_proc_mac(list,parasize);
  788. system_powerpc_linux:
  789. g_return_from_proc_sysv(list,parasize)
  790. else
  791. internalerror(2204001);
  792. end;
  793. end;
  794. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  795. var
  796. ref2, tmpref: treference;
  797. begin
  798. ref2 := ref;
  799. FixRef(list,ref2);
  800. if assigned(ref2.symbol) then
  801. { add the symbol's value to the base of the reference, and if the }
  802. { reference doesn't have a base, create one }
  803. begin
  804. reference_reset(tmpref);
  805. tmpref.offset := ref2.offset;
  806. tmpref.symbol := ref2.symbol;
  807. tmpref.symaddr := refs_ha;
  808. if ref2.base <> R_NO then
  809. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  810. ref2.base,tmpref))
  811. else
  812. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  813. tmpref.base := R_NO;
  814. tmpref.symaddr := refs_l;
  815. { can be folded with one of the next instructions by the }
  816. { optimizer probably }
  817. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  818. end
  819. else if ref2.offset <> 0 Then
  820. if ref2.base <> R_NO then
  821. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  822. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  823. { occurs, so now only ref.offset has to be loaded }
  824. else a_load_const_reg(list,OS_32,ref2.offset,r)
  825. else if ref.index <> R_NO Then
  826. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  827. else if (ref2.base <> R_NO) and
  828. (r <> ref2.base) then
  829. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  830. end;
  831. { ************* concatcopy ************ }
  832. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  833. var
  834. countreg: TRegister;
  835. src, dst: TReference;
  836. lab: tasmlabel;
  837. count, count2: aword;
  838. orgsrc, orgdst : boolean;
  839. begin
  840. {$ifdef extdebug}
  841. if len > high(longint) then
  842. internalerror(2002072704);
  843. {$endif extdebug}
  844. { make sure short loads are handled as optimally as possible }
  845. if not loadref then
  846. if (len <= 8) and
  847. (byte(len) in [1,2,4,8]) then
  848. begin
  849. if len < 8 then
  850. begin
  851. a_load_ref_ref(list,int_cgsize(len),source,dest);
  852. if delsource then
  853. reference_release(exprasmlist,source);
  854. end
  855. else
  856. begin
  857. a_reg_alloc(list,R_F0);
  858. a_loadfpu_ref_reg(list,OS_F64,source,R_F0);
  859. if delsource then
  860. reference_release(exprasmlist,source);
  861. a_loadfpu_reg_ref(list,OS_F64,R_F0,dest);
  862. a_reg_dealloc(list,R_F0);
  863. end;
  864. exit;
  865. end;
  866. { make sure source and dest are valid }
  867. src := source;
  868. fixref(list,src);
  869. dst := dest;
  870. fixref(list,dst);
  871. reference_reset(src);
  872. reference_reset(dst);
  873. { load the address of source into src.base }
  874. if loadref then
  875. begin
  876. src.base := get_scratch_reg_address(list);
  877. a_load_ref_reg(list,OS_32,source,src.base);
  878. orgsrc := false;
  879. end
  880. else if assigned(source.symbol) or
  881. ((source.offset + longint(len)) > high(smallint)) then
  882. begin
  883. src.base := get_scratch_reg_address(list);
  884. a_loadaddr_ref_reg(list,source,src.base);
  885. orgsrc := false;
  886. end
  887. else
  888. begin
  889. src := source;
  890. orgsrc := true;
  891. end;
  892. if not orgsrc and delsource then
  893. reference_release(exprasmlist,source);
  894. { load the address of dest into dst.base }
  895. if assigned(dest.symbol) or
  896. ((dest.offset + longint(len)) > high(smallint)) then
  897. begin
  898. dst.base := get_scratch_reg_address(list);
  899. a_loadaddr_ref_reg(list,dest,dst.base);
  900. orgdst := false;
  901. end
  902. else
  903. begin
  904. dst := dest;
  905. orgdst := true;
  906. end;
  907. count := len div 8;
  908. if count > 4 then
  909. { generate a loop }
  910. begin
  911. { the offsets are zero after the a_loadaddress_ref_reg and just }
  912. { have to be set to 8. I put an Inc there so debugging may be }
  913. { easier (should offset be different from zero here, it will be }
  914. { easy to notice in the generated assembler }
  915. inc(dst.offset,8);
  916. inc(src.offset,8);
  917. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  918. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  919. countreg := get_scratch_reg_int(list);
  920. a_load_const_reg(list,OS_32,count,countreg);
  921. { explicitely allocate R_0 since it can be used safely here }
  922. { (for holding date that's being copied) }
  923. a_reg_alloc(list,R_F0);
  924. getlabel(lab);
  925. a_label(list, lab);
  926. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  927. list.concat(taicpu.op_reg_ref(A_LFDU,R_F0,src));
  928. list.concat(taicpu.op_reg_ref(A_STFDU,R_F0,dst));
  929. a_jmp(list,A_BC,C_NE,0,lab);
  930. free_scratch_reg(list,countreg);
  931. a_reg_dealloc(list,R_F0);
  932. len := len mod 8;
  933. end;
  934. count := len div 8;
  935. if count > 0 then
  936. { unrolled loop }
  937. begin
  938. a_reg_alloc(list,R_F0);
  939. for count2 := 1 to count do
  940. begin
  941. a_loadfpu_ref_reg(list,OS_F64,src,R_F0);
  942. a_loadfpu_reg_ref(list,OS_F64,R_F0,dst);
  943. inc(src.offset,8);
  944. inc(dst.offset,8);
  945. end;
  946. a_reg_dealloc(list,R_F0);
  947. len := len mod 8;
  948. end;
  949. if (len and 4) <> 0 then
  950. begin
  951. a_reg_alloc(list,R_0);
  952. a_load_ref_reg(list,OS_32,src,R_0);
  953. a_load_reg_ref(list,OS_32,R_0,dst);
  954. inc(src.offset,4);
  955. inc(dst.offset,4);
  956. a_reg_dealloc(list,R_0);
  957. end;
  958. { copy the leftovers }
  959. if (len and 2) <> 0 then
  960. begin
  961. a_reg_alloc(list,R_0);
  962. a_load_ref_reg(list,OS_16,src,R_0);
  963. a_load_reg_ref(list,OS_16,R_0,dst);
  964. inc(src.offset,2);
  965. inc(dst.offset,2);
  966. a_reg_dealloc(list,R_0);
  967. end;
  968. if (len and 1) <> 0 then
  969. begin
  970. a_reg_alloc(list,R_0);
  971. a_load_ref_reg(list,OS_8,src,R_0);
  972. a_load_reg_ref(list,OS_8,R_0,dst);
  973. a_reg_dealloc(list,R_0);
  974. end;
  975. if orgsrc then
  976. begin
  977. if delsource then
  978. reference_release(exprasmlist,source);
  979. end
  980. else
  981. free_scratch_reg(list,src.base);
  982. if not orgdst then
  983. free_scratch_reg(list,dst.base);
  984. end;
  985. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  986. var
  987. hl : tasmlabel;
  988. begin
  989. if not(cs_check_overflow in aktlocalswitches) then
  990. exit;
  991. getlabel(hl);
  992. if not ((p.resulttype.def.deftype=pointerdef) or
  993. ((p.resulttype.def.deftype=orddef) and
  994. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  995. bool8bit,bool16bit,bool32bit]))) then
  996. begin
  997. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  998. a_jmp(list,A_BC,C_OV,7,hl)
  999. end
  1000. else
  1001. a_jmp_cond(list,OC_AE,hl);
  1002. a_call_name(list,'FPC_OVERFLOW');
  1003. a_label(list,hl);
  1004. end;
  1005. {***************** This is private property, keep out! :) *****************}
  1006. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  1007. var
  1008. regcounter: TRegister;
  1009. begin
  1010. { release parameter registers }
  1011. for regcounter := R_3 to R_10 do
  1012. a_reg_dealloc(list,regcounter);
  1013. { AltiVec context restore, not yet implemented !!! }
  1014. { address of gpr save area to r11 }
  1015. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_31,-144));
  1016. { restore gprs }
  1017. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restgpr_14'),0));
  1018. { address of fpr save area to r11 }
  1019. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,144));
  1020. { restore fprs and return }
  1021. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restfpr_14_x'),0));
  1022. end;
  1023. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1024. var
  1025. regcounter: TRegister;
  1026. href : treference;
  1027. begin
  1028. { release parameter registers }
  1029. for regcounter := R_3 to R_10 do
  1030. a_reg_dealloc(list,regcounter);
  1031. { AltiVec context restore, not yet implemented !!! }
  1032. { restore SP }
  1033. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER_REG,R_31,0));
  1034. { restore gprs }
  1035. reference_reset_base(href,STACK_POINTER_REG,-220);
  1036. list.concat(taicpu.op_reg_ref(A_LMW,R_13,href));
  1037. { restore return address ... }
  1038. reference_reset_base(href,STACK_POINTER_REG,8);
  1039. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1040. { ... and return from _restf14 }
  1041. list.concat(taicpu.op_sym_ofs(A_B,newasmsymbol('_restf14'),0));
  1042. end;
  1043. procedure tcgppc.fixref(list: taasmoutput; var ref: treference);
  1044. begin
  1045. If (ref.base <> R_NO) then
  1046. begin
  1047. if (ref.index <> R_NO) and
  1048. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1049. begin
  1050. if not assigned(ref.symbol) and
  1051. (cardinal(ref.offset-low(smallint)) <=
  1052. high(smallint)-low(smallint)) then
  1053. begin
  1054. list.concat(taicpu.op_reg_reg_const(
  1055. A_ADDI,ref.base,ref.base,ref.offset));
  1056. ref.offset := 0;
  1057. end
  1058. else
  1059. begin
  1060. list.concat(taicpu.op_reg_reg_reg(
  1061. A_ADD,ref.base,ref.base,ref.index));
  1062. ref.index := R_NO;
  1063. end;
  1064. end
  1065. end
  1066. else
  1067. begin
  1068. ref.base := ref.index;
  1069. ref.index := R_NO
  1070. end
  1071. end;
  1072. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1073. { that's the case, we can use rlwinm to do an AND operation }
  1074. function tcgppc.get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  1075. var
  1076. temp, testbit: longint;
  1077. compare: boolean;
  1078. begin
  1079. get_rlwi_const := false;
  1080. if (a = 0) or (a = $ffffffff) then
  1081. exit;
  1082. { start with the lowest bit }
  1083. testbit := 1;
  1084. { check its value }
  1085. compare := boolean(a and testbit);
  1086. { find out how long the run of bits with this value is }
  1087. { (it's impossible that all bits are 1 or 0, because in that case }
  1088. { this function wouldn't have been called) }
  1089. l1 := 31;
  1090. while (((a and testbit) <> 0) = compare) do
  1091. begin
  1092. testbit := testbit shl 1;
  1093. dec(l1);
  1094. end;
  1095. { check the length of the run of bits that comes next }
  1096. compare := not compare;
  1097. l2 := l1;
  1098. while (((a and testbit) <> 0) = compare) and
  1099. (l2 >= 0) do
  1100. begin
  1101. testbit := testbit shl 1;
  1102. dec(l2);
  1103. end;
  1104. { and finally the check whether the rest of the bits all have the }
  1105. { same value }
  1106. compare := not compare;
  1107. temp := l2;
  1108. if temp >= 0 then
  1109. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1110. exit;
  1111. { we have done "not(not(compare))", so compare is back to its }
  1112. { initial value. If the lowest bit was 0, a is of the form }
  1113. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1114. { because l2 now contains the position of the last zero of the }
  1115. { first run instead of that of the first 1) so switch l1 and l2 }
  1116. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1117. if not compare then
  1118. begin
  1119. temp := l1;
  1120. l1 := l2+1;
  1121. l2 := temp;
  1122. end
  1123. else
  1124. { otherwise, l1 currently contains the position of the last }
  1125. { zero instead of that of the first 1 of the second run -> +1 }
  1126. inc(l1);
  1127. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1128. l1 := l1 and 31;
  1129. l2 := l2 and 31;
  1130. get_rlwi_const := true;
  1131. end;
  1132. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1133. ref: treference);
  1134. var
  1135. tmpreg: tregister;
  1136. tmpref: treference;
  1137. begin
  1138. if assigned(ref.symbol) then
  1139. begin
  1140. tmpreg := get_scratch_reg_address(list);
  1141. reference_reset(tmpref);
  1142. tmpref.symbol := ref.symbol;
  1143. tmpref.symaddr := refs_ha;
  1144. if ref.base <> R_NO then
  1145. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1146. ref.base,tmpref))
  1147. else
  1148. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1149. ref.base := tmpreg;
  1150. ref.symaddr := refs_l;
  1151. end;
  1152. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1153. if assigned(ref.symbol) then
  1154. free_scratch_reg(list,tmpreg);
  1155. end;
  1156. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1157. crval: longint; l: tasmlabel);
  1158. var
  1159. p: taicpu;
  1160. begin
  1161. p := taicpu.op_sym(op,newasmsymbol(l.name));
  1162. if op <> A_B then
  1163. create_cond_norm(c,crval,p.condition);
  1164. p.is_jmp := true;
  1165. list.concat(p)
  1166. end;
  1167. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1168. begin
  1169. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1170. end;
  1171. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  1172. begin
  1173. a_op64_const_reg_reg(list,op,value,reg,reg);
  1174. end;
  1175. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1176. begin
  1177. case op of
  1178. OP_AND,OP_OR,OP_XOR:
  1179. begin
  1180. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1181. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1182. end;
  1183. OP_ADD:
  1184. begin
  1185. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1186. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1187. end;
  1188. OP_SUB:
  1189. begin
  1190. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1191. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1192. end;
  1193. else
  1194. internalerror(2002072801);
  1195. end;
  1196. end;
  1197. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  1198. const
  1199. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1200. (A_SUBIC,A_SUBC,A_ADDME));
  1201. var
  1202. tmpreg: tregister;
  1203. tmpreg64: tregister64;
  1204. issub: boolean;
  1205. begin
  1206. case op of
  1207. OP_AND,OP_OR,OP_XOR:
  1208. begin
  1209. cg.a_op_const_reg_reg(list,op,OS_32,cardinal(value),regsrc.reglo,regdst.reglo);
  1210. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1211. regdst.reghi);
  1212. end;
  1213. OP_ADD, OP_SUB:
  1214. begin
  1215. if (longint(value) <> 0) then
  1216. begin
  1217. issub := op = OP_SUB;
  1218. if (longint(value)-ord(issub) >= -32768) and
  1219. (longint(value)-ord(issub) <= 32767) then
  1220. begin
  1221. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1222. regdst.reglo,regsrc.reglo,longint(value)));
  1223. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1224. regdst.reghi,regsrc.reghi));
  1225. end
  1226. else if ((value shr 32) = 0) then
  1227. begin
  1228. tmpreg := cg.get_scratch_reg_int(list);
  1229. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  1230. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1231. regdst.reglo,regsrc.reglo,tmpreg));
  1232. cg.free_scratch_reg(list,tmpreg);
  1233. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1234. regdst.reghi,regsrc.reghi));
  1235. end
  1236. else
  1237. begin
  1238. tmpreg64.reglo := cg.get_scratch_reg_int(list);
  1239. tmpreg64.reghi := cg.get_scratch_reg_int(list);
  1240. a_load64_const_reg(list,value,tmpreg64);
  1241. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  1242. cg.free_scratch_reg(list,tmpreg64.reghi);
  1243. cg.free_scratch_reg(list,tmpreg64.reglo);
  1244. end
  1245. end
  1246. else
  1247. begin
  1248. cg.a_load_reg_reg(list,OS_INT,regsrc.reglo,regdst.reglo);
  1249. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  1250. regdst.reghi);
  1251. end;
  1252. end;
  1253. else
  1254. internalerror(2002072802);
  1255. end;
  1256. end;
  1257. begin
  1258. cg := tcgppc.create;
  1259. cg64 :=tcg64fppc.create;
  1260. end.
  1261. {
  1262. $Log$
  1263. Revision 1.35 2002-08-06 07:12:05 jonas
  1264. * fixed bug in g_flags2reg()
  1265. * and yet more constant operation fixes :)
  1266. Revision 1.34 2002/08/05 08:58:53 jonas
  1267. * fixed compilation problems
  1268. Revision 1.33 2002/08/04 12:57:55 jonas
  1269. * more misc. fixes, mostly constant-related
  1270. Revision 1.32 2002/08/02 11:10:42 jonas
  1271. * some misc constant fixes
  1272. Revision 1.31 2002/07/30 20:50:44 florian
  1273. * the code generator knows now if parameters are in registers
  1274. Revision 1.30 2002/07/29 21:23:44 florian
  1275. * more fixes for the ppc
  1276. + wrappers for the tcnvnode.first_* stuff introduced
  1277. Revision 1.29 2002/07/28 21:38:30 florian
  1278. - removed debug code which was commited by accident
  1279. Revision 1.28 2002/07/28 21:34:31 florian
  1280. * more powerpc fixes
  1281. + dummy tcgvecnode
  1282. Revision 1.27 2002/07/28 16:01:59 jonas
  1283. + tcg64fppc.a_op64_const_reg_reg() and tcg64fppc.a_op64_reg_reg_reg()
  1284. * several fixes, most notably in a_load_reg_reg(): it didn't do any
  1285. conversion from smaller to larger sizes or vice versa
  1286. * some small optimizations
  1287. Revision 1.26 2002/07/27 19:59:29 jonas
  1288. * fixed a_loadaddr_ref_reg()
  1289. * fixed g_flags2reg()
  1290. * optimized g_concatcopy()
  1291. Revision 1.25 2002/07/26 21:15:45 florian
  1292. * rewrote the system handling
  1293. Revision 1.24 2002/07/21 17:00:23 jonas
  1294. * make sure we use rlwi* when possible instead of andi.
  1295. Revision 1.23 2002/07/11 14:41:34 florian
  1296. * start of the new generic parameter handling
  1297. Revision 1.22 2002/07/11 07:38:28 jonas
  1298. + tcg64fpc implementation (only a_op64_reg_reg and a_op64_const_reg for
  1299. now)
  1300. * fixed and improved tcgppc.a_load_const_reg
  1301. * improved tcgppc.a_op_const_reg, tcgppc.a_cmp_const_reg_label
  1302. * A_CMP* -> A_CMPW* (this means that 32bit compares should be done)
  1303. Revision 1.21 2002/07/09 19:45:01 jonas
  1304. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  1305. * small fixes in the assembler writer
  1306. * changed scratch registers, because they were used by the linker (r11
  1307. and r12) and by the abi under linux (r31)
  1308. Revision 1.20 2002/07/07 09:44:31 florian
  1309. * powerpc target fixed, very simple units can be compiled
  1310. Revision 1.19 2002/05/20 13:30:41 carl
  1311. * bugfix of hdisponen (base must be set, not index)
  1312. * more portability fixes
  1313. Revision 1.18 2002/05/18 13:34:26 peter
  1314. * readded missing revisions
  1315. Revision 1.17 2002/05/16 19:46:53 carl
  1316. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1317. + try to fix temp allocation (still in ifdef)
  1318. + generic constructor calls
  1319. + start of tassembler / tmodulebase class cleanup
  1320. Revision 1.14 2002/05/13 19:52:46 peter
  1321. * a ppcppc can be build again
  1322. Revision 1.13 2002/04/20 21:41:51 carl
  1323. * renamed some constants
  1324. Revision 1.12 2002/04/06 18:13:01 jonas
  1325. * several powerpc-related additions and fixes
  1326. Revision 1.11 2002/01/02 14:53:04 jonas
  1327. * fixed small bug in a_jmp_flags
  1328. }