cgcpu.pas 56 KB

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