paramgr.pas 21 KB

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