cpupara.pas 14 KB

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