psub.pas 81 KB

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