2
0

paramgr.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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; forcetempdef: tdef): tcgpara;virtual;abstract;
  95. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  96. { This is used to populate the location information on all parameters
  97. for the routine when it is being inlined. It returns
  98. the size allocated on the stack
  99. }
  100. function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual;
  101. { This is used to populate the location information on all parameters
  102. for the routine that are passed as varargs. It returns
  103. the size allocated on the stack (including the normal parameters)
  104. }
  105. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;virtual;abstract;
  106. function is_stack_paraloc(paraloc: pcgparalocation): boolean;virtual;
  107. procedure createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);virtual;
  108. procedure duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  109. procedure duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  110. function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual;
  111. function parsefuncretloc(p : tabstractprocdef; const s : string) : boolean;virtual;
  112. { allocate room for parameters on the stack in the entry code? }
  113. function use_fixed_stack: boolean;
  114. { whether stack pointer can be changed in the middle of procedure }
  115. function use_stackalloc: boolean;
  116. strict protected
  117. { common part of get_funcretloc; returns true if retloc is completely
  118. initialized afterwards }
  119. function set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  120. end;
  121. var
  122. paramanager : tparamanager;
  123. implementation
  124. uses
  125. systems,
  126. cgobj,tgobj,
  127. defutil,verbose;
  128. { true if the location in paraloc can be reused as localloc }
  129. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  130. begin
  131. result:=false;
  132. end;
  133. { true if uses a parameter as return value }
  134. function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  135. begin
  136. if (tf_safecall_exceptions in target_info.flags) and
  137. (calloption=pocall_safecall) then
  138. begin
  139. result:=true;
  140. exit;
  141. end;
  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.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  161. begin
  162. push_copyout_param:=false;
  163. end;
  164. { return the size of a push }
  165. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  166. begin
  167. push_size:=-1;
  168. case varspez of
  169. vs_constref,
  170. vs_out,
  171. vs_var :
  172. push_size:=sizeof(pint);
  173. vs_value,
  174. vs_const :
  175. begin
  176. if push_addr_param(varspez,def,calloption) then
  177. push_size:=sizeof(pint)
  178. else
  179. begin
  180. { special array are normally pushed by addr, only for
  181. cdecl array of const it comes here and the pushsize
  182. is unknown }
  183. if is_array_of_const(def) then
  184. push_size:=0
  185. else
  186. push_size:=def.size;
  187. end;
  188. end;
  189. end;
  190. end;
  191. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  192. begin
  193. result:=std_param_align;
  194. end;
  195. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  196. begin
  197. result:=[];
  198. end;
  199. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  200. begin
  201. result:=[];
  202. end;
  203. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  204. begin
  205. result:=[];
  206. end;
  207. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  208. begin
  209. result:=[];
  210. end;
  211. {$if first_mm_imreg = 0}
  212. {$WARN 4044 OFF} { Comparison might be always false ... }
  213. {$endif}
  214. procedure tparamanager.allocparaloc(list: TAsmList; const paraloc: pcgparalocation);
  215. begin
  216. case paraloc^.loc of
  217. LOC_REGISTER,
  218. LOC_CREGISTER:
  219. begin
  220. if getsupreg(paraloc^.register)<first_int_imreg then
  221. cg.getcpuregister(list,paraloc^.register);
  222. end;
  223. {$ifndef x86}
  224. { don't allocate ST(x), they're not handled by the register allocator }
  225. LOC_FPUREGISTER,
  226. LOC_CFPUREGISTER:
  227. begin
  228. if getsupreg(paraloc^.register)<first_fpu_imreg then
  229. cg.getcpuregister(list,paraloc^.register);
  230. end;
  231. {$endif not x86}
  232. LOC_MMREGISTER,
  233. LOC_CMMREGISTER :
  234. begin
  235. if getsupreg(paraloc^.register)<first_mm_imreg then
  236. cg.getcpuregister(list,paraloc^.register);
  237. end;
  238. end;
  239. end;
  240. procedure tparamanager.alloccgpara(list: TAsmList; const cgpara: TCGPara);
  241. var
  242. paraloc : pcgparalocation;
  243. begin
  244. paraloc:=cgpara.location;
  245. while assigned(paraloc) do
  246. begin
  247. allocparaloc(list,paraloc);
  248. paraloc:=paraloc^.next;
  249. end;
  250. end;
  251. procedure tparamanager.freecgpara(list: TAsmList; const cgpara: TCGPara);
  252. var
  253. paraloc : Pcgparalocation;
  254. href : treference;
  255. begin
  256. paraloc:=cgpara.location;
  257. while assigned(paraloc) do
  258. begin
  259. case paraloc^.loc of
  260. LOC_VOID:
  261. ;
  262. LOC_REGISTER,
  263. LOC_CREGISTER:
  264. begin
  265. if getsupreg(paraloc^.register)<first_int_imreg then
  266. cg.ungetcpuregister(list,paraloc^.register);
  267. end;
  268. LOC_FPUREGISTER,
  269. LOC_CFPUREGISTER:
  270. begin
  271. if getsupreg(paraloc^.register)<first_fpu_imreg then
  272. cg.ungetcpuregister(list,paraloc^.register);
  273. end;
  274. LOC_MMREGISTER,
  275. LOC_CMMREGISTER :
  276. begin
  277. if getsupreg(paraloc^.register)<first_mm_imreg then
  278. cg.ungetcpuregister(list,paraloc^.register);
  279. end;
  280. LOC_REFERENCE,
  281. LOC_CREFERENCE :
  282. begin
  283. if use_fixed_stack then
  284. begin
  285. { don't use reference_reset_base, because that will depend on cgobj }
  286. fillchar(href,sizeof(href),0);
  287. href.base:=paraloc^.reference.index;
  288. href.offset:=paraloc^.reference.offset;
  289. tg.ungetiftemp(list,href);
  290. end;
  291. end;
  292. else
  293. internalerror(2004110212);
  294. end;
  295. paraloc:=paraloc^.next;
  296. end;
  297. end;
  298. function tparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  299. begin
  300. result:=
  301. assigned(paraloc) and
  302. (paraloc^.loc=LOC_REFERENCE) and
  303. (paraloc^.reference.index=NR_STACK_POINTER_REG);
  304. end;
  305. procedure tparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  306. var
  307. href : treference;
  308. len : aint;
  309. paraloc,
  310. newparaloc : pcgparalocation;
  311. begin
  312. paraloc:=parasym.paraloc[callerside].location;
  313. cgpara.reset;
  314. cgpara.size:=parasym.paraloc[callerside].size;
  315. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  316. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  317. cgpara.def:=parasym.paraloc[callerside].def;
  318. {$ifdef powerpc}
  319. cgpara.composite:=parasym.paraloc[callerside].composite;
  320. {$endif powerpc}
  321. while assigned(paraloc) do
  322. begin
  323. if paraloc^.size=OS_NO then
  324. len:=push_size(parasym.varspez,parasym.vardef,calloption)
  325. else
  326. len:=tcgsize2size[paraloc^.size];
  327. newparaloc:=cgpara.add_location;
  328. newparaloc^.size:=paraloc^.size;
  329. newparaloc^.shiftval:=paraloc^.shiftval;
  330. { $warning maybe release this optimization for all targets? }
  331. { released for all CPUs:
  332. i386 isn't affected anyways because it uses the stack to push parameters
  333. on arm it reduces executable size of the compiler by 2.1 per cent (FK) }
  334. { Does it fit a register? }
  335. if ((not can_use_final_stack_loc and
  336. use_fixed_stack) or
  337. not is_stack_paraloc(paraloc)) and
  338. (len<=sizeof(pint)) and
  339. (paraloc^.size in [OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128]) then
  340. newparaloc^.loc:=LOC_REGISTER
  341. else
  342. newparaloc^.loc:=paraloc^.loc;
  343. case newparaloc^.loc of
  344. LOC_REGISTER :
  345. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  346. LOC_FPUREGISTER :
  347. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  348. LOC_MMREGISTER :
  349. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  350. LOC_REFERENCE :
  351. begin
  352. if (can_use_final_stack_loc or
  353. not use_fixed_stack) and
  354. is_stack_paraloc(paraloc) then
  355. duplicatecgparaloc(paraloc,newparaloc)
  356. else
  357. begin
  358. if assigned(cgpara.def) then
  359. tg.gethltemp(list,cgpara.def,len,tt_persistent,href)
  360. else
  361. tg.gettemp(list,len,cgpara.alignment,tt_persistent,href);
  362. newparaloc^.reference.index:=href.base;
  363. newparaloc^.reference.offset:=href.offset;
  364. end;
  365. end;
  366. end;
  367. paraloc:=paraloc^.next;
  368. end;
  369. end;
  370. procedure tparamanager.duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation);
  371. begin
  372. move(orgparaloc^,intonewparaloc^,sizeof(intonewparaloc^));
  373. intonewparaloc^.next:=nil;
  374. end;
  375. procedure tparamanager.duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  376. var
  377. paraloc,
  378. newparaloc : pcgparalocation;
  379. begin
  380. cgpara.reset;
  381. cgpara.size:=parasym.paraloc[callerside].size;
  382. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  383. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  384. {$ifdef powerpc}
  385. cgpara.composite:=parasym.paraloc[callerside].composite;
  386. {$endif powerpc}
  387. paraloc:=parasym.paraloc[callerside].location;
  388. while assigned(paraloc) do
  389. begin
  390. newparaloc:=cgpara.add_location;
  391. duplicatecgparaloc(paraloc,newparaloc);
  392. paraloc:=paraloc^.next;
  393. end;
  394. end;
  395. procedure tparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  396. begin
  397. p.funcretloc[side]:=get_funcretloc(p,side,nil);
  398. end;
  399. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  400. begin
  401. { We need to return the size allocated }
  402. p.init_paraloc_info(callbothsides);
  403. result:=p.calleeargareasize;
  404. end;
  405. function tparamanager.parseparaloc(parasym: tparavarsym; const s: string): boolean;
  406. begin
  407. Result:=False;
  408. internalerror(200807235);
  409. end;
  410. function tparamanager.parsefuncretloc(p: tabstractprocdef; const s: string): boolean;
  411. begin
  412. Result:=False;
  413. internalerror(200807236);
  414. end;
  415. function tparamanager.use_fixed_stack: boolean;
  416. begin
  417. {$ifdef i386}
  418. result := (target_info.system in [system_i386_darwin,system_i386_iphonesim]);
  419. {$else i386}
  420. {$ifdef cputargethasfixedstack}
  421. result := true;
  422. {$else cputargethasfixedstack}
  423. result := false;
  424. {$endif cputargethasfixedstack}
  425. {$endif i386}
  426. end;
  427. { This is a separate function because at least win64 allows stack allocations
  428. despite of fixed stack semantics (actually supporting it requires generating
  429. a compliant stack frame, not yet possible) }
  430. function tparamanager.use_stackalloc: boolean;
  431. begin
  432. result:=not use_fixed_stack;
  433. end;
  434. function tparamanager.set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean;
  435. var
  436. paraloc : pcgparalocation;
  437. begin
  438. result:=true;
  439. retloc.init;
  440. if not assigned(forcetempdef) then
  441. retloc.def:=p.returndef
  442. else
  443. begin
  444. retloc.def:=forcetempdef;
  445. retloc.temporary:=true;
  446. end;
  447. retloc.alignment:=get_para_align(p.proccalloption);
  448. { void has no location }
  449. if is_void(retloc.def) then
  450. begin
  451. paraloc:=retloc.add_location;
  452. retloc.size:=OS_NO;
  453. retcgsize:=OS_NO;
  454. retloc.intsize:=0;
  455. paraloc^.size:=OS_NO;
  456. paraloc^.loc:=LOC_VOID;
  457. exit;
  458. end;
  459. { Constructors return self instead of a boolean }
  460. if p.proctypeoption=potype_constructor then
  461. begin
  462. if is_implicit_pointer_object_type(tdef(p.owner.defowner)) then
  463. retloc.def:=tdef(p.owner.defowner)
  464. else
  465. retloc.def:=getpointerdef(tdef(p.owner.defowner));
  466. retcgsize:=OS_ADDR;
  467. retloc.intsize:=sizeof(pint);
  468. end
  469. else
  470. begin
  471. retcgsize:=def_cgsize(retloc.def);
  472. retloc.intsize:=retloc.def.size;
  473. end;
  474. retloc.size:=retcgsize;
  475. { Return is passed as var parameter }
  476. if ret_in_param(retloc.def,p.proccalloption) then
  477. begin
  478. retloc.def:=getpointerdef(retloc.def);
  479. paraloc:=retloc.add_location;
  480. paraloc^.loc:=LOC_REFERENCE;
  481. paraloc^.size:=retcgsize;
  482. exit;
  483. end;
  484. result:=false;
  485. end;
  486. initialization
  487. ;
  488. finalization
  489. paramanager.free;
  490. end.