psub.pas 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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 (po_assembler in procdef.procoptions) 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. function checklocalinlining(var n: tnode; arg: pointer): foreachnoderesult;
  855. begin
  856. result:=fen_false;
  857. case n.nodetype of
  858. loadn :
  859. if tloadnode(n).symtableentry.owner.symtabletype=staticsymtable then
  860. result:=fen_norecurse_true;
  861. calln :
  862. if tcallnode(n).procdefinition.owner.symtabletype=staticsymtable then
  863. result:=fen_norecurse_true;
  864. end;
  865. end;
  866. procedure tcgprocinfo.parse_body;
  867. var
  868. oldprocinfo : tprocinfo;
  869. oldblock_type : tblock_type;
  870. begin
  871. oldprocinfo:=current_procinfo;
  872. oldblock_type:=block_type;
  873. { reset break and continue labels }
  874. block_type:=bt_body;
  875. current_procinfo:=self;
  876. { calculate the lexical level }
  877. if procdef.parast.symtablelevel>maxnesting then
  878. Message(parser_e_too_much_lexlevel);
  879. { static is also important for local procedures !! }
  880. if (po_staticmethod in procdef.procoptions) then
  881. allow_only_static:=true
  882. else if (procdef.parast.symtablelevel=normal_function_level) then
  883. allow_only_static:=false;
  884. {$ifdef state_tracking}
  885. { aktstate:=Tstate_storage.create;}
  886. {$endif state_tracking}
  887. { create a local symbol table for this routine }
  888. if not assigned(procdef.localst) then
  889. procdef.insert_localst;
  890. { add parast/localst to symtablestack }
  891. add_to_symtablestack;
  892. { constant symbols are inserted in this symboltable }
  893. constsymtable:=symtablestack;
  894. { save entry info }
  895. entrypos:=aktfilepos;
  896. entryswitches:=aktlocalswitches;
  897. { parse the code ... }
  898. code:=block(current_module.islibrary);
  899. { save exit info }
  900. exitswitches:=aktlocalswitches;
  901. exitpos:=last_endtoken_filepos;
  902. { the procedure is now defined }
  903. procdef.forwarddef:=false;
  904. if assigned(code) then
  905. begin
  906. { get a better entry point }
  907. entrypos:=code.fileinfo;
  908. { Finish type checking pass }
  909. do_resulttypepass(code);
  910. end;
  911. { Check for unused labels, forwards, symbols for procedures. Static
  912. symtable is checked in pmodules.
  913. The check must be done after the resulttypepass }
  914. if (Errorcount=0) and
  915. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  916. begin
  917. { check if forwards are resolved }
  918. tstoredsymtable(procdef.localst).check_forwards;
  919. { check if all labels are used }
  920. tstoredsymtable(procdef.localst).checklabels;
  921. { remove cross unit overloads }
  922. tstoredsymtable(procdef.localst).unchain_overloaded;
  923. { check for unused symbols, but only if there is no asm block }
  924. if not(pi_has_assembler_block in flags) then
  925. begin
  926. tstoredsymtable(procdef.localst).allsymbolsused;
  927. tstoredsymtable(procdef.parast).allsymbolsused;
  928. end;
  929. end;
  930. if (procdef.proccalloption=pocall_inline) then
  931. begin
  932. { Can we inline this procedure? }
  933. if checknodeinlining(procdef) then
  934. begin
  935. new(procdef.inlininginfo);
  936. include(procdef.procoptions,po_has_inlininginfo);
  937. procdef.inlininginfo^.code:=code.getcopy;
  938. procdef.inlininginfo^.flags:=current_procinfo.flags;
  939. { References a local var or proc? }
  940. if foreachnodestatic(procdef.inlininginfo^.code,@checklocalinlining,nil) then
  941. include(procdef.inlininginfo^.flags,pi_inline_local_only);
  942. { The blocknode needs to set an exit label }
  943. if procdef.inlininginfo^.code.nodetype=blockn then
  944. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  945. end;
  946. end;
  947. { Print the node to tree.log }
  948. if paraprintnodetree=1 then
  949. printproc;
  950. { ... remove symbol tables }
  951. remove_from_symtablestack;
  952. {$ifdef state_tracking}
  953. { aktstate.destroy;}
  954. {$endif state_tracking}
  955. { reset to normal non static function }
  956. if (procdef.parast.symtablelevel=normal_function_level) then
  957. allow_only_static:=false;
  958. current_procinfo:=oldprocinfo;
  959. block_type:=oldblock_type;
  960. end;
  961. {****************************************************************************
  962. PROCEDURE/FUNCTION PARSING
  963. ****************************************************************************}
  964. procedure check_init_paras(p:tnamedindexitem;arg:pointer);
  965. begin
  966. if tsym(p).typ<>paravarsym then
  967. exit;
  968. with tparavarsym(p) do
  969. if (not is_class(vartype.def) and
  970. vartype.def.needs_inittable and
  971. (varspez in [vs_value,vs_out])) then
  972. include(current_procinfo.flags,pi_do_call);
  973. end;
  974. procedure read_proc;
  975. {
  976. Parses the procedure directives, then parses the procedure body, then
  977. generates the code for it
  978. }
  979. procedure do_generate_code(pi:tcgprocinfo);
  980. var
  981. hpi : tcgprocinfo;
  982. begin
  983. { generate code for this procedure }
  984. pi.generate_code;
  985. { process nested procs }
  986. hpi:=tcgprocinfo(pi.nestedprocs.first);
  987. while assigned(hpi) do
  988. begin
  989. do_generate_code(hpi);
  990. hpi:=tcgprocinfo(hpi.next);
  991. end;
  992. pi.resetprocdef;
  993. end;
  994. var
  995. old_current_procinfo : tprocinfo;
  996. oldconstsymtable : tsymtable;
  997. oldfailtokenmode : tmodeswitch;
  998. pdflags : tpdflags;
  999. pd : tprocdef;
  1000. isnestedproc : boolean;
  1001. s : string;
  1002. begin
  1003. { save old state }
  1004. oldconstsymtable:=constsymtable;
  1005. old_current_procinfo:=current_procinfo;
  1006. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1007. to an other procdef }
  1008. current_procinfo:=nil;
  1009. { parse procedure declaration }
  1010. if assigned(old_current_procinfo) and
  1011. assigned(old_current_procinfo.procdef) then
  1012. pd:=parse_proc_dec(old_current_procinfo.procdef._class)
  1013. else
  1014. pd:=parse_proc_dec(nil);
  1015. { set the default function options }
  1016. if parse_only then
  1017. begin
  1018. pd.forwarddef:=true;
  1019. { set also the interface flag, for better error message when the
  1020. implementation doesn't much this header }
  1021. pd.interfacedef:=true;
  1022. include(pd.procoptions,po_global);
  1023. pdflags:=[pd_interface];
  1024. end
  1025. else
  1026. begin
  1027. pdflags:=[pd_body];
  1028. if (not current_module.in_interface) then
  1029. include(pdflags,pd_implemen);
  1030. if (not current_module.is_unit) or
  1031. maybe_smartlink_symbol then
  1032. include(pd.procoptions,po_global);
  1033. pd.forwarddef:=false;
  1034. end;
  1035. { parse the directives that may follow }
  1036. parse_proc_directives(pd,pdflags);
  1037. { hint directives, these can be separated by semicolons here,
  1038. that needs to be handled here with a loop (PFV) }
  1039. while try_consume_hintdirective(pd.symoptions) do
  1040. Consume(_SEMICOLON);
  1041. { Set calling convention }
  1042. handle_calling_convention(pd);
  1043. { search for forward declarations }
  1044. if not proc_add_definition(pd) then
  1045. begin
  1046. { A method must be forward defined (in the object declaration) }
  1047. if assigned(pd._class) and
  1048. (not assigned(old_current_procinfo.procdef._class)) then
  1049. begin
  1050. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1051. tprocsym(pd.procsym).write_parameter_lists(pd);
  1052. end
  1053. else
  1054. begin
  1055. { Give a better error if there is a forward def in the interface and only
  1056. a single implementation }
  1057. if (not pd.forwarddef) and
  1058. (not pd.interfacedef) and
  1059. (tprocsym(pd.procsym).procdef_count>1) and
  1060. tprocsym(pd.procsym).first_procdef.forwarddef and
  1061. tprocsym(pd.procsym).first_procdef.interfacedef and
  1062. not(tprocsym(pd.procsym).procdef_count>2) then
  1063. begin
  1064. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1065. tprocsym(pd.procsym).write_parameter_lists(pd);
  1066. end;
  1067. end;
  1068. end;
  1069. { Set mangled name }
  1070. proc_set_mangledname(pd);
  1071. { compile procedure when a body is needed }
  1072. if (pd_body in pdflags) then
  1073. begin
  1074. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1075. { create a new procedure }
  1076. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1077. current_module.procinfo:=current_procinfo;
  1078. current_procinfo.procdef:=pd;
  1079. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1080. { Insert mangledname }
  1081. pd.aliasnames.insert(pd.mangledname);
  1082. { Handle Export of this procedure }
  1083. if (po_exports in pd.procoptions) and
  1084. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1085. begin
  1086. pd.aliasnames.insert(pd.procsym.realname);
  1087. if cs_link_deffile in aktglobalswitches then
  1088. deffile.AddExport(pd.mangledname);
  1089. end;
  1090. { Insert result variables in the localst }
  1091. insert_funcret_local(pd);
  1092. { check if there are para's which require initing -> set }
  1093. { pi_do_call (if not yet set) }
  1094. if not(pi_do_call in current_procinfo.flags) then
  1095. pd.parast.foreach_static(@check_init_paras,nil);
  1096. { set _FAIL as keyword if constructor }
  1097. if (pd.proctypeoption=potype_constructor) then
  1098. begin
  1099. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1100. tokeninfo^[_FAIL].keyword:=m_all;
  1101. end;
  1102. tcgprocinfo(current_procinfo).parse_body;
  1103. { When it's a nested procedure then defer the code generation,
  1104. when back at normal function level then generate the code
  1105. for all defered nested procedures and the current procedure }
  1106. if isnestedproc then
  1107. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1108. else
  1109. begin
  1110. { We can't support inlining for procedures that have nested
  1111. procedures because the nested procedures use a fixed offset
  1112. for accessing locals in the parent procedure (PFV) }
  1113. if (current_procinfo.procdef.proccalloption=pocall_inline) and
  1114. (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1115. begin
  1116. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1117. Message(parser_w_inlining_disabled);
  1118. current_procinfo.procdef.proccalloption:=pocall_default;
  1119. end;
  1120. do_generate_code(tcgprocinfo(current_procinfo));
  1121. end;
  1122. { reset _FAIL as _SELF normal }
  1123. if (pd.proctypeoption=potype_constructor) then
  1124. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1125. { release procinfo }
  1126. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1127. internalerror(200304274);
  1128. current_module.procinfo:=current_procinfo.parent;
  1129. if not isnestedproc then
  1130. current_procinfo.free;
  1131. consume(_SEMICOLON);
  1132. end
  1133. else
  1134. begin
  1135. { Handle imports }
  1136. if (po_external in pd.procoptions) then
  1137. begin
  1138. { External declared in implementation, and there was already a
  1139. forward (or interface) declaration then we need to generate
  1140. a stub that calls the external routine }
  1141. if (not pd.forwarddef) and
  1142. (pd.hasforward) and
  1143. not(
  1144. assigned(pd.import_dll) and
  1145. (target_info.system in [system_i386_win32,system_i386_wdosx,
  1146. system_i386_emx,system_i386_os2])
  1147. ) then
  1148. begin
  1149. s:=proc_get_importname(pd);
  1150. if s<>'' then
  1151. gen_external_stub(codesegment,pd,s);
  1152. end;
  1153. { Import DLL specified? }
  1154. if assigned(pd.import_dll) then
  1155. begin
  1156. { create importlib if not already done }
  1157. if not(current_module.uses_imports) then
  1158. begin
  1159. current_module.uses_imports:=true;
  1160. importlib.preparelib(current_module.realmodulename^);
  1161. end;
  1162. if assigned(pd.import_name) then
  1163. importlib.importprocedure(pd,pd.import_dll^,pd.import_nr,pd.import_name^)
  1164. else
  1165. importlib.importprocedure(pd,pd.import_dll^,pd.import_nr,'');
  1166. end
  1167. else
  1168. begin
  1169. { add import name to external list for DLL scanning }
  1170. if target_info.DllScanSupported then
  1171. current_module.externals.insert(tExternalsItem.create(proc_get_importname(pd)));
  1172. end;
  1173. end;
  1174. end;
  1175. { Restore old state }
  1176. constsymtable:=oldconstsymtable;
  1177. current_procinfo:=old_current_procinfo;
  1178. end;
  1179. {****************************************************************************
  1180. DECLARATION PARSING
  1181. ****************************************************************************}
  1182. { search in symtablestack for not complete classes }
  1183. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  1184. begin
  1185. if (tsym(p).typ=typesym) and
  1186. (ttypesym(p).restype.def.deftype=objectdef) and
  1187. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  1188. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1189. end;
  1190. procedure read_declarations(islibrary : boolean);
  1191. begin
  1192. repeat
  1193. if not assigned(current_procinfo) then
  1194. internalerror(200304251);
  1195. case token of
  1196. _LABEL:
  1197. label_dec;
  1198. _CONST:
  1199. const_dec;
  1200. _TYPE:
  1201. type_dec;
  1202. _VAR:
  1203. var_dec;
  1204. _THREADVAR:
  1205. threadvar_dec;
  1206. _CONSTRUCTOR,
  1207. _DESTRUCTOR,
  1208. _FUNCTION,
  1209. _PROCEDURE,
  1210. _OPERATOR,
  1211. _CLASS:
  1212. read_proc;
  1213. _EXPORTS:
  1214. begin
  1215. if not(assigned(current_procinfo.procdef.localst)) or
  1216. (current_procinfo.procdef.localst.symtablelevel>main_program_level) or
  1217. (current_module.is_unit) then
  1218. begin
  1219. Message(parser_e_syntax_error);
  1220. consume_all_until(_SEMICOLON);
  1221. end
  1222. else if islibrary or
  1223. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware,system_i386_netwlibc]) then
  1224. read_exports
  1225. else
  1226. begin
  1227. Message(parser_w_unsupported_feature);
  1228. consume(_BEGIN);
  1229. end;
  1230. end
  1231. else
  1232. begin
  1233. case idtoken of
  1234. _RESOURCESTRING :
  1235. resourcestring_dec;
  1236. _PROPERTY:
  1237. begin
  1238. if (m_fpc in aktmodeswitches) then
  1239. property_dec
  1240. else
  1241. break;
  1242. end;
  1243. else
  1244. break;
  1245. end;
  1246. end;
  1247. end;
  1248. until false;
  1249. { check for incomplete class definitions, this is only required
  1250. for fpc modes }
  1251. if (m_fpc in aktmodeswitches) then
  1252. symtablestack.foreach_static(@check_forward_class,nil);
  1253. end;
  1254. procedure read_interface_declarations;
  1255. begin
  1256. repeat
  1257. case token of
  1258. _CONST :
  1259. const_dec;
  1260. _TYPE :
  1261. type_dec;
  1262. _VAR :
  1263. var_dec;
  1264. _THREADVAR :
  1265. threadvar_dec;
  1266. _FUNCTION,
  1267. _PROCEDURE,
  1268. _OPERATOR :
  1269. read_proc;
  1270. else
  1271. begin
  1272. case idtoken of
  1273. _RESOURCESTRING :
  1274. resourcestring_dec;
  1275. _PROPERTY:
  1276. begin
  1277. if (m_fpc in aktmodeswitches) then
  1278. property_dec
  1279. else
  1280. break;
  1281. end;
  1282. else
  1283. break;
  1284. end;
  1285. end;
  1286. end;
  1287. until false;
  1288. { check for incomplete class definitions, this is only required
  1289. for fpc modes }
  1290. if (m_fpc in aktmodeswitches) then
  1291. symtablestack.foreach_static(@check_forward_class,nil);
  1292. end;
  1293. end.
  1294. {
  1295. $Log$
  1296. Revision 1.225 2004-12-15 21:08:15 peter
  1297. * disable inlining across units when the inline procedure references
  1298. a variable or procedure in the static symtable
  1299. Revision 1.224 2004/12/15 17:01:28 peter
  1300. * fixed crash with -vp
  1301. Revision 1.223 2004/12/15 16:00:16 peter
  1302. * external is again allowed in implementation
  1303. Revision 1.222 2004/12/05 12:28:11 peter
  1304. * procvar handling for tp procvar mode fixed
  1305. * proc to procvar moved from addrnode to typeconvnode
  1306. * inlininginfo is now allocated only for inline routines that
  1307. can be inlined, introduced a new flag po_has_inlining_info
  1308. Revision 1.221 2004/12/02 19:26:15 peter
  1309. * disable pass2inline
  1310. Revision 1.220 2004/11/29 18:50:15 peter
  1311. * os2 fixes for import
  1312. * asmsymtype support for intel reader
  1313. Revision 1.219 2004/11/21 17:17:03 florian
  1314. * changed funcret location back to tlocation
  1315. Revision 1.218 2004/11/19 08:17:02 michael
  1316. * Split po_public into po_public and po_global (Peter)
  1317. Revision 1.217 2004/11/17 22:21:35 peter
  1318. mangledname setting moved to place after the complete proc declaration is read
  1319. import generation moved to place where body is also parsed (still gives problems with win32)
  1320. Revision 1.216 2004/11/16 20:32:41 peter
  1321. * fixes for win32 mangledname
  1322. Revision 1.215 2004/11/15 23:35:31 peter
  1323. * tparaitem removed, use tparavarsym instead
  1324. * parameter order is now calculated from paranr value in tparavarsym
  1325. Revision 1.214 2004/11/08 22:09:59 peter
  1326. * tvarsym splitted
  1327. Revision 1.213 2004/11/02 12:55:17 peter
  1328. * nf_internal flag for internal inserted typeconvs. This will
  1329. supress the generation of warning/hints
  1330. Revision 1.212 2004/10/31 18:54:25 peter
  1331. * $fpctarget expands to <cpu>-<os>
  1332. * allow * in middle of the path to support ../*/units/$fpctarget
  1333. Revision 1.211 2004/10/30 15:21:37 florian
  1334. * fixed generic optimizer
  1335. * enabled generic optimizer for sparc
  1336. Revision 1.210 2004/10/24 20:01:08 peter
  1337. * remove saveregister calling convention
  1338. Revision 1.209 2004/10/15 09:14:17 mazen
  1339. - remove $IFDEF DELPHI and related code
  1340. - remove $IFDEF FPCPROCVAR and related code
  1341. Revision 1.208 2004/10/10 20:22:53 peter
  1342. * symtable allocation rewritten
  1343. * loading of parameters to local temps/regs cleanup
  1344. * regvar support for parameters
  1345. * regvar support for staticsymtable (main body)
  1346. Revision 1.207 2004/09/26 17:45:30 peter
  1347. * simple regvar support, not yet finished
  1348. Revision 1.206 2004/09/21 17:25:12 peter
  1349. * paraloc branch merged
  1350. Revision 1.205 2004/09/13 20:34:28 peter
  1351. * keep localst in memory, it is also needed for finalizing
  1352. typedconst
  1353. Revision 1.204 2004/09/04 21:18:47 armin
  1354. * target netwlibc added (libc is preferred for newer netware versions)
  1355. Revision 1.203.4.1 2004/09/17 17:19:26 peter
  1356. * fixed 64 bit unaryminus for sparc
  1357. * fixed 64 bit inlining
  1358. * signness of not operation
  1359. Revision 1.203 2004/08/14 14:50:42 florian
  1360. * fixed several sparc alignment issues
  1361. + Jonas' inline node patch; non functional yet
  1362. Revision 1.202 2004/07/16 21:11:31 jonas
  1363. - disable node-based inlining of routines with special array parameters
  1364. for now (de indexes of open arrays have to be changed, because on the
  1365. caller-side these routines are not necessarily 0-based)
  1366. Revision 1.201 2004/07/15 19:55:40 jonas
  1367. + (incomplete) node_complexity function to assess the complexity of a
  1368. tree
  1369. + support for inlining value and const parameters at the node level
  1370. (all procedures without local variables and without formal parameters
  1371. can now be inlined at the node level)
  1372. Revision 1.200 2004/07/12 09:14:04 jonas
  1373. * inline procedures at the node tree level, but only under some very
  1374. limited circumstances for now (only procedures, and only if they have
  1375. no or only vs_out/vs_var parameters).
  1376. * fixed ppudump for inline procedures
  1377. * fixed ppudump for ppc
  1378. Revision 1.199 2004/07/10 20:24:34 peter
  1379. * put every proc in a new object file
  1380. Revision 1.198 2004/07/09 22:17:32 peter
  1381. * revert has_localst patch
  1382. * replace aktstaticsymtable/aktglobalsymtable with current_module
  1383. Revision 1.197 2004/07/06 19:52:04 peter
  1384. * fix storing of localst in ppu
  1385. Revision 1.196 2004/06/20 08:55:30 florian
  1386. * logs truncated
  1387. Revision 1.195 2004/06/16 20:07:09 florian
  1388. * dwarf branch merged
  1389. Revision 1.194 2004/05/28 21:14:13 peter
  1390. * first load para's to temps before calling entry code (profile
  1391. Revision 1.193 2004/05/24 17:31:12 peter
  1392. * also check local typed const
  1393. Revision 1.192 2004/05/23 18:28:41 peter
  1394. * methodpointer is loaded into a temp when it was a calln
  1395. Revision 1.191 2004/05/23 15:06:21 peter
  1396. * implicit_finally flag must be set in pass1
  1397. * add check whether the implicit frame is generated when expected
  1398. }