cgcpu.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. interface
  20. uses
  21. cgbase,cgobj,aasm,cpuasm,cpubase,cpuinfo;
  22. type
  23. tcgppc = class(tcg)
  24. { passing parameters, per default the parameter is pushed }
  25. { nr gives the number of the parameter (enumerated from }
  26. { left to right), this allows to move the parameter to }
  27. { register, if the cpu supports register calling }
  28. { conventions }
  29. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);override;
  30. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);override;
  31. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);override;
  32. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);override;
  33. procedure a_call_name(list : taasmoutput;const s : string;
  34. offset : longint);override;
  35. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  36. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  37. { move instructions }
  38. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  39. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  40. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  41. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);override;
  42. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister); override;
  43. { comparison operations }
  44. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  45. l : pasmlabel);override;
  46. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : pasmlabel);
  47. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: pasmlabel);
  48. procedure g_flags2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister); override;
  49. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  50. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  51. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  52. procedure g_restore_frame_pointer(list : taasmoutput);override;
  53. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  54. procedure a_loadaddress_ref_reg(list : taasmoutput;const ref2 : treference;r : tregister);override;
  55. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  56. private
  57. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  58. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  59. procedure a_op_reg_reg_const32(list: taasmoutput; op: TOpCg;
  60. dst, src: tregister; a: aword);
  61. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg; dst, src1,
  62. src2: tregister);
  63. { Make sure ref is a valid reference for the PowerPC and sets the }
  64. { base to the value of the index if (base = R_NO). }
  65. procedure fixref(var ref: treference);
  66. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  67. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  68. ref: treference);
  69. { creates the correct branch instruction for a given combination }
  70. { of asmcondflags and destination addressing mode }
  71. procedure a_jmp(list: taasmoutput; op: tasmop;
  72. c: tasmcondflags; l: pasmlabel);
  73. end;
  74. const
  75. {
  76. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_ADD,A_AND,A_DIVWU,
  77. A_DIVW,A_MULLW, A_MULLW, A_NEG,A_NOT,A_OR,
  78. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  79. }
  80. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (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);
  83. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (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);
  86. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlags = (CF_NONE,CF_EQ,CF_GT,
  87. CF_LT,CF_GE,CF_LE,CF_NE,CF_LE,CF_NG,CF_GE,CF_NL);
  88. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  89. { indexed? updating?}
  90. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  91. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  92. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  93. { 64bit stuff should be handled separately }
  94. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  95. { there's no load-byte-with-sign-extend :( }
  96. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  97. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  98. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  99. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  100. { indexed? updating?}
  101. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  102. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  103. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  104. implementation
  105. uses
  106. globtype,globals,verbose,systems,cutils;
  107. { parameter passing... Still needs extra support from the processor }
  108. { independent code generator }
  109. procedure tcgppc.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);
  110. var
  111. ref: treference;
  112. begin
  113. {$ifdef para_sizes_known}
  114. if (nr <= max_param_regs_int) then
  115. a_load_reg_reg(list,size,r,param_regs_int[nr])
  116. else
  117. begin
  118. reset_reference(ref);
  119. ref.base := stack_pointer;
  120. ref.offset := LinkageAreaSize+para_size_till_now;
  121. a_load_reg_ref(list,size,reg,ref);
  122. end;
  123. {$endif para_sizes_known}
  124. end;
  125. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);
  126. var
  127. ref: treference;
  128. begin
  129. {$ifdef para_sizes_known}
  130. if (nr <= max_param_regs_int) then
  131. a_load_const_reg(list,size,a,param_regs_int[nr])
  132. else
  133. begin
  134. reset_reference(ref);
  135. ref.base := stack_pointer;
  136. ref.offset := LinkageAreaSize+para_size_till_now;
  137. a_load_const_ref(list,size,a,ref);
  138. end;
  139. {$endif para_sizes_known}
  140. end;
  141. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);
  142. var
  143. ref: treference;
  144. tmpreg: tregister;
  145. begin
  146. {$ifdef para_sizes_known}
  147. if (nr <= max_param_regs_int) then
  148. a_load_ref_reg(list,size,r,param_regs_int[nr])
  149. else
  150. begin
  151. reset_reference(ref);
  152. ref.base := stack_pointer;
  153. ref.offset := LinkageAreaSize+para_size_till_now;
  154. tmpreg := get_scratch_reg(list);
  155. a_load_ref_reg(list,size,r,tmpreg);
  156. a_load_reg_ref(list,size,tmpreg,ref);
  157. free_scratch_reg(list,tmpreg);
  158. end;
  159. {$endif para_sizes_known}
  160. end;
  161. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);
  162. var
  163. ref: treference;
  164. tmpreg: tregister;
  165. begin
  166. {$ifdef para_sizes_known}
  167. if (nr <= max_param_regs_int) then
  168. a_loadaddress_ref_reg(list,size,r,param_regs_int[nr])
  169. else
  170. begin
  171. reset_reference(ref);
  172. ref.base := stack_pointer;
  173. ref.offset := LinkageAreaSize+para_size_till_now;
  174. tmpreg := get_scratch_reg(list);
  175. a_loadaddress_ref_reg(list,size,r,tmpreg);
  176. a_load_reg_ref(list,size,tmpreg,ref);
  177. free_scratch_reg(list,tmpreg);
  178. end;
  179. {$endif para_sizes_known}
  180. end;
  181. { calling a code fragment by name }
  182. procedure tcgppc.a_call_name(list : taasmoutput;const s : string;
  183. offset : longint);
  184. begin
  185. { save our RTOC register value. Only necessary when doing pointer based }
  186. { calls or cross TOC calls, but currently done always }
  187. list.concat(taicpu.op_reg_ref(A_STW,R_RTOC,
  188. new_reference(stack_pointer,LA_RTOC)));
  189. list.concat(taicpu.op_sym(A_BL,newasmsymbol(s)));
  190. list.concat(taicpu.op_reg_ref(A_LWZ,R_RTOC,
  191. new_reference(stack_pointer,LA_RTOC)));
  192. end;
  193. {********************** load instructions ********************}
  194. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  195. begin
  196. if (a and $ffff) <> 0 Then
  197. begin
  198. list.concat(taicpu.op_reg_const(A_LI,reg,a and $ffff));
  199. if (longint(a) < low(smallint)) or
  200. (longint(a) > high(smallint)) then
  201. list.concat(taicpu.op_reg_const(A_ADDIS,reg,
  202. (a shr 16)+ord(smallint(a and $ffff) < 0)))
  203. end
  204. else
  205. list.concat(taicpu.op_reg_const(A_LIS,reg,a shr 16));
  206. end;
  207. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  208. var
  209. op: TAsmOp;
  210. ref2: TReference;
  211. begin
  212. ref2 := ref;
  213. FixRef(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[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. var
  225. op: tasmop;
  226. tmpreg: tregister;
  227. ref2, tmpref: treference;
  228. begin
  229. if ref.is_immediate then
  230. a_load_const_reg(list,size,ref.offset,reg)
  231. else
  232. begin
  233. ref2 := ref;
  234. fixref(ref2);
  235. op := loadinstr[size,ref2.index<>R_NO,false];
  236. a_load_store(list,op,reg,ref2);
  237. { sign extend shortint if necessary, since there is no }
  238. { load instruction that does that automatically (JM) }
  239. if size = OS_S8 then
  240. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  241. end;
  242. end;
  243. procedure tcgppc.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  244. begin
  245. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  246. end;
  247. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  248. var
  249. scratch_register: TRegister;
  250. begin
  251. Case Op of
  252. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL:
  253. If (Op = OP_IMUL) And (longint(a) >= -32768) And
  254. (longint(a) <= 32767) Then
  255. list.concat(taicpu.op_reg_reg_const(A_MULLI,reg,reg,a))
  256. Else
  257. Begin
  258. scratch_register := get_scratch_reg(list);
  259. a_load_const_reg(list,OS_32,a,scratch_register);
  260. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOpConstLo[Op],
  261. reg,scratch_register,reg));
  262. free_scratch_reg(list,scratch_register);
  263. End;
  264. OP_ADD, OP_AND, OP_OR, OP_SUB,OP_XOR:
  265. a_op_reg_reg_const32(list,op,reg,reg,a)
  266. OP_SHL,OP_SHR,OP_SAR:
  267. Begin
  268. if (a and 31) <> 0 Then
  269. list.concat(taicpu.op_reg_reg_const(
  270. TOpCG2AsmOpConstLo[Op],reg,reg,a and 31));
  271. If (a shr 5) <> 0 Then
  272. InternalError(68991);
  273. End
  274. Else InternalError(68992);
  275. end;
  276. end;
  277. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  278. begin
  279. a_op_reg_reg_reg(list,op,dst,src,dst);
  280. end;
  281. {*************** compare instructructions ****************}
  282. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  283. l : pasmlabel);
  284. var
  285. p: taicpu;
  286. scratch_register: TRegister;
  287. signed: boolean;
  288. begin
  289. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  290. if signed then
  291. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  292. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,reg,a))
  293. else
  294. begin
  295. scratch_register := get_scratch_reg(list);
  296. a_load_const_reg(list,OS_32,a,scratch_register);
  297. list.concat(taicpu.op_reg_reg_reg(A_CMP,R_CR0,reg,scratch_register));
  298. free_scratch_reg(list,scratch_register);
  299. end
  300. else
  301. if (a <= $ffff) then
  302. list.concat(taicpu.op_reg_reg_const(A_CMPLI,R_CR0,reg,a))
  303. else
  304. begin
  305. scratch_register := get_scratch_reg(list);
  306. a_load_const_reg(list,OS_32,a,scratch_register);
  307. list.concat(taicpu.op_reg_reg_reg(A_CMPL,R_CR0,reg,scratch_register));
  308. free_scratch_reg(list,scratch_register);
  309. end;
  310. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],l);
  311. end;
  312. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  313. reg1,reg2 : tregister;l : pasmlabel);
  314. var p: paicpu;
  315. op: tasmop;
  316. begin
  317. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  318. op := A_CMP
  319. else op := A_CMPL;
  320. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  321. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],l);
  322. end;
  323. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: pasmlabel);
  324. begin
  325. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],l);
  326. end;
  327. procedure tcgppc.g_flags2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  328. var
  329. testbit: byte;
  330. bitvalue: boolean;
  331. begin
  332. { get the bit to extract from the conditional register + its }
  333. { requested value (0 or 1) }
  334. case simple of
  335. false:
  336. begin
  337. { we don't generate this in the compiler }
  338. internalerror(200109062);
  339. end;
  340. true:
  341. case f.cond of
  342. C_None:
  343. internalerror(200109063);
  344. C_LT..C_NU:
  345. begin
  346. testbit := (ord(f.cr) - ord(R_CR0))*4;
  347. inc(testbit,AsmCondFlag2BI[f.cond]);
  348. bitvalue := AsmCondFlagTF[f.cond];
  349. end;
  350. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  351. begin
  352. testbit := f.crbit
  353. bitvalue := AsmCondFlagTF[f.cond];
  354. end;
  355. else
  356. internalerror(200109064);
  357. end;
  358. end;
  359. { load the conditional register in the destination reg }
  360. list.concat(taicpu.create(op_reg_reg(A_MFCR,reg)));
  361. { we will move the bit that has to be tested to bit 31 -> rotate }
  362. { left by bitpos+1 (remember, this is big-endian!) }
  363. if bitpos <> 31 then
  364. inc(bitpos)
  365. else
  366. bitpos := 0;
  367. { extract bit }
  368. list.concat(taicpu.create(op_reg_reg_const_const_const(
  369. A_RLWINM,reg,reg,bitpos,31,31)));
  370. { if we need the inverse, xor with 1 }
  371. if not bitvalue then
  372. list.concat(taicpu.create(op_reg_reg_const(A_XORI,reg,reg,1)));
  373. end;
  374. { *********** entry/exit code and address loading ************ }
  375. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  376. begin
  377. case target_os.id of
  378. os_powerpc_macos:
  379. g_stackframe_entry_mac(list,localsize);
  380. os_powerpc_linux:
  381. g_stackframe_entry_sysv(list,localsize)
  382. else
  383. internalerror(2204001);
  384. end;
  385. end;
  386. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  387. { generated the entry code of a procedure/function. Note: localsize is the }
  388. { sum of the size necessary for local variables and the maximum possible }
  389. { combined size of ALL the parameters of a procedure called by the current }
  390. { one }
  391. var regcounter: TRegister;
  392. begin
  393. if (localsize mod 8) <> 0 then internalerror(58991);
  394. { CR and LR only have to be saved in case they are modified by the current }
  395. { procedure, but currently this isn't checked, so save them always }
  396. { following is the entry code as described in "Altivec Programming }
  397. { Interface Manual", bar the saving of AltiVec registers }
  398. a_reg_alloc(list,stack_pointer);
  399. a_reg_alloc(list,R_0);
  400. { allocate registers containing reg parameters }
  401. for regcounter := R_3 to R_10 do
  402. a_reg_alloc(list,regcounter);
  403. { save return address... }
  404. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  405. { ... in caller's frame }
  406. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER,4)));
  407. a_reg_dealloc(list,R_0);
  408. a_reg_alloc(list,R_11);
  409. { save end of fpr save area }
  410. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER,0));
  411. a_reg_alloc(list,R_12);
  412. { 0 or 8 based on SP alignment }
  413. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  414. R_12,STACK_POINTER,0,28,28));
  415. { add in stack length }
  416. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  417. -localsize));
  418. { establish new alignment }
  419. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER,STACK_POINTER,R_12));
  420. a_reg_dealloc(list,R_12);
  421. { save floating-point registers }
  422. { !!! has to be optimized: only save registers that are used }
  423. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savefpr_14'),0));
  424. { compute end of gpr save area }
  425. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-144));
  426. { save gprs and fetch GOT pointer }
  427. { !!! has to be optimized: only save registers that are used }
  428. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savegpr_14_go'),0));
  429. a_reg_alloc(list,R_31);
  430. { place GOT ptr in r31 }
  431. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  432. { save the CR if necessary ( !!! always done currently ) }
  433. { still need to find out where this has to be done for SystemV
  434. a_reg_alloc(list,R_0);
  435. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  436. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  437. new_reference(stack_pointer,LA_CR)));
  438. a_reg_dealloc(list,R_0); }
  439. { save pointer to incoming arguments }
  440. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_30,R_11,144));
  441. { now comes the AltiVec context save, not yet implemented !!! }
  442. end;
  443. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  444. { generated the entry code of a procedure/function. Note: localsize is the }
  445. { sum of the size necessary for local variables and the maximum possible }
  446. { combined size of ALL the parameters of a procedure called by the current }
  447. { one }
  448. var regcounter: TRegister;
  449. begin
  450. if (localsize mod 8) <> 0 then internalerror(58991);
  451. { CR and LR only have to be saved in case they are modified by the current }
  452. { procedure, but currently this isn't checked, so save them always }
  453. { following is the entry code as described in "Altivec Programming }
  454. { Interface Manual", bar the saving of AltiVec registers }
  455. a_reg_alloc(list,STACK_POINTER);
  456. a_reg_alloc(list,R_0);
  457. { allocate registers containing reg parameters }
  458. for regcounter := R_3 to R_10 do
  459. a_reg_alloc(list,regcounter);
  460. { save return address... }
  461. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  462. { ... in caller's frame }
  463. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER,8)));
  464. a_reg_dealloc(list,R_0);
  465. { save floating-point registers }
  466. { !!! has to be optimized: only save registers that are used }
  467. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savef14'),0));
  468. { save gprs in gpr save area }
  469. { !!! has to be optimized: only save registers that are used }
  470. list.concat(taicpu.op_reg_ref(A_STMW,R_13,new_reference(STACK_POINTER,-220)));
  471. { save the CR if necessary ( !!! always done currently ) }
  472. a_reg_alloc(list,R_0);
  473. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  474. list.concat(taicpu.op_reg_ref(A_STW,R_0,
  475. new_reference(stack_pointer,LA_CR)));
  476. a_reg_dealloc(list,R_0);
  477. { save pointer to incoming arguments }
  478. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER,0));
  479. a_reg_alloc(list,R_12);
  480. { 0 or 8 based on SP alignment }
  481. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  482. R_12,STACK_POINTER,0,28,28));
  483. { add in stack length }
  484. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  485. -localsize));
  486. { establish new alignment }
  487. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER,STACK_POINTER,R_12));
  488. a_reg_dealloc(list,R_12);
  489. { now comes the AltiVec context save, not yet implemented !!! }
  490. end;
  491. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  492. begin
  493. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  494. end;
  495. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  496. begin
  497. case target_os.id of
  498. os_powerpc_macos:
  499. g_return_from_proc_mac(list,parasize);
  500. os_powerpc_linux:
  501. g_return_from_proc_sysv(list,parasize)
  502. else
  503. internalerror(2204001);
  504. end;
  505. end;
  506. procedure tcgppc.a_loadaddress_ref_reg(list : taasmoutput;const ref2 : treference;r : tregister);
  507. var tmpreg: tregister;
  508. ref, tmpref: treference;
  509. begin
  510. ref := ref2;
  511. FixRef(ref);
  512. if assigned(ref.symbol) then
  513. { add the symbol's value to the base of the reference, and if the }
  514. { reference doesn't have a base, create one }
  515. begin
  516. tmpreg := get_scratch_reg(list);
  517. reset_reference(tmpref);
  518. tmpref.symbol := ref.symbol;
  519. tmpref.symaddr := refs_ha;
  520. tmpref.is_immediate := true;
  521. if ref.base <> R_NO then
  522. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  523. ref.base,newreference(tmpref)))
  524. else
  525. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  526. newreference(tmpref)));
  527. ref.base := tmpreg;
  528. ref.symaddr := refs_l;
  529. { can be folded with one of the next instructions by the }
  530. { optimizer probably }
  531. list.concat(taicpu.op_reg_reg_ref(A_ADDI,tmpreg,tmpreg,
  532. newreference(tmpref)));
  533. end;
  534. if ref.offset <> 0 Then
  535. if ref.base <> R_NO then
  536. a_op_reg_reg_const32(list,OP_ADD,r,ref.base,ref.offset)
  537. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  538. { occurs, so now only ref.offset has to be loaded }
  539. else a_load_const_reg(list, OS_32, ref.offset, r)
  540. else
  541. if ref.index <> R_NO Then
  542. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.base,ref.index))
  543. else
  544. if r <> ref.base then
  545. list.concat(taicpu.op_reg_reg(A_MR,r,ref.base));
  546. if assigned(ref.symbol) then
  547. free_scratch_reg(list,tmpreg);
  548. end;
  549. { ************* concatcopy ************ }
  550. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  551. var
  552. p: paicpu;
  553. countreg, tempreg: TRegister;
  554. src, dst: TReference;
  555. lab: PAsmLabel;
  556. count, count2: aword;
  557. begin
  558. { make sure source and dest are valid }
  559. src := source;
  560. fixref(src);
  561. dst := dest;
  562. fixref(dst);
  563. reset_reference(src);
  564. reset_reference(dst);
  565. { load the address of source into src.base }
  566. src.base := get_scratch_reg(list);
  567. if loadref then
  568. a_load_ref_reg(list,OS_32,source,src.base)
  569. else a_loadaddress_ref_reg(list,source,src.base);
  570. if delsource then
  571. del_reference(list,source);
  572. { load the address of dest into dst.base }
  573. dst.base := get_scratch_reg(list);
  574. a_loadaddress_ref_reg(list,dest,dst.base);
  575. count := len div 4;
  576. if count > 3 then
  577. { generate a loop }
  578. begin
  579. { the offsets are zero after the a_loadaddress_ref_reg and just }
  580. { have to be set to 4. I put an Inc there so debugging may be }
  581. { easier (should offset be different from zero here, it will be }
  582. { easy to notice in the genreated assembler }
  583. Inc(dst.offset,4);
  584. Inc(src.offset,4);
  585. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  586. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  587. countreg := get_scratch_reg(list);
  588. a_load_const_reg(list,OS_32,count-1,countreg);
  589. { explicitely allocate R_0 since it can be used safely here }
  590. { (for holding date that's being copied) }
  591. tempreg := R_0;
  592. a_reg_alloc(list,R_0);
  593. getlabel(lab);
  594. a_label(list, lab);
  595. list.concat(taicpu.op_reg_ref(A_LWZU,tempreg,
  596. newreference(src)));
  597. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,countreg,0));
  598. list.concat(taicpu.op_reg_ref(A_STWU,tempreg,newreference(dst)));
  599. list.concat(taicpu.op_reg_reg_const(A_SUBI,countreg,countreg,1));
  600. a_jmp(list,A_BC,CF_NE,lab);
  601. free_scratch_reg(list,countreg);
  602. end
  603. else
  604. { unrolled loop }
  605. begin
  606. tempreg := get_scratch_reg(list);
  607. for count2 := 1 to count do
  608. begin
  609. a_load_ref_reg(list,OS_32,src,tempreg);
  610. a_load_reg_ref(list,OS_32,tempreg,dst);
  611. inc(src.offset,4);
  612. inc(dst.offset,4);
  613. end
  614. end;
  615. { copy the leftovers }
  616. if (len and 2) <> 0 then
  617. begin
  618. a_load_ref_reg(list,OS_16,src,tempreg);
  619. a_load_reg_ref(list,OS_16,tempreg,dst);
  620. inc(src.offset,2);
  621. inc(dst.offset,2);
  622. end;
  623. if (len and 1) <> 0 then
  624. begin
  625. a_load_ref_reg(list,OS_8,src,tempreg);
  626. a_load_reg_ref(list,OS_8,tempreg,dst);
  627. end;
  628. a_reg_dealloc(list,tempreg);
  629. free_scratch_reg(list,src.base);
  630. free_scratch_reg(list,dst.base);
  631. end;
  632. {***************** This is private property, keep out! :) *****************}
  633. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  634. var
  635. regcounter: TRegister;
  636. begin
  637. { release parameter registers }
  638. for regcounter := R_3 to R_10 do
  639. a_reg_dealloc(list,regcounter);
  640. { AltiVec context restore, not yet implemented !!! }
  641. { address of gpr save area to r11 }
  642. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_31,-144));
  643. { restore gprs }
  644. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restgpr_14'),0));
  645. { address of fpr save area to r11 }
  646. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,144));
  647. { restore fprs and return }
  648. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restfpr_14_x'),0));
  649. end;
  650. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  651. var
  652. regcounter: TRegister;
  653. begin
  654. { release parameter registers }
  655. for regcounter := R_3 to R_10 do
  656. a_reg_dealloc(list,regcounter);
  657. { AltiVec context restore, not yet implemented !!! }
  658. { restore SP }
  659. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER,R_31,0));
  660. { restore gprs }
  661. list.concat(taicpu.op_reg_ref(A_LMW,R_13,new_reference(STACK_POINTER,-220)));
  662. { restore return address ... }
  663. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,new_reference(STACK_POINTER,8)));
  664. { ... and return from _restf14 }
  665. list.concat(taicpu.op_sym_ofs(A_B,newasmsymbol('_restf14'),0));
  666. end;
  667. procedure tcgppc.fixref(list: taasmoutput; var ref: treference);
  668. begin
  669. If (ref.base <> R_NO) then
  670. begin
  671. if (ref.index <> R_NO) and
  672. ((ref.offset <> 0) or assigned(ref.symbol)) then
  673. begin
  674. if not assigned(ref.symbol) and
  675. (cardinal(ref.offset-low(smallint)) <=
  676. high(smallint)-low(smallint)) then
  677. begin
  678. list.concat(A_ADDI,ref.base,ref.base,ref.offset);
  679. ref.offset := 0;
  680. end
  681. else
  682. begin
  683. list.concat(A_ADD,ref.base,ref.base,ref.index);
  684. ref.index := R_NO;
  685. end;
  686. end
  687. end
  688. else
  689. begin
  690. ref.base := ref.index;
  691. ref.index := R_NO
  692. end
  693. end;
  694. procedure tcgppc.a_op_reg_reg_const32(list: taasmoutput; op: TOpCg;
  695. dst, src: tregister; a: aword): boolean;
  696. var
  697. l1,l2: longint;
  698. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  699. { that's the case, we can use rlwinm to do an AND operation }
  700. function get_rlwi_const: boolean;
  701. var
  702. temp, testbit: longint;
  703. compare: boolean;
  704. begin
  705. get_rlwi_const := false;
  706. { start with the lowest bit }
  707. testbit := 1;
  708. { check its value }
  709. compare := boolean(a and testbit);
  710. { find out how long the run of bits with this value is }
  711. { (it's impossible that all bits are 1 or 0, because in that case }
  712. { this function wouldn't have been called) }
  713. l1 := 31;
  714. while (((a and testbit) <> 0) = compare) do
  715. begin
  716. testbit := testbit shl 1;
  717. dec(l1);
  718. end;
  719. { check the length of the run of bits that comes next }
  720. compare := not compare;
  721. l2 := l1;
  722. while (((a and testbit) <> 0) = compare) and
  723. (l2 >= 0) do
  724. begin
  725. testbit := testbit shl 1;
  726. dec(l2);
  727. end;
  728. { and finally the check whether the rest of the bits all have the }
  729. { same value }
  730. compare := not compare;
  731. temp := l2;
  732. if temp >= 0 then
  733. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  734. exit;
  735. { we have done "not(not(compare))", so compare is back to its }
  736. { initial value. If the lowest bit was 0, a is of the form }
  737. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  738. { because l2 now contains the position of the last zero of the }
  739. { first run instead of that of the first 1) so switch l1 and l2 }
  740. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  741. if not compare then
  742. begin
  743. temp := l1;
  744. l1 := l2+1;
  745. l2 := temp;
  746. end
  747. else
  748. { otherwise, l1 currently contains the position of the last }
  749. { zero instead of that of the first 1 of the second run -> +1 }
  750. inc(l1);
  751. { the following is the same as "if l1 = -1 then l1 := 31;" }
  752. l1 := l1 and 31;
  753. l2 := l2 and 31;
  754. get_rlwi_const := true;
  755. end;
  756. var
  757. oplo, ophi: tasmop;
  758. scratchreg: tregister;
  759. useReg: boolean;
  760. begin
  761. ophi := TOpCG2AsmOpConstHi[op];
  762. oplo := TOpCG2AsmOpConstLo[op];
  763. { constants in a PPC instruction are always interpreted as signed }
  764. { 16bit values, so if the value is between low(smallint) and }
  765. { high(smallint), it's easy }
  766. if (longint(a) >= low(smallint)) and
  767. (longint(a) <= high(smallint)) then
  768. begin
  769. list.concat(taicpu.op_reg_reg_const(oplo,reg1,reg2,a));
  770. exit;
  771. end;
  772. { all basic constant instructions also have a shifted form that }
  773. { works only on the highest 16bits, so if low(a) is 0, we can }
  774. { use that one }
  775. if (low(a) = 0) then
  776. begin
  777. list.concat(taicpu.op_reg_reg(ophi,reg1,reg2,high(a)));
  778. exit;
  779. end;
  780. { otherwise, the instructions we can generate depend on the }
  781. { operation }
  782. useReg := false;
  783. case op of
  784. OP_ADD,OP_SUB:
  785. begin
  786. list.concat(taicpu.op_reg_reg_const(oplo,reg1,reg2,low(a)));
  787. list.concat(taicpu.op_reg_reg_const(ophi,reg1,reg1,
  788. high(a) + ord(smallint(a) < 0)));
  789. end;
  790. OP_OR:
  791. { try to use rlwimi }
  792. if get_rlwi_const then
  793. begin
  794. if reg1 <> reg2 then
  795. list.concat(taicpu.op_reg_reg(A_MR,reg1,reg2));
  796. scratch_reg := get_scratch_reg(list);
  797. list.concat(taicpu.op_reg_const(A_LI,scratch_reg,-1));
  798. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,reg1,
  799. reg2,0,l1,l2));
  800. free_scratch_reg(list,scratch_reg);
  801. end
  802. else
  803. useReg := true;
  804. OP_AND:
  805. { try to use rlwinm }
  806. if get_rlwi_const then
  807. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,reg1,
  808. reg2,0,l1,l2))
  809. else
  810. useReg := true;
  811. OP_XOR:
  812. useReg := true;
  813. else
  814. internalerror(200109091);
  815. end;
  816. { if all else failed, load the constant in a register and then }
  817. { perform the operation }
  818. if useReg then
  819. begin
  820. scratchreg := get_scratch_reg(list);
  821. a_load_const_reg(list,OS_32,a,scratchreg);
  822. a_op_reg_reg_reg(list,op,reg1,scratchreg,reg2);
  823. free_scratch_reg(list,scratchreg);
  824. end;
  825. end;
  826. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  827. dst, src1, src2: tregister);
  828. const
  829. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  830. (A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  831. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR)
  832. begin
  833. Case Op of
  834. OP_NEG,OP_NOT:
  835. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],size,dst,dst));
  836. else
  837. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],size,dst,src1,src2));
  838. end;
  839. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  840. ref: treference);
  841. var
  842. tmpreg: tregister;
  843. tmpref: treference;
  844. begin
  845. if assigned(ref.symbol) then
  846. begin
  847. tmpreg := get_scratch_reg(list);
  848. reset_reference(tmpref);
  849. tmpref.symbol := ref.symbol;
  850. tmpref.symaddr := refs_ha;
  851. tmpref.is_immediate := true;
  852. if ref.base <> R_NO then
  853. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  854. ref.base,newreference(tmpref)))
  855. else
  856. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  857. newreference(tmpref)));
  858. ref.base := tmpreg;
  859. ref.symaddr := refs_l;
  860. end;
  861. list.concat(taicpu.op_reg_ref(op,reg,newreference(ref)));
  862. if assigned(ref.symbol) then
  863. free_scratch_reg(list,tmpreg);
  864. end;
  865. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflags;
  866. l: pasmlabel);
  867. var
  868. p: paicpu;
  869. begin
  870. p := taicpu.op_sym(op,newasmsymbol(l.name));
  871. create_cond_norm(c,0,p.condition);
  872. list.concat(p)
  873. end;
  874. end.
  875. {
  876. $Log$
  877. Revision 1.8 2001-10-28 14:16:49 jonas
  878. * small fixes
  879. Revision 1.7 2001/09/29 21:33:30 jonas
  880. * small optimization
  881. Revision 1.6 2001/09/28 20:40:05 jonas
  882. * several additions, almost complete (only some problems with resflags left)
  883. Revision 1.5 2001/09/16 10:33:21 jonas
  884. * some fixes to operations with constants
  885. Revision 1.3 2001/09/06 15:25:55 jonas
  886. * changed type of tcg from object to class -> abstract methods are now
  887. a lot cleaner :)
  888. + more updates: load_*_loc methods, op_*_* methods, g_flags2reg method
  889. (if possible with generic implementation and necessary ppc
  890. implementations)
  891. * worked a bit further on cgflw, now working on exitnode
  892. Revision 1.2 2001/09/05 20:21:03 jonas
  893. * new cgflow based on n386flw with all nodes until forn "translated"
  894. + a_cmp_loc_*_label methods for tcg
  895. + base implementatino for a_cmp_ref_*_label methods
  896. * small bugfixes to powerpc cg
  897. Revision 1.1 2001/08/26 13:31:04 florian
  898. * some cg reorganisation
  899. * some PPC updates
  900. Revision 1.2 2001/08/26 13:29:33 florian
  901. * some cg reorganisation
  902. * some PPC updates
  903. Revision 1.1 2000/07/13 06:30:12 michael
  904. + Initial import
  905. Revision 1.12 2000/04/22 14:25:04 jonas
  906. * aasm.pas: pai_align instead of pai_align_abstract if cpu <> i386
  907. + systems.pas: info for macos/ppc
  908. * new/cgobj.pas: compiles again without newst define
  909. * new/powerpc/cgcpu: generate different entry/exit code depending on
  910. whether target_os is MacOs or Linux
  911. Revision 1.11 2000/01/07 01:14:57 peter
  912. * updated copyright to 2000
  913. Revision 1.10 1999/12/24 22:48:10 jonas
  914. * compiles again
  915. Revision 1.9 1999/11/05 07:05:56 jonas
  916. + a_jmp_cond()
  917. Revision 1.8 1999/10/24 09:22:18 jonas
  918. + entry/exitcode for SystemV (Linux) and AIX/Mac from the Altivec
  919. PIM (no AltiVec support yet though)
  920. * small fix to the a_cmp_* methods
  921. Revision 1.7 1999/10/20 12:23:24 jonas
  922. * fixed a_loadaddress_ref_reg (mentioned as ToDo in rev. 1.5)
  923. * small bugfix in a_load_store
  924. Revision 1.6 1999/09/15 20:35:47 florian
  925. * small fix to operator overloading when in MMX mode
  926. + the compiler uses now fldz and fld1 if possible
  927. + some fixes to floating point registers
  928. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  929. * .... ???
  930. Revision 1.5 1999/09/03 13:14:11 jonas
  931. + implemented some parameter passing methods, but they require
  932. some more helper routines
  933. * fix for loading symbol addresses (still needs to be done in a_loadaddress)
  934. * several changes to the way conditional branches are handled
  935. Revision 1.4 1999/08/26 14:53:41 jonas
  936. * first implementation of concatcopy (requires 4 scratch regs)
  937. Revision 1.3 1999/08/25 12:00:23 jonas
  938. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  939. Revision 1.2 1999/08/18 17:05:57 florian
  940. + implemented initilizing of data for the new code generator
  941. so it should compile now simple programs
  942. Revision 1.1 1999/08/06 16:41:11 jonas
  943. * PowerPC compiles again, several routines implemented in cgcpu.pas
  944. * added constant to cpubase of alpha and powerpc for maximum
  945. number of operands
  946. }