procinfo.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Information about the current procedure that is being compiled
  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. unit procinfo;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,
  25. { symtable }
  26. symconst,symtype,symdef,symsym,
  27. node,
  28. { aasm }
  29. cpubase,cgbase,cgutils,
  30. aasmbase,aasmdata;
  31. const
  32. inherited_inlining_flags : tprocinfoflags =
  33. [pi_do_call,
  34. { the stack frame can't be removed in this case }
  35. pi_has_assembler_block,
  36. pi_uses_exceptions];
  37. type
  38. tsavedlabels = array[Boolean] of TAsmLabel;
  39. { This object gives information on the current routine being
  40. compiled.
  41. }
  42. tprocinfo = class(tlinkedlistitem)
  43. private
  44. { list to store the procinfo's of the nested procedures }
  45. nestedprocs : tlinkedlist;
  46. { required alignment for this stackframe }
  47. fstackalignment : longint;
  48. procedure addnestedproc(child: tprocinfo);
  49. public
  50. { pointer to parent in nested procedures }
  51. parent : tprocinfo;
  52. { the definition of the routine itself }
  53. procdef : tprocdef;
  54. { nested implicit finalzation procedure, used for platform-specific
  55. exception handling }
  56. finalize_procinfo : tprocinfo;
  57. { file location of begin of procedure }
  58. entrypos : tfileposinfo;
  59. { file location of end of procedure }
  60. exitpos : tfileposinfo;
  61. { local switches at begin of procedure }
  62. entryswitches : tlocalswitches;
  63. { local switches at end of procedure }
  64. exitswitches : tlocalswitches;
  65. { Size of the parameters on the stack }
  66. para_stack_size : pint;
  67. { Offset of temp after para/local are allocated }
  68. tempstart : longint;
  69. { some collected informations about the procedure
  70. see pi_xxxx constants above
  71. }
  72. flags : tprocinfoflags;
  73. { register used as frame pointer }
  74. framepointer : tregister;
  75. { register containing currently the got }
  76. got : tregister;
  77. CurrGOTLabel : tasmlabel;
  78. { register containing the tlsoffset }
  79. tlsoffset : tregister;
  80. { reference label for tls addresses }
  81. tlslabel : tasmlabel;
  82. { Holds the reference used to store all saved registers. }
  83. save_regs_ref : treference;
  84. { Last assembler instruction of procedure prologue }
  85. endprologue_ai : tlinkedlistitem;
  86. { Amount of stack adjustment after all alignments }
  87. final_localsize : longint;
  88. { Labels for TRUE/FALSE condition, BREAK and CONTINUE }
  89. CurrBreakLabel,
  90. CurrContinueLabel : tasmlabel;
  91. { label to leave the sub routine }
  92. CurrExitLabel : tasmlabel;
  93. { label for nested exits }
  94. nestedexitlabel : tlabelsym;
  95. { The code for the routine itself, excluding entry and
  96. exit code. This is a linked list of tai classes.
  97. }
  98. aktproccode : TAsmList;
  99. { Data (like jump tables) that belongs to this routine }
  100. aktlocaldata : TAsmList;
  101. { max. of space need for parameters }
  102. maxpushedparasize : SizeInt;
  103. { some architectures need to know a stack size before the first compilation pass
  104. estimatedtempsize contains an estimated value how big temps will get }
  105. estimatedtempsize : longint;
  106. { is this a constructor that calls another constructor on itself
  107. (either inherited, or another constructor of the same class)?
  108. Requires different entry code for some targets. }
  109. ConstructorCallingConstructor: boolean;
  110. { true, if an FPU instruction has been generated which could raise an exception and where the flags
  111. need to be checked explicitly like on RISC-V or certain ARM architectures }
  112. FPUExceptionCheckNeeded : Boolean;
  113. { local symbols and defs referenced by global functions; these need
  114. to be exported in case the function gets inlined }
  115. localrefsyms : tfpobjectlist;
  116. localrefdefs : tfpobjectlist;
  117. constructor create(aparent:tprocinfo);virtual;
  118. destructor destroy;override;
  119. procedure allocate_push_parasize(size:longint);
  120. function calc_stackframe_size:longint;virtual;abstract;
  121. { Set the address of the first temp, can be used to allocate
  122. space for pushing parameters }
  123. procedure set_first_temp_offset;virtual;
  124. { Generate parameter information }
  125. procedure generate_parameter_info;virtual;
  126. { Allocate got register }
  127. procedure allocate_got_register(list: TAsmList);virtual;
  128. { Allocate tls register }
  129. procedure allocate_tls_register(list: TAsmList);virtual;
  130. { get frame pointer }
  131. procedure init_framepointer; virtual;
  132. { Destroy the entire procinfo tree, starting from the outermost parent }
  133. procedure destroy_tree;
  134. function get_first_nestedproc: tprocinfo;
  135. function has_nestedprocs: boolean;
  136. function get_normal_proc: tprocinfo;
  137. procedure add_local_ref_sym(sym:tsym);
  138. procedure export_local_ref_syms;
  139. procedure add_local_ref_def(def:tdef);
  140. procedure export_local_ref_defs;
  141. function create_for_outlining(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef; entrynodeinfo: tnode): tprocinfo;
  142. { Add to parent's list of nested procedures even if parent is a 'main' procedure }
  143. procedure force_nested;
  144. { Get the required alignment for the current stack frame }
  145. property stackalignment: longint read fstackalignment;
  146. { Update the resuired alignment for the current stack frame based
  147. on the current value and the new required alignment }
  148. procedure updatestackalignment(alignment: longint);
  149. { Specific actions after the code has been generated }
  150. procedure postprocess_code; virtual;
  151. { set exception handling info }
  152. procedure set_eh_info; virtual;
  153. procedure setup_eh; virtual;
  154. procedure finish_eh; virtual;
  155. { called to insert needed eh info into the entry code }
  156. procedure start_eh(list : TAsmList); virtual;
  157. { called to insert needed eh info into the exit code }
  158. procedure end_eh(list : TAsmList); virtual;
  159. { Sets the pio_needs_parentfp flag for the current nested procedure and
  160. all its parent procedures until parent_level }
  161. procedure set_needs_parentfp(parent_level: byte);
  162. end;
  163. tcprocinfo = class of tprocinfo;
  164. var
  165. cprocinfo : tcprocinfo;
  166. { information about the current sub routine being parsed (@var(pprocinfo))}
  167. current_procinfo : tprocinfo;
  168. implementation
  169. uses
  170. globals,cutils,systems,verbose,
  171. procdefutil;
  172. {****************************************************************************
  173. TProcInfo
  174. ****************************************************************************}
  175. constructor tprocinfo.create(aparent:tprocinfo);
  176. begin
  177. parent:=aparent;
  178. procdef:=nil;
  179. para_stack_size:=0;
  180. fstackalignment:=target_info.stackalign;
  181. flags:=[];
  182. init_framepointer;
  183. framepointer:=NR_FRAME_POINTER_REG;
  184. maxpushedparasize:=0;
  185. { asmlists }
  186. aktproccode:=TAsmList.Create;
  187. aktlocaldata:=TAsmList.Create;
  188. reference_reset(save_regs_ref,sizeof(aint),[]);
  189. { labels }
  190. current_asmdata.getjumplabel(CurrExitLabel);
  191. current_asmdata.getjumplabel(CurrGOTLabel);
  192. CurrBreakLabel:=nil;
  193. CurrContinueLabel:=nil;
  194. if Assigned(parent) and (parent.procdef.parast.symtablelevel>=normal_function_level) then
  195. parent.addnestedproc(Self);
  196. end;
  197. procedure tprocinfo.force_nested;
  198. begin
  199. if Assigned(parent) and (parent.procdef.parast.symtablelevel<normal_function_level) then
  200. parent.addnestedproc(Self);
  201. end;
  202. destructor tprocinfo.destroy;
  203. begin
  204. nestedprocs.free;
  205. aktproccode.free;
  206. aktlocaldata.free;
  207. localrefsyms.free;
  208. localrefdefs.free;
  209. end;
  210. procedure tprocinfo.destroy_tree;
  211. var
  212. hp: tprocinfo;
  213. begin
  214. hp:=Self;
  215. while Assigned(hp.parent) do
  216. hp:=hp.parent;
  217. hp.Free;
  218. end;
  219. procedure tprocinfo.addnestedproc(child: tprocinfo);
  220. begin
  221. if nestedprocs=nil then
  222. nestedprocs:=TLinkedList.Create;
  223. nestedprocs.insert(child);
  224. end;
  225. procedure tprocinfo.updatestackalignment(alignment: longint);
  226. begin
  227. fstackalignment:=max(fstackalignment,alignment);
  228. end;
  229. function tprocinfo.get_first_nestedproc: tprocinfo;
  230. begin
  231. if assigned(nestedprocs) then
  232. result:=tprocinfo(nestedprocs.first)
  233. else
  234. result:=nil;
  235. end;
  236. function tprocinfo.has_nestedprocs: boolean;
  237. begin
  238. result:=assigned(nestedprocs) and (nestedprocs.count>0);
  239. end;
  240. function tprocinfo.get_normal_proc: tprocinfo;
  241. begin
  242. result:=self;
  243. while assigned(result.parent) and (result.procdef.parast.symtablelevel>normal_function_level) do
  244. result:=result.parent;
  245. end;
  246. procedure tprocinfo.add_local_ref_sym(sym:tsym);
  247. begin
  248. if not assigned(localrefsyms) then
  249. localrefsyms:=tfpobjectlist.create(false);
  250. if localrefsyms.indexof(sym)<0 then
  251. localrefsyms.add(sym);
  252. end;
  253. procedure tprocinfo.export_local_ref_syms;
  254. var
  255. i : longint;
  256. sym : tsym;
  257. begin
  258. if not assigned(localrefsyms) then
  259. exit;
  260. for i:=0 to localrefsyms.count-1 do
  261. begin
  262. sym:=tsym(localrefsyms[i]);
  263. if sym.typ<>staticvarsym then
  264. internalerror(2019110901);
  265. include(tstaticvarsym(sym).varoptions,vo_has_global_ref);
  266. end;
  267. end;
  268. procedure tprocinfo.add_local_ref_def(def:tdef);
  269. begin
  270. if not assigned(localrefdefs) then
  271. localrefdefs:=tfpobjectlist.create(false);
  272. if localrefdefs.indexof(def)<0 then
  273. localrefdefs.add(def);
  274. end;
  275. procedure tprocinfo.export_local_ref_defs;
  276. var
  277. i : longint;
  278. def : tdef;
  279. begin
  280. if not assigned(localrefdefs) then
  281. exit;
  282. for i:=0 to localrefdefs.count-1 do
  283. begin
  284. def:=tdef(localrefdefs[i]);
  285. if def.typ<>symconst.procdef then
  286. internalerror(2019111801);
  287. include(tprocdef(def).defoptions,df_has_global_ref);
  288. end;
  289. end;
  290. function tprocinfo.create_for_outlining(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef; entrynodeinfo: tnode): tprocinfo;
  291. begin
  292. result:=cprocinfo.create(self);
  293. result.force_nested;
  294. result.procdef:=create_outline_procdef(basesymname,astruct,potype,resultdef);
  295. result.entrypos:=entrynodeinfo.fileinfo;
  296. result.entryswitches:=entrynodeinfo.localswitches;
  297. result.exitpos:=current_filepos; // filepos of last node?
  298. result.exitswitches:=current_settings.localswitches; // localswitches of last node?
  299. end;
  300. procedure tprocinfo.allocate_push_parasize(size:longint);
  301. begin
  302. if size>maxpushedparasize then
  303. maxpushedparasize:=size;
  304. end;
  305. procedure tprocinfo.set_first_temp_offset;
  306. begin
  307. end;
  308. procedure tprocinfo.generate_parameter_info;
  309. begin
  310. { generate callee paraloc register info, it initialises the size that
  311. is allocated on the stack }
  312. procdef.init_paraloc_info(calleeside);
  313. para_stack_size:=procdef.calleeargareasize;
  314. end;
  315. procedure tprocinfo.allocate_got_register(list: TAsmList);
  316. begin
  317. { most os/cpu combo's don't use this yet, so not yet abstract }
  318. end;
  319. procedure tprocinfo.allocate_tls_register(list : TAsmList);
  320. begin
  321. end;
  322. procedure tprocinfo.init_framepointer;
  323. begin
  324. { most targets use a constant, but some have a typed constant that must
  325. be initialized }
  326. end;
  327. procedure tprocinfo.postprocess_code;
  328. begin
  329. { no action by default }
  330. end;
  331. procedure tprocinfo.set_eh_info;
  332. begin
  333. { default code is in tcgprocinfo }
  334. end;
  335. procedure tprocinfo.setup_eh;
  336. begin
  337. { no action by default }
  338. end;
  339. procedure tprocinfo.finish_eh;
  340. begin
  341. { no action by default }
  342. end;
  343. procedure tprocinfo.start_eh(list: TAsmList);
  344. begin
  345. { no action by default }
  346. end;
  347. procedure tprocinfo.end_eh(list: TAsmList);
  348. begin
  349. { no action by default }
  350. end;
  351. procedure tprocinfo.set_needs_parentfp(parent_level: byte);
  352. var
  353. pi : tprocinfo;
  354. begin
  355. if (procdef.parast.symtablelevel<=normal_function_level)
  356. or (procdef.parast.symtablelevel<=parent_level) then
  357. Internalerror(2020050302);
  358. if parent_level<normal_function_level then
  359. parent_level:=normal_function_level;
  360. pi:=Self;
  361. repeat
  362. include(pi.procdef.implprocoptions, pio_needs_parentfp);
  363. pi:=pi.parent;
  364. until pi.procdef.parast.symtablelevel<=parent_level;
  365. end;
  366. end.