paramgr.pas 22 KB

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