cpupara.pas 17 KB

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