psub.pas 58 KB

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