paramgr.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. Generic calling convention handling
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# Parameter passing manager. Used to manage how
  18. parameters are passed to routines.
  19. }
  20. unit paramgr;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,globtype,
  25. cpubase,cgbase,
  26. parabase,
  27. aasmtai,aasmdata,
  28. symconst,symtype,symsym,symdef;
  29. type
  30. {# This class defines some methods to take care of routine
  31. parameters. It should be overriden for each new processor
  32. }
  33. tparamanager = class
  34. { true if the location in paraloc can be reused as localloc }
  35. function param_use_paraloc(const cgpara:tcgpara):boolean;virtual;
  36. {# Returns true if the return value is actually a parameter
  37. pointer.
  38. }
  39. function ret_in_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  40. function push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  41. { Returns true if a parameter is too large to copy and only
  42. the address is pushed
  43. }
  44. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;abstract;
  45. { return the size of a push }
  46. function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  47. {# Returns a structure giving the information on
  48. the storage of the parameter (which must be
  49. an integer parameter). This is only used when calling
  50. internal routines directly, where all parameters must
  51. be 4-byte values.
  52. In case the location is a register, this register is allocated.
  53. Call freeintparaloc() after the call to free the locations again.
  54. Default implementation: don't do anything at all (in case you don't
  55. use register parameter passing)
  56. @param(list Current assembler list)
  57. @param(nr Parameter number of routine, starting from 1)
  58. }
  59. function get_para_align(calloption : tproccalloption):byte;virtual;
  60. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;virtual;
  61. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;virtual;
  62. function get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;virtual;
  63. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;virtual;
  64. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara:TCGPara);virtual;abstract;
  65. {# allocate a parameter location created with create_paraloc_info
  66. @param(list Current assembler list)
  67. @param(loc Parameter location)
  68. }
  69. procedure allocparaloc(list: TAsmList; const cgpara: TCGPara); virtual;
  70. {# free a parameter location allocated with alloccgpara
  71. @param(list Current assembler list)
  72. @param(loc Parameter location)
  73. }
  74. procedure freeparaloc(list: TAsmList; const cgpara: TCGPara); virtual;
  75. { This is used to populate the location information on all parameters
  76. for the routine as seen in either the caller or the callee. It returns
  77. the size allocated on the stack
  78. }
  79. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;virtual;abstract;
  80. { This is used to populate the location information on all parameters
  81. for the routine when it is being inlined. It returns
  82. the size allocated on the stack
  83. }
  84. function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual;
  85. { This is used to populate the location information on all parameters
  86. for the routine that are passed as varargs. It returns
  87. the size allocated on the stack (including the normal parameters)
  88. }
  89. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;virtual;abstract;
  90. procedure createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);virtual;
  91. procedure duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  92. function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual;abstract;
  93. function parsefuncretloc(p : tabstractprocdef; const s : string) : boolean;virtual;abstract;
  94. end;
  95. var
  96. paramanager : tparamanager;
  97. implementation
  98. uses
  99. systems,
  100. cgobj,tgobj,cgutils,
  101. defutil,verbose;
  102. { true if the location in paraloc can be reused as localloc }
  103. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  104. begin
  105. result:=false;
  106. end;
  107. { true if uses a parameter as return value }
  108. function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  109. begin
  110. ret_in_param:=((def.typ=arraydef) and not(is_dynamic_array(def))) or
  111. (def.typ=recorddef) or
  112. ((def.typ=stringdef) and (tstringdef(def).stringtype in [st_shortstring,st_longstring])) or
  113. ((def.typ=procvardef) and (po_methodpointer in tprocvardef(def).procoptions)) or
  114. { interfaces are also passed by reference to be compatible with delphi and COM }
  115. ((def.typ=objectdef) and (is_object(def) or is_interface(def))) or
  116. (def.typ=variantdef) or
  117. ((def.typ=setdef) and (tsetdef(def).settype<>smallset));
  118. end;
  119. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  120. begin
  121. push_high_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and
  122. (
  123. is_open_array(def) or
  124. is_open_string(def) or
  125. is_array_of_const(def)
  126. );
  127. end;
  128. { return the size of a push }
  129. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  130. begin
  131. push_size:=-1;
  132. case varspez of
  133. vs_out,
  134. vs_var :
  135. push_size:=sizeof(aint);
  136. vs_value,
  137. vs_const :
  138. begin
  139. if push_addr_param(varspez,def,calloption) then
  140. push_size:=sizeof(aint)
  141. else
  142. begin
  143. { special array are normally pushed by addr, only for
  144. cdecl array of const it comes here and the pushsize
  145. is unknown }
  146. if is_array_of_const(def) then
  147. push_size:=0
  148. else
  149. push_size:=def.size;
  150. end;
  151. end;
  152. end;
  153. end;
  154. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  155. begin
  156. result:=std_param_align;
  157. end;
  158. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  159. begin
  160. result:=[];
  161. end;
  162. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  163. begin
  164. result:=[];
  165. end;
  166. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  167. begin
  168. result:=[];
  169. end;
  170. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  171. begin
  172. result:=[];
  173. end;
  174. procedure tparamanager.allocparaloc(list: TAsmList; const cgpara: TCGPara);
  175. var
  176. paraloc : pcgparalocation;
  177. begin
  178. paraloc:=cgpara.location;
  179. while assigned(paraloc) do
  180. begin
  181. case paraloc^.loc of
  182. LOC_REGISTER,
  183. LOC_CREGISTER:
  184. begin
  185. if getsupreg(paraloc^.register)<first_int_imreg then
  186. cg.getcpuregister(list,paraloc^.register);
  187. end;
  188. LOC_FPUREGISTER,
  189. LOC_CFPUREGISTER:
  190. begin
  191. if getsupreg(paraloc^.register)<first_fpu_imreg then
  192. cg.getcpuregister(list,paraloc^.register);
  193. end;
  194. LOC_MMREGISTER,
  195. LOC_CMMREGISTER :
  196. begin
  197. if getsupreg(paraloc^.register)<first_mm_imreg then
  198. cg.getcpuregister(list,paraloc^.register);
  199. end;
  200. end;
  201. paraloc:=paraloc^.next;
  202. end;
  203. end;
  204. procedure tparamanager.freeparaloc(list: TAsmList; const cgpara: TCGPara);
  205. var
  206. paraloc : Pcgparalocation;
  207. href : treference;
  208. begin
  209. paraloc:=cgpara.location;
  210. while assigned(paraloc) do
  211. begin
  212. case paraloc^.loc of
  213. LOC_VOID:
  214. ;
  215. LOC_REGISTER,
  216. LOC_CREGISTER:
  217. begin
  218. if getsupreg(paraloc^.register)<first_int_imreg then
  219. cg.ungetcpuregister(list,paraloc^.register);
  220. end;
  221. LOC_FPUREGISTER,
  222. LOC_CFPUREGISTER:
  223. begin
  224. if getsupreg(paraloc^.register)<first_fpu_imreg then
  225. cg.ungetcpuregister(list,paraloc^.register);
  226. end;
  227. LOC_MMREGISTER,
  228. LOC_CMMREGISTER :
  229. begin
  230. if getsupreg(paraloc^.register)<first_mm_imreg then
  231. cg.ungetcpuregister(list,paraloc^.register);
  232. end;
  233. LOC_REFERENCE,
  234. LOC_CREFERENCE :
  235. begin
  236. if use_fixed_stack then
  237. begin
  238. { don't use reference_reset_base, because that will depend on cgobj }
  239. fillchar(href,sizeof(href),0);
  240. href.base:=paraloc^.reference.index;
  241. href.offset:=paraloc^.reference.offset;
  242. tg.ungettemp(list,href);
  243. end;
  244. end;
  245. else
  246. internalerror(2004110212);
  247. end;
  248. paraloc:=paraloc^.next;
  249. end;
  250. end;
  251. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  252. var
  253. href : treference;
  254. len : aint;
  255. paraloc,
  256. newparaloc : pcgparalocation;
  257. begin
  258. cgpara.reset;
  259. cgpara.size:=parasym.paraloc[callerside].size;
  260. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  261. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  262. {$ifdef powerpc}
  263. cgpara.composite:=parasym.paraloc[callerside].composite;
  264. {$endif powerpc}
  265. paraloc:=parasym.paraloc[callerside].location;
  266. while assigned(paraloc) do
  267. begin
  268. if paraloc^.size=OS_NO then
  269. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  270. else
  271. len:=tcgsize2size[paraloc^.size];
  272. newparaloc:=cgpara.add_location;
  273. newparaloc^.size:=paraloc^.size;
  274. { $warning maybe release this optimization for all targets? }
  275. { released for all CPUs:
  276. i386 isn't affected anyways because it uses the stack to push parameters
  277. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  278. { Does it fit a register? }
  279. if (len<=sizeof(aint)) and
  280. (cgpara.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  281. newparaloc^.loc:=LOC_REGISTER
  282. else
  283. newparaloc^.loc:=paraloc^.loc;
  284. case newparaloc^.loc of
  285. LOC_REGISTER :
  286. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  287. LOC_FPUREGISTER :
  288. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  289. LOC_MMREGISTER :
  290. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  291. LOC_REFERENCE :
  292. begin
  293. tg.gettemp(list,len,tt_persistent,href);
  294. newparaloc^.reference.index:=href.base;
  295. newparaloc^.reference.offset:=href.offset;
  296. end;
  297. end;
  298. paraloc:=paraloc^.next;
  299. end;
  300. end;
  301. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  302. var
  303. paraloc,
  304. newparaloc : pcgparalocation;
  305. begin
  306. cgpara.reset;
  307. cgpara.size:=parasym.paraloc[callerside].size;
  308. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  309. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  310. {$ifdef powerpc}
  311. cgpara.composite:=parasym.paraloc[callerside].composite;
  312. {$endif powerpc}
  313. paraloc:=parasym.paraloc[callerside].location;
  314. while assigned(paraloc) do
  315. begin
  316. newparaloc:=cgpara.add_location;
  317. move(paraloc^,newparaloc^,sizeof(newparaloc^));
  318. newparaloc^.next:=nil;
  319. paraloc:=paraloc^.next;
  320. end;
  321. end;
  322. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  323. begin
  324. { We need to return the size allocated }
  325. create_paraloc_info(p,callerside);
  326. result:=create_paraloc_info(p,calleeside);
  327. end;
  328. initialization
  329. ;
  330. finalization
  331. paramanager.free;
  332. end.