cpupara.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_R10 then
  160. begin
  161. paraloc.loc:=LOC_REGISTER;
  162. paraloc.register:=nextintreg;
  163. inc(nextintreg.number,NR_R1-NR_R0);
  164. if target_info.abi=abi_powerpc_aix then
  165. inc(stack_offset,4);
  166. end
  167. else
  168. begin
  169. paraloc.loc:=LOC_REFERENCE;
  170. paraloc.reference.index.enum:=R_INTREGISTER;
  171. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  172. paraloc.reference.offset:=stack_offset;
  173. inc(stack_offset,4);
  174. end;
  175. end;
  176. begin
  177. { zero alignment bytes }
  178. fillchar(nextintreg,sizeof(nextintreg),0);
  179. fillchar(nextfloatreg,sizeof(nextfloatreg),0);
  180. fillchar(nextmmreg,sizeof(nextmmreg),0);
  181. nextintreg.enum:=R_INTREGISTER;
  182. nextintreg.number:=NR_R0;
  183. nextfloatreg.enum:=R_F0;
  184. // nextmmreg:=0;
  185. stack_offset:=0;
  186. { frame pointer for nested procedures? }
  187. { inc(nextintreg); }
  188. { constructor? }
  189. { destructor? }
  190. hp:=tparaitem(p.para.first);
  191. while assigned(hp) do
  192. begin
  193. if (hp.paratyp in [vs_var,vs_out]) then
  194. begin
  195. paradef := voidpointertype.def;
  196. loc := LOC_REGISTER;
  197. end
  198. else
  199. begin
  200. paradef := hp.paratype.def;
  201. loc:=getparaloc(paradef);
  202. end;
  203. { make sure all alignment bytes are 0 as well }
  204. fillchar(paraloc,sizeof(paraloc),0);
  205. case loc of
  206. LOC_REGISTER:
  207. begin
  208. paraloc.size := def_cgsize(paradef);
  209. { for things like formaldef }
  210. if paraloc.size = OS_NO then
  211. paraloc.size := OS_ADDR;
  212. is_64bit := paraloc.size in [OS_64,OS_S64];
  213. if nextintreg.number<=(NR_R10-ord(is_64bit)*(NR_R1-NR_R0)) then
  214. begin
  215. paraloc.loc:=LOC_REGISTER;
  216. if is_64bit then
  217. begin
  218. if odd((nextintreg.number-NR_R3) shr 8) and (target_info.abi=abi_powerpc_sysv) Then
  219. inc(nextintreg.number,NR_R1-NR_R0);
  220. paraloc.registerhigh:=nextintreg;
  221. inc(nextintreg.number,NR_R1-NR_R0);
  222. if target_info.abi=abi_powerpc_aix then
  223. inc(stack_offset,4);
  224. end;
  225. paraloc.registerlow:=nextintreg;
  226. inc(nextintreg.number,NR_R1-NR_R0);
  227. if target_info.abi=abi_powerpc_aix then
  228. inc(stack_offset,4);
  229. end
  230. else
  231. begin
  232. nextintreg.number := NR_R11;
  233. paraloc.loc:=LOC_REFERENCE;
  234. paraloc.reference.index.enum:=R_INTREGISTER;
  235. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  236. paraloc.reference.offset:=stack_offset;
  237. if not is_64bit then
  238. inc(stack_offset,4)
  239. else
  240. inc(stack_offset,8);
  241. end;
  242. end;
  243. LOC_FPUREGISTER:
  244. begin
  245. paraloc.size:=def_cgsize(paradef);
  246. if nextfloatreg.enum<=R_F10 then
  247. begin
  248. paraloc.loc:=LOC_FPUREGISTER;
  249. paraloc.register:=nextfloatreg;
  250. inc(nextfloatreg.enum);
  251. end
  252. else
  253. begin
  254. {!!!!!!!}
  255. paraloc.size:=def_cgsize(paradef);
  256. internalerror(2002071004);
  257. end;
  258. end;
  259. LOC_REFERENCE:
  260. begin
  261. paraloc.size:=OS_ADDR;
  262. if push_addr_param(paradef,p.proccalloption) or
  263. is_open_array(paradef) or
  264. is_array_of_const(paradef) then
  265. assignintreg
  266. else
  267. begin
  268. paraloc.loc:=LOC_REFERENCE;
  269. paraloc.reference.index.enum:=R_INTREGISTER;
  270. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  271. paraloc.reference.offset:=stack_offset;
  272. inc(stack_offset,hp.paratype.def.size);
  273. end;
  274. end;
  275. else
  276. internalerror(2002071002);
  277. end;
  278. if side = calleeside then
  279. begin
  280. if (paraloc.loc = LOC_REFERENCE) then
  281. paraloc.reference.offset := tvarsym(hp.parasym).adjusted_address;
  282. end;
  283. hp.paraloc[side]:=paraloc;
  284. hp:=tparaitem(hp.next);
  285. end;
  286. { Function return }
  287. fillchar(paraloc,sizeof(tparalocation),0);
  288. paraloc.size:=def_cgsize(p.rettype.def);
  289. { Return in FPU register? }
  290. if p.rettype.def.deftype=floatdef then
  291. begin
  292. paraloc.loc:=LOC_FPUREGISTER;
  293. paraloc.register.enum:=FPU_RESULT_REG;
  294. end
  295. else
  296. { Return in register? }
  297. if not ret_in_param(p.rettype.def,p.proccalloption) then
  298. begin
  299. paraloc.loc:=LOC_REGISTER;
  300. if paraloc.size in [OS_64,OS_S64] then
  301. begin
  302. paraloc.register64.reglo.enum:=R_INTREGISTER;
  303. paraloc.register64.reglo.number:=NR_FUNCTION_RETURN64_LOW_REG;
  304. paraloc.register64.reghi.enum:=R_INTREGISTER;
  305. paraloc.register64.reghi.number:=NR_FUNCTION_RETURN64_HIGH_REG;
  306. end
  307. else
  308. begin
  309. paraloc.register.enum:=R_INTREGISTER;
  310. paraloc.register.number:=NR_FUNCTION_RETURN_REG;
  311. end;
  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.2 2003-08-16 13:23:01 florian
  325. * several arm related stuff fixed
  326. Revision 1.1 2003/07/21 16:35:30 florian
  327. * very basic stuff for the arm
  328. }