cgcpu.pas 67 KB

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