psub.pas 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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. begin
  437. generate_except_block:=internalstatements(newstatement);
  438. { a constructor needs call destructor (if available) when it
  439. is not inherited }
  440. if assigned(current_objectdef) and
  441. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  442. begin
  443. { Don't test self and the vmt here. See generate_bodyexit_block }
  444. { why (JM) }
  445. oldlocalswitches:=current_settings.localswitches;
  446. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  447. pd:=current_objectdef.Finddestructor;
  448. if assigned(pd) then
  449. begin
  450. { if vmt<>0 then call destructor }
  451. addstatement(newstatement,cifnode.create(
  452. caddnode.create(unequaln,
  453. load_vmt_pointer_node,
  454. cnilnode.create),
  455. { cnf_create_failed -> don't call BeforeDestruction }
  456. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[cnf_create_failed]),
  457. nil));
  458. end;
  459. current_settings.localswitches:=oldlocalswitches;
  460. end
  461. else
  462. begin
  463. { no constructor }
  464. { must be the return value finalized before reraising the exception? }
  465. if (not is_void(current_procinfo.procdef.returndef)) and
  466. (current_procinfo.procdef.returndef.needs_inittable) and
  467. (not paramanager.ret_in_param(current_procinfo.procdef.returndef, current_procinfo.procdef.proccalloption)) and
  468. (not is_class(current_procinfo.procdef.returndef)) then
  469. addstatement(newstatement,finalize_data_node(load_result_node));
  470. end;
  471. end;
  472. {****************************************************************************
  473. TCGProcInfo
  474. ****************************************************************************}
  475. constructor tcgprocinfo.create(aparent:tprocinfo);
  476. begin
  477. inherited Create(aparent);
  478. nestedprocs:=tlinkedlist.create;
  479. end;
  480. destructor tcgprocinfo.destroy;
  481. begin
  482. nestedprocs.free;
  483. if assigned(code) then
  484. code.free;
  485. inherited destroy;
  486. end;
  487. procedure tcgprocinfo.printproc(pass:string);
  488. begin
  489. assign(printnodefile,treelogfilename);
  490. {$I-}
  491. append(printnodefile);
  492. if ioresult<>0 then
  493. rewrite(printnodefile);
  494. {$I+}
  495. if ioresult<>0 then
  496. begin
  497. Comment(V_Error,'Error creating '+treelogfilename);
  498. exit;
  499. end;
  500. writeln(printnodefile);
  501. writeln(printnodefile,'*******************************************************************************');
  502. writeln(printnodefile, pass);
  503. writeln(printnodefile,procdef.fullprocname(false));
  504. writeln(printnodefile,'*******************************************************************************');
  505. printnode(printnodefile,code);
  506. close(printnodefile);
  507. end;
  508. procedure tcgprocinfo.add_entry_exit_code;
  509. var
  510. finalcode,
  511. bodyentrycode,
  512. bodyexitcode,
  513. exceptcode : tnode;
  514. newblock : tblocknode;
  515. codestatement,
  516. newstatement : tstatementnode;
  517. oldfilepos : tfileposinfo;
  518. begin
  519. oldfilepos:=current_filepos;
  520. { Generate code/locations used at start of proc }
  521. current_filepos:=entrypos;
  522. entry_asmnode:=casmnode.create_get_position;
  523. loadpara_asmnode:=casmnode.create_get_position;
  524. stackcheck_asmnode:=casmnode.create_get_position;
  525. init_asmnode:=casmnode.create_get_position;
  526. bodyentrycode:=generate_bodyentry_block;
  527. { Generate code/locations used at end of proc }
  528. current_filepos:=exitpos;
  529. exitlabel_asmnode:=casmnode.create_get_position;
  530. final_asmnode:=casmnode.create_get_position;
  531. bodyexitcode:=generate_bodyexit_block;
  532. { Generate procedure by combining init+body+final,
  533. depending on the implicit finally we need to add
  534. an try...finally...end wrapper }
  535. newblock:=internalstatements(newstatement);
  536. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  537. (pi_needs_implicit_finally in flags) and
  538. { but it's useless in init/final code of units }
  539. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  540. begin
  541. { Generate special exception block only needed when
  542. implicit finaly is used }
  543. current_filepos:=exitpos;
  544. exceptcode:=generate_except_block;
  545. { Generate code that will be in the try...finally }
  546. finalcode:=internalstatements(codestatement);
  547. addstatement(codestatement,final_asmnode);
  548. { Initialize before try...finally...end frame }
  549. addstatement(newstatement,loadpara_asmnode);
  550. addstatement(newstatement,stackcheck_asmnode);
  551. addstatement(newstatement,entry_asmnode);
  552. addstatement(newstatement,init_asmnode);
  553. addstatement(newstatement,bodyentrycode);
  554. current_filepos:=entrypos;
  555. addstatement(newstatement,ctryfinallynode.create_implicit(
  556. code,
  557. finalcode,
  558. exceptcode));
  559. addstatement(newstatement,exitlabel_asmnode);
  560. addstatement(newstatement,bodyexitcode);
  561. { set flag the implicit finally has been generated }
  562. include(flags,pi_has_implicit_finally);
  563. end
  564. else
  565. begin
  566. addstatement(newstatement,loadpara_asmnode);
  567. addstatement(newstatement,stackcheck_asmnode);
  568. addstatement(newstatement,entry_asmnode);
  569. addstatement(newstatement,init_asmnode);
  570. addstatement(newstatement,bodyentrycode);
  571. addstatement(newstatement,code);
  572. addstatement(newstatement,exitlabel_asmnode);
  573. addstatement(newstatement,bodyexitcode);
  574. addstatement(newstatement,final_asmnode);
  575. end;
  576. do_firstpass(tnode(newblock));
  577. code:=newblock;
  578. current_filepos:=oldfilepos;
  579. end;
  580. procedure clearrefs(p:TObject;arg:pointer);
  581. begin
  582. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  583. if tabstractvarsym(p).refs>1 then
  584. tabstractvarsym(p).refs:=1;
  585. end;
  586. procedure translate_registers(p:TObject;list:pointer);
  587. begin
  588. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  589. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  590. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  591. begin
  592. if not(cs_no_regalloc in current_settings.globalswitches) then
  593. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  594. if cs_asm_source in current_settings.globalswitches then
  595. TAsmList(list).concat(Tai_comment.Create(strpnew('Var '+tabstractnormalvarsym(p).realname+' located in register '+
  596. std_regname(tabstractnormalvarsym(p).localloc.register))))
  597. end;
  598. end;
  599. procedure check_for_stack(p:TObject;arg:pointer);
  600. begin
  601. if tsym(p).typ=paravarsym then
  602. begin
  603. { check if there no parameter of the current procedure is stack dependend }
  604. if is_open_array(tparavarsym(p).vardef) or
  605. is_array_of_const(tparavarsym(p).vardef) then
  606. pboolean(arg)^:=true;
  607. if assigned(p) and
  608. assigned(tparavarsym(p).paraloc[calleeside].location) and
  609. (tparavarsym(p).paraloc[calleeside].location^.loc=LOC_REFERENCE) then
  610. pboolean(arg)^:=true;
  611. end;
  612. end;
  613. function tcgprocinfo.stack_tainting_parameter : boolean;
  614. begin
  615. result:=false;
  616. procdef.parast.SymList.ForEachCall(@check_for_stack,@result);
  617. end;
  618. function tcgprocinfo.has_assembler_child : boolean;
  619. var
  620. hp : tcgprocinfo;
  621. begin
  622. result:=false;
  623. hp:=tcgprocinfo(nestedprocs.first);
  624. while assigned(hp) do
  625. begin
  626. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  627. begin
  628. result:=true;
  629. exit;
  630. end;
  631. hp:=tcgprocinfo(hp.next);
  632. end;
  633. end;
  634. procedure tcgprocinfo.generate_code;
  635. var
  636. old_current_procinfo : tprocinfo;
  637. oldmaxfpuregisters : longint;
  638. oldfilepos : tfileposinfo;
  639. old_current_objectdef : tobjectdef;
  640. templist : TAsmList;
  641. headertai : tai;
  642. i : integer;
  643. varsym : tabstractnormalvarsym;
  644. {RedoDFA : boolean;}
  645. begin
  646. { the initialization procedure can be empty, then we
  647. don't need to generate anything. When it was an empty
  648. procedure there would be at least a blocknode }
  649. if not assigned(code) then
  650. exit;
  651. { We need valid code }
  652. if Errorcount<>0 then
  653. exit;
  654. { No code can be generated for generic template }
  655. if (df_generic in procdef.defoptions) then
  656. internalerror(200511152);
  657. { The RA and Tempgen shall not be available yet }
  658. if assigned(tg) then
  659. internalerror(200309201);
  660. old_current_procinfo:=current_procinfo;
  661. oldfilepos:=current_filepos;
  662. old_current_objectdef:=current_objectdef;
  663. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  664. current_procinfo:=self;
  665. current_filepos:=entrypos;
  666. current_objectdef:=procdef._class;
  667. templist:=TAsmList.create;
  668. { add parast/localst to symtablestack }
  669. add_to_symtablestack;
  670. { clear register count }
  671. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  672. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  673. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  674. if (procdef.localst.symtablelevel=main_program_level) and
  675. (not current_module.is_unit) then
  676. include(flags,pi_do_call);
  677. { set implicit_finally flag when there are locals/paras to be finalized }
  678. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  679. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  680. {$if defined(x86) or defined(arm)}
  681. { set implicit_finally flag for if procedure is safecall }
  682. if (target_info.system in systems_all_windows) and
  683. (procdef.proccalloption=pocall_safecall) then
  684. include(flags, pi_needs_implicit_finally);
  685. {$endif}
  686. { firstpass everything }
  687. flowcontrol:=[];
  688. do_firstpass(code);
  689. {$ifdef i386}
  690. procdef.fpu_used:=node_resources_fpu(code);
  691. if procdef.fpu_used>0 then
  692. include(flags,pi_uses_fpu);
  693. {$endif i386}
  694. { Print the node to tree.log }
  695. if paraprintnodetree=1 then
  696. printproc( 'after the firstpass');
  697. { do this before adding the entry code else the tail recursion recognition won't work,
  698. if this causes troubles, it must be if'ed
  699. }
  700. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  701. (pi_is_recursive in flags) then
  702. do_opttail(code,procdef);
  703. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  704. { creating dfa is not always possible }
  705. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler,
  706. pi_needs_implicit_finally,pi_has_implicit_finally])=[]) then
  707. begin
  708. dfabuilder:=TDFABuilder.Create;
  709. dfabuilder.createdfainfo(code);
  710. { when life info is available, we can give more sophisticated warning about unintialized
  711. variables }
  712. { iterate through life info of the first node }
  713. for i:=0 to dfabuilder.nodemap.count-1 do
  714. begin
  715. if DFASetIn(code.optinfo^.life,i) then
  716. case tnode(dfabuilder.nodemap[i]).nodetype of
  717. loadn:
  718. begin
  719. varsym:=tabstractnormalvarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry);
  720. { Give warning/note for living locals }
  721. if assigned(varsym.owner) and
  722. not(vo_is_external in varsym.varoptions) then
  723. begin
  724. if (vo_is_funcret in varsym.varoptions) then
  725. CGMessage(sym_w_function_result_uninitialized)
  726. else
  727. begin
  728. if (varsym.owner=procdef.localst) and not (vo_is_typed_const in varsym.varoptions) then
  729. CGMessage1(sym_w_uninitialized_local_variable,varsym.realname);
  730. end;
  731. end;
  732. end;
  733. end;
  734. end;
  735. include(flags,pi_dfaavailable);
  736. end;
  737. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  738. { our induction variable strength reduction doesn't like
  739. for loops with more than one entry }
  740. and not(pi_has_goto in current_procinfo.flags) then
  741. begin
  742. {RedoDFA:=}OptimizeInductionVariables(code);
  743. end;
  744. if cs_opt_nodecse in current_settings.optimizerswitches then
  745. do_optcse(code);
  746. { add implicit entry and exit code }
  747. add_entry_exit_code;
  748. { only do secondpass if there are no errors }
  749. if (ErrorCount=0) then
  750. begin
  751. create_codegen;
  752. { set the start offset to the start of the temp area in the stack }
  753. tg:=ttgobj.create;
  754. {$if defined(x86) or defined(arm)}
  755. { try to strip the stack frame }
  756. { set the framepointer to esp if:
  757. - no assembler directive, those are handled in assembler_block
  758. in pstatment.pas (for cases not caught by the Delphi
  759. exception below)
  760. - no exceptions are used
  761. - no pushes are used/esp modifications, could be:
  762. * outgoing parameters on the stack
  763. * incoming parameters on the stack
  764. * open arrays
  765. - no inline assembler
  766. or
  767. - Delphi mode
  768. - assembler directive
  769. - no pushes are used/esp modifications, could be:
  770. * outgoing parameters on the stack
  771. * incoming parameters on the stack
  772. * open arrays
  773. - no local variables
  774. }
  775. if ((po_assembler in procdef.procoptions) and
  776. (m_delphi in current_settings.modeswitches) and
  777. { localst at main_program_level is a staticsymtable }
  778. (procdef.localst.symtablelevel<>main_program_level) and
  779. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  780. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  781. not(cs_generate_stackframes in current_settings.localswitches) and
  782. not(po_assembler in procdef.procoptions) and
  783. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler,
  784. pi_needs_implicit_finally,pi_has_implicit_finally,pi_has_stackparameter,
  785. pi_needs_stackframe])=[])
  786. )
  787. then
  788. begin
  789. { we need the parameter info here to determine if the procedure gets
  790. parameters on the stack
  791. calling generate_parameter_info doesn't hurt but it costs time
  792. }
  793. generate_parameter_info;
  794. if not(stack_tainting_parameter) and
  795. not(has_assembler_child) and (para_stack_size=0) then
  796. begin
  797. { Only need to set the framepointer }
  798. framepointer:=NR_STACK_POINTER_REG;
  799. tg.direction:=1;
  800. end;
  801. end;
  802. {$endif}
  803. { Create register allocator }
  804. cg.init_register_allocators;
  805. set_first_temp_offset;
  806. generate_parameter_info;
  807. { allocate got register if needed }
  808. current_procinfo.allocate_got_register(aktproccode);
  809. { Allocate space in temp/registers for parast and localst }
  810. current_filepos:=entrypos;
  811. gen_alloc_symtable(aktproccode,procdef.parast);
  812. gen_alloc_symtable(aktproccode,procdef.localst);
  813. { Store temp offset for information about 'real' temps }
  814. tempstart:=tg.lasttemp;
  815. { Generate code to load register parameters in temps and insert local
  816. copies for values parameters. This must be done before the code for the
  817. body is generated because the localloc is updated.
  818. Note: The generated code will be inserted after the code generation of
  819. the body is finished, because only then the position is known }
  820. {$ifdef oldregvars}
  821. assign_regvars(code);
  822. {$endif oldreg}
  823. current_filepos:=entrypos;
  824. { record which registers are allocated here, since all code }
  825. { allocating registers comes after it }
  826. cg.set_regalloc_live_range_direction(rad_backwards);
  827. gen_load_para_value(templist);
  828. cg.set_regalloc_live_range_direction(rad_forward);
  829. { caller paraloc info is also necessary in the stackframe_entry
  830. code of the ppc (and possibly other processors) }
  831. if not procdef.has_paraloc_info then
  832. begin
  833. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  834. procdef.has_paraloc_info:=true;
  835. end;
  836. { generate code for the node tree }
  837. do_secondpass(code);
  838. aktproccode.concatlist(current_asmdata.CurrAsmList);
  839. { The position of the loadpara_asmnode is now known }
  840. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  841. { first generate entry and initialize code with the correct
  842. position and switches }
  843. current_filepos:=entrypos;
  844. current_settings.localswitches:=entryswitches;
  845. cg.set_regalloc_live_range_direction(rad_backwards);
  846. gen_entry_code(templist);
  847. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  848. gen_initialize_code(templist);
  849. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  850. { now generate finalize and exit code with the correct position
  851. and switches }
  852. current_filepos:=exitpos;
  853. current_settings.localswitches:=exitswitches;
  854. cg.set_regalloc_live_range_direction(rad_forward);
  855. gen_finalize_code(templist);
  856. { the finalcode must be concated if there was no position available,
  857. using insertlistafter will result in an insert at the start
  858. when currentai=nil }
  859. if assigned(final_asmnode.currenttai) then
  860. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  861. else
  862. aktproccode.concatlist(templist);
  863. { insert exit label at the correct position }
  864. cg.a_label(templist,CurrExitLabel);
  865. if assigned(exitlabel_asmnode.currenttai) then
  866. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  867. else
  868. aktproccode.concatlist(templist);
  869. { exit code }
  870. gen_exit_code(templist);
  871. aktproccode.concatlist(templist);
  872. {$ifdef OLDREGVARS}
  873. { note: this must be done only after as much code as possible has }
  874. { been generated. The result is that when you ungetregister() a }
  875. { regvar, it will actually free the regvar (and alse free the }
  876. { the regvars at the same time). Doing this too early will }
  877. { confuse the register allocator, as the regvars will still be }
  878. { used. It should be done before loading the result regs (so }
  879. { they don't conflict with the regvars) and before }
  880. { gen_entry_code (that one has to be able to allocate the }
  881. { regvars again) (JM) }
  882. free_regvars(aktproccode);
  883. {$endif OLDREGVARS}
  884. { generate symbol and save end of header position }
  885. current_filepos:=entrypos;
  886. gen_proc_symbol(templist);
  887. headertai:=tai(templist.last);
  888. { insert symbol }
  889. aktproccode.insertlist(templist);
  890. { Free space in temp/registers for parast and localst, must be
  891. done after gen_entry_code }
  892. current_filepos:=exitpos;
  893. { make sure the got/pic register doesn't get freed in the }
  894. { middle of a loop }
  895. if (cs_create_pic in current_settings.moduleswitches) and
  896. (pi_needs_got in current_procinfo.flags) and
  897. (current_procinfo.got<>NR_NO) then
  898. cg.a_reg_sync(aktproccode,current_procinfo.got);
  899. gen_free_symtable(aktproccode,procdef.localst);
  900. gen_free_symtable(aktproccode,procdef.parast);
  901. { add code that will load the return value, this is not done
  902. for assembler routines when they didn't reference the result
  903. variable }
  904. gen_load_return_value(templist);
  905. aktproccode.concatlist(templist);
  906. { Already reserve all registers for stack checking code and
  907. generate the call to the helper function }
  908. if not(tf_no_generic_stackcheck in target_info.flags) and
  909. (cs_check_stack in entryswitches) and
  910. not(po_assembler in procdef.procoptions) and
  911. (procdef.proctypeoption<>potype_proginit) then
  912. begin
  913. current_filepos:=entrypos;
  914. gen_stack_check_call(templist);
  915. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  916. end;
  917. { this code (got loading) comes before everything which has }
  918. { already been generated, so reset the info about already }
  919. { backwards extended registers (so their live range can be }
  920. { extended backwards even further if needed) }
  921. { This code must be }
  922. { a) generated after do_secondpass has been called }
  923. { (because pi_needs_got may be set there) }
  924. { b) generated before register allocation, because the }
  925. { got/pic register can be a virtual one }
  926. { c) inserted before the entry code, because the entry }
  927. { code may need global symbols such as init rtti }
  928. { d) inserted after the stackframe allocation, because }
  929. { this register may have to be spilled }
  930. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  931. current_filepos:=entrypos;
  932. { load got if necessary }
  933. cg.g_maybe_got_init(templist);
  934. aktproccode.insertlistafter(headertai,templist);
  935. cg.set_regalloc_live_range_direction(rad_forward);
  936. { The procedure body is finished, we can now
  937. allocate the registers }
  938. cg.do_register_allocation(aktproccode,headertai);
  939. { translate imag. register to their real counter parts
  940. this is necessary for debuginfo and verbose assembler output
  941. when SSA will be implented, this will be more complicated because we've to
  942. maintain location lists }
  943. current_procinfo.procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  944. current_procinfo.procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  945. if (cs_create_pic in current_settings.moduleswitches) and
  946. (pi_needs_got in current_procinfo.flags) and
  947. not(cs_no_regalloc in current_settings.globalswitches) and
  948. (current_procinfo.got<>NR_NO) then
  949. cg.translate_register(current_procinfo.got);
  950. { Add save and restore of used registers }
  951. current_filepos:=entrypos;
  952. gen_save_used_regs(templist);
  953. aktproccode.insertlistafter(headertai,templist);
  954. current_filepos:=exitpos;
  955. gen_restore_used_regs(aktproccode);
  956. { We know the size of the stack, now we can generate the
  957. parameter that is passed to the stack checking code }
  958. if not(tf_no_generic_stackcheck in target_info.flags) and
  959. (cs_check_stack in entryswitches) and
  960. not(po_assembler in procdef.procoptions) and
  961. (procdef.proctypeoption<>potype_proginit) then
  962. begin
  963. current_filepos:=entrypos;
  964. gen_stack_check_size_para(templist);
  965. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  966. end;
  967. { Add entry code (stack allocation) after header }
  968. current_filepos:=entrypos;
  969. gen_proc_entry_code(templist);
  970. aktproccode.insertlistafter(headertai,templist);
  971. {$if defined(x86) or defined(arm)}
  972. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  973. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  974. (target_info.system in systems_all_windows) and
  975. (procdef.proccalloption=pocall_safecall) then
  976. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  977. {$endif}
  978. { Add exit code at the end }
  979. current_filepos:=exitpos;
  980. gen_proc_exit_code(templist);
  981. aktproccode.concatlist(templist);
  982. { check if the implicit finally has been generated. The flag
  983. should already be set in pass1 }
  984. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  985. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  986. (pi_needs_implicit_finally in flags) and
  987. not(pi_has_implicit_finally in flags) then
  988. internalerror(200405231);
  989. {$ifndef NoOpt}
  990. if not(cs_no_regalloc in current_settings.globalswitches) then
  991. begin
  992. if (cs_opt_level1 in current_settings.optimizerswitches) and
  993. { do not optimize pure assembler procedures }
  994. not(pi_is_assembler in flags) then
  995. optimize(aktproccode);
  996. end;
  997. {$endif NoOpt}
  998. {$ifdef ARM}
  999. { because of the limited constant size of the arm, all data access is done pc relative }
  1000. insertpcrelativedata(aktproccode,aktlocaldata);
  1001. {$endif ARM}
  1002. { Add end symbol and debug info }
  1003. { this must be done after the pcrelativedata is appended else the distance calculation of
  1004. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  1005. so it should be inserted before the end symbol (FK)
  1006. }
  1007. current_filepos:=exitpos;
  1008. gen_proc_symbol_end(templist);
  1009. aktproccode.concatlist(templist);
  1010. {$if defined(POWERPC) or defined(POWERPC64)}
  1011. fixup_jmps(aktproccode);
  1012. {$endif}
  1013. { insert line debuginfo }
  1014. if (cs_debuginfo in current_settings.moduleswitches) or
  1015. (cs_use_lineinfo in current_settings.globalswitches) then
  1016. current_debuginfo.insertlineinfo(aktproccode);
  1017. { add the procedure to the al_procedures }
  1018. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  1019. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(procdef.mangledname),getprocalign);
  1020. current_asmdata.asmlists[al_procedures].concatlist(aktproccode);
  1021. { save local data (casetable) also in the same file }
  1022. if assigned(aktlocaldata) and
  1023. (not aktlocaldata.empty) then
  1024. current_asmdata.asmlists[al_procedures].concatlist(aktlocaldata);
  1025. { only now we can remove the temps }
  1026. tg.resettempgen;
  1027. { stop tempgen and ra }
  1028. tg.free;
  1029. cg.done_register_allocators;
  1030. destroy_codegen;
  1031. tg:=nil;
  1032. end;
  1033. dfabuilder.free;
  1034. { restore symtablestack }
  1035. remove_from_symtablestack;
  1036. { restore }
  1037. templist.free;
  1038. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1039. current_filepos:=oldfilepos;
  1040. current_objectdef:=old_current_objectdef;
  1041. current_procinfo:=old_current_procinfo;
  1042. end;
  1043. procedure tcgprocinfo.add_to_symtablestack;
  1044. var
  1045. _class,hp : tobjectdef;
  1046. begin
  1047. { insert symtables for the class, but only if it is no nested function }
  1048. if assigned(procdef._class) and
  1049. not(assigned(parent) and
  1050. assigned(parent.procdef) and
  1051. assigned(parent.procdef._class)) then
  1052. begin
  1053. { insert them in the reverse order }
  1054. hp:=nil;
  1055. repeat
  1056. _class:=procdef._class;
  1057. while _class.childof<>hp do
  1058. _class:=_class.childof;
  1059. hp:=_class;
  1060. symtablestack.push(_class.symtable);
  1061. until hp=procdef._class;
  1062. end;
  1063. { insert parasymtable in symtablestack when parsing
  1064. a function }
  1065. if procdef.parast.symtablelevel>=normal_function_level then
  1066. symtablestack.push(procdef.parast);
  1067. { insert localsymtable, except for the main procedure
  1068. (in that case the localst is the unit's static symtable,
  1069. which is already on the stack) }
  1070. if procdef.localst.symtablelevel>=normal_function_level then
  1071. symtablestack.push(procdef.localst);
  1072. end;
  1073. procedure tcgprocinfo.remove_from_symtablestack;
  1074. var
  1075. _class : tobjectdef;
  1076. begin
  1077. { remove localsymtable }
  1078. if procdef.localst.symtablelevel>=normal_function_level then
  1079. symtablestack.pop(procdef.localst);
  1080. { remove parasymtable }
  1081. if procdef.parast.symtablelevel>=normal_function_level then
  1082. symtablestack.pop(procdef.parast);
  1083. { remove symtables for the class, but only if it is no nested function }
  1084. if assigned(procdef._class) and
  1085. not(assigned(parent) and
  1086. assigned(parent.procdef) and
  1087. assigned(parent.procdef._class)) then
  1088. begin
  1089. _class:=procdef._class;
  1090. while assigned(_class) do
  1091. begin
  1092. symtablestack.pop(_class.symtable);
  1093. _class:=_class.childof;
  1094. end;
  1095. end;
  1096. end;
  1097. procedure tcgprocinfo.resetprocdef;
  1098. begin
  1099. { remove code tree, if not inline procedure }
  1100. if assigned(code) then
  1101. begin
  1102. { the inline procedure has already got a copy of the tree
  1103. stored in procdef.inlininginfo }
  1104. code.free;
  1105. code:=nil;
  1106. end;
  1107. end;
  1108. function checknodeinlining(procdef: tprocdef): boolean;
  1109. var
  1110. i : integer;
  1111. currpara : tparavarsym;
  1112. begin
  1113. result := false;
  1114. if (pi_has_assembler_block in current_procinfo.flags) then
  1115. begin
  1116. Message1(parser_w_not_supported_for_inline,'assembler');
  1117. Message(parser_w_inlining_disabled);
  1118. exit;
  1119. end;
  1120. for i:=0 to procdef.paras.count-1 do
  1121. begin
  1122. currpara:=tparavarsym(procdef.paras[i]);
  1123. case currpara.vardef.typ of
  1124. formaldef :
  1125. begin
  1126. if (currpara.varspez in [vs_out,vs_var,vs_const]) then
  1127. begin
  1128. Message1(parser_w_not_supported_for_inline,'formal parameter');
  1129. Message(parser_w_inlining_disabled);
  1130. exit;
  1131. end;
  1132. end;
  1133. arraydef :
  1134. begin
  1135. if is_array_of_const(currpara.vardef) or
  1136. is_variant_array(currpara.vardef) then
  1137. begin
  1138. Message1(parser_w_not_supported_for_inline,'array of const');
  1139. Message(parser_w_inlining_disabled);
  1140. exit;
  1141. end;
  1142. { open arrays might need re-basing of the index, i.e. if you pass
  1143. an array[1..10] as open array, you have to add 1 to all index operations
  1144. if you directly inline it }
  1145. if is_open_array(currpara.vardef) then
  1146. begin
  1147. Message1(parser_w_not_supported_for_inline,'open array');
  1148. Message(parser_w_inlining_disabled);
  1149. exit;
  1150. end;
  1151. end;
  1152. end;
  1153. end;
  1154. result:=true;
  1155. end;
  1156. procedure tcgprocinfo.parse_body;
  1157. var
  1158. old_current_procinfo : tprocinfo;
  1159. old_block_type : tblock_type;
  1160. st : TSymtable;
  1161. old_current_objectdef : tobjectdef;
  1162. begin
  1163. old_current_procinfo:=current_procinfo;
  1164. old_block_type:=block_type;
  1165. old_current_objectdef:=current_objectdef;
  1166. current_procinfo:=self;
  1167. current_objectdef:=procdef._class;
  1168. { calculate the lexical level }
  1169. if procdef.parast.symtablelevel>maxnesting then
  1170. Message(parser_e_too_much_lexlevel);
  1171. block_type:=bt_body;
  1172. {$ifdef state_tracking}
  1173. { aktstate:=Tstate_storage.create;}
  1174. {$endif state_tracking}
  1175. { allocate the symbol for this procedure }
  1176. alloc_proc_symbol(procdef);
  1177. { add parast/localst to symtablestack }
  1178. add_to_symtablestack;
  1179. { save entry info }
  1180. entrypos:=current_filepos;
  1181. entryswitches:=current_settings.localswitches;
  1182. if (df_generic in procdef.defoptions) then
  1183. begin
  1184. { start token recorder for generic template }
  1185. procdef.initgeneric;
  1186. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1187. end;
  1188. { parse the code ... }
  1189. code:=block(current_module.islibrary);
  1190. if (df_generic in procdef.defoptions) then
  1191. begin
  1192. { stop token recorder for generic template }
  1193. current_scanner.stoprecordtokens;
  1194. { Give an error for accesses in the static symtable that aren't visible
  1195. outside the current unit }
  1196. st:=procdef.owner;
  1197. while (st.symtabletype=ObjectSymtable) do
  1198. st:=st.defowner.owner;
  1199. if (pi_uses_static_symtable in flags) and
  1200. (st.symtabletype<>staticsymtable) then
  1201. Comment(V_Error,'Global Generic template references static symtable');
  1202. end;
  1203. { save exit info }
  1204. exitswitches:=current_settings.localswitches;
  1205. exitpos:=last_endtoken_filepos;
  1206. { the procedure is now defined }
  1207. procdef.forwarddef:=false;
  1208. if assigned(code) then
  1209. begin
  1210. { get a better entry point }
  1211. entrypos:=code.fileinfo;
  1212. { Finish type checking pass }
  1213. do_typecheckpass(code);
  1214. end;
  1215. { Check for unused labels, forwards, symbols for procedures. Static
  1216. symtable is checked in pmodules.
  1217. The check must be done after the typecheckpass }
  1218. if (Errorcount=0) and
  1219. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1220. begin
  1221. { check if forwards are resolved }
  1222. tstoredsymtable(procdef.localst).check_forwards;
  1223. { check if all labels are used }
  1224. tstoredsymtable(procdef.localst).checklabels;
  1225. { check for unused symbols, but only if there is no asm block }
  1226. if not(pi_has_assembler_block in flags) then
  1227. begin
  1228. tstoredsymtable(procdef.localst).allsymbolsused;
  1229. tstoredsymtable(procdef.parast).allsymbolsused;
  1230. end;
  1231. end;
  1232. if (po_inline in procdef.procoptions) then
  1233. begin
  1234. { Can we inline this procedure? }
  1235. if checknodeinlining(procdef) then
  1236. begin
  1237. new(procdef.inlininginfo);
  1238. include(procdef.procoptions,po_has_inlininginfo);
  1239. procdef.inlininginfo^.code:=code.getcopy;
  1240. procdef.inlininginfo^.flags:=current_procinfo.flags;
  1241. { The blocknode needs to set an exit label }
  1242. if procdef.inlininginfo^.code.nodetype=blockn then
  1243. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1244. end;
  1245. end;
  1246. { Print the node to tree.log }
  1247. if paraprintnodetree=1 then
  1248. printproc( 'after parsing');
  1249. { ... remove symbol tables }
  1250. remove_from_symtablestack;
  1251. {$ifdef state_tracking}
  1252. { aktstate.destroy;}
  1253. {$endif state_tracking}
  1254. current_objectdef:=old_current_objectdef;
  1255. current_procinfo:=old_current_procinfo;
  1256. { Restore old state }
  1257. block_type:=old_block_type;
  1258. end;
  1259. {****************************************************************************
  1260. PROCEDURE/FUNCTION PARSING
  1261. ****************************************************************************}
  1262. procedure check_init_paras(p:TObject;arg:pointer);
  1263. begin
  1264. if tsym(p).typ<>paravarsym then
  1265. exit;
  1266. with tparavarsym(p) do
  1267. if (not is_class(vardef) and
  1268. vardef.needs_inittable and
  1269. (varspez in [vs_value,vs_out])) then
  1270. include(current_procinfo.flags,pi_do_call);
  1271. end;
  1272. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1273. {
  1274. Parses the procedure directives, then parses the procedure body, then
  1275. generates the code for it
  1276. }
  1277. procedure do_generate_code(pi:tcgprocinfo);
  1278. var
  1279. hpi : tcgprocinfo;
  1280. begin
  1281. { generate code for this procedure }
  1282. pi.generate_code;
  1283. { process nested procs }
  1284. hpi:=tcgprocinfo(pi.nestedprocs.first);
  1285. while assigned(hpi) do
  1286. begin
  1287. do_generate_code(hpi);
  1288. hpi:=tcgprocinfo(hpi.next);
  1289. end;
  1290. pi.resetprocdef;
  1291. end;
  1292. var
  1293. oldfailtokenmode : tmodeswitch;
  1294. isnestedproc : boolean;
  1295. begin
  1296. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1297. { create a new procedure }
  1298. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1299. current_module.procinfo:=current_procinfo;
  1300. current_procinfo.procdef:=pd;
  1301. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1302. { Insert mangledname }
  1303. pd.aliasnames.insert(pd.mangledname);
  1304. { Handle Export of this procedure }
  1305. if (po_exports in pd.procoptions) and
  1306. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1307. begin
  1308. pd.aliasnames.insert(pd.procsym.realname);
  1309. if cs_link_deffile in current_settings.globalswitches then
  1310. deffile.AddExport(pd.mangledname);
  1311. end;
  1312. { Insert result variables in the localst }
  1313. insert_funcret_local(pd);
  1314. { check if there are para's which require initing -> set }
  1315. { pi_do_call (if not yet set) }
  1316. if not(pi_do_call in current_procinfo.flags) then
  1317. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1318. { set _FAIL as keyword if constructor }
  1319. if (pd.proctypeoption=potype_constructor) then
  1320. begin
  1321. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1322. tokeninfo^[_FAIL].keyword:=m_all;
  1323. end;
  1324. tcgprocinfo(current_procinfo).parse_body;
  1325. { We can't support inlining for procedures that have nested
  1326. procedures because the nested procedures use a fixed offset
  1327. for accessing locals in the parent procedure (PFV) }
  1328. if (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1329. begin
  1330. if (df_generic in current_procinfo.procdef.defoptions) then
  1331. Comment(V_Error,'Generic methods cannot have nested procedures')
  1332. else
  1333. if (po_inline in current_procinfo.procdef.procoptions) then
  1334. begin
  1335. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1336. Message(parser_w_inlining_disabled);
  1337. exclude(current_procinfo.procdef.procoptions,po_inline);
  1338. end;
  1339. end;
  1340. { When it's a nested procedure then defer the code generation,
  1341. when back at normal function level then generate the code
  1342. for all defered nested procedures and the current procedure }
  1343. if isnestedproc then
  1344. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1345. else
  1346. begin
  1347. if not(df_generic in current_procinfo.procdef.defoptions) then
  1348. do_generate_code(tcgprocinfo(current_procinfo));
  1349. end;
  1350. { reset _FAIL as _SELF normal }
  1351. if (pd.proctypeoption=potype_constructor) then
  1352. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1353. { release procinfo }
  1354. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1355. internalerror(200304274);
  1356. current_module.procinfo:=current_procinfo.parent;
  1357. { For specialization we didn't record the last semicolon. Moving this parsing
  1358. into the parse_body routine is not done because of having better file position
  1359. information available }
  1360. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1361. consume(_SEMICOLON);
  1362. if not isnestedproc then
  1363. { current_procinfo is checked for nil later on }
  1364. freeandnil(current_procinfo);
  1365. end;
  1366. procedure read_proc(isclassmethod:boolean);
  1367. {
  1368. Parses the procedure directives, then parses the procedure body, then
  1369. generates the code for it
  1370. }
  1371. var
  1372. old_current_procinfo : tprocinfo;
  1373. old_current_objectdef : tobjectdef;
  1374. pdflags : tpdflags;
  1375. pd,firstpd : tprocdef;
  1376. s : string;
  1377. begin
  1378. { save old state }
  1379. old_current_procinfo:=current_procinfo;
  1380. old_current_objectdef:=current_objectdef;
  1381. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1382. to an other procdef }
  1383. current_procinfo:=nil;
  1384. current_objectdef:=nil;
  1385. { parse procedure declaration }
  1386. pd:=parse_proc_dec(isclassmethod, old_current_objectdef);
  1387. { set the default function options }
  1388. if parse_only then
  1389. begin
  1390. pd.forwarddef:=true;
  1391. { set also the interface flag, for better error message when the
  1392. implementation doesn't much this header }
  1393. pd.interfacedef:=true;
  1394. include(pd.procoptions,po_global);
  1395. pdflags:=[pd_interface];
  1396. end
  1397. else
  1398. begin
  1399. pdflags:=[pd_body];
  1400. if (not current_module.in_interface) then
  1401. include(pdflags,pd_implemen);
  1402. if (not current_module.is_unit) or
  1403. create_smartlink or
  1404. {
  1405. taking addresses of static procedures goes wrong
  1406. if they aren't global when pic is used (FK)
  1407. }
  1408. (cs_create_pic in current_settings.moduleswitches) then
  1409. include(pd.procoptions,po_global);
  1410. pd.forwarddef:=false;
  1411. end;
  1412. { parse the directives that may follow }
  1413. parse_proc_directives(pd,pdflags);
  1414. { hint directives, these can be separated by semicolons here,
  1415. that needs to be handled here with a loop (PFV) }
  1416. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1417. Consume(_SEMICOLON);
  1418. { Set calling convention }
  1419. handle_calling_convention(pd);
  1420. { search for forward declarations }
  1421. if not proc_add_definition(pd) then
  1422. begin
  1423. { A method must be forward defined (in the object declaration) }
  1424. if assigned(pd._class) and
  1425. (not assigned(old_current_objectdef)) then
  1426. begin
  1427. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1428. tprocsym(pd.procsym).write_parameter_lists(pd);
  1429. end
  1430. else
  1431. begin
  1432. { Give a better error if there is a forward def in the interface and only
  1433. a single implementation }
  1434. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1435. if (not pd.forwarddef) and
  1436. (not pd.interfacedef) and
  1437. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1438. firstpd.forwarddef and
  1439. firstpd.interfacedef and
  1440. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1441. { don't give an error if it may be an overload }
  1442. not(m_fpc in current_settings.modeswitches) and
  1443. (not(po_overload in pd.procoptions) or
  1444. not(po_overload in firstpd.procoptions)) then
  1445. begin
  1446. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1447. tprocsym(pd.procsym).write_parameter_lists(pd);
  1448. end;
  1449. end;
  1450. end;
  1451. { Set mangled name }
  1452. proc_set_mangledname(pd);
  1453. { compile procedure when a body is needed }
  1454. if (pd_body in pdflags) then
  1455. begin
  1456. read_proc_body(old_current_procinfo,pd);
  1457. end
  1458. else
  1459. begin
  1460. { Handle imports }
  1461. if (po_external in pd.procoptions) then
  1462. begin
  1463. { External declared in implementation, and there was already a
  1464. forward (or interface) declaration then we need to generate
  1465. a stub that calls the external routine }
  1466. if (not pd.forwarddef) and
  1467. (pd.hasforward) and
  1468. not(
  1469. assigned(pd.import_dll) and
  1470. (target_info.system in [system_i386_wdosx,
  1471. system_arm_wince,system_i386_wince])
  1472. ) then
  1473. begin
  1474. s:=proc_get_importname(pd);
  1475. if s<>'' then
  1476. gen_external_stub(current_asmdata.asmlists[al_procedures],pd,s);
  1477. end;
  1478. { Import DLL specified? }
  1479. if assigned(pd.import_dll) then
  1480. current_module.AddExternalImport(pd.import_dll^,proc_get_importname(pd),pd.import_nr,false,pd.import_name=nil)
  1481. else
  1482. begin
  1483. { add import name to external list for DLL scanning }
  1484. if tf_has_dllscanner in target_info.flags then
  1485. current_module.dllscannerinputlist.Add(proc_get_importname(pd),pd);
  1486. end;
  1487. end;
  1488. end;
  1489. { make sure that references to forward-declared functions are not }
  1490. { treated as references to external symbols, needed for darwin. }
  1491. { make sure we don't change the binding of real external symbols }
  1492. if not(po_external in pd.procoptions) then
  1493. begin
  1494. if (po_global in pd.procoptions) or
  1495. (cs_profile in current_settings.moduleswitches) then
  1496. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION)
  1497. else
  1498. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  1499. end;
  1500. current_objectdef:=old_current_objectdef;
  1501. current_procinfo:=old_current_procinfo;
  1502. end;
  1503. {****************************************************************************
  1504. DECLARATION PARSING
  1505. ****************************************************************************}
  1506. { search in symtablestack for not complete classes }
  1507. procedure check_forward_class(p:TObject;arg:pointer);
  1508. begin
  1509. if (tsym(p).typ=typesym) and
  1510. (ttypesym(p).typedef.typ=objectdef) and
  1511. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  1512. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1513. end;
  1514. procedure read_declarations(islibrary : boolean);
  1515. var
  1516. is_classdef:boolean;
  1517. begin
  1518. is_classdef:=false;
  1519. repeat
  1520. if not assigned(current_procinfo) then
  1521. internalerror(200304251);
  1522. case token of
  1523. _LABEL:
  1524. label_dec;
  1525. _CONST:
  1526. const_dec;
  1527. _TYPE:
  1528. type_dec;
  1529. _VAR:
  1530. var_dec;
  1531. _THREADVAR:
  1532. threadvar_dec;
  1533. _CLASS:
  1534. begin
  1535. is_classdef:=false;
  1536. if try_to_consume(_CLASS) then
  1537. begin
  1538. { class method only allowed for procedures and functions }
  1539. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR]) then
  1540. Message(parser_e_procedure_or_function_expected);
  1541. if is_interface(current_objectdef) then
  1542. Message(parser_e_no_static_method_in_interfaces)
  1543. else
  1544. { class methods are also allowed for Objective-C protocols }
  1545. is_classdef:=true;
  1546. end;
  1547. end;
  1548. _CONSTRUCTOR,
  1549. _DESTRUCTOR,
  1550. _FUNCTION,
  1551. _PROCEDURE,
  1552. _OPERATOR:
  1553. begin
  1554. read_proc(is_classdef);
  1555. is_classdef:=false;
  1556. end;
  1557. _EXPORTS:
  1558. begin
  1559. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  1560. begin
  1561. Message(parser_e_syntax_error);
  1562. consume_all_until(_SEMICOLON);
  1563. end
  1564. else if islibrary or
  1565. (target_info.system in systems_unit_program_exports) then
  1566. read_exports
  1567. else
  1568. begin
  1569. Message(parser_w_unsupported_feature);
  1570. consume(_BEGIN);
  1571. end;
  1572. end
  1573. else
  1574. begin
  1575. case idtoken of
  1576. _RESOURCESTRING :
  1577. begin
  1578. { m_class is needed, because the resourcestring
  1579. loading is in the ObjPas unit }
  1580. { if (m_class in current_settings.modeswitches) then}
  1581. resourcestring_dec
  1582. { else
  1583. break;}
  1584. end;
  1585. _PROPERTY:
  1586. begin
  1587. if (m_fpc in current_settings.modeswitches) then
  1588. begin
  1589. property_dec(is_classdef);
  1590. is_classdef:=false;
  1591. end
  1592. else
  1593. break;
  1594. end;
  1595. else
  1596. break;
  1597. end;
  1598. end;
  1599. end;
  1600. until false;
  1601. { check for incomplete class definitions, this is only required
  1602. for fpc modes }
  1603. if (m_fpc in current_settings.modeswitches) then
  1604. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  1605. end;
  1606. procedure read_interface_declarations;
  1607. begin
  1608. repeat
  1609. case token of
  1610. _CONST :
  1611. const_dec;
  1612. _TYPE :
  1613. type_dec;
  1614. _VAR :
  1615. var_dec;
  1616. _THREADVAR :
  1617. threadvar_dec;
  1618. _FUNCTION,
  1619. _PROCEDURE,
  1620. _OPERATOR :
  1621. read_proc(false);
  1622. else
  1623. begin
  1624. case idtoken of
  1625. _RESOURCESTRING :
  1626. resourcestring_dec;
  1627. _PROPERTY:
  1628. begin
  1629. if (m_fpc in current_settings.modeswitches) then
  1630. property_dec(false)
  1631. else
  1632. break;
  1633. end;
  1634. else
  1635. break;
  1636. end;
  1637. end;
  1638. end;
  1639. until false;
  1640. { check for incomplete class definitions, this is only required
  1641. for fpc modes }
  1642. if (m_fpc in current_settings.modeswitches) then
  1643. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  1644. end;
  1645. {****************************************************************************
  1646. SPECIALIZATION BODY GENERATION
  1647. ****************************************************************************}
  1648. procedure specialize_objectdefs(p:TObject;arg:pointer);
  1649. var
  1650. i : longint;
  1651. hp : tdef;
  1652. oldcurrent_filepos : tfileposinfo;
  1653. oldsymtablestack : tsymtablestack;
  1654. pu : tused_unit;
  1655. hmodule : tmodule;
  1656. specobj : tobjectdef;
  1657. begin
  1658. if not((tsym(p).typ=typesym) and
  1659. (ttypesym(p).typedef.typesym=tsym(p)) and
  1660. (ttypesym(p).typedef.typ=objectdef) and
  1661. (df_specialization in ttypesym(p).typedef.defoptions)
  1662. ) then
  1663. exit;
  1664. { Setup symtablestack a definition time }
  1665. specobj:=tobjectdef(ttypesym(p).typedef);
  1666. oldsymtablestack:=symtablestack;
  1667. symtablestack:=tsymtablestack.create;
  1668. if not assigned(tobjectdef(ttypesym(p).typedef).genericdef) then
  1669. internalerror(200705151);
  1670. hmodule:=find_module_from_symtable(specobj.genericdef.owner);
  1671. if hmodule=nil then
  1672. internalerror(200705152);
  1673. pu:=tused_unit(hmodule.used_units.first);
  1674. while assigned(pu) do
  1675. begin
  1676. if not assigned(pu.u.globalsymtable) then
  1677. internalerror(200705153);
  1678. symtablestack.push(pu.u.globalsymtable);
  1679. pu:=tused_unit(pu.next);
  1680. end;
  1681. if assigned(hmodule.globalsymtable) then
  1682. symtablestack.push(hmodule.globalsymtable);
  1683. if assigned(hmodule.localsymtable) then
  1684. symtablestack.push(hmodule.localsymtable);
  1685. { procedure definitions for classes or objects }
  1686. if is_class(specobj) or is_object(specobj) then
  1687. begin
  1688. for i:=0 to specobj.symtable.DefList.Count-1 do
  1689. begin
  1690. hp:=tdef(specobj.symtable.DefList[i]);
  1691. if hp.typ=procdef then
  1692. begin
  1693. if assigned(tprocdef(hp).genericdef) and
  1694. (tprocdef(hp).genericdef.typ=procdef) and
  1695. assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf) then
  1696. begin
  1697. oldcurrent_filepos:=current_filepos;
  1698. current_filepos:=tprocdef(tprocdef(hp).genericdef).fileinfo;
  1699. { use the index the module got from the current compilation process }
  1700. current_filepos.moduleindex:=hmodule.unit_index;
  1701. current_tokenpos:=current_filepos;
  1702. current_scanner.startreplaytokens(tprocdef(tprocdef(hp).genericdef).generictokenbuf);
  1703. read_proc_body(nil,tprocdef(hp));
  1704. current_filepos:=oldcurrent_filepos;
  1705. end
  1706. else
  1707. MessagePos1(tprocdef(hp).fileinfo,sym_e_forward_not_resolved,tprocdef(hp).fullprocname(false));
  1708. end;
  1709. end;
  1710. end;
  1711. { Restore symtablestack }
  1712. symtablestack.free;
  1713. symtablestack:=oldsymtablestack;
  1714. end;
  1715. procedure generate_specialization_procs;
  1716. begin
  1717. if assigned(current_module.globalsymtable) then
  1718. current_module.globalsymtable.SymList.ForEachCall(@specialize_objectdefs,nil);
  1719. if assigned(current_module.localsymtable) then
  1720. current_module.localsymtable.SymList.ForEachCall(@specialize_objectdefs,nil);
  1721. end;
  1722. end.