2
0

cgcpu.pas 68 KB

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