cpupara.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. function getfuncretparaloc(p : tabstractprocdef) : tparalocation;override;
  35. end;
  36. implementation
  37. uses
  38. verbose,systems,
  39. cpuinfo,cginfo,cgbase,
  40. rgobj,
  41. defutil,symsym;
  42. function tarmparamanager.getintparaloc(list: taasmoutput; nr : longint) : tparalocation;
  43. begin
  44. fillchar(result,sizeof(tparalocation),0);
  45. if nr<1 then
  46. internalerror(2002070801)
  47. else if nr<=4 then
  48. begin
  49. result.loc:=LOC_REGISTER;
  50. result.register.enum:=R_INTREGISTER;
  51. result.register.number:=NR_R0+(nr-1)*(NR_R1-NR_R0);
  52. rg.getexplicitregisterint(list,result.register.number);
  53. end
  54. else
  55. begin
  56. result.loc:=LOC_REFERENCE;
  57. result.reference.index.enum:=R_INTREGISTER;
  58. result.reference.index.number:=NR_STACK_POINTER_REG;
  59. result.reference.offset:=(nr-4)*4;
  60. end;
  61. result.size := OS_INT;
  62. end;
  63. procedure tarmparamanager.freeintparaloc(list: taasmoutput; nr : longint);
  64. var
  65. r: tregister;
  66. begin
  67. if nr<1 then
  68. internalerror(2003060401)
  69. else if nr<=4 then
  70. begin
  71. r.enum := R_INTREGISTER;
  72. r.number := NR_R0+(nr-1)*(NR_R1-NR_R0);
  73. rg.ungetregisterint(list,r);
  74. end;
  75. end;
  76. function getparaloc(p : tdef) : tcgloc;
  77. begin
  78. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  79. if push_addr_param for the def is true
  80. }
  81. case p.deftype of
  82. orddef:
  83. getparaloc:=LOC_REGISTER;
  84. floatdef:
  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(def : tdef;calloption : tproccalloption) : boolean;
  130. begin
  131. case def.deftype of
  132. recorddef:
  133. push_addr_param:=true;
  134. arraydef:
  135. push_addr_param:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  136. is_open_array(def) or
  137. is_array_of_const(def) or
  138. is_array_constructor(def);
  139. setdef :
  140. push_addr_param:=(tsetdef(def).settype<>smallset);
  141. stringdef :
  142. push_addr_param:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  143. procvardef :
  144. push_addr_param:=po_methodpointer in tprocvardef(def).procoptions;
  145. else
  146. push_addr_param:=inherited push_addr_param(def,calloption);
  147. end;
  148. end;
  149. procedure tarmparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  150. var
  151. nextintreg,nextfloatreg,nextmmreg : tregister;
  152. paradef : tdef;
  153. paraloc : tparalocation;
  154. stack_offset : aword;
  155. hp : tparaitem;
  156. loc : tcgloc;
  157. is_64bit: boolean;
  158. procedure assignintreg;
  159. begin
  160. if nextintreg.number<=NR_R10 then
  161. begin
  162. paraloc.loc:=LOC_REGISTER;
  163. paraloc.register:=nextintreg;
  164. inc(nextintreg.number,NR_R1-NR_R0);
  165. if target_info.abi=abi_powerpc_aix then
  166. inc(stack_offset,4);
  167. end
  168. else
  169. begin
  170. paraloc.loc:=LOC_REFERENCE;
  171. paraloc.reference.index.enum:=R_INTREGISTER;
  172. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  173. paraloc.reference.offset:=stack_offset;
  174. inc(stack_offset,4);
  175. end;
  176. end;
  177. begin
  178. { zero alignment bytes }
  179. fillchar(nextintreg,sizeof(nextintreg),0);
  180. fillchar(nextfloatreg,sizeof(nextfloatreg),0);
  181. fillchar(nextmmreg,sizeof(nextmmreg),0);
  182. nextintreg.enum:=R_INTREGISTER;
  183. nextintreg.number:=NR_R0;
  184. nextfloatreg.enum:=R_F0;
  185. // nextmmreg:=0;
  186. stack_offset:=0;
  187. { frame pointer for nested procedures? }
  188. { inc(nextintreg); }
  189. { constructor? }
  190. { destructor? }
  191. hp:=tparaitem(p.para.first);
  192. while assigned(hp) do
  193. begin
  194. if (hp.paratyp in [vs_var,vs_out]) then
  195. begin
  196. paradef := voidpointertype.def;
  197. loc := LOC_REGISTER;
  198. end
  199. else
  200. begin
  201. paradef := hp.paratype.def;
  202. loc:=getparaloc(paradef);
  203. end;
  204. { make sure all alignment bytes are 0 as well }
  205. fillchar(paraloc,sizeof(paraloc),0);
  206. case loc of
  207. LOC_REGISTER:
  208. begin
  209. paraloc.size := def_cgsize(paradef);
  210. { for things like formaldef }
  211. if paraloc.size = OS_NO then
  212. paraloc.size := OS_ADDR;
  213. is_64bit := paraloc.size in [OS_64,OS_S64];
  214. if nextintreg.number<=(NR_R10-ord(is_64bit)*(NR_R1-NR_R0)) then
  215. begin
  216. paraloc.loc:=LOC_REGISTER;
  217. if is_64bit then
  218. begin
  219. if odd((nextintreg.number-NR_R3) shr 8) and (target_info.abi=abi_powerpc_sysv) Then
  220. inc(nextintreg.number,NR_R1-NR_R0);
  221. paraloc.registerhigh:=nextintreg;
  222. inc(nextintreg.number,NR_R1-NR_R0);
  223. if target_info.abi=abi_powerpc_aix then
  224. inc(stack_offset,4);
  225. end;
  226. paraloc.registerlow:=nextintreg;
  227. inc(nextintreg.number,NR_R1-NR_R0);
  228. if target_info.abi=abi_powerpc_aix then
  229. inc(stack_offset,4);
  230. end
  231. else
  232. begin
  233. nextintreg.number := NR_R11;
  234. paraloc.loc:=LOC_REFERENCE;
  235. paraloc.reference.index.enum:=R_INTREGISTER;
  236. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  237. paraloc.reference.offset:=stack_offset;
  238. if not is_64bit then
  239. inc(stack_offset,4)
  240. else
  241. inc(stack_offset,8);
  242. end;
  243. end;
  244. LOC_FPUREGISTER:
  245. begin
  246. paraloc.size:=def_cgsize(paradef);
  247. if nextfloatreg.enum<=R_F10 then
  248. begin
  249. paraloc.loc:=LOC_FPUREGISTER;
  250. paraloc.register:=nextfloatreg;
  251. inc(nextfloatreg.enum);
  252. end
  253. else
  254. begin
  255. {!!!!!!!}
  256. paraloc.size:=def_cgsize(paradef);
  257. internalerror(2002071004);
  258. end;
  259. end;
  260. LOC_REFERENCE:
  261. begin
  262. paraloc.size:=OS_ADDR;
  263. if push_addr_param(paradef,p.proccalloption) or
  264. is_open_array(paradef) or
  265. is_array_of_const(paradef) then
  266. assignintreg
  267. else
  268. begin
  269. paraloc.loc:=LOC_REFERENCE;
  270. paraloc.reference.index.enum:=R_INTREGISTER;
  271. paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  272. paraloc.reference.offset:=stack_offset;
  273. inc(stack_offset,hp.paratype.def.size);
  274. end;
  275. end;
  276. else
  277. internalerror(2002071002);
  278. end;
  279. if side = callerside then
  280. hp.callerparaloc:=paraloc
  281. else
  282. begin
  283. if (paraloc.loc = LOC_REFERENCE) then
  284. paraloc.reference.offset := tvarsym(hp.parasym).adjusted_address;
  285. hp.calleeparaloc:=paraloc;
  286. end;
  287. hp:=tparaitem(hp.next);
  288. end;
  289. end;
  290. function tarmparamanager.getfuncretparaloc(p : tabstractprocdef) : tparalocation;
  291. begin
  292. fillchar(result,sizeof(result),0);
  293. case p.rettype.def.deftype of
  294. orddef,
  295. enumdef:
  296. begin
  297. getfuncretparaloc.loc:=LOC_REGISTER;
  298. getfuncretparaloc.register.enum:=R_INTREGISTER;
  299. getfuncretparaloc.register.number:=NR_R0;
  300. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  301. if getfuncretparaloc.size in [OS_S64,OS_64] then
  302. begin
  303. getfuncretparaloc.registerhigh.enum:=R_INTREGISTER;
  304. getfuncretparaloc.registerhigh.number:=NR_R0;
  305. getfuncretparaloc.register.number:=NR_R1;
  306. end;
  307. end;
  308. floatdef:
  309. begin
  310. getfuncretparaloc.loc:=LOC_FPUREGISTER;
  311. getfuncretparaloc.register.enum:=R_F0;
  312. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  313. end;
  314. { smallsets are OS_INT in R0, others are OS_ADDR in R0 -> the same }
  315. { ugly, I know :) (JM) }
  316. setdef,
  317. variantdef,
  318. pointerdef,
  319. formaldef,
  320. classrefdef,
  321. recorddef,
  322. objectdef,
  323. procvardef,
  324. filedef,
  325. arraydef,
  326. stringdef:
  327. begin
  328. if (p.rettype.def.deftype <> stringdef) or
  329. (is_ansistring(p.rettype.def) or
  330. is_widestring(p.rettype.def)) then
  331. begin
  332. getfuncretparaloc.loc:=LOC_REGISTER;
  333. getfuncretparaloc.register.enum:=R_INTREGISTER;
  334. getfuncretparaloc.register.number:=NR_R0;
  335. getfuncretparaloc.size:=OS_ADDR;
  336. end
  337. else
  338. internalerror(2003061601);
  339. end;
  340. else
  341. internalerror(2002090903);
  342. end;
  343. end;
  344. begin
  345. paramanager:=tarmparamanager.create;
  346. end.
  347. {
  348. $Log$
  349. Revision 1.1 2003-07-21 16:35:30 florian
  350. * very basic stuff for the arm
  351. }