cgcpu.pas 69 KB

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