paramgr.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. cclasses,globtype,
  25. cpubase,cgbase,cgutils,
  26. parabase,
  27. aasmtai,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. function push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  40. function keep_para_array_range(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  41. { Returns true if a parameter is too large to copy and only
  42. the address is pushed
  43. }
  44. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;abstract;
  45. { returns true if a parameter must be handled via copy-out (construct
  46. a reference, copy the parameter's value there in case of copy-in/out, pass the reference)
  47. }
  48. function push_copyout_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  49. { return the size of a push }
  50. function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;virtual;
  51. {# Returns a structure giving the information on
  52. the storage of the parameter (which must be
  53. an integer parameter). This is only used when calling
  54. internal routines directly, where all parameters must
  55. be 4-byte values.
  56. In case the location is a register, this register is allocated.
  57. Call freeintparaloc() after the call to free the locations again.
  58. Default implementation: don't do anything at all (in case you don't
  59. use register parameter passing)
  60. @param(list Current assembler list)
  61. @param(nr Parameter number of routine, starting from 1)
  62. }
  63. function get_para_align(calloption : tproccalloption):byte;virtual;
  64. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;virtual;
  65. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;virtual;
  66. function get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;virtual;
  67. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;virtual;
  68. procedure getintparaloc(pd: tabstractprocdef; nr : longint; var cgpara: tcgpara);virtual;abstract;
  69. {# allocate an individual pcgparalocation that's part of a tcgpara
  70. @param(list Current assembler list)
  71. @param(loc Parameter location element)
  72. }
  73. procedure allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  74. {# allocate a parameter location created with create_paraloc_info
  75. @param(list Current assembler list)
  76. @param(loc Parameter location)
  77. }
  78. procedure alloccgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  79. {# free a parameter location allocated with alloccgpara
  80. @param(list Current assembler list)
  81. @param(loc Parameter location)
  82. }
  83. procedure freecgpara(list: TAsmList; const cgpara: TCGPara); virtual;
  84. { This is used to populate the location information on all parameters
  85. for the routine as seen in either the caller or the callee. It returns
  86. the size allocated on the stack
  87. }
  88. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;virtual;abstract;
  89. { Returns the location of the function result if p had def as
  90. function result instead of its actual result. Used if the compiler
  91. forces the function result to something different than the real
  92. result. }
  93. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;virtual;abstract;
  94. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  95. { This is used to populate the location information on all parameters
  96. for the routine when it is being inlined. It returns
  97. the size allocated on the stack
  98. }
  99. function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual;
  100. { This is used to populate the location information on all parameters
  101. for the routine that are passed as varargs. It returns
  102. the size allocated on the stack (including the normal parameters)
  103. }
  104. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;virtual;abstract;
  105. function is_stack_paraloc(paraloc: pcgparalocation): boolean;virtual;
  106. procedure createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);virtual;
  107. procedure duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  108. procedure duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  109. function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual;
  110. function parsefuncretloc(p : tabstractprocdef; const s : string) : boolean;virtual;
  111. { allocate room for parameters on the stack in the entry code? }
  112. function use_fixed_stack: boolean;
  113. { whether stack pointer can be changed in the middle of procedure }
  114. function use_stackalloc: boolean;
  115. strict protected
  116. { common part of get_funcretloc; returns true if retloc is completely
  117. initialized afterwards }
  118. function set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  119. { common part of ret_in_param; is called by ret_in_param at the
  120. beginning and every tparamanager descendant can decide to call it
  121. itself as well; parameter retinparam is only valid if function
  122. returns true }
  123. function handle_common_ret_in_param(def:tdef;pd:tabstractprocdef;out retinparam:boolean):boolean;
  124. end;
  125. var
  126. paramanager : tparamanager;
  127. implementation
  128. uses
  129. systems,
  130. cgobj,tgobj,
  131. defutil,verbose;
  132. { true if the location in paraloc can be reused as localloc }
  133. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  134. begin
  135. result:=false;
  136. end;
  137. { true if uses a parameter as return value }
  138. function tparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  139. begin
  140. if handle_common_ret_in_param(def,pd,result) then
  141. exit;
  142. ret_in_param:=((def.typ=arraydef) and not(is_dynamic_array(def))) or
  143. (def.typ=recorddef) or
  144. (def.typ=stringdef) or
  145. ((def.typ=procvardef) and not tprocvardef(def).is_addressonly) or
  146. { interfaces are also passed by reference to be compatible with delphi and COM }
  147. ((def.typ=objectdef) and (is_object(def) or is_interface(def) or is_dispinterface(def))) or
  148. (def.typ=variantdef) or
  149. ((def.typ=setdef) and not is_smallset(def));
  150. end;
  151. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  152. begin
  153. push_high_param:=not(calloption in cdecl_pocalls) and
  154. (
  155. is_open_array(def) or
  156. is_open_string(def) or
  157. is_array_of_const(def)
  158. );
  159. end;
  160. function tparamanager.keep_para_array_range(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  161. begin
  162. result:=push_high_param(varspez,def,calloption);
  163. end;
  164. function tparamanager.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  165. begin
  166. push_copyout_param:=false;
  167. end;
  168. { return the size of a push }
  169. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  170. begin
  171. push_size:=-1;
  172. case varspez of
  173. vs_constref,
  174. vs_out,
  175. vs_var :
  176. push_size:=sizeof(pint);
  177. vs_value,
  178. vs_const :
  179. begin
  180. if push_addr_param(varspez,def,calloption) then
  181. push_size:=sizeof(pint)
  182. else
  183. begin
  184. { special array are normally pushed by addr, only for
  185. cdecl array of const it comes here and the pushsize
  186. is unknown }
  187. if is_array_of_const(def) then
  188. push_size:=0
  189. else
  190. push_size:=def.size;
  191. end;
  192. end;
  193. end;
  194. end;
  195. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  196. begin
  197. result:=std_param_align;
  198. end;
  199. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  200. begin
  201. result:=[];
  202. end;
  203. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  204. begin
  205. result:=[];
  206. end;
  207. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  208. begin
  209. result:=[];
  210. end;
  211. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  212. begin
  213. result:=[];
  214. end;
  215. {$if first_mm_imreg = 0}
  216. {$WARN 4044 OFF} { Comparison might be always false ... }
  217. {$endif}
  218. procedure tparamanager.allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  219. begin
  220. case paraloc^.loc of
  221. LOC_REGISTER,
  222. LOC_CREGISTER:
  223. begin
  224. if getsupreg(paraloc^.register)<first_int_imreg then
  225. begin
  226. cg.getcpuregister(list,paraloc^.register);
  227. {$ifdef cpu16bitalu}
  228. if paraloc^.Size in [OS_32, OS_S32] then
  229. cg.getcpuregister(list,GetNextReg(paraloc^.register));
  230. {$endif cpu16bitalu}
  231. end;
  232. end;
  233. {$ifndef x86}
  234. { don't allocate ST(x), they're not handled by the register allocator }
  235. LOC_FPUREGISTER,
  236. LOC_CFPUREGISTER:
  237. begin
  238. if getsupreg(paraloc^.register)<first_fpu_imreg then
  239. cg.getcpuregister(list,paraloc^.register);
  240. end;
  241. {$endif not x86}
  242. LOC_MMREGISTER,
  243. LOC_CMMREGISTER :
  244. begin
  245. if getsupreg(paraloc^.register)<first_mm_imreg then
  246. cg.getcpuregister(list,paraloc^.register);
  247. end;
  248. end;
  249. end;
  250. procedure tparamanager.alloccgpara(list: TAsmList; const cgpara: TCGPara);
  251. var
  252. paraloc : pcgparalocation;
  253. begin
  254. paraloc:=cgpara.location;
  255. while assigned(paraloc) do
  256. begin
  257. allocparaloc(list,paraloc);
  258. paraloc:=paraloc^.next;
  259. end;
  260. end;
  261. procedure tparamanager.freecgpara(list: TAsmList; const cgpara: TCGPara);
  262. var
  263. paraloc : Pcgparalocation;
  264. href : treference;
  265. begin
  266. paraloc:=cgpara.location;
  267. while assigned(paraloc) do
  268. begin
  269. case paraloc^.loc of
  270. LOC_VOID:
  271. ;
  272. LOC_REGISTER,
  273. LOC_CREGISTER:
  274. begin
  275. if getsupreg(paraloc^.register)<first_int_imreg then
  276. begin
  277. cg.ungetcpuregister(list,paraloc^.register);
  278. {$ifdef cpu16bitalu}
  279. if paraloc^.Size in [OS_32, OS_S32] then
  280. cg.ungetcpuregister(list,GetNextReg(paraloc^.register));
  281. {$endif cpu16bitalu}
  282. end;
  283. end;
  284. LOC_FPUREGISTER,
  285. LOC_CFPUREGISTER:
  286. begin
  287. if getsupreg(paraloc^.register)<first_fpu_imreg then
  288. cg.ungetcpuregister(list,paraloc^.register);
  289. end;
  290. LOC_MMREGISTER,
  291. LOC_CMMREGISTER :
  292. begin
  293. if getsupreg(paraloc^.register)<first_mm_imreg then
  294. cg.ungetcpuregister(list,paraloc^.register);
  295. end;
  296. LOC_REFERENCE,
  297. LOC_CREFERENCE :
  298. begin
  299. if use_fixed_stack then
  300. begin
  301. { don't use reference_reset_base, because that will depend on cgobj }
  302. fillchar(href,sizeof(href),0);
  303. href.base:=paraloc^.reference.index;
  304. href.offset:=paraloc^.reference.offset;
  305. tg.ungetiftemp(list,href);
  306. end;
  307. end;
  308. else
  309. internalerror(2004110212);
  310. end;
  311. paraloc:=paraloc^.next;
  312. end;
  313. end;
  314. function tparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  315. begin
  316. result:=
  317. assigned(paraloc) and
  318. (paraloc^.loc=LOC_REFERENCE) and
  319. (paraloc^.reference.index=NR_STACK_POINTER_REG);
  320. end;
  321. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  322. var
  323. href : treference;
  324. len : aint;
  325. paraloc,
  326. newparaloc : pcgparalocation;
  327. begin
  328. paraloc:=parasym.paraloc[callerside].location;
  329. cgpara.reset;
  330. cgpara.size:=parasym.paraloc[callerside].size;
  331. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  332. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  333. cgpara.def:=parasym.paraloc[callerside].def;
  334. while assigned(paraloc) do
  335. begin
  336. if paraloc^.size=OS_NO then
  337. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  338. else
  339. len:=tcgsize2size[paraloc^.size];
  340. newparaloc:=cgpara.add_location;
  341. newparaloc^.size:=paraloc^.size;
  342. newparaloc^.shiftval:=paraloc^.shiftval;
  343. { $warning maybe release this optimization for all targets? }
  344. { released for all CPUs:
  345. i386 isn't affected anyways because it uses the stack to push parameters
  346. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  347. { Does it fit a register? }
  348. if ((not can_use_final_stack_loc and
  349. use_fixed_stack) or
  350. not is_stack_paraloc(paraloc)) and
  351. (len<=sizeof(pint)) and
  352. (paraloc^.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  353. newparaloc^.loc:=LOC_REGISTER
  354. else
  355. newparaloc^.loc:=paraloc^.loc;
  356. case newparaloc^.loc of
  357. LOC_REGISTER :
  358. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  359. LOC_FPUREGISTER :
  360. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  361. LOC_MMREGISTER :
  362. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  363. LOC_REFERENCE :
  364. begin
  365. if (can_use_final_stack_loc or
  366. not use_fixed_stack) and
  367. is_stack_paraloc(paraloc) then
  368. duplicatecgparaloc(paraloc,newparaloc)
  369. else
  370. begin
  371. if assigned(cgpara.def) then
  372. tg.gethltemp(list,cgpara.def,len,tt_persistent,href)
  373. else
  374. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  375. newparaloc^.reference.index:=href.base;
  376. newparaloc^.reference.offset:=href.offset;
  377. end;
  378. end;
  379. end;
  380. paraloc:=paraloc^.next;
  381. end;
  382. end;
  383. procedure tparamanager.duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  384. begin
  385. move(orgparaloc^,intonewparaloc^,sizeof(intonewparaloc^));
  386. intonewparaloc^.next:=nil;
  387. end;
  388. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  389. var
  390. paraloc,
  391. newparaloc : pcgparalocation;
  392. begin
  393. cgpara.reset;
  394. cgpara.size:=parasym.paraloc[callerside].size;
  395. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  396. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  397. paraloc:=parasym.paraloc[callerside].location;
  398. while assigned(paraloc) do
  399. begin
  400. newparaloc:=cgpara.add_location;
  401. duplicatecgparaloc(paraloc,newparaloc);
  402. paraloc:=paraloc^.next;
  403. end;
  404. end;
  405. procedure tparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  406. begin
  407. p.funcretloc[side]:=get_funcretloc(p,side,nil);
  408. end;
  409. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  410. begin
  411. { We need to return the size allocated }
  412. p.init_paraloc_info(callbothsides);
  413. result:=p.calleeargareasize;
  414. end;
  415. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  416. begin
  417. Result:=False;
  418. internalerror(200807235);
  419. end;
  420. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  421. begin
  422. Result:=False;
  423. internalerror(200807236);
  424. end;
  425. function tparamanager.use_fixed_stack: boolean;
  426. begin
  427. {$ifdef i386}
  428. result := target_info.stackalign > 4;
  429. {$else i386}
  430. {$ifdef cputargethasfixedstack}
  431. result := true;
  432. {$else cputargethasfixedstack}
  433. result := false;
  434. {$endif cputargethasfixedstack}
  435. {$endif i386}
  436. end;
  437. { This is a separate function because at least win64 allows stack allocations
  438. despite of fixed stack semantics (actually supporting it requires generating
  439. a compliant stack frame, not yet possible) }
  440. function tparamanager.use_stackalloc: boolean;
  441. begin
  442. result:=not use_fixed_stack;
  443. end;
  444. function tparamanager.set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  445. var
  446. paraloc : pcgparalocation;
  447. begin
  448. result:=true;
  449. retloc.init;
  450. if not assigned(forcetempdef) then
  451. retloc.def:=p.returndef
  452. else
  453. begin
  454. retloc.def:=forcetempdef;
  455. retloc.temporary:=true;
  456. end;
  457. retloc.alignment:=get_para_align(p.proccalloption);
  458. { void has no location }
  459. if is_void(retloc.def) then
  460. begin
  461. paraloc:=retloc.add_location;
  462. retloc.size:=OS_NO;
  463. retcgsize:=OS_NO;
  464. retloc.intsize:=0;
  465. paraloc^.size:=OS_NO;
  466. paraloc^.loc:=LOC_VOID;
  467. exit;
  468. end;
  469. { Constructors return self instead of a boolean }
  470. if p.proctypeoption=potype_constructor then
  471. begin
  472. retloc.def:=tdef(p.owner.defowner);
  473. if not (is_implicit_pointer_object_type(retloc.def) or
  474. (retloc.def.typ<>objectdef)) then
  475. retloc.def:=getpointerdef(retloc.def);
  476. end;
  477. retcgsize:=def_cgsize(retloc.def);
  478. retloc.intsize:=retloc.def.size;
  479. retloc.size:=retcgsize;
  480. { Return is passed as var parameter }
  481. if ret_in_param(retloc.def,p) then
  482. begin
  483. retloc.def:=getpointerdef(retloc.def);
  484. paraloc:=retloc.add_location;
  485. paraloc^.loc:=LOC_REFERENCE;
  486. paraloc^.size:=retcgsize;
  487. exit;
  488. end;
  489. result:=false;
  490. end;
  491. function tparamanager.handle_common_ret_in_param(def: tdef;
  492. pd: tabstractprocdef; out retinparam: boolean): boolean;
  493. begin
  494. { this must be system independent safecall and record constructor result
  495. is always return in param }
  496. if (tf_safecall_exceptions in target_info.flags) and
  497. (pd.proccalloption=pocall_safecall) or
  498. (
  499. (pd.proctypeoption=potype_constructor)and
  500. (
  501. is_record(def) or
  502. (
  503. (def.typ<>objectdef) and
  504. (pd.owner.symtabletype=objectsymtable) and
  505. is_objectpascal_helper(tdef(pd.owner.defowner))
  506. )
  507. )
  508. ) then
  509. begin
  510. retinparam:=true;
  511. exit(true);
  512. end;
  513. if pd.proctypeoption=potype_constructor then
  514. begin
  515. retinparam:=false;
  516. exit(true);
  517. end;
  518. result:=false;
  519. end;
  520. initialization
  521. ;
  522. finalization
  523. paramanager.free;
  524. end.