cpupara.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 (hp.paratyp in [vs_var,vs_out]) then
  218. assignintreg
  219. else
  220. begin
  221. hp.paraloc.loc:=LOC_REFERENCE;
  222. hp.paraloc.reference.index.enum:=stack_pointer_reg;
  223. hp.paraloc.reference.offset:=stack_offset;
  224. inc(stack_offset,hp.paratype.def.size);
  225. end;
  226. end;
  227. else
  228. internalerror(2002071002);
  229. end;
  230. hp:=tparaitem(hp.previous);
  231. end;
  232. end;
  233. function tppcparamanager.getfuncretparaloc(p : tabstractprocdef) : tparalocation;
  234. begin
  235. case p.rettype.def.deftype of
  236. orddef,
  237. enumdef:
  238. begin
  239. getfuncretparaloc.loc:=LOC_REGISTER;
  240. getfuncretparaloc.register.enum:=R_3;
  241. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  242. if getfuncretparaloc.size in [OS_S64,OS_64] then
  243. getfuncretparaloc.registerhigh.enum:=R_4;
  244. end;
  245. floatdef:
  246. begin
  247. getfuncretparaloc.loc:=LOC_FPUREGISTER;
  248. getfuncretparaloc.register.enum:=R_F1;
  249. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  250. end;
  251. { smallsets are OS_INT in R3, others are OS_ADDR in R3 -> the same }
  252. { ugly, I know :) (JM) }
  253. setdef,
  254. variantdef,
  255. pointerdef,
  256. formaldef,
  257. classrefdef,
  258. recorddef,
  259. objectdef,
  260. stringdef,
  261. procvardef,
  262. filedef,
  263. arraydef,
  264. errordef:
  265. begin
  266. getfuncretparaloc.loc:=LOC_REGISTER;
  267. getfuncretparaloc.register.enum:=R_3;
  268. getfuncretparaloc.size:=OS_ADDR;
  269. end;
  270. else
  271. internalerror(2002090903);
  272. end;
  273. end;
  274. begin
  275. paramanager:=tppcparamanager.create;
  276. end.
  277. {
  278. $Log$
  279. Revision 1.21 2003-01-09 20:41:10 florian
  280. * fixed broken PowerPC compiler
  281. Revision 1.20 2003/01/09 11:22:14 olle
  282. * made powerpc compiler compile after Daniels Tregister modification
  283. Revision 1.19 2003/01/08 18:43:58 daniel
  284. * Tregister changed into a record
  285. Revision 1.18 2002/12/15 19:22:01 florian
  286. * fixed some crashes and a rte 201
  287. Revision 1.17 2002/11/25 17:43:27 peter
  288. * splitted defbase in defutil,symutil,defcmp
  289. * merged isconvertable and is_equal into compare_defs(_ext)
  290. * made operator search faster by walking the list only once
  291. Revision 1.16 2002/11/18 17:32:01 peter
  292. * pass proccalloption to ret_in_xxx and push_xxx functions
  293. Revision 1.15 2002/10/02 13:33:36 jonas
  294. + set, variant support in getfuncretparaloc
  295. Revision 1.14 2002/09/28 21:27:16 florian
  296. + getparaloc supports now sets and variants
  297. Revision 1.13 2002/09/10 21:28:05 jonas
  298. * int64 paras are now handled correctly (until the registers are used up
  299. anyway :)
  300. * the return location is now initialized correctly
  301. * fixed bug where ret_in_reg() was called for the procdefinition instead
  302. of for the result of the procedure
  303. Revision 1.12 2002/09/09 09:11:37 florian
  304. - removed passes_parameters_in_reg
  305. Revision 1.11 2002/09/07 17:54:59 florian
  306. * first part of PowerPC fixes
  307. Revision 1.10 2002/09/01 21:04:49 florian
  308. * several powerpc related stuff fixed
  309. Revision 1.9 2002/08/31 12:43:31 florian
  310. * ppc compilation fixed
  311. Revision 1.8 2002/08/18 10:42:38 florian
  312. * remaining assembler writer bugs fixed, the errors in the
  313. system unit are inline assembler problems
  314. Revision 1.7 2002/08/17 22:09:47 florian
  315. * result type handling in tcgcal.pass_2 overhauled
  316. * better tnode.dowrite
  317. * some ppc stuff fixed
  318. Revision 1.6 2002/08/13 21:40:58 florian
  319. * more fixes for ppc calling conventions
  320. Revision 1.5 2002/07/30 20:50:44 florian
  321. * the code generator knows now if parameters are in registers
  322. Revision 1.4 2002/07/28 20:45:22 florian
  323. + added direct assembler reader for PowerPC
  324. Revision 1.3 2002/07/26 22:22:10 florian
  325. * several PowerPC related fixes to get forward with system unit compilation
  326. Revision 1.2 2002/07/11 14:41:34 florian
  327. * start of the new generic parameter handling
  328. Revision 1.1 2002/07/07 09:44:32 florian
  329. * powerpc target fixed, very simple units can be compiled
  330. }