paramgr.pas 30 KB

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