paramgr.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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,cgutils,
  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. { Returns the location of the function result if p had def as
  81. function result instead of its actual result. Used if the compiler
  82. forces the function result to something different than the real
  83. result. }
  84. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tlocation;virtual;abstract;
  85. { This is used to populate the location information on all parameters
  86. for the routine when it is being inlined. It returns
  87. the size allocated on the stack
  88. }
  89. function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual;
  90. { This is used to populate the location information on all parameters
  91. for the routine that are passed as varargs. It returns
  92. the size allocated on the stack (including the normal parameters)
  93. }
  94. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;virtual;abstract;
  95. procedure createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);virtual;
  96. procedure duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  97. function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual;
  98. function parsefuncretloc(p : tabstractprocdef; const s : string) : boolean;virtual;
  99. end;
  100. var
  101. paramanager : tparamanager;
  102. implementation
  103. uses
  104. systems,
  105. cgobj,tgobj,
  106. defutil,verbose;
  107. { true if the location in paraloc can be reused as localloc }
  108. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  109. begin
  110. result:=false;
  111. end;
  112. { true if uses a parameter as return value }
  113. function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  114. begin
  115. ret_in_param:=((def.typ=arraydef) and not(is_dynamic_array(def))) or
  116. (def.typ=recorddef) or
  117. (def.typ=stringdef) or
  118. ((def.typ=procvardef) and (po_methodpointer in tprocvardef(def).procoptions)) or
  119. { interfaces are also passed by reference to be compatible with delphi and COM }
  120. ((def.typ=objectdef) and (is_object(def) or is_interface(def))) or
  121. (def.typ=variantdef) or
  122. ((def.typ=setdef) and not is_smallset(def));
  123. end;
  124. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  125. begin
  126. push_high_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and
  127. (
  128. is_open_array(def) or
  129. is_open_string(def) or
  130. is_array_of_const(def)
  131. );
  132. end;
  133. { return the size of a push }
  134. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  135. begin
  136. push_size:=-1;
  137. case varspez of
  138. vs_out,
  139. vs_var :
  140. push_size:=sizeof(pint);
  141. vs_value,
  142. vs_const :
  143. begin
  144. if push_addr_param(varspez,def,calloption) then
  145. push_size:=sizeof(pint)
  146. else
  147. begin
  148. { special array are normally pushed by addr, only for
  149. cdecl array of const it comes here and the pushsize
  150. is unknown }
  151. if is_array_of_const(def) then
  152. push_size:=0
  153. else
  154. push_size:=def.size;
  155. end;
  156. end;
  157. end;
  158. end;
  159. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  160. begin
  161. result:=std_param_align;
  162. end;
  163. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  164. begin
  165. result:=[];
  166. end;
  167. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  168. begin
  169. result:=[];
  170. end;
  171. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  172. begin
  173. result:=[];
  174. end;
  175. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  176. begin
  177. result:=[];
  178. end;
  179. procedure tparamanager.allocparaloc(list: TAsmList; const cgpara: TCGPara);
  180. var
  181. paraloc : pcgparalocation;
  182. begin
  183. paraloc:=cgpara.location;
  184. while assigned(paraloc) do
  185. begin
  186. case paraloc^.loc of
  187. LOC_REGISTER,
  188. LOC_CREGISTER:
  189. begin
  190. if getsupreg(paraloc^.register)<first_int_imreg then
  191. cg.getcpuregister(list,paraloc^.register);
  192. end;
  193. LOC_FPUREGISTER,
  194. LOC_CFPUREGISTER:
  195. begin
  196. if getsupreg(paraloc^.register)<first_fpu_imreg then
  197. cg.getcpuregister(list,paraloc^.register);
  198. end;
  199. LOC_MMREGISTER,
  200. LOC_CMMREGISTER :
  201. begin
  202. if getsupreg(paraloc^.register)<first_mm_imreg then
  203. cg.getcpuregister(list,paraloc^.register);
  204. end;
  205. end;
  206. paraloc:=paraloc^.next;
  207. end;
  208. end;
  209. procedure tparamanager.freeparaloc(list: TAsmList; const cgpara: TCGPara);
  210. var
  211. paraloc : Pcgparalocation;
  212. href : treference;
  213. begin
  214. paraloc:=cgpara.location;
  215. while assigned(paraloc) do
  216. begin
  217. case paraloc^.loc of
  218. LOC_VOID:
  219. ;
  220. LOC_REGISTER,
  221. LOC_CREGISTER:
  222. begin
  223. if getsupreg(paraloc^.register)<first_int_imreg then
  224. cg.ungetcpuregister(list,paraloc^.register);
  225. end;
  226. LOC_FPUREGISTER,
  227. LOC_CFPUREGISTER:
  228. begin
  229. if getsupreg(paraloc^.register)<first_fpu_imreg then
  230. cg.ungetcpuregister(list,paraloc^.register);
  231. end;
  232. LOC_MMREGISTER,
  233. LOC_CMMREGISTER :
  234. begin
  235. if getsupreg(paraloc^.register)<first_mm_imreg then
  236. cg.ungetcpuregister(list,paraloc^.register);
  237. end;
  238. LOC_REFERENCE,
  239. LOC_CREFERENCE :
  240. begin
  241. if use_fixed_stack then
  242. begin
  243. { don't use reference_reset_base, because that will depend on cgobj }
  244. fillchar(href,sizeof(href),0);
  245. href.base:=paraloc^.reference.index;
  246. href.offset:=paraloc^.reference.offset;
  247. tg.ungettemp(list,href);
  248. end;
  249. end;
  250. else
  251. internalerror(2004110212);
  252. end;
  253. paraloc:=paraloc^.next;
  254. end;
  255. end;
  256. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  257. var
  258. href : treference;
  259. len : aint;
  260. paraloc,
  261. newparaloc : pcgparalocation;
  262. begin
  263. cgpara.reset;
  264. cgpara.size:=parasym.paraloc[callerside].size;
  265. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  266. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  267. {$ifdef powerpc}
  268. cgpara.composite:=parasym.paraloc[callerside].composite;
  269. {$endif powerpc}
  270. paraloc:=parasym.paraloc[callerside].location;
  271. while assigned(paraloc) do
  272. begin
  273. if paraloc^.size=OS_NO then
  274. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  275. else
  276. len:=tcgsize2size[paraloc^.size];
  277. newparaloc:=cgpara.add_location;
  278. newparaloc^.size:=paraloc^.size;
  279. { $warning maybe release this optimization for all targets? }
  280. { released for all CPUs:
  281. i386 isn't affected anyways because it uses the stack to push parameters
  282. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  283. { Does it fit a register? }
  284. if (len<=sizeof(pint)) and
  285. (cgpara.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  286. newparaloc^.loc:=LOC_REGISTER
  287. else
  288. newparaloc^.loc:=paraloc^.loc;
  289. case newparaloc^.loc of
  290. LOC_REGISTER :
  291. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  292. LOC_FPUREGISTER :
  293. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  294. LOC_MMREGISTER :
  295. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  296. LOC_REFERENCE :
  297. begin
  298. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  299. newparaloc^.reference.index:=href.base;
  300. newparaloc^.reference.offset:=href.offset;
  301. end;
  302. end;
  303. paraloc:=paraloc^.next;
  304. end;
  305. end;
  306. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  307. var
  308. paraloc,
  309. newparaloc : pcgparalocation;
  310. begin
  311. cgpara.reset;
  312. cgpara.size:=parasym.paraloc[callerside].size;
  313. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  314. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  315. {$ifdef powerpc}
  316. cgpara.composite:=parasym.paraloc[callerside].composite;
  317. {$endif powerpc}
  318. paraloc:=parasym.paraloc[callerside].location;
  319. while assigned(paraloc) do
  320. begin
  321. newparaloc:=cgpara.add_location;
  322. move(paraloc^,newparaloc^,sizeof(newparaloc^));
  323. newparaloc^.next:=nil;
  324. paraloc:=paraloc^.next;
  325. end;
  326. end;
  327. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  328. begin
  329. { We need to return the size allocated }
  330. create_paraloc_info(p,callerside);
  331. result:=create_paraloc_info(p,calleeside);
  332. end;
  333. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  334. begin
  335. Result:=False;
  336. internalerror(200807235);
  337. end;
  338. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  339. begin
  340. Result:=False;
  341. internalerror(200807236);
  342. end;
  343. initialization
  344. ;
  345. finalization
  346. paramanager.free;
  347. end.