2
0

psub.pas 57 KB

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