paramgr.pas 18 KB

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