procinfo.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. aktproccode.free;
  213. aktlocaldata.free;
  214. localrefsyms.free;
  215. localrefdefs.free;
  216. end;
  217. procedure tprocinfo.destroy_tree;
  218. var
  219. hp: tprocinfo;
  220. begin
  221. hp:=Self;
  222. while Assigned(hp.parent) do
  223. hp:=hp.parent;
  224. hp.Free;
  225. end;
  226. procedure tprocinfo.addnestedproc(child: tprocinfo);
  227. begin
  228. if nestedprocs=nil then
  229. nestedprocs:=TLinkedList.Create;
  230. nestedprocs.insert(child);
  231. end;
  232. function tprocinfo.find_nestedproc_by_pd(pd:tprocdef):tprocinfo;
  233. var
  234. pi : tprocinfo;
  235. begin
  236. if not assigned(nestedprocs) then
  237. exit(nil);
  238. pi:=tprocinfo(nestedprocs.first);
  239. while assigned(pi) do
  240. begin
  241. if pi.procdef=pd then
  242. exit(pi);
  243. pi:=tprocinfo(pi.next);
  244. end;
  245. result:=nil;
  246. end;
  247. procedure tprocinfo.updatestackalignment(alignment: longint);
  248. begin
  249. fstackalignment:=max(fstackalignment,alignment);
  250. end;
  251. function tprocinfo.get_first_nestedproc: tprocinfo;
  252. begin
  253. if assigned(nestedprocs) then
  254. result:=tprocinfo(nestedprocs.first)
  255. else
  256. result:=nil;
  257. end;
  258. function tprocinfo.has_nestedprocs: boolean;
  259. begin
  260. result:=assigned(nestedprocs) and (nestedprocs.count>0);
  261. end;
  262. function tprocinfo.get_normal_proc: tprocinfo;
  263. begin
  264. result:=self;
  265. while assigned(result.parent) and (result.procdef.parast.symtablelevel>normal_function_level) do
  266. result:=result.parent;
  267. end;
  268. procedure tprocinfo.add_local_ref_sym(sym:tsym);
  269. begin
  270. if not assigned(localrefsyms) then
  271. localrefsyms:=tfpobjectlist.create(false);
  272. if localrefsyms.indexof(sym)<0 then
  273. localrefsyms.add(sym);
  274. end;
  275. procedure tprocinfo.export_local_ref_syms;
  276. var
  277. i : longint;
  278. sym : tsym;
  279. begin
  280. if not assigned(localrefsyms) then
  281. exit;
  282. for i:=0 to localrefsyms.count-1 do
  283. begin
  284. sym:=tsym(localrefsyms[i]);
  285. if sym.typ<>staticvarsym then
  286. internalerror(2019110901);
  287. include(tstaticvarsym(sym).varoptions,vo_has_global_ref);
  288. end;
  289. end;
  290. procedure tprocinfo.add_local_ref_def(def:tdef);
  291. begin
  292. if not assigned(localrefdefs) then
  293. localrefdefs:=tfpobjectlist.create(false);
  294. if localrefdefs.indexof(def)<0 then
  295. localrefdefs.add(def);
  296. end;
  297. procedure tprocinfo.export_local_ref_defs;
  298. var
  299. i : longint;
  300. def : tdef;
  301. begin
  302. if not assigned(localrefdefs) then
  303. exit;
  304. for i:=0 to localrefdefs.count-1 do
  305. begin
  306. def:=tdef(localrefdefs[i]);
  307. if def.typ<>symconst.procdef then
  308. internalerror(2019111801);
  309. include(tprocdef(def).defoptions,df_has_global_ref);
  310. end;
  311. end;
  312. procedure tprocinfo.add_captured_sym(sym:tsym;def:tdef;const fileinfo:tfileposinfo);
  313. begin
  314. procdef.add_captured_sym(sym,def,fileinfo);
  315. end;
  316. function tprocinfo.create_for_outlining(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef; entrynodeinfo: tnode): tprocinfo;
  317. begin
  318. result:=cprocinfo.create(self);
  319. result.force_nested;
  320. result.procdef:=create_outline_procdef(basesymname,astruct,potype,resultdef);
  321. result.entrypos:=entrynodeinfo.fileinfo;
  322. result.entryswitches:=entrynodeinfo.localswitches;
  323. result.exitpos:=current_filepos; // filepos of last node?
  324. result.exitswitches:=current_settings.localswitches; // localswitches of last node?
  325. end;
  326. procedure tprocinfo.allocate_push_parasize(size:longint);
  327. begin
  328. if size>maxpushedparasize then
  329. maxpushedparasize:=size;
  330. end;
  331. procedure tprocinfo.set_first_temp_offset;
  332. begin
  333. end;
  334. procedure tprocinfo.generate_parameter_info;
  335. begin
  336. { generate callee paraloc register info, it initialises the size that
  337. is allocated on the stack }
  338. procdef.init_paraloc_info(calleeside);
  339. para_stack_size:=procdef.calleeargareasize;
  340. end;
  341. procedure tprocinfo.allocate_got_register(list: TAsmList);
  342. begin
  343. { most os/cpu combo's don't use this yet, so not yet abstract }
  344. end;
  345. procedure tprocinfo.allocate_tls_register(list : TAsmList);
  346. begin
  347. end;
  348. procedure tprocinfo.init_framepointer;
  349. begin
  350. { most targets use a constant, but some have a typed constant that must
  351. be initialized }
  352. end;
  353. procedure tprocinfo.postprocess_code;
  354. begin
  355. { no action by default }
  356. end;
  357. procedure tprocinfo.set_eh_info;
  358. begin
  359. { default code is in tcgprocinfo }
  360. end;
  361. procedure tprocinfo.setup_eh;
  362. begin
  363. { no action by default }
  364. end;
  365. procedure tprocinfo.finish_eh;
  366. begin
  367. { no action by default }
  368. end;
  369. procedure tprocinfo.start_eh(list: TAsmList);
  370. begin
  371. { no action by default }
  372. end;
  373. procedure tprocinfo.end_eh(list: TAsmList);
  374. begin
  375. { no action by default }
  376. end;
  377. procedure tprocinfo.set_needs_parentfp(parent_level: byte);
  378. var
  379. pi : tprocinfo;
  380. p : tparavarsym;
  381. begin
  382. if procdef.parast.symtablelevel<=normal_function_level then
  383. Internalerror(2020050302);
  384. if procdef.parast.symtablelevel<=parent_level then
  385. exit;
  386. if parent_level<normal_function_level then
  387. parent_level:=normal_function_level;
  388. { Mark parentfp as used for the current proc }
  389. pi:=Self;
  390. tparavarsym(pi.procdef.parentfpsym).varstate:=vs_read;
  391. { Set both parentfp is used and pio_nested_access for all parent procs until parent_level }
  392. while pi.procdef.parast.symtablelevel>parent_level do
  393. begin
  394. pi:=pi.parent;
  395. if pi.procdef.parast.symtablelevel>normal_function_level then
  396. begin
  397. p:=tparavarsym(pi.procdef.parentfpsym);
  398. p.varstate:=vs_read;
  399. { parentfp is accessed from a nested routine.
  400. Must be in the memory. }
  401. p.varregable:=vr_none;
  402. end;
  403. include(pi.procdef.implprocoptions,pio_nested_access);
  404. end;
  405. end;
  406. end.