procinfo.pas 9.1 KB

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