cpupara.pas 12 KB

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