paramgr.pas 31 KB

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