psub.pas 79 KB

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