cgcpu.pas 65 KB

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