paramgr.pas 16 KB

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