paramgr.pas 28 KB

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