paramgr.pas 18 KB

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