psub.pas 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing and codegeneration at subroutine level
  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 psub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globals,
  22. node,nbas,
  23. symdef,procinfo,optdfa;
  24. type
  25. { tcgprocinfo }
  26. tcgprocinfo = class(tprocinfo)
  27. private
  28. procedure CreateInlineInfo;
  29. { returns the node which is the start of the user code, this is needed by the dfa }
  30. function GetUserCode: tnode;
  31. procedure maybe_add_constructor_wrapper(var tocode: tnode; withexceptblock: boolean);
  32. procedure add_entry_exit_code;
  33. procedure setup_tempgen;
  34. public
  35. { code for the subroutine as tree }
  36. code : tnode;
  37. { positions in the tree for init/final }
  38. entry_asmnode,
  39. loadpara_asmnode,
  40. exitlabel_asmnode,
  41. stackcheck_asmnode,
  42. init_asmnode,
  43. final_asmnode : tasmnode;
  44. final_used : boolean;
  45. dfabuilder : TDFABuilder;
  46. destructor destroy;override;
  47. procedure printproc(pass:string);
  48. procedure generate_code;
  49. procedure generate_code_tree;
  50. procedure generate_exceptfilter(nestedpi: tcgprocinfo);
  51. procedure resetprocdef;
  52. procedure add_to_symtablestack;
  53. procedure remove_from_symtablestack;
  54. procedure parse_body;
  55. function has_assembler_child : boolean;
  56. end;
  57. procedure printnode_reset;
  58. { reads the declaration blocks }
  59. procedure read_declarations(islibrary : boolean);
  60. { reads declarations in the interface part of a unit }
  61. procedure read_interface_declarations;
  62. { reads any routine in the implementation, or a non-method routine
  63. declaration in the interface (depending on whether or not parse_only is
  64. true) }
  65. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef);
  66. procedure generate_specialization_procs;
  67. implementation
  68. uses
  69. sysutils,
  70. { common }
  71. cutils,
  72. { global }
  73. globtype,tokens,verbose,comphook,constexp,
  74. systems,cpubase,aasmbase,aasmtai,aasmdata,
  75. { symtable }
  76. symconst,symbase,symsym,symtype,symtable,defutil,symcreat,
  77. paramgr,
  78. ppu,fmodule,
  79. { pass 1 }
  80. nutils,ngenutil,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  81. pass_1,
  82. {$ifdef state_tracking}
  83. nstate,
  84. {$endif state_tracking}
  85. { pass 2 }
  86. {$ifndef NOPASS2}
  87. pass_2,
  88. {$endif}
  89. { parser }
  90. scanner,gendef,
  91. pbase,pstatmnt,pdecl,pdecsub,pexports,pgenutil,pparautl,
  92. { codegen }
  93. tgobj,cgbase,cgobj,cgutils,hlcgobj,hlcgcpu,dbgbase,
  94. ncgutil,regvars,
  95. optbase,
  96. opttail,
  97. optcse,
  98. optloop,
  99. optconstprop,
  100. optdeadstore
  101. {$if defined(arm)}
  102. ,cpuinfo
  103. {$endif arm}
  104. {$ifndef NOOPT}
  105. {$ifdef i386}
  106. ,aopt386
  107. {$else i386}
  108. ,aopt
  109. {$endif i386}
  110. {$endif}
  111. ;
  112. function checknodeinlining(procdef: tprocdef): boolean;
  113. var
  114. i : integer;
  115. currpara : tparavarsym;
  116. begin
  117. result := false;
  118. if pi_has_assembler_block in current_procinfo.flags then
  119. begin
  120. Message1(parser_h_not_supported_for_inline,'assembler');
  121. Message(parser_h_inlining_disabled);
  122. exit;
  123. end;
  124. if pi_has_global_goto in current_procinfo.flags then
  125. begin
  126. Message1(parser_h_not_supported_for_inline,'global goto');
  127. Message(parser_h_inlining_disabled);
  128. exit;
  129. end;
  130. if pi_has_nested_exit in current_procinfo.flags then
  131. begin
  132. Message1(parser_h_not_supported_for_inline,'nested exit');
  133. Message(parser_h_inlining_disabled);
  134. exit;
  135. end;
  136. if pi_calls_c_varargs in current_procinfo.flags then
  137. begin
  138. Message1(parser_h_not_supported_for_inline,'called C-style varargs functions');
  139. Message(parser_h_inlining_disabled);
  140. exit;
  141. end;
  142. { the compiler cannot handle inherited in inlined subroutines because
  143. it tries to search for self in the symtable, however, the symtable
  144. is not available }
  145. if pi_has_inherited in current_procinfo.flags then
  146. begin
  147. Message1(parser_h_not_supported_for_inline,'inherited');
  148. Message(parser_h_inlining_disabled);
  149. exit;
  150. end;
  151. for i:=0 to procdef.paras.count-1 do
  152. begin
  153. currpara:=tparavarsym(procdef.paras[i]);
  154. case currpara.vardef.typ of
  155. formaldef :
  156. begin
  157. if (currpara.varspez in [vs_out,vs_var,vs_const,vs_constref]) then
  158. begin
  159. Message1(parser_h_not_supported_for_inline,'formal parameter');
  160. Message(parser_h_inlining_disabled);
  161. exit;
  162. end;
  163. end;
  164. arraydef :
  165. begin
  166. if is_array_of_const(currpara.vardef) or
  167. is_variant_array(currpara.vardef) then
  168. begin
  169. Message1(parser_h_not_supported_for_inline,'array of const');
  170. Message(parser_h_inlining_disabled);
  171. exit;
  172. end;
  173. { open arrays might need re-basing of the index, i.e. if you pass
  174. an array[1..10] as open array, you have to add 1 to all index operations
  175. if you directly inline it }
  176. if is_open_array(currpara.vardef) then
  177. begin
  178. Message1(parser_h_not_supported_for_inline,'open array');
  179. Message(parser_h_inlining_disabled);
  180. exit;
  181. end;
  182. end;
  183. end;
  184. end;
  185. result:=true;
  186. end;
  187. {****************************************************************************
  188. PROCEDURE/FUNCTION BODY PARSING
  189. ****************************************************************************}
  190. procedure initializedefaultvars(p:TObject;arg:pointer);
  191. var
  192. b : tblocknode;
  193. begin
  194. if tsym(p).typ<>localvarsym then
  195. exit;
  196. with tabstractnormalvarsym(p) do
  197. begin
  198. if (vo_is_default_var in varoptions) and (vardef.size>0) then
  199. begin
  200. b:=tblocknode(arg);
  201. b.left:=cstatementnode.create(
  202. ccallnode.createintern('fpc_zeromem',
  203. ccallparanode.create(
  204. cordconstnode.create(vardef.size,ptruinttype,false),
  205. ccallparanode.create(
  206. caddrnode.create_internal(
  207. cloadnode.create(tsym(p),tsym(p).owner)),
  208. nil
  209. )
  210. )
  211. ),
  212. b.left);
  213. end;
  214. end;
  215. end;
  216. procedure initializevars(p:TObject;arg:pointer);
  217. var
  218. b : tblocknode;
  219. begin
  220. if not (tsym(p).typ in [localvarsym,staticvarsym]) then
  221. exit;
  222. with tabstractnormalvarsym(p) do
  223. begin
  224. if assigned(defaultconstsym) then
  225. begin
  226. b:=tblocknode(arg);
  227. b.left:=cstatementnode.create(
  228. cassignmentnode.create(
  229. cloadnode.create(tsym(p),tsym(p).owner),
  230. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  231. b.left);
  232. end
  233. else
  234. initializedefaultvars(p,arg);
  235. end;
  236. end;
  237. procedure check_finalize_paras(p:TObject;arg:pointer);
  238. begin
  239. if (tsym(p).typ=paravarsym) then
  240. begin
  241. if tparavarsym(p).needs_finalization then
  242. begin
  243. include(current_procinfo.flags,pi_needs_implicit_finally);
  244. include(current_procinfo.flags,pi_do_call);
  245. end;
  246. if (tparavarsym(p).varspez in [vs_value,vs_out]) and
  247. (cs_create_pic in current_settings.moduleswitches) and
  248. (tf_pic_uses_got in target_info.flags) and
  249. is_rtti_managed_type(tparavarsym(p).vardef) then
  250. include(current_procinfo.flags,pi_needs_got);
  251. end;
  252. end;
  253. procedure check_finalize_locals(p:TObject;arg:pointer);
  254. begin
  255. { include the result: it needs to be finalized in case an exception }
  256. { occurs }
  257. if (tsym(p).typ=localvarsym) and
  258. (tlocalvarsym(p).refs>0) and
  259. is_managed_type(tlocalvarsym(p).vardef) then
  260. begin
  261. include(current_procinfo.flags,pi_needs_implicit_finally);
  262. include(current_procinfo.flags,pi_do_call);
  263. if is_rtti_managed_type(tlocalvarsym(p).vardef) and
  264. (cs_create_pic in current_settings.moduleswitches) and
  265. (tf_pic_uses_got in target_info.flags) then
  266. include(current_procinfo.flags,pi_needs_got);
  267. end;
  268. end;
  269. function block(islibrary : boolean) : tnode;
  270. var
  271. oldfilepos: tfileposinfo;
  272. begin
  273. { parse const,types and vars }
  274. read_declarations(islibrary);
  275. { do we have an assembler block without the po_assembler?
  276. we should allow this for Delphi compatibility (PFV) }
  277. if (token=_ASM) and (m_delphi in current_settings.modeswitches) then
  278. include(current_procinfo.procdef.procoptions,po_assembler);
  279. { Handle assembler block different }
  280. if (po_assembler in current_procinfo.procdef.procoptions) then
  281. begin
  282. block:=assembler_block;
  283. exit;
  284. end;
  285. {Unit initialization?.}
  286. if (
  287. assigned(current_procinfo.procdef.localst) and
  288. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  289. (current_module.is_unit or islibrary)
  290. ) then
  291. begin
  292. if (token=_END) then
  293. begin
  294. consume(_END);
  295. { We need at least a node, else the entry/exit code is not
  296. generated and thus no PASCALMAIN symbol which we need (PFV) }
  297. if islibrary then
  298. block:=cnothingnode.create
  299. else
  300. block:=nil;
  301. end
  302. else
  303. begin
  304. if token=_INITIALIZATION then
  305. begin
  306. { The library init code is already called and does not
  307. need to be in the initfinal table (PFV) }
  308. block:=statement_block(_INITIALIZATION);
  309. end
  310. else if token=_FINALIZATION then
  311. begin
  312. { when a unit has only a finalization section, we can come to this
  313. point when we try to read the nonh existing initalization section
  314. so we've to check if we are really try to parse the finalization }
  315. if current_procinfo.procdef.proctypeoption=potype_unitfinalize then
  316. block:=statement_block(_FINALIZATION)
  317. else
  318. block:=nil;
  319. end
  320. else
  321. block:=statement_block(_BEGIN);
  322. end;
  323. end
  324. else
  325. begin
  326. { parse routine body }
  327. block:=statement_block(_BEGIN);
  328. { initialized variables }
  329. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  330. begin
  331. { initialization of local variables with their initial
  332. values: part of function entry }
  333. oldfilepos:=current_filepos;
  334. current_filepos:=current_procinfo.entrypos;
  335. current_procinfo.procdef.localst.SymList.ForEachCall(@initializevars,block);
  336. current_filepos:=oldfilepos;
  337. end
  338. else if current_procinfo.procdef.localst.symtabletype=staticsymtable then
  339. begin
  340. { for program and unit initialization code we also need to
  341. initialize the local variables used of Default() }
  342. oldfilepos:=current_filepos;
  343. current_filepos:=current_procinfo.entrypos;
  344. current_procinfo.procdef.localst.SymList.ForEachCall(@initializedefaultvars,block);
  345. current_filepos:=oldfilepos;
  346. end;
  347. if assigned(current_procinfo.procdef.parentfpstruct) then
  348. begin
  349. { we only do this after the code has been parsed because
  350. otherwise for-loop counters moved to the struct cause
  351. errors; we still do it nevertheless to prevent false
  352. "unused" symbols warnings and to assist debug info
  353. generation }
  354. redirect_parentfpstruct_local_syms(current_procinfo.procdef);
  355. { finish the parentfpstruct (add padding, ...) }
  356. finish_parentfpstruct(current_procinfo.procdef);
  357. end;
  358. end;
  359. end;
  360. {****************************************************************************
  361. PROCEDURE/FUNCTION COMPILING
  362. ****************************************************************************}
  363. procedure printnode_reset;
  364. begin
  365. assign(printnodefile,treelogfilename);
  366. {$push}{$I-}
  367. rewrite(printnodefile);
  368. {$pop}
  369. if ioresult<>0 then
  370. begin
  371. Comment(V_Error,'Error creating '+treelogfilename);
  372. exit;
  373. end;
  374. close(printnodefile);
  375. end;
  376. procedure add_label_init(p:TObject;arg:pointer);
  377. begin
  378. if tstoredsym(p).typ=labelsym then
  379. begin
  380. addstatement(tstatementnode(arg^),
  381. cifnode.create(caddnode.create(equaln,
  382. ccallnode.createintern('fpc_setjmp',
  383. ccallparanode.create(cloadnode.create(tlabelsym(p).jumpbuf,tlabelsym(p).jumpbuf.owner),nil)),
  384. cordconstnode.create(1,sinttype,true))
  385. ,cgotonode.create(tlabelsym(p)),nil)
  386. );
  387. end;
  388. end;
  389. function generate_bodyentry_block:tnode;
  390. var
  391. srsym : tsym;
  392. para : tcallparanode;
  393. call : tcallnode;
  394. newstatement : tstatementnode;
  395. def : tabstractrecorddef;
  396. begin
  397. result:=internalstatements(newstatement);
  398. if assigned(current_structdef) then
  399. begin
  400. { a constructor needs a help procedure }
  401. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  402. begin
  403. if is_class(current_structdef) or
  404. (
  405. is_objectpascal_helper(current_structdef) and
  406. is_class(tobjectdef(current_structdef).extendeddef)
  407. ) then
  408. begin
  409. if is_objectpascal_helper(current_structdef) then
  410. def:=tabstractrecorddef(tobjectdef(current_structdef).extendeddef)
  411. else
  412. def:=current_structdef;
  413. srsym:=search_struct_member(def,'NEWINSTANCE');
  414. if assigned(srsym) and
  415. (srsym.typ=procsym) then
  416. begin
  417. { if vmt=1 then newinstance }
  418. addstatement(newstatement,cifnode.create(
  419. caddnode.create_internal(equaln,
  420. ctypeconvnode.create_internal(
  421. load_vmt_pointer_node,
  422. voidpointertype),
  423. cpointerconstnode.create(1,voidpointertype)),
  424. cassignmentnode.create(
  425. ctypeconvnode.create_internal(
  426. load_self_pointer_node,
  427. voidpointertype),
  428. ccallnode.create(nil,tprocsym(srsym),srsym.owner,
  429. ctypeconvnode.create_internal(load_self_pointer_node,cclassrefdef.create(current_structdef)),
  430. [])),
  431. nil));
  432. end
  433. else
  434. internalerror(200305108);
  435. end
  436. else
  437. if is_object(current_structdef) then
  438. begin
  439. { parameter 3 : vmt_offset }
  440. { parameter 2 : address of pointer to vmt,
  441. this is required to allow setting the vmt to -1 to indicate
  442. that memory was allocated }
  443. { parameter 1 : self pointer }
  444. para:=ccallparanode.create(
  445. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  446. ccallparanode.create(
  447. ctypeconvnode.create_internal(
  448. load_vmt_pointer_node,
  449. voidpointertype),
  450. ccallparanode.create(
  451. ctypeconvnode.create_internal(
  452. load_self_pointer_node,
  453. voidpointertype),
  454. nil)));
  455. addstatement(newstatement,cassignmentnode.create(
  456. ctypeconvnode.create_internal(
  457. load_self_pointer_node,
  458. voidpointertype),
  459. ccallnode.createintern('fpc_help_constructor',para)));
  460. end
  461. else
  462. if is_javaclass(current_structdef) or
  463. ((target_info.system in systems_jvm) and
  464. is_record(current_structdef)) then
  465. begin
  466. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  467. not current_procinfo.ConstructorCallingConstructor then
  468. begin
  469. { call inherited constructor }
  470. if is_javaclass(current_structdef) then
  471. srsym:=search_struct_member_no_helper(tobjectdef(current_structdef).childof,'CREATE')
  472. else
  473. srsym:=search_struct_member_no_helper(java_fpcbaserecordtype,'CREATE');
  474. if assigned(srsym) and
  475. (srsym.typ=procsym) then
  476. begin
  477. call:=ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[cnf_inherited]);
  478. exclude(tcallnode(call).callnodeflags,cnf_return_value_used);
  479. addstatement(newstatement,call);
  480. end
  481. else
  482. internalerror(2011010312);
  483. end;
  484. end
  485. else
  486. if not is_record(current_structdef) and
  487. not (
  488. is_objectpascal_helper(current_structdef) and
  489. (tobjectdef(current_structdef).extendeddef.typ<>objectdef)
  490. ) then
  491. internalerror(200305103);
  492. { if self=nil then exit
  493. calling fail instead of exit is useless because
  494. there is nothing to dispose (PFV) }
  495. if is_class_or_object(current_structdef) then
  496. addstatement(newstatement,cifnode.create(
  497. caddnode.create(equaln,
  498. load_self_pointer_node,
  499. cnilnode.create),
  500. cexitnode.create(nil),
  501. nil));
  502. end;
  503. { maybe call BeforeDestruction for classes }
  504. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  505. is_class(current_structdef) then
  506. begin
  507. srsym:=search_struct_member(current_structdef,'BEFOREDESTRUCTION');
  508. if assigned(srsym) and
  509. (srsym.typ=procsym) then
  510. begin
  511. { if vmt>0 then beforedestruction }
  512. addstatement(newstatement,cifnode.create(
  513. caddnode.create(gtn,
  514. ctypeconvnode.create_internal(
  515. load_vmt_pointer_node,ptrsinttype),
  516. ctypeconvnode.create_internal(
  517. cnilnode.create,ptrsinttype)),
  518. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  519. nil));
  520. end
  521. else
  522. internalerror(200305104);
  523. end;
  524. end;
  525. if m_non_local_goto in current_settings.modeswitches then
  526. tsymtable(current_procinfo.procdef.localst).SymList.ForEachCall(@add_label_init,@newstatement);
  527. end;
  528. function generate_bodyexit_block:tnode;
  529. var
  530. srsym : tsym;
  531. para : tcallparanode;
  532. newstatement : tstatementnode;
  533. oldlocalswitches: tlocalswitches;
  534. begin
  535. result:=internalstatements(newstatement);
  536. if assigned(current_structdef) then
  537. begin
  538. { Don't test self and the vmt here. The reason is that }
  539. { a constructor already checks whether these are valid }
  540. { before. Further, in case of TThread the thread may }
  541. { free the class instance right after AfterConstruction }
  542. { has been called, so it may no longer be valid (JM) }
  543. oldlocalswitches:=current_settings.localswitches;
  544. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  545. { a destructor needs a help procedure }
  546. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  547. begin
  548. if is_class(current_structdef) then
  549. begin
  550. srsym:=search_struct_member(current_structdef,'FREEINSTANCE');
  551. if assigned(srsym) and
  552. (srsym.typ=procsym) then
  553. begin
  554. { if self<>0 and vmt<>0 then freeinstance }
  555. addstatement(newstatement,cifnode.create(
  556. caddnode.create(andn,
  557. caddnode.create(unequaln,
  558. load_self_pointer_node,
  559. cnilnode.create),
  560. caddnode.create(unequaln,
  561. ctypeconvnode.create(
  562. load_vmt_pointer_node,
  563. voidpointertype),
  564. cpointerconstnode.create(0,voidpointertype))),
  565. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  566. nil));
  567. end
  568. else
  569. internalerror(200305108);
  570. end
  571. else
  572. if is_object(current_structdef) then
  573. begin
  574. { finalize object data, but only if not in inherited call }
  575. if is_managed_type(current_structdef) then
  576. begin
  577. addstatement(newstatement,cifnode.create(
  578. caddnode.create(unequaln,
  579. ctypeconvnode.create_internal(load_vmt_pointer_node,voidpointertype),
  580. cnilnode.create),
  581. cnodeutils.finalize_data_node(load_self_node),
  582. nil));
  583. end;
  584. { parameter 3 : vmt_offset }
  585. { parameter 2 : pointer to vmt }
  586. { parameter 1 : self pointer }
  587. para:=ccallparanode.create(
  588. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  589. ccallparanode.create(
  590. ctypeconvnode.create_internal(
  591. load_vmt_pointer_node,
  592. voidpointertype),
  593. ccallparanode.create(
  594. ctypeconvnode.create_internal(
  595. load_self_pointer_node,
  596. voidpointertype),
  597. nil)));
  598. addstatement(newstatement,
  599. ccallnode.createintern('fpc_help_destructor',para));
  600. end
  601. else if is_javaclass(current_structdef) then
  602. begin
  603. { nothing to do }
  604. end
  605. else
  606. internalerror(200305105);
  607. end;
  608. current_settings.localswitches:=oldlocalswitches;
  609. end;
  610. end;
  611. {****************************************************************************
  612. TCGProcInfo
  613. ****************************************************************************}
  614. destructor tcgprocinfo.destroy;
  615. begin
  616. if assigned(code) then
  617. code.free;
  618. if not final_used then
  619. final_asmnode.free;
  620. inherited destroy;
  621. end;
  622. procedure tcgprocinfo.printproc(pass:string);
  623. begin
  624. assign(printnodefile,treelogfilename);
  625. {$push}{$I-}
  626. append(printnodefile);
  627. if ioresult<>0 then
  628. rewrite(printnodefile);
  629. {$pop}
  630. if ioresult<>0 then
  631. begin
  632. Comment(V_Error,'Error creating '+treelogfilename);
  633. exit;
  634. end;
  635. writeln(printnodefile);
  636. writeln(printnodefile,'*******************************************************************************');
  637. writeln(printnodefile, pass);
  638. writeln(printnodefile,procdef.fullprocname(false));
  639. writeln(printnodefile,'*******************************************************************************');
  640. printnode(printnodefile,code);
  641. close(printnodefile);
  642. end;
  643. procedure tcgprocinfo.maybe_add_constructor_wrapper(var tocode: tnode; withexceptblock: boolean);
  644. var
  645. oldlocalswitches: tlocalswitches;
  646. srsym: tsym;
  647. afterconstructionblock,
  648. exceptblock,
  649. newblock: tblocknode;
  650. newstatement: tstatementnode;
  651. pd: tprocdef;
  652. begin
  653. if assigned(procdef.struct) and
  654. (procdef.proctypeoption=potype_constructor) then
  655. begin
  656. withexceptblock:=
  657. withexceptblock and
  658. not(target_info.system in systems_garbage_collected_managed_types);
  659. { Don't test self and the vmt here. See generate_bodyexit_block }
  660. { why (JM) }
  661. oldlocalswitches:=current_settings.localswitches;
  662. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  663. { call AfterConstruction for classes }
  664. if is_class(procdef.struct) then
  665. begin
  666. srsym:=search_struct_member(procdef.struct,'AFTERCONSTRUCTION');
  667. if assigned(srsym) and
  668. (srsym.typ=procsym) then
  669. begin
  670. current_filepos:=exitpos;
  671. afterconstructionblock:=internalstatements(newstatement);
  672. { first execute all constructor code. If no exception
  673. occurred then we will execute afterconstruction,
  674. otherwise we won't (the exception will jump over us) }
  675. addstatement(newstatement,tocode);
  676. { if implicit finally node wasn't created, then exit label and
  677. finalization code must be handled here and placed before
  678. afterconstruction }
  679. if not ((pi_needs_implicit_finally in flags) and
  680. (cs_implicit_exceptions in current_settings.moduleswitches)) then
  681. begin
  682. include(tocode.flags,nf_block_with_exit);
  683. addstatement(newstatement,final_asmnode);
  684. final_used:=true;
  685. end;
  686. { Self can be nil when fail is called }
  687. { if self<>nil and vmt<>nil then afterconstruction }
  688. addstatement(newstatement,cifnode.create(
  689. caddnode.create(andn,
  690. caddnode.create(unequaln,
  691. load_self_node,
  692. cnilnode.create),
  693. caddnode.create(unequaln,
  694. load_vmt_pointer_node,
  695. cnilnode.create)),
  696. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  697. nil));
  698. tocode:=afterconstructionblock;
  699. end
  700. else
  701. internalerror(200305106);
  702. end;
  703. if withexceptblock and (procdef.struct.typ=objectdef) then
  704. begin
  705. { Generate the implicit "fail" code for a constructor (destroy
  706. in case an exception happened) }
  707. pd:=tobjectdef(procdef.struct).find_destructor;
  708. { this will always be the case for classes, since tobject has
  709. a destructor }
  710. if assigned(pd) or is_object(procdef.struct) then
  711. begin
  712. current_filepos:=exitpos;
  713. exceptblock:=internalstatements(newstatement);
  714. { first free the instance if non-nil }
  715. if assigned(pd) then
  716. { if vmt<>0 then call destructor }
  717. addstatement(newstatement,
  718. cifnode.create(
  719. caddnode.create(unequaln,
  720. load_vmt_pointer_node,
  721. cnilnode.create),
  722. { cnf_create_failed -> don't call BeforeDestruction }
  723. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[cnf_create_failed]),
  724. nil))
  725. else
  726. { object without destructor, call 'fail' helper }
  727. addstatement(newstatement,
  728. ccallnode.createintern('fpc_help_fail',
  729. ccallparanode.create(
  730. cordconstnode.create(tobjectdef(procdef.struct).vmt_offset,s32inttype,false),
  731. ccallparanode.create(
  732. ctypeconvnode.create_internal(
  733. load_vmt_pointer_node,
  734. voidpointertype),
  735. ccallparanode.create(
  736. ctypeconvnode.create_internal(
  737. load_self_pointer_node,
  738. voidpointertype),
  739. nil))))
  740. );
  741. { then re-raise the exception }
  742. addstatement(newstatement,craisenode.create(nil,nil,nil));
  743. current_filepos:=entrypos;
  744. newblock:=internalstatements(newstatement);
  745. { try
  746. tocode
  747. except
  748. exceptblock
  749. end
  750. }
  751. addstatement(newstatement,ctryexceptnode.create(
  752. tocode,
  753. nil,
  754. exceptblock));
  755. tocode:=newblock;
  756. end;
  757. end;
  758. current_settings.localswitches:=oldlocalswitches;
  759. end;
  760. end;
  761. procedure tcgprocinfo.add_entry_exit_code;
  762. var
  763. finalcode,
  764. bodyentrycode,
  765. bodyexitcode,
  766. wrappedbody,
  767. newblock : tnode;
  768. codestatement,
  769. newstatement : tstatementnode;
  770. oldfilepos : tfileposinfo;
  771. is_constructor: boolean;
  772. begin
  773. is_constructor:=assigned(procdef.struct) and
  774. (procdef.proctypeoption=potype_constructor);
  775. oldfilepos:=current_filepos;
  776. { Generate code/locations used at start of proc }
  777. current_filepos:=entrypos;
  778. entry_asmnode:=casmnode.create_get_position;
  779. loadpara_asmnode:=casmnode.create_get_position;
  780. stackcheck_asmnode:=casmnode.create_get_position;
  781. init_asmnode:=casmnode.create_get_position;
  782. bodyentrycode:=generate_bodyentry_block;
  783. { Generate code/locations used at end of proc }
  784. current_filepos:=exitpos;
  785. exitlabel_asmnode:=casmnode.create_get_position;
  786. final_asmnode:=casmnode.create_get_position;
  787. final_used:=false;
  788. bodyexitcode:=generate_bodyexit_block;
  789. { Generate procedure by combining init+body+final,
  790. depending on the implicit finally we need to add
  791. an try...finally...end wrapper }
  792. newblock:=internalstatements(newstatement);
  793. { initialization is common for all cases }
  794. addstatement(newstatement,loadpara_asmnode);
  795. addstatement(newstatement,stackcheck_asmnode);
  796. addstatement(newstatement,entry_asmnode);
  797. addstatement(newstatement,init_asmnode);
  798. addstatement(newstatement,bodyentrycode);
  799. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  800. (pi_needs_implicit_finally in flags) and
  801. { but it's useless in init/final code of units }
  802. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  803. not(po_assembler in procdef.procoptions) and
  804. not(target_info.system in systems_garbage_collected_managed_types) then
  805. begin
  806. { Any result of managed type must be returned in parameter }
  807. if is_managed_type(procdef.returndef) and
  808. (not paramanager.ret_in_param(procdef.returndef,procdef)) and
  809. (not is_class(procdef.returndef)) then
  810. InternalError(2013121301);
  811. { Generate special exception block only needed when
  812. implicit finaly is used }
  813. current_filepos:=exitpos;
  814. { Generate code that will be in the try...finally }
  815. finalcode:=internalstatements(codestatement);
  816. addstatement(codestatement,final_asmnode);
  817. final_used:=true;
  818. current_filepos:=entrypos;
  819. wrappedbody:=ctryfinallynode.create_implicit(
  820. code,
  821. finalcode,
  822. cnothingnode.create);
  823. { afterconstruction must be called after final_asmnode, because it
  824. has to execute after the temps have been finalised in case of a
  825. refcounted class (afterconstruction decreases the refcount
  826. without freeing the instance if the count becomes nil, while
  827. the finalising of the temps can free the instance) }
  828. maybe_add_constructor_wrapper(wrappedbody,true);
  829. addstatement(newstatement,wrappedbody);
  830. addstatement(newstatement,exitlabel_asmnode);
  831. addstatement(newstatement,bodyexitcode);
  832. { set flag the implicit finally has been generated }
  833. include(flags,pi_has_implicit_finally);
  834. end
  835. else
  836. begin
  837. { constructors need destroy-on-exception code even if they don't
  838. have managed variables/temps }
  839. maybe_add_constructor_wrapper(code,
  840. cs_implicit_exceptions in current_settings.moduleswitches);
  841. addstatement(newstatement,code);
  842. if assigned(nestedexitlabel) then
  843. addstatement(newstatement,clabelnode.create(cnothingnode.create,nestedexitlabel));
  844. addstatement(newstatement,exitlabel_asmnode);
  845. addstatement(newstatement,bodyexitcode);
  846. if not is_constructor then
  847. begin
  848. addstatement(newstatement,final_asmnode);
  849. final_used:=true;
  850. end;
  851. end;
  852. do_firstpass(newblock);
  853. code:=newblock;
  854. current_filepos:=oldfilepos;
  855. end;
  856. procedure clearrefs(p:TObject;arg:pointer);
  857. begin
  858. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  859. if tabstractvarsym(p).refs>1 then
  860. tabstractvarsym(p).refs:=1;
  861. end;
  862. procedure translate_registers(p:TObject;list:pointer);
  863. begin
  864. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  865. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  866. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  867. begin
  868. if not(cs_no_regalloc in current_settings.globalswitches) then
  869. begin
  870. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  871. if (tabstractnormalvarsym(p).localloc.registerhi<>NR_NO) then
  872. cg.translate_register(tabstractnormalvarsym(p).localloc.registerhi);
  873. end;
  874. if cs_asm_source in current_settings.globalswitches then
  875. begin
  876. TAsmList(list).concat(Tai_comment.Create(strpnew('Var '+tabstractnormalvarsym(p).realname+' located in register '+
  877. location_reg2string(tabstractnormalvarsym(p).localloc))));
  878. end;
  879. end;
  880. end;
  881. const
  882. exception_flags: array[boolean] of tprocinfoflags = (
  883. [],
  884. [pi_uses_exceptions,pi_needs_implicit_finally,pi_has_implicit_finally]
  885. );
  886. procedure tcgprocinfo.setup_tempgen;
  887. begin
  888. tg:=tgobjclass.create;
  889. {$if defined(i386) or defined(x86_64) or defined(arm)}
  890. {$if defined(arm)}
  891. { frame and stack pointer must be always the same on arm thumb so it makes no
  892. sense to fiddle with a frame pointer }
  893. if GenerateThumbCode then
  894. begin
  895. framepointer:=NR_STACK_POINTER_REG;
  896. tg.direction:=1;
  897. end
  898. else
  899. {$endif defined(arm)}
  900. begin
  901. { try to strip the stack frame }
  902. { set the framepointer to esp if:
  903. - no assembler directive, those are handled in assembler_block
  904. in pstatment.pas (for cases not caught by the Delphi
  905. exception below)
  906. - no exceptions are used
  907. - no pushes are used/esp modifications, could be:
  908. * outgoing parameters on the stack on non-fixed stack target
  909. * incoming parameters on the stack
  910. * open arrays
  911. - no inline assembler
  912. or
  913. - Delphi mode
  914. - assembler directive
  915. - no pushes are used/esp modifications, could be:
  916. * outgoing parameters on the stack
  917. * incoming parameters on the stack
  918. * open arrays
  919. - no local variables
  920. - stack frame cannot be optimized if using Win64 SEH
  921. (at least with the current state of our codegenerator).
  922. }
  923. if ((po_assembler in procdef.procoptions) and
  924. (m_delphi in current_settings.modeswitches) and
  925. { localst at main_program_level is a staticsymtable }
  926. (procdef.localst.symtablelevel<>main_program_level) and
  927. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  928. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  929. not(cs_generate_stackframes in current_settings.localswitches) and
  930. not(cs_profile in current_settings.moduleswitches) and
  931. not(po_assembler in procdef.procoptions) and
  932. not ((pi_has_stackparameter in flags)
  933. {$ifndef arm} { Outgoing parameter(s) on stack do not need stackframe on x86 targets
  934. with fixed stack. On ARM it fails, see bug #25050 }
  935. and (not paramanager.use_fixed_stack)
  936. {$endif arm}
  937. ) and
  938. ((flags*([pi_has_assembler_block,pi_is_assembler,
  939. pi_needs_stackframe]+
  940. exception_flags[(target_info.cpu=cpu_i386)
  941. {$ifndef DISABLE_WIN64_SEH}
  942. or (target_info.system=system_x86_64_win64)
  943. {$endif DISABLE_WIN64_SEH}
  944. ]))=[])
  945. )
  946. then
  947. begin
  948. { we need the parameter info here to determine if the procedure gets
  949. parameters on the stack
  950. calling generate_parameter_info doesn't hurt but it costs time
  951. (necessary to init para_stack_size)
  952. }
  953. generate_parameter_info;
  954. if not(procdef.stack_tainting_parameter(calleeside)) and
  955. not(has_assembler_child) and (para_stack_size=0) then
  956. begin
  957. { Only need to set the framepointer }
  958. framepointer:=NR_STACK_POINTER_REG;
  959. tg.direction:=1;
  960. end
  961. {$if defined(arm)}
  962. { On arm, the stack frame size can be estimated to avoid using an extra frame pointer,
  963. in case parameters are passed on the stack.
  964. However, the draw back is, if the estimation fails, compilation will break later on
  965. with an internal error, so this switch is not enabled by default yet. To overcome this,
  966. multipass compilation of subroutines must be supported
  967. }
  968. else if (cs_opt_forcenostackframe in current_settings.optimizerswitches) and
  969. not(has_assembler_child) then
  970. begin
  971. { Only need to set the framepointer }
  972. framepointer:=NR_STACK_POINTER_REG;
  973. tg.direction:=1;
  974. include(flags,pi_estimatestacksize);
  975. set_first_temp_offset;
  976. procdef.has_paraloc_info:=callnoside;
  977. generate_parameter_info;
  978. exit;
  979. end;
  980. {$endif defined(arm)}
  981. end;
  982. end;
  983. {$endif defined(x86) or defined(arm)}
  984. { set the start offset to the start of the temp area in the stack }
  985. set_first_temp_offset;
  986. end;
  987. function tcgprocinfo.has_assembler_child : boolean;
  988. var
  989. hp : tprocinfo;
  990. begin
  991. result:=false;
  992. hp:=get_first_nestedproc;
  993. while assigned(hp) do
  994. begin
  995. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  996. begin
  997. result:=true;
  998. exit;
  999. end;
  1000. hp:=tprocinfo(hp.next);
  1001. end;
  1002. end;
  1003. procedure tcgprocinfo.generate_code_tree;
  1004. var
  1005. hpi : tcgprocinfo;
  1006. begin
  1007. { generate code for this procedure }
  1008. generate_code;
  1009. { process nested procedures }
  1010. hpi:=tcgprocinfo(get_first_nestedproc);
  1011. while assigned(hpi) do
  1012. begin
  1013. hpi.generate_code_tree;
  1014. hpi:=tcgprocinfo(hpi.next);
  1015. end;
  1016. resetprocdef;
  1017. end;
  1018. { For SEH, the code from 'finally' blocks must be put into a separate procedures,
  1019. which can be called by OS during stack unwind. This resembles nested procedures,
  1020. but finalizer procedures do not have their own local variables and work directly
  1021. with the stack frame of parent. In particular, the tempgen must be shared, so
  1022. 1) finalizer procedure is able to finalize temps of the parent,
  1023. 2) if the finalizer procedure is complex enough to need its own temps, they are
  1024. allocated in stack frame of parent, so second-level finalizer procedures are
  1025. not needed.
  1026. Due to requirement of shared tempgen we cannot process finalizer as a regular nested
  1027. procedure (after the parent) and have to do it inline.
  1028. This is called by platform-specific tryfinallynodes during pass2.
  1029. Here we put away the codegen (which carries the register allocator state), process
  1030. the 'nested' procedure, then restore previous cg and continue processing the parent
  1031. procedure. generate_code() will create another cg, but not another tempgen because
  1032. setup_tempgen() is not called for potype_exceptfilter procedures. }
  1033. procedure tcgprocinfo.generate_exceptfilter(nestedpi: tcgprocinfo);
  1034. var
  1035. saved_cg: tcg;
  1036. saved_hlcg: thlcgobj;
  1037. {$ifdef cpu64bitalu}
  1038. saved_cg128 : tcg128;
  1039. {$else cpu64bitalu}
  1040. saved_cg64 : tcg64;
  1041. {$endif cpu64bitalu}
  1042. begin
  1043. if nestedpi.procdef.proctypeoption<>potype_exceptfilter then
  1044. InternalError(201201141);
  1045. { flush code generated this far }
  1046. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1047. { save the codegen }
  1048. saved_cg:=cg;
  1049. saved_hlcg:=hlcg;
  1050. cg:=nil;
  1051. hlcg:=nil;
  1052. {$ifdef cpu64bitalu}
  1053. saved_cg128:=cg128;
  1054. cg128:=nil;
  1055. {$else cpu64bitalu}
  1056. saved_cg64:=cg64;
  1057. cg64:=nil;
  1058. {$endif cpu64bitalu}
  1059. nestedpi.generate_code;
  1060. { prevents generating code the second time when processing nested procedures }
  1061. nestedpi.resetprocdef;
  1062. cg:=saved_cg;
  1063. hlcg:=saved_hlcg;
  1064. {$ifdef cpu64bitalu}
  1065. cg128:=saved_cg128;
  1066. {$else cpu64bitalu}
  1067. cg64:=saved_cg64;
  1068. {$endif cpu64bitalu}
  1069. add_reg_instruction_hook:[email protected]_reg_instruction;
  1070. end;
  1071. procedure TCGProcinfo.CreateInlineInfo;
  1072. begin
  1073. new(procdef.inlininginfo);
  1074. procdef.inlininginfo^.code:=code.getcopy;
  1075. procdef.inlininginfo^.flags:=flags;
  1076. { The blocknode needs to set an exit label }
  1077. if procdef.inlininginfo^.code.nodetype=blockn then
  1078. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1079. procdef.has_inlininginfo:=true;
  1080. end;
  1081. function searchusercode(var n: tnode; arg: pointer): foreachnoderesult;
  1082. begin
  1083. if nf_usercode_entry in n.flags then
  1084. begin
  1085. pnode(arg)^:=n;
  1086. result:=fen_norecurse_true
  1087. end
  1088. else
  1089. result:=fen_false;
  1090. end;
  1091. function TCGProcinfo.GetUserCode : tnode;
  1092. var
  1093. n : tnode;
  1094. begin
  1095. n:=nil;
  1096. foreachnodestatic(code,@searchusercode,@n);
  1097. if not(assigned(n)) then
  1098. internalerror(2013111004);
  1099. result:=n;
  1100. end;
  1101. procedure tcgprocinfo.generate_code;
  1102. var
  1103. old_current_procinfo : tprocinfo;
  1104. oldmaxfpuregisters : longint;
  1105. oldfilepos : tfileposinfo;
  1106. old_current_structdef : tabstractrecorddef;
  1107. templist : TAsmList;
  1108. headertai : tai;
  1109. i : integer;
  1110. varsym : tabstractnormalvarsym;
  1111. {RedoDFA : boolean;}
  1112. procedure delete_marker(anode: tasmnode);
  1113. var
  1114. ai: tai;
  1115. begin
  1116. if assigned(anode) then
  1117. begin
  1118. ai:=anode.currenttai;
  1119. if assigned(ai) then
  1120. begin
  1121. aktproccode.remove(ai);
  1122. ai.free;
  1123. anode.currenttai:=nil;
  1124. end;
  1125. end;
  1126. end;
  1127. begin
  1128. { the initialization procedure can be empty, then we
  1129. don't need to generate anything. When it was an empty
  1130. procedure there would be at least a blocknode }
  1131. if not assigned(code) then
  1132. exit;
  1133. { We need valid code }
  1134. if Errorcount<>0 then
  1135. exit;
  1136. { No code can be generated for generic template }
  1137. if (df_generic in procdef.defoptions) then
  1138. internalerror(200511152);
  1139. { For regular procedures the RA and Tempgen shall not be available yet,
  1140. but exception filters reuse Tempgen of parent }
  1141. if assigned(tg)<>(procdef.proctypeoption=potype_exceptfilter) then
  1142. internalerror(200309201);
  1143. old_current_procinfo:=current_procinfo;
  1144. oldfilepos:=current_filepos;
  1145. old_current_structdef:=current_structdef;
  1146. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  1147. current_procinfo:=self;
  1148. current_filepos:=entrypos;
  1149. current_structdef:=procdef.struct;
  1150. { store start of user code, it must be a block node, it will be used later one to
  1151. check variable lifeness }
  1152. include(code.flags,nf_usercode_entry);
  1153. { add wrapping code if necessary (initialization of typed constants on
  1154. some platforms, initing of local variables and out parameters with
  1155. trashing values, ...) }
  1156. { init/final code must be wrapped later (after code for main proc body
  1157. has been generated) }
  1158. if not(current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1159. code:=cnodeutils.wrap_proc_body(procdef,code);
  1160. { automatic inlining? }
  1161. if (cs_opt_autoinline in current_settings.optimizerswitches) and
  1162. { inlining not turned off? }
  1163. (cs_do_inline in current_settings.localswitches) and
  1164. { no inlining yet? }
  1165. not(procdef.has_inlininginfo) and not(has_nestedprocs) and
  1166. not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize,potype_constructor,
  1167. potype_destructor,potype_class_constructor,potype_class_destructor]) and
  1168. ((procdef.procoptions*[po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck])=[]) and
  1169. (not(procdef.proccalloption in [pocall_safecall])) and
  1170. { rough approximation if we should auto inline }
  1171. (node_count(code)<=10) then
  1172. begin
  1173. { Can we inline this procedure? }
  1174. if checknodeinlining(procdef) then
  1175. begin
  1176. Message1(cg_d_autoinlining,procdef.GetTypeName);
  1177. include(procdef.procoptions,po_inline);
  1178. CreateInlineInfo;
  1179. end;
  1180. end;
  1181. templist:=TAsmList.create;
  1182. { add parast/localst to symtablestack }
  1183. add_to_symtablestack;
  1184. { clear register count }
  1185. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  1186. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  1187. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  1188. if (procdef.localst.symtablelevel=main_program_level) and
  1189. (not current_module.is_unit) then
  1190. begin
  1191. include(flags,pi_do_call);
  1192. { the main program never returns due to the do_exit call }
  1193. if not(DLLsource) then
  1194. include(procdef.procoptions,po_noreturn);
  1195. end;
  1196. { set implicit_finally flag when there are locals/paras to be finalized }
  1197. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  1198. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  1199. {$ifdef SUPPORT_SAFECALL}
  1200. { set implicit_finally flag for if procedure is safecall }
  1201. if (tf_safecall_exceptions in target_info.flags) and
  1202. (procdef.proccalloption=pocall_safecall) then
  1203. include(flags, pi_needs_implicit_finally);
  1204. {$endif}
  1205. { firstpass everything }
  1206. flowcontrol:=[];
  1207. do_firstpass(code);
  1208. {$if defined(i386) or defined(i8086)}
  1209. if node_resources_fpu(code)>0 then
  1210. include(flags,pi_uses_fpu);
  1211. {$endif i386 or i8086}
  1212. { Print the node to tree.log }
  1213. if paraprintnodetree=1 then
  1214. printproc( 'after the firstpass');
  1215. { do this before adding the entry code else the tail recursion recognition won't work,
  1216. if this causes troubles, it must be if'ed
  1217. }
  1218. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  1219. (pi_is_recursive in flags) then
  1220. do_opttail(code,procdef);
  1221. if cs_opt_constant_propagate in current_settings.optimizerswitches then
  1222. do_optconstpropagate(code);
  1223. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  1224. { creating dfa is not always possible }
  1225. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler])=[]) then
  1226. begin
  1227. dfabuilder:=TDFABuilder.Create;
  1228. dfabuilder.createdfainfo(code);
  1229. include(flags,pi_dfaavailable);
  1230. { when life info is available, we can give more sophisticated warning about uninitialized
  1231. variables ...
  1232. ... but not for the finalization section of a unit, we would need global dfa to handle
  1233. it properly }
  1234. if potype_unitfinalize<>procdef.proctypeoption then
  1235. { iterate through life info of the first node }
  1236. for i:=0 to dfabuilder.nodemap.count-1 do
  1237. begin
  1238. if DFASetIn(GetUserCode.optinfo^.life,i) then
  1239. begin
  1240. { do not warn for certain parameters: }
  1241. if not((tnode(dfabuilder.nodemap[i]).nodetype=loadn) and (tloadnode(dfabuilder.nodemap[i]).symtableentry.typ=paravarsym) and
  1242. { do not warn about parameters passed by var }
  1243. (((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varspez=vs_var) and
  1244. { function result is passed by var but it must be initialized }
  1245. not(vo_is_funcret in tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions)) or
  1246. { do not warn about initialized hidden parameters }
  1247. ((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions*[vo_is_high_para,vo_is_parentfp,vo_is_result,vo_is_self])<>[]))) then
  1248. CheckAndWarn(GetUserCode,tnode(dfabuilder.nodemap[i]));
  1249. end;
  1250. end;
  1251. end;
  1252. if (pi_dfaavailable in flags) and (cs_opt_dead_store_eliminate in current_settings.optimizerswitches) then
  1253. do_optdeadstoreelim(code);
  1254. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  1255. { our induction variable strength reduction doesn't like
  1256. for loops with more than one entry }
  1257. and not(pi_has_label in flags) then
  1258. begin
  1259. {RedoDFA:=}OptimizeInductionVariables(code);
  1260. end;
  1261. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  1262. (procdef.proctypeoption in [potype_operator,potype_procedure,potype_function]) and
  1263. (code.nodetype=blockn) and (tblocknode(code).statements=nil) then
  1264. procdef.isempty:=true;
  1265. { add implicit entry and exit code }
  1266. add_entry_exit_code;
  1267. if cs_opt_nodecse in current_settings.optimizerswitches then
  1268. do_optcse(code);
  1269. { only do secondpass if there are no errors }
  1270. if (ErrorCount=0) then
  1271. begin
  1272. create_hlcodegen;
  1273. if (procdef.proctypeoption<>potype_exceptfilter) then
  1274. setup_tempgen;
  1275. { Create register allocator, must come after framepointer is known }
  1276. hlcg.init_register_allocators;
  1277. generate_parameter_info;
  1278. { allocate got register if needed }
  1279. allocate_got_register(aktproccode);
  1280. { Allocate space in temp/registers for parast and localst }
  1281. current_filepos:=entrypos;
  1282. gen_alloc_symtable(aktproccode,procdef,procdef.parast);
  1283. gen_alloc_symtable(aktproccode,procdef,procdef.localst);
  1284. { Store temp offset for information about 'real' temps }
  1285. tempstart:=tg.lasttemp;
  1286. { Generate code to load register parameters in temps and insert local
  1287. copies for values parameters. This must be done before the code for the
  1288. body is generated because the localloc is updated.
  1289. Note: The generated code will be inserted after the code generation of
  1290. the body is finished, because only then the position is known }
  1291. {$ifdef oldregvars}
  1292. assign_regvars(code);
  1293. {$endif oldreg}
  1294. current_filepos:=entrypos;
  1295. hlcg.gen_load_para_value(templist);
  1296. { caller paraloc info is also necessary in the stackframe_entry
  1297. code of the ppc (and possibly other processors) }
  1298. procdef.init_paraloc_info(callerside);
  1299. { generate code for the node tree }
  1300. do_secondpass(code);
  1301. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1302. { The position of the loadpara_asmnode is now known }
  1303. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  1304. { first generate entry and initialize code with the correct
  1305. position and switches }
  1306. current_filepos:=entrypos;
  1307. current_settings.localswitches:=entryswitches;
  1308. cg.set_regalloc_live_range_direction(rad_backwards);
  1309. hlcg.gen_entry_code(templist);
  1310. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  1311. hlcg.gen_initialize_code(templist);
  1312. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  1313. { now generate finalize and exit code with the correct position
  1314. and switches }
  1315. current_filepos:=exitpos;
  1316. current_settings.localswitches:=exitswitches;
  1317. cg.set_regalloc_live_range_direction(rad_forward);
  1318. if assigned(finalize_procinfo) then
  1319. generate_exceptfilter(tcgprocinfo(finalize_procinfo))
  1320. else
  1321. begin
  1322. hlcg.gen_finalize_code(templist);
  1323. { the finalcode must be concated if there was no position available,
  1324. using insertlistafter will result in an insert at the start
  1325. when currentai=nil }
  1326. if assigned(final_asmnode) and assigned(final_asmnode.currenttai) then
  1327. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  1328. else
  1329. aktproccode.concatlist(templist);
  1330. end;
  1331. { insert exit label at the correct position }
  1332. hlcg.a_label(templist,CurrExitLabel);
  1333. if assigned(exitlabel_asmnode.currenttai) then
  1334. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  1335. else
  1336. aktproccode.concatlist(templist);
  1337. { exit code }
  1338. hlcg.gen_exit_code(templist);
  1339. aktproccode.concatlist(templist);
  1340. {$ifdef OLDREGVARS}
  1341. { note: this must be done only after as much code as possible has }
  1342. { been generated. The result is that when you ungetregister() a }
  1343. { regvar, it will actually free the regvar (and alse free the }
  1344. { the regvars at the same time). Doing this too early will }
  1345. { confuse the register allocator, as the regvars will still be }
  1346. { used. It should be done before loading the result regs (so }
  1347. { they don't conflict with the regvars) and before }
  1348. { gen_entry_code (that one has to be able to allocate the }
  1349. { regvars again) (JM) }
  1350. free_regvars(aktproccode);
  1351. {$endif OLDREGVARS}
  1352. { generate symbol and save end of header position }
  1353. current_filepos:=entrypos;
  1354. hlcg.gen_proc_symbol(templist);
  1355. headertai:=tai(templist.last);
  1356. { insert symbol }
  1357. aktproccode.insertlist(templist);
  1358. { Free space in temp/registers for parast and localst, must be
  1359. done after gen_entry_code }
  1360. current_filepos:=exitpos;
  1361. { make sure the got/pic register doesn't get freed in the }
  1362. { middle of a loop }
  1363. if (cs_create_pic in current_settings.moduleswitches) and
  1364. (pi_needs_got in flags) and
  1365. (got<>NR_NO) then
  1366. cg.a_reg_sync(aktproccode,got);
  1367. gen_free_symtable(aktproccode,procdef.localst);
  1368. gen_free_symtable(aktproccode,procdef.parast);
  1369. { add code that will load the return value, this is not done
  1370. for assembler routines when they didn't reference the result
  1371. variable }
  1372. hlcg.gen_load_return_value(templist);
  1373. aktproccode.concatlist(templist);
  1374. { Already reserve all registers for stack checking code and
  1375. generate the call to the helper function }
  1376. if not(tf_no_generic_stackcheck in target_info.flags) and
  1377. (cs_check_stack in entryswitches) and
  1378. not(po_assembler in procdef.procoptions) and
  1379. (procdef.proctypeoption<>potype_proginit) then
  1380. begin
  1381. current_filepos:=entrypos;
  1382. gen_stack_check_call(templist);
  1383. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1384. end;
  1385. { this code (got loading) comes before everything which has }
  1386. { already been generated, so reset the info about already }
  1387. { backwards extended registers (so their live range can be }
  1388. { extended backwards even further if needed) }
  1389. { This code must be }
  1390. { a) generated after do_secondpass has been called }
  1391. { (because pi_needs_got may be set there) }
  1392. { b) generated before register allocation, because the }
  1393. { got/pic register can be a virtual one }
  1394. { c) inserted before the entry code, because the entry }
  1395. { code may need global symbols such as init rtti }
  1396. { d) inserted after the stackframe allocation, because }
  1397. { this register may have to be spilled }
  1398. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  1399. current_filepos:=entrypos;
  1400. { load got if necessary }
  1401. cg.g_maybe_got_init(templist);
  1402. aktproccode.insertlistafter(headertai,templist);
  1403. { re-enable if more code at the end is ever generated here
  1404. cg.set_regalloc_live_range_direction(rad_forward);
  1405. }
  1406. {$ifndef NoOpt}
  1407. {$ifndef i386}
  1408. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1409. { do not optimize pure assembler procedures }
  1410. not(pi_is_assembler in flags) then
  1411. preregallocschedule(aktproccode);
  1412. {$endif i386}
  1413. {$endif NoOpt}
  1414. { The procedure body is finished, we can now
  1415. allocate the registers }
  1416. cg.do_register_allocation(aktproccode,headertai);
  1417. { translate imag. register to their real counter parts
  1418. this is necessary for debuginfo and verbose assembler output
  1419. when SSA will be implented, this will be more complicated because we've to
  1420. maintain location lists }
  1421. procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  1422. procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  1423. if (cs_create_pic in current_settings.moduleswitches) and
  1424. (pi_needs_got in flags) and
  1425. not(cs_no_regalloc in current_settings.globalswitches) and
  1426. (got<>NR_NO) then
  1427. cg.translate_register(got);
  1428. { Add save and restore of used registers }
  1429. current_filepos:=entrypos;
  1430. gen_save_used_regs(templist);
  1431. { Remember the last instruction of register saving block
  1432. (may be =nil for e.g. assembler procedures) }
  1433. endprologue_ai:=templist.last;
  1434. aktproccode.insertlistafter(headertai,templist);
  1435. current_filepos:=exitpos;
  1436. gen_restore_used_regs(aktproccode);
  1437. { We know the size of the stack, now we can generate the
  1438. parameter that is passed to the stack checking code }
  1439. if not(tf_no_generic_stackcheck in target_info.flags) and
  1440. (cs_check_stack in entryswitches) and
  1441. not(po_assembler in procdef.procoptions) and
  1442. (procdef.proctypeoption<>potype_proginit) then
  1443. begin
  1444. current_filepos:=entrypos;
  1445. gen_stack_check_size_para(templist);
  1446. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1447. end;
  1448. { Add entry code (stack allocation) after header }
  1449. current_filepos:=entrypos;
  1450. gen_proc_entry_code(templist);
  1451. aktproccode.insertlistafter(headertai,templist);
  1452. {$ifdef SUPPORT_SAFECALL}
  1453. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  1454. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  1455. (tf_safecall_exceptions in target_info.flags) and
  1456. (procdef.proccalloption=pocall_safecall) then
  1457. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  1458. {$endif}
  1459. { Add exit code at the end }
  1460. current_filepos:=exitpos;
  1461. gen_proc_exit_code(templist);
  1462. aktproccode.concatlist(templist);
  1463. { check if the implicit finally has been generated. The flag
  1464. should already be set in pass1 }
  1465. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  1466. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  1467. (pi_needs_implicit_finally in flags) and
  1468. not(po_assembler in procdef.procoptions) and
  1469. not(pi_has_implicit_finally in flags) and
  1470. not(target_info.system in systems_garbage_collected_managed_types) then
  1471. internalerror(200405231);
  1472. { Position markers are only used to insert additional code after the secondpass
  1473. and before this point. They are of no use in optimizer. Instead of checking and
  1474. ignoring all over the optimizer, just remove them here. }
  1475. delete_marker(entry_asmnode);
  1476. delete_marker(loadpara_asmnode);
  1477. delete_marker(exitlabel_asmnode);
  1478. delete_marker(stackcheck_asmnode);
  1479. delete_marker(init_asmnode);
  1480. delete_marker(final_asmnode);
  1481. {$ifndef NoOpt}
  1482. if not(cs_no_regalloc in current_settings.globalswitches) then
  1483. begin
  1484. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1485. { do not optimize pure assembler procedures }
  1486. not(pi_is_assembler in flags) then
  1487. optimize(aktproccode);
  1488. {$ifndef i386}
  1489. { schedule after assembler optimization, it could have brought up
  1490. new schedule possibilities }
  1491. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1492. { do not optimize pure assembler procedures }
  1493. not(pi_is_assembler in flags) then
  1494. preregallocschedule(aktproccode);
  1495. {$endif i386}
  1496. end;
  1497. {$endif NoOpt}
  1498. { Perform target-specific processing if necessary }
  1499. postprocess_code;
  1500. { Add end symbol and debug info }
  1501. { this must be done after the pcrelativedata is appended else the distance calculation of
  1502. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  1503. so it should be inserted before the end symbol (FK)
  1504. }
  1505. current_filepos:=exitpos;
  1506. hlcg.gen_proc_symbol_end(templist);
  1507. aktproccode.concatlist(templist);
  1508. { insert line debuginfo }
  1509. if (cs_debuginfo in current_settings.moduleswitches) or
  1510. (cs_use_lineinfo in current_settings.globalswitches) then
  1511. current_debuginfo.insertlineinfo(aktproccode);
  1512. hlcg.record_generated_code_for_procdef(current_procinfo.procdef,aktproccode,aktlocaldata);
  1513. { only now we can remove the temps }
  1514. if (procdef.proctypeoption<>potype_exceptfilter) then
  1515. begin
  1516. tg.resettempgen;
  1517. tg.free;
  1518. tg:=nil;
  1519. end;
  1520. { stop tempgen and ra }
  1521. hlcg.done_register_allocators;
  1522. destroy_hlcodegen;
  1523. end;
  1524. dfabuilder.free;
  1525. { restore symtablestack }
  1526. remove_from_symtablestack;
  1527. { restore }
  1528. templist.free;
  1529. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1530. current_filepos:=oldfilepos;
  1531. current_structdef:=old_current_structdef;
  1532. current_procinfo:=old_current_procinfo;
  1533. end;
  1534. procedure tcgprocinfo.add_to_symtablestack;
  1535. begin
  1536. { insert symtables for the class, but only if it is no nested function }
  1537. if assigned(procdef.struct) and
  1538. not(assigned(parent) and
  1539. assigned(parent.procdef) and
  1540. assigned(parent.procdef.struct)) then
  1541. push_nested_hierarchy(procdef.struct);
  1542. { insert parasymtable in symtablestack when parsing
  1543. a function }
  1544. if procdef.parast.symtablelevel>=normal_function_level then
  1545. symtablestack.push(procdef.parast);
  1546. { insert localsymtable, except for the main procedure
  1547. (in that case the localst is the unit's static symtable,
  1548. which is already on the stack) }
  1549. if procdef.localst.symtablelevel>=normal_function_level then
  1550. symtablestack.push(procdef.localst);
  1551. end;
  1552. procedure tcgprocinfo.remove_from_symtablestack;
  1553. begin
  1554. { remove localsymtable }
  1555. if procdef.localst.symtablelevel>=normal_function_level then
  1556. symtablestack.pop(procdef.localst);
  1557. { remove parasymtable }
  1558. if procdef.parast.symtablelevel>=normal_function_level then
  1559. symtablestack.pop(procdef.parast);
  1560. { remove symtables for the class, but only if it is no nested function }
  1561. if assigned(procdef.struct) and
  1562. not(assigned(parent) and
  1563. assigned(parent.procdef) and
  1564. assigned(parent.procdef.struct)) then
  1565. pop_nested_hierarchy(procdef.struct);
  1566. end;
  1567. procedure tcgprocinfo.resetprocdef;
  1568. begin
  1569. { remove code tree, if not inline procedure }
  1570. if assigned(code) then
  1571. begin
  1572. { the inline procedure has already got a copy of the tree
  1573. stored in procdef.inlininginfo }
  1574. code.free;
  1575. code:=nil;
  1576. end;
  1577. end;
  1578. procedure tcgprocinfo.parse_body;
  1579. var
  1580. old_current_procinfo : tprocinfo;
  1581. old_block_type : tblock_type;
  1582. st : TSymtable;
  1583. old_current_structdef: tabstractrecorddef;
  1584. old_current_genericdef,
  1585. old_current_specializedef: tstoreddef;
  1586. old_parse_generic: boolean;
  1587. begin
  1588. old_current_procinfo:=current_procinfo;
  1589. old_block_type:=block_type;
  1590. old_current_structdef:=current_structdef;
  1591. old_current_genericdef:=current_genericdef;
  1592. old_current_specializedef:=current_specializedef;
  1593. old_parse_generic:=parse_generic;
  1594. current_procinfo:=self;
  1595. current_structdef:=procdef.struct;
  1596. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1597. begin
  1598. current_genericdef:=current_structdef;
  1599. parse_generic:=true;
  1600. end;
  1601. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1602. current_specializedef:=current_structdef;
  1603. { calculate the lexical level }
  1604. if procdef.parast.symtablelevel>maxnesting then
  1605. Message(parser_e_too_much_lexlevel);
  1606. block_type:=bt_body;
  1607. {$ifdef state_tracking}
  1608. { aktstate:=Tstate_storage.create;}
  1609. {$endif state_tracking}
  1610. { allocate the symbol for this procedure }
  1611. alloc_proc_symbol(procdef);
  1612. { add parast/localst to symtablestack }
  1613. add_to_symtablestack;
  1614. { save entry info }
  1615. entrypos:=current_filepos;
  1616. entryswitches:=current_settings.localswitches;
  1617. if (df_generic in procdef.defoptions) then
  1618. begin
  1619. { start token recorder for generic template }
  1620. procdef.initgeneric;
  1621. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1622. end;
  1623. { parse the code ... }
  1624. code:=block(current_module.islibrary);
  1625. if (df_generic in procdef.defoptions) then
  1626. begin
  1627. { stop token recorder for generic template }
  1628. current_scanner.stoprecordtokens;
  1629. { Give an error for accesses in the static symtable that aren't visible
  1630. outside the current unit }
  1631. st:=procdef.owner;
  1632. while (st.symtabletype=ObjectSymtable) do
  1633. st:=st.defowner.owner;
  1634. if (pi_uses_static_symtable in flags) and
  1635. (st.symtabletype<>staticsymtable) then
  1636. Comment(V_Error,'Global Generic template references static symtable');
  1637. end;
  1638. { save exit info }
  1639. exitswitches:=current_settings.localswitches;
  1640. exitpos:=last_endtoken_filepos;
  1641. { the procedure is now defined }
  1642. procdef.forwarddef:=false;
  1643. if assigned(code) then
  1644. begin
  1645. { get a better entry point }
  1646. entrypos:=code.fileinfo;
  1647. { Finish type checking pass }
  1648. do_typecheckpass(code);
  1649. if assigned(procdef.parentfpinitblock) then
  1650. begin
  1651. tblocknode(code).left:=cstatementnode.create(procdef.parentfpinitblock,tblocknode(code).left);
  1652. do_typecheckpass(tblocknode(code).left);
  1653. procdef.parentfpinitblock:=nil;
  1654. end;
  1655. end;
  1656. { Check for unused labels, forwards, symbols for procedures. Static
  1657. symtable is checked in pmodules.
  1658. The check must be done after the typecheckpass }
  1659. if (Errorcount=0) and
  1660. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1661. begin
  1662. { check if forwards are resolved }
  1663. tstoredsymtable(procdef.localst).check_forwards;
  1664. { check if all labels are used }
  1665. tstoredsymtable(procdef.localst).checklabels;
  1666. { check for unused symbols, but only if there is no asm block }
  1667. if not(pi_has_assembler_block in flags) then
  1668. begin
  1669. tstoredsymtable(procdef.localst).allsymbolsused;
  1670. tstoredsymtable(procdef.parast).allsymbolsused;
  1671. end;
  1672. end;
  1673. if (po_inline in procdef.procoptions) and
  1674. { Can we inline this procedure? }
  1675. checknodeinlining(procdef) then
  1676. CreateInlineInfo;
  1677. { Print the node to tree.log }
  1678. if paraprintnodetree=1 then
  1679. printproc( 'after parsing');
  1680. { ... remove symbol tables }
  1681. remove_from_symtablestack;
  1682. {$ifdef state_tracking}
  1683. { aktstate.destroy;}
  1684. {$endif state_tracking}
  1685. current_structdef:=old_current_structdef;
  1686. current_genericdef:=old_current_genericdef;
  1687. current_specializedef:=old_current_specializedef;
  1688. current_procinfo:=old_current_procinfo;
  1689. parse_generic:=old_parse_generic;
  1690. { Restore old state }
  1691. block_type:=old_block_type;
  1692. end;
  1693. {****************************************************************************
  1694. PROCEDURE/FUNCTION PARSING
  1695. ****************************************************************************}
  1696. procedure check_init_paras(p:TObject;arg:pointer);
  1697. begin
  1698. if tsym(p).typ<>paravarsym then
  1699. exit;
  1700. with tparavarsym(p) do
  1701. if (is_managed_type(vardef) and
  1702. (varspez in [vs_value,vs_out])) or
  1703. (is_shortstring(vardef) and
  1704. (varspez=vs_value)) then
  1705. include(current_procinfo.flags,pi_do_call);
  1706. end;
  1707. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1708. {
  1709. Parses the procedure directives, then parses the procedure body, then
  1710. generates the code for it
  1711. }
  1712. var
  1713. oldfailtokenmode : tmodeswitches;
  1714. isnestedproc : boolean;
  1715. begin
  1716. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1717. oldfailtokenmode:=[];
  1718. { create a new procedure }
  1719. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1720. current_module.procinfo:=current_procinfo;
  1721. current_procinfo.procdef:=pd;
  1722. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1723. { Insert mangledname }
  1724. pd.aliasnames.insert(pd.mangledname);
  1725. { Handle Export of this procedure }
  1726. if (po_exports in pd.procoptions) and
  1727. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1728. begin
  1729. pd.aliasnames.insert(pd.procsym.realname);
  1730. if cs_link_deffile in current_settings.globalswitches then
  1731. deffile.AddExport(pd.mangledname);
  1732. end;
  1733. { Insert result variables in the localst }
  1734. insert_funcret_local(pd);
  1735. { check if there are para's which require initing -> set }
  1736. { pi_do_call (if not yet set) }
  1737. if not(pi_do_call in current_procinfo.flags) then
  1738. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1739. { set _FAIL as keyword if constructor }
  1740. if (pd.proctypeoption=potype_constructor) then
  1741. begin
  1742. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1743. tokeninfo^[_FAIL].keyword:=alllanguagemodes;
  1744. end;
  1745. tcgprocinfo(current_procinfo).parse_body;
  1746. { We can't support inlining for procedures that have nested
  1747. procedures because the nested procedures use a fixed offset
  1748. for accessing locals in the parent procedure (PFV) }
  1749. if current_procinfo.has_nestedprocs then
  1750. begin
  1751. if (df_generic in current_procinfo.procdef.defoptions) then
  1752. Comment(V_Error,'Generic methods cannot have nested procedures')
  1753. else
  1754. if (po_inline in current_procinfo.procdef.procoptions) then
  1755. begin
  1756. Message1(parser_h_not_supported_for_inline,'nested procedures');
  1757. Message(parser_h_inlining_disabled);
  1758. exclude(current_procinfo.procdef.procoptions,po_inline);
  1759. end;
  1760. end;
  1761. { When it's a nested procedure then defer the code generation,
  1762. when back at normal function level then generate the code
  1763. for all defered nested procedures and the current procedure }
  1764. if not isnestedproc then
  1765. begin
  1766. if not(df_generic in current_procinfo.procdef.defoptions) then
  1767. tcgprocinfo(current_procinfo).generate_code_tree;
  1768. end;
  1769. { reset _FAIL as _SELF normal }
  1770. if (pd.proctypeoption=potype_constructor) then
  1771. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1772. { release procinfo }
  1773. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1774. internalerror(200304274);
  1775. current_module.procinfo:=current_procinfo.parent;
  1776. { For specialization we didn't record the last semicolon. Moving this parsing
  1777. into the parse_body routine is not done because of having better file position
  1778. information available }
  1779. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1780. consume(_SEMICOLON);
  1781. if not isnestedproc then
  1782. { current_procinfo is checked for nil later on }
  1783. freeandnil(current_procinfo);
  1784. end;
  1785. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef);
  1786. {
  1787. Parses the procedure directives, then parses the procedure body, then
  1788. generates the code for it
  1789. }
  1790. var
  1791. old_current_procinfo : tprocinfo;
  1792. old_current_structdef: tabstractrecorddef;
  1793. old_current_genericdef,
  1794. old_current_specializedef: tstoreddef;
  1795. pdflags : tpdflags;
  1796. pd,firstpd : tprocdef;
  1797. s : string;
  1798. begin
  1799. { save old state }
  1800. old_current_procinfo:=current_procinfo;
  1801. old_current_structdef:=current_structdef;
  1802. old_current_genericdef:=current_genericdef;
  1803. old_current_specializedef:=current_specializedef;
  1804. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1805. to another procdef }
  1806. current_procinfo:=nil;
  1807. current_structdef:=nil;
  1808. current_genericdef:=nil;
  1809. current_specializedef:=nil;
  1810. if not assigned(usefwpd) then
  1811. { parse procedure declaration }
  1812. pd:=parse_proc_dec(isclassmethod,old_current_structdef)
  1813. else
  1814. pd:=usefwpd;
  1815. { set the default function options }
  1816. if parse_only then
  1817. begin
  1818. pd.forwarddef:=true;
  1819. { set also the interface flag, for better error message when the
  1820. implementation doesn't much this header }
  1821. pd.interfacedef:=true;
  1822. include(pd.procoptions,po_global);
  1823. pdflags:=[pd_interface];
  1824. end
  1825. else
  1826. begin
  1827. pdflags:=[pd_body];
  1828. if (not current_module.in_interface) then
  1829. include(pdflags,pd_implemen);
  1830. if (not current_module.is_unit) or
  1831. create_smartlink then
  1832. include(pd.procoptions,po_global);
  1833. pd.forwarddef:=false;
  1834. end;
  1835. if not assigned(usefwpd) then
  1836. begin
  1837. { parse the directives that may follow }
  1838. parse_proc_directives(pd,pdflags);
  1839. { hint directives, these can be separated by semicolons here,
  1840. that needs to be handled here with a loop (PFV) }
  1841. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1842. Consume(_SEMICOLON);
  1843. { Set calling convention }
  1844. handle_calling_convention(pd);
  1845. end;
  1846. { search for forward declarations }
  1847. if not proc_add_definition(pd) then
  1848. begin
  1849. { A method must be forward defined (in the object declaration) }
  1850. if assigned(pd.struct) and
  1851. (not assigned(old_current_structdef)) then
  1852. begin
  1853. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1854. tprocsym(pd.procsym).write_parameter_lists(pd);
  1855. end
  1856. else
  1857. begin
  1858. { Give a better error if there is a forward def in the interface and only
  1859. a single implementation }
  1860. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1861. if (not pd.forwarddef) and
  1862. (not pd.interfacedef) and
  1863. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1864. firstpd.forwarddef and
  1865. firstpd.interfacedef and
  1866. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1867. { don't give an error if it may be an overload }
  1868. not(m_fpc in current_settings.modeswitches) and
  1869. (not(po_overload in pd.procoptions) or
  1870. not(po_overload in firstpd.procoptions)) then
  1871. begin
  1872. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1873. tprocsym(pd.procsym).write_parameter_lists(pd);
  1874. end;
  1875. end;
  1876. end;
  1877. { Set mangled name }
  1878. proc_set_mangledname(pd);
  1879. { compile procedure when a body is needed }
  1880. if (pd_body in pdflags) then
  1881. begin
  1882. read_proc_body(old_current_procinfo,pd);
  1883. end
  1884. else
  1885. begin
  1886. { Handle imports }
  1887. if (po_external in pd.procoptions) then
  1888. begin
  1889. { Import DLL specified? }
  1890. if assigned(pd.import_dll) then
  1891. begin
  1892. if assigned (pd.import_name) then
  1893. current_module.AddExternalImport(pd.import_dll^,
  1894. pd.import_name^,proc_get_importname(pd),
  1895. pd.import_nr,false,false)
  1896. else
  1897. current_module.AddExternalImport(pd.import_dll^,
  1898. proc_get_importname(pd),proc_get_importname(pd),
  1899. pd.import_nr,false,true);
  1900. end
  1901. else
  1902. begin
  1903. { add import name to external list for DLL scanning }
  1904. if tf_has_dllscanner in target_info.flags then
  1905. current_module.dllscannerinputlist.Add(proc_get_importname(pd),pd);
  1906. end;
  1907. { External declared in implementation, and there was already a
  1908. forward (or interface) declaration then we need to generate
  1909. a stub that calls the external routine }
  1910. if (not pd.forwarddef) and
  1911. (pd.hasforward)
  1912. { it is unclear to me what's the use of the following condition,
  1913. so commented out, see also issue #18371 (FK)
  1914. and
  1915. not(
  1916. assigned(pd.import_dll) and
  1917. (target_info.system in [system_i386_wdosx,
  1918. system_arm_wince,system_i386_wince])
  1919. ) } then
  1920. begin
  1921. s:=proc_get_importname(pd);
  1922. if s<>'' then
  1923. gen_external_stub(current_asmdata.asmlists[al_procedures],pd,s);
  1924. { remove the external stuff, so that the interface crc
  1925. doesn't change. This makes the function calls less
  1926. efficient, but it means that the interface doesn't
  1927. change if the function is ever redirected to another
  1928. function or implemented in the unit. }
  1929. pd.procoptions:=pd.procoptions-[po_external,po_has_importname,po_has_importdll];
  1930. stringdispose(pd.import_name);
  1931. stringdispose(pd.import_dll);
  1932. pd.import_nr:=0;
  1933. end;
  1934. end;
  1935. end;
  1936. { make sure that references to forward-declared functions are not }
  1937. { treated as references to external symbols, needed for darwin. }
  1938. { make sure we don't change the binding of real external symbols }
  1939. if not(po_external in pd.procoptions) then
  1940. begin
  1941. if (po_global in pd.procoptions) or
  1942. (cs_profile in current_settings.moduleswitches) then
  1943. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION)
  1944. else
  1945. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  1946. end;
  1947. current_structdef:=old_current_structdef;
  1948. current_genericdef:=old_current_genericdef;
  1949. current_specializedef:=old_current_specializedef;
  1950. current_procinfo:=old_current_procinfo;
  1951. end;
  1952. {****************************************************************************
  1953. DECLARATION PARSING
  1954. ****************************************************************************}
  1955. { search in symtablestack for not complete classes }
  1956. procedure check_forward_class(p:TObject;arg:pointer);
  1957. begin
  1958. if (tsym(p).typ=typesym) and
  1959. (ttypesym(p).typedef.typ=objectdef) and
  1960. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  1961. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1962. end;
  1963. procedure read_declarations(islibrary : boolean);
  1964. var
  1965. is_classdef:boolean;
  1966. begin
  1967. is_classdef:=false;
  1968. repeat
  1969. if not assigned(current_procinfo) then
  1970. internalerror(200304251);
  1971. case token of
  1972. _LABEL:
  1973. label_dec;
  1974. _CONST:
  1975. const_dec;
  1976. _TYPE:
  1977. type_dec;
  1978. _VAR:
  1979. var_dec;
  1980. _THREADVAR:
  1981. threadvar_dec;
  1982. _CLASS:
  1983. begin
  1984. is_classdef:=false;
  1985. if try_to_consume(_CLASS) then
  1986. begin
  1987. { class modifier is only allowed for procedures, functions, }
  1988. { constructors, destructors }
  1989. if not(token in [_FUNCTION,_PROCEDURE,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  1990. not((token=_ID) and (idtoken=_OPERATOR)) then
  1991. Message(parser_e_procedure_or_function_expected);
  1992. if is_interface(current_structdef) then
  1993. Message(parser_e_no_static_method_in_interfaces)
  1994. else
  1995. { class methods are also allowed for Objective-C protocols }
  1996. is_classdef:=true;
  1997. end;
  1998. end;
  1999. _CONSTRUCTOR,
  2000. _DESTRUCTOR,
  2001. _FUNCTION,
  2002. _PROCEDURE,
  2003. _OPERATOR:
  2004. begin
  2005. read_proc(is_classdef,nil);
  2006. is_classdef:=false;
  2007. end;
  2008. _EXPORTS:
  2009. begin
  2010. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  2011. begin
  2012. Message(parser_e_syntax_error);
  2013. consume_all_until(_SEMICOLON);
  2014. end
  2015. else if islibrary or
  2016. (target_info.system in systems_unit_program_exports) then
  2017. read_exports
  2018. else
  2019. begin
  2020. Message(parser_w_unsupported_feature);
  2021. consume(_BEGIN);
  2022. end;
  2023. end;
  2024. _PROPERTY:
  2025. begin
  2026. if (m_fpc in current_settings.modeswitches) then
  2027. property_dec
  2028. else
  2029. break;
  2030. end;
  2031. else
  2032. begin
  2033. case idtoken of
  2034. _RESOURCESTRING:
  2035. begin
  2036. { m_class is needed, because the resourcestring
  2037. loading is in the ObjPas unit }
  2038. { if (m_class in current_settings.modeswitches) then}
  2039. resourcestring_dec
  2040. { else
  2041. break;}
  2042. end;
  2043. _OPERATOR:
  2044. begin
  2045. if is_classdef then
  2046. begin
  2047. read_proc(is_classdef,nil);
  2048. is_classdef:=false;
  2049. end
  2050. else
  2051. break;
  2052. end;
  2053. else
  2054. break;
  2055. end;
  2056. end;
  2057. end;
  2058. until false;
  2059. { add implementations for synthetic method declarations added by
  2060. the compiler (not for unit/program init functions, their localst
  2061. is the staticst -> would duplicate the work done in pmodules) }
  2062. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  2063. add_synthetic_method_implementations(current_procinfo.procdef.localst);
  2064. { check for incomplete class definitions, this is only required
  2065. for fpc modes }
  2066. if (m_fpc in current_settings.modeswitches) then
  2067. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  2068. end;
  2069. procedure read_interface_declarations;
  2070. begin
  2071. repeat
  2072. case token of
  2073. _CONST :
  2074. const_dec;
  2075. _TYPE :
  2076. type_dec;
  2077. _VAR :
  2078. var_dec;
  2079. _THREADVAR :
  2080. threadvar_dec;
  2081. _FUNCTION,
  2082. _PROCEDURE,
  2083. _OPERATOR :
  2084. read_proc(false,nil);
  2085. else
  2086. begin
  2087. case idtoken of
  2088. _RESOURCESTRING :
  2089. resourcestring_dec;
  2090. _PROPERTY:
  2091. begin
  2092. if (m_fpc in current_settings.modeswitches) then
  2093. property_dec
  2094. else
  2095. break;
  2096. end;
  2097. else
  2098. break;
  2099. end;
  2100. end;
  2101. end;
  2102. until false;
  2103. { check for incomplete class definitions, this is only required
  2104. for fpc modes }
  2105. if (m_fpc in current_settings.modeswitches) then
  2106. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  2107. end;
  2108. {****************************************************************************
  2109. SPECIALIZATION BODY GENERATION
  2110. ****************************************************************************}
  2111. procedure specialize_objectdefs(p:TObject;arg:pointer);
  2112. var
  2113. oldcurrent_filepos : tfileposinfo;
  2114. specobj : tabstractrecorddef;
  2115. state : tspecializationstate;
  2116. procedure process_abstractrecorddef(def:tabstractrecorddef);
  2117. var
  2118. i : longint;
  2119. hp : tdef;
  2120. hmodule : tmodule;
  2121. begin
  2122. hmodule:=find_module_from_symtable(def.genericdef.owner);
  2123. if hmodule=nil then
  2124. internalerror(201202041);
  2125. for i:=0 to def.symtable.DefList.Count-1 do
  2126. begin
  2127. hp:=tdef(def.symtable.DefList[i]);
  2128. if hp.typ=procdef then
  2129. begin
  2130. { only generate the code if we need a body }
  2131. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  2132. continue;
  2133. if assigned(tprocdef(hp).genericdef) and
  2134. (tprocdef(hp).genericdef.typ=procdef) and
  2135. assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf) then
  2136. begin
  2137. oldcurrent_filepos:=current_filepos;
  2138. current_filepos:=tprocdef(tprocdef(hp).genericdef).fileinfo;
  2139. { use the index the module got from the current compilation process }
  2140. current_filepos.moduleindex:=hmodule.unit_index;
  2141. current_tokenpos:=current_filepos;
  2142. current_scanner.startreplaytokens(tprocdef(tprocdef(hp).genericdef).generictokenbuf);
  2143. read_proc_body(nil,tprocdef(hp));
  2144. current_filepos:=oldcurrent_filepos;
  2145. end
  2146. { synthetic routines will be implemented afterwards }
  2147. else if tprocdef(hp).synthetickind=tsk_none then
  2148. MessagePos1(tprocdef(hp).fileinfo,sym_e_forward_not_resolved,tprocdef(hp).fullprocname(false));
  2149. end
  2150. else
  2151. if hp.typ in [objectdef,recorddef] then
  2152. { generate code for subtypes as well }
  2153. process_abstractrecorddef(tabstractrecorddef(hp));
  2154. end;
  2155. end;
  2156. begin
  2157. if not((tsym(p).typ=typesym) and
  2158. (ttypesym(p).typedef.typesym=tsym(p)) and
  2159. (ttypesym(p).typedef.typ in [objectdef,recorddef]) and
  2160. (df_specialization in ttypesym(p).typedef.defoptions)
  2161. ) then
  2162. exit;
  2163. { Setup symtablestack a definition time }
  2164. specobj:=tabstractrecorddef(ttypesym(p).typedef);
  2165. if not (is_class_or_object(specobj) or is_record(specobj) or is_javaclass(specobj)) then
  2166. exit;
  2167. specialization_init(specobj.genericdef,state);
  2168. { procedure definitions for classes or objects }
  2169. process_abstractrecorddef(specobj);
  2170. specialization_done(state);
  2171. end;
  2172. procedure generate_specialization_procs;
  2173. begin
  2174. if assigned(current_module.globalsymtable) then
  2175. current_module.globalsymtable.SymList.WhileEachCall(@specialize_objectdefs,nil);
  2176. if assigned(current_module.localsymtable) then
  2177. current_module.localsymtable.SymList.WhileEachCall(@specialize_objectdefs,nil);
  2178. end;
  2179. end.