cpupara.pas 13 KB

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