cgcpu.pas 76 KB

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