cgcpu.pas 54 KB

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