paramgr.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. Generic calling convention handling
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# Parameter passing manager. Used to manage how
  18. parameters are passed to routines.
  19. }
  20. unit paramgr;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. cpubase,cgbase,cgutils,
  26. parabase,
  27. aasmdata,
  28. symconst,symtype,symsym,symdef;
  29. type
  30. {# This class defines some methods to take care of routine
  31. parameters. It should be overridden for each new processor
  32. }
  33. { tparamanager }
  34. tparamanager = class
  35. { true if the location in paraloc can be reused as localloc }
  36. function param_use_paraloc(const cgpara:tcgpara):boolean;virtual;
  37. { Returns true if the return value is actually a parameter pointer }
  38. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;virtual;
  39. { Returns true if a result variable should be allocated for an assembler routine }
  40. function asm_result_var(def:tdef;pd:tabstractprocdef):boolean;virtual;
  41. function push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  42. function keep_para_array_range(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  43. { Returns true if a parameter is too large to copy and only
  44. the address is pushed
  45. }
  46. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;abstract;
  47. { returns true if a parameter must be handled via copy-out (construct
  48. a reference, copy the parameter's value there in case of copy-in/out, pass the reference)
  49. }
  50. function push_copyout_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  51. { return the size of a push }
  52. function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;virtual;
  53. {# Returns a structure giving the information on
  54. the storage of the parameter (which must be
  55. an integer parameter). This is only used when calling
  56. internal routines directly, where all parameters must
  57. be 4-byte values.
  58. In case the location is a register, this register is allocated.
  59. Call freeintparaloc() after the call to free the locations again.
  60. Default implementation: don't do anything at all (in case you don't
  61. use register parameter passing)
  62. @param(list Current assembler list)
  63. @param(nr Parameter number of routine, starting from 1)
  64. }
  65. function get_para_align(calloption : tproccalloption):byte;virtual;
  66. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;virtual;
  67. function get_volatile_registers_address(calloption : tproccalloption):tcpuregisterset;virtual;
  68. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;virtual;
  69. function get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;virtual;
  70. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;virtual;
  71. {# Registers which must be saved when calling a routine declared as
  72. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  73. saved should be the ones as defined in the target ABI and / or GCC.
  74. This value can be deduced from the CALLED_USED_REGISTERS array in the
  75. GCC source.
  76. }
  77. function get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;virtual;
  78. function get_saved_registers_address(calloption : tproccalloption):tcpuregisterarray;virtual;
  79. function get_saved_registers_fpu(calloption : tproccalloption):tcpuregisterarray;virtual;
  80. function get_saved_registers_mm(calloption : tproccalloption):tcpuregisterarray;virtual;
  81. procedure getintparaloc(list: TAsmList; pd: tabstractprocdef; nr : longint; var cgpara: tcgpara);virtual;
  82. {# allocate an individual pcgparalocation that's part of a tcgpara
  83. @param(list Current assembler list)
  84. @param(loc Parameter location element)
  85. }
  86. procedure allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  87. {# allocate a parameter location created with create_paraloc_info
  88. @param(list Current assembler list)
  89. @param(loc Parameter location)
  90. }
  91. procedure alloccgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  92. {# free a parameter location allocated with alloccgpara
  93. @param(list Current assembler list)
  94. @param(loc Parameter location)
  95. }
  96. procedure freecgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  97. { This is used to populate the location information on all parameters
  98. for the routine as seen in either the caller or the callee. It returns
  99. the size allocated on the stack
  100. }
  101. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;virtual;abstract;
  102. { Returns the location of the function result if p had def as
  103. function result instead of its actual result. Used if the compiler
  104. forces the function result to something different than the real
  105. result. }
  106. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;virtual;abstract;
  107. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  108. { This is used to populate the location information on all parameters
  109. for the routine when it is being inlined. It returns
  110. the size allocated on the stack
  111. }
  112. function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual;
  113. { This is used to populate the location information on all parameters
  114. for the routine that are passed as varargs. It returns
  115. the size allocated on the stack (including the normal parameters)
  116. }
  117. function create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; 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. else
  225. ;
  226. end;
  227. end;
  228. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  229. begin
  230. result:=std_param_align;
  231. end;
  232. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  233. begin
  234. result:=[];
  235. end;
  236. function tparamanager.get_volatile_registers_address(calloption : tproccalloption):tcpuregisterset;
  237. begin
  238. result:=[];
  239. end;
  240. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  241. begin
  242. result:=[];
  243. end;
  244. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  245. begin
  246. result:=[];
  247. end;
  248. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  249. begin
  250. result:=[];
  251. end;
  252. function tparamanager.get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;
  253. const
  254. inv: {$ifndef VER3_0}tcpuregisterarray{$else}array [0..0] of tsuperregister{$endif} = (RS_INVALID);
  255. begin
  256. result:=inv;
  257. end;
  258. function tparamanager.get_saved_registers_address(calloption : tproccalloption):tcpuregisterarray;
  259. const
  260. inv: {$ifndef VER3_0}tcpuregisterarray{$else}array [0..0] of tsuperregister{$endif} = (RS_INVALID);
  261. begin
  262. result:=inv;
  263. end;
  264. function tparamanager.get_saved_registers_fpu(calloption : tproccalloption):tcpuregisterarray;
  265. const
  266. inv: {$ifndef VER3_0}tcpuregisterarray{$else}array [0..0] of tsuperregister{$endif} = (RS_INVALID);
  267. begin
  268. result:=inv;
  269. end;
  270. function tparamanager.get_saved_registers_mm(calloption : tproccalloption):tcpuregisterarray;
  271. const
  272. inv: {$ifndef VER3_0}tcpuregisterarray{$else}array [0..0] of tsuperregister{$endif} = (RS_INVALID);
  273. begin
  274. result:=inv;
  275. end;
  276. {$if first_mm_imreg = 0}
  277. {$WARN 4044 OFF} { Comparison might be always false ... }
  278. {$endif}
  279. procedure tparamanager.allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  280. begin
  281. case paraloc^.loc of
  282. LOC_REGISTER,
  283. LOC_CREGISTER:
  284. begin
  285. if getsupreg(paraloc^.register)<first_int_imreg then
  286. cg.getcpuregister(list,paraloc^.register);
  287. end;
  288. {$ifndef x86}
  289. { don't allocate ST(x), they're not handled by the register allocator }
  290. LOC_FPUREGISTER,
  291. LOC_CFPUREGISTER:
  292. begin
  293. if getsupreg(paraloc^.register)<first_fpu_imreg then
  294. cg.getcpuregister(list,paraloc^.register);
  295. end;
  296. {$endif not x86}
  297. LOC_MMREGISTER,
  298. LOC_CMMREGISTER :
  299. begin
  300. if getsupreg(paraloc^.register)<first_mm_imreg then
  301. cg.getcpuregister(list,paraloc^.register);
  302. end;
  303. else
  304. ;
  305. end;
  306. end;
  307. procedure tparamanager.alloccgpara(list: TAsmList; const cgpara: TCGPara);
  308. var
  309. paraloc : pcgparalocation;
  310. begin
  311. paraloc:=cgpara.location;
  312. while assigned(paraloc) do
  313. begin
  314. allocparaloc(list,paraloc);
  315. paraloc:=paraloc^.next;
  316. end;
  317. end;
  318. procedure tparamanager.freecgpara(list: TAsmList; const cgpara: TCGPara);
  319. var
  320. paraloc : Pcgparalocation;
  321. href : treference;
  322. begin
  323. paraloc:=cgpara.location;
  324. while assigned(paraloc) do
  325. begin
  326. case paraloc^.loc of
  327. LOC_VOID:
  328. ;
  329. LOC_REGISTER,
  330. LOC_CREGISTER:
  331. begin
  332. if getsupreg(paraloc^.register)<first_int_imreg then
  333. cg.ungetcpuregister(list,paraloc^.register);
  334. end;
  335. LOC_FPUREGISTER,
  336. LOC_CFPUREGISTER:
  337. begin
  338. if getsupreg(paraloc^.register)<first_fpu_imreg then
  339. cg.ungetcpuregister(list,paraloc^.register);
  340. end;
  341. LOC_MMREGISTER,
  342. LOC_CMMREGISTER :
  343. begin
  344. if getsupreg(paraloc^.register)<first_mm_imreg then
  345. cg.ungetcpuregister(list,paraloc^.register);
  346. end;
  347. LOC_REFERENCE,
  348. LOC_CREFERENCE :
  349. begin
  350. if use_fixed_stack then
  351. begin
  352. { don't use reference_reset_base, because that will depend on cgobj }
  353. fillchar(href,sizeof(href),0);
  354. href.base:=paraloc^.reference.index;
  355. href.offset:=paraloc^.reference.offset;
  356. href.temppos:=ctempposinvalid;
  357. tg.ungetiftemp(list,href);
  358. end;
  359. end;
  360. else
  361. internalerror(2004110212);
  362. end;
  363. paraloc:=paraloc^.next;
  364. end;
  365. end;
  366. function tparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  367. begin
  368. result:=
  369. assigned(paraloc) and
  370. (paraloc^.loc=LOC_REFERENCE) and
  371. (paraloc^.reference.index=NR_STACK_POINTER_REG);
  372. end;
  373. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  374. var
  375. href : treference;
  376. len : aint;
  377. paraloc,
  378. newparaloc : pcgparalocation;
  379. begin
  380. paraloc:=parasym.paraloc[callerside].location;
  381. cgpara.reset;
  382. cgpara.size:=parasym.paraloc[callerside].size;
  383. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  384. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  385. cgpara.def:=parasym.paraloc[callerside].def;
  386. while assigned(paraloc) do
  387. begin
  388. if paraloc^.size=OS_NO then
  389. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  390. else
  391. len:=tcgsize2size[paraloc^.size];
  392. newparaloc:=cgpara.add_location;
  393. newparaloc^.size:=paraloc^.size;
  394. newparaloc^.def:=paraloc^.def;
  395. { shiftval overlaps with part of the reference, so it may be
  396. different from 0 and if wr then force the newparaloc to a register
  397. in the optimization below, shiftval will remain <> 0 }
  398. if not(paraloc^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  399. newparaloc^.shiftval:=paraloc^.shiftval;
  400. { $warning maybe release this optimization for all targets? }
  401. { released for all CPUs:
  402. i386 isn't affected anyways because it uses the stack to push parameters
  403. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  404. { Does it fit a register? }
  405. if ((not can_use_final_stack_loc and
  406. use_fixed_stack) or
  407. not is_stack_paraloc(paraloc)) and
  408. (len<=sizeof(pint)) and
  409. (paraloc^.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  410. newparaloc^.loc:=LOC_REGISTER
  411. else
  412. newparaloc^.loc:=paraloc^.loc;
  413. case newparaloc^.loc of
  414. LOC_REGISTER :
  415. begin
  416. if (vo_has_explicit_paraloc in parasym.varoptions) and (paraloc^.loc = LOC_REGISTER) then
  417. if getregtype(paraloc^.register) = R_ADDRESSREGISTER then
  418. newparaloc^.register:=cg.getaddressregister(list)
  419. else
  420. newparaloc^.register:=cg.getintregister(list,paraloc^.size)
  421. else
  422. begin
  423. {$ifdef cpu_uses_separate_address_registers}
  424. if hlcg.def2regtyp(paraloc^.def) = R_ADDRESSREGISTER then
  425. newparaloc^.register:=hlcg.getaddressregister(list,paraloc^.def)
  426. else
  427. {$endif}
  428. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  429. end;
  430. end;
  431. LOC_FPUREGISTER :
  432. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  433. LOC_MMREGISTER :
  434. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  435. LOC_REFERENCE :
  436. begin
  437. if (can_use_final_stack_loc or
  438. not use_fixed_stack) and
  439. is_stack_paraloc(paraloc) then
  440. duplicatecgparaloc(paraloc,newparaloc)
  441. else
  442. begin
  443. if assigned(cgpara.def) then
  444. tg.gethltemp(list,cgpara.def,len,tt_persistent,href)
  445. else
  446. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  447. newparaloc^.reference.index:=href.base;
  448. newparaloc^.reference.offset:=href.offset;
  449. end;
  450. end;
  451. else
  452. ;
  453. end;
  454. paraloc:=paraloc^.next;
  455. end;
  456. end;
  457. procedure tparamanager.duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  458. begin
  459. move(orgparaloc^,intonewparaloc^,sizeof(intonewparaloc^));
  460. intonewparaloc^.next:=nil;
  461. end;
  462. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  463. var
  464. paraloc,
  465. newparaloc : pcgparalocation;
  466. begin
  467. cgpara.reset;
  468. cgpara.size:=parasym.paraloc[callerside].size;
  469. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  470. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  471. paraloc:=parasym.paraloc[callerside].location;
  472. while assigned(paraloc) do
  473. begin
  474. newparaloc:=cgpara.add_location;
  475. duplicatecgparaloc(paraloc,newparaloc);
  476. paraloc:=paraloc^.next;
  477. end;
  478. end;
  479. procedure tparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  480. begin
  481. if not assigned(p.funcretloc[side].Location) then
  482. p.funcretloc[side]:=get_funcretloc(p,side,nil);
  483. end;
  484. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  485. begin
  486. { We need to return the size allocated }
  487. p.init_paraloc_info(callbothsides);
  488. result:=p.calleeargareasize;
  489. end;
  490. { used by syscall conventions which require explicit paralocation support }
  491. { this is the standard implementation, CGs might overwrite it }
  492. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  493. var
  494. paraloc : pcgparalocation;
  495. begin
  496. parasym.paraloc[callerside].alignment:=sizeof(pint);
  497. paraloc:=parasym.paraloc[callerside].add_location;
  498. paraloc^.loc:=LOC_REGISTER;
  499. paraloc^.size:=def_cgsize(parasym.vardef);
  500. paraloc^.def:=parasym.vardef;
  501. paraloc^.register:=std_regnum_search(lowercase(s));
  502. { copy to callee side }
  503. parasym.paraloc[calleeside].add_location^:=paraloc^;
  504. result:=(paraloc^.register <> NR_NO) and
  505. (paraloc^.register <> NR_STACK_POINTER_REG);
  506. end;
  507. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  508. begin
  509. Result:=False;
  510. internalerror(200807236);
  511. end;
  512. function tparamanager.use_fixed_stack: boolean;
  513. begin
  514. {$ifdef i386}
  515. result := target_info.stackalign > 4;
  516. {$else i386}
  517. {$ifdef cputargethasfixedstack}
  518. result := true;
  519. {$else cputargethasfixedstack}
  520. result := false;
  521. {$endif cputargethasfixedstack}
  522. {$endif i386}
  523. end;
  524. { This is a separate function because at least win64 allows stack allocations
  525. despite of fixed stack semantics (actually supporting it requires generating
  526. a compliant stack frame, not yet possible) }
  527. function tparamanager.use_stackalloc: boolean;
  528. begin
  529. result:=not use_fixed_stack;
  530. end;
  531. function tparamanager.set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  532. var
  533. paraloc : pcgparalocation;
  534. begin
  535. result:=true;
  536. retloc.init;
  537. if not assigned(forcetempdef) then
  538. retloc.def:=p.returndef
  539. else
  540. begin
  541. retloc.def:=forcetempdef;
  542. retloc.temporary:=true;
  543. end;
  544. retloc.alignment:=get_para_align(p.proccalloption);
  545. { void has no location }
  546. if is_void(retloc.def) then
  547. begin
  548. paraloc:=retloc.add_location;
  549. retloc.size:=OS_NO;
  550. retcgsize:=OS_NO;
  551. retloc.intsize:=0;
  552. paraloc^.def:=retloc.def;
  553. paraloc^.size:=OS_NO;
  554. paraloc^.loc:=LOC_VOID;
  555. exit;
  556. end;
  557. { Constructors return self instead of a boolean }
  558. if p.proctypeoption=potype_constructor then
  559. begin
  560. retloc.def:=tdef(p.owner.defowner);
  561. if not (is_implicit_pointer_object_type(retloc.def) or
  562. (retloc.def.typ<>objectdef)) then
  563. retloc.def:=cpointerdef.getreusable_no_free(retloc.def);
  564. end;
  565. retcgsize:=def_cgsize(retloc.def);
  566. retloc.intsize:=retloc.def.size;
  567. retloc.size:=retcgsize;
  568. { Return is passed as var parameter }
  569. if ret_in_param(retloc.def,p) then
  570. begin
  571. retloc.def:=cpointerdef.getreusable_no_free(retloc.def);
  572. paraloc:=retloc.add_location;
  573. paraloc^.loc:=LOC_REFERENCE;
  574. paraloc^.size:=retcgsize;
  575. paraloc^.def:=retloc.def;
  576. exit;
  577. end;
  578. result:=false;
  579. end;
  580. function tparamanager.handle_common_ret_in_param(def: tdef;
  581. pd: tabstractprocdef; out retinparam: boolean): boolean;
  582. begin
  583. { This must be system independent: safecall and record constructor result
  584. is always returned in param.
  585. Furthermore, any managed type is returned in param, in order to avoid
  586. its finalization on exception at callee side. }
  587. if ((tf_safecall_exceptions in target_info.flags) and
  588. (pd.proccalloption=pocall_safecall)) or
  589. (
  590. (pd.proctypeoption=potype_constructor) and
  591. (
  592. is_record(def) or
  593. (
  594. (def.typ<>objectdef) and
  595. (pd.owner.symtabletype=objectsymtable) and
  596. is_objectpascal_helper(tdef(pd.owner.defowner))
  597. )
  598. )
  599. ) or is_managed_type(def) then
  600. begin
  601. retinparam:=true;
  602. exit(true);
  603. end;
  604. if pd.proctypeoption=potype_constructor then
  605. begin
  606. retinparam:=false;
  607. exit(true);
  608. end;
  609. result:=false;
  610. end;
  611. function tparamanager.get_paraloc_def(paradef: tdef; restlen: aint; fullsize: boolean): tdef;
  612. begin
  613. if fullsize then
  614. result:=paradef
  615. { no support for 128 bit ints -> tcgsize2orddef can't handle
  616. OS_(S)128 }
  617. else if restlen in [1,2,4,8] then
  618. result:=cgsize_orddef(int_cgsize(restlen))
  619. else
  620. result:=carraydef.getreusable_no_free(u8inttype,restlen);
  621. end;
  622. procedure tparamanager.getintparaloc(list: TAsmList; pd: tabstractprocdef; nr : longint; var cgpara: tcgpara);
  623. begin
  624. if (nr<1) or (nr>pd.paras.count) then
  625. InternalError(2013060101);
  626. pd.init_paraloc_info(callerside);
  627. cgpara:=tparavarsym(pd.paras[nr-1]).paraloc[callerside].getcopy;
  628. end;
  629. function tparamanager.cgparalocs_to_rttiparalocs(paralocs:pcgparalocation):trttiparalocs;
  630. var
  631. c : longint;
  632. tmploc : pcgparalocation;
  633. begin
  634. c:=0;
  635. tmploc:=paralocs;
  636. result:=nil;
  637. while assigned(tmploc) do
  638. begin
  639. inc(c);
  640. tmploc:=tmploc^.next;
  641. end;
  642. setlength(result,c);
  643. c:=0;
  644. tmploc:=paralocs;
  645. while assigned(tmploc) do
  646. begin
  647. result[c]:=cgparaloc_to_rttiparaloc(tmploc);
  648. inc(c);
  649. tmploc:=tmploc^.next;
  650. end;
  651. end;
  652. function tparamanager.cgparaloc_to_rttiparaloc(paraloc:pcgparalocation):trttiparaloc;
  653. var
  654. reg : tregisterrec;
  655. begin
  656. if paraloc^.Loc=LOC_REFERENCE then
  657. begin
  658. reg:=tregisterrec(paraloc^.reference.index);
  659. result.offset:=paraloc^.reference.offset;
  660. result.loctype:=$80;
  661. end
  662. else
  663. begin
  664. reg:=tregisterrec(paraloc^.register);
  665. { use sign extension }
  666. result.offset:=paraloc^.shiftval;
  667. result.loctype:=$00;
  668. end;
  669. case reg.regtype of
  670. R_INTREGISTER,
  671. R_FPUREGISTER,
  672. R_MMXREGISTER,
  673. R_MMREGISTER,
  674. R_SPECIALREGISTER,
  675. R_ADDRESSREGISTER:
  676. begin
  677. result.loctype:=result.loctype or ord(reg.regtype);
  678. result.regsub:=ord(reg.subreg);
  679. result.regindex:=reg.supreg;
  680. end;
  681. else
  682. begin
  683. { no need to adjust loctype }
  684. result.regsub:=0;
  685. result.regindex:=0;
  686. end;
  687. end;
  688. end;
  689. initialization
  690. ;
  691. finalization
  692. paramanager.free;
  693. end.