cgcpu.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,cpuinfo,node,cg64f32,cginfo;
  25. type
  26. tcgppc = class(tcg)
  27. { passing parameters, per default the parameter is pushed }
  28. { nr gives the number of the parameter (enumerated from }
  29. { left to right), this allows to move the parameter to }
  30. { register, if the cpu supports register calling }
  31. { conventions }
  32. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  33. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  34. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  35. procedure a_call_name(list : taasmoutput;const s : string);override;
  36. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  37. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  38. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  39. size: tcgsize; a: aword; src, dst: tregister); override;
  40. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  41. size: tcgsize; src1, src2, dst: tregister); override;
  42. { move instructions }
  43. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  44. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  45. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  46. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);override;
  47. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister); override;
  48. { fpu move instructions }
  49. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  50. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  51. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  52. { comparison operations }
  53. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  54. l : tasmlabel);override;
  55. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  56. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: taasmoutput; const f: TResFlags; reg: TRegister); override;
  59. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  60. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  61. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  62. procedure g_restore_frame_pointer(list : taasmoutput);override;
  63. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  64. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  65. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  66. procedure g_overflowcheck(list: taasmoutput; const p: tnode); override;
  67. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  68. { that's the case, we can use rlwinm to do an AND operation }
  69. function get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  70. private
  71. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  72. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  73. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  74. { Make sure ref is a valid reference for the PowerPC and sets the }
  75. { base to the value of the index if (base = R_NO). }
  76. procedure fixref(list: taasmoutput; var ref: treference);
  77. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  78. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  79. ref: treference);
  80. { creates the correct branch instruction for a given combination }
  81. { of asmcondflags and destination addressing mode }
  82. procedure a_jmp(list: taasmoutput; op: tasmop;
  83. c: tasmcondflag; crval: longint; l: tasmlabel);
  84. end;
  85. tcg64fppc = class(tcg64f32)
  86. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  87. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  88. end;
  89. const
  90. {
  91. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIVWU,
  92. A_DIVW,A_MULLW, A_MULLW, A_NEG,A_NOT,A_OR,
  93. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  94. }
  95. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  96. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  97. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  98. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  99. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  100. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  101. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  102. C_LT,C_GE,C_LE,C_NE,C_LE,C_NG,C_GE,C_NL);
  103. implementation
  104. uses
  105. globtype,globals,verbose,systems,cutils,symconst,symdef,rgobj;
  106. { parameter passing... Still needs extra support from the processor }
  107. { independent code generator }
  108. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  109. var
  110. ref: treference;
  111. begin
  112. case locpara.loc of
  113. LOC_REGISTER:
  114. a_load_const_reg(list,size,a,locpara.register);
  115. LOC_REFERENCE:
  116. begin
  117. reference_reset(ref);
  118. ref.base:=locpara.reference.index;
  119. ref.offset:=locpara.reference.offset;
  120. a_load_const_ref(list,size,a,ref);
  121. end;
  122. else
  123. internalerror(2002081101);
  124. end;
  125. if locpara.sp_fixup<>0 then
  126. internalerror(2002081102);
  127. end;
  128. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  129. var
  130. ref: treference;
  131. tmpreg: tregister;
  132. begin
  133. case locpara.loc of
  134. LOC_REGISTER:
  135. a_load_ref_reg(list,size,r,locpara.register);
  136. LOC_REFERENCE:
  137. begin
  138. reference_reset(ref);
  139. ref.base:=locpara.reference.index;
  140. ref.offset:=locpara.reference.offset;
  141. tmpreg := get_scratch_reg_int(list);
  142. a_load_ref_reg(list,size,r,tmpreg);
  143. a_load_reg_ref(list,size,tmpreg,ref);
  144. free_scratch_reg(list,tmpreg);
  145. end;
  146. else
  147. internalerror(2002081103);
  148. end;
  149. if locpara.sp_fixup<>0 then
  150. internalerror(2002081104);
  151. end;
  152. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  153. var
  154. ref: treference;
  155. tmpreg: tregister;
  156. begin
  157. {$ifdef para_sizes_known}
  158. if (nr <= max_param_regs_int) then
  159. a_loadaddr_ref_reg(list,size,r,param_regs_int[nr])
  160. else
  161. begin
  162. reset_reference(ref);
  163. ref.base := STACK_POINTER_REG;
  164. ref.offset := LinkageAreaSize+para_size_till_now;
  165. tmpreg := get_scratch_reg_address(list);
  166. a_loadaddr_ref_reg(list,size,r,tmpreg);
  167. a_load_reg_ref(list,size,tmpreg,ref);
  168. free_scratch_reg(list,tmpreg);
  169. end;
  170. {$endif para_sizes_known}
  171. end;
  172. { calling a code fragment by name }
  173. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  174. var
  175. href : treference;
  176. begin
  177. { save our RTOC register value. Only necessary when doing pointer based }
  178. { calls or cross TOC calls, but currently done always }
  179. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  180. list.concat(taicpu.op_reg_ref(A_STW,R_TOC,href));
  181. list.concat(taicpu.op_sym(A_BL,newasmsymbol(s)));
  182. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  183. list.concat(taicpu.op_reg_ref(A_LWZ,R_TOC,href));
  184. end;
  185. {********************** load instructions ********************}
  186. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  187. begin
  188. if (longint(a) >= low(smallint)) and
  189. (longint(a) <= high(smallint)) then
  190. list.concat(taicpu.op_reg_const(A_LI,reg,longint(a)))
  191. else if ((a and $ffff) <> 0) then
  192. begin
  193. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  194. if ((a shr 16) <> 0) then
  195. list.concat(taicpu.op_reg_const(A_ADDIS,reg,
  196. (a shr 16)+ord(smallint(a and $ffff) < 0)))
  197. end
  198. else
  199. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  200. end;
  201. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  202. const
  203. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  204. { indexed? updating?}
  205. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  206. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  207. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  208. var
  209. op: TAsmOp;
  210. ref2: TReference;
  211. begin
  212. ref2 := ref;
  213. FixRef(list,ref2);
  214. if size in [OS_S8..OS_S16] then
  215. { storing is the same for signed and unsigned values }
  216. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  217. { 64 bit stuff should be handled separately }
  218. if size in [OS_64,OS_S64] then
  219. internalerror(200109236);
  220. op := storeinstr[tcgsize2unsigned[size],ref2.index<>R_NO,false];
  221. a_load_store(list,op,reg,ref2);
  222. End;
  223. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  224. const
  225. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  226. { indexed? updating?}
  227. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  228. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  229. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  230. { 64bit stuff should be handled separately }
  231. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  232. { there's no load-byte-with-sign-extend :( }
  233. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  234. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  235. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  236. var
  237. op: tasmop;
  238. tmpreg: tregister;
  239. ref2, tmpref: treference;
  240. begin
  241. ref2 := ref;
  242. fixref(list,ref2);
  243. op := loadinstr[size,ref2.index<>R_NO,false];
  244. a_load_store(list,op,reg,ref2);
  245. { sign extend shortint if necessary, since there is no }
  246. { load instruction that does that automatically (JM) }
  247. if size = OS_S8 then
  248. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  249. end;
  250. procedure tcgppc.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  251. begin
  252. if (reg1 <> reg2) then
  253. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  254. end;
  255. procedure tcgppc.a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);
  256. begin
  257. { can't use op_sym_ofs_reg because sym+ofs can be > 32767!! }
  258. internalerror(200112293);
  259. end;
  260. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  261. begin
  262. list.concat(taicpu.op_reg_reg(A_FMR,reg1,reg2));
  263. end;
  264. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  265. const
  266. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  267. { indexed? updating?}
  268. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  269. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  270. var
  271. op: tasmop;
  272. ref2: treference;
  273. begin
  274. if not(size in [OS_F32,OS_F64]) then
  275. internalerror(200201121);
  276. ref2 := ref;
  277. fixref(list,ref2);
  278. op := fpuloadinstr[size,ref2.index <> R_NO,false];
  279. a_load_store(list,op,reg,ref2);
  280. end;
  281. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  282. const
  283. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  284. { indexed? updating?}
  285. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  286. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  287. var
  288. op: tasmop;
  289. ref2: treference;
  290. begin
  291. if not(size in [OS_F32,OS_F64]) then
  292. internalerror(200201122);
  293. ref2 := ref;
  294. fixref(list,ref2);
  295. op := fpustoreinstr[size,ref2.index <> R_NO,false];
  296. a_load_store(list,op,reg,ref2);
  297. end;
  298. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  299. var
  300. scratch_register: TRegister;
  301. begin
  302. case op of
  303. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL, OP_ADD, OP_AND, OP_OR, OP_SUB,
  304. OP_XOR:
  305. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  306. OP_SHL,OP_SHR,OP_SAR:
  307. begin
  308. if (a and 31) <> 0 then
  309. list.concat(taicpu.op_reg_reg_const(
  310. TOpCG2AsmOpConstLo[op],reg,reg,a and 31));
  311. if (a shr 5) <> 0 then
  312. internalError(68991);
  313. end
  314. else internalError(68992);
  315. end;
  316. end;
  317. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  318. begin
  319. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  320. end;
  321. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  322. size: tcgsize; a: aword; src, dst: tregister);
  323. var
  324. l1,l2: longint;
  325. var
  326. oplo, ophi: tasmop;
  327. scratchreg: tregister;
  328. useReg: boolean;
  329. begin
  330. ophi := TOpCG2AsmOpConstHi[op];
  331. oplo := TOpCG2AsmOpConstLo[op];
  332. { constants in a PPC instruction are always interpreted as signed }
  333. { 16bit values, so if the value is between low(smallint) and }
  334. { high(smallint), it's easy }
  335. if (op in [OP_ADD,OP_SUB,OP_AND,OP_OR,OP_XOR]) then
  336. begin
  337. if (a = 0) then
  338. begin
  339. if op = OP_AND then
  340. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  341. exit;
  342. end
  343. else if (a = high(aword)) and
  344. (op in [OP_AND,OP_OR]) then
  345. begin
  346. if op = OP_OR then
  347. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  348. exit;
  349. end
  350. else if (longint(a) >= low(smallint)) and
  351. (longint(a) <= high(smallint)) then
  352. begin
  353. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,a));
  354. exit;
  355. end;
  356. { all basic constant instructions also have a shifted form that }
  357. { works only on the highest 16bits, so if low(a) is 0, we can }
  358. { use that one }
  359. if (lo(a) = 0) then
  360. begin
  361. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,hi(a)));
  362. exit;
  363. end;
  364. end;
  365. { otherwise, the instructions we can generate depend on the }
  366. { operation }
  367. useReg := false;
  368. case op of
  369. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL:
  370. if (Op = OP_IMUL) and (longint(a) >= -32768) and
  371. (longint(a) <= 32767) then
  372. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,a))
  373. else
  374. usereg := true;
  375. OP_ADD,OP_SUB:
  376. begin
  377. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,low(a)));
  378. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  379. high(a) + ord(smallint(a) < 0)));
  380. end;
  381. OP_OR:
  382. { try to use rlwimi }
  383. if get_rlwi_const(a,l1,l2) then
  384. begin
  385. if src <> dst then
  386. list.concat(taicpu.op_reg_reg(A_MR,dst,src));
  387. scratchreg := get_scratch_reg_int(list);
  388. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  389. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  390. scratchreg,0,l1,l2));
  391. free_scratch_reg(list,scratchreg);
  392. end
  393. else
  394. useReg := true;
  395. OP_AND:
  396. { try to use rlwinm }
  397. if get_rlwi_const(a,l1,l2) then
  398. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  399. src,0,l1,l2))
  400. else
  401. useReg := true;
  402. OP_XOR:
  403. useReg := true;
  404. OP_SHL,OP_SHR,OP_SAR:
  405. begin
  406. if (a and 31) <> 0 Then
  407. list.concat(taicpu.op_reg_reg_const(
  408. TOpCG2AsmOpConstLo[Op],dst,src,a and 31));
  409. if (a shr 5) <> 0 then
  410. internalError(68991);
  411. end
  412. else
  413. internalerror(200109091);
  414. end;
  415. { if all else failed, load the constant in a register and then }
  416. { perform the operation }
  417. if useReg then
  418. begin
  419. scratchreg := get_scratch_reg_int(list);
  420. a_load_const_reg(list,OS_32,a,scratchreg);
  421. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  422. free_scratch_reg(list,scratchreg);
  423. end;
  424. end;
  425. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  426. size: tcgsize; src1, src2, dst: tregister);
  427. const
  428. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  429. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  430. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  431. begin
  432. case op of
  433. OP_NEG,OP_NOT:
  434. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  435. else
  436. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  437. end;
  438. end;
  439. {*************** compare instructructions ****************}
  440. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  441. l : tasmlabel);
  442. var
  443. p: taicpu;
  444. scratch_register: TRegister;
  445. signed: boolean;
  446. begin
  447. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  448. { in the following case, we generate more efficient code when }
  449. { signed is true }
  450. if (cmp_op in [OC_EQ,OC_NE]) and
  451. (a > $ffff) then
  452. signed := true;
  453. if signed then
  454. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  455. list.concat(taicpu.op_reg_reg_const(A_CMPWI,R_CR0,reg,longint(a)))
  456. else
  457. begin
  458. scratch_register := get_scratch_reg_int(list);
  459. a_load_const_reg(list,OS_32,a,scratch_register);
  460. list.concat(taicpu.op_reg_reg_reg(A_CMPW,R_CR0,reg,scratch_register));
  461. free_scratch_reg(list,scratch_register);
  462. end
  463. else
  464. if (a <= $ffff) then
  465. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,R_CR0,reg,a))
  466. else
  467. begin
  468. scratch_register := get_scratch_reg_int(list);
  469. a_load_const_reg(list,OS_32,a,scratch_register);
  470. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,R_CR0,reg,scratch_register));
  471. free_scratch_reg(list,scratch_register);
  472. end;
  473. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  474. end;
  475. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  476. reg1,reg2 : tregister;l : tasmlabel);
  477. var
  478. p: taicpu;
  479. op: tasmop;
  480. begin
  481. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  482. op := A_CMPW
  483. else op := A_CMPLW;
  484. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  485. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  486. end;
  487. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  488. begin
  489. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  490. end;
  491. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  492. begin
  493. a_jmp(list,A_B,C_None,0,l);
  494. end;
  495. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  496. var
  497. c: tasmcond;
  498. begin
  499. c := flags_to_cond(f);
  500. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(R_CR0),l);
  501. end;
  502. procedure tcgppc.g_flags2reg(list: taasmoutput; const f: TResFlags; reg: TRegister);
  503. var
  504. testbit: byte;
  505. bitvalue: boolean;
  506. begin
  507. { get the bit to extract from the conditional register + its }
  508. { requested value (0 or 1) }
  509. testbit := (ord(f.cr) * 4);
  510. case f.flag of
  511. F_EQ,F_NE:
  512. bitvalue := f.flag = F_EQ;
  513. F_LT,F_GE:
  514. begin
  515. inc(testbit);
  516. bitvalue := f.flag = F_LT;
  517. end;
  518. F_GT,F_LE:
  519. begin
  520. inc(testbit,2);
  521. bitvalue := f.flag = F_GT;
  522. end;
  523. else
  524. internalerror(200112261);
  525. end;
  526. { load the conditional register in the destination reg }
  527. list.concat(taicpu.op_reg(A_MFCR,reg));
  528. { we will move the bit that has to be tested to bit 31 -> rotate }
  529. { left by bitpos+1 (remember, this is big-endian!) }
  530. testbit := (testbit + 1) and 31;
  531. { extract bit }
  532. list.concat(taicpu.op_reg_reg_const_const_const(
  533. A_RLWINM,reg,reg,testbit,31,31));
  534. { if we need the inverse, xor with 1 }
  535. if not bitvalue then
  536. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  537. end;
  538. (*
  539. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  540. var
  541. testbit: byte;
  542. bitvalue: boolean;
  543. begin
  544. { get the bit to extract from the conditional register + its }
  545. { requested value (0 or 1) }
  546. case f.simple of
  547. false:
  548. begin
  549. { we don't generate this in the compiler }
  550. internalerror(200109062);
  551. end;
  552. true:
  553. case f.cond of
  554. C_None:
  555. internalerror(200109063);
  556. C_LT..C_NU:
  557. begin
  558. testbit := (ord(f.cr) - ord(R_CR0))*4;
  559. inc(testbit,AsmCondFlag2BI[f.cond]);
  560. bitvalue := AsmCondFlagTF[f.cond];
  561. end;
  562. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  563. begin
  564. testbit := f.crbit
  565. bitvalue := AsmCondFlagTF[f.cond];
  566. end;
  567. else
  568. internalerror(200109064);
  569. end;
  570. end;
  571. { load the conditional register in the destination reg }
  572. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  573. { we will move the bit that has to be tested to bit 31 -> rotate }
  574. { left by bitpos+1 (remember, this is big-endian!) }
  575. if bitpos <> 31 then
  576. inc(bitpos)
  577. else
  578. bitpos := 0;
  579. { extract bit }
  580. list.concat(taicpu.op_reg_reg_const_const_const(
  581. A_RLWINM,reg,reg,bitpos,31,31));
  582. { if we need the inverse, xor with 1 }
  583. if not bitvalue then
  584. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  585. end;
  586. *)
  587. { *********** entry/exit code and address loading ************ }
  588. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  589. begin
  590. case target_info.target of
  591. target_powerpc_macos:
  592. g_stackframe_entry_mac(list,localsize);
  593. target_powerpc_linux:
  594. g_stackframe_entry_sysv(list,localsize)
  595. else
  596. internalerror(2204001);
  597. end;
  598. end;
  599. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  600. { generated the entry code of a procedure/function. Note: localsize is the }
  601. { sum of the size necessary for local variables and the maximum possible }
  602. { combined size of ALL the parameters of a procedure called by the current }
  603. { one }
  604. var regcounter: TRegister;
  605. href : treference;
  606. begin
  607. if (localsize mod 8) <> 0 then internalerror(58991);
  608. { CR and LR only have to be saved in case they are modified by the current }
  609. { procedure, but currently this isn't checked, so save them always }
  610. { following is the entry code as described in "Altivec Programming }
  611. { Interface Manual", bar the saving of AltiVec registers }
  612. a_reg_alloc(list,STACK_POINTER_REG);
  613. a_reg_alloc(list,R_0);
  614. { allocate registers containing reg parameters }
  615. for regcounter := R_3 to R_10 do
  616. a_reg_alloc(list,regcounter);
  617. { save return address... }
  618. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  619. { ... in caller's frame }
  620. reference_reset_base(href,STACK_POINTER_REG,4);
  621. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  622. a_reg_dealloc(list,R_0);
  623. a_reg_alloc(list,R_11);
  624. { save end of fpr save area }
  625. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER_REG,0));
  626. a_reg_alloc(list,R_12);
  627. { 0 or 8 based on SP alignment }
  628. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  629. R_12,STACK_POINTER_REG,0,28,28));
  630. { add in stack length }
  631. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  632. -localsize));
  633. { establish new alignment }
  634. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  635. a_reg_dealloc(list,R_12);
  636. { save floating-point registers }
  637. { !!! has to be optimized: only save registers that are used }
  638. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savefpr_14'),0));
  639. { compute end of gpr save area }
  640. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-144));
  641. { save gprs and fetch GOT pointer }
  642. { !!! has to be optimized: only save registers that are used }
  643. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savegpr_14_go'),0));
  644. a_reg_alloc(list,R_31);
  645. { place GOT ptr in r31 }
  646. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  647. { save the CR if necessary ( !!! always done currently ) }
  648. { still need to find out where this has to be done for SystemV
  649. a_reg_alloc(list,R_0);
  650. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  651. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  652. new_reference(STACK_POINTER_REG,LA_CR)));
  653. a_reg_dealloc(list,R_0); }
  654. { save pointer to incoming arguments }
  655. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_30,R_11,144));
  656. { now comes the AltiVec context save, not yet implemented !!! }
  657. end;
  658. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  659. { generated the entry code of a procedure/function. Note: localsize is the }
  660. { sum of the size necessary for local variables and the maximum possible }
  661. { combined size of ALL the parameters of a procedure called by the current }
  662. { one }
  663. var regcounter: TRegister;
  664. href : treference;
  665. begin
  666. if (localsize mod 8) <> 0 then internalerror(58991);
  667. { CR and LR only have to be saved in case they are modified by the current }
  668. { procedure, but currently this isn't checked, so save them always }
  669. { following is the entry code as described in "Altivec Programming }
  670. { Interface Manual", bar the saving of AltiVec registers }
  671. a_reg_alloc(list,STACK_POINTER_REG);
  672. a_reg_alloc(list,R_0);
  673. { allocate registers containing reg parameters }
  674. for regcounter := R_3 to R_10 do
  675. a_reg_alloc(list,regcounter);
  676. { save return address... }
  677. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  678. { ... in caller's frame }
  679. reference_reset_base(href,STACK_POINTER_REG,8);
  680. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  681. a_reg_dealloc(list,R_0);
  682. { save floating-point registers }
  683. { !!! has to be optimized: only save registers that are used }
  684. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savef14'),0));
  685. { save gprs in gpr save area }
  686. { !!! has to be optimized: only save registers that are used }
  687. reference_reset_base(href,STACK_POINTER_REG,-220);
  688. list.concat(taicpu.op_reg_ref(A_STMW,R_13,href));
  689. { save the CR if necessary ( !!! always done currently ) }
  690. a_reg_alloc(list,R_0);
  691. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  692. reference_reset_base(href,stack_pointer_reg,LA_CR);
  693. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  694. a_reg_dealloc(list,R_0);
  695. { save pointer to incoming arguments }
  696. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  697. a_reg_alloc(list,R_12);
  698. { 0 or 8 based on SP alignment }
  699. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  700. R_12,STACK_POINTER_REG,0,28,28));
  701. { add in stack length }
  702. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  703. -localsize));
  704. { establish new alignment }
  705. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  706. a_reg_dealloc(list,R_12);
  707. { now comes the AltiVec context save, not yet implemented !!! }
  708. end;
  709. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  710. begin
  711. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  712. end;
  713. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  714. begin
  715. case target_info.target of
  716. target_powerpc_macos:
  717. g_return_from_proc_mac(list,parasize);
  718. target_powerpc_linux:
  719. g_return_from_proc_sysv(list,parasize)
  720. else
  721. internalerror(2204001);
  722. end;
  723. end;
  724. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  725. var tmpreg: tregister;
  726. ref2, tmpref: treference;
  727. begin
  728. ref2 := ref;
  729. FixRef(list,ref2);
  730. if assigned(ref2.symbol) then
  731. { add the symbol's value to the base of the reference, and if the }
  732. { reference doesn't have a base, create one }
  733. begin
  734. tmpreg := get_scratch_reg_address(list);
  735. reference_reset(tmpref);
  736. tmpref.symbol := ref2.symbol;
  737. tmpref.symaddr := refs_ha;
  738. // tmpref.is_immediate := true;
  739. if ref2.base <> R_NO then
  740. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  741. ref2.base,tmpref))
  742. else
  743. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  744. tmpref.base := tmpreg;
  745. tmpref.symaddr := refs_l;
  746. { can be folded with one of the next instructions by the }
  747. { optimizer probably }
  748. list.concat(taicpu.op_reg_reg_ref(A_ADDI,tmpreg,tmpreg,tmpref));
  749. end;
  750. if ref2.offset <> 0 Then
  751. if ref2.base <> R_NO then
  752. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  753. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  754. { occurs, so now only ref.offset has to be loaded }
  755. else a_load_const_reg(list,OS_32,ref2.offset,r)
  756. else
  757. if ref.index <> R_NO Then
  758. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  759. else
  760. if r <> ref2.base then
  761. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  762. if assigned(ref2.symbol) then
  763. free_scratch_reg(list,tmpreg);
  764. end;
  765. { ************* concatcopy ************ }
  766. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  767. var
  768. t: taicpu;
  769. countreg, tempreg: TRegister;
  770. src, dst: TReference;
  771. lab: tasmlabel;
  772. count, count2: aword;
  773. begin
  774. { make sure short loads are handled as optimally as possible }
  775. if not loadref then
  776. if (len <= 4) and
  777. (byte(len) in [1,2,4]) then
  778. begin
  779. a_load_ref_ref(list,int_cgsize(len),source,dest);
  780. if delsource then
  781. reference_release(exprasmlist,source);
  782. exit;
  783. end;
  784. { make sure source and dest are valid }
  785. src := source;
  786. fixref(list,src);
  787. dst := dest;
  788. fixref(list,dst);
  789. reference_reset(src);
  790. reference_reset(dst);
  791. { load the address of source into src.base }
  792. src.base := get_scratch_reg_address(list);
  793. if loadref then
  794. a_load_ref_reg(list,OS_32,source,src.base)
  795. else a_loadaddr_ref_reg(list,source,src.base);
  796. if delsource then
  797. reference_release(exprasmlist,source);
  798. { load the address of dest into dst.base }
  799. dst.base := get_scratch_reg_address(list);
  800. a_loadaddr_ref_reg(list,dest,dst.base);
  801. count := len div 4;
  802. if count > 3 then
  803. { generate a loop }
  804. begin
  805. { the offsets are zero after the a_loadaddress_ref_reg and just }
  806. { have to be set to 4. I put an Inc there so debugging may be }
  807. { easier (should offset be different from zero here, it will be }
  808. { easy to notice in the generated assembler }
  809. inc(dst.offset,4);
  810. inc(src.offset,4);
  811. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  812. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  813. countreg := get_scratch_reg_int(list);
  814. a_load_const_reg(list,OS_32,count-1,countreg);
  815. { explicitely allocate R_0 since it can be used safely here }
  816. { (for holding date that's being copied) }
  817. tempreg := R_0;
  818. a_reg_alloc(list,R_0);
  819. getlabel(lab);
  820. a_label(list, lab);
  821. list.concat(taicpu.op_reg_ref(A_LWZU,tempreg,src));
  822. list.concat(taicpu.op_reg_reg_const(A_CMPWI,R_CR0,countreg,0));
  823. list.concat(taicpu.op_reg_ref(A_STWU,tempreg,dst));
  824. list.concat(taicpu.op_reg_reg_const(A_SUBI,countreg,countreg,1));
  825. a_jmp(list,A_BC,C_NE,0,lab);
  826. free_scratch_reg(list,countreg);
  827. a_reg_dealloc(list,R_0);
  828. end
  829. else
  830. { unrolled loop }
  831. begin
  832. a_reg_alloc(list,R_0);
  833. for count2 := 1 to count do
  834. begin
  835. a_load_ref_reg(list,OS_32,src,R_0);
  836. a_load_reg_ref(list,OS_32,R_0,dst);
  837. inc(src.offset,4);
  838. inc(dst.offset,4);
  839. end;
  840. a_reg_dealloc(list,R_0);
  841. end;
  842. { copy the leftovers }
  843. if (len and 2) <> 0 then
  844. begin
  845. a_reg_alloc(list,R_0);
  846. a_load_ref_reg(list,OS_16,src,R_0);
  847. a_load_reg_ref(list,OS_16,R_0,dst);
  848. inc(src.offset,2);
  849. inc(dst.offset,2);
  850. a_reg_dealloc(list,R_0);
  851. end;
  852. if (len and 1) <> 0 then
  853. begin
  854. a_load_reg_ref(list,OS_16,R_0,dst);
  855. a_load_ref_reg(list,OS_8,src,R_0);
  856. a_load_reg_ref(list,OS_8,R_0,dst);
  857. a_reg_dealloc(list,R_0);
  858. end;
  859. free_scratch_reg(list,src.base);
  860. free_scratch_reg(list,dst.base);
  861. end;
  862. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  863. var
  864. hl : tasmlabel;
  865. begin
  866. if not(cs_check_overflow in aktlocalswitches) then
  867. exit;
  868. getlabel(hl);
  869. if not ((p.resulttype.def.deftype=pointerdef) or
  870. ((p.resulttype.def.deftype=orddef) and
  871. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  872. bool8bit,bool16bit,bool32bit]))) then
  873. begin
  874. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  875. a_jmp(list,A_BC,C_OV,7,hl)
  876. end
  877. else
  878. a_jmp_cond(list,OC_AE,hl);
  879. a_call_name(list,'FPC_OVERFLOW');
  880. a_label(list,hl);
  881. end;
  882. {***************** This is private property, keep out! :) *****************}
  883. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  884. var
  885. regcounter: TRegister;
  886. begin
  887. { release parameter registers }
  888. for regcounter := R_3 to R_10 do
  889. a_reg_dealloc(list,regcounter);
  890. { AltiVec context restore, not yet implemented !!! }
  891. { address of gpr save area to r11 }
  892. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_31,-144));
  893. { restore gprs }
  894. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restgpr_14'),0));
  895. { address of fpr save area to r11 }
  896. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,144));
  897. { restore fprs and return }
  898. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restfpr_14_x'),0));
  899. end;
  900. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  901. var
  902. regcounter: TRegister;
  903. href : treference;
  904. begin
  905. { release parameter registers }
  906. for regcounter := R_3 to R_10 do
  907. a_reg_dealloc(list,regcounter);
  908. { AltiVec context restore, not yet implemented !!! }
  909. { restore SP }
  910. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER_REG,R_31,0));
  911. { restore gprs }
  912. reference_reset_base(href,STACK_POINTER_REG,-220);
  913. list.concat(taicpu.op_reg_ref(A_LMW,R_13,href));
  914. { restore return address ... }
  915. reference_reset_base(href,STACK_POINTER_REG,8);
  916. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  917. { ... and return from _restf14 }
  918. list.concat(taicpu.op_sym_ofs(A_B,newasmsymbol('_restf14'),0));
  919. end;
  920. procedure tcgppc.fixref(list: taasmoutput; var ref: treference);
  921. begin
  922. If (ref.base <> R_NO) then
  923. begin
  924. if (ref.index <> R_NO) and
  925. ((ref.offset <> 0) or assigned(ref.symbol)) then
  926. begin
  927. if not assigned(ref.symbol) and
  928. (cardinal(ref.offset-low(smallint)) <=
  929. high(smallint)-low(smallint)) then
  930. begin
  931. list.concat(taicpu.op_reg_reg_const(
  932. A_ADDI,ref.base,ref.base,ref.offset));
  933. ref.offset := 0;
  934. end
  935. else
  936. begin
  937. list.concat(taicpu.op_reg_reg_reg(
  938. A_ADD,ref.base,ref.base,ref.index));
  939. ref.index := R_NO;
  940. end;
  941. end
  942. end
  943. else
  944. begin
  945. ref.base := ref.index;
  946. ref.index := R_NO
  947. end
  948. end;
  949. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  950. { that's the case, we can use rlwinm to do an AND operation }
  951. function tcgppc.get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  952. var
  953. temp, testbit: longint;
  954. compare: boolean;
  955. begin
  956. get_rlwi_const := false;
  957. { start with the lowest bit }
  958. testbit := 1;
  959. { check its value }
  960. compare := boolean(a and testbit);
  961. { find out how long the run of bits with this value is }
  962. { (it's impossible that all bits are 1 or 0, because in that case }
  963. { this function wouldn't have been called) }
  964. l1 := 31;
  965. while (((a and testbit) <> 0) = compare) do
  966. begin
  967. testbit := testbit shl 1;
  968. dec(l1);
  969. end;
  970. { check the length of the run of bits that comes next }
  971. compare := not compare;
  972. l2 := l1;
  973. while (((a and testbit) <> 0) = compare) and
  974. (l2 >= 0) do
  975. begin
  976. testbit := testbit shl 1;
  977. dec(l2);
  978. end;
  979. { and finally the check whether the rest of the bits all have the }
  980. { same value }
  981. compare := not compare;
  982. temp := l2;
  983. if temp >= 0 then
  984. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  985. exit;
  986. { we have done "not(not(compare))", so compare is back to its }
  987. { initial value. If the lowest bit was 0, a is of the form }
  988. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  989. { because l2 now contains the position of the last zero of the }
  990. { first run instead of that of the first 1) so switch l1 and l2 }
  991. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  992. if not compare then
  993. begin
  994. temp := l1;
  995. l1 := l2+1;
  996. l2 := temp;
  997. end
  998. else
  999. { otherwise, l1 currently contains the position of the last }
  1000. { zero instead of that of the first 1 of the second run -> +1 }
  1001. inc(l1);
  1002. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1003. l1 := l1 and 31;
  1004. l2 := l2 and 31;
  1005. get_rlwi_const := true;
  1006. end;
  1007. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1008. ref: treference);
  1009. var
  1010. tmpreg: tregister;
  1011. tmpref: treference;
  1012. begin
  1013. if assigned(ref.symbol) then
  1014. begin
  1015. tmpreg := get_scratch_reg_address(list);
  1016. reference_reset(tmpref);
  1017. tmpref.symbol := ref.symbol;
  1018. tmpref.symaddr := refs_ha;
  1019. // tmpref.is_immediate := true;
  1020. if ref.base <> R_NO then
  1021. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1022. ref.base,tmpref))
  1023. else
  1024. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1025. ref.base := tmpreg;
  1026. ref.symaddr := refs_l;
  1027. end;
  1028. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1029. if assigned(ref.symbol) then
  1030. free_scratch_reg(list,tmpreg);
  1031. end;
  1032. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1033. crval: longint; l: tasmlabel);
  1034. var
  1035. p: taicpu;
  1036. begin
  1037. p := taicpu.op_sym(op,newasmsymbol(l.name));
  1038. if op <> A_B then
  1039. create_cond_norm(c,crval,p.condition);
  1040. p.is_jmp := true;
  1041. list.concat(p)
  1042. end;
  1043. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1044. begin
  1045. case op of
  1046. OP_AND,OP_OR,OP_XOR:
  1047. begin
  1048. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1049. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1050. end;
  1051. OP_ADD:
  1052. begin
  1053. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc.reglo,regdst.reglo));
  1054. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc.reghi,regdst.reghi));
  1055. end;
  1056. OP_SUB:
  1057. begin
  1058. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regdst.reglo,regsrc.reglo));
  1059. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc.reghi,regdst.reghi));
  1060. end;
  1061. end;
  1062. end;
  1063. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  1064. const
  1065. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1066. (A_SUBIC,A_SUBC,A_ADDME));
  1067. var
  1068. tmpreg: tregister;
  1069. tmpreg64: tregister64;
  1070. isadd: boolean;
  1071. begin
  1072. case op of
  1073. OP_AND,OP_OR,OP_XOR:
  1074. begin
  1075. cg.a_op_const_reg(list,op,cardinal(value),reg.reglo);
  1076. cg.a_op_const_reg(list,op,value shr 32,reg.reghi);
  1077. end;
  1078. OP_ADD, OP_SUB:
  1079. begin
  1080. if (longint(value) <> 0) then
  1081. begin
  1082. isadd := op = OP_ADD;
  1083. if (longint(value) >= -32768) and
  1084. (longint(value) <= 32767) then
  1085. begin
  1086. list.concat(taicpu.op_reg_reg_const(ops[isadd,1],
  1087. reg.reglo,reg.reglo,aword(value)));
  1088. end
  1089. else if ((value shr 32) = 0) then
  1090. begin
  1091. tmpreg := cg.get_scratch_reg_int(list);
  1092. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  1093. list.concat(taicpu.op_reg_reg_reg(ops[isadd,2],
  1094. reg.reglo,reg.reglo,tmpreg));
  1095. list.concat(taicpu.op_reg_reg(ops[isadd,3],
  1096. reg.reghi,reg.reghi));
  1097. cg.free_scratch_reg(list,tmpreg);
  1098. end
  1099. else
  1100. begin
  1101. tmpreg64.reglo := cg.get_scratch_reg_int(list);
  1102. tmpreg64.reghi := cg.get_scratch_reg_int(list);
  1103. a_load64_const_reg(list,value,tmpreg64);
  1104. a_op64_reg_reg(list,op,tmpreg64,reg);
  1105. cg.free_scratch_reg(list,tmpreg64.reghi);
  1106. cg.free_scratch_reg(list,tmpreg64.reglo);
  1107. end
  1108. end
  1109. else
  1110. cg.a_op_const_reg(list,op,value shr 32,reg.reghi);
  1111. end;
  1112. end;
  1113. end;
  1114. begin
  1115. cg := tcgppc.create;
  1116. cg64 :=tcg64fppc.create;
  1117. end.
  1118. {
  1119. $Log$
  1120. Revision 1.23 2002-07-11 14:41:34 florian
  1121. * start of the new generic parameter handling
  1122. Revision 1.22 2002/07/11 07:38:28 jonas
  1123. + tcg64fpc implementation (only a_op64_reg_reg and a_op64_const_reg for
  1124. now)
  1125. * fixed and improved tcgppc.a_load_const_reg
  1126. * improved tcgppc.a_op_const_reg, tcgppc.a_cmp_const_reg_label
  1127. * A_CMP* -> A_CMPW* (this means that 32bit compares should be done)
  1128. Revision 1.21 2002/07/09 19:45:01 jonas
  1129. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  1130. * small fixes in the assembler writer
  1131. * changed scratch registers, because they were used by the linker (r11
  1132. and r12) and by the abi under linux (r31)
  1133. Revision 1.20 2002/07/07 09:44:31 florian
  1134. * powerpc target fixed, very simple units can be compiled
  1135. Revision 1.19 2002/05/20 13:30:41 carl
  1136. * bugfix of hdisponen (base must be set, not index)
  1137. * more portability fixes
  1138. Revision 1.18 2002/05/18 13:34:26 peter
  1139. * readded missing revisions
  1140. Revision 1.17 2002/05/16 19:46:53 carl
  1141. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1142. + try to fix temp allocation (still in ifdef)
  1143. + generic constructor calls
  1144. + start of tassembler / tmodulebase class cleanup
  1145. Revision 1.14 2002/05/13 19:52:46 peter
  1146. * a ppcppc can be build again
  1147. Revision 1.13 2002/04/20 21:41:51 carl
  1148. * renamed some constants
  1149. Revision 1.12 2002/04/06 18:13:01 jonas
  1150. * several powerpc-related additions and fixes
  1151. Revision 1.11 2002/01/02 14:53:04 jonas
  1152. * fixed small bug in a_jmp_flags
  1153. }