cgcpu.pas 64 KB

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