paramgr.pas 31 KB

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