cpupara.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. {
  2. $Id$
  3. Copyright (c) 2003 by Florian Klaempfl
  4. ARM 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. { ARM specific calling conventions are handled by this unit
  19. }
  20. unit cpupara;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. aasmtai,
  26. cpubase,
  27. symconst,symbase,symtype,symdef,paramgr;
  28. type
  29. tarmparamanager = class(tparamanager)
  30. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  31. function getintparaloc(calloption : tproccalloption; nr : longint) : tparalocation;override;
  32. // procedure freeintparaloc(list: taasmoutput; nr : longint); override;
  33. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;override;
  34. end;
  35. implementation
  36. uses
  37. verbose,systems,
  38. cpuinfo,cgbase,
  39. rgobj,
  40. defutil,symsym;
  41. function tarmparamanager.getintparaloc(calloption : tproccalloption; nr : longint) : tparalocation;
  42. begin
  43. fillchar(result,sizeof(tparalocation),0);
  44. if nr<1 then
  45. internalerror(2002070801)
  46. else if nr<=4 then
  47. begin
  48. result.loc:=LOC_REGISTER;
  49. result.register:=newreg(R_INTREGISTER,RS_R0+nr,R_SUBWHOLE);
  50. end
  51. else
  52. begin
  53. result.loc:=LOC_REFERENCE;
  54. result.reference.index:=NR_STACK_POINTER_REG;
  55. result.reference.offset:=(nr-4)*4;
  56. end;
  57. result.size := OS_INT;
  58. end;
  59. {
  60. procedure tarmparamanager.freeintparaloc(list: taasmoutput; nr : longint);
  61. var
  62. r: tregister;
  63. begin
  64. if nr<1 then
  65. internalerror(2003060401)
  66. else if nr<=4 then
  67. begin
  68. r:=newreg(R_INTREGISTER,RS_R0+nr,R_SUBWHOLE);
  69. rg.ungetregisterint(list,r);
  70. end;
  71. end;
  72. }
  73. function getparaloc(p : tdef) : tcgloc;
  74. begin
  75. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  76. if push_addr_param for the def is true
  77. }
  78. case p.deftype of
  79. orddef:
  80. getparaloc:=LOC_REGISTER;
  81. floatdef:
  82. getparaloc:=LOC_FPUREGISTER;
  83. enumdef:
  84. getparaloc:=LOC_REGISTER;
  85. pointerdef:
  86. getparaloc:=LOC_REGISTER;
  87. formaldef:
  88. getparaloc:=LOC_REGISTER;
  89. classrefdef:
  90. getparaloc:=LOC_REGISTER;
  91. recorddef:
  92. getparaloc:=LOC_REFERENCE;
  93. objectdef:
  94. if is_object(p) then
  95. getparaloc:=LOC_REFERENCE
  96. else
  97. getparaloc:=LOC_REGISTER;
  98. stringdef:
  99. if is_shortstring(p) or is_longstring(p) then
  100. getparaloc:=LOC_REFERENCE
  101. else
  102. getparaloc:=LOC_REGISTER;
  103. procvardef:
  104. if (po_methodpointer in tprocvardef(p).procoptions) then
  105. getparaloc:=LOC_REFERENCE
  106. else
  107. getparaloc:=LOC_REGISTER;
  108. filedef:
  109. getparaloc:=LOC_REGISTER;
  110. arraydef:
  111. getparaloc:=LOC_REFERENCE;
  112. setdef:
  113. if is_smallset(p) then
  114. getparaloc:=LOC_REGISTER
  115. else
  116. getparaloc:=LOC_REFERENCE;
  117. variantdef:
  118. getparaloc:=LOC_REFERENCE;
  119. { avoid problems with errornous definitions }
  120. errordef:
  121. getparaloc:=LOC_REGISTER;
  122. else
  123. internalerror(2002071001);
  124. end;
  125. end;
  126. function tarmparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  127. begin
  128. if varspez in [vs_var,vs_out] then
  129. begin
  130. result:=true;
  131. exit;
  132. end;
  133. case def.deftype of
  134. recorddef:
  135. result:=true;
  136. arraydef:
  137. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  138. is_open_array(def) or
  139. is_array_of_const(def) or
  140. is_array_constructor(def);
  141. setdef :
  142. result:=(tsetdef(def).settype<>smallset);
  143. stringdef :
  144. result:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  145. procvardef :
  146. result:=po_methodpointer in tprocvardef(def).procoptions;
  147. else
  148. result:=inherited push_addr_param(varspez,def,calloption);
  149. end;
  150. end;
  151. function tarmparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  152. var
  153. nextintreg,nextfloatreg,nextmmreg : tregister;
  154. paradef : tdef;
  155. paraloc : tparalocation;
  156. stack_offset : aword;
  157. hp : tparaitem;
  158. loc : tcgloc;
  159. is_64bit: boolean;
  160. procedure assignintreg;
  161. begin
  162. if nextintreg<=NR_R3 then
  163. begin
  164. paraloc.loc:=LOC_REGISTER;
  165. paraloc.register:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);;
  166. inc(nextintreg);
  167. end
  168. else
  169. begin
  170. paraloc.loc:=LOC_REFERENCE;
  171. paraloc.reference.index:=NR_STACK_POINTER_REG;
  172. paraloc.reference.offset:=stack_offset;
  173. inc(stack_offset,4);
  174. end;
  175. end;
  176. begin
  177. result:=0;
  178. { zero alignment bytes }
  179. fillchar(nextintreg,sizeof(nextintreg),0);
  180. fillchar(nextfloatreg,sizeof(nextfloatreg),0);
  181. fillchar(nextmmreg,sizeof(nextmmreg),0);
  182. nextintreg:=RS_R0;
  183. nextfloatreg:=RS_F0;
  184. nextmmreg:=RS_D0;
  185. stack_offset:=0;
  186. { frame pointer for nested procedures? }
  187. { inc(nextintreg); }
  188. { constructor? }
  189. { destructor? }
  190. hp:=tparaitem(p.para.first);
  191. while assigned(hp) do
  192. begin
  193. if (hp.paratyp in [vs_var,vs_out]) then
  194. begin
  195. paradef := voidpointertype.def;
  196. loc := LOC_REGISTER;
  197. end
  198. else
  199. begin
  200. paradef := hp.paratype.def;
  201. loc:=getparaloc(paradef);
  202. end;
  203. { make sure all alignment bytes are 0 as well }
  204. fillchar(paraloc,sizeof(paraloc),0);
  205. case loc of
  206. LOC_REGISTER:
  207. begin
  208. paraloc.size := def_cgsize(paradef);
  209. { for things like formaldef }
  210. if paraloc.size = OS_NO then
  211. paraloc.size := OS_ADDR;
  212. is_64bit := paraloc.size in [OS_64,OS_S64];
  213. if nextintreg<=(RS_R3-ord(is_64bit)) then
  214. begin
  215. paraloc.loc:=LOC_REGISTER;
  216. if is_64bit then
  217. begin
  218. paraloc.registerhigh:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);;
  219. inc(nextintreg);
  220. end;
  221. paraloc.registerlow:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);;
  222. inc(nextintreg);
  223. end
  224. else
  225. begin
  226. nextintreg:=RS_R4;
  227. paraloc.loc:=LOC_REFERENCE;
  228. paraloc.reference.index:=NR_STACK_POINTER_REG;
  229. paraloc.reference.offset:=stack_offset;
  230. if not is_64bit then
  231. inc(stack_offset,4)
  232. else
  233. inc(stack_offset,8);
  234. end;
  235. end;
  236. LOC_FPUREGISTER:
  237. begin
  238. paraloc.size:=def_cgsize(paradef);
  239. if nextfloatreg<=RS_F3 then
  240. begin
  241. paraloc.loc:=LOC_FPUREGISTER;
  242. paraloc.register:=newreg(R_FPUREGISTER,nextfloatreg,R_SUBWHOLE);
  243. inc(nextfloatreg);
  244. end
  245. else
  246. begin
  247. {!!!!!!!}
  248. paraloc.size:=def_cgsize(paradef);
  249. internalerror(2002071004);
  250. end;
  251. end;
  252. LOC_REFERENCE:
  253. begin
  254. paraloc.size:=OS_ADDR;
  255. if push_addr_param(hp.paratyp,paradef,p.proccalloption) or
  256. is_open_array(paradef) or
  257. is_array_of_const(paradef) then
  258. assignintreg
  259. else
  260. begin
  261. paraloc.loc:=LOC_REFERENCE;
  262. paraloc.reference.index:=NR_STACK_POINTER_REG;
  263. paraloc.reference.offset:=stack_offset;
  264. inc(stack_offset,hp.paratype.def.size);
  265. end;
  266. end;
  267. else
  268. internalerror(2002071002);
  269. end;
  270. if side=calleeside then
  271. begin
  272. {$warning FIXME Calleeside offset needs to be calculated}
  273. {!!!!!!
  274. if (paraloc.loc = LOC_REFERENCE) then
  275. paraloc.reference.offset := tvarsym(hp.parasym).adjusted_address;
  276. }
  277. end;
  278. hp.paraloc[side]:=paraloc;
  279. hp:=tparaitem(hp.next);
  280. end;
  281. { Function return }
  282. fillchar(paraloc,sizeof(tparalocation),0);
  283. paraloc.size:=def_cgsize(p.rettype.def);
  284. { Return in FPU register? }
  285. if p.rettype.def.deftype=floatdef then
  286. begin
  287. paraloc.loc:=LOC_FPUREGISTER;
  288. paraloc.register:=NR_FPU_RESULT_REG;
  289. end
  290. else
  291. { Return in register? }
  292. if not ret_in_param(p.rettype.def,p.proccalloption) then
  293. begin
  294. paraloc.loc:=LOC_REGISTER;
  295. if paraloc.size in [OS_64,OS_S64] then
  296. begin
  297. paraloc.register64.reglo:=NR_FUNCTION_RETURN64_LOW_REG;
  298. paraloc.register64.reghi:=NR_FUNCTION_RETURN64_HIGH_REG;
  299. end
  300. else
  301. paraloc.register:=NR_FUNCTION_RETURN_REG;
  302. end
  303. else
  304. begin
  305. paraloc.loc:=LOC_REFERENCE;
  306. end;
  307. p.funcret_paraloc[side]:=paraloc;
  308. end;
  309. begin
  310. paramanager:=tarmparamanager.create;
  311. end.
  312. {
  313. $Log$
  314. Revision 1.8 2003-11-02 14:30:03 florian
  315. * fixed ARM for new reg. allocation scheme
  316. Revision 1.7 2003/09/11 11:55:00 florian
  317. * improved arm code generation
  318. * move some protected and private field around
  319. * the temp. register for register parameters/arguments are now released
  320. before the move to the parameter register is done. This improves
  321. the code in a lot of cases.
  322. Revision 1.6 2003/09/09 12:53:40 florian
  323. * some assembling problems fixed
  324. * improved loadaddr_ref_reg
  325. Revision 1.5 2003/09/05 23:57:01 florian
  326. * arm is working again as before the new register naming scheme was implemented
  327. Revision 1.4 2003/09/04 00:15:29 florian
  328. * first bunch of adaptions of arm compiler for new register type
  329. Revision 1.3 2003/08/27 00:27:56 florian
  330. + same procedure as very day: today's work on arm
  331. Revision 1.2 2003/08/16 13:23:01 florian
  332. * several arm related stuff fixed
  333. Revision 1.1 2003/07/21 16:35:30 florian
  334. * very basic stuff for the arm
  335. }