procinfo.pas 16 KB

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