procinfo.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 : aint;
  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. 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. { Allocate tls register }
  122. procedure allocate_tls_register(list: TAsmList);virtual;
  123. { get frame pointer }
  124. procedure init_framepointer; virtual;
  125. { Destroy the entire procinfo tree, starting from the outermost parent }
  126. procedure destroy_tree;
  127. function get_first_nestedproc: tprocinfo;
  128. function has_nestedprocs: boolean;
  129. function get_normal_proc: tprocinfo;
  130. function create_for_outlining(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef; entrynodeinfo: tnode): tprocinfo;
  131. { Add to parent's list of nested procedures even if parent is a 'main' procedure }
  132. procedure force_nested;
  133. { Get the required alignment for the current stack frame }
  134. property stackalignment: longint read fstackalignment;
  135. { Update the resuired alignment for the current stack frame based
  136. on the current value and the new required alignment }
  137. procedure updatestackalignment(alignment: longint);
  138. { Specific actions after the code has been generated }
  139. procedure postprocess_code; virtual;
  140. { set exception handling info }
  141. procedure set_eh_info; virtual;
  142. procedure setup_eh; virtual;
  143. procedure finish_eh; virtual;
  144. { called to insert needed eh info into the entry code }
  145. procedure start_eh(list : TAsmList); virtual;
  146. { called to insert needed eh info into the exit code }
  147. procedure end_eh(list : TAsmList); virtual;
  148. end;
  149. tcprocinfo = class of tprocinfo;
  150. var
  151. cprocinfo : tcprocinfo;
  152. { information about the current sub routine being parsed (@var(pprocinfo))}
  153. current_procinfo : tprocinfo;
  154. implementation
  155. uses
  156. globals,cutils,systems,
  157. procdefutil;
  158. {****************************************************************************
  159. TProcInfo
  160. ****************************************************************************}
  161. constructor tprocinfo.create(aparent:tprocinfo);
  162. begin
  163. parent:=aparent;
  164. procdef:=nil;
  165. para_stack_size:=0;
  166. fstackalignment:=target_info.stackalign;
  167. flags:=[];
  168. init_framepointer;
  169. framepointer:=NR_FRAME_POINTER_REG;
  170. maxpushedparasize:=0;
  171. { asmlists }
  172. aktproccode:=TAsmList.Create;
  173. aktlocaldata:=TAsmList.Create;
  174. reference_reset(save_regs_ref,sizeof(aint),[]);
  175. { labels }
  176. current_asmdata.getjumplabel(CurrExitLabel);
  177. current_asmdata.getjumplabel(CurrGOTLabel);
  178. CurrBreakLabel:=nil;
  179. CurrContinueLabel:=nil;
  180. if Assigned(parent) and (parent.procdef.parast.symtablelevel>=normal_function_level) then
  181. parent.addnestedproc(Self);
  182. end;
  183. procedure tprocinfo.force_nested;
  184. begin
  185. if Assigned(parent) and (parent.procdef.parast.symtablelevel<normal_function_level) then
  186. parent.addnestedproc(Self);
  187. end;
  188. destructor tprocinfo.destroy;
  189. begin
  190. nestedprocs.free;
  191. aktproccode.free;
  192. aktlocaldata.free;
  193. end;
  194. procedure tprocinfo.destroy_tree;
  195. var
  196. hp: tprocinfo;
  197. begin
  198. hp:=Self;
  199. while Assigned(hp.parent) do
  200. hp:=hp.parent;
  201. hp.Free;
  202. end;
  203. procedure tprocinfo.addnestedproc(child: tprocinfo);
  204. begin
  205. if nestedprocs=nil then
  206. nestedprocs:=TLinkedList.Create;
  207. nestedprocs.insert(child);
  208. end;
  209. procedure tprocinfo.updatestackalignment(alignment: longint);
  210. begin
  211. fstackalignment:=max(fstackalignment,alignment);
  212. end;
  213. function tprocinfo.get_first_nestedproc: tprocinfo;
  214. begin
  215. if assigned(nestedprocs) then
  216. result:=tprocinfo(nestedprocs.first)
  217. else
  218. result:=nil;
  219. end;
  220. function tprocinfo.has_nestedprocs: boolean;
  221. begin
  222. result:=assigned(nestedprocs) and (nestedprocs.count>0);
  223. end;
  224. function tprocinfo.get_normal_proc: tprocinfo;
  225. begin
  226. result:=self;
  227. while assigned(result.parent) and (result.procdef.parast.symtablelevel>normal_function_level) do
  228. result:=result.parent;
  229. end;
  230. function tprocinfo.create_for_outlining(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef; entrynodeinfo: tnode): tprocinfo;
  231. begin
  232. result:=cprocinfo.create(self);
  233. result.force_nested;
  234. result.procdef:=create_outline_procdef(basesymname,astruct,potype,resultdef);
  235. result.entrypos:=entrynodeinfo.fileinfo;
  236. result.entryswitches:=entrynodeinfo.localswitches;
  237. result.exitpos:=current_filepos; // filepos of last node?
  238. result.exitswitches:=current_settings.localswitches; // localswitches of last node?
  239. end;
  240. procedure tprocinfo.allocate_push_parasize(size:longint);
  241. begin
  242. if size>maxpushedparasize then
  243. maxpushedparasize:=size;
  244. end;
  245. procedure tprocinfo.set_first_temp_offset;
  246. begin
  247. end;
  248. procedure tprocinfo.generate_parameter_info;
  249. begin
  250. { generate callee paraloc register info, it initialises the size that
  251. is allocated on the stack }
  252. procdef.init_paraloc_info(calleeside);
  253. para_stack_size:=procdef.calleeargareasize;
  254. end;
  255. procedure tprocinfo.allocate_got_register(list: TAsmList);
  256. begin
  257. { most os/cpu combo's don't use this yet, so not yet abstract }
  258. end;
  259. procedure tprocinfo.allocate_tls_register(list : TAsmList);
  260. begin
  261. end;
  262. procedure tprocinfo.init_framepointer;
  263. begin
  264. { most targets use a constant, but some have a typed constant that must
  265. be initialized }
  266. end;
  267. procedure tprocinfo.postprocess_code;
  268. begin
  269. { no action by default }
  270. end;
  271. procedure tprocinfo.set_eh_info;
  272. begin
  273. { default code is in tcgprocinfo }
  274. end;
  275. procedure tprocinfo.setup_eh;
  276. begin
  277. { no action by default }
  278. end;
  279. procedure tprocinfo.finish_eh;
  280. begin
  281. { no action by default }
  282. end;
  283. procedure tprocinfo.start_eh(list: TAsmList);
  284. begin
  285. { no action by default }
  286. end;
  287. procedure tprocinfo.end_eh(list: TAsmList);
  288. begin
  289. { no action by default }
  290. end;
  291. end.