paramgr.pas 20 KB

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