cgcpu.pas 40 KB

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