cpupara.pas 15 KB

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