paramgr.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 pointer }
  38. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;virtual;
  39. function push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  40. function keep_para_array_range(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. { returns true if a parameter must be handled via copy-out (construct
  46. a reference, copy the parameter's value there in case of copy-in/out, pass the reference)
  47. }
  48. function push_copyout_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  49. { return the size of a push }
  50. function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;virtual;
  51. {# Returns a structure giving the information on
  52. the storage of the parameter (which must be
  53. an integer parameter). This is only used when calling
  54. internal routines directly, where all parameters must
  55. be 4-byte values.
  56. In case the location is a register, this register is allocated.
  57. Call freeintparaloc() after the call to free the locations again.
  58. Default implementation: don't do anything at all (in case you don't
  59. use register parameter passing)
  60. @param(list Current assembler list)
  61. @param(nr Parameter number of routine, starting from 1)
  62. }
  63. function get_para_align(calloption : tproccalloption):byte;virtual;
  64. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;virtual;
  65. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;virtual;
  66. function get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;virtual;
  67. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;virtual;
  68. procedure getintparaloc(pd: tabstractprocdef; nr : longint; var cgpara: tcgpara);virtual;abstract;
  69. {# allocate an individual pcgparalocation that's part of a tcgpara
  70. @param(list Current assembler list)
  71. @param(loc Parameter location element)
  72. }
  73. procedure allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  74. {# allocate a parameter location created with create_paraloc_info
  75. @param(list Current assembler list)
  76. @param(loc Parameter location)
  77. }
  78. procedure alloccgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  79. {# free a parameter location allocated with alloccgpara
  80. @param(list Current assembler list)
  81. @param(loc Parameter location)
  82. }
  83. procedure freecgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  84. { This is used to populate the location information on all parameters
  85. for the routine as seen in either the caller or the callee. It returns
  86. the size allocated on the stack
  87. }
  88. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;virtual;abstract;
  89. { Returns the location of the function result if p had def as
  90. function result instead of its actual result. Used if the compiler
  91. forces the function result to something different than the real
  92. result. }
  93. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;virtual;abstract;
  94. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  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. strict protected
  116. { common part of get_funcretloc; returns true if retloc is completely
  117. initialized afterwards }
  118. function set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  119. end;
  120. var
  121. paramanager : tparamanager;
  122. implementation
  123. uses
  124. systems,
  125. cgobj,tgobj,
  126. defutil,verbose;
  127. { true if the location in paraloc can be reused as localloc }
  128. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  129. begin
  130. result:=false;
  131. end;
  132. { true if uses a parameter as return value }
  133. function tparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  134. begin
  135. { this must be system independent safecall and record constructor result
  136. is always return in param }
  137. if (tf_safecall_exceptions in target_info.flags) and
  138. (pd.proccalloption=pocall_safecall) or
  139. ((pd.proctypeoption=potype_constructor)and is_record(def)) then
  140. begin
  141. result:=true;
  142. exit;
  143. end;
  144. ret_in_param:=((def.typ=arraydef) and not(is_dynamic_array(def))) or
  145. (def.typ=recorddef) or
  146. (def.typ=stringdef) or
  147. ((def.typ=procvardef) and not tprocvardef(def).is_addressonly) or
  148. { interfaces are also passed by reference to be compatible with delphi and COM }
  149. ((def.typ=objectdef) and (is_object(def) or is_interface(def) or is_dispinterface(def))) or
  150. (def.typ=variantdef) or
  151. ((def.typ=setdef) and not is_smallset(def));
  152. end;
  153. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  154. begin
  155. push_high_param:=not(calloption in cdecl_pocalls) and
  156. (
  157. is_open_array(def) or
  158. is_open_string(def) or
  159. is_array_of_const(def)
  160. );
  161. end;
  162. function tparamanager.keep_para_array_range(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  163. begin
  164. result:=push_high_param(varspez,def,calloption);
  165. end;
  166. function tparamanager.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  167. begin
  168. push_copyout_param:=false;
  169. end;
  170. { return the size of a push }
  171. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  172. begin
  173. push_size:=-1;
  174. case varspez of
  175. vs_constref,
  176. vs_out,
  177. vs_var :
  178. push_size:=sizeof(pint);
  179. vs_value,
  180. vs_const :
  181. begin
  182. if push_addr_param(varspez,def,calloption) then
  183. push_size:=sizeof(pint)
  184. else
  185. begin
  186. { special array are normally pushed by addr, only for
  187. cdecl array of const it comes here and the pushsize
  188. is unknown }
  189. if is_array_of_const(def) then
  190. push_size:=0
  191. else
  192. push_size:=def.size;
  193. end;
  194. end;
  195. end;
  196. end;
  197. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  198. begin
  199. result:=std_param_align;
  200. end;
  201. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  202. begin
  203. result:=[];
  204. end;
  205. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  206. begin
  207. result:=[];
  208. end;
  209. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  210. begin
  211. result:=[];
  212. end;
  213. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  214. begin
  215. result:=[];
  216. end;
  217. {$if first_mm_imreg = 0}
  218. {$WARN 4044 OFF} { Comparison might be always false ... }
  219. {$endif}
  220. procedure tparamanager.allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  221. begin
  222. case paraloc^.loc of
  223. LOC_REGISTER,
  224. LOC_CREGISTER:
  225. begin
  226. if getsupreg(paraloc^.register)<first_int_imreg then
  227. cg.getcpuregister(list,paraloc^.register);
  228. end;
  229. {$ifndef x86}
  230. { don't allocate ST(x), they're not handled by the register allocator }
  231. LOC_FPUREGISTER,
  232. LOC_CFPUREGISTER:
  233. begin
  234. if getsupreg(paraloc^.register)<first_fpu_imreg then
  235. cg.getcpuregister(list,paraloc^.register);
  236. end;
  237. {$endif not x86}
  238. LOC_MMREGISTER,
  239. LOC_CMMREGISTER :
  240. begin
  241. if getsupreg(paraloc^.register)<first_mm_imreg then
  242. cg.getcpuregister(list,paraloc^.register);
  243. end;
  244. end;
  245. end;
  246. procedure tparamanager.alloccgpara(list: TAsmList; const cgpara: TCGPara);
  247. var
  248. paraloc : pcgparalocation;
  249. begin
  250. paraloc:=cgpara.location;
  251. while assigned(paraloc) do
  252. begin
  253. allocparaloc(list,paraloc);
  254. paraloc:=paraloc^.next;
  255. end;
  256. end;
  257. procedure tparamanager.freecgpara(list: TAsmList; const cgpara: TCGPara);
  258. var
  259. paraloc : Pcgparalocation;
  260. href : treference;
  261. begin
  262. paraloc:=cgpara.location;
  263. while assigned(paraloc) do
  264. begin
  265. case paraloc^.loc of
  266. LOC_VOID:
  267. ;
  268. LOC_REGISTER,
  269. LOC_CREGISTER:
  270. begin
  271. if getsupreg(paraloc^.register)<first_int_imreg then
  272. cg.ungetcpuregister(list,paraloc^.register);
  273. end;
  274. LOC_FPUREGISTER,
  275. LOC_CFPUREGISTER:
  276. begin
  277. if getsupreg(paraloc^.register)<first_fpu_imreg then
  278. cg.ungetcpuregister(list,paraloc^.register);
  279. end;
  280. LOC_MMREGISTER,
  281. LOC_CMMREGISTER :
  282. begin
  283. if getsupreg(paraloc^.register)<first_mm_imreg then
  284. cg.ungetcpuregister(list,paraloc^.register);
  285. end;
  286. LOC_REFERENCE,
  287. LOC_CREFERENCE :
  288. begin
  289. if use_fixed_stack then
  290. begin
  291. { don't use reference_reset_base, because that will depend on cgobj }
  292. fillchar(href,sizeof(href),0);
  293. href.base:=paraloc^.reference.index;
  294. href.offset:=paraloc^.reference.offset;
  295. tg.ungetiftemp(list,href);
  296. end;
  297. end;
  298. else
  299. internalerror(2004110212);
  300. end;
  301. paraloc:=paraloc^.next;
  302. end;
  303. end;
  304. function tparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  305. begin
  306. result:=
  307. assigned(paraloc) and
  308. (paraloc^.loc=LOC_REFERENCE) and
  309. (paraloc^.reference.index=NR_STACK_POINTER_REG);
  310. end;
  311. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  312. var
  313. href : treference;
  314. len : aint;
  315. paraloc,
  316. newparaloc : pcgparalocation;
  317. begin
  318. paraloc:=parasym.paraloc[callerside].location;
  319. cgpara.reset;
  320. cgpara.size:=parasym.paraloc[callerside].size;
  321. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  322. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  323. cgpara.def:=parasym.paraloc[callerside].def;
  324. {$ifdef powerpc}
  325. cgpara.composite:=parasym.paraloc[callerside].composite;
  326. {$endif powerpc}
  327. while assigned(paraloc) do
  328. begin
  329. if paraloc^.size=OS_NO then
  330. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  331. else
  332. len:=tcgsize2size[paraloc^.size];
  333. newparaloc:=cgpara.add_location;
  334. newparaloc^.size:=paraloc^.size;
  335. newparaloc^.shiftval:=paraloc^.shiftval;
  336. { $warning maybe release this optimization for all targets? }
  337. { released for all CPUs:
  338. i386 isn't affected anyways because it uses the stack to push parameters
  339. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  340. { Does it fit a register? }
  341. if ((not can_use_final_stack_loc and
  342. use_fixed_stack) or
  343. not is_stack_paraloc(paraloc)) and
  344. (len<=sizeof(pint)) and
  345. (paraloc^.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  346. newparaloc^.loc:=LOC_REGISTER
  347. else
  348. newparaloc^.loc:=paraloc^.loc;
  349. case newparaloc^.loc of
  350. LOC_REGISTER :
  351. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  352. LOC_FPUREGISTER :
  353. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  354. LOC_MMREGISTER :
  355. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  356. LOC_REFERENCE :
  357. begin
  358. if (can_use_final_stack_loc or
  359. not use_fixed_stack) and
  360. is_stack_paraloc(paraloc) then
  361. duplicatecgparaloc(paraloc,newparaloc)
  362. else
  363. begin
  364. if assigned(cgpara.def) then
  365. tg.gethltemp(list,cgpara.def,len,tt_persistent,href)
  366. else
  367. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  368. newparaloc^.reference.index:=href.base;
  369. newparaloc^.reference.offset:=href.offset;
  370. end;
  371. end;
  372. end;
  373. paraloc:=paraloc^.next;
  374. end;
  375. end;
  376. procedure tparamanager.duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  377. begin
  378. move(orgparaloc^,intonewparaloc^,sizeof(intonewparaloc^));
  379. intonewparaloc^.next:=nil;
  380. end;
  381. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  382. var
  383. paraloc,
  384. newparaloc : pcgparalocation;
  385. begin
  386. cgpara.reset;
  387. cgpara.size:=parasym.paraloc[callerside].size;
  388. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  389. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  390. {$ifdef powerpc}
  391. cgpara.composite:=parasym.paraloc[callerside].composite;
  392. {$endif powerpc}
  393. paraloc:=parasym.paraloc[callerside].location;
  394. while assigned(paraloc) do
  395. begin
  396. newparaloc:=cgpara.add_location;
  397. duplicatecgparaloc(paraloc,newparaloc);
  398. paraloc:=paraloc^.next;
  399. end;
  400. end;
  401. procedure tparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  402. begin
  403. p.funcretloc[side]:=get_funcretloc(p,side,nil);
  404. end;
  405. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  406. begin
  407. { We need to return the size allocated }
  408. p.init_paraloc_info(callbothsides);
  409. result:=p.calleeargareasize;
  410. end;
  411. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  412. begin
  413. Result:=False;
  414. internalerror(200807235);
  415. end;
  416. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  417. begin
  418. Result:=False;
  419. internalerror(200807236);
  420. end;
  421. function tparamanager.use_fixed_stack: boolean;
  422. begin
  423. {$ifdef i386}
  424. result := target_info.stackalign > 4;
  425. {$else i386}
  426. {$ifdef cputargethasfixedstack}
  427. result := true;
  428. {$else cputargethasfixedstack}
  429. result := false;
  430. {$endif cputargethasfixedstack}
  431. {$endif i386}
  432. end;
  433. { This is a separate function because at least win64 allows stack allocations
  434. despite of fixed stack semantics (actually supporting it requires generating
  435. a compliant stack frame, not yet possible) }
  436. function tparamanager.use_stackalloc: boolean;
  437. begin
  438. result:=not use_fixed_stack;
  439. end;
  440. function tparamanager.set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  441. var
  442. paraloc : pcgparalocation;
  443. begin
  444. result:=true;
  445. retloc.init;
  446. if not assigned(forcetempdef) then
  447. retloc.def:=p.returndef
  448. else
  449. begin
  450. retloc.def:=forcetempdef;
  451. retloc.temporary:=true;
  452. end;
  453. retloc.alignment:=get_para_align(p.proccalloption);
  454. { void has no location }
  455. if is_void(retloc.def) then
  456. begin
  457. paraloc:=retloc.add_location;
  458. retloc.size:=OS_NO;
  459. retcgsize:=OS_NO;
  460. retloc.intsize:=0;
  461. paraloc^.size:=OS_NO;
  462. paraloc^.loc:=LOC_VOID;
  463. exit;
  464. end;
  465. { Constructors return self instead of a boolean }
  466. if p.proctypeoption=potype_constructor then
  467. begin
  468. retloc.def:=tdef(p.owner.defowner);
  469. if not (is_implicit_pointer_object_type(retloc.def) or
  470. is_record(retloc.def)) then
  471. retloc.def:=getpointerdef(retloc.def);
  472. end;
  473. retcgsize:=def_cgsize(retloc.def);
  474. retloc.intsize:=retloc.def.size;
  475. retloc.size:=retcgsize;
  476. { Return is passed as var parameter }
  477. if ret_in_param(retloc.def,p) then
  478. begin
  479. retloc.def:=getpointerdef(retloc.def);
  480. paraloc:=retloc.add_location;
  481. paraloc^.loc:=LOC_REFERENCE;
  482. paraloc^.size:=retcgsize;
  483. exit;
  484. end;
  485. result:=false;
  486. end;
  487. initialization
  488. ;
  489. finalization
  490. paramanager.free;
  491. end.