paramgr.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. tg.ungetiftemp(list,href);
  353. end;
  354. end;
  355. else
  356. internalerror(2004110212);
  357. end;
  358. paraloc:=paraloc^.next;
  359. end;
  360. end;
  361. function tparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  362. begin
  363. result:=
  364. assigned(paraloc) and
  365. (paraloc^.loc=LOC_REFERENCE) and
  366. (paraloc^.reference.index=NR_STACK_POINTER_REG);
  367. end;
  368. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  369. var
  370. href : treference;
  371. len : aint;
  372. paraloc,
  373. newparaloc : pcgparalocation;
  374. begin
  375. paraloc:=parasym.paraloc[callerside].location;
  376. cgpara.reset;
  377. cgpara.size:=parasym.paraloc[callerside].size;
  378. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  379. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  380. cgpara.def:=parasym.paraloc[callerside].def;
  381. while assigned(paraloc) do
  382. begin
  383. if paraloc^.size=OS_NO then
  384. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  385. else
  386. len:=tcgsize2size[paraloc^.size];
  387. newparaloc:=cgpara.add_location;
  388. newparaloc^.size:=paraloc^.size;
  389. newparaloc^.def:=paraloc^.def;
  390. { shiftval overlaps with part of the reference, so it may be
  391. different from 0 and if wr then force the newparaloc to a register
  392. in the optimization below, shiftval will remain <> 0 }
  393. if not(paraloc^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  394. newparaloc^.shiftval:=paraloc^.shiftval;
  395. { $warning maybe release this optimization for all targets? }
  396. { released for all CPUs:
  397. i386 isn't affected anyways because it uses the stack to push parameters
  398. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  399. { Does it fit a register? }
  400. if ((not can_use_final_stack_loc and
  401. use_fixed_stack) or
  402. not is_stack_paraloc(paraloc)) and
  403. (len<=sizeof(pint)) and
  404. (paraloc^.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  405. newparaloc^.loc:=LOC_REGISTER
  406. else
  407. newparaloc^.loc:=paraloc^.loc;
  408. case newparaloc^.loc of
  409. LOC_REGISTER :
  410. begin
  411. if (vo_has_explicit_paraloc in parasym.varoptions) and (paraloc^.loc = LOC_REGISTER) then
  412. if getregtype(paraloc^.register) = R_ADDRESSREGISTER then
  413. newparaloc^.register:=cg.getaddressregister(list)
  414. else
  415. newparaloc^.register:=cg.getintregister(list,paraloc^.size)
  416. else
  417. begin
  418. {$ifdef cpu_uses_separate_address_registers}
  419. if hlcg.def2regtyp(paraloc^.def) = R_ADDRESSREGISTER then
  420. newparaloc^.register:=hlcg.getaddressregister(list,paraloc^.def)
  421. else
  422. {$endif}
  423. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  424. end;
  425. end;
  426. LOC_FPUREGISTER :
  427. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  428. LOC_MMREGISTER :
  429. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  430. LOC_REFERENCE :
  431. begin
  432. if (can_use_final_stack_loc or
  433. not use_fixed_stack) and
  434. is_stack_paraloc(paraloc) then
  435. duplicatecgparaloc(paraloc,newparaloc)
  436. else
  437. begin
  438. if assigned(cgpara.def) then
  439. tg.gethltemp(list,cgpara.def,len,tt_persistent,href)
  440. else
  441. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  442. newparaloc^.reference.index:=href.base;
  443. newparaloc^.reference.offset:=href.offset;
  444. end;
  445. end;
  446. end;
  447. paraloc:=paraloc^.next;
  448. end;
  449. end;
  450. procedure tparamanager.duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  451. begin
  452. move(orgparaloc^,intonewparaloc^,sizeof(intonewparaloc^));
  453. intonewparaloc^.next:=nil;
  454. end;
  455. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  456. var
  457. paraloc,
  458. newparaloc : pcgparalocation;
  459. begin
  460. cgpara.reset;
  461. cgpara.size:=parasym.paraloc[callerside].size;
  462. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  463. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  464. paraloc:=parasym.paraloc[callerside].location;
  465. while assigned(paraloc) do
  466. begin
  467. newparaloc:=cgpara.add_location;
  468. duplicatecgparaloc(paraloc,newparaloc);
  469. paraloc:=paraloc^.next;
  470. end;
  471. end;
  472. procedure tparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  473. begin
  474. if not assigned(p.funcretloc[side].Location) then
  475. p.funcretloc[side]:=get_funcretloc(p,side,nil);
  476. end;
  477. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  478. begin
  479. { We need to return the size allocated }
  480. p.init_paraloc_info(callbothsides);
  481. result:=p.calleeargareasize;
  482. end;
  483. { used by syscall conventions which require explicit paralocation support }
  484. { this is the standard implementation, CGs might overwrite it }
  485. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  486. var
  487. paraloc : pcgparalocation;
  488. begin
  489. parasym.paraloc[callerside].alignment:=sizeof(pint);
  490. paraloc:=parasym.paraloc[callerside].add_location;
  491. paraloc^.loc:=LOC_REGISTER;
  492. paraloc^.size:=def_cgsize(parasym.vardef);
  493. paraloc^.def:=parasym.vardef;
  494. paraloc^.register:=std_regnum_search(lowercase(s));
  495. { copy to callee side }
  496. parasym.paraloc[calleeside].add_location^:=paraloc^;
  497. result:=(paraloc^.register <> NR_NO) and
  498. (paraloc^.register <> NR_STACK_POINTER_REG);
  499. end;
  500. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  501. begin
  502. Result:=False;
  503. internalerror(200807236);
  504. end;
  505. function tparamanager.use_fixed_stack: boolean;
  506. begin
  507. {$ifdef i386}
  508. result := target_info.stackalign > 4;
  509. {$else i386}
  510. {$ifdef cputargethasfixedstack}
  511. result := true;
  512. {$else cputargethasfixedstack}
  513. result := false;
  514. {$endif cputargethasfixedstack}
  515. {$endif i386}
  516. end;
  517. { This is a separate function because at least win64 allows stack allocations
  518. despite of fixed stack semantics (actually supporting it requires generating
  519. a compliant stack frame, not yet possible) }
  520. function tparamanager.use_stackalloc: boolean;
  521. begin
  522. result:=not use_fixed_stack;
  523. end;
  524. function tparamanager.set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  525. var
  526. paraloc : pcgparalocation;
  527. begin
  528. result:=true;
  529. retloc.init;
  530. if not assigned(forcetempdef) then
  531. retloc.def:=p.returndef
  532. else
  533. begin
  534. retloc.def:=forcetempdef;
  535. retloc.temporary:=true;
  536. end;
  537. retloc.alignment:=get_para_align(p.proccalloption);
  538. { void has no location }
  539. if is_void(retloc.def) then
  540. begin
  541. paraloc:=retloc.add_location;
  542. retloc.size:=OS_NO;
  543. retcgsize:=OS_NO;
  544. retloc.intsize:=0;
  545. paraloc^.def:=retloc.def;
  546. paraloc^.size:=OS_NO;
  547. paraloc^.loc:=LOC_VOID;
  548. exit;
  549. end;
  550. { Constructors return self instead of a boolean }
  551. if p.proctypeoption=potype_constructor then
  552. begin
  553. retloc.def:=tdef(p.owner.defowner);
  554. if not (is_implicit_pointer_object_type(retloc.def) or
  555. (retloc.def.typ<>objectdef)) then
  556. retloc.def:=cpointerdef.getreusable_no_free(retloc.def);
  557. end;
  558. retcgsize:=def_cgsize(retloc.def);
  559. retloc.intsize:=retloc.def.size;
  560. retloc.size:=retcgsize;
  561. { Return is passed as var parameter }
  562. if ret_in_param(retloc.def,p) then
  563. begin
  564. retloc.def:=cpointerdef.getreusable_no_free(retloc.def);
  565. paraloc:=retloc.add_location;
  566. paraloc^.loc:=LOC_REFERENCE;
  567. paraloc^.size:=retcgsize;
  568. paraloc^.def:=retloc.def;
  569. exit;
  570. end;
  571. result:=false;
  572. end;
  573. function tparamanager.handle_common_ret_in_param(def: tdef;
  574. pd: tabstractprocdef; out retinparam: boolean): boolean;
  575. begin
  576. { This must be system independent: safecall and record constructor result
  577. is always returned in param.
  578. Furthermore, any managed type is returned in param, in order to avoid
  579. its finalization on exception at callee side. }
  580. if ((tf_safecall_exceptions in target_info.flags) and
  581. (pd.proccalloption=pocall_safecall)) or
  582. (
  583. (pd.proctypeoption=potype_constructor) and
  584. (
  585. is_record(def) or
  586. (
  587. (def.typ<>objectdef) and
  588. (pd.owner.symtabletype=objectsymtable) and
  589. is_objectpascal_helper(tdef(pd.owner.defowner))
  590. )
  591. )
  592. ) or is_managed_type(def) then
  593. begin
  594. retinparam:=true;
  595. exit(true);
  596. end;
  597. if pd.proctypeoption=potype_constructor then
  598. begin
  599. retinparam:=false;
  600. exit(true);
  601. end;
  602. result:=false;
  603. end;
  604. function tparamanager.get_paraloc_def(paradef: tdef; restlen: aint; fullsize: boolean): tdef;
  605. begin
  606. if fullsize then
  607. result:=paradef
  608. { no support for 128 bit ints -> tcgsize2orddef can't handle
  609. OS_(S)128 }
  610. else if restlen in [1,2,4,8] then
  611. result:=cgsize_orddef(int_cgsize(restlen))
  612. else
  613. result:=carraydef.getreusable_no_free(u8inttype,restlen);
  614. end;
  615. procedure tparamanager.getintparaloc(list: TAsmList; pd: tabstractprocdef; nr : longint; var cgpara: tcgpara);
  616. begin
  617. if (nr<1) or (nr>pd.paras.count) then
  618. InternalError(2013060101);
  619. pd.init_paraloc_info(callerside);
  620. cgpara:=tparavarsym(pd.paras[nr-1]).paraloc[callerside].getcopy;
  621. end;
  622. function tparamanager.cgparalocs_to_rttiparalocs(paralocs:pcgparalocation):trttiparalocs;
  623. var
  624. c : longint;
  625. tmploc : pcgparalocation;
  626. begin
  627. c:=0;
  628. tmploc:=paralocs;
  629. while assigned(tmploc) do
  630. begin
  631. inc(c);
  632. tmploc:=tmploc^.next;
  633. end;
  634. setlength(result,c);
  635. c:=0;
  636. tmploc:=paralocs;
  637. while assigned(tmploc) do
  638. begin
  639. result[c]:=cgparaloc_to_rttiparaloc(tmploc);
  640. inc(c);
  641. tmploc:=tmploc^.next;
  642. end;
  643. end;
  644. function tparamanager.cgparaloc_to_rttiparaloc(paraloc:pcgparalocation):trttiparaloc;
  645. var
  646. reg : tregisterrec;
  647. begin
  648. if paraloc^.Loc=LOC_REFERENCE then
  649. begin
  650. reg:=tregisterrec(paraloc^.reference.index);
  651. result.offset:=paraloc^.reference.offset;
  652. result.loctype:=$80;
  653. end
  654. else
  655. begin
  656. reg:=tregisterrec(paraloc^.register);
  657. { use sign extension }
  658. result.offset:=paraloc^.shiftval;
  659. result.loctype:=$00;
  660. end;
  661. case reg.regtype of
  662. R_INTREGISTER,
  663. R_FPUREGISTER,
  664. R_MMXREGISTER,
  665. R_MMREGISTER,
  666. R_SPECIALREGISTER,
  667. R_ADDRESSREGISTER:
  668. begin
  669. result.loctype:=result.loctype or ord(reg.regtype);
  670. result.regsub:=ord(reg.subreg);
  671. result.regindex:=reg.supreg;
  672. end;
  673. else
  674. begin
  675. { no need to adjust loctype }
  676. result.regsub:=0;
  677. result.regindex:=0;
  678. end;
  679. end;
  680. end;
  681. initialization
  682. ;
  683. finalization
  684. paramanager.free;
  685. end.