cgcpu.pas 39 KB

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