paramgr.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 overridden 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_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 duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  104. procedure duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  105. function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual;
  106. function parsefuncretloc(p : tabstractprocdef; const s : string) : boolean;virtual;
  107. { allocate room for parameters on the stack in the entry code? }
  108. function use_fixed_stack: boolean;
  109. end;
  110. var
  111. paramanager : tparamanager;
  112. implementation
  113. uses
  114. systems,
  115. cgobj,tgobj,
  116. defutil,verbose;
  117. { true if the location in paraloc can be reused as localloc }
  118. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  119. begin
  120. result:=false;
  121. end;
  122. { true if uses a parameter as return value }
  123. function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  124. begin
  125. ret_in_param:=((def.typ=arraydef) and not(is_dynamic_array(def))) or
  126. (def.typ=recorddef) or
  127. (def.typ=stringdef) or
  128. ((def.typ=procvardef) and not tprocvardef(def).is_addressonly) or
  129. { interfaces are also passed by reference to be compatible with delphi and COM }
  130. ((def.typ=objectdef) and (is_object(def) or is_interface(def) or is_dispinterface(def))) or
  131. (def.typ=variantdef) or
  132. ((def.typ=setdef) and not is_smallset(def));
  133. end;
  134. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  135. begin
  136. push_high_param:=not(calloption in cdecl_pocalls) and
  137. (
  138. is_open_array(def) or
  139. is_open_string(def) or
  140. is_array_of_const(def)
  141. );
  142. end;
  143. { return the size of a push }
  144. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  145. begin
  146. push_size:=-1;
  147. case varspez of
  148. vs_constref,
  149. vs_out,
  150. vs_var :
  151. push_size:=sizeof(pint);
  152. vs_value,
  153. vs_const :
  154. begin
  155. if push_addr_param(varspez,def,calloption) then
  156. push_size:=sizeof(pint)
  157. else
  158. begin
  159. { special array are normally pushed by addr, only for
  160. cdecl array of const it comes here and the pushsize
  161. is unknown }
  162. if is_array_of_const(def) then
  163. push_size:=0
  164. else
  165. push_size:=def.size;
  166. end;
  167. end;
  168. end;
  169. end;
  170. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  171. begin
  172. result:=std_param_align;
  173. end;
  174. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  175. begin
  176. result:=[];
  177. end;
  178. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  179. begin
  180. result:=[];
  181. end;
  182. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  183. begin
  184. result:=[];
  185. end;
  186. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  187. begin
  188. result:=[];
  189. end;
  190. procedure tparamanager.allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  191. begin
  192. case paraloc^.loc of
  193. LOC_REGISTER,
  194. LOC_CREGISTER:
  195. begin
  196. if getsupreg(paraloc^.register)<first_int_imreg then
  197. cg.getcpuregister(list,paraloc^.register);
  198. end;
  199. {$ifndef x86}
  200. { don't allocate ST(x), they're not handled by the register allocator }
  201. LOC_FPUREGISTER,
  202. LOC_CFPUREGISTER:
  203. begin
  204. if getsupreg(paraloc^.register)<first_fpu_imreg then
  205. cg.getcpuregister(list,paraloc^.register);
  206. end;
  207. {$endif not x86}
  208. LOC_MMREGISTER,
  209. LOC_CMMREGISTER :
  210. begin
  211. if getsupreg(paraloc^.register)<first_mm_imreg then
  212. cg.getcpuregister(list,paraloc^.register);
  213. end;
  214. end;
  215. end;
  216. procedure tparamanager.alloccgpara(list: TAsmList; const cgpara: TCGPara);
  217. var
  218. paraloc : pcgparalocation;
  219. begin
  220. paraloc:=cgpara.location;
  221. while assigned(paraloc) do
  222. begin
  223. allocparaloc(list,paraloc);
  224. paraloc:=paraloc^.next;
  225. end;
  226. end;
  227. procedure tparamanager.freecgpara(list: TAsmList; const cgpara: TCGPara);
  228. var
  229. paraloc : Pcgparalocation;
  230. href : treference;
  231. begin
  232. paraloc:=cgpara.location;
  233. while assigned(paraloc) do
  234. begin
  235. case paraloc^.loc of
  236. LOC_VOID:
  237. ;
  238. LOC_REGISTER,
  239. LOC_CREGISTER:
  240. begin
  241. if getsupreg(paraloc^.register)<first_int_imreg then
  242. cg.ungetcpuregister(list,paraloc^.register);
  243. end;
  244. LOC_FPUREGISTER,
  245. LOC_CFPUREGISTER:
  246. begin
  247. if getsupreg(paraloc^.register)<first_fpu_imreg then
  248. cg.ungetcpuregister(list,paraloc^.register);
  249. end;
  250. LOC_MMREGISTER,
  251. LOC_CMMREGISTER :
  252. begin
  253. if getsupreg(paraloc^.register)<first_mm_imreg then
  254. cg.ungetcpuregister(list,paraloc^.register);
  255. end;
  256. LOC_REFERENCE,
  257. LOC_CREFERENCE :
  258. begin
  259. if use_fixed_stack then
  260. begin
  261. { don't use reference_reset_base, because that will depend on cgobj }
  262. fillchar(href,sizeof(href),0);
  263. href.base:=paraloc^.reference.index;
  264. href.offset:=paraloc^.reference.offset;
  265. tg.ungetiftemp(list,href);
  266. end;
  267. end;
  268. else
  269. internalerror(2004110212);
  270. end;
  271. paraloc:=paraloc^.next;
  272. end;
  273. end;
  274. function tparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  275. begin
  276. result:=
  277. assigned(paraloc) and
  278. (paraloc^.loc=LOC_REFERENCE) and
  279. (paraloc^.reference.index=NR_STACK_POINTER_REG);
  280. end;
  281. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  282. var
  283. href : treference;
  284. len : aint;
  285. paraloc,
  286. newparaloc : pcgparalocation;
  287. begin
  288. paraloc:=parasym.paraloc[callerside].location;
  289. cgpara.reset;
  290. cgpara.size:=parasym.paraloc[callerside].size;
  291. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  292. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  293. {$ifdef powerpc}
  294. cgpara.composite:=parasym.paraloc[callerside].composite;
  295. {$endif powerpc}
  296. while assigned(paraloc) do
  297. begin
  298. if paraloc^.size=OS_NO then
  299. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  300. else
  301. len:=tcgsize2size[paraloc^.size];
  302. newparaloc:=cgpara.add_location;
  303. newparaloc^.size:=paraloc^.size;
  304. { $warning maybe release this optimization for all targets? }
  305. { released for all CPUs:
  306. i386 isn't affected anyways because it uses the stack to push parameters
  307. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  308. { Does it fit a register? }
  309. if ((not can_use_final_stack_loc and
  310. use_fixed_stack) or
  311. not is_stack_paraloc(paraloc)) and
  312. (len<=sizeof(pint)) and
  313. (paraloc^.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  314. newparaloc^.loc:=LOC_REGISTER
  315. else
  316. newparaloc^.loc:=paraloc^.loc;
  317. case newparaloc^.loc of
  318. LOC_REGISTER :
  319. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  320. LOC_FPUREGISTER :
  321. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  322. LOC_MMREGISTER :
  323. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  324. LOC_REFERENCE :
  325. begin
  326. if (can_use_final_stack_loc or
  327. not use_fixed_stack) and
  328. is_stack_paraloc(paraloc) then
  329. duplicatecgparaloc(paraloc,newparaloc)
  330. else
  331. begin
  332. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  333. newparaloc^.reference.index:=href.base;
  334. newparaloc^.reference.offset:=href.offset;
  335. end;
  336. end;
  337. end;
  338. paraloc:=paraloc^.next;
  339. end;
  340. end;
  341. procedure tparamanager.duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  342. begin
  343. move(orgparaloc^,intonewparaloc^,sizeof(intonewparaloc^));
  344. intonewparaloc^.next:=nil;
  345. end;
  346. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  347. var
  348. paraloc,
  349. newparaloc : pcgparalocation;
  350. begin
  351. cgpara.reset;
  352. cgpara.size:=parasym.paraloc[callerside].size;
  353. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  354. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  355. {$ifdef powerpc}
  356. cgpara.composite:=parasym.paraloc[callerside].composite;
  357. {$endif powerpc}
  358. paraloc:=parasym.paraloc[callerside].location;
  359. while assigned(paraloc) do
  360. begin
  361. newparaloc:=cgpara.add_location;
  362. duplicatecgparaloc(paraloc,newparaloc);
  363. paraloc:=paraloc^.next;
  364. end;
  365. end;
  366. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  367. begin
  368. { We need to return the size allocated }
  369. p.init_paraloc_info(callbothsides);
  370. result:=p.calleeargareasize;
  371. end;
  372. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  373. begin
  374. Result:=False;
  375. internalerror(200807235);
  376. end;
  377. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  378. begin
  379. Result:=False;
  380. internalerror(200807236);
  381. end;
  382. function tparamanager.use_fixed_stack: boolean;
  383. begin
  384. {$ifdef i386}
  385. result := (target_info.system in [system_i386_darwin,system_i386_iphonesim]);
  386. {$else i386}
  387. {$ifdef cputargethasfixedstack}
  388. result := true;
  389. {$else cputargethasfixedstack}
  390. result := false;
  391. {$endif cputargethasfixedstack}
  392. {$endif i386}
  393. end;
  394. initialization
  395. ;
  396. finalization
  397. paramanager.free;
  398. end.