paramgr.pas 17 KB

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