paramgr.pas 15 KB

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