psub.pas 74 KB

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