cgcpu.pas 56 KB

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