cgcpu.pas 66 KB

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