psub.pas 54 KB

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