procinfo.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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,globals,verbose,
  25. { symtable }
  26. symconst,symtype,symdef,symsym,
  27. { aasm }
  28. cpubase,cpuinfo,cgbase,cgutils,
  29. aasmbase,aasmtai,aasmdata,
  30. optutils
  31. ;
  32. const
  33. inherited_inlining_flags : tprocinfoflags =
  34. [pi_do_call,
  35. { the stack frame can't be removed in this case }
  36. pi_has_assembler_block,
  37. pi_uses_exceptions];
  38. type
  39. tsavedlabels = array[Boolean] of TAsmLabel;
  40. { This object gives information on the current routine being
  41. compiled.
  42. }
  43. { tprocinfo }
  44. tprocinfo = class(tlinkedlistitem)
  45. private
  46. { list to store the procinfo's of the nested procedures }
  47. nestedprocs : tlinkedlist;
  48. { required alignment for this stackframe }
  49. fstackalignment : longint;
  50. procedure addnestedproc(child: tprocinfo);
  51. public
  52. { pointer to parent in nested procedures }
  53. parent : tprocinfo;
  54. { the definition of the routine itself }
  55. procdef : tprocdef;
  56. { nested implicit finalzation procedure, used for platform-specific
  57. exception handling }
  58. finalize_procinfo : tprocinfo;
  59. { file location of begin of procedure }
  60. entrypos : tfileposinfo;
  61. { file location of end of procedure }
  62. exitpos : tfileposinfo;
  63. { local switches at begin of procedure }
  64. entryswitches : tlocalswitches;
  65. { local switches at end of procedure }
  66. exitswitches : tlocalswitches;
  67. { Size of the parameters on the stack }
  68. para_stack_size : pint;
  69. { Offset of temp after para/local are allocated }
  70. tempstart : longint;
  71. { some collected informations about the procedure
  72. see pi_xxxx constants above
  73. }
  74. flags : tprocinfoflags;
  75. { register used as frame pointer }
  76. framepointer : tregister;
  77. { register containing currently the got }
  78. got : tregister;
  79. CurrGOTLabel : tasmlabel;
  80. { Holds the reference used to store all saved registers. }
  81. save_regs_ref : treference;
  82. { Last assembler instruction of procedure prologue }
  83. endprologue_ai : tlinkedlistitem;
  84. { Amount of stack adjustment after all alignments }
  85. final_localsize : longint;
  86. { Labels for TRUE/FALSE condition, BREAK and CONTINUE }
  87. CurrBreakLabel,
  88. CurrContinueLabel,
  89. CurrTrueLabel,
  90. CurrFalseLabel : 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;
  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. { Store CurrTrueLabel and CurrFalseLabel to saved and generate new ones }
  126. procedure save_jump_labels(out saved: tsavedlabels);
  127. { Restore CurrTrueLabel and CurrFalseLabel from saved }
  128. procedure restore_jump_labels(const saved: tsavedlabels);
  129. function get_first_nestedproc: tprocinfo;
  130. function has_nestedprocs: boolean;
  131. function get_normal_proc: 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. end;
  142. tcprocinfo = class of tprocinfo;
  143. var
  144. cprocinfo : tcprocinfo;
  145. { information about the current sub routine being parsed (@var(pprocinfo))}
  146. current_procinfo : tprocinfo;
  147. implementation
  148. uses
  149. cutils,systems,
  150. tgobj,cgobj,
  151. paramgr
  152. ;
  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. CurrTrueLabel:=nil;
  176. CurrFalseLabel:=nil;
  177. if Assigned(parent) and (parent.procdef.parast.symtablelevel>=normal_function_level) then
  178. parent.addnestedproc(Self);
  179. end;
  180. procedure tprocinfo.force_nested;
  181. begin
  182. if Assigned(parent) and (parent.procdef.parast.symtablelevel<normal_function_level) then
  183. parent.addnestedproc(Self);
  184. end;
  185. destructor tprocinfo.destroy;
  186. begin
  187. nestedprocs.free;
  188. aktproccode.free;
  189. aktlocaldata.free;
  190. end;
  191. procedure tprocinfo.destroy_tree;
  192. var
  193. hp: tprocinfo;
  194. begin
  195. hp:=Self;
  196. while Assigned(hp.parent) do
  197. hp:=hp.parent;
  198. hp.Free;
  199. end;
  200. procedure tprocinfo.addnestedproc(child: tprocinfo);
  201. begin
  202. if nestedprocs=nil then
  203. nestedprocs:=TLinkedList.Create;
  204. nestedprocs.insert(child);
  205. end;
  206. procedure tprocinfo.updatestackalignment(alignment: longint);
  207. begin
  208. fstackalignment:=max(fstackalignment,alignment);
  209. end;
  210. function tprocinfo.get_first_nestedproc: tprocinfo;
  211. begin
  212. if assigned(nestedprocs) then
  213. result:=tprocinfo(nestedprocs.first)
  214. else
  215. result:=nil;
  216. end;
  217. function tprocinfo.has_nestedprocs: boolean;
  218. begin
  219. result:=assigned(nestedprocs) and (nestedprocs.count>0);
  220. end;
  221. function tprocinfo.get_normal_proc: tprocinfo;
  222. begin
  223. result:=self;
  224. while assigned(result.parent)and(result.procdef.parast.symtablelevel>normal_function_level) do
  225. result:=result.parent;
  226. end;
  227. procedure tprocinfo.save_jump_labels(out saved: tsavedlabels);
  228. begin
  229. saved[false]:=CurrFalseLabel;
  230. saved[true]:=CurrTrueLabel;
  231. current_asmdata.getjumplabel(CurrTrueLabel);
  232. current_asmdata.getjumplabel(CurrFalseLabel);
  233. end;
  234. procedure tprocinfo.restore_jump_labels(const saved: tsavedlabels);
  235. begin
  236. CurrFalseLabel:=saved[false];
  237. CurrTrueLabel:=saved[true];
  238. end;
  239. procedure tprocinfo.allocate_push_parasize(size:longint);
  240. begin
  241. if size>maxpushedparasize then
  242. maxpushedparasize:=size;
  243. end;
  244. function tprocinfo.calc_stackframe_size:longint;
  245. begin
  246. result:=Align(tg.direction*tg.lasttemp,current_settings.alignment.localalignmin);
  247. end;
  248. procedure tprocinfo.set_first_temp_offset;
  249. begin
  250. end;
  251. procedure tprocinfo.generate_parameter_info;
  252. begin
  253. { generate callee paraloc register info, it initialises the size that
  254. is allocated on the stack }
  255. procdef.init_paraloc_info(calleeside);
  256. para_stack_size:=procdef.calleeargareasize;
  257. end;
  258. procedure tprocinfo.allocate_got_register(list: TAsmList);
  259. begin
  260. { most os/cpu combo's don't use this yet, so not yet abstract }
  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. end.