cpupara.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. PowerPC specific calling conventions
  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. { PowerPC specific calling conventions are handled by this unit
  19. }
  20. unit cpupara;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cpubase,
  25. symconst,symbase,symtype,symdef,paramgr;
  26. type
  27. tppcparamanager = class(tparamanager)
  28. function getintparaloc(nr : longint) : tparalocation;override;
  29. procedure create_param_loc_info(p : tabstractprocdef);override;
  30. function getfuncretparaloc(p : tabstractprocdef) : tparalocation;override;
  31. end;
  32. implementation
  33. uses
  34. verbose,
  35. globtype,
  36. cpuinfo,cginfo,cgbase,
  37. defutil;
  38. function tppcparamanager.getintparaloc(nr : longint) : tparalocation;
  39. begin
  40. fillchar(result,sizeof(tparalocation),0);
  41. if nr<1 then
  42. internalerror(2002070801)
  43. else if nr<=8 then
  44. begin
  45. result.loc:=LOC_REGISTER;
  46. result.register.enum:=R_INTREGISTER;
  47. result.register.number:=NR_R2+nr*(NR_R1-NR_R0);
  48. end
  49. else
  50. begin
  51. result.loc:=LOC_REFERENCE;
  52. result.reference.index.enum:=R_INTREGISTER;
  53. result.reference.index.number:=NR_STACK_POINTER_REG;
  54. result.reference.offset:=(nr-8)*4;
  55. end;
  56. end;
  57. function getparaloc(p : tdef) : tloc;
  58. begin
  59. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  60. if push_addr_param for the def is true
  61. }
  62. case p.deftype of
  63. orddef:
  64. getparaloc:=LOC_REGISTER;
  65. floatdef:
  66. getparaloc:=LOC_FPUREGISTER;
  67. enumdef:
  68. getparaloc:=LOC_REGISTER;
  69. pointerdef:
  70. getparaloc:=LOC_REGISTER;
  71. formaldef:
  72. getparaloc:=LOC_REGISTER;
  73. classrefdef:
  74. getparaloc:=LOC_REGISTER;
  75. recorddef:
  76. getparaloc:=LOC_REFERENCE;
  77. objectdef:
  78. if is_object(p) then
  79. getparaloc:=LOC_REFERENCE
  80. else
  81. getparaloc:=LOC_REGISTER;
  82. stringdef:
  83. if is_shortstring(p) or is_longstring(p) then
  84. getparaloc:=LOC_REFERENCE
  85. else
  86. getparaloc:=LOC_REGISTER;
  87. procvardef:
  88. if (po_methodpointer in tprocvardef(p).procoptions) then
  89. getparaloc:=LOC_REFERENCE
  90. else
  91. getparaloc:=LOC_REGISTER;
  92. filedef:
  93. getparaloc:=LOC_REGISTER;
  94. arraydef:
  95. getparaloc:=LOC_REFERENCE;
  96. setdef:
  97. if is_smallset(p) then
  98. getparaloc:=LOC_REGISTER
  99. else
  100. getparaloc:=LOC_REFERENCE;
  101. variantdef:
  102. getparaloc:=LOC_REFERENCE;
  103. { avoid problems with errornous definitions }
  104. errordef:
  105. getparaloc:=LOC_REGISTER;
  106. else
  107. internalerror(2002071001);
  108. end;
  109. end;
  110. procedure tppcparamanager.create_param_loc_info(p : tabstractprocdef);
  111. var
  112. nextintreg,nextfloatreg,nextmmreg : tregister;
  113. stack_offset : aword;
  114. hp : tparaitem;
  115. loc : tloc;
  116. is_64bit: boolean;
  117. procedure assignintreg;
  118. begin
  119. if nextintreg.number<=NR_R10 then
  120. begin
  121. hp.paraloc.loc:=LOC_REGISTER;
  122. hp.paraloc.register:=nextintreg;
  123. inc(nextintreg.number,NR_R1-NR_R0);
  124. end
  125. else
  126. begin
  127. hp.paraloc.loc:=LOC_REFERENCE;
  128. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  129. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  130. hp.paraloc.reference.offset:=stack_offset;
  131. inc(stack_offset,4);
  132. end;
  133. end;
  134. begin
  135. nextintreg.enum:=R_INTREGISTER;
  136. nextintreg.number:=NR_R3;
  137. nextfloatreg.enum:=R_F1;
  138. nextmmreg.enum:=R_M1;
  139. stack_offset:=0;
  140. { pointer for structured results ? }
  141. if not is_void(p.rettype.def) then
  142. begin
  143. if not(ret_in_reg(p.rettype.def,p.proccalloption)) then
  144. inc(nextintreg.number,NR_R1-NR_R0);
  145. end;
  146. { frame pointer for nested procedures? }
  147. { inc(nextintreg); }
  148. { constructor? }
  149. { destructor? }
  150. hp:=tparaitem(p.para.last);
  151. while assigned(hp) do
  152. begin
  153. loc:=getparaloc(hp.paratype.def);
  154. hp.paraloc.sp_fixup:=0;
  155. case loc of
  156. LOC_REGISTER:
  157. begin
  158. hp.paraloc.size := def_cgsize(hp.paratype.def);
  159. { for things like formaldef }
  160. if hp.paraloc.size = OS_NO then
  161. hp.paraloc.size := OS_ADDR;
  162. is_64bit := hp.paraloc.size in [OS_64,OS_S64];
  163. if nextintreg.number<=(NR_R10-ord(is_64bit)*(NR_R1-NR_R0)) then
  164. begin
  165. hp.paraloc.loc:=LOC_REGISTER;
  166. hp.paraloc.registerlow:=nextintreg;
  167. inc(nextintreg.number,NR_R1-NR_R0);
  168. if is_64bit then
  169. begin
  170. hp.paraloc.registerhigh:=nextintreg;
  171. inc(nextintreg.number,NR_R1-NR_R0);
  172. end;
  173. end
  174. else
  175. begin
  176. nextintreg.number := NR_R11;
  177. hp.paraloc.loc:=LOC_REFERENCE;
  178. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  179. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  180. hp.paraloc.reference.offset:=stack_offset;
  181. if not is_64bit then
  182. inc(stack_offset,4)
  183. else
  184. inc(stack_offset,8);
  185. end;
  186. end;
  187. LOC_FPUREGISTER:
  188. begin
  189. if hp.paratyp in [vs_var,vs_out] then
  190. begin
  191. if nextintreg.number<=NR_R10 then
  192. begin
  193. hp.paraloc.size:=OS_ADDR;
  194. hp.paraloc.loc:=LOC_REGISTER;
  195. hp.paraloc.register:=nextintreg;
  196. inc(nextintreg.number,NR_R1-NR_R0);
  197. end
  198. else
  199. begin
  200. {!!!!!!!}
  201. hp.paraloc.size:=def_cgsize(hp.paratype.def);
  202. internalerror(2002071006);
  203. end;
  204. end
  205. else if nextfloatreg.enum<=R_F10 then
  206. begin
  207. hp.paraloc.size:=def_cgsize(hp.paratype.def);
  208. hp.paraloc.loc:=LOC_FPUREGISTER;
  209. hp.paraloc.register:=nextfloatreg;
  210. inc(nextfloatreg.enum);
  211. end
  212. else
  213. begin
  214. {!!!!!!!}
  215. hp.paraloc.size:=def_cgsize(hp.paratype.def);
  216. internalerror(2002071004);
  217. end;
  218. end;
  219. LOC_REFERENCE:
  220. begin
  221. hp.paraloc.size:=OS_ADDR;
  222. if push_addr_param(hp.paratype.def,p.proccalloption) or
  223. is_open_array(hp.paratype.def) or
  224. is_array_of_const(hp.paratype.def) or
  225. (hp.paratyp in [vs_var,vs_out]) then
  226. assignintreg
  227. else
  228. begin
  229. hp.paraloc.loc:=LOC_REFERENCE;
  230. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  231. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  232. hp.paraloc.reference.offset:=stack_offset;
  233. inc(stack_offset,hp.paratype.def.size);
  234. end;
  235. end;
  236. else
  237. internalerror(2002071002);
  238. end;
  239. hp:=tparaitem(hp.previous);
  240. end;
  241. end;
  242. function tppcparamanager.getfuncretparaloc(p : tabstractprocdef) : tparalocation;
  243. begin
  244. case p.rettype.def.deftype of
  245. orddef,
  246. enumdef:
  247. begin
  248. getfuncretparaloc.loc:=LOC_REGISTER;
  249. getfuncretparaloc.register.enum:=R_INTREGISTER;
  250. getfuncretparaloc.register.number:=NR_R3;
  251. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  252. if getfuncretparaloc.size in [OS_S64,OS_64] then
  253. begin
  254. getfuncretparaloc.registerhigh.enum:=R_INTREGISTER;
  255. getfuncretparaloc.registerhigh.number:=NR_R4;
  256. end;
  257. end;
  258. floatdef:
  259. begin
  260. getfuncretparaloc.loc:=LOC_FPUREGISTER;
  261. getfuncretparaloc.register.enum:=R_F1;
  262. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  263. end;
  264. { smallsets are OS_INT in R3, others are OS_ADDR in R3 -> the same }
  265. { ugly, I know :) (JM) }
  266. setdef,
  267. variantdef,
  268. pointerdef,
  269. formaldef,
  270. classrefdef,
  271. recorddef,
  272. objectdef,
  273. stringdef,
  274. procvardef,
  275. filedef,
  276. arraydef,
  277. errordef:
  278. begin
  279. getfuncretparaloc.loc:=LOC_REGISTER;
  280. getfuncretparaloc.register.enum:=R_INTREGISTER;
  281. getfuncretparaloc.register.number:=NR_R3;
  282. getfuncretparaloc.size:=OS_ADDR;
  283. end;
  284. else
  285. internalerror(2002090903);
  286. end;
  287. end;
  288. begin
  289. paramanager:=tppcparamanager.create;
  290. end.
  291. {
  292. $Log$
  293. Revision 1.23 2003-03-11 21:46:24 jonas
  294. * lots of new regallocator fixes, both in generic and ppc-specific code
  295. (ppc compiler still can't compile the linux system unit though)
  296. Revision 1.22 2003/01/09 22:00:53 florian
  297. * fixed some PowerPC issues
  298. Revision 1.21 2003/01/09 20:41:10 florian
  299. * fixed broken PowerPC compiler
  300. Revision 1.20 2003/01/09 11:22:14 olle
  301. * made powerpc compiler compile after Daniels Tregister modification
  302. Revision 1.19 2003/01/08 18:43:58 daniel
  303. * Tregister changed into a record
  304. Revision 1.18 2002/12/15 19:22:01 florian
  305. * fixed some crashes and a rte 201
  306. Revision 1.17 2002/11/25 17:43:27 peter
  307. * splitted defbase in defutil,symutil,defcmp
  308. * merged isconvertable and is_equal into compare_defs(_ext)
  309. * made operator search faster by walking the list only once
  310. Revision 1.16 2002/11/18 17:32:01 peter
  311. * pass proccalloption to ret_in_xxx and push_xxx functions
  312. Revision 1.15 2002/10/02 13:33:36 jonas
  313. + set, variant support in getfuncretparaloc
  314. Revision 1.14 2002/09/28 21:27:16 florian
  315. + getparaloc supports now sets and variants
  316. Revision 1.13 2002/09/10 21:28:05 jonas
  317. * int64 paras are now handled correctly (until the registers are used up
  318. anyway :)
  319. * the return location is now initialized correctly
  320. * fixed bug where ret_in_reg() was called for the procdefinition instead
  321. of for the result of the procedure
  322. Revision 1.12 2002/09/09 09:11:37 florian
  323. - removed passes_parameters_in_reg
  324. Revision 1.11 2002/09/07 17:54:59 florian
  325. * first part of PowerPC fixes
  326. Revision 1.10 2002/09/01 21:04:49 florian
  327. * several powerpc related stuff fixed
  328. Revision 1.9 2002/08/31 12:43:31 florian
  329. * ppc compilation fixed
  330. Revision 1.8 2002/08/18 10:42:38 florian
  331. * remaining assembler writer bugs fixed, the errors in the
  332. system unit are inline assembler problems
  333. Revision 1.7 2002/08/17 22:09:47 florian
  334. * result type handling in tcgcal.pass_2 overhauled
  335. * better tnode.dowrite
  336. * some ppc stuff fixed
  337. Revision 1.6 2002/08/13 21:40:58 florian
  338. * more fixes for ppc calling conventions
  339. Revision 1.5 2002/07/30 20:50:44 florian
  340. * the code generator knows now if parameters are in registers
  341. Revision 1.4 2002/07/28 20:45:22 florian
  342. + added direct assembler reader for PowerPC
  343. Revision 1.3 2002/07/26 22:22:10 florian
  344. * several PowerPC related fixes to get forward with system unit compilation
  345. Revision 1.2 2002/07/11 14:41:34 florian
  346. * start of the new generic parameter handling
  347. Revision 1.1 2002/07/07 09:44:32 florian
  348. * powerpc target fixed, very simple units can be compiled
  349. }