cgcpu.pas 72 KB

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