psub.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing and codegeneration at subroutine level
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit psub;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,globals,
  23. node,nbas,
  24. symdef,procinfo;
  25. type
  26. tcgprocinfo = class(tprocinfo)
  27. private
  28. procedure add_entry_exit_code;
  29. public
  30. { code for the subroutine as tree }
  31. code : tnode;
  32. { positions in the tree for init/final }
  33. entry_asmnode,
  34. loadpara_asmnode,
  35. exitlabel_asmnode,
  36. init_asmnode,
  37. final_asmnode : tasmnode;
  38. { list to store the procinfo's of the nested procedures }
  39. nestedprocs : tlinkedlist;
  40. constructor create(aparent:tprocinfo);override;
  41. destructor destroy;override;
  42. procedure generate_code;
  43. procedure resetprocdef;
  44. procedure add_to_symtablestack;
  45. procedure remove_from_symtablestack;
  46. procedure parse_body;
  47. end;
  48. procedure printnode_reset;
  49. { reads the declaration blocks }
  50. procedure read_declarations(islibrary : boolean);
  51. { reads declarations in the interface part of a unit }
  52. procedure read_interface_declarations;
  53. implementation
  54. uses
  55. { common }
  56. cutils,
  57. { global }
  58. globtype,tokens,verbose,comphook,
  59. systems,
  60. { aasm }
  61. cpubase,aasmbase,aasmtai,
  62. { symtable }
  63. symconst,symbase,symsym,symtype,symtable,defutil,
  64. paramgr,
  65. ppu,fmodule,
  66. { pass 1 }
  67. nutils,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  68. pass_1,
  69. {$ifdef state_tracking}
  70. nstate,
  71. {$endif state_tracking}
  72. { pass 2 }
  73. {$ifndef NOPASS2}
  74. pass_2,
  75. {$endif}
  76. { parser }
  77. scanner,
  78. pbase,pstatmnt,pdecl,pdecsub,pexports,
  79. { codegen }
  80. tgobj,cgobj,
  81. ncgutil,regvars
  82. {$ifdef arm}
  83. ,aasmcpu
  84. {$endif arm}
  85. {$ifndef NOOPT}
  86. {$ifdef i386}
  87. ,aopt386
  88. {$else i386}
  89. ,aoptcpu
  90. {$endif i386}
  91. {$endif}
  92. ;
  93. {****************************************************************************
  94. PROCEDURE/FUNCTION BODY PARSING
  95. ****************************************************************************}
  96. procedure initializevars(p:tnamedindexitem;arg:pointer);
  97. var
  98. b : tblocknode;
  99. begin
  100. if tsym(p).typ<>varsym then
  101. exit;
  102. with tvarsym(p) do
  103. begin
  104. if assigned(defaultconstsym) then
  105. begin
  106. b:=tblocknode(arg);
  107. b.left:=cstatementnode.create(
  108. cassignmentnode.create(
  109. cloadnode.create(tsym(p),tsym(p).owner),
  110. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  111. b.left);
  112. end;
  113. end;
  114. end;
  115. procedure check_finalize_paras(p : tnamedindexitem;arg:pointer);
  116. begin
  117. if (tsym(p).typ=varsym) and
  118. (tvarsym(p).varspez=vs_value) and
  119. not is_class(tvarsym(p).vartype.def) and
  120. tvarsym(p).vartype.def.needs_inittable then
  121. include(current_procinfo.flags,pi_needs_implicit_finally);
  122. end;
  123. procedure check_finalize_locals(p : tnamedindexitem;arg:pointer);
  124. begin
  125. case tsym(p).typ of
  126. varsym :
  127. begin
  128. if (tvarsym(p).refs>0) and
  129. not(vo_is_funcret in tvarsym(p).varoptions) and
  130. not(is_class(tvarsym(p).vartype.def)) and
  131. tvarsym(p).vartype.def.needs_inittable then
  132. include(current_procinfo.flags,pi_needs_implicit_finally);
  133. end;
  134. {
  135. must be done at the end of the program, not at the end of the procedure
  136. typedconstsym :
  137. begin
  138. if ttypedconstsym(p).typedconsttype.def.needs_inittable then
  139. include(current_procinfo.flags,pi_needs_implicit_finally);
  140. end;
  141. }
  142. end;
  143. end;
  144. function block(islibrary : boolean) : tnode;
  145. begin
  146. { parse const,types and vars }
  147. read_declarations(islibrary);
  148. { do we have an assembler block without the po_assembler?
  149. we should allow this for Delphi compatibility (PFV) }
  150. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  151. include(current_procinfo.procdef.procoptions,po_assembler);
  152. { Handle assembler block different }
  153. if (po_assembler in current_procinfo.procdef.procoptions) then
  154. begin
  155. block:=assembler_block;
  156. exit;
  157. end;
  158. {Unit initialization?.}
  159. if (
  160. assigned(current_procinfo.procdef.localst) and
  161. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  162. (current_module.is_unit)
  163. ) or
  164. islibrary then
  165. begin
  166. if (token=_END) then
  167. begin
  168. consume(_END);
  169. { We need at least a node, else the entry/exit code is not
  170. generated and thus no PASCALMAIN symbol which we need (PFV) }
  171. if islibrary then
  172. block:=cnothingnode.create
  173. else
  174. block:=nil;
  175. end
  176. else
  177. begin
  178. if token=_INITIALIZATION then
  179. begin
  180. { The library init code is already called and does not
  181. need to be in the initfinal table (PFV) }
  182. if not islibrary then
  183. current_module.flags:=current_module.flags or uf_init;
  184. block:=statement_block(_INITIALIZATION);
  185. end
  186. else if (token=_FINALIZATION) then
  187. begin
  188. if (current_module.flags and uf_finalize)<>0 then
  189. block:=statement_block(_FINALIZATION)
  190. else
  191. begin
  192. { can we allow no INITIALIZATION for DLL ??
  193. I think it should work PM }
  194. block:=nil;
  195. exit;
  196. end;
  197. end
  198. else
  199. begin
  200. { The library init code is already called and does not
  201. need to be in the initfinal table (PFV) }
  202. if not islibrary then
  203. current_module.flags:=current_module.flags or uf_init;
  204. block:=statement_block(_BEGIN);
  205. end;
  206. end;
  207. end
  208. else
  209. begin
  210. block:=statement_block(_BEGIN);
  211. if symtablestack.symtabletype=localsymtable then
  212. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}initializevars,block);
  213. end;
  214. end;
  215. {****************************************************************************
  216. PROCEDURE/FUNCTION COMPILING
  217. ****************************************************************************}
  218. procedure printnode_reset;
  219. begin
  220. assign(printnodefile,treelogfilename);
  221. {$I-}
  222. rewrite(printnodefile);
  223. {$I+}
  224. if ioresult<>0 then
  225. begin
  226. Comment(V_Error,'Error creating '+treelogfilename);
  227. exit;
  228. end;
  229. close(printnodefile);
  230. end;
  231. procedure printnode_procdef(pd:tprocdef);
  232. begin
  233. assign(printnodefile,treelogfilename);
  234. {$I-}
  235. append(printnodefile);
  236. if ioresult<>0 then
  237. rewrite(printnodefile);
  238. {$I+}
  239. if ioresult<>0 then
  240. begin
  241. Comment(V_Error,'Error creating '+treelogfilename);
  242. exit;
  243. end;
  244. writeln(printnodefile);
  245. writeln(printnodefile,'*******************************************************************************');
  246. writeln(printnodefile,current_procinfo.procdef.fullprocname(false));
  247. writeln(printnodefile,'*******************************************************************************');
  248. printnode(printnodefile,pd.inlininginfo^.code);
  249. close(printnodefile);
  250. end;
  251. function generate_bodyentry_block:tnode;
  252. var
  253. srsym : tsym;
  254. para : tcallparanode;
  255. newstatement : tstatementnode;
  256. htype : ttype;
  257. begin
  258. result:=internalstatements(newstatement);
  259. if assigned(current_procinfo.procdef._class) then
  260. begin
  261. { a constructor needs a help procedure }
  262. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  263. begin
  264. if is_class(current_procinfo.procdef._class) then
  265. begin
  266. include(current_procinfo.flags,pi_needs_implicit_finally);
  267. srsym:=search_class_member(current_procinfo.procdef._class,'NEWINSTANCE');
  268. if assigned(srsym) and
  269. (srsym.typ=procsym) then
  270. begin
  271. { if vmt<>0 then newinstance }
  272. addstatement(newstatement,cifnode.create(
  273. caddnode.create(unequaln,
  274. load_vmt_pointer_node,
  275. cnilnode.create),
  276. cassignmentnode.create(
  277. ctypeconvnode.create_explicit(
  278. load_self_pointer_node,
  279. voidpointertype),
  280. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_vmt_pointer_node,[])),
  281. nil));
  282. end
  283. else
  284. internalerror(200305108);
  285. end
  286. else
  287. if is_object(current_procinfo.procdef._class) then
  288. begin
  289. htype.setdef(current_procinfo.procdef._class);
  290. htype.setdef(tpointerdef.create(htype));
  291. { parameter 3 : vmt_offset }
  292. { parameter 2 : address of pointer to vmt,
  293. this is required to allow setting the vmt to -1 to indicate
  294. that memory was allocated }
  295. { parameter 1 : self pointer }
  296. para:=ccallparanode.create(
  297. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  298. ccallparanode.create(
  299. ctypeconvnode.create_explicit(
  300. load_vmt_pointer_node,
  301. voidpointertype),
  302. ccallparanode.create(
  303. ctypeconvnode.create_explicit(
  304. load_self_pointer_node,
  305. voidpointertype),
  306. nil)));
  307. addstatement(newstatement,cassignmentnode.create(
  308. ctypeconvnode.create_explicit(
  309. load_self_pointer_node,
  310. voidpointertype),
  311. ccallnode.createintern('fpc_help_constructor',para)));
  312. end
  313. else
  314. internalerror(200305103);
  315. { if self=nil then exit
  316. calling fail instead of exit is useless because
  317. there is nothing to dispose (PFV) }
  318. addstatement(newstatement,cifnode.create(
  319. caddnode.create(equaln,
  320. load_self_pointer_node,
  321. cnilnode.create),
  322. cexitnode.create(nil),
  323. nil));
  324. end;
  325. { maybe call BeforeDestruction for classes }
  326. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  327. is_class(current_procinfo.procdef._class) then
  328. begin
  329. srsym:=search_class_member(current_procinfo.procdef._class,'BEFOREDESTRUCTION');
  330. if assigned(srsym) and
  331. (srsym.typ=procsym) then
  332. begin
  333. { if vmt<>0 then beforedestruction }
  334. addstatement(newstatement,cifnode.create(
  335. caddnode.create(unequaln,
  336. load_vmt_pointer_node,
  337. cnilnode.create),
  338. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  339. nil));
  340. end
  341. else
  342. internalerror(200305104);
  343. end;
  344. end;
  345. end;
  346. function generate_bodyexit_block:tnode;
  347. var
  348. srsym : tsym;
  349. para : tcallparanode;
  350. newstatement : tstatementnode;
  351. begin
  352. result:=internalstatements(newstatement);
  353. if assigned(current_procinfo.procdef._class) then
  354. begin
  355. { maybe call AfterConstruction for classes }
  356. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  357. is_class(current_procinfo.procdef._class) then
  358. begin
  359. srsym:=search_class_member(current_procinfo.procdef._class,'AFTERCONSTRUCTION');
  360. if assigned(srsym) and
  361. (srsym.typ=procsym) then
  362. begin
  363. { Self can be nil when fail is called }
  364. { if self<>nil and vmt<>nil then afterconstruction }
  365. addstatement(newstatement,cifnode.create(
  366. caddnode.create(andn,
  367. caddnode.create(unequaln,
  368. load_self_pointer_node,
  369. cnilnode.create),
  370. caddnode.create(unequaln,
  371. load_vmt_pointer_node,
  372. cnilnode.create)),
  373. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  374. nil));
  375. end
  376. else
  377. internalerror(200305106);
  378. end;
  379. { a destructor needs a help procedure }
  380. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  381. begin
  382. if is_class(current_procinfo.procdef._class) then
  383. begin
  384. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  385. if assigned(srsym) and
  386. (srsym.typ=procsym) then
  387. begin
  388. { if self<>0 and vmt=1 then freeinstance }
  389. addstatement(newstatement,cifnode.create(
  390. caddnode.create(andn,
  391. caddnode.create(unequaln,
  392. load_self_pointer_node,
  393. cnilnode.create),
  394. caddnode.create(equaln,
  395. ctypeconvnode.create(
  396. load_vmt_pointer_node,
  397. voidpointertype),
  398. cpointerconstnode.create(1,voidpointertype))),
  399. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  400. nil));
  401. end
  402. else
  403. internalerror(200305108);
  404. end
  405. else
  406. if is_object(current_procinfo.procdef._class) then
  407. begin
  408. { finalize object data }
  409. if current_procinfo.procdef._class.needs_inittable then
  410. addstatement(newstatement,finalize_data_node(load_self_node));
  411. { parameter 3 : vmt_offset }
  412. { parameter 2 : pointer to vmt }
  413. { parameter 1 : self pointer }
  414. para:=ccallparanode.create(
  415. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  416. ccallparanode.create(
  417. ctypeconvnode.create_explicit(
  418. load_vmt_pointer_node,
  419. voidpointertype),
  420. ccallparanode.create(
  421. ctypeconvnode.create_explicit(
  422. load_self_pointer_node,
  423. voidpointertype),
  424. nil)));
  425. addstatement(newstatement,
  426. ccallnode.createintern('fpc_help_destructor',para));
  427. end
  428. else
  429. internalerror(200305105);
  430. end;
  431. end;
  432. end;
  433. function generate_except_block:tnode;
  434. var
  435. pd : tprocdef;
  436. newstatement : tstatementnode;
  437. dummycall : tcallnode;
  438. begin
  439. generate_except_block:=internalstatements(newstatement);
  440. { a constructor needs call destructor (if available) when it
  441. is not inherited }
  442. if assigned(current_procinfo.procdef._class) and
  443. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  444. begin
  445. pd:=current_procinfo.procdef._class.searchdestructor;
  446. if assigned(pd) then
  447. begin
  448. { if vmt<>0 then call destructor }
  449. addstatement(newstatement,cifnode.create(
  450. caddnode.create(unequaln,
  451. load_vmt_pointer_node,
  452. cnilnode.create),
  453. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[]),
  454. nil));
  455. end;
  456. end
  457. else
  458. begin
  459. { no constructor }
  460. { must be the return value finalized before reraising the exception? }
  461. if (not is_void(current_procinfo.procdef.rettype.def)) and
  462. (current_procinfo.procdef.rettype.def.needs_inittable) and
  463. (not is_class(current_procinfo.procdef.rettype.def)) then
  464. addstatement(newstatement,finalize_data_node(load_result_node));
  465. end;
  466. end;
  467. {****************************************************************************
  468. TCGProcInfo
  469. ****************************************************************************}
  470. constructor tcgprocinfo.create(aparent:tprocinfo);
  471. begin
  472. inherited Create(aparent);
  473. nestedprocs:=tlinkedlist.create;
  474. end;
  475. destructor tcgprocinfo.destroy;
  476. begin
  477. nestedprocs.free;
  478. if assigned(code) then
  479. code.free;
  480. inherited destroy;
  481. end;
  482. procedure tcgprocinfo.add_entry_exit_code;
  483. var
  484. finalcode,
  485. bodyentrycode,
  486. bodyexitcode,
  487. exceptcode : tnode;
  488. newblock : tblocknode;
  489. codestatement,
  490. newstatement : tstatementnode;
  491. oldfilepos : tfileposinfo;
  492. begin
  493. oldfilepos:=aktfilepos;
  494. { Generate code/locations used at start of proc }
  495. aktfilepos:=entrypos;
  496. entry_asmnode:=casmnode.create_get_position;
  497. loadpara_asmnode:=casmnode.create_get_position;
  498. init_asmnode:=casmnode.create_get_position;
  499. bodyentrycode:=generate_bodyentry_block;
  500. { Generate code/locations used at end of proc }
  501. aktfilepos:=exitpos;
  502. exitlabel_asmnode:=casmnode.create_get_position;
  503. final_asmnode:=casmnode.create_get_position;
  504. bodyexitcode:=generate_bodyexit_block;
  505. { Generate procedure by combining init+body+final,
  506. depending on the implicit finally we need to add
  507. an try...finally...end wrapper }
  508. newblock:=internalstatements(newstatement);
  509. if (cs_implicit_exceptions in aktmoduleswitches) and
  510. (pi_needs_implicit_finally in flags) and
  511. { but it's useless in init/final code of units }
  512. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  513. begin
  514. { Generate special exception block only needed when
  515. implicit finaly is used }
  516. aktfilepos:=exitpos;
  517. exceptcode:=generate_except_block;
  518. { Generate code that will be in the try...finally }
  519. finalcode:=internalstatements(codestatement);
  520. addstatement(codestatement,bodyexitcode);
  521. addstatement(codestatement,final_asmnode);
  522. { Initialize before try...finally...end frame }
  523. addstatement(newstatement,loadpara_asmnode);
  524. addstatement(newstatement,entry_asmnode);
  525. addstatement(newstatement,init_asmnode);
  526. addstatement(newstatement,bodyentrycode);
  527. aktfilepos:=entrypos;
  528. addstatement(newstatement,ctryfinallynode.create_implicit(
  529. code,
  530. finalcode,
  531. exceptcode));
  532. addstatement(newstatement,exitlabel_asmnode);
  533. { set flag the implicit finally has been generated }
  534. include(flags,pi_has_implicit_finally);
  535. end
  536. else
  537. begin
  538. addstatement(newstatement,loadpara_asmnode);
  539. addstatement(newstatement,entry_asmnode);
  540. addstatement(newstatement,init_asmnode);
  541. addstatement(newstatement,bodyentrycode);
  542. addstatement(newstatement,code);
  543. addstatement(newstatement,exitlabel_asmnode);
  544. addstatement(newstatement,bodyexitcode);
  545. addstatement(newstatement,final_asmnode);
  546. end;
  547. do_firstpass(newblock);
  548. code:=newblock;
  549. aktfilepos:=oldfilepos;
  550. end;
  551. procedure clearrefs(p : tnamedindexitem;arg:pointer);
  552. begin
  553. if (tsym(p).typ=varsym) then
  554. if tvarsym(p).refs>1 then
  555. tvarsym(p).refs:=1;
  556. end;
  557. procedure tcgprocinfo.generate_code;
  558. var
  559. oldprocinfo : tprocinfo;
  560. oldaktmaxfpuregisters : longint;
  561. oldfilepos : tfileposinfo;
  562. templist : Taasmoutput;
  563. headertai : tai;
  564. begin
  565. { the initialization procedure can be empty, then we
  566. don't need to generate anything. When it was an empty
  567. procedure there would be at least a blocknode }
  568. if not assigned(code) then
  569. exit;
  570. { We need valid code }
  571. if Errorcount<>0 then
  572. exit;
  573. { The RA and Tempgen shall not be available yet }
  574. if assigned(tg) then
  575. internalerror(200309201);
  576. oldprocinfo:=current_procinfo;
  577. oldfilepos:=aktfilepos;
  578. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  579. current_procinfo:=self;
  580. aktfilepos:=entrypos;
  581. { get new labels }
  582. aktbreaklabel:=nil;
  583. aktcontinuelabel:=nil;
  584. templist:=Taasmoutput.create;
  585. { add parast/localst to symtablestack }
  586. add_to_symtablestack;
  587. { when size optimization only count occurrence }
  588. if cs_littlesize in aktglobalswitches then
  589. cg.t_times:=1
  590. else
  591. { reference for repetition is 100 }
  592. cg.t_times:=100;
  593. { clear register count }
  594. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  595. symtablestack.next.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  596. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  597. if (procdef.localst.symtablelevel=main_program_level) and
  598. (not current_module.is_unit) then
  599. include(flags,pi_do_call);
  600. { set implicit_finally flag when there are locals/paras to be finalized }
  601. current_procinfo.procdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_finalize_paras,nil);
  602. current_procinfo.procdef.localst.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_finalize_locals,nil);
  603. { firstpass everything }
  604. flowcontrol:=[];
  605. do_firstpass(code);
  606. if code.registersfpu>0 then
  607. include(current_procinfo.flags,pi_uses_fpu);
  608. { add implicit entry and exit code }
  609. add_entry_exit_code;
  610. { only do secondpass if there are no errors }
  611. if ErrorCount=0 then
  612. begin
  613. { set the start offset to the start of the temp area in the stack }
  614. tg:=ttgobj.create;
  615. { Create register allocator }
  616. cg.init_register_allocators;
  617. set_first_temp_offset;
  618. generate_parameter_info;
  619. { Allocate space in temp/registers for parast and localst }
  620. aktfilepos:=entrypos;
  621. gen_alloc_parast(aktproccode,tparasymtable(procdef.parast));
  622. if procdef.localst.symtabletype=localsymtable then
  623. gen_alloc_localst(aktproccode,tlocalsymtable(procdef.localst));
  624. { Store temp offset for information about 'real' temps }
  625. tempstart:=tg.lasttemp;
  626. { Generate code to load register parameters in temps and insert local
  627. copies for values parameters. This must be done before the code for the
  628. body is generated because the localloc is updated.
  629. Note: The generated code will be inserted after the code generation of
  630. the body is finished, because only then the position is known }
  631. {$ifdef oldregvars}
  632. assign_regvars(code);
  633. {$endif oldreg}
  634. aktfilepos:=entrypos;
  635. gen_load_para_value(templist);
  636. { caller paraloc info is also necessary in the stackframe_entry
  637. code of the ppc (and possibly other processors) }
  638. if not procdef.has_paraloc_info then
  639. begin
  640. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  641. procdef.has_paraloc_info:=true;
  642. end;
  643. { generate code for the node tree }
  644. do_secondpass(code);
  645. aktproccode.concatlist(exprasmlist);
  646. {$ifdef i386}
  647. procdef.fpu_used:=code.registersfpu;
  648. {$endif i386}
  649. { The position of the loadpara_asmnode is now known }
  650. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  651. { first generate entry and initialize code with the correct
  652. position and switches }
  653. aktfilepos:=entrypos;
  654. aktlocalswitches:=entryswitches;
  655. gen_entry_code(templist);
  656. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  657. gen_initialize_code(templist,false);
  658. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  659. { now generate finalize and exit code with the correct position
  660. and switches }
  661. aktfilepos:=exitpos;
  662. aktlocalswitches:=exitswitches;
  663. gen_finalize_code(templist,false);
  664. { the finalcode must be concated if there was no position available,
  665. using insertlistafter will result in an insert at the start
  666. when currentai=nil }
  667. if assigned(final_asmnode.currenttai) then
  668. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  669. else
  670. aktproccode.concatlist(templist);
  671. { insert exit label at the correct position }
  672. cg.a_label(templist,aktexitlabel);
  673. if assigned(exitlabel_asmnode.currenttai) then
  674. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  675. else
  676. aktproccode.concatlist(templist);
  677. { exit code }
  678. gen_exit_code(templist);
  679. aktproccode.concatlist(templist);
  680. {$ifdef OLDREGVARS}
  681. { note: this must be done only after as much code as possible has }
  682. { been generated. The result is that when you ungetregister() a }
  683. { regvar, it will actually free the regvar (and alse free the }
  684. { the regvars at the same time). Doing this too early will }
  685. { confuse the register allocator, as the regvars will still be }
  686. { used. It should be done before loading the result regs (so }
  687. { they don't conflict with the regvars) and before }
  688. { gen_entry_code (that one has to be able to allocate the }
  689. { regvars again) (JM) }
  690. free_regvars(aktproccode);
  691. {$endif OLDREGVARS}
  692. { add code that will load the return value, this is not done
  693. for assembler routines when they didn't reference the result
  694. variable }
  695. gen_load_return_value(templist);
  696. aktproccode.concatlist(templist);
  697. { generate symbol and save end of header position }
  698. aktfilepos:=entrypos;
  699. gen_proc_symbol(templist);
  700. headertai:=tai(templist.last);
  701. { insert symbol }
  702. aktproccode.insertlist(templist);
  703. { Free space in temp/registers for parast and localst, must be
  704. done after gen_entry_code }
  705. aktfilepos:=exitpos;
  706. if procdef.localst.symtabletype=localsymtable then
  707. gen_free_localst(aktproccode,tlocalsymtable(procdef.localst));
  708. gen_free_parast(aktproccode,tparasymtable(procdef.parast));
  709. { The procedure body is finished, we can now
  710. allocate the registers }
  711. cg.do_register_allocation(aktproccode,headertai);
  712. { Add save and restore of used registers }
  713. aktfilepos:=entrypos;
  714. gen_save_used_regs(templist);
  715. aktproccode.insertlistafter(headertai,templist);
  716. aktfilepos:=exitpos;
  717. gen_restore_used_regs(aktproccode,procdef.funcret_paraloc[calleeside]);
  718. { Add entry code (stack allocation) after header }
  719. aktfilepos:=entrypos;
  720. gen_proc_entry_code(templist);
  721. aktproccode.insertlistafter(headertai,templist);
  722. { Add exit code at the end }
  723. aktfilepos:=exitpos;
  724. gen_proc_exit_code(templist);
  725. aktproccode.concatlist(templist);
  726. { check if the implicit finally has been generated. The flag
  727. should already be set in pass1 }
  728. if (cs_implicit_exceptions in aktmoduleswitches) and
  729. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  730. (pi_needs_implicit_finally in flags) and
  731. not(pi_has_implicit_finally in flags) then
  732. internalerror(200405231);
  733. {$ifndef NoOpt}
  734. if not(cs_no_regalloc in aktglobalswitches) then
  735. begin
  736. if (cs_optimize in aktglobalswitches) and
  737. { do not optimize pure assembler procedures }
  738. not(pi_is_assembler in flags) then
  739. optimize(aktproccode);
  740. end;
  741. {$endif NoOpt}
  742. { Add end symbol and debug info }
  743. aktfilepos:=exitpos;
  744. gen_proc_symbol_end(templist);
  745. aktproccode.concatlist(templist);
  746. {$ifdef ARM}
  747. insertpcrelativedata(aktproccode,aktlocaldata);
  748. {$endif ARM}
  749. { save local data (casetable) also in the same file }
  750. if assigned(aktlocaldata) and
  751. (not aktlocaldata.empty) then
  752. begin
  753. { because of the limited constant size of the arm, all data access is done pc relative }
  754. if target_info.cpu=cpu_arm then
  755. aktproccode.concatlist(aktlocaldata)
  756. else
  757. begin
  758. new_section(aktproccode,sec_data,lower(procdef.mangledname),0);
  759. aktproccode.concatlist(aktlocaldata);
  760. end;
  761. end;
  762. { add the procedure to the codesegment }
  763. maybe_new_object_file(codesegment);
  764. new_section(codesegment,sec_code,lower(procdef.mangledname),aktalignment.procalign);
  765. codesegment.concatlist(aktproccode);
  766. { only now we can remove the temps }
  767. tg.resettempgen;
  768. { stop tempgen and ra }
  769. tg.free;
  770. cg.done_register_allocators;
  771. tg:=nil;
  772. end;
  773. { restore symtablestack }
  774. remove_from_symtablestack;
  775. { restore }
  776. templist.free;
  777. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  778. aktfilepos:=oldfilepos;
  779. current_procinfo:=oldprocinfo;
  780. end;
  781. procedure tcgprocinfo.add_to_symtablestack;
  782. var
  783. _class,hp : tobjectdef;
  784. begin
  785. { insert symtables for the class, but only if it is no nested function }
  786. if assigned(procdef._class) and
  787. not(assigned(parent) and
  788. assigned(parent.procdef) and
  789. assigned(parent.procdef._class)) then
  790. begin
  791. { insert them in the reverse order }
  792. hp:=nil;
  793. repeat
  794. _class:=procdef._class;
  795. while _class.childof<>hp do
  796. _class:=_class.childof;
  797. hp:=_class;
  798. _class.symtable.next:=symtablestack;
  799. symtablestack:=_class.symtable;
  800. until hp=procdef._class;
  801. end;
  802. { insert parasymtable in symtablestack when parsing
  803. a function }
  804. if procdef.parast.symtablelevel>=normal_function_level then
  805. begin
  806. procdef.parast.next:=symtablestack;
  807. symtablestack:=procdef.parast;
  808. end;
  809. procdef.localst.next:=symtablestack;
  810. symtablestack:=procdef.localst;
  811. end;
  812. procedure tcgprocinfo.remove_from_symtablestack;
  813. begin
  814. { remove localst/parast }
  815. if procdef.parast.symtablelevel>=normal_function_level then
  816. symtablestack:=symtablestack.next.next
  817. else
  818. symtablestack:=symtablestack.next;
  819. { remove class member symbol tables }
  820. while symtablestack.symtabletype=objectsymtable do
  821. symtablestack:=symtablestack.next;
  822. end;
  823. procedure tcgprocinfo.resetprocdef;
  824. begin
  825. { the local symtables can be deleted, but the parast }
  826. { doesn't, (checking definitons when calling a }
  827. { function }
  828. { not for a inline procedure !! (PM) }
  829. { at lexlevel = 1 localst is the staticsymtable itself }
  830. { so no dispose here !! }
  831. { The localst is also needed for debuginfo }
  832. if assigned(code) and
  833. not(cs_debuginfo in aktmoduleswitches) and
  834. not(cs_browser in aktmoduleswitches) and
  835. (procdef.proccalloption<>pocall_inline) then
  836. begin
  837. if procdef.parast.symtablelevel>=normal_function_level then
  838. procdef.localst.free;
  839. procdef.localst:=nil;
  840. end;
  841. { remove code tree, if not inline procedure }
  842. if assigned(code) then
  843. begin
  844. { the inline procedure has already got a copy of the tree
  845. stored in current_procinfo.procdef.code }
  846. code.free;
  847. code:=nil;
  848. if (procdef.proccalloption<>pocall_inline) then
  849. procdef.inlininginfo^.code:=nil;
  850. end;
  851. end;
  852. function containsforbiddennode(var n: tnode; arg: pointer): foreachnoderesult;
  853. begin
  854. if (n.nodetype <> exitn) then
  855. result := fen_false
  856. else
  857. result := fen_norecurse_true;
  858. end;
  859. function checknodeinlining(procdef: tprocdef): boolean;
  860. var
  861. paraitem: tparaitem;
  862. begin
  863. result := false;
  864. if not assigned(procdef.inlininginfo^.code) or
  865. (po_assembler in procdef.procoptions) then
  866. exit;
  867. paraitem:=tparaitem(procdef.para.first);
  868. while assigned(paraitem) do
  869. begin
  870. { we can't handle formaldefs and special arrays (the latter may need a }
  871. { re-basing of the index, i.e. if you pass an array[1..10] as open array, }
  872. { you have to add 1 to all index operations if you directly inline it }
  873. if ((paraitem.paratyp in [vs_out,vs_var]) and
  874. (paraitem.paratype.def.deftype=formaldef)) or
  875. is_special_array(paraitem.paratype.def) then
  876. exit;
  877. paraitem := tparaitem(paraitem.next);
  878. end;
  879. { we currently can't handle exit-statements (would exit the caller) }
  880. result := not foreachnodestatic(procdef.inlininginfo^.code,{$ifdef FPCPROCVAR}@{$endif}containsforbiddennode,nil);
  881. end;
  882. procedure tcgprocinfo.parse_body;
  883. var
  884. oldprocinfo : tprocinfo;
  885. oldblock_type : tblock_type;
  886. begin
  887. oldprocinfo:=current_procinfo;
  888. oldblock_type:=block_type;
  889. { reset break and continue labels }
  890. block_type:=bt_body;
  891. current_procinfo:=self;
  892. { calculate the lexical level }
  893. if procdef.parast.symtablelevel>maxnesting then
  894. Message(parser_e_too_much_lexlevel);
  895. { static is also important for local procedures !! }
  896. if (po_staticmethod in procdef.procoptions) then
  897. allow_only_static:=true
  898. else if (procdef.parast.symtablelevel=normal_function_level) then
  899. allow_only_static:=false;
  900. {$ifdef state_tracking}
  901. { aktstate:=Tstate_storage.create;}
  902. {$endif state_tracking}
  903. { create a local symbol table for this routine }
  904. if not assigned(procdef.localst) then
  905. procdef.insert_localst;
  906. { add parast/localst to symtablestack }
  907. add_to_symtablestack;
  908. { constant symbols are inserted in this symboltable }
  909. constsymtable:=symtablestack;
  910. { save entry info }
  911. entrypos:=aktfilepos;
  912. entryswitches:=aktlocalswitches;
  913. { parse the code ... }
  914. code:=block(current_module.islibrary);
  915. { save exit info }
  916. exitswitches:=aktlocalswitches;
  917. exitpos:=last_endtoken_filepos;
  918. { the procedure is now defined }
  919. procdef.forwarddef:=false;
  920. if assigned(code) then
  921. begin
  922. { get a better entry point }
  923. entrypos:=code.fileinfo;
  924. { Finish type checking pass }
  925. do_resulttypepass(code);
  926. end;
  927. { Check for unused labels, forwards, symbols for procedures. Static
  928. symtable is checked in pmodules.
  929. The check must be done after the resulttypepass }
  930. if (Errorcount=0) and
  931. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  932. begin
  933. { check if forwards are resolved }
  934. tstoredsymtable(procdef.localst).check_forwards;
  935. { check if all labels are used }
  936. tstoredsymtable(procdef.localst).checklabels;
  937. { remove cross unit overloads }
  938. tstoredsymtable(procdef.localst).unchain_overloaded;
  939. { check for unused symbols, but only if there is no asm block }
  940. if not(pi_uses_asm in flags) then
  941. begin
  942. tstoredsymtable(procdef.localst).allsymbolsused;
  943. tstoredsymtable(procdef.parast).allsymbolsused;
  944. end;
  945. end;
  946. { store a copy of the original tree for inline, for
  947. normal procedures only store a reference to the
  948. current tree }
  949. if (procdef.proccalloption=pocall_inline) then
  950. begin
  951. procdef.inlininginfo^.code:=code.getcopy;
  952. procdef.inlininginfo^.flags:=current_procinfo.flags;
  953. procdef.inlininginfo^.inlinenode:=checknodeinlining(procdef);
  954. end
  955. else
  956. procdef.inlininginfo^.code:=code;
  957. { Print the node to tree.log }
  958. if paraprintnodetree=1 then
  959. printnode_procdef(procdef);
  960. { ... remove symbol tables }
  961. remove_from_symtablestack;
  962. {$ifdef state_tracking}
  963. { aktstate.destroy;}
  964. {$endif state_tracking}
  965. { reset to normal non static function }
  966. if (procdef.parast.symtablelevel=normal_function_level) then
  967. allow_only_static:=false;
  968. current_procinfo:=oldprocinfo;
  969. block_type:=oldblock_type;
  970. end;
  971. {****************************************************************************
  972. PROCEDURE/FUNCTION PARSING
  973. ****************************************************************************}
  974. procedure check_init_paras(p:tnamedindexitem;arg:pointer);
  975. begin
  976. if tsym(p).typ<>varsym then
  977. exit;
  978. with tvarsym(p) do
  979. if (not is_class(vartype.def) and
  980. vartype.def.needs_inittable and
  981. (varspez in [vs_value,vs_out])) then
  982. include(current_procinfo.flags,pi_do_call);
  983. end;
  984. procedure read_proc;
  985. {
  986. Parses the procedure directives, then parses the procedure body, then
  987. generates the code for it
  988. }
  989. procedure do_generate_code(pi:tcgprocinfo);
  990. var
  991. hpi : tcgprocinfo;
  992. begin
  993. { generate code for this procedure }
  994. pi.generate_code;
  995. { process nested procs }
  996. hpi:=tcgprocinfo(pi.nestedprocs.first);
  997. while assigned(hpi) do
  998. begin
  999. do_generate_code(hpi);
  1000. hpi:=tcgprocinfo(hpi.next);
  1001. end;
  1002. pi.resetprocdef;
  1003. end;
  1004. var
  1005. old_current_procinfo : tprocinfo;
  1006. oldconstsymtable : tsymtable;
  1007. oldfailtokenmode : tmodeswitch;
  1008. pdflags : tpdflags;
  1009. pd : tprocdef;
  1010. isnestedproc : boolean;
  1011. begin
  1012. { save old state }
  1013. oldconstsymtable:=constsymtable;
  1014. old_current_procinfo:=current_procinfo;
  1015. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1016. to an other procdef }
  1017. current_procinfo:=nil;
  1018. { parse procedure declaration }
  1019. if assigned(old_current_procinfo) and
  1020. assigned(old_current_procinfo.procdef) then
  1021. pd:=parse_proc_dec(old_current_procinfo.procdef._class)
  1022. else
  1023. pd:=parse_proc_dec(nil);
  1024. { set the default function options }
  1025. if parse_only then
  1026. begin
  1027. pd.forwarddef:=true;
  1028. { set also the interface flag, for better error message when the
  1029. implementation doesn't much this header }
  1030. pd.interfacedef:=true;
  1031. include(pd.procoptions,po_public);
  1032. pdflags:=[pd_interface];
  1033. end
  1034. else
  1035. begin
  1036. pdflags:=[pd_body];
  1037. if (not current_module.in_interface) then
  1038. include(pdflags,pd_implemen);
  1039. if (not current_module.is_unit) or
  1040. maybe_smartlink_symbol then
  1041. include(pd.procoptions,po_public);
  1042. pd.forwarddef:=false;
  1043. end;
  1044. { parse the directives that may follow }
  1045. parse_proc_directives(pd,pdflags);
  1046. { hint directives, these can be separated by semicolons here,
  1047. that needs to be handled here with a loop (PFV) }
  1048. while try_consume_hintdirective(pd.symoptions) do
  1049. Consume(_SEMICOLON);
  1050. { Set calling convention }
  1051. handle_calling_convention(pd);
  1052. { everything of the proc definition is known, we can now
  1053. calculate the parameters }
  1054. calc_parast(pd);
  1055. { search for forward declarations }
  1056. if not proc_add_definition(pd) then
  1057. begin
  1058. { A method must be forward defined (in the object declaration) }
  1059. if assigned(pd._class) and
  1060. (not assigned(old_current_procinfo.procdef._class)) then
  1061. begin
  1062. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1063. tprocsym(pd.procsym).write_parameter_lists(pd);
  1064. end
  1065. else
  1066. begin
  1067. { Give a better error if there is a forward def in the interface and only
  1068. a single implementation }
  1069. if (not pd.forwarddef) and
  1070. (not pd.interfacedef) and
  1071. (tprocsym(pd.procsym).procdef_count>1) and
  1072. tprocsym(pd.procsym).first_procdef.forwarddef and
  1073. tprocsym(pd.procsym).first_procdef.interfacedef and
  1074. not(tprocsym(pd.procsym).procdef_count>2) then
  1075. begin
  1076. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1077. tprocsym(pd.procsym).write_parameter_lists(pd);
  1078. end;
  1079. end;
  1080. end;
  1081. { compile procedure when a body is needed }
  1082. if (pd_body in pdflags) then
  1083. begin
  1084. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1085. { create a new procedure }
  1086. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1087. current_module.procinfo:=current_procinfo;
  1088. current_procinfo.procdef:=pd;
  1089. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1090. { Insert mangledname }
  1091. pd.aliasnames.insert(pd.mangledname);
  1092. { Insert result variables in the localst }
  1093. insert_funcret_local(pd);
  1094. { check if there are para's which require initing -> set }
  1095. { pi_do_call (if not yet set) }
  1096. if not(pi_do_call in current_procinfo.flags) then
  1097. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_init_paras,nil);
  1098. { set _FAIL as keyword if constructor }
  1099. if (pd.proctypeoption=potype_constructor) then
  1100. begin
  1101. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1102. tokeninfo^[_FAIL].keyword:=m_all;
  1103. end;
  1104. tcgprocinfo(current_procinfo).parse_body;
  1105. { When it's a nested procedure then defer the code generation,
  1106. when back at normal function level then generate the code
  1107. for all defered nested procedures and the current procedure }
  1108. if isnestedproc then
  1109. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1110. else
  1111. begin
  1112. { We can't support inlining for procedures that have nested
  1113. procedures because the nested procedures use a fixed offset
  1114. for accessing locals in the parent procedure (PFV) }
  1115. if (current_procinfo.procdef.proccalloption=pocall_inline) and
  1116. (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1117. begin
  1118. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1119. Message(parser_w_inlining_disabled);
  1120. current_procinfo.procdef.proccalloption:=pocall_default;
  1121. end;
  1122. do_generate_code(tcgprocinfo(current_procinfo));
  1123. end;
  1124. { reset _FAIL as _SELF normal }
  1125. if (pd.proctypeoption=potype_constructor) then
  1126. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1127. { release procinfo }
  1128. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1129. internalerror(200304274);
  1130. current_module.procinfo:=current_procinfo.parent;
  1131. if not isnestedproc then
  1132. current_procinfo.free;
  1133. consume(_SEMICOLON);
  1134. end;
  1135. { Restore old state }
  1136. constsymtable:=oldconstsymtable;
  1137. current_procinfo:=old_current_procinfo;
  1138. end;
  1139. {****************************************************************************
  1140. DECLARATION PARSING
  1141. ****************************************************************************}
  1142. { search in symtablestack for not complete classes }
  1143. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  1144. begin
  1145. if (tsym(p).typ=typesym) and
  1146. (ttypesym(p).restype.def.deftype=objectdef) and
  1147. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  1148. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1149. end;
  1150. procedure read_declarations(islibrary : boolean);
  1151. begin
  1152. repeat
  1153. if not assigned(current_procinfo) then
  1154. internalerror(200304251);
  1155. case token of
  1156. _LABEL:
  1157. label_dec;
  1158. _CONST:
  1159. const_dec;
  1160. _TYPE:
  1161. type_dec;
  1162. _VAR:
  1163. var_dec;
  1164. _THREADVAR:
  1165. threadvar_dec;
  1166. _CONSTRUCTOR,
  1167. _DESTRUCTOR,
  1168. _FUNCTION,
  1169. _PROCEDURE,
  1170. _OPERATOR,
  1171. _CLASS:
  1172. read_proc;
  1173. _EXPORTS:
  1174. begin
  1175. if not(assigned(current_procinfo.procdef.localst)) or
  1176. (current_procinfo.procdef.localst.symtablelevel>main_program_level) or
  1177. (current_module.is_unit) then
  1178. begin
  1179. Message(parser_e_syntax_error);
  1180. consume_all_until(_SEMICOLON);
  1181. end
  1182. else if islibrary or
  1183. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware,system_i386_netwlibc]) then
  1184. read_exports
  1185. else
  1186. begin
  1187. Message(parser_w_unsupported_feature);
  1188. consume(_BEGIN);
  1189. end;
  1190. end
  1191. else
  1192. begin
  1193. case idtoken of
  1194. _RESOURCESTRING :
  1195. resourcestring_dec;
  1196. _PROPERTY:
  1197. begin
  1198. if (m_fpc in aktmodeswitches) then
  1199. property_dec
  1200. else
  1201. break;
  1202. end;
  1203. else
  1204. break;
  1205. end;
  1206. end;
  1207. end;
  1208. until false;
  1209. { check for incomplete class definitions, this is only required
  1210. for fpc modes }
  1211. if (m_fpc in aktmodeswitches) then
  1212. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1213. end;
  1214. procedure read_interface_declarations;
  1215. begin
  1216. repeat
  1217. case token of
  1218. _CONST :
  1219. const_dec;
  1220. _TYPE :
  1221. type_dec;
  1222. _VAR :
  1223. var_dec;
  1224. _THREADVAR :
  1225. threadvar_dec;
  1226. _FUNCTION,
  1227. _PROCEDURE,
  1228. _OPERATOR :
  1229. read_proc;
  1230. else
  1231. begin
  1232. case idtoken of
  1233. _RESOURCESTRING :
  1234. resourcestring_dec;
  1235. _PROPERTY:
  1236. begin
  1237. if (m_fpc in aktmodeswitches) then
  1238. property_dec
  1239. else
  1240. break;
  1241. end;
  1242. else
  1243. break;
  1244. end;
  1245. end;
  1246. end;
  1247. until false;
  1248. { check for incomplete class definitions, this is only required
  1249. for fpc modes }
  1250. if (m_fpc in aktmodeswitches) then
  1251. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1252. end;
  1253. end.
  1254. {
  1255. $Log$
  1256. Revision 1.204 2004-09-04 21:18:47 armin
  1257. * target netwlibc added (libc is preferred for newer netware versions)
  1258. Revision 1.203 2004/08/14 14:50:42 florian
  1259. * fixed several sparc alignment issues
  1260. + Jonas' inline node patch; non functional yet
  1261. Revision 1.202 2004/07/16 21:11:31 jonas
  1262. - disable node-based inlining of routines with special array parameters
  1263. for now (de indexes of open arrays have to be changed, because on the
  1264. caller-side these routines are not necessarily 0-based)
  1265. Revision 1.201 2004/07/15 19:55:40 jonas
  1266. + (incomplete) node_complexity function to assess the complexity of a
  1267. tree
  1268. + support for inlining value and const parameters at the node level
  1269. (all procedures without local variables and without formal parameters
  1270. can now be inlined at the node level)
  1271. Revision 1.200 2004/07/12 09:14:04 jonas
  1272. * inline procedures at the node tree level, but only under some very
  1273. limited circumstances for now (only procedures, and only if they have
  1274. no or only vs_out/vs_var parameters).
  1275. * fixed ppudump for inline procedures
  1276. * fixed ppudump for ppc
  1277. Revision 1.199 2004/07/10 20:24:34 peter
  1278. * put every proc in a new object file
  1279. Revision 1.198 2004/07/09 22:17:32 peter
  1280. * revert has_localst patch
  1281. * replace aktstaticsymtable/aktglobalsymtable with current_module
  1282. Revision 1.197 2004/07/06 19:52:04 peter
  1283. * fix storing of localst in ppu
  1284. Revision 1.196 2004/06/20 08:55:30 florian
  1285. * logs truncated
  1286. Revision 1.195 2004/06/16 20:07:09 florian
  1287. * dwarf branch merged
  1288. Revision 1.194 2004/05/28 21:14:13 peter
  1289. * first load para's to temps before calling entry code (profile
  1290. Revision 1.193 2004/05/24 17:31:12 peter
  1291. * also check local typed const
  1292. Revision 1.192 2004/05/23 18:28:41 peter
  1293. * methodpointer is loaded into a temp when it was a calln
  1294. Revision 1.191 2004/05/23 15:06:21 peter
  1295. * implicit_finally flag must be set in pass1
  1296. * add check whether the implicit frame is generated when expected
  1297. }