cgcpu.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  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 ((sreg.bitlen mod 8) = 0) then
  352. begin
  353. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,destreg,destreg);
  354. a_load_reg_reg(list,subsetsize,tosize,destreg,destreg);
  355. end;
  356. end
  357. else
  358. a_load_reg_reg(list,subsetsize,tosize,sreg.subsetreg,destreg);
  359. end;
  360. procedure tcgppc.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  361. begin
  362. if (slopt in [SL_SETZERO,SL_SETMAX]) then
  363. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  364. else if (sreg.bitlen <> sizeof(aint) * 8) then
  365. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,sreg.subsetreg,fromreg,
  366. sreg.startbit,32-sreg.startbit-sreg.bitlen,31-sreg.startbit))
  367. else
  368. a_load_reg_reg(list,fromsize,subsetsize,fromreg,sreg.subsetreg);
  369. end;
  370. procedure tcgppc.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tcgsize; const fromsreg, tosreg: tsubsetregister);
  371. begin
  372. if (fromsreg.bitlen >= tosreg.bitlen) then
  373. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,tosreg.subsetreg, fromsreg.subsetreg,
  374. (tosreg.startbit-fromsreg.startbit) and 31,
  375. 32-tosreg.startbit-tosreg.bitlen,31-tosreg.startbit))
  376. else
  377. inherited a_load_subsetreg_subsetreg(list,fromsubsetsize,tosubsetsize,fromsreg,tosreg);
  378. end;
  379. procedure tcgppc.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  380. begin
  381. a_op_const_reg_reg(list,op,size,a,reg,reg);
  382. end;
  383. procedure tcgppc.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  384. begin
  385. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  386. end;
  387. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  388. const
  389. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  390. begin
  391. if (op in overflowops) and
  392. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  393. a_load_reg_reg(list,OS_32,size,dst,dst);
  394. end;
  395. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  396. size: tcgsize; a: aint; src, dst: tregister);
  397. var
  398. l1,l2: longint;
  399. oplo, ophi: tasmop;
  400. scratchreg: tregister;
  401. useReg, gotrlwi: boolean;
  402. procedure do_lo_hi;
  403. begin
  404. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  405. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  406. end;
  407. begin
  408. if (op = OP_MOVE) then
  409. internalerror(2006031401);
  410. if op = OP_SUB then
  411. begin
  412. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  413. exit;
  414. end;
  415. ophi := TOpCG2AsmOpConstHi[op];
  416. oplo := TOpCG2AsmOpConstLo[op];
  417. gotrlwi := get_rlwi_const(a,l1,l2);
  418. if (op in [OP_AND,OP_OR,OP_XOR]) then
  419. begin
  420. if (a = 0) then
  421. begin
  422. if op = OP_AND then
  423. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  424. else
  425. a_load_reg_reg(list,size,size,src,dst);
  426. exit;
  427. end
  428. else if (a = -1) then
  429. begin
  430. case op of
  431. OP_OR:
  432. case size of
  433. OS_8, OS_S8:
  434. list.concat(taicpu.op_reg_const(A_LI,dst,255));
  435. OS_16, OS_S16:
  436. a_load_const_reg(list,OS_16,65535,dst);
  437. else
  438. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  439. end;
  440. OP_XOR:
  441. case size of
  442. OS_8, OS_S8:
  443. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,255));
  444. OS_16, OS_S16:
  445. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,65535));
  446. else
  447. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  448. end;
  449. OP_AND:
  450. a_load_reg_reg(list,size,size,src,dst);
  451. end;
  452. exit;
  453. end
  454. else if (aword(a) <= high(word)) and
  455. ((op <> OP_AND) or
  456. not gotrlwi) then
  457. begin
  458. if ((size = OS_8) and
  459. (byte(a) <> a)) or
  460. ((size = OS_S8) and
  461. (shortint(a) <> a)) then
  462. internalerror(200604142);
  463. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  464. { and/or/xor -> cannot overflow in high 16 bits }
  465. exit;
  466. end;
  467. { all basic constant instructions also have a shifted form that }
  468. { works only on the highest 16bits, so if lo(a) is 0, we can }
  469. { use that one }
  470. if (word(a) = 0) and
  471. (not(op = OP_AND) or
  472. not gotrlwi) then
  473. begin
  474. if (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  475. internalerror(200604141);
  476. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  477. exit;
  478. end;
  479. end
  480. else if (op = OP_ADD) then
  481. if a = 0 then
  482. begin
  483. a_load_reg_reg(list,size,size,src,dst);
  484. exit
  485. end
  486. else if (a >= low(smallint)) and
  487. (a <= high(smallint)) then
  488. begin
  489. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  490. maybeadjustresult(list,op,size,dst);
  491. exit;
  492. end;
  493. { otherwise, the instructions we can generate depend on the }
  494. { operation }
  495. useReg := false;
  496. case op of
  497. OP_DIV,OP_IDIV:
  498. if (a = 0) then
  499. internalerror(200208103)
  500. else if (a = 1) then
  501. begin
  502. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  503. exit
  504. end
  505. else if ispowerof2(a,l1) then
  506. begin
  507. case op of
  508. OP_DIV:
  509. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  510. OP_IDIV:
  511. begin
  512. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  513. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  514. end;
  515. end;
  516. exit;
  517. end
  518. else
  519. usereg := true;
  520. OP_IMUL, OP_MUL:
  521. if (a = 0) then
  522. begin
  523. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  524. exit
  525. end
  526. else if (a = 1) then
  527. begin
  528. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  529. exit
  530. end
  531. else if ispowerof2(a,l1) then
  532. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  533. else if (longint(a) >= low(smallint)) and
  534. (longint(a) <= high(smallint)) then
  535. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  536. else
  537. usereg := true;
  538. OP_ADD:
  539. begin
  540. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  541. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  542. smallint((a shr 16) + ord(smallint(a) < 0))));
  543. end;
  544. OP_OR:
  545. { try to use rlwimi }
  546. if gotrlwi and
  547. (src = dst) then
  548. begin
  549. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  550. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  551. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  552. scratchreg,0,l1,l2));
  553. end
  554. else
  555. do_lo_hi;
  556. OP_AND:
  557. { try to use rlwinm }
  558. if gotrlwi then
  559. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  560. src,0,l1,l2))
  561. else
  562. useReg := true;
  563. OP_XOR:
  564. do_lo_hi;
  565. OP_SHL,OP_SHR,OP_SAR:
  566. begin
  567. if (a and 31) <> 0 Then
  568. list.concat(taicpu.op_reg_reg_const(
  569. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  570. else
  571. a_load_reg_reg(list,size,size,src,dst);
  572. if (a shr 5) <> 0 then
  573. internalError(68991);
  574. end
  575. else
  576. internalerror(200109091);
  577. end;
  578. { if all else failed, load the constant in a register and then }
  579. { perform the operation }
  580. if useReg then
  581. begin
  582. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  583. a_load_const_reg(list,OS_32,a,scratchreg);
  584. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  585. end;
  586. maybeadjustresult(list,op,size,dst);
  587. end;
  588. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  589. size: tcgsize; src1, src2, dst: tregister);
  590. const
  591. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  592. (A_NONE,A_MR,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  593. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  594. begin
  595. if (op = OP_MOVE) then
  596. internalerror(2006031402);
  597. case op of
  598. OP_NEG,OP_NOT:
  599. begin
  600. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  601. if (op = OP_NOT) and
  602. not(size in [OS_32,OS_S32]) then
  603. { zero/sign extend result again }
  604. a_load_reg_reg(list,OS_32,size,dst,dst);
  605. end;
  606. else
  607. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  608. end;
  609. maybeadjustresult(list,op,size,dst);
  610. end;
  611. {*************** compare instructructions ****************}
  612. procedure tcgppc.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  613. l : tasmlabel);
  614. var
  615. scratch_register: TRegister;
  616. signed: boolean;
  617. begin
  618. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE,OC_EQ,OC_NE];
  619. { in the following case, we generate more efficient code when }
  620. { signed is false }
  621. if (cmp_op in [OC_EQ,OC_NE]) and
  622. (aword(a) >= $8000) and
  623. (aword(a) <= $ffff) then
  624. signed := false;
  625. if signed then
  626. if (a >= low(smallint)) and (a <= high(smallint)) Then
  627. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  628. else
  629. begin
  630. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  631. a_load_const_reg(list,OS_32,a,scratch_register);
  632. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  633. end
  634. else
  635. if (aword(a) <= $ffff) then
  636. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  637. else
  638. begin
  639. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  640. a_load_const_reg(list,OS_32,a,scratch_register);
  641. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  642. end;
  643. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  644. end;
  645. procedure tcgppc.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  646. reg1,reg2 : tregister;l : tasmlabel);
  647. var
  648. op: tasmop;
  649. begin
  650. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  651. op := A_CMPW
  652. else
  653. op := A_CMPLW;
  654. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  655. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  656. end;
  657. procedure tcgppc.a_jmp_name(list : TAsmList;const s : string);
  658. var
  659. p : taicpu;
  660. begin
  661. if (target_info.system = system_powerpc_darwin) then
  662. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  663. else
  664. p := taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  665. p.is_jmp := true;
  666. list.concat(p)
  667. end;
  668. procedure tcgppc.a_jmp_always(list : TAsmList;l: tasmlabel);
  669. begin
  670. a_jmp(list,A_B,C_None,0,l);
  671. end;
  672. procedure tcgppc.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  673. var
  674. c: tasmcond;
  675. begin
  676. c := flags_to_cond(f);
  677. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  678. end;
  679. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  680. var
  681. testbit: byte;
  682. bitvalue: boolean;
  683. begin
  684. { get the bit to extract from the conditional register + its }
  685. { requested value (0 or 1) }
  686. testbit := ((f.cr-RS_CR0) * 4);
  687. case f.flag of
  688. F_EQ,F_NE:
  689. begin
  690. inc(testbit,2);
  691. bitvalue := f.flag = F_EQ;
  692. end;
  693. F_LT,F_GE:
  694. begin
  695. bitvalue := f.flag = F_LT;
  696. end;
  697. F_GT,F_LE:
  698. begin
  699. inc(testbit);
  700. bitvalue := f.flag = F_GT;
  701. end;
  702. else
  703. internalerror(200112261);
  704. end;
  705. { load the conditional register in the destination reg }
  706. list.concat(taicpu.op_reg(A_MFCR,reg));
  707. { we will move the bit that has to be tested to bit 0 by rotating }
  708. { left }
  709. testbit := (testbit + 1) and 31;
  710. { extract bit }
  711. list.concat(taicpu.op_reg_reg_const_const_const(
  712. A_RLWINM,reg,reg,testbit,31,31));
  713. { if we need the inverse, xor with 1 }
  714. if not bitvalue then
  715. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  716. end;
  717. (*
  718. procedure tcgppc.g_cond2reg(list: TAsmList; const f: TAsmCond; reg: TRegister);
  719. var
  720. testbit: byte;
  721. bitvalue: boolean;
  722. begin
  723. { get the bit to extract from the conditional register + its }
  724. { requested value (0 or 1) }
  725. case f.simple of
  726. false:
  727. begin
  728. { we don't generate this in the compiler }
  729. internalerror(200109062);
  730. end;
  731. true:
  732. case f.cond of
  733. C_None:
  734. internalerror(200109063);
  735. C_LT..C_NU:
  736. begin
  737. testbit := (ord(f.cr) - ord(R_CR0))*4;
  738. inc(testbit,AsmCondFlag2BI[f.cond]);
  739. bitvalue := AsmCondFlagTF[f.cond];
  740. end;
  741. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  742. begin
  743. testbit := f.crbit
  744. bitvalue := AsmCondFlagTF[f.cond];
  745. end;
  746. else
  747. internalerror(200109064);
  748. end;
  749. end;
  750. { load the conditional register in the destination reg }
  751. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  752. { we will move the bit that has to be tested to bit 31 -> rotate }
  753. { left by bitpos+1 (remember, this is big-endian!) }
  754. if bitpos <> 31 then
  755. inc(bitpos)
  756. else
  757. bitpos := 0;
  758. { extract bit }
  759. list.concat(taicpu.op_reg_reg_const_const_const(
  760. A_RLWINM,reg,reg,bitpos,31,31));
  761. { if we need the inverse, xor with 1 }
  762. if not bitvalue then
  763. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  764. end;
  765. *)
  766. { *********** entry/exit code and address loading ************ }
  767. procedure tcgppc.g_save_standard_registers(list:TAsmList);
  768. begin
  769. { this work is done in g_proc_entry }
  770. end;
  771. procedure tcgppc.g_restore_standard_registers(list:TAsmList);
  772. begin
  773. { this work is done in g_proc_exit }
  774. end;
  775. procedure tcgppc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  776. { generated the entry code of a procedure/function. Note: localsize is the }
  777. { sum of the size necessary for local variables and the maximum possible }
  778. { combined size of ALL the parameters of a procedure called by the current }
  779. { one. }
  780. { This procedure may be called before, as well as after g_return_from_proc }
  781. { is called. NOTE registers are not to be allocated through the register }
  782. { allocator here, because the register colouring has already occured !! }
  783. var regcounter,firstregfpu,firstregint: TSuperRegister;
  784. href : treference;
  785. usesfpr,usesgpr,gotgot : boolean;
  786. cond : tasmcond;
  787. instr : taicpu;
  788. begin
  789. { CR and LR only have to be saved in case they are modified by the current }
  790. { procedure, but currently this isn't checked, so save them always }
  791. { following is the entry code as described in "Altivec Programming }
  792. { Interface Manual", bar the saving of AltiVec registers }
  793. a_reg_alloc(list,NR_STACK_POINTER_REG);
  794. usesgpr := false;
  795. usesfpr := false;
  796. if not(po_assembler in current_procinfo.procdef.procoptions) then
  797. begin
  798. { save link register? }
  799. if (pi_do_call in current_procinfo.flags) or
  800. ([cs_lineinfo,cs_debuginfo,cs_profile] * current_settings.moduleswitches <> []) then
  801. begin
  802. a_reg_alloc(list,NR_R0);
  803. { save return address... }
  804. { warning: if this is no longer done via r0, or if r0 is }
  805. { added to the usable registers, adapt tcgppcgen.g_profilecode }
  806. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  807. { ... in caller's frame }
  808. case target_info.abi of
  809. abi_powerpc_aix:
  810. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  811. abi_powerpc_sysv:
  812. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  813. end;
  814. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  815. if not(cs_profile in current_settings.moduleswitches) then
  816. a_reg_dealloc(list,NR_R0);
  817. end;
  818. (*
  819. { save the CR if necessary in callers frame. }
  820. if target_info.abi = abi_powerpc_aix then
  821. if false then { Not needed at the moment. }
  822. begin
  823. a_reg_alloc(list,NR_R0);
  824. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  825. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  826. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  827. a_reg_dealloc(list,NR_R0);
  828. end;
  829. *)
  830. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  831. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  832. usesgpr := firstregint <> 32;
  833. usesfpr := firstregfpu <> 32;
  834. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  835. begin
  836. a_reg_alloc(list,NR_R12);
  837. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  838. end;
  839. end;
  840. { no GOT pointer loaded yet }
  841. gotgot:=false;
  842. if usesfpr then
  843. begin
  844. { save floating-point registers
  845. if (cs_create_pic in current_settings.moduleswitches) and not(usesgpr) then
  846. begin
  847. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g'));
  848. gotgot:=true;
  849. end
  850. else
  851. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)));
  852. }
  853. reference_reset_base(href,NR_R1,-8);
  854. for regcounter:=firstregfpu to RS_F31 do
  855. begin
  856. a_loadfpu_reg_ref(list,OS_F64,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  857. dec(href.offset,8);
  858. end;
  859. { compute start of gpr save area }
  860. inc(href.offset,4);
  861. end
  862. else
  863. { compute start of gpr save area }
  864. reference_reset_base(href,NR_R1,-4);
  865. { save gprs and fetch GOT pointer }
  866. if usesgpr then
  867. begin
  868. {
  869. if cs_create_pic in current_settings.moduleswitches then
  870. begin
  871. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g'));
  872. gotgot:=true;
  873. end
  874. else
  875. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)))
  876. }
  877. if (firstregint <= RS_R22) or
  878. ((cs_opt_size in current_settings.optimizerswitches) and
  879. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  880. (firstregint <= RS_R29)) then
  881. begin
  882. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  883. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  884. end
  885. else
  886. for regcounter:=firstregint to RS_R31 do
  887. begin
  888. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  889. dec(href.offset,4);
  890. end;
  891. end;
  892. { done in ncgutil because it may only be released after the parameters }
  893. { have been moved to their final resting place }
  894. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  895. { a_reg_dealloc(list,NR_R12); }
  896. { if we didn't get the GOT pointer till now, we've to calculate it now }
  897. (*
  898. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  899. case target_info.system of
  900. system_powerpc_darwin:
  901. begin
  902. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  903. fillchar(cond,sizeof(cond),0);
  904. cond.simple:=false;
  905. cond.bo:=20;
  906. cond.bi:=31;
  907. instr:=taicpu.op_sym(A_BCL,current_procinfo.CurrGOTLabel);
  908. instr.setcondition(cond);
  909. list.concat(instr);
  910. a_label(list,current_procinfo.CurrGOTLabel);
  911. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  912. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  913. end;
  914. else
  915. begin
  916. a_reg_alloc(list,NR_R31);
  917. { place GOT ptr in r31 }
  918. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  919. end;
  920. end;
  921. *)
  922. if (not nostackframe) and
  923. (localsize <> 0) then
  924. begin
  925. if (localsize <= high(smallint)) then
  926. begin
  927. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  928. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  929. end
  930. else
  931. begin
  932. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  933. { can't use getregisterint here, the register colouring }
  934. { is already done when we get here }
  935. href.index := NR_R11;
  936. a_reg_alloc(list,href.index);
  937. a_load_const_reg(list,OS_S32,-localsize,href.index);
  938. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  939. a_reg_dealloc(list,href.index);
  940. end;
  941. end;
  942. { save the CR if necessary ( !!! never done currently ) }
  943. { still need to find out where this has to be done for SystemV
  944. a_reg_alloc(list,R_0);
  945. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  946. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  947. new_reference(STACK_POINTER_REG,LA_CR)));
  948. a_reg_dealloc(list,R_0);
  949. }
  950. { now comes the AltiVec context save, not yet implemented !!! }
  951. end;
  952. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  953. { This procedure may be called before, as well as after g_stackframe_entry }
  954. { is called. NOTE registers are not to be allocated through the register }
  955. { allocator here, because the register colouring has already occured !! }
  956. var
  957. regcounter,firstregfpu,firstregint: TsuperRegister;
  958. href : treference;
  959. usesfpr,usesgpr,genret : boolean;
  960. localsize: aint;
  961. begin
  962. { AltiVec context restore, not yet implemented !!! }
  963. usesfpr:=false;
  964. usesgpr:=false;
  965. if not (po_assembler in current_procinfo.procdef.procoptions) then
  966. begin
  967. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  968. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  969. usesgpr := firstregint <> 32;
  970. usesfpr := firstregfpu <> 32;
  971. end;
  972. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  973. { adjust r1 }
  974. { (register allocator is no longer valid at this time and an add of 0 }
  975. { is translated into a move, which is then registered with the register }
  976. { allocator, causing a crash }
  977. if (not nostackframe) and
  978. (localsize <> 0) then
  979. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  980. { no return (blr) generated yet }
  981. genret:=true;
  982. if usesfpr then
  983. begin
  984. reference_reset_base(href,NR_R1,-8);
  985. for regcounter := firstregfpu to RS_F31 do
  986. begin
  987. a_loadfpu_ref_reg(list,OS_F64,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  988. dec(href.offset,8);
  989. end;
  990. inc(href.offset,4);
  991. end
  992. else
  993. reference_reset_base(href,NR_R1,-4);
  994. if (usesgpr) then
  995. begin
  996. if (firstregint <= RS_R22) or
  997. ((cs_opt_size in current_settings.optimizerswitches) and
  998. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  999. (firstregint <= RS_R29)) then
  1000. begin
  1001. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1002. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1003. end
  1004. else
  1005. for regcounter:=firstregint to RS_R31 do
  1006. begin
  1007. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  1008. dec(href.offset,4);
  1009. end;
  1010. end;
  1011. (*
  1012. { restore fprs and return }
  1013. if usesfpr then
  1014. begin
  1015. { address of fpr save area to r11 }
  1016. r:=NR_R12;
  1017. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1018. {
  1019. if (pi_do_call in current_procinfo.flags) then
  1020. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x'))
  1021. else
  1022. { leaf node => lr haven't to be restored }
  1023. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  1024. genret:=false;
  1025. }
  1026. end;
  1027. *)
  1028. { if we didn't generate the return code, we've to do it now }
  1029. if genret then
  1030. begin
  1031. { load link register? }
  1032. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1033. begin
  1034. if (pi_do_call in current_procinfo.flags) then
  1035. begin
  1036. case target_info.abi of
  1037. abi_powerpc_aix:
  1038. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1039. abi_powerpc_sysv:
  1040. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1041. end;
  1042. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1043. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1044. end;
  1045. (*
  1046. { restore the CR if necessary from callers frame}
  1047. if target_info.abi = abi_powerpc_aix then
  1048. if false then { Not needed at the moment. }
  1049. begin
  1050. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1051. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1052. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1053. a_reg_dealloc(list,NR_R0);
  1054. end;
  1055. *)
  1056. end;
  1057. list.concat(taicpu.op_none(A_BLR));
  1058. end;
  1059. end;
  1060. function tcgppc.save_regs(list : TAsmList):longint;
  1061. {Generates code which saves used non-volatile registers in
  1062. the save area right below the address the stackpointer point to.
  1063. Returns the actual used save area size.}
  1064. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1065. usesfpr,usesgpr: boolean;
  1066. href : treference;
  1067. offset: aint;
  1068. regcounter2, firstfpureg: Tsuperregister;
  1069. begin
  1070. usesfpr:=false;
  1071. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1072. begin
  1073. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1074. case target_info.abi of
  1075. abi_powerpc_aix:
  1076. firstfpureg := RS_F14;
  1077. abi_powerpc_sysv:
  1078. firstfpureg := RS_F9;
  1079. else
  1080. internalerror(2003122903);
  1081. end;
  1082. for regcounter:=firstfpureg to RS_F31 do
  1083. begin
  1084. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1085. begin
  1086. usesfpr:=true;
  1087. firstregfpu:=regcounter;
  1088. break;
  1089. end;
  1090. end;
  1091. end;
  1092. usesgpr:=false;
  1093. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1094. for regcounter2:=RS_R13 to RS_R31 do
  1095. begin
  1096. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1097. begin
  1098. usesgpr:=true;
  1099. firstreggpr:=regcounter2;
  1100. break;
  1101. end;
  1102. end;
  1103. offset:= 0;
  1104. { save floating-point registers }
  1105. if usesfpr then
  1106. for regcounter := firstregfpu to RS_F31 do
  1107. begin
  1108. offset:= offset - 8;
  1109. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1110. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1111. end;
  1112. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1113. { save gprs in gpr save area }
  1114. if usesgpr then
  1115. if firstreggpr < RS_R30 then
  1116. begin
  1117. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1118. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1119. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1120. {STMW stores multiple registers}
  1121. end
  1122. else
  1123. begin
  1124. for regcounter := firstreggpr to RS_R31 do
  1125. begin
  1126. offset:= offset - 4;
  1127. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1128. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1129. end;
  1130. end;
  1131. { now comes the AltiVec context save, not yet implemented !!! }
  1132. save_regs:= -offset;
  1133. end;
  1134. procedure tcgppc.restore_regs(list : TAsmList);
  1135. {Generates code which restores used non-volatile registers from
  1136. the save area right below the address the stackpointer point to.}
  1137. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1138. usesfpr,usesgpr: boolean;
  1139. href : treference;
  1140. offset: integer;
  1141. regcounter2, firstfpureg: Tsuperregister;
  1142. begin
  1143. usesfpr:=false;
  1144. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1145. begin
  1146. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1147. case target_info.abi of
  1148. abi_powerpc_aix:
  1149. firstfpureg := RS_F14;
  1150. abi_powerpc_sysv:
  1151. firstfpureg := RS_F9;
  1152. else
  1153. internalerror(2003122903);
  1154. end;
  1155. for regcounter:=firstfpureg to RS_F31 do
  1156. begin
  1157. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1158. begin
  1159. usesfpr:=true;
  1160. firstregfpu:=regcounter;
  1161. break;
  1162. end;
  1163. end;
  1164. end;
  1165. usesgpr:=false;
  1166. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1167. for regcounter2:=RS_R13 to RS_R31 do
  1168. begin
  1169. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1170. begin
  1171. usesgpr:=true;
  1172. firstreggpr:=regcounter2;
  1173. break;
  1174. end;
  1175. end;
  1176. offset:= 0;
  1177. { restore fp registers }
  1178. if usesfpr then
  1179. for regcounter := firstregfpu to RS_F31 do
  1180. begin
  1181. offset:= offset - 8;
  1182. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1183. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1184. end;
  1185. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1186. { restore gprs }
  1187. if usesgpr then
  1188. if firstreggpr < RS_R30 then
  1189. begin
  1190. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1191. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1192. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1193. {LMW loads multiple registers}
  1194. end
  1195. else
  1196. begin
  1197. for regcounter := firstreggpr to RS_R31 do
  1198. begin
  1199. offset:= offset - 4;
  1200. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1201. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1202. end;
  1203. end;
  1204. { now comes the AltiVec context restore, not yet implemented !!! }
  1205. end;
  1206. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1207. (* NOT IN USE *)
  1208. { generated the entry code of a procedure/function. Note: localsize is the }
  1209. { sum of the size necessary for local variables and the maximum possible }
  1210. { combined size of ALL the parameters of a procedure called by the current }
  1211. { one }
  1212. const
  1213. macosLinkageAreaSize = 24;
  1214. var
  1215. href : treference;
  1216. registerSaveAreaSize : longint;
  1217. begin
  1218. if (localsize mod 8) <> 0 then
  1219. internalerror(58991);
  1220. { CR and LR only have to be saved in case they are modified by the current }
  1221. { procedure, but currently this isn't checked, so save them always }
  1222. { following is the entry code as described in "Altivec Programming }
  1223. { Interface Manual", bar the saving of AltiVec registers }
  1224. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1225. a_reg_alloc(list,NR_R0);
  1226. { save return address in callers frame}
  1227. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1228. { ... in caller's frame }
  1229. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1230. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1231. a_reg_dealloc(list,NR_R0);
  1232. { save non-volatile registers in callers frame}
  1233. registerSaveAreaSize:= save_regs(list);
  1234. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1235. a_reg_alloc(list,NR_R0);
  1236. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1237. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1238. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1239. a_reg_dealloc(list,NR_R0);
  1240. (*
  1241. { save pointer to incoming arguments }
  1242. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1243. *)
  1244. (*
  1245. a_reg_alloc(list,R_12);
  1246. { 0 or 8 based on SP alignment }
  1247. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1248. R_12,STACK_POINTER_REG,0,28,28));
  1249. { add in stack length }
  1250. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1251. -localsize));
  1252. { establish new alignment }
  1253. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1254. a_reg_dealloc(list,R_12);
  1255. *)
  1256. { allocate stack frame }
  1257. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1258. inc(localsize,tg.lasttemp);
  1259. localsize:=align(localsize,16);
  1260. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1261. if (localsize <> 0) then
  1262. begin
  1263. if (localsize <= high(smallint)) then
  1264. begin
  1265. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1266. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1267. end
  1268. else
  1269. begin
  1270. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1271. href.index := NR_R11;
  1272. a_reg_alloc(list,href.index);
  1273. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1274. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1275. a_reg_dealloc(list,href.index);
  1276. end;
  1277. end;
  1278. end;
  1279. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : aint);
  1280. (* NOT IN USE *)
  1281. var
  1282. href : treference;
  1283. begin
  1284. a_reg_alloc(list,NR_R0);
  1285. { restore stack pointer }
  1286. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1287. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1288. (*
  1289. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1290. *)
  1291. { restore the CR if necessary from callers frame
  1292. ( !!! always done currently ) }
  1293. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1294. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1295. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1296. a_reg_dealloc(list,NR_R0);
  1297. (*
  1298. { restore return address from callers frame }
  1299. reference_reset_base(href,STACK_POINTER_REG,8);
  1300. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1301. *)
  1302. { restore non-volatile registers from callers frame }
  1303. restore_regs(list);
  1304. (*
  1305. { return to caller }
  1306. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1307. list.concat(taicpu.op_none(A_BLR));
  1308. *)
  1309. { restore return address from callers frame }
  1310. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1311. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1312. { return to caller }
  1313. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1314. list.concat(taicpu.op_none(A_BLR));
  1315. end;
  1316. { ************* concatcopy ************ }
  1317. {$ifndef ppc603}
  1318. const
  1319. maxmoveunit = 8;
  1320. {$else ppc603}
  1321. const
  1322. maxmoveunit = 4;
  1323. {$endif ppc603}
  1324. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1325. var
  1326. countreg: TRegister;
  1327. src, dst: TReference;
  1328. lab: tasmlabel;
  1329. count, count2: aint;
  1330. size: tcgsize;
  1331. copyreg: tregister;
  1332. begin
  1333. {$ifdef extdebug}
  1334. if len > high(longint) then
  1335. internalerror(2002072704);
  1336. {$endif extdebug}
  1337. if (references_equal(source,dest)) then
  1338. exit;
  1339. { make sure short loads are handled as optimally as possible }
  1340. if (len <= maxmoveunit) and
  1341. (byte(len) in [1,2,4,8]) then
  1342. begin
  1343. if len < 8 then
  1344. begin
  1345. size := int_cgsize(len);
  1346. a_load_ref_ref(list,size,size,source,dest);
  1347. end
  1348. else
  1349. begin
  1350. copyreg := getfpuregister(list,OS_F64);
  1351. a_loadfpu_ref_reg(list,OS_F64,OS_F64,source,copyreg);
  1352. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dest);
  1353. end;
  1354. exit;
  1355. end;
  1356. count := len div maxmoveunit;
  1357. reference_reset(src);
  1358. reference_reset(dst);
  1359. { load the address of source into src.base }
  1360. if (count > 4) or
  1361. not issimpleref(source) or
  1362. ((source.index <> NR_NO) and
  1363. ((source.offset + longint(len)) > high(smallint))) then
  1364. begin
  1365. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1366. a_loadaddr_ref_reg(list,source,src.base);
  1367. end
  1368. else
  1369. begin
  1370. src := source;
  1371. end;
  1372. { load the address of dest into dst.base }
  1373. if (count > 4) or
  1374. not issimpleref(dest) or
  1375. ((dest.index <> NR_NO) and
  1376. ((dest.offset + longint(len)) > high(smallint))) then
  1377. begin
  1378. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1379. a_loadaddr_ref_reg(list,dest,dst.base);
  1380. end
  1381. else
  1382. begin
  1383. dst := dest;
  1384. end;
  1385. {$ifndef ppc603}
  1386. if count > 4 then
  1387. { generate a loop }
  1388. begin
  1389. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1390. { have to be set to 8. I put an Inc there so debugging may be }
  1391. { easier (should offset be different from zero here, it will be }
  1392. { easy to notice in the generated assembler }
  1393. inc(dst.offset,8);
  1394. inc(src.offset,8);
  1395. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1396. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1397. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1398. a_load_const_reg(list,OS_32,count,countreg);
  1399. copyreg := getfpuregister(list,OS_F64);
  1400. a_reg_sync(list,copyreg);
  1401. current_asmdata.getjumplabel(lab);
  1402. a_label(list, lab);
  1403. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1404. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1405. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1406. a_jmp(list,A_BC,C_NE,0,lab);
  1407. a_reg_sync(list,copyreg);
  1408. len := len mod 8;
  1409. end;
  1410. count := len div 8;
  1411. if count > 0 then
  1412. { unrolled loop }
  1413. begin
  1414. copyreg := getfpuregister(list,OS_F64);
  1415. for count2 := 1 to count do
  1416. begin
  1417. a_loadfpu_ref_reg(list,OS_F64,OS_F64,src,copyreg);
  1418. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dst);
  1419. inc(src.offset,8);
  1420. inc(dst.offset,8);
  1421. end;
  1422. len := len mod 8;
  1423. end;
  1424. if (len and 4) <> 0 then
  1425. begin
  1426. a_reg_alloc(list,NR_R0);
  1427. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1428. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1429. inc(src.offset,4);
  1430. inc(dst.offset,4);
  1431. a_reg_dealloc(list,NR_R0);
  1432. end;
  1433. {$else not ppc603}
  1434. if count > 4 then
  1435. { generate a loop }
  1436. begin
  1437. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1438. { have to be set to 4. I put an Inc there so debugging may be }
  1439. { easier (should offset be different from zero here, it will be }
  1440. { easy to notice in the generated assembler }
  1441. inc(dst.offset,4);
  1442. inc(src.offset,4);
  1443. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1444. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1445. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1446. a_load_const_reg(list,OS_32,count,countreg);
  1447. { explicitely allocate R_0 since it can be used safely here }
  1448. { (for holding date that's being copied) }
  1449. a_reg_alloc(list,NR_R0);
  1450. current_asmdata.getjumplabel(lab);
  1451. a_label(list, lab);
  1452. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1453. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1454. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1455. a_jmp(list,A_BC,C_NE,0,lab);
  1456. a_reg_dealloc(list,NR_R0);
  1457. len := len mod 4;
  1458. end;
  1459. count := len div 4;
  1460. if count > 0 then
  1461. { unrolled loop }
  1462. begin
  1463. a_reg_alloc(list,NR_R0);
  1464. for count2 := 1 to count do
  1465. begin
  1466. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1467. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1468. inc(src.offset,4);
  1469. inc(dst.offset,4);
  1470. end;
  1471. a_reg_dealloc(list,NR_R0);
  1472. len := len mod 4;
  1473. end;
  1474. {$endif not ppc603}
  1475. { copy the leftovers }
  1476. if (len and 2) <> 0 then
  1477. begin
  1478. a_reg_alloc(list,NR_R0);
  1479. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1480. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1481. inc(src.offset,2);
  1482. inc(dst.offset,2);
  1483. a_reg_dealloc(list,NR_R0);
  1484. end;
  1485. if (len and 1) <> 0 then
  1486. begin
  1487. a_reg_alloc(list,NR_R0);
  1488. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1489. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1490. a_reg_dealloc(list,NR_R0);
  1491. end;
  1492. end;
  1493. {***************** This is private property, keep out! :) *****************}
  1494. function tcgppc.issimpleref(const ref: treference): boolean;
  1495. begin
  1496. if (ref.base = NR_NO) and
  1497. (ref.index <> NR_NO) then
  1498. internalerror(200208101);
  1499. result :=
  1500. not(assigned(ref.symbol)) and
  1501. (((ref.index = NR_NO) and
  1502. (ref.offset >= low(smallint)) and
  1503. (ref.offset <= high(smallint))) or
  1504. ((ref.index <> NR_NO) and
  1505. (ref.offset = 0)));
  1506. end;
  1507. function tcgppc.fixref(list: TAsmList; var ref: treference): boolean;
  1508. var
  1509. tmpreg: tregister;
  1510. begin
  1511. result := false;
  1512. if (target_info.system = system_powerpc_darwin) and
  1513. assigned(ref.symbol) and
  1514. (ref.symbol.bind = AB_EXTERNAL) then
  1515. begin
  1516. tmpreg := g_indirect_sym_load(list,ref.symbol.name);
  1517. if (ref.base = NR_NO) then
  1518. ref.base := tmpreg
  1519. else if (ref.index = NR_NO) then
  1520. ref.index := tmpreg
  1521. else
  1522. begin
  1523. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1524. ref.base := tmpreg;
  1525. end;
  1526. ref.symbol := nil;
  1527. end;
  1528. if (ref.base = NR_NO) then
  1529. begin
  1530. ref.base := ref.index;
  1531. ref.index := NR_NO;
  1532. end;
  1533. if (ref.base <> NR_NO) then
  1534. begin
  1535. if (ref.index <> NR_NO) and
  1536. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1537. begin
  1538. result := true;
  1539. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1540. list.concat(taicpu.op_reg_reg_reg(
  1541. A_ADD,tmpreg,ref.base,ref.index));
  1542. ref.index := NR_NO;
  1543. ref.base := tmpreg;
  1544. end
  1545. end
  1546. else
  1547. if ref.index <> NR_NO then
  1548. internalerror(200208102);
  1549. end;
  1550. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1551. { that's the case, we can use rlwinm to do an AND operation }
  1552. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1553. var
  1554. temp : longint;
  1555. testbit : aint;
  1556. compare: boolean;
  1557. begin
  1558. get_rlwi_const := false;
  1559. if (a = 0) or (a = -1) then
  1560. exit;
  1561. { start with the lowest bit }
  1562. testbit := 1;
  1563. { check its value }
  1564. compare := boolean(a and testbit);
  1565. { find out how long the run of bits with this value is }
  1566. { (it's impossible that all bits are 1 or 0, because in that case }
  1567. { this function wouldn't have been called) }
  1568. l1 := 31;
  1569. while (((a and testbit) <> 0) = compare) do
  1570. begin
  1571. testbit := testbit shl 1;
  1572. dec(l1);
  1573. end;
  1574. { check the length of the run of bits that comes next }
  1575. compare := not compare;
  1576. l2 := l1;
  1577. while (((a and testbit) <> 0) = compare) and
  1578. (l2 >= 0) do
  1579. begin
  1580. testbit := testbit shl 1;
  1581. dec(l2);
  1582. end;
  1583. { and finally the check whether the rest of the bits all have the }
  1584. { same value }
  1585. compare := not compare;
  1586. temp := l2;
  1587. if temp >= 0 then
  1588. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1589. exit;
  1590. { we have done "not(not(compare))", so compare is back to its }
  1591. { initial value. If the lowest bit was 0, a is of the form }
  1592. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1593. { because l2 now contains the position of the last zero of the }
  1594. { first run instead of that of the first 1) so switch l1 and l2 }
  1595. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1596. if not compare then
  1597. begin
  1598. temp := l1;
  1599. l1 := l2+1;
  1600. l2 := temp;
  1601. end
  1602. else
  1603. { otherwise, l1 currently contains the position of the last }
  1604. { zero instead of that of the first 1 of the second run -> +1 }
  1605. inc(l1);
  1606. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1607. l1 := l1 and 31;
  1608. l2 := l2 and 31;
  1609. get_rlwi_const := true;
  1610. end;
  1611. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1612. begin
  1613. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1614. end;
  1615. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1616. begin
  1617. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1618. end;
  1619. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1620. begin
  1621. case op of
  1622. OP_AND,OP_OR,OP_XOR:
  1623. begin
  1624. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1625. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1626. end;
  1627. OP_ADD:
  1628. begin
  1629. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1630. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1631. end;
  1632. OP_SUB:
  1633. begin
  1634. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1635. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1636. end;
  1637. else
  1638. internalerror(2002072801);
  1639. end;
  1640. end;
  1641. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1642. const
  1643. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1644. (A_SUBIC,A_SUBC,A_ADDME));
  1645. var
  1646. tmpreg: tregister;
  1647. tmpreg64: tregister64;
  1648. issub: boolean;
  1649. begin
  1650. case op of
  1651. OP_AND,OP_OR,OP_XOR:
  1652. begin
  1653. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1654. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1655. regdst.reghi);
  1656. end;
  1657. OP_ADD, OP_SUB:
  1658. begin
  1659. if (value < 0) and
  1660. (value <> low(value)) then
  1661. begin
  1662. if op = OP_ADD then
  1663. op := OP_SUB
  1664. else
  1665. op := OP_ADD;
  1666. value := -value;
  1667. end;
  1668. if (longint(value) <> 0) then
  1669. begin
  1670. issub := op = OP_SUB;
  1671. if (value > 0) and
  1672. (value-ord(issub) <= 32767) then
  1673. begin
  1674. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1675. regdst.reglo,regsrc.reglo,longint(value)));
  1676. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1677. regdst.reghi,regsrc.reghi));
  1678. end
  1679. else if ((value shr 32) = 0) then
  1680. begin
  1681. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1682. cg.a_load_const_reg(list,OS_32,aint(value),tmpreg);
  1683. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1684. regdst.reglo,regsrc.reglo,tmpreg));
  1685. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1686. regdst.reghi,regsrc.reghi));
  1687. end
  1688. else
  1689. begin
  1690. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1691. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1692. a_load64_const_reg(list,value,tmpreg64);
  1693. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1694. end
  1695. end
  1696. else
  1697. begin
  1698. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1699. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1700. regdst.reghi);
  1701. end;
  1702. end;
  1703. else
  1704. internalerror(2002072802);
  1705. end;
  1706. end;
  1707. begin
  1708. cg := tcgppc.create;
  1709. cg64 :=tcg64fppc.create;
  1710. end.