cpupara.pas 14 KB

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