cpupara.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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(list: taasmoutput; 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(list: taasmoutput; 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.enum:=R_INTREGISTER;
  50. result.register.number:=NR_R0+(nr-1)*(NR_R1-NR_R0);
  51. rg.getexplicitregisterint(list,result.register.number);
  52. end
  53. else
  54. begin
  55. result.loc:=LOC_REFERENCE;
  56. result.reference.index.enum:=R_INTREGISTER;
  57. result.reference.index.number:=NR_STACK_POINTER_REG;
  58. result.reference.offset:=(nr-4)*4;
  59. end;
  60. result.size := OS_INT;
  61. end;
  62. procedure tarmparamanager.freeintparaloc(list: taasmoutput; nr : longint);
  63. var
  64. r: tregister;
  65. begin
  66. if nr<1 then
  67. internalerror(2003060401)
  68. else if nr<=4 then
  69. begin
  70. r.enum := R_INTREGISTER;
  71. r.number := NR_R0+(nr-1)*(NR_R1-NR_R0);
  72. rg.ungetregisterint(list,r);
  73. end;
  74. end;
  75. function getparaloc(p : tdef) : tcgloc;
  76. begin
  77. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  78. if push_addr_param for the def is true
  79. }
  80. case p.deftype of
  81. orddef:
  82. getparaloc:=LOC_REGISTER;
  83. floatdef:
  84. getparaloc:=LOC_FPUREGISTER;
  85. enumdef:
  86. getparaloc:=LOC_REGISTER;
  87. pointerdef:
  88. getparaloc:=LOC_REGISTER;
  89. formaldef:
  90. getparaloc:=LOC_REGISTER;
  91. classrefdef:
  92. getparaloc:=LOC_REGISTER;
  93. recorddef:
  94. getparaloc:=LOC_REFERENCE;
  95. objectdef:
  96. if is_object(p) then
  97. getparaloc:=LOC_REFERENCE
  98. else
  99. getparaloc:=LOC_REGISTER;
  100. stringdef:
  101. if is_shortstring(p) or is_longstring(p) then
  102. getparaloc:=LOC_REFERENCE
  103. else
  104. getparaloc:=LOC_REGISTER;
  105. procvardef:
  106. if (po_methodpointer in tprocvardef(p).procoptions) then
  107. getparaloc:=LOC_REFERENCE
  108. else
  109. getparaloc:=LOC_REGISTER;
  110. filedef:
  111. getparaloc:=LOC_REGISTER;
  112. arraydef:
  113. getparaloc:=LOC_REFERENCE;
  114. setdef:
  115. if is_smallset(p) then
  116. getparaloc:=LOC_REGISTER
  117. else
  118. getparaloc:=LOC_REFERENCE;
  119. variantdef:
  120. getparaloc:=LOC_REFERENCE;
  121. { avoid problems with errornous definitions }
  122. errordef:
  123. getparaloc:=LOC_REGISTER;
  124. else
  125. internalerror(2002071001);
  126. end;
  127. end;
  128. function tarmparamanager.push_addr_param(def : tdef;calloption : tproccalloption) : boolean;
  129. begin
  130. case def.deftype of
  131. recorddef:
  132. push_addr_param:=true;
  133. arraydef:
  134. push_addr_param:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  135. is_open_array(def) or
  136. is_array_of_const(def) or
  137. is_array_constructor(def);
  138. setdef :
  139. push_addr_param:=(tsetdef(def).settype<>smallset);
  140. stringdef :
  141. push_addr_param:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  142. procvardef :
  143. push_addr_param:=po_methodpointer in tprocvardef(def).procoptions;
  144. else
  145. push_addr_param:=inherited push_addr_param(def,calloption);
  146. end;
  147. end;
  148. procedure tarmparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  149. var
  150. nextintreg,nextfloatreg,nextmmreg : tregister;
  151. paradef : tdef;
  152. paraloc : tparalocation;
  153. stack_offset : aword;
  154. hp : tparaitem;
  155. loc : tcgloc;
  156. is_64bit: boolean;
  157. procedure assignintreg;
  158. begin
  159. if nextintreg.number<=NR_R3 then
  160. begin
  161. paraloc.loc:=LOC_REGISTER;
  162. paraloc.register:=nextintreg;
  163. inc(nextintreg.number,NR_R1-NR_R0);
  164. end
  165. else
  166. begin
  167. paraloc.loc:=LOC_REFERENCE;
  168. paraloc.reference.index.enum:=R_INTREGISTER;
  169. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  170. paraloc.reference.offset:=stack_offset;
  171. inc(stack_offset,4);
  172. end;
  173. end;
  174. begin
  175. { zero alignment bytes }
  176. fillchar(nextintreg,sizeof(nextintreg),0);
  177. fillchar(nextfloatreg,sizeof(nextfloatreg),0);
  178. fillchar(nextmmreg,sizeof(nextmmreg),0);
  179. nextintreg.enum:=R_INTREGISTER;
  180. nextintreg.number:=NR_R0;
  181. nextfloatreg.enum:=R_F0;
  182. // nextmmreg:=0;
  183. stack_offset:=0;
  184. { frame pointer for nested procedures? }
  185. { inc(nextintreg); }
  186. { constructor? }
  187. { destructor? }
  188. hp:=tparaitem(p.para.first);
  189. while assigned(hp) do
  190. begin
  191. if (hp.paratyp in [vs_var,vs_out]) then
  192. begin
  193. paradef := voidpointertype.def;
  194. loc := LOC_REGISTER;
  195. end
  196. else
  197. begin
  198. paradef := hp.paratype.def;
  199. loc:=getparaloc(paradef);
  200. end;
  201. { make sure all alignment bytes are 0 as well }
  202. fillchar(paraloc,sizeof(paraloc),0);
  203. case loc of
  204. LOC_REGISTER:
  205. begin
  206. paraloc.size := def_cgsize(paradef);
  207. { for things like formaldef }
  208. if paraloc.size = OS_NO then
  209. paraloc.size := OS_ADDR;
  210. is_64bit := paraloc.size in [OS_64,OS_S64];
  211. if nextintreg.number<=(NR_R3-ord(is_64bit)*(NR_R1-NR_R0)) then
  212. begin
  213. paraloc.loc:=LOC_REGISTER;
  214. if is_64bit then
  215. begin
  216. paraloc.registerhigh:=nextintreg;
  217. inc(nextintreg.number,NR_R1-NR_R0);
  218. end;
  219. paraloc.registerlow:=nextintreg;
  220. inc(nextintreg.number,NR_R1-NR_R0);
  221. end
  222. else
  223. begin
  224. nextintreg.number := NR_R4;
  225. paraloc.loc:=LOC_REFERENCE;
  226. paraloc.reference.index.enum:=R_INTREGISTER;
  227. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  228. paraloc.reference.offset:=stack_offset;
  229. if not is_64bit then
  230. inc(stack_offset,4)
  231. else
  232. inc(stack_offset,8);
  233. end;
  234. end;
  235. LOC_FPUREGISTER:
  236. begin
  237. paraloc.size:=def_cgsize(paradef);
  238. if nextfloatreg.enum<=R_F3 then
  239. begin
  240. paraloc.loc:=LOC_FPUREGISTER;
  241. paraloc.register:=nextfloatreg;
  242. inc(nextfloatreg.enum);
  243. end
  244. else
  245. begin
  246. {!!!!!!!}
  247. paraloc.size:=def_cgsize(paradef);
  248. internalerror(2002071004);
  249. end;
  250. end;
  251. LOC_REFERENCE:
  252. begin
  253. paraloc.size:=OS_ADDR;
  254. if push_addr_param(paradef,p.proccalloption) or
  255. is_open_array(paradef) or
  256. is_array_of_const(paradef) then
  257. assignintreg
  258. else
  259. begin
  260. paraloc.loc:=LOC_REFERENCE;
  261. paraloc.reference.index.enum:=R_INTREGISTER;
  262. paraloc.reference.index.number:=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. if (paraloc.loc = LOC_REFERENCE) then
  273. paraloc.reference.offset := tvarsym(hp.parasym).adjusted_address;
  274. end;
  275. hp.paraloc[side]:=paraloc;
  276. hp:=tparaitem(hp.next);
  277. end;
  278. { Function return }
  279. fillchar(paraloc,sizeof(tparalocation),0);
  280. paraloc.size:=def_cgsize(p.rettype.def);
  281. { Return in FPU register? }
  282. if p.rettype.def.deftype=floatdef then
  283. begin
  284. paraloc.loc:=LOC_FPUREGISTER;
  285. paraloc.register.enum:=FPU_RESULT_REG;
  286. end
  287. else
  288. { Return in register? }
  289. if not ret_in_param(p.rettype.def,p.proccalloption) then
  290. begin
  291. paraloc.loc:=LOC_REGISTER;
  292. if paraloc.size in [OS_64,OS_S64] then
  293. begin
  294. paraloc.register64.reglo.enum:=R_INTREGISTER;
  295. paraloc.register64.reglo.number:=NR_FUNCTION_RETURN64_LOW_REG;
  296. paraloc.register64.reghi.enum:=R_INTREGISTER;
  297. paraloc.register64.reghi.number:=NR_FUNCTION_RETURN64_HIGH_REG;
  298. end
  299. else
  300. begin
  301. paraloc.register.enum:=R_INTREGISTER;
  302. paraloc.register.number:=NR_FUNCTION_RETURN_REG;
  303. end;
  304. end
  305. else
  306. begin
  307. paraloc.loc:=LOC_REFERENCE;
  308. end;
  309. p.funcret_paraloc[side]:=paraloc;
  310. end;
  311. begin
  312. paramanager:=tarmparamanager.create;
  313. end.
  314. {
  315. $Log$
  316. Revision 1.3 2003-08-27 00:27:56 florian
  317. + same procedure as very day: today's work on arm
  318. Revision 1.2 2003/08/16 13:23:01 florian
  319. * several arm related stuff fixed
  320. Revision 1.1 2003/07/21 16:35:30 florian
  321. * very basic stuff for the arm
  322. }