cpupara.pas 13 KB

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