cpupara.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. defbase;
  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:=tregister(longint(R_2)+nr);
  47. end
  48. else
  49. begin
  50. result.loc:=LOC_REFERENCE;
  51. result.reference.index:=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. { avoid problems with errornous definitions }
  95. errordef:
  96. getparaloc:=LOC_REGISTER;
  97. else
  98. internalerror(2002071001);
  99. end;
  100. end;
  101. procedure tppcparamanager.create_param_loc_info(p : tabstractprocdef);
  102. var
  103. nextintreg,nextfloatreg,nextmmreg : tregister;
  104. stack_offset : aword;
  105. hp : tparaitem;
  106. loc : tloc;
  107. is_64bit: boolean;
  108. begin
  109. nextintreg:=R_3;
  110. nextfloatreg:=R_F1;
  111. nextmmreg:=R_M1;
  112. stack_offset:=0;
  113. { pointer for structured results ? }
  114. if not is_void(p.rettype.def) then
  115. begin
  116. if not(ret_in_reg(p.rettype.def)) then
  117. inc(nextintreg);
  118. end;
  119. { frame pointer for nested procedures? }
  120. { inc(nextintreg); }
  121. { constructor? }
  122. { destructor? }
  123. hp:=tparaitem(p.para.last);
  124. while assigned(hp) do
  125. begin
  126. loc:=getparaloc(hp.paratype.def);
  127. hp.paraloc.sp_fixup:=0;
  128. case loc of
  129. LOC_REGISTER:
  130. begin
  131. hp.paraloc.size := def_cgsize(hp.paratype.def);
  132. { for things like formaldef }
  133. if hp.paraloc.size = OS_NO then
  134. hp.paraloc.size := OS_ADDR;
  135. is_64bit := hp.paraloc.size in [OS_64,OS_S64];
  136. if nextintreg<=tregister(ord(R_10)-ord(is_64bit)) then
  137. begin
  138. hp.paraloc.loc:=LOC_REGISTER;
  139. hp.paraloc.registerlow:=nextintreg;
  140. inc(nextintreg);
  141. if is_64bit then
  142. begin
  143. hp.paraloc.registerhigh:=nextintreg;
  144. inc(nextintreg);
  145. end;
  146. end
  147. else
  148. begin
  149. nextintreg := R_11;
  150. hp.paraloc.loc:=LOC_REFERENCE;
  151. hp.paraloc.reference.index:=stack_pointer_reg;
  152. hp.paraloc.reference.offset:=stack_offset;
  153. if not is_64bit then
  154. inc(stack_offset,4)
  155. else
  156. inc(stack_offset,8);
  157. end;
  158. end;
  159. LOC_FPUREGISTER:
  160. begin
  161. if hp.paratyp in [vs_var,vs_out] then
  162. begin
  163. if nextintreg<=R_10 then
  164. begin
  165. hp.paraloc.size:=OS_ADDR;
  166. hp.paraloc.loc:=LOC_REGISTER;
  167. hp.paraloc.register:=nextintreg;
  168. inc(nextintreg);
  169. end
  170. else
  171. begin
  172. {!!!!!!!}
  173. hp.paraloc.size:=def_cgsize(hp.paratype.def);
  174. internalerror(2002071006);
  175. end;
  176. end
  177. else if nextfloatreg<=R_F10 then
  178. begin
  179. hp.paraloc.size:=def_cgsize(hp.paratype.def);
  180. hp.paraloc.loc:=LOC_FPUREGISTER;
  181. hp.paraloc.register:=nextfloatreg;
  182. inc(nextfloatreg);
  183. end
  184. else
  185. begin
  186. {!!!!!!!}
  187. hp.paraloc.size:=def_cgsize(hp.paratype.def);
  188. internalerror(2002071004);
  189. end;
  190. end;
  191. LOC_REFERENCE:
  192. begin
  193. hp.paraloc.size:=OS_ADDR;
  194. if push_addr_param(hp.paratype.def,p.proccalloption in [pocall_cdecl,pocall_cppdecl]) or (hp.paratyp in [vs_var,vs_out]) then
  195. begin
  196. if nextintreg<=R_10 then
  197. begin
  198. hp.paraloc.loc:=LOC_REGISTER;
  199. hp.paraloc.register:=nextintreg;
  200. inc(nextintreg);
  201. end
  202. else
  203. begin
  204. hp.paraloc.loc:=LOC_REFERENCE;
  205. hp.paraloc.reference.index:=stack_pointer_reg;
  206. hp.paraloc.reference.offset:=stack_offset;
  207. inc(stack_offset,4);
  208. end;
  209. end
  210. else
  211. begin
  212. hp.paraloc.loc:=LOC_REFERENCE;
  213. hp.paraloc.reference.index:=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.previous);
  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:=R_3;
  232. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  233. if getfuncretparaloc.size in [OS_S64,OS_64] then
  234. getfuncretparaloc.registerhigh:=R_4;
  235. end;
  236. floatdef:
  237. begin
  238. getfuncretparaloc.loc:=LOC_FPUREGISTER;
  239. getfuncretparaloc.register:=R_F1;
  240. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  241. end;
  242. pointerdef,
  243. formaldef,
  244. classrefdef,
  245. recorddef,
  246. objectdef,
  247. stringdef,
  248. procvardef,
  249. filedef,
  250. arraydef,
  251. errordef:
  252. begin
  253. getfuncretparaloc.loc:=LOC_REGISTER;
  254. getfuncretparaloc.register:=R_3;
  255. getfuncretparaloc.size:=OS_ADDR;
  256. end;
  257. else
  258. internalerror(2002090903);
  259. end;
  260. end;
  261. begin
  262. paramanager:=tppcparamanager.create;
  263. end.
  264. {
  265. $Log$
  266. Revision 1.13 2002-09-10 21:28:05 jonas
  267. * int64 paras are now handled correctly (until the registers are used up
  268. anyway :)
  269. * the return location is now initialized correctly
  270. * fixed bug where ret_in_reg() was called for the procdefinition instead
  271. of for the result of the procedure
  272. Revision 1.12 2002/09/09 09:11:37 florian
  273. - removed passes_parameters_in_reg
  274. Revision 1.11 2002/09/07 17:54:59 florian
  275. * first part of PowerPC fixes
  276. Revision 1.10 2002/09/01 21:04:49 florian
  277. * several powerpc related stuff fixed
  278. Revision 1.9 2002/08/31 12:43:31 florian
  279. * ppc compilation fixed
  280. Revision 1.8 2002/08/18 10:42:38 florian
  281. * remaining assembler writer bugs fixed, the errors in the
  282. system unit are inline assembler problems
  283. Revision 1.7 2002/08/17 22:09:47 florian
  284. * result type handling in tcgcal.pass_2 overhauled
  285. * better tnode.dowrite
  286. * some ppc stuff fixed
  287. Revision 1.6 2002/08/13 21:40:58 florian
  288. * more fixes for ppc calling conventions
  289. Revision 1.5 2002/07/30 20:50:44 florian
  290. * the code generator knows now if parameters are in registers
  291. Revision 1.4 2002/07/28 20:45:22 florian
  292. + added direct assembler reader for PowerPC
  293. Revision 1.3 2002/07/26 22:22:10 florian
  294. * several PowerPC related fixes to get forward with system unit compilation
  295. Revision 1.2 2002/07/11 14:41:34 florian
  296. * start of the new generic parameter handling
  297. Revision 1.1 2002/07/07 09:44:32 florian
  298. * powerpc target fixed, very simple units can be compiled
  299. }