procinfo.pas 9.4 KB

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