paramgr.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. globtype,
  25. cpubase,cgbase,cgutils,
  26. parabase,
  27. 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. { Returns true if a result variable should be allocated for an assembler routine }
  40. function asm_result_var(def:tdef;pd:tabstractprocdef):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_address(calloption : tproccalloption):tcpuregisterset;virtual;
  68. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;virtual;
  69. function get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;virtual;
  70. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;virtual;
  71. {# Registers which must be saved when calling a routine declared as
  72. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  73. saved should be the ones as defined in the target ABI and / or GCC.
  74. This value can be deduced from the CALLED_USED_REGISTERS array in the
  75. GCC source.
  76. }
  77. function get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;virtual;
  78. function get_saved_registers_address(calloption : tproccalloption):tcpuregisterarray;virtual;
  79. function get_saved_registers_fpu(calloption : tproccalloption):tcpuregisterarray;virtual;
  80. function get_saved_registers_mm(calloption : tproccalloption):tcpuregisterarray;virtual;
  81. { \brief Get a parameter location for calling a procdef directly instead of via a call node }
  82. { \returns parameter location in \c cgpara for parameter \c nr of \c pd }
  83. procedure getcgtempparaloc(list: TAsmList; pd: tabstractprocdef; nr : longint; var cgpara: tcgpara);virtual;
  84. {# allocate an individual pcgparalocation that's part of a tcgpara
  85. @param(list Current assembler list)
  86. @param(loc Parameter location element)
  87. }
  88. procedure allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  89. {# allocate a parameter location created with create_paraloc_info
  90. @param(list Current assembler list)
  91. @param(loc Parameter location)
  92. }
  93. procedure alloccgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  94. {# free a parameter location allocated with alloccgpara
  95. @param(list Current assembler list)
  96. @param(loc Parameter location)
  97. }
  98. procedure freecgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  99. { This is used to populate the location information on all parameters
  100. for the routine as seen in either the caller or the callee. It returns
  101. the size allocated on the stack
  102. }
  103. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;virtual;abstract;
  104. { Returns the location of the function result if p had def as
  105. function result instead of its actual result. Used if the compiler
  106. forces the function result to something different than the real
  107. result. }
  108. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;virtual;abstract;
  109. function get_safecallresult_funcretloc(p : tabstractprocdef; side: tcallercallee): tcgpara; virtual;
  110. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  111. { This is used to populate the location information on all parameters
  112. for the routine when it is being inlined. It returns
  113. the size allocated on the stack
  114. }
  115. function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual;
  116. { This is used to populate the location information on all parameters
  117. for the routine that are passed as varargs. It returns
  118. the size allocated on the stack (including the normal parameters)
  119. }
  120. function create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;virtual;abstract;
  121. function is_stack_paraloc(paraloc: pcgparalocation): boolean;virtual;
  122. procedure createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);virtual;
  123. procedure duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  124. procedure duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  125. function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual;
  126. function parsefuncretloc(p : tabstractprocdef; const s : string) : boolean;virtual;
  127. { Convert a list of CGParaLocation entries to a RTTIParaLoc array that
  128. can be written by ncgrtti }
  129. function cgparalocs_to_rttiparalocs(paralocs:pcgparalocation):trttiparalocs;
  130. { allocate room for parameters on the stack in the entry code? }
  131. function use_fixed_stack: boolean;
  132. { whether stack pointer can be changed in the middle of procedure }
  133. function use_stackalloc: boolean;
  134. { Returns true for platforms where the parameters are part of the signature
  135. and checked by the runtime/backend compiler (e.g. JVM, LLVM).
  136. The default implementation returns false. }
  137. function has_strict_proc_signature: boolean; virtual;
  138. { Returns true if parasym is unused and can be optimized. }
  139. function can_opt_unused_para(parasym: tparavarsym): boolean;
  140. strict protected
  141. { common part of get_funcretloc; returns true if retloc is completely
  142. initialized afterwards }
  143. function set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  144. { common part of ret_in_param; is called by ret_in_param at the
  145. beginning and every tparamanager descendant can decide to call it
  146. itself as well; parameter retinparam is only valid if function
  147. returns true }
  148. function handle_common_ret_in_param(def:tdef;pd:tabstractprocdef;out retinparam:boolean):boolean;
  149. { returns the def to use for a tparalocation part of a cgpara for paradef,
  150. for which the def is paradef and the integer length is restlen.
  151. fullsize is true if restlen equals the full paradef size }
  152. function get_paraloc_def(paradef: tdef; restlen: aint; fullsize: boolean): tdef;
  153. { convert a single CGParaLocation to a RTTIParaLoc; the method *might*
  154. be overriden by targets to provide backwards compatibility with
  155. older versions in case register indices changed }
  156. function cgparaloc_to_rttiparaloc(paraloc:pcgparalocation):trttiparaloc;virtual;
  157. end;
  158. var
  159. paramanager : tparamanager;
  160. implementation
  161. uses
  162. systems,
  163. globals,
  164. cgobj,tgobj,
  165. defutil,verbose,
  166. hlcgobj;
  167. { true if the location in paraloc can be reused as localloc }
  168. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  169. begin
  170. result:=false;
  171. end;
  172. { true if uses a parameter as return value }
  173. function tparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  174. begin
  175. { This handles all managed types, including COM interfaces and Variants }
  176. if handle_common_ret_in_param(def,pd,result) then
  177. exit;
  178. ret_in_param:=(def.typ=arraydef) or
  179. (def.typ=recorddef) or
  180. (def.typ=stringdef) or
  181. ((def.typ=procvardef) and not tprocvardef(def).is_addressonly) or
  182. ((def.typ=objectdef) and (is_object(def))) or
  183. ((def.typ=setdef) and not is_smallset(def));
  184. end;
  185. { true if a result variable should be allocated for an assembler routine }
  186. function tparamanager.asm_result_var(def:tdef;pd:tabstractprocdef):boolean;
  187. begin
  188. if not(po_assembler in pd.procoptions) then
  189. internalerror(2018021501);
  190. result:=true;
  191. end;
  192. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  193. begin
  194. push_high_param:=not(calloption in cdecl_pocalls) and
  195. (
  196. is_open_array(def) or
  197. is_open_string(def) or
  198. is_array_of_const(def)
  199. );
  200. end;
  201. function tparamanager.keep_para_array_range(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  202. begin
  203. result:=push_high_param(varspez,def,calloption);
  204. end;
  205. function tparamanager.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  206. begin
  207. push_copyout_param:=false;
  208. end;
  209. { return the size of a push }
  210. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  211. begin
  212. push_size:=-1;
  213. case varspez of
  214. vs_constref,
  215. vs_out,
  216. vs_var :
  217. push_size:=voidpointertype.size;
  218. vs_value,
  219. vs_const :
  220. begin
  221. if push_addr_param(varspez,def,calloption) then
  222. push_size:=voidpointertype.size
  223. else
  224. begin
  225. { special array are normally pushed by addr, only for
  226. cdecl array of const it comes here and the pushsize
  227. is unknown }
  228. if is_array_of_const(def) then
  229. push_size:=0
  230. else
  231. push_size:=def.size;
  232. end;
  233. end;
  234. else
  235. ;
  236. end;
  237. end;
  238. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  239. begin
  240. result:=std_param_align;
  241. end;
  242. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  243. begin
  244. result:=[];
  245. end;
  246. function tparamanager.get_volatile_registers_address(calloption : tproccalloption):tcpuregisterset;
  247. begin
  248. result:=[];
  249. end;
  250. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  251. begin
  252. result:=[];
  253. end;
  254. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  255. begin
  256. result:=[];
  257. end;
  258. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  259. begin
  260. result:=[];
  261. end;
  262. function tparamanager.get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;
  263. const
  264. inv: tcpuregisterarray = (RS_INVALID);
  265. begin
  266. result:=inv;
  267. end;
  268. function tparamanager.get_saved_registers_address(calloption : tproccalloption):tcpuregisterarray;
  269. const
  270. inv: tcpuregisterarray = (RS_INVALID);
  271. begin
  272. result:=inv;
  273. end;
  274. function tparamanager.get_saved_registers_fpu(calloption : tproccalloption):tcpuregisterarray;
  275. const
  276. inv: tcpuregisterarray = (RS_INVALID);
  277. begin
  278. result:=inv;
  279. end;
  280. function tparamanager.get_saved_registers_mm(calloption : tproccalloption):tcpuregisterarray;
  281. const
  282. inv: tcpuregisterarray = (RS_INVALID);
  283. begin
  284. result:=inv;
  285. end;
  286. {$if first_mm_imreg = 0}
  287. {$WARN 4044 OFF} { Comparison might be always false ... }
  288. {$endif}
  289. procedure tparamanager.allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  290. begin
  291. case paraloc^.loc of
  292. LOC_REGISTER,
  293. LOC_CREGISTER:
  294. begin
  295. if getsupreg(paraloc^.register)<first_int_imreg then
  296. cg.getcpuregister(list,paraloc^.register);
  297. end;
  298. {$ifndef x86}
  299. { don't allocate ST(x), they're not handled by the register allocator }
  300. LOC_FPUREGISTER,
  301. LOC_CFPUREGISTER:
  302. begin
  303. if getsupreg(paraloc^.register)<first_fpu_imreg then
  304. cg.getcpuregister(list,paraloc^.register);
  305. end;
  306. {$endif not x86}
  307. LOC_MMREGISTER,
  308. LOC_CMMREGISTER :
  309. begin
  310. if getsupreg(paraloc^.register)<first_mm_imreg then
  311. cg.getcpuregister(list,paraloc^.register);
  312. end;
  313. else
  314. ;
  315. end;
  316. end;
  317. procedure tparamanager.alloccgpara(list: TAsmList; const cgpara: TCGPara);
  318. var
  319. paraloc : pcgparalocation;
  320. begin
  321. paraloc:=cgpara.location;
  322. while assigned(paraloc) do
  323. begin
  324. allocparaloc(list,paraloc);
  325. paraloc:=paraloc^.next;
  326. end;
  327. end;
  328. procedure tparamanager.freecgpara(list: TAsmList; const cgpara: TCGPara);
  329. var
  330. paraloc : Pcgparalocation;
  331. href : treference;
  332. begin
  333. paraloc:=cgpara.location;
  334. while assigned(paraloc) do
  335. begin
  336. case paraloc^.loc of
  337. LOC_VOID:
  338. ;
  339. LOC_REGISTER,
  340. LOC_CREGISTER:
  341. begin
  342. if getsupreg(paraloc^.register)<first_int_imreg then
  343. cg.ungetcpuregister(list,paraloc^.register);
  344. end;
  345. LOC_FPUREGISTER,
  346. LOC_CFPUREGISTER:
  347. begin
  348. if getsupreg(paraloc^.register)<first_fpu_imreg then
  349. cg.ungetcpuregister(list,paraloc^.register);
  350. end;
  351. LOC_MMREGISTER,
  352. LOC_CMMREGISTER :
  353. begin
  354. if getsupreg(paraloc^.register)<first_mm_imreg then
  355. cg.ungetcpuregister(list,paraloc^.register);
  356. end;
  357. LOC_REFERENCE,
  358. LOC_CREFERENCE :
  359. begin
  360. if use_fixed_stack then
  361. begin
  362. { don't use reference_reset_base, because that will depend on cgobj }
  363. fillchar(href,sizeof(href),0);
  364. href.base:=paraloc^.reference.index;
  365. href.offset:=paraloc^.reference.offset;
  366. href.temppos:=ctempposinvalid;
  367. tg.ungetiftemp(list,href);
  368. end;
  369. end;
  370. else
  371. internalerror(2004110212);
  372. end;
  373. paraloc:=paraloc^.next;
  374. end;
  375. end;
  376. function tparamanager.get_safecallresult_funcretloc(p: tabstractprocdef; side: tcallercallee): tcgpara;
  377. var
  378. paraloc: pcgparalocation;
  379. begin
  380. result.init;
  381. result.def:=ossinttype;
  382. result.intsize:=result.def.size;
  383. result.size:=def_cgsize(result.def);
  384. result.alignment:=result.def.alignment;
  385. paraloc:=result.add_location;
  386. paraloc^.size:=result.size;
  387. paraloc^.def:=result.def;
  388. paraloc^.loc:=LOC_REGISTER;
  389. if side=callerside then
  390. paraloc^.register:=NR_FUNCTION_RESULT_REG
  391. else
  392. paraloc^.register:=NR_FUNCTION_RETURN_REG;
  393. result.Temporary:=true;
  394. end;
  395. function tparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  396. begin
  397. result:=
  398. assigned(paraloc) and
  399. (paraloc^.loc=LOC_REFERENCE) and
  400. (paraloc^.reference.index=NR_STACK_POINTER_REG);
  401. end;
  402. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  403. var
  404. href : treference;
  405. len : aint;
  406. paraloc,
  407. newparaloc : pcgparalocation;
  408. begin
  409. paraloc:=parasym.paraloc[callerside].location;
  410. cgpara.reset;
  411. cgpara.size:=parasym.paraloc[callerside].size;
  412. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  413. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  414. cgpara.def:=parasym.paraloc[callerside].def;
  415. while assigned(paraloc) do
  416. begin
  417. if paraloc^.size=OS_NO then
  418. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  419. else
  420. len:=tcgsize2size[paraloc^.size];
  421. newparaloc:=cgpara.add_location;
  422. newparaloc^.size:=paraloc^.size;
  423. newparaloc^.def:=paraloc^.def;
  424. { shiftval overlaps with part of the reference, so it may be
  425. different from 0 and if wr then force the newparaloc to a register
  426. in the optimization below, shiftval will remain <> 0 }
  427. if not(paraloc^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  428. newparaloc^.shiftval:=paraloc^.shiftval;
  429. { $warning maybe release this optimization for all targets? }
  430. { released for all CPUs:
  431. i386 isn't affected anyways because it uses the stack to push parameters
  432. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  433. { Does it fit a register? }
  434. if ((not can_use_final_stack_loc and
  435. use_fixed_stack) or
  436. not is_stack_paraloc(paraloc)) and
  437. (len<=sizeof(pint)) and
  438. (paraloc^.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  439. newparaloc^.loc:=LOC_REGISTER
  440. else
  441. newparaloc^.loc:=paraloc^.loc;
  442. case newparaloc^.loc of
  443. LOC_REGISTER :
  444. begin
  445. if (vo_has_explicit_paraloc in parasym.varoptions) and (paraloc^.loc = LOC_REGISTER) then
  446. if getregtype(paraloc^.register) = R_ADDRESSREGISTER then
  447. newparaloc^.register:=cg.getaddressregister(list)
  448. else
  449. newparaloc^.register:=cg.getintregister(list,paraloc^.size)
  450. else
  451. begin
  452. {$ifdef cpu_uses_separate_address_registers}
  453. if hlcg.def2regtyp(paraloc^.def) = R_ADDRESSREGISTER then
  454. newparaloc^.register:=hlcg.getaddressregister(list,paraloc^.def)
  455. else
  456. {$endif}
  457. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  458. end;
  459. end;
  460. LOC_FPUREGISTER :
  461. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  462. LOC_MMREGISTER :
  463. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  464. LOC_REFERENCE :
  465. begin
  466. if (can_use_final_stack_loc or
  467. not use_fixed_stack) and
  468. is_stack_paraloc(paraloc) then
  469. duplicatecgparaloc(paraloc,newparaloc)
  470. else
  471. begin
  472. if assigned(cgpara.def) then
  473. tg.gethltemp(list,cgpara.def,len,tt_persistent,href)
  474. else
  475. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  476. newparaloc^.reference.index:=href.base;
  477. newparaloc^.reference.offset:=href.offset;
  478. end;
  479. end;
  480. else
  481. ;
  482. end;
  483. paraloc:=paraloc^.next;
  484. end;
  485. end;
  486. procedure tparamanager.duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  487. begin
  488. move(orgparaloc^,intonewparaloc^,sizeof(intonewparaloc^));
  489. intonewparaloc^.next:=nil;
  490. end;
  491. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  492. var
  493. paraloc,
  494. newparaloc : pcgparalocation;
  495. begin
  496. cgpara.reset;
  497. cgpara.size:=parasym.paraloc[callerside].size;
  498. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  499. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  500. paraloc:=parasym.paraloc[callerside].location;
  501. while assigned(paraloc) do
  502. begin
  503. newparaloc:=cgpara.add_location;
  504. duplicatecgparaloc(paraloc,newparaloc);
  505. paraloc:=paraloc^.next;
  506. end;
  507. end;
  508. procedure tparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  509. begin
  510. if not assigned(p.funcretloc[side].Location) then
  511. p.funcretloc[side]:=get_funcretloc(p,side,nil);
  512. end;
  513. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  514. begin
  515. { We need to return the size allocated }
  516. p.init_paraloc_info(callbothsides);
  517. result:=p.calleeargareasize;
  518. end;
  519. { used by syscall conventions which require explicit paralocation support }
  520. { this is the standard implementation, CGs might overwrite it }
  521. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  522. var
  523. paraloc : pcgparalocation;
  524. begin
  525. parasym.paraloc[callerside].alignment:=sizeof(pint);
  526. paraloc:=parasym.paraloc[callerside].add_location;
  527. paraloc^.loc:=LOC_REGISTER;
  528. paraloc^.size:=def_cgsize(parasym.vardef);
  529. paraloc^.def:=parasym.vardef;
  530. paraloc^.register:=std_regnum_search(lowercase(s));
  531. { copy to callee side }
  532. parasym.paraloc[calleeside].add_location^:=paraloc^;
  533. result:=(paraloc^.register <> NR_NO) and
  534. (paraloc^.register <> NR_STACK_POINTER_REG);
  535. end;
  536. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  537. begin
  538. Result:=False;
  539. internalerror(200807236);
  540. end;
  541. function tparamanager.use_fixed_stack: boolean;
  542. begin
  543. {$ifdef i386}
  544. result := target_info.stackalign > 4;
  545. {$else i386}
  546. {$ifdef cputargethasfixedstack}
  547. result := true;
  548. {$else cputargethasfixedstack}
  549. result := false;
  550. {$endif cputargethasfixedstack}
  551. {$endif i386}
  552. end;
  553. { This is a separate function because at least win64 allows stack allocations
  554. despite of fixed stack semantics (actually supporting it requires generating
  555. a compliant stack frame, not yet possible) }
  556. function tparamanager.use_stackalloc: boolean;
  557. begin
  558. result:=not use_fixed_stack;
  559. end;
  560. function tparamanager.has_strict_proc_signature: boolean;
  561. begin
  562. result:=false;
  563. end;
  564. function tparamanager.set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  565. var
  566. paraloc : pcgparalocation;
  567. begin
  568. result:=true;
  569. retloc.init;
  570. if not assigned(forcetempdef) then
  571. retloc.def:=p.returndef
  572. else
  573. begin
  574. retloc.def:=forcetempdef;
  575. retloc.temporary:=true;
  576. end;
  577. retloc.alignment:=get_para_align(p.proccalloption);
  578. { void has no location }
  579. if is_void(retloc.def) then
  580. begin
  581. paraloc:=retloc.add_location;
  582. retloc.size:=OS_NO;
  583. retcgsize:=OS_NO;
  584. retloc.intsize:=0;
  585. paraloc^.def:=retloc.def;
  586. paraloc^.size:=OS_NO;
  587. paraloc^.loc:=LOC_VOID;
  588. exit;
  589. end;
  590. { Constructors return self instead of a boolean }
  591. if p.proctypeoption=potype_constructor then
  592. begin
  593. retloc.def:=tdef(p.owner.defowner);
  594. if not (is_implicit_pointer_object_type(retloc.def) or
  595. (retloc.def.typ<>objectdef)) then
  596. retloc.def:=cpointerdef.getreusable_no_free(retloc.def);
  597. end;
  598. retcgsize:=def_cgsize(retloc.def);
  599. retloc.intsize:=retloc.def.size;
  600. retloc.size:=retcgsize;
  601. { Return is passed as var parameter }
  602. if ret_in_param(retloc.def,p) then
  603. begin
  604. retloc.def:=cpointerdef.getreusable_no_free(retloc.def);
  605. paraloc:=retloc.add_location;
  606. paraloc^.loc:=LOC_REFERENCE;
  607. paraloc^.size:=retcgsize;
  608. paraloc^.def:=retloc.def;
  609. exit;
  610. end;
  611. result:=false;
  612. end;
  613. function tparamanager.handle_common_ret_in_param(def: tdef;
  614. pd: tabstractprocdef; out retinparam: boolean): boolean;
  615. begin
  616. { This must be system independent: safecall and record constructor result
  617. is always returned in param.
  618. Furthermore, any managed type is returned in param, in order to avoid
  619. its finalization on exception at callee side. }
  620. if ((tf_safecall_exceptions in target_info.flags) and
  621. (pd.proccalloption=pocall_safecall)) or
  622. (
  623. (pd.proctypeoption=potype_constructor) and
  624. (
  625. is_record(def) or
  626. (
  627. (def.typ<>objectdef) and
  628. (pd.owner.symtabletype=objectsymtable) and
  629. is_objectpascal_helper(tdef(pd.owner.defowner))
  630. )
  631. )
  632. ) or is_managed_type(def) then
  633. begin
  634. retinparam:=true;
  635. exit(true);
  636. end;
  637. if pd.proctypeoption=potype_constructor then
  638. begin
  639. retinparam:=false;
  640. exit(true);
  641. end;
  642. result:=false;
  643. end;
  644. function tparamanager.get_paraloc_def(paradef: tdef; restlen: aint; fullsize: boolean): tdef;
  645. begin
  646. if fullsize then
  647. result:=paradef
  648. { no support for 128 bit ints -> tcgsize2orddef can't handle
  649. OS_(S)128 }
  650. else if restlen in [1,2,4,8] then
  651. result:=cgsize_orddef(int_cgsize(restlen))
  652. else
  653. result:=carraydef.getreusable_no_free(u8inttype,restlen);
  654. end;
  655. procedure tparamanager.getcgtempparaloc(list: TAsmList; pd: tabstractprocdef; nr : longint; var cgpara: tcgpara);
  656. begin
  657. if (nr<1) or (nr>pd.paras.count) then
  658. InternalError(2013060101);
  659. pd.init_paraloc_info(callerside);
  660. cgpara:=tparavarsym(pd.paras[nr-1]).paraloc[callerside].getcopy;
  661. end;
  662. function tparamanager.cgparalocs_to_rttiparalocs(paralocs:pcgparalocation):trttiparalocs;
  663. var
  664. c : longint;
  665. tmploc : pcgparalocation;
  666. begin
  667. c:=0;
  668. tmploc:=paralocs;
  669. result:=nil;
  670. while assigned(tmploc) do
  671. begin
  672. inc(c);
  673. tmploc:=tmploc^.next;
  674. end;
  675. setlength(result,c);
  676. c:=0;
  677. tmploc:=paralocs;
  678. while assigned(tmploc) do
  679. begin
  680. result[c]:=cgparaloc_to_rttiparaloc(tmploc);
  681. inc(c);
  682. tmploc:=tmploc^.next;
  683. end;
  684. end;
  685. function tparamanager.cgparaloc_to_rttiparaloc(paraloc:pcgparalocation):trttiparaloc;
  686. var
  687. reg : tregisterrec;
  688. begin
  689. { Explicitly zero the whole record, to avoid
  690. trouble as this record is used as is in a
  691. hash calculation, which might give unreliable
  692. results if the record as gaps between fields
  693. due to field alignment. PM 2021-05-06 }
  694. fillchar(result,sizeof(trttiparaloc),#0);
  695. if paraloc^.Loc=LOC_REFERENCE then
  696. begin
  697. reg:=tregisterrec(paraloc^.reference.index);
  698. result.offset:=paraloc^.reference.offset;
  699. result.loctype:=$80;
  700. end
  701. else
  702. begin
  703. reg:=tregisterrec(paraloc^.register);
  704. { use sign extension }
  705. result.offset:=paraloc^.shiftval;
  706. result.loctype:=$00;
  707. end;
  708. case reg.regtype of
  709. R_INTREGISTER,
  710. R_FPUREGISTER,
  711. R_MMXREGISTER,
  712. R_MMREGISTER,
  713. R_SPECIALREGISTER,
  714. R_ADDRESSREGISTER:
  715. begin
  716. result.loctype:=result.loctype or ord(reg.regtype);
  717. result.regsub:=ord(reg.subreg);
  718. result.regindex:=reg.supreg;
  719. end;
  720. else
  721. begin
  722. { no need to adjust loctype }
  723. result.regsub:=0;
  724. result.regindex:=0;
  725. end;
  726. end;
  727. end;
  728. function tparamanager.can_opt_unused_para(parasym: tparavarsym): boolean;
  729. var
  730. pd: tprocdef;
  731. begin
  732. { The parameter can be optimized as unused when:
  733. this optimization is enabled
  734. this is a direct call to a routine, not a procvar
  735. and the routine is not an exception filter
  736. and the parameter is not used by the routine
  737. and implementation of the routine is already processed.
  738. }
  739. result:=(cs_opt_unused_para in current_settings.optimizerswitches) and
  740. assigned(parasym.Owner) and
  741. (parasym.Owner.defowner.typ=procdef);
  742. if not result then
  743. exit;
  744. pd:=tprocdef(parasym.Owner.defowner);
  745. result:=(pd.proctypeoption<>potype_exceptfilter) and
  746. not parasym.is_used and
  747. pd.is_implemented;
  748. end;
  749. initialization
  750. ;
  751. finalization
  752. paramanager.free;
  753. end.