cpupara.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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) : tcgloc;
  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. paradef : tdef;
  114. stack_offset : aword;
  115. hp : tparaitem;
  116. loc : tcgloc;
  117. is_64bit: boolean;
  118. procedure assignintreg;
  119. begin
  120. if nextintreg.number<=NR_R10 then
  121. begin
  122. hp.paraloc.loc:=LOC_REGISTER;
  123. hp.paraloc.register:=nextintreg;
  124. inc(nextintreg.number,NR_R1-NR_R0);
  125. end
  126. else
  127. begin
  128. hp.paraloc.loc:=LOC_REFERENCE;
  129. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  130. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  131. hp.paraloc.reference.offset:=stack_offset;
  132. inc(stack_offset,4);
  133. end;
  134. end;
  135. begin
  136. nextintreg.enum:=R_INTREGISTER;
  137. nextintreg.number:=NR_R3;
  138. nextfloatreg.enum:=R_F1;
  139. nextmmreg.enum:=R_M1;
  140. stack_offset:=0;
  141. { frame pointer for nested procedures? }
  142. { inc(nextintreg); }
  143. { constructor? }
  144. { destructor? }
  145. hp:=tparaitem(p.para.first);
  146. while assigned(hp) do
  147. begin
  148. if (hp.paratyp in [vs_var,vs_out]) then
  149. paradef := voidpointertype.def
  150. else
  151. paradef := hp.paratype.def;
  152. loc:=getparaloc(paradef);
  153. hp.paraloc.sp_fixup:=0;
  154. case loc of
  155. LOC_REGISTER:
  156. begin
  157. hp.paraloc.size := def_cgsize(paradef);
  158. { for things like formaldef }
  159. if hp.paraloc.size = OS_NO then
  160. hp.paraloc.size := OS_ADDR;
  161. is_64bit := hp.paraloc.size in [OS_64,OS_S64];
  162. if nextintreg.number<=(NR_R10-ord(is_64bit)*(NR_R1-NR_R0)) then
  163. begin
  164. hp.paraloc.loc:=LOC_REGISTER;
  165. hp.paraloc.registerlow:=nextintreg;
  166. inc(nextintreg.number,NR_R1-NR_R0);
  167. if is_64bit then
  168. begin
  169. hp.paraloc.registerhigh:=nextintreg;
  170. inc(nextintreg.number,NR_R1-NR_R0);
  171. end;
  172. end
  173. else
  174. begin
  175. nextintreg.number := NR_R11;
  176. hp.paraloc.loc:=LOC_REFERENCE;
  177. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  178. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  179. hp.paraloc.reference.offset:=stack_offset;
  180. if not is_64bit then
  181. inc(stack_offset,4)
  182. else
  183. inc(stack_offset,8);
  184. end;
  185. end;
  186. LOC_FPUREGISTER:
  187. begin
  188. if nextfloatreg.enum<=R_F10 then
  189. begin
  190. hp.paraloc.size:=def_cgsize(paradef);
  191. hp.paraloc.loc:=LOC_FPUREGISTER;
  192. hp.paraloc.register:=nextfloatreg;
  193. inc(nextfloatreg.enum);
  194. end
  195. else
  196. begin
  197. {!!!!!!!}
  198. hp.paraloc.size:=def_cgsize(paradef);
  199. internalerror(2002071004);
  200. end;
  201. end;
  202. LOC_REFERENCE:
  203. begin
  204. hp.paraloc.size:=OS_ADDR;
  205. if push_addr_param(paradef,p.proccalloption) or
  206. is_open_array(paradef) or
  207. is_array_of_const(paradef) then
  208. assignintreg
  209. else
  210. begin
  211. hp.paraloc.loc:=LOC_REFERENCE;
  212. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  213. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  214. hp.paraloc.reference.offset:=stack_offset;
  215. inc(stack_offset,hp.paratype.def.size);
  216. end;
  217. end;
  218. else
  219. internalerror(2002071002);
  220. end;
  221. hp:=tparaitem(hp.next);
  222. end;
  223. end;
  224. function tppcparamanager.getfuncretparaloc(p : tabstractprocdef) : tparalocation;
  225. begin
  226. case p.rettype.def.deftype of
  227. orddef,
  228. enumdef:
  229. begin
  230. getfuncretparaloc.loc:=LOC_REGISTER;
  231. getfuncretparaloc.register.enum:=R_INTREGISTER;
  232. getfuncretparaloc.register.number:=NR_R3;
  233. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  234. if getfuncretparaloc.size in [OS_S64,OS_64] then
  235. begin
  236. getfuncretparaloc.registerhigh.enum:=R_INTREGISTER;
  237. getfuncretparaloc.registerhigh.number:=NR_R4;
  238. end;
  239. end;
  240. floatdef:
  241. begin
  242. getfuncretparaloc.loc:=LOC_FPUREGISTER;
  243. getfuncretparaloc.register.enum:=R_F1;
  244. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  245. end;
  246. { smallsets are OS_INT in R3, others are OS_ADDR in R3 -> the same }
  247. { ugly, I know :) (JM) }
  248. setdef,
  249. variantdef,
  250. pointerdef,
  251. formaldef,
  252. classrefdef,
  253. recorddef,
  254. objectdef,
  255. stringdef,
  256. procvardef,
  257. filedef,
  258. arraydef,
  259. errordef:
  260. begin
  261. getfuncretparaloc.loc:=LOC_REGISTER;
  262. getfuncretparaloc.register.enum:=R_INTREGISTER;
  263. getfuncretparaloc.register.number:=NR_R3;
  264. getfuncretparaloc.size:=OS_ADDR;
  265. end;
  266. else
  267. internalerror(2002090903);
  268. end;
  269. end;
  270. begin
  271. paramanager:=tppcparamanager.create;
  272. end.
  273. {
  274. $Log$
  275. Revision 1.27 2003-04-26 11:30:59 florian
  276. * fixed the powerpc to work with the new function result handling
  277. Revision 1.26 2003/04/23 12:35:35 florian
  278. * fixed several issues with powerpc
  279. + applied a patch from Jonas for nested function calls (PowerPC only)
  280. * ...
  281. Revision 1.25 2003/04/17 18:52:35 jonas
  282. * process para's from first to last instead of the other way round
  283. Revision 1.24 2003/04/16 07:55:07 jonas
  284. * fixed paralocation for integer var/out parameters
  285. Revision 1.23 2003/03/11 21:46:24 jonas
  286. * lots of new regallocator fixes, both in generic and ppc-specific code
  287. (ppc compiler still can't compile the linux system unit though)
  288. Revision 1.22 2003/01/09 22:00:53 florian
  289. * fixed some PowerPC issues
  290. Revision 1.21 2003/01/09 20:41:10 florian
  291. * fixed broken PowerPC compiler
  292. Revision 1.20 2003/01/09 11:22:14 olle
  293. * made powerpc compiler compile after Daniels Tregister modification
  294. Revision 1.19 2003/01/08 18:43:58 daniel
  295. * Tregister changed into a record
  296. Revision 1.18 2002/12/15 19:22:01 florian
  297. * fixed some crashes and a rte 201
  298. Revision 1.17 2002/11/25 17:43:27 peter
  299. * splitted defbase in defutil,symutil,defcmp
  300. * merged isconvertable and is_equal into compare_defs(_ext)
  301. * made operator search faster by walking the list only once
  302. Revision 1.16 2002/11/18 17:32:01 peter
  303. * pass proccalloption to ret_in_xxx and push_xxx functions
  304. Revision 1.15 2002/10/02 13:33:36 jonas
  305. + set, variant support in getfuncretparaloc
  306. Revision 1.14 2002/09/28 21:27:16 florian
  307. + getparaloc supports now sets and variants
  308. Revision 1.13 2002/09/10 21:28:05 jonas
  309. * int64 paras are now handled correctly (until the registers are used up
  310. anyway :)
  311. * the return location is now initialized correctly
  312. * fixed bug where ret_in_reg() was called for the procdefinition instead
  313. of for the result of the procedure
  314. Revision 1.12 2002/09/09 09:11:37 florian
  315. - removed passes_parameters_in_reg
  316. Revision 1.11 2002/09/07 17:54:59 florian
  317. * first part of PowerPC fixes
  318. Revision 1.10 2002/09/01 21:04:49 florian
  319. * several powerpc related stuff fixed
  320. Revision 1.9 2002/08/31 12:43:31 florian
  321. * ppc compilation fixed
  322. Revision 1.8 2002/08/18 10:42:38 florian
  323. * remaining assembler writer bugs fixed, the errors in the
  324. system unit are inline assembler problems
  325. Revision 1.7 2002/08/17 22:09:47 florian
  326. * result type handling in tcgcal.pass_2 overhauled
  327. * better tnode.dowrite
  328. * some ppc stuff fixed
  329. Revision 1.6 2002/08/13 21:40:58 florian
  330. * more fixes for ppc calling conventions
  331. Revision 1.5 2002/07/30 20:50:44 florian
  332. * the code generator knows now if parameters are in registers
  333. Revision 1.4 2002/07/28 20:45:22 florian
  334. + added direct assembler reader for PowerPC
  335. Revision 1.3 2002/07/26 22:22:10 florian
  336. * several PowerPC related fixes to get forward with system unit compilation
  337. Revision 1.2 2002/07/11 14:41:34 florian
  338. * start of the new generic parameter handling
  339. Revision 1.1 2002/07/07 09:44:32 florian
  340. * powerpc target fixed, very simple units can be compiled
  341. }