psub.pas 78 KB

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