psub.pas 58 KB

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