cgcpu.pas 71 KB

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