procinfo.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 }
  43. tprocinfo = class(tlinkedlistitem)
  44. private
  45. { list to store the procinfo's of the nested procedures }
  46. nestedprocs : tlinkedlist;
  47. { required alignment for this stackframe }
  48. fstackalignment : longint;
  49. procedure addnestedproc(child: tprocinfo);
  50. public
  51. { pointer to parent in nested procedures }
  52. parent : tprocinfo;
  53. { the definition of the routine itself }
  54. procdef : tprocdef;
  55. { nested implicit finalzation procedure, used for platform-specific
  56. exception handling }
  57. finalize_procinfo : tprocinfo;
  58. { file location of begin of procedure }
  59. entrypos : tfileposinfo;
  60. { file location of end of procedure }
  61. exitpos : tfileposinfo;
  62. { local switches at begin of procedure }
  63. entryswitches : tlocalswitches;
  64. { local switches at end of procedure }
  65. exitswitches : tlocalswitches;
  66. { Size of the parameters on the stack }
  67. para_stack_size : pint;
  68. { Offset of temp after para/local are allocated }
  69. tempstart : longint;
  70. { some collected informations about the procedure
  71. see pi_xxxx constants above
  72. }
  73. flags : tprocinfoflags;
  74. { register used as frame pointer }
  75. framepointer : tregister;
  76. { register containing currently the got }
  77. got : tregister;
  78. CurrGOTLabel : tasmlabel;
  79. { Holds the reference used to store all saved registers. }
  80. save_regs_ref : treference;
  81. { Last assembler instruction of procedure prologue }
  82. endprologue_ai : tlinkedlistitem;
  83. { Amount of stack adjustment after all alignments }
  84. final_localsize : longint;
  85. { Labels for TRUE/FALSE condition, BREAK and CONTINUE }
  86. CurrBreakLabel,
  87. CurrContinueLabel : tasmlabel;
  88. { label to leave the sub routine }
  89. CurrExitLabel : tasmlabel;
  90. { label for nested exits }
  91. nestedexitlabel : tlabelsym;
  92. { The code for the routine itself, excluding entry and
  93. exit code. This is a linked list of tai classes.
  94. }
  95. aktproccode : TAsmList;
  96. { Data (like jump tables) that belongs to this routine }
  97. aktlocaldata : TAsmList;
  98. { max. of space need for parameters }
  99. maxpushedparasize : aint;
  100. { some architectures need to know a stack size before the first compilation pass
  101. estimatedtempsize contains an estimated value how big temps will get }
  102. estimatedtempsize : longint;
  103. { is this a constructor that calls another constructor on itself
  104. (either inherited, or another constructor of the same class)?
  105. Requires different entry code for some targets. }
  106. ConstructorCallingConstructor: boolean;
  107. { true, if an FPU instruction has been generated which could raise an exception and where the flags
  108. need to be checked explicitly like on RISC-V or certain ARM architectures }
  109. FPUExceptionCheckNeeded : Boolean;
  110. constructor create(aparent:tprocinfo);virtual;
  111. destructor destroy;override;
  112. procedure allocate_push_parasize(size:longint);
  113. function calc_stackframe_size:longint;virtual;abstract;
  114. { Set the address of the first temp, can be used to allocate
  115. space for pushing parameters }
  116. procedure set_first_temp_offset;virtual;
  117. { Generate parameter information }
  118. procedure generate_parameter_info;virtual;
  119. { Allocate got register }
  120. procedure allocate_got_register(list: TAsmList);virtual;
  121. { get frame pointer }
  122. procedure init_framepointer; virtual;
  123. { Destroy the entire procinfo tree, starting from the outermost parent }
  124. procedure destroy_tree;
  125. function get_first_nestedproc: tprocinfo;
  126. function has_nestedprocs: boolean;
  127. function get_normal_proc: tprocinfo;
  128. function create_for_outlining(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef; entrynodeinfo: tnode): tprocinfo;
  129. { Add to parent's list of nested procedures even if parent is a 'main' procedure }
  130. procedure force_nested;
  131. { Get the required alignment for the current stack frame }
  132. property stackalignment: longint read fstackalignment;
  133. { Update the resuired alignment for the current stack frame based
  134. on the current value and the new required alignment }
  135. procedure updatestackalignment(alignment: longint);
  136. { Specific actions after the code has been generated }
  137. procedure postprocess_code; virtual;
  138. end;
  139. tcprocinfo = class of tprocinfo;
  140. var
  141. cprocinfo : tcprocinfo;
  142. { information about the current sub routine being parsed (@var(pprocinfo))}
  143. current_procinfo : tprocinfo;
  144. implementation
  145. uses
  146. globals,cutils,systems,
  147. procdefutil;
  148. {****************************************************************************
  149. TProcInfo
  150. ****************************************************************************}
  151. constructor tprocinfo.create(aparent:tprocinfo);
  152. begin
  153. parent:=aparent;
  154. procdef:=nil;
  155. para_stack_size:=0;
  156. fstackalignment:=target_info.stackalign;
  157. flags:=[];
  158. init_framepointer;
  159. framepointer:=NR_FRAME_POINTER_REG;
  160. maxpushedparasize:=0;
  161. { asmlists }
  162. aktproccode:=TAsmList.Create;
  163. aktlocaldata:=TAsmList.Create;
  164. reference_reset(save_regs_ref,sizeof(aint),[]);
  165. { labels }
  166. current_asmdata.getjumplabel(CurrExitLabel);
  167. current_asmdata.getjumplabel(CurrGOTLabel);
  168. CurrBreakLabel:=nil;
  169. CurrContinueLabel:=nil;
  170. if Assigned(parent) and (parent.procdef.parast.symtablelevel>=normal_function_level) then
  171. parent.addnestedproc(Self);
  172. end;
  173. procedure tprocinfo.force_nested;
  174. begin
  175. if Assigned(parent) and (parent.procdef.parast.symtablelevel<normal_function_level) then
  176. parent.addnestedproc(Self);
  177. end;
  178. destructor tprocinfo.destroy;
  179. begin
  180. nestedprocs.free;
  181. aktproccode.free;
  182. aktlocaldata.free;
  183. end;
  184. procedure tprocinfo.destroy_tree;
  185. var
  186. hp: tprocinfo;
  187. begin
  188. hp:=Self;
  189. while Assigned(hp.parent) do
  190. hp:=hp.parent;
  191. hp.Free;
  192. end;
  193. procedure tprocinfo.addnestedproc(child: tprocinfo);
  194. begin
  195. if nestedprocs=nil then
  196. nestedprocs:=TLinkedList.Create;
  197. nestedprocs.insert(child);
  198. end;
  199. procedure tprocinfo.updatestackalignment(alignment: longint);
  200. begin
  201. fstackalignment:=max(fstackalignment,alignment);
  202. end;
  203. function tprocinfo.get_first_nestedproc: tprocinfo;
  204. begin
  205. if assigned(nestedprocs) then
  206. result:=tprocinfo(nestedprocs.first)
  207. else
  208. result:=nil;
  209. end;
  210. function tprocinfo.has_nestedprocs: boolean;
  211. begin
  212. result:=assigned(nestedprocs) and (nestedprocs.count>0);
  213. end;
  214. function tprocinfo.get_normal_proc: tprocinfo;
  215. begin
  216. result:=self;
  217. while assigned(result.parent) and (result.procdef.parast.symtablelevel>normal_function_level) do
  218. result:=result.parent;
  219. end;
  220. function tprocinfo.create_for_outlining(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef; entrynodeinfo: tnode): tprocinfo;
  221. begin
  222. result:=cprocinfo.create(self);
  223. result.force_nested;
  224. result.procdef:=create_outline_procdef(basesymname,astruct,potype,resultdef);
  225. result.entrypos:=entrynodeinfo.fileinfo;
  226. result.entryswitches:=entrynodeinfo.localswitches;
  227. result.exitpos:=current_filepos; // filepos of last node?
  228. result.exitswitches:=current_settings.localswitches; // localswitches of last node?
  229. end;
  230. procedure tprocinfo.allocate_push_parasize(size:longint);
  231. begin
  232. if size>maxpushedparasize then
  233. maxpushedparasize:=size;
  234. end;
  235. procedure tprocinfo.set_first_temp_offset;
  236. begin
  237. end;
  238. procedure tprocinfo.generate_parameter_info;
  239. begin
  240. { generate callee paraloc register info, it initialises the size that
  241. is allocated on the stack }
  242. procdef.init_paraloc_info(calleeside);
  243. para_stack_size:=procdef.calleeargareasize;
  244. end;
  245. procedure tprocinfo.allocate_got_register(list: TAsmList);
  246. begin
  247. { most os/cpu combo's don't use this yet, so not yet abstract }
  248. end;
  249. procedure tprocinfo.init_framepointer;
  250. begin
  251. { most targets use a constant, but some have a typed constant that must
  252. be initialized }
  253. end;
  254. procedure tprocinfo.postprocess_code;
  255. begin
  256. { no action by default }
  257. end;
  258. end.