cgcpu.pas 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: 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)))
  150. else
  151. list.concat(taicpu.op_sym(A_BL,current_asmdata.WeakRefAsmSymbol(s)));
  152. end
  153. else
  154. begin
  155. if not(weak) then
  156. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol('.'+s)))
  157. else
  158. list.concat(taicpu.op_sym(A_BL,current_asmdata.WeakRefAsmSymbol('.'+s)));
  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(current_asmdata.CurrAsmList, 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));
  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_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  601. var
  602. c: tasmcond;
  603. begin
  604. c := flags_to_cond(f);
  605. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  606. end;
  607. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; 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. testbit := ((f.cr-RS_CR0) * 4);
  615. case f.flag of
  616. F_EQ,F_NE:
  617. begin
  618. inc(testbit,2);
  619. bitvalue := f.flag = F_EQ;
  620. end;
  621. F_LT,F_GE:
  622. begin
  623. bitvalue := f.flag = F_LT;
  624. end;
  625. F_GT,F_LE:
  626. begin
  627. inc(testbit);
  628. bitvalue := f.flag = F_GT;
  629. end;
  630. else
  631. internalerror(200112261);
  632. end;
  633. { load the conditional register in the destination reg }
  634. list.concat(taicpu.op_reg(A_MFCR,reg));
  635. { we will move the bit that has to be tested to bit 0 by rotating }
  636. { left }
  637. testbit := (testbit + 1) and 31;
  638. { extract bit }
  639. list.concat(taicpu.op_reg_reg_const_const_const(
  640. A_RLWINM,reg,reg,testbit,31,31));
  641. { if we need the inverse, xor with 1 }
  642. if not bitvalue then
  643. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  644. end;
  645. (*
  646. procedure tcgppc.g_cond2reg(list: TAsmList; const f: TAsmCond; reg: TRegister);
  647. var
  648. testbit: byte;
  649. bitvalue: boolean;
  650. begin
  651. { get the bit to extract from the conditional register + its }
  652. { requested value (0 or 1) }
  653. case f.simple of
  654. false:
  655. begin
  656. { we don't generate this in the compiler }
  657. internalerror(200109062);
  658. end;
  659. true:
  660. case f.cond of
  661. C_None:
  662. internalerror(200109063);
  663. C_LT..C_NU:
  664. begin
  665. testbit := (ord(f.cr) - ord(R_CR0))*4;
  666. inc(testbit,AsmCondFlag2BI[f.cond]);
  667. bitvalue := AsmCondFlagTF[f.cond];
  668. end;
  669. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  670. begin
  671. testbit := f.crbit
  672. bitvalue := AsmCondFlagTF[f.cond];
  673. end;
  674. else
  675. internalerror(200109064);
  676. end;
  677. end;
  678. { load the conditional register in the destination reg }
  679. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  680. { we will move the bit that has to be tested to bit 31 -> rotate }
  681. { left by bitpos+1 (remember, this is big-endian!) }
  682. if bitpos <> 31 then
  683. inc(bitpos)
  684. else
  685. bitpos := 0;
  686. { extract bit }
  687. list.concat(taicpu.op_reg_reg_const_const_const(
  688. A_RLWINM,reg,reg,bitpos,31,31));
  689. { if we need the inverse, xor with 1 }
  690. if not bitvalue then
  691. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  692. end;
  693. *)
  694. { *********** entry/exit code and address loading ************ }
  695. procedure tcgppc.g_save_registers(list:TAsmList);
  696. begin
  697. { this work is done in g_proc_entry }
  698. end;
  699. procedure tcgppc.g_restore_registers(list:TAsmList);
  700. begin
  701. { this work is done in g_proc_exit }
  702. end;
  703. procedure tcgppc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  704. { generated the entry code of a procedure/function. Note: localsize is the }
  705. { sum of the size necessary for local variables and the maximum possible }
  706. { combined size of ALL the parameters of a procedure called by the current }
  707. { one. }
  708. { This procedure may be called before, as well as after g_return_from_proc }
  709. { is called. NOTE registers are not to be allocated through the register }
  710. { allocator here, because the register colouring has already occured !! }
  711. var regcounter,firstregfpu,firstregint: TSuperRegister;
  712. href : treference;
  713. usesfpr,usesgpr : boolean;
  714. begin
  715. { CR and LR only have to be saved in case they are modified by the current }
  716. { procedure, but currently this isn't checked, so save them always }
  717. { following is the entry code as described in "Altivec Programming }
  718. { Interface Manual", bar the saving of AltiVec registers }
  719. a_reg_alloc(list,NR_STACK_POINTER_REG);
  720. usesgpr := false;
  721. usesfpr := false;
  722. if not(po_assembler in current_procinfo.procdef.procoptions) then
  723. begin
  724. { save link register? }
  725. if save_lr_in_prologue then
  726. begin
  727. a_reg_alloc(list,NR_R0);
  728. { save return address... }
  729. { warning: if this is no longer done via r0, or if r0 is }
  730. { added to the usable registers, adapt tcgppcgen.g_profilecode }
  731. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  732. { ... in caller's frame }
  733. case target_info.abi of
  734. abi_powerpc_aix:
  735. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX,4);
  736. abi_powerpc_sysv:
  737. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV,4);
  738. end;
  739. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  740. if not(cs_profile in current_settings.moduleswitches) then
  741. a_reg_dealloc(list,NR_R0);
  742. end;
  743. (*
  744. { save the CR if necessary in callers frame. }
  745. if target_info.abi = abi_powerpc_aix then
  746. if false then { Not needed at the moment. }
  747. begin
  748. a_reg_alloc(list,NR_R0);
  749. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  750. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  751. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  752. a_reg_dealloc(list,NR_R0);
  753. end;
  754. *)
  755. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  756. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  757. usesgpr := firstregint <> 32;
  758. usesfpr := firstregfpu <> 32;
  759. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  760. begin
  761. a_reg_alloc(list,NR_R12);
  762. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  763. end;
  764. end;
  765. if usesfpr then
  766. begin
  767. reference_reset_base(href,NR_R1,-8,8);
  768. for regcounter:=firstregfpu to RS_F31 do
  769. begin
  770. a_loadfpu_reg_ref(list,OS_F64,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  771. dec(href.offset,8);
  772. end;
  773. { compute start of gpr save area }
  774. inc(href.offset,4);
  775. end
  776. else
  777. { compute start of gpr save area }
  778. reference_reset_base(href,NR_R1,-4,4);
  779. { save gprs and fetch GOT pointer }
  780. if usesgpr then
  781. begin
  782. if (firstregint <= RS_R22) or
  783. ((cs_opt_size in current_settings.optimizerswitches) and
  784. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  785. (firstregint <= RS_R29)) then
  786. begin
  787. { TODO: TODO: 64 bit support }
  788. dec(href.offset,(RS_R31-firstregint)*sizeof(pint));
  789. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  790. end
  791. else
  792. for regcounter:=firstregint to RS_R31 do
  793. begin
  794. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  795. dec(href.offset,4);
  796. end;
  797. end;
  798. { done in ncgutil because it may only be released after the parameters }
  799. { have been moved to their final resting place }
  800. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  801. { a_reg_dealloc(list,NR_R12); }
  802. if (not nostackframe) and
  803. tppcprocinfo(current_procinfo).needstackframe and
  804. (localsize <> 0) then
  805. begin
  806. if (localsize <= high(smallint)) then
  807. begin
  808. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize,8);
  809. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  810. end
  811. else
  812. begin
  813. reference_reset_base(href,NR_STACK_POINTER_REG,0,4);
  814. { can't use getregisterint here, the register colouring }
  815. { is already done when we get here }
  816. { R12 may hold previous stack pointer, R11 may be in }
  817. { use as got => use R0 (but then we can't use }
  818. { a_load_const_reg) }
  819. href.index := NR_R0;
  820. a_reg_alloc(list,href.index);
  821. list.concat(taicpu.op_reg_const(A_LI,NR_R0,smallint((-localsize) and $ffff)));
  822. if (smallint((-localsize) and $ffff) < 0) then
  823. { upper 16 bits are now $ffff -> xor with inverse }
  824. list.concat(taicpu.op_reg_reg_const(A_XORIS,NR_R0,NR_R0,word(not(((-localsize) shr 16) and $ffff))))
  825. else
  826. list.concat(taicpu.op_reg_reg_const(A_ORIS,NR_R0,NR_R0,word(((-localsize) shr 16) and $ffff)));
  827. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  828. a_reg_dealloc(list,href.index);
  829. end;
  830. end;
  831. { save the CR if necessary ( !!! never done currently ) }
  832. { still need to find out where this has to be done for SystemV
  833. a_reg_alloc(list,R_0);
  834. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  835. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  836. new_reference(STACK_POINTER_REG,LA_CR)));
  837. a_reg_dealloc(list,R_0);
  838. }
  839. { now comes the AltiVec context save, not yet implemented !!! }
  840. end;
  841. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  842. { This procedure may be called before, as well as after g_stackframe_entry }
  843. { is called. NOTE registers are not to be allocated through the register }
  844. { allocator here, because the register colouring has already occured !! }
  845. var
  846. regcounter,firstregfpu,firstregint: TsuperRegister;
  847. href : treference;
  848. usesfpr,usesgpr,genret : boolean;
  849. localsize: tcgint;
  850. begin
  851. { AltiVec context restore, not yet implemented !!! }
  852. usesfpr:=false;
  853. usesgpr:=false;
  854. if not (po_assembler in current_procinfo.procdef.procoptions) then
  855. begin
  856. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  857. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  858. usesgpr := firstregint <> 32;
  859. usesfpr := firstregfpu <> 32;
  860. end;
  861. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  862. { adjust r1 }
  863. { (register allocator is no longer valid at this time and an add of 0 }
  864. { is translated into a move, which is then registered with the register }
  865. { allocator, causing a crash }
  866. if (not nostackframe) and
  867. tppcprocinfo(current_procinfo).needstackframe and
  868. (localsize <> 0) then
  869. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  870. { no return (blr) generated yet }
  871. genret:=true;
  872. if usesfpr then
  873. begin
  874. reference_reset_base(href,NR_R1,-8,8);
  875. for regcounter := firstregfpu to RS_F31 do
  876. begin
  877. a_loadfpu_ref_reg(list,OS_F64,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  878. dec(href.offset,8);
  879. end;
  880. inc(href.offset,4);
  881. end
  882. else
  883. reference_reset_base(href,NR_R1,-4,4);
  884. if (usesgpr) then
  885. begin
  886. if (firstregint <= RS_R22) or
  887. ((cs_opt_size in current_settings.optimizerswitches) and
  888. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  889. (firstregint <= RS_R29)) then
  890. begin
  891. { TODO: TODO: 64 bit support }
  892. dec(href.offset,(RS_R31-firstregint)*sizeof(pint));
  893. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  894. end
  895. else
  896. for regcounter:=firstregint to RS_R31 do
  897. begin
  898. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  899. dec(href.offset,4);
  900. end;
  901. end;
  902. (*
  903. { restore fprs and return }
  904. if usesfpr then
  905. begin
  906. { address of fpr save area to r11 }
  907. r:=NR_R12;
  908. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  909. {
  910. if (pi_do_call in current_procinfo.flags) then
  911. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x'))
  912. else
  913. { leaf node => lr haven't to be restored }
  914. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  915. genret:=false;
  916. }
  917. end;
  918. *)
  919. { if we didn't generate the return code, we've to do it now }
  920. if genret then
  921. begin
  922. { load link register? }
  923. if not (po_assembler in current_procinfo.procdef.procoptions) then
  924. begin
  925. if (pi_do_call in current_procinfo.flags) then
  926. begin
  927. case target_info.abi of
  928. abi_powerpc_aix:
  929. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX,4);
  930. abi_powerpc_sysv:
  931. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV,4);
  932. end;
  933. a_reg_alloc(list,NR_R0);
  934. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  935. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  936. a_reg_dealloc(list,NR_R0);
  937. end;
  938. (*
  939. { restore the CR if necessary from callers frame}
  940. if target_info.abi = abi_powerpc_aix then
  941. if false then { Not needed at the moment. }
  942. begin
  943. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  944. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  945. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  946. a_reg_dealloc(list,NR_R0);
  947. end;
  948. *)
  949. end;
  950. list.concat(taicpu.op_none(A_BLR));
  951. end;
  952. end;
  953. function tcgppc.save_regs(list : TAsmList):longint;
  954. {Generates code which saves used non-volatile registers in
  955. the save area right below the address the stackpointer point to.
  956. Returns the actual used save area size.}
  957. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  958. usesfpr,usesgpr: boolean;
  959. href : treference;
  960. offset: tcgint;
  961. regcounter2, firstfpureg: Tsuperregister;
  962. begin
  963. usesfpr:=false;
  964. if not (po_assembler in current_procinfo.procdef.procoptions) then
  965. begin
  966. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  967. case target_info.abi of
  968. abi_powerpc_aix:
  969. firstfpureg := RS_F14;
  970. abi_powerpc_sysv:
  971. firstfpureg := RS_F9;
  972. else
  973. internalerror(2003122903);
  974. end;
  975. for regcounter:=firstfpureg to RS_F31 do
  976. begin
  977. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  978. begin
  979. usesfpr:=true;
  980. firstregfpu:=regcounter;
  981. break;
  982. end;
  983. end;
  984. end;
  985. usesgpr:=false;
  986. if not (po_assembler in current_procinfo.procdef.procoptions) then
  987. for regcounter2:=RS_R13 to RS_R31 do
  988. begin
  989. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  990. begin
  991. usesgpr:=true;
  992. firstreggpr:=regcounter2;
  993. break;
  994. end;
  995. end;
  996. offset:= 0;
  997. { save floating-point registers }
  998. if usesfpr then
  999. for regcounter := firstregfpu to RS_F31 do
  1000. begin
  1001. offset:= offset - 8;
  1002. reference_reset_base(href, NR_STACK_POINTER_REG, offset, 8);
  1003. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1004. end;
  1005. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1006. { save gprs in gpr save area }
  1007. if usesgpr then
  1008. if firstreggpr < RS_R30 then
  1009. begin
  1010. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1011. reference_reset_base(href,NR_STACK_POINTER_REG,offset,4);
  1012. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1013. {STMW stores multiple registers}
  1014. end
  1015. else
  1016. begin
  1017. for regcounter := firstreggpr to RS_R31 do
  1018. begin
  1019. offset:= offset - 4;
  1020. reference_reset_base(href, NR_STACK_POINTER_REG, offset, 4);
  1021. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1022. end;
  1023. end;
  1024. { now comes the AltiVec context save, not yet implemented !!! }
  1025. save_regs:= -offset;
  1026. end;
  1027. procedure tcgppc.restore_regs(list : TAsmList);
  1028. {Generates code which restores used non-volatile registers from
  1029. the save area right below the address the stackpointer point to.}
  1030. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1031. usesfpr,usesgpr: boolean;
  1032. href : treference;
  1033. offset: integer;
  1034. regcounter2, firstfpureg: Tsuperregister;
  1035. begin
  1036. usesfpr:=false;
  1037. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1038. begin
  1039. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1040. case target_info.abi of
  1041. abi_powerpc_aix:
  1042. firstfpureg := RS_F14;
  1043. abi_powerpc_sysv:
  1044. firstfpureg := RS_F9;
  1045. else
  1046. internalerror(2003122903);
  1047. end;
  1048. for regcounter:=firstfpureg to RS_F31 do
  1049. begin
  1050. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1051. begin
  1052. usesfpr:=true;
  1053. firstregfpu:=regcounter;
  1054. break;
  1055. end;
  1056. end;
  1057. end;
  1058. usesgpr:=false;
  1059. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1060. for regcounter2:=RS_R13 to RS_R31 do
  1061. begin
  1062. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1063. begin
  1064. usesgpr:=true;
  1065. firstreggpr:=regcounter2;
  1066. break;
  1067. end;
  1068. end;
  1069. offset:= 0;
  1070. { restore fp registers }
  1071. if usesfpr then
  1072. for regcounter := firstregfpu to RS_F31 do
  1073. begin
  1074. offset:= offset - 8;
  1075. reference_reset_base(href, NR_STACK_POINTER_REG, offset, 8);
  1076. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1077. end;
  1078. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1079. { restore gprs }
  1080. if usesgpr then
  1081. if firstreggpr < RS_R30 then
  1082. begin
  1083. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1084. reference_reset_base(href,NR_STACK_POINTER_REG,offset, 4); //-220
  1085. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1086. {LMW loads multiple registers}
  1087. end
  1088. else
  1089. begin
  1090. for regcounter := firstreggpr to RS_R31 do
  1091. begin
  1092. offset:= offset - 4;
  1093. reference_reset_base(href, NR_STACK_POINTER_REG, offset, 4);
  1094. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1095. end;
  1096. end;
  1097. { now comes the AltiVec context restore, not yet implemented !!! }
  1098. end;
  1099. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1100. (* NOT IN USE *)
  1101. { generated the entry code of a procedure/function. Note: localsize is the }
  1102. { sum of the size necessary for local variables and the maximum possible }
  1103. { combined size of ALL the parameters of a procedure called by the current }
  1104. { one }
  1105. const
  1106. macosLinkageAreaSize = 24;
  1107. var
  1108. href : treference;
  1109. registerSaveAreaSize : longint;
  1110. begin
  1111. if (localsize mod 8) <> 0 then
  1112. internalerror(58991);
  1113. { CR and LR only have to be saved in case they are modified by the current }
  1114. { procedure, but currently this isn't checked, so save them always }
  1115. { following is the entry code as described in "Altivec Programming }
  1116. { Interface Manual", bar the saving of AltiVec registers }
  1117. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1118. a_reg_alloc(list,NR_R0);
  1119. { save return address in callers frame}
  1120. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1121. { ... in caller's frame }
  1122. reference_reset_base(href,NR_STACK_POINTER_REG,8, 8);
  1123. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1124. a_reg_dealloc(list,NR_R0);
  1125. { save non-volatile registers in callers frame}
  1126. registerSaveAreaSize:= save_regs(list);
  1127. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1128. a_reg_alloc(list,NR_R0);
  1129. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1130. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX,4);
  1131. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1132. a_reg_dealloc(list,NR_R0);
  1133. (*
  1134. { save pointer to incoming arguments }
  1135. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1136. *)
  1137. (*
  1138. a_reg_alloc(list,R_12);
  1139. { 0 or 8 based on SP alignment }
  1140. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1141. R_12,STACK_POINTER_REG,0,28,28));
  1142. { add in stack length }
  1143. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1144. -localsize));
  1145. { establish new alignment }
  1146. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1147. a_reg_dealloc(list,R_12);
  1148. *)
  1149. { allocate stack frame }
  1150. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1151. inc(localsize,tg.lasttemp);
  1152. localsize:=align(localsize,16);
  1153. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1154. if (localsize <> 0) then
  1155. begin
  1156. if (localsize <= high(smallint)) then
  1157. begin
  1158. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize,8);
  1159. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1160. end
  1161. else
  1162. begin
  1163. reference_reset_base(href,NR_STACK_POINTER_REG,0,8);
  1164. href.index := NR_R11;
  1165. a_reg_alloc(list,href.index);
  1166. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1167. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1168. a_reg_dealloc(list,href.index);
  1169. end;
  1170. end;
  1171. end;
  1172. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : tcgint);
  1173. (* NOT IN USE *)
  1174. var
  1175. href : treference;
  1176. begin
  1177. a_reg_alloc(list,NR_R0);
  1178. { restore stack pointer }
  1179. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP,4);
  1180. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1181. (*
  1182. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1183. *)
  1184. { restore the CR if necessary from callers frame
  1185. ( !!! always done currently ) }
  1186. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX,4);
  1187. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1188. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1189. a_reg_dealloc(list,NR_R0);
  1190. (*
  1191. { restore return address from callers frame }
  1192. reference_reset_base(href,STACK_POINTER_REG,8);
  1193. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1194. *)
  1195. { restore non-volatile registers from callers frame }
  1196. restore_regs(list);
  1197. (*
  1198. { return to caller }
  1199. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1200. list.concat(taicpu.op_none(A_BLR));
  1201. *)
  1202. { restore return address from callers frame }
  1203. reference_reset_base(href,NR_STACK_POINTER_REG,8,8);
  1204. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1205. { return to caller }
  1206. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1207. list.concat(taicpu.op_none(A_BLR));
  1208. end;
  1209. { ************* concatcopy ************ }
  1210. {$ifdef use8byteconcatcopy}
  1211. const
  1212. maxmoveunit = 8;
  1213. {$else use8byteconcatcopy}
  1214. const
  1215. maxmoveunit = 4;
  1216. {$endif use8byteconcatcopy}
  1217. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1218. var
  1219. countreg: TRegister;
  1220. src, dst: TReference;
  1221. lab: tasmlabel;
  1222. count, count2: aint;
  1223. size: tcgsize;
  1224. copyreg: tregister;
  1225. begin
  1226. {$ifdef extdebug}
  1227. if len > high(longint) then
  1228. internalerror(2002072704);
  1229. {$endif extdebug}
  1230. if (references_equal(source,dest)) then
  1231. exit;
  1232. { make sure short loads are handled as optimally as possible }
  1233. if (len <= maxmoveunit) and
  1234. (byte(len) in [1,2,4,8]) then
  1235. begin
  1236. if len < 8 then
  1237. begin
  1238. size := int_cgsize(len);
  1239. a_load_ref_ref(list,size,size,source,dest);
  1240. end
  1241. else
  1242. begin
  1243. copyreg := getfpuregister(list,OS_F64);
  1244. a_loadfpu_ref_reg(list,OS_F64,OS_F64,source,copyreg);
  1245. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dest);
  1246. end;
  1247. exit;
  1248. end;
  1249. count := len div maxmoveunit;
  1250. reference_reset(src,source.alignment);
  1251. reference_reset(dst,dest.alignment);
  1252. { load the address of source into src.base }
  1253. if (count > 4) or
  1254. not issimpleref(source) or
  1255. ((source.index <> NR_NO) and
  1256. ((source.offset + longint(len)) > high(smallint))) then
  1257. begin
  1258. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1259. a_loadaddr_ref_reg(list,source,src.base);
  1260. end
  1261. else
  1262. begin
  1263. src := source;
  1264. end;
  1265. { load the address of dest into dst.base }
  1266. if (count > 4) or
  1267. not issimpleref(dest) or
  1268. ((dest.index <> NR_NO) and
  1269. ((dest.offset + longint(len)) > high(smallint))) then
  1270. begin
  1271. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1272. a_loadaddr_ref_reg(list,dest,dst.base);
  1273. end
  1274. else
  1275. begin
  1276. dst := dest;
  1277. end;
  1278. {$ifdef use8byteconcatcopy}
  1279. if count > 4 then
  1280. { generate a loop }
  1281. begin
  1282. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1283. { have to be set to 8. I put an Inc there so debugging may be }
  1284. { easier (should offset be different from zero here, it will be }
  1285. { easy to notice in the generated assembler }
  1286. inc(dst.offset,8);
  1287. inc(src.offset,8);
  1288. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1289. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1290. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1291. a_load_const_reg(list,OS_32,count,countreg);
  1292. copyreg := getfpuregister(list,OS_F64);
  1293. a_reg_sync(list,copyreg);
  1294. current_asmdata.getjumplabel(lab);
  1295. a_label(list, lab);
  1296. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1297. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1298. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1299. a_jmp(list,A_BC,C_NE,0,lab);
  1300. a_reg_sync(list,copyreg);
  1301. len := len mod 8;
  1302. end;
  1303. count := len div 8;
  1304. if count > 0 then
  1305. { unrolled loop }
  1306. begin
  1307. copyreg := getfpuregister(list,OS_F64);
  1308. for count2 := 1 to count do
  1309. begin
  1310. a_loadfpu_ref_reg(list,OS_F64,OS_F64,src,copyreg);
  1311. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dst);
  1312. inc(src.offset,8);
  1313. inc(dst.offset,8);
  1314. end;
  1315. len := len mod 8;
  1316. end;
  1317. if (len and 4) <> 0 then
  1318. begin
  1319. a_reg_alloc(list,NR_R0);
  1320. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1321. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1322. inc(src.offset,4);
  1323. inc(dst.offset,4);
  1324. a_reg_dealloc(list,NR_R0);
  1325. end;
  1326. {$else use8byteconcatcopy}
  1327. if count > 4 then
  1328. { generate a loop }
  1329. begin
  1330. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1331. { have to be set to 4. I put an Inc there so debugging may be }
  1332. { easier (should offset be different from zero here, it will be }
  1333. { easy to notice in the generated assembler }
  1334. inc(dst.offset,4);
  1335. inc(src.offset,4);
  1336. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1337. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1338. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1339. a_load_const_reg(list,OS_32,count,countreg);
  1340. { explicitely allocate R_0 since it can be used safely here }
  1341. { (for holding date that's being copied) }
  1342. a_reg_alloc(list,NR_R0);
  1343. current_asmdata.getjumplabel(lab);
  1344. a_label(list, lab);
  1345. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1346. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1347. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1348. a_jmp(list,A_BC,C_NE,0,lab);
  1349. a_reg_dealloc(list,NR_R0);
  1350. len := len mod 4;
  1351. end;
  1352. count := len div 4;
  1353. if count > 0 then
  1354. { unrolled loop }
  1355. begin
  1356. a_reg_alloc(list,NR_R0);
  1357. for count2 := 1 to count do
  1358. begin
  1359. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1360. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1361. inc(src.offset,4);
  1362. inc(dst.offset,4);
  1363. end;
  1364. a_reg_dealloc(list,NR_R0);
  1365. len := len mod 4;
  1366. end;
  1367. {$endif use8byteconcatcopy}
  1368. { copy the leftovers }
  1369. if (len and 2) <> 0 then
  1370. begin
  1371. a_reg_alloc(list,NR_R0);
  1372. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1373. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1374. inc(src.offset,2);
  1375. inc(dst.offset,2);
  1376. a_reg_dealloc(list,NR_R0);
  1377. end;
  1378. if (len and 1) <> 0 then
  1379. begin
  1380. a_reg_alloc(list,NR_R0);
  1381. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1382. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1383. a_reg_dealloc(list,NR_R0);
  1384. end;
  1385. end;
  1386. {***************** This is private property, keep out! :) *****************}
  1387. function tcgppc.issimpleref(const ref: treference): boolean;
  1388. begin
  1389. if (ref.base = NR_NO) and
  1390. (ref.index <> NR_NO) then
  1391. internalerror(200208101);
  1392. result :=
  1393. not(assigned(ref.symbol)) and
  1394. (((ref.index = NR_NO) and
  1395. (ref.offset >= low(smallint)) and
  1396. (ref.offset <= high(smallint))) or
  1397. ((ref.index <> NR_NO) and
  1398. (ref.offset = 0)));
  1399. end;
  1400. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1401. { that's the case, we can use rlwinm to do an AND operation }
  1402. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1403. var
  1404. temp : longint;
  1405. testbit : aint;
  1406. compare: boolean;
  1407. begin
  1408. get_rlwi_const := false;
  1409. if (a = 0) or (a = -1) then
  1410. exit;
  1411. { start with the lowest bit }
  1412. testbit := 1;
  1413. { check its value }
  1414. compare := boolean(a and testbit);
  1415. { find out how long the run of bits with this value is }
  1416. { (it's impossible that all bits are 1 or 0, because in that case }
  1417. { this function wouldn't have been called) }
  1418. l1 := 31;
  1419. while (((a and testbit) <> 0) = compare) do
  1420. begin
  1421. testbit := testbit shl 1;
  1422. dec(l1);
  1423. end;
  1424. { check the length of the run of bits that comes next }
  1425. compare := not compare;
  1426. l2 := l1;
  1427. while (((a and testbit) <> 0) = compare) and
  1428. (l2 >= 0) do
  1429. begin
  1430. testbit := testbit shl 1;
  1431. dec(l2);
  1432. end;
  1433. { and finally the check whether the rest of the bits all have the }
  1434. { same value }
  1435. compare := not compare;
  1436. temp := l2;
  1437. if temp >= 0 then
  1438. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1439. exit;
  1440. { we have done "not(not(compare))", so compare is back to its }
  1441. { initial value. If the lowest bit was 0, a is of the form }
  1442. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1443. { because l2 now contains the position of the last zero of the }
  1444. { first run instead of that of the first 1) so switch l1 and l2 }
  1445. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1446. if not compare then
  1447. begin
  1448. temp := l1;
  1449. l1 := l2+1;
  1450. l2 := temp;
  1451. end
  1452. else
  1453. { otherwise, l1 currently contains the position of the last }
  1454. { zero instead of that of the first 1 of the second run -> +1 }
  1455. inc(l1);
  1456. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1457. l1 := l1 and 31;
  1458. l2 := l2 and 31;
  1459. get_rlwi_const := true;
  1460. end;
  1461. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1462. begin
  1463. case op of
  1464. OP_NOT:
  1465. begin
  1466. cg.a_op_reg_reg(list,OP_NOT,OS_32,regsrc.reglo,regdst.reglo);
  1467. cg.a_op_reg_reg(list,OP_NOT,OS_32,regsrc.reghi,regdst.reghi);
  1468. end;
  1469. OP_NEG:
  1470. begin
  1471. list.concat(taicpu.op_reg_reg_const(a_subfic,regdst.reglo,regsrc.reglo,0));
  1472. list.concat(taicpu.op_reg_reg(a_subfze,regdst.reghi,regsrc.reghi));
  1473. end;
  1474. else
  1475. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1476. end;
  1477. end;
  1478. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1479. begin
  1480. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1481. end;
  1482. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1483. begin
  1484. case op of
  1485. OP_AND,OP_OR,OP_XOR:
  1486. begin
  1487. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1488. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1489. end;
  1490. OP_ADD:
  1491. begin
  1492. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1493. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1494. end;
  1495. OP_SUB:
  1496. begin
  1497. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1498. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1499. end;
  1500. else
  1501. internalerror(2002072801);
  1502. end;
  1503. end;
  1504. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1505. const
  1506. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1507. (A_SUBIC,A_SUBC,A_ADDME));
  1508. var
  1509. tmpreg: tregister;
  1510. tmpreg64: tregister64;
  1511. issub: boolean;
  1512. begin
  1513. case op of
  1514. OP_AND,OP_OR,OP_XOR:
  1515. begin
  1516. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1517. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1518. regdst.reghi);
  1519. end;
  1520. OP_ADD, OP_SUB:
  1521. begin
  1522. if (value < 0) and
  1523. (value <> low(value)) then
  1524. begin
  1525. if op = OP_ADD then
  1526. op := OP_SUB
  1527. else
  1528. op := OP_ADD;
  1529. value := -value;
  1530. end;
  1531. if (longint(value) <> 0) then
  1532. begin
  1533. issub := op = OP_SUB;
  1534. if (value > 0) and
  1535. (value-ord(issub) <= 32767) then
  1536. begin
  1537. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1538. regdst.reglo,regsrc.reglo,longint(value)));
  1539. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1540. regdst.reghi,regsrc.reghi));
  1541. end
  1542. else if ((value shr 32) = 0) then
  1543. begin
  1544. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1545. cg.a_load_const_reg(list,OS_32,aint(value),tmpreg);
  1546. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1547. regdst.reglo,regsrc.reglo,tmpreg));
  1548. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1549. regdst.reghi,regsrc.reghi));
  1550. end
  1551. else
  1552. begin
  1553. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1554. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1555. a_load64_const_reg(list,value,tmpreg64);
  1556. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1557. end
  1558. end
  1559. else
  1560. begin
  1561. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1562. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1563. regdst.reghi);
  1564. end;
  1565. end;
  1566. else
  1567. internalerror(2002072802);
  1568. end;
  1569. end;
  1570. procedure create_codegen;
  1571. begin
  1572. cg := tcgppc.create;
  1573. cg64 :=tcg64fppc.create;
  1574. end;
  1575. end.