cpupara.pas 14 KB

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