procinfo.pas 11 KB

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