cpupara.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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,parabase,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. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara:TCGPara);override;
  34. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;override;
  35. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;override;
  36. private
  37. procedure init_values(var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword);
  38. function create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist;
  39. var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword):longint;
  40. end;
  41. implementation
  42. uses
  43. verbose,systems,
  44. rgobj,
  45. defutil,symsym,
  46. cgutils;
  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. procedure tarmparamanager.getintparaloc(calloption : tproccalloption; nr : longint;var cgpara:TCGPara);
  56. var
  57. paraloc : pcgparalocation;
  58. begin
  59. if nr<1 then
  60. internalerror(2002070801);
  61. cgpara.reset;
  62. cgpara.size:=OS_INT;
  63. cgpara.intsize:=tcgsize2size[OS_INT];
  64. cgpara.alignment:=std_param_align;
  65. paraloc:=cgpara.add_location;
  66. with paraloc^ do
  67. begin
  68. size:=OS_INT;
  69. { the four first parameters are passed into registers }
  70. if nr<=4 then
  71. begin
  72. loc:=LOC_REGISTER;
  73. register:=newreg(R_INTREGISTER,RS_R0+nr-1,R_SUBWHOLE);
  74. end
  75. else
  76. begin
  77. { the other parameters are passed on the stack }
  78. loc:=LOC_REFERENCE;
  79. reference.index:=NR_STACK_POINTER_REG;
  80. reference.offset:=(nr-5)*4;
  81. end;
  82. end;
  83. end;
  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. result:=false;
  143. if varspez in [vs_var,vs_out] then
  144. begin
  145. result:=true;
  146. exit;
  147. end;
  148. case def.deftype of
  149. variantdef,
  150. formaldef,
  151. recorddef:
  152. result:=true;
  153. arraydef:
  154. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  155. is_open_array(def) or
  156. is_array_of_const(def) or
  157. is_array_constructor(def);
  158. objectdef :
  159. result:=is_object(def);
  160. setdef :
  161. result:=(tsetdef(def).settype<>smallset);
  162. stringdef :
  163. result:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  164. procvardef :
  165. result:=po_methodpointer in tprocvardef(def).procoptions;
  166. end;
  167. end;
  168. procedure tarmparamanager.init_values(var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword);
  169. begin
  170. curintreg:=RS_R0;
  171. curfloatreg:=RS_F0;
  172. curmmreg:=RS_D0;
  173. cur_stack_offset:=0;
  174. end;
  175. function tarmparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist;
  176. var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword):longint;
  177. var
  178. nextintreg,nextfloatreg,nextmmreg : tsuperregister;
  179. paradef : tdef;
  180. paraloc : pcgparalocation;
  181. stack_offset : aword;
  182. hp : tparavarsym;
  183. loc : tcgloc;
  184. paracgsize : tcgsize;
  185. paralen : longint;
  186. i : integer;
  187. procedure assignintreg;
  188. begin
  189. if nextintreg<=RS_R3 then
  190. begin
  191. paraloc^.loc:=LOC_REGISTER;
  192. paraloc^.register:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  193. inc(nextintreg);
  194. end
  195. else
  196. begin
  197. paraloc^.loc:=LOC_REFERENCE;
  198. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  199. paraloc^.reference.offset:=stack_offset;
  200. inc(stack_offset,4);
  201. end;
  202. end;
  203. begin
  204. result:=0;
  205. nextintreg:=curintreg;
  206. nextfloatreg:=curfloatreg;
  207. nextmmreg:=curmmreg;
  208. stack_offset:=cur_stack_offset;
  209. for i:=0 to paras.count-1 do
  210. begin
  211. hp:=tparavarsym(paras[i]);
  212. { currently only support C-style array of const,
  213. there should be no location assigned to the vararg array itself }
  214. if (p.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  215. is_array_of_const(hp.vartype.def) then
  216. begin
  217. paraloc:=hp.paraloc[side].add_location;
  218. { hack: the paraloc must be valid, but is not actually used }
  219. paraloc^.loc:=LOC_REGISTER;
  220. paraloc^.register:=NR_R0;
  221. paraloc^.size:=OS_ADDR;
  222. break;
  223. end;
  224. if push_addr_param(hp.varspez,hp.vartype.def,p.proccalloption) then
  225. paracgsize:=OS_ADDR
  226. else
  227. begin
  228. paracgsize:=def_cgSize(hp.vartype.def);
  229. if paracgsize=OS_NO then
  230. paracgsize:=OS_ADDR;
  231. end;
  232. hp.paraloc[side].reset;
  233. hp.paraloc[side].size:=paracgsize;
  234. hp.paraloc[side].Alignment:=std_param_align;
  235. if (hp.varspez in [vs_var,vs_out]) then
  236. begin
  237. paradef:=voidpointertype.def;
  238. loc:=LOC_REGISTER;
  239. end
  240. else
  241. begin
  242. paradef:=hp.vartype.def;
  243. loc:=getparaloc(p.proccalloption,paradef);
  244. end;
  245. paralen:=tcgsize2size[paracgsize];
  246. hp.paraloc[side].intsize:=paralen;
  247. {$ifdef EXTDEBUG}
  248. if paralen=0 then
  249. internalerror(200410311);
  250. {$endif EXTDEBUG}
  251. while paralen>0 do
  252. begin
  253. paraloc:=hp.paraloc[side].add_location;
  254. { for things like formaldef }
  255. if paracgsize=OS_NO then
  256. paraloc^.size:=OS_ADDR
  257. else if paracgsize in [OS_64,OS_S64] then
  258. paraloc^.size:=OS_32
  259. else
  260. paraloc^.size:=paracgsize;
  261. case loc of
  262. LOC_REGISTER:
  263. begin
  264. { this is not abi compliant }
  265. if nextintreg<=RS_R3 then
  266. begin
  267. paraloc^.loc:=LOC_REGISTER;
  268. paraloc^.register:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  269. inc(nextintreg);
  270. end
  271. else
  272. begin
  273. paraloc^.loc:=LOC_REFERENCE;
  274. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  275. paraloc^.reference.offset:=stack_offset;
  276. inc(stack_offset,4);
  277. end;
  278. end;
  279. LOC_FPUREGISTER:
  280. begin
  281. if nextfloatreg<=RS_F3 then
  282. begin
  283. paraloc^.loc:=LOC_FPUREGISTER;
  284. paraloc^.register:=newreg(R_FPUREGISTER,nextfloatreg,R_SUBWHOLE);
  285. inc(nextfloatreg);
  286. end
  287. else
  288. begin
  289. paraloc^.loc:=LOC_REFERENCE;
  290. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  291. paraloc^.reference.offset:=stack_offset;
  292. case paraloc^.size of
  293. OS_F32:
  294. inc(stack_offset,4);
  295. OS_F64:
  296. inc(stack_offset,8);
  297. OS_F80:
  298. inc(stack_offset,10);
  299. OS_F128:
  300. inc(stack_offset,16);
  301. else
  302. internalerror(200403201);
  303. end;
  304. end;
  305. end;
  306. LOC_REFERENCE:
  307. begin
  308. paraloc^.size:=OS_ADDR;
  309. if push_addr_param(hp.varspez,paradef,p.proccalloption) or
  310. is_open_array(paradef) or
  311. is_array_of_const(paradef) then
  312. assignintreg
  313. else
  314. begin
  315. paraloc^.loc:=LOC_REFERENCE;
  316. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  317. paraloc^.reference.offset:=stack_offset;
  318. inc(stack_offset,hp.vartype.def.size);
  319. end;
  320. end;
  321. else
  322. internalerror(2002071002);
  323. end;
  324. if side=calleeside then
  325. begin
  326. if paraloc^.loc=LOC_REFERENCE then
  327. begin
  328. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  329. inc(paraloc^.reference.offset,4);
  330. end;
  331. end;
  332. dec(paralen,tcgsize2size[paraloc^.size]);
  333. end;
  334. end;
  335. curintreg:=nextintreg;
  336. curfloatreg:=nextfloatreg;
  337. curmmreg:=nextmmreg;
  338. cur_stack_offset:=stack_offset;
  339. result:=cur_stack_offset;
  340. end;
  341. function tarmparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  342. var
  343. cur_stack_offset: aword;
  344. curintreg, curfloatreg, curmmreg: tsuperregister;
  345. retcgsize : tcgsize;
  346. begin
  347. init_values(curintreg,curfloatreg,curmmreg,cur_stack_offset);
  348. result:=create_paraloc_info_intern(p,side,p.paras,curintreg,curfloatreg,curmmreg,cur_stack_offset);
  349. { Constructors return self instead of a boolean }
  350. if (p.proctypeoption=potype_constructor) then
  351. retcgsize:=OS_ADDR
  352. else
  353. retcgsize:=def_cgsize(p.rettype.def);
  354. location_reset(p.funcretloc[side],LOC_INVALID,OS_NO);
  355. p.funcretloc[side].size:=retcgsize;
  356. { void has no location }
  357. if is_void(p.rettype.def) then
  358. begin
  359. location_reset(p.funcretloc[side],LOC_VOID,OS_NO);
  360. exit;
  361. end;
  362. { Return in FPU register? }
  363. if p.rettype.def.deftype=floatdef then
  364. begin
  365. p.funcretloc[side].loc:=LOC_FPUREGISTER;
  366. p.funcretloc[side].register:=NR_FPU_RESULT_REG;
  367. end
  368. { Return in register? }
  369. else if not ret_in_param(p.rettype.def,p.proccalloption) then
  370. begin
  371. if retcgsize in [OS_64,OS_S64] then
  372. begin
  373. { low }
  374. p.funcretloc[side].loc:=LOC_REGISTER;
  375. p.funcretloc[side].register64.reglo:=NR_FUNCTION_RESULT64_LOW_REG;
  376. p.funcretloc[side].register64.reghi:=NR_FUNCTION_RESULT64_HIGH_REG;
  377. end
  378. else
  379. begin
  380. p.funcretloc[side].loc:=LOC_REGISTER;
  381. p.funcretloc[side].register:=NR_FUNCTION_RETURN_REG;
  382. end;
  383. end
  384. else
  385. begin
  386. p.funcretloc[side].loc:=LOC_REFERENCE;
  387. p.funcretloc[side].size:=retcgsize;
  388. end;
  389. end;
  390. function tarmparamanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  391. var
  392. cur_stack_offset: aword;
  393. curintreg, curfloatreg, curmmreg: tsuperregister;
  394. begin
  395. init_values(curintreg,curfloatreg,curmmreg,cur_stack_offset);
  396. result:=create_paraloc_info_intern(p,callerside,p.paras,curintreg,curfloatreg,curmmreg,cur_stack_offset);
  397. if (p.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  398. { just continue loading the parameters in the registers }
  399. result:=create_paraloc_info_intern(p,callerside,varargspara,curintreg,curfloatreg,curmmreg,cur_stack_offset)
  400. else
  401. internalerror(200410231);
  402. end;
  403. begin
  404. paramanager:=tarmparamanager.create;
  405. end.
  406. {
  407. $Log$
  408. Revision 1.30 2005-02-03 20:04:49 peter
  409. * push_addr_param must be defined per target
  410. Revision 1.29 2005/01/15 21:45:35 florian
  411. * arm compiler fixed
  412. Revision 1.28 2005/01/01 19:30:17 florian
  413. * ie with array of const;cdecl; fixed
  414. Revision 1.27 2004/11/24 22:03:26 florian
  415. * fixed arm compilation
  416. Revision 1.26 2004/11/21 17:54:59 peter
  417. * ttempcreatenode.create_reg merged into .create with parameter
  418. whether a register is allowed
  419. * funcret_paraloc renamed to funcretloc
  420. Revision 1.25 2004/11/06 17:44:47 florian
  421. + additional extdebug check for wrong add_reg_instructions added
  422. * too long manglednames are cut off at 200 chars using a crc
  423. Revision 1.24 2004/11/01 09:23:01 florian
  424. * fixed handling of stack parameters on the arm
  425. Revision 1.23 2004/10/31 12:37:11 florian
  426. * another couple of arm fixed
  427. Revision 1.22 2004/10/24 17:32:53 florian
  428. * fixed several arm compiler bugs
  429. Revision 1.21 2004/10/24 07:54:25 florian
  430. * fixed compilation of arm compiler
  431. Revision 1.20 2004/10/22 16:36:57 florian
  432. * first arm fixes for new paraloc handling
  433. Revision 1.19 2004/06/20 08:55:31 florian
  434. * logs truncated
  435. Revision 1.18 2004/06/16 20:07:10 florian
  436. * dwarf branch merged
  437. Revision 1.17.2.1 2004/06/13 20:38:38 florian
  438. * fixed floating point register spilling on sparc
  439. Revision 1.17 2004/03/20 21:11:01 florian
  440. + float parameters can be on the stack now as well
  441. Revision 1.16 2004/03/20 20:55:36 florian
  442. + implemented cdecl'd varargs on arm
  443. + -dCMEM supported by the compiler
  444. * label/goto asmsymbol type with -dextdebug fixed
  445. Revision 1.15 2004/03/07 00:16:59 florian
  446. * compilation of arm rtl fixed
  447. }