psub.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing and codegeneration at subroutine level
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit psub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globals,
  22. node,nbas,
  23. symdef,procinfo;
  24. type
  25. tcgprocinfo = class(tprocinfo)
  26. private
  27. procedure add_entry_exit_code;
  28. public
  29. { code for the subroutine as tree }
  30. code : tnode;
  31. { positions in the tree for init/final }
  32. entry_asmnode,
  33. loadpara_asmnode,
  34. exitlabel_asmnode,
  35. stackcheck_asmnode,
  36. init_asmnode,
  37. final_asmnode : tasmnode;
  38. { list to store the procinfo's of the nested procedures }
  39. nestedprocs : tlinkedlist;
  40. constructor create(aparent:tprocinfo);override;
  41. destructor destroy;override;
  42. procedure printproc;
  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,dbgbase,
  82. ncgutil,regvars
  83. {$if defined(arm) or defined(powerpc) or defined(powerpc64)}
  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. function generate_bodyentry_block:tnode;
  221. var
  222. srsym : tsym;
  223. para : tcallparanode;
  224. newstatement : tstatementnode;
  225. htype : ttype;
  226. begin
  227. result:=internalstatements(newstatement);
  228. if assigned(current_procinfo.procdef._class) then
  229. begin
  230. { a constructor needs a help procedure }
  231. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  232. begin
  233. if is_class(current_procinfo.procdef._class) then
  234. begin
  235. include(current_procinfo.flags,pi_needs_implicit_finally);
  236. srsym:=search_class_member(current_procinfo.procdef._class,'NEWINSTANCE');
  237. if assigned(srsym) and
  238. (srsym.typ=procsym) then
  239. begin
  240. { if vmt>1 then newinstance }
  241. addstatement(newstatement,cifnode.create(
  242. caddnode.create(gtn,
  243. ctypeconvnode.create_internal(
  244. load_vmt_pointer_node,
  245. voidpointertype),
  246. cpointerconstnode.create(1,voidpointertype)),
  247. cassignmentnode.create(
  248. ctypeconvnode.create_internal(
  249. load_self_pointer_node,
  250. voidpointertype),
  251. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_vmt_pointer_node,[])),
  252. nil));
  253. end
  254. else
  255. internalerror(200305108);
  256. end
  257. else
  258. if is_object(current_procinfo.procdef._class) then
  259. begin
  260. htype.setdef(current_procinfo.procdef._class);
  261. htype.setdef(tpointerdef.create(htype));
  262. { parameter 3 : vmt_offset }
  263. { parameter 2 : address of pointer to vmt,
  264. this is required to allow setting the vmt to -1 to indicate
  265. that memory was allocated }
  266. { parameter 1 : self pointer }
  267. para:=ccallparanode.create(
  268. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  269. ccallparanode.create(
  270. ctypeconvnode.create_internal(
  271. load_vmt_pointer_node,
  272. voidpointertype),
  273. ccallparanode.create(
  274. ctypeconvnode.create_internal(
  275. load_self_pointer_node,
  276. voidpointertype),
  277. nil)));
  278. addstatement(newstatement,cassignmentnode.create(
  279. ctypeconvnode.create_internal(
  280. load_self_pointer_node,
  281. voidpointertype),
  282. ccallnode.createintern('fpc_help_constructor',para)));
  283. end
  284. else
  285. internalerror(200305103);
  286. { if self=nil then exit
  287. calling fail instead of exit is useless because
  288. there is nothing to dispose (PFV) }
  289. addstatement(newstatement,cifnode.create(
  290. caddnode.create(equaln,
  291. load_self_pointer_node,
  292. cnilnode.create),
  293. cexitnode.create(nil),
  294. nil));
  295. end;
  296. { maybe call BeforeDestruction for classes }
  297. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  298. is_class(current_procinfo.procdef._class) then
  299. begin
  300. srsym:=search_class_member(current_procinfo.procdef._class,'BEFOREDESTRUCTION');
  301. if assigned(srsym) and
  302. (srsym.typ=procsym) then
  303. begin
  304. { if vmt<>0 then beforedestruction }
  305. addstatement(newstatement,cifnode.create(
  306. caddnode.create(unequaln,
  307. load_vmt_pointer_node,
  308. cnilnode.create),
  309. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  310. nil));
  311. end
  312. else
  313. internalerror(200305104);
  314. end;
  315. end;
  316. end;
  317. function generate_bodyexit_block:tnode;
  318. var
  319. srsym : tsym;
  320. para : tcallparanode;
  321. newstatement : tstatementnode;
  322. begin
  323. result:=internalstatements(newstatement);
  324. if assigned(current_procinfo.procdef._class) then
  325. begin
  326. { maybe call AfterConstruction for classes }
  327. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  328. is_class(current_procinfo.procdef._class) then
  329. begin
  330. srsym:=search_class_member(current_procinfo.procdef._class,'AFTERCONSTRUCTION');
  331. if assigned(srsym) and
  332. (srsym.typ=procsym) then
  333. begin
  334. { Self can be nil when fail is called }
  335. { if self<>nil and vmt<>nil then afterconstruction }
  336. addstatement(newstatement,cifnode.create(
  337. caddnode.create(andn,
  338. caddnode.create(unequaln,
  339. load_self_pointer_node,
  340. cnilnode.create),
  341. caddnode.create(unequaln,
  342. load_vmt_pointer_node,
  343. cnilnode.create)),
  344. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  345. nil));
  346. end
  347. else
  348. internalerror(200305106);
  349. end;
  350. { a destructor needs a help procedure }
  351. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  352. begin
  353. if is_class(current_procinfo.procdef._class) then
  354. begin
  355. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  356. if assigned(srsym) and
  357. (srsym.typ=procsym) then
  358. begin
  359. { if self<>0 and vmt=1 then freeinstance }
  360. addstatement(newstatement,cifnode.create(
  361. caddnode.create(andn,
  362. caddnode.create(unequaln,
  363. load_self_pointer_node,
  364. cnilnode.create),
  365. caddnode.create(equaln,
  366. ctypeconvnode.create(
  367. load_vmt_pointer_node,
  368. voidpointertype),
  369. cpointerconstnode.create(1,voidpointertype))),
  370. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  371. nil));
  372. end
  373. else
  374. internalerror(200305108);
  375. end
  376. else
  377. if is_object(current_procinfo.procdef._class) then
  378. begin
  379. { finalize object data }
  380. if current_procinfo.procdef._class.needs_inittable then
  381. addstatement(newstatement,finalize_data_node(load_self_node));
  382. { parameter 3 : vmt_offset }
  383. { parameter 2 : pointer to vmt }
  384. { parameter 1 : self pointer }
  385. para:=ccallparanode.create(
  386. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  387. ccallparanode.create(
  388. ctypeconvnode.create_internal(
  389. load_vmt_pointer_node,
  390. voidpointertype),
  391. ccallparanode.create(
  392. ctypeconvnode.create_internal(
  393. load_self_pointer_node,
  394. voidpointertype),
  395. nil)));
  396. addstatement(newstatement,
  397. ccallnode.createintern('fpc_help_destructor',para));
  398. end
  399. else
  400. internalerror(200305105);
  401. end;
  402. end;
  403. end;
  404. function generate_except_block:tnode;
  405. var
  406. pd : tprocdef;
  407. newstatement : tstatementnode;
  408. begin
  409. generate_except_block:=internalstatements(newstatement);
  410. { a constructor needs call destructor (if available) when it
  411. is not inherited }
  412. if assigned(current_procinfo.procdef._class) and
  413. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  414. begin
  415. pd:=current_procinfo.procdef._class.searchdestructor;
  416. if assigned(pd) then
  417. begin
  418. { if vmt<>0 then call destructor }
  419. addstatement(newstatement,cifnode.create(
  420. caddnode.create(unequaln,
  421. load_vmt_pointer_node,
  422. cnilnode.create),
  423. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[]),
  424. nil));
  425. end;
  426. end
  427. else
  428. begin
  429. { no constructor }
  430. { must be the return value finalized before reraising the exception? }
  431. if (not is_void(current_procinfo.procdef.rettype.def)) and
  432. (current_procinfo.procdef.rettype.def.needs_inittable) and
  433. (not is_class(current_procinfo.procdef.rettype.def)) then
  434. addstatement(newstatement,finalize_data_node(load_result_node));
  435. end;
  436. end;
  437. {****************************************************************************
  438. TCGProcInfo
  439. ****************************************************************************}
  440. constructor tcgprocinfo.create(aparent:tprocinfo);
  441. begin
  442. inherited Create(aparent);
  443. nestedprocs:=tlinkedlist.create;
  444. end;
  445. destructor tcgprocinfo.destroy;
  446. begin
  447. nestedprocs.free;
  448. if assigned(code) then
  449. code.free;
  450. inherited destroy;
  451. end;
  452. procedure tcgprocinfo.printproc;
  453. begin
  454. assign(printnodefile,treelogfilename);
  455. {$I-}
  456. append(printnodefile);
  457. if ioresult<>0 then
  458. rewrite(printnodefile);
  459. {$I+}
  460. if ioresult<>0 then
  461. begin
  462. Comment(V_Error,'Error creating '+treelogfilename);
  463. exit;
  464. end;
  465. writeln(printnodefile);
  466. writeln(printnodefile,'*******************************************************************************');
  467. writeln(printnodefile,procdef.fullprocname(false));
  468. writeln(printnodefile,'*******************************************************************************');
  469. printnode(printnodefile,code);
  470. close(printnodefile);
  471. end;
  472. procedure tcgprocinfo.add_entry_exit_code;
  473. var
  474. finalcode,
  475. bodyentrycode,
  476. bodyexitcode,
  477. exceptcode : tnode;
  478. newblock : tblocknode;
  479. codestatement,
  480. newstatement : tstatementnode;
  481. oldfilepos : tfileposinfo;
  482. begin
  483. oldfilepos:=aktfilepos;
  484. { Generate code/locations used at start of proc }
  485. aktfilepos:=entrypos;
  486. entry_asmnode:=casmnode.create_get_position;
  487. loadpara_asmnode:=casmnode.create_get_position;
  488. stackcheck_asmnode:=casmnode.create_get_position;
  489. init_asmnode:=casmnode.create_get_position;
  490. bodyentrycode:=generate_bodyentry_block;
  491. { Generate code/locations used at end of proc }
  492. aktfilepos:=exitpos;
  493. exitlabel_asmnode:=casmnode.create_get_position;
  494. final_asmnode:=casmnode.create_get_position;
  495. bodyexitcode:=generate_bodyexit_block;
  496. { Generate procedure by combining init+body+final,
  497. depending on the implicit finally we need to add
  498. an try...finally...end wrapper }
  499. newblock:=internalstatements(newstatement);
  500. if (cs_implicit_exceptions in aktmoduleswitches) and
  501. (pi_needs_implicit_finally in flags) and
  502. { but it's useless in init/final code of units }
  503. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  504. begin
  505. { Generate special exception block only needed when
  506. implicit finaly is used }
  507. aktfilepos:=exitpos;
  508. exceptcode:=generate_except_block;
  509. { Generate code that will be in the try...finally }
  510. finalcode:=internalstatements(codestatement);
  511. addstatement(codestatement,bodyexitcode);
  512. addstatement(codestatement,final_asmnode);
  513. { Initialize before try...finally...end frame }
  514. addstatement(newstatement,loadpara_asmnode);
  515. addstatement(newstatement,stackcheck_asmnode);
  516. addstatement(newstatement,entry_asmnode);
  517. addstatement(newstatement,init_asmnode);
  518. addstatement(newstatement,bodyentrycode);
  519. aktfilepos:=entrypos;
  520. addstatement(newstatement,ctryfinallynode.create_implicit(
  521. code,
  522. finalcode,
  523. exceptcode));
  524. addstatement(newstatement,exitlabel_asmnode);
  525. { set flag the implicit finally has been generated }
  526. include(flags,pi_has_implicit_finally);
  527. end
  528. else
  529. begin
  530. addstatement(newstatement,loadpara_asmnode);
  531. addstatement(newstatement,stackcheck_asmnode);
  532. addstatement(newstatement,entry_asmnode);
  533. addstatement(newstatement,init_asmnode);
  534. addstatement(newstatement,bodyentrycode);
  535. addstatement(newstatement,code);
  536. addstatement(newstatement,exitlabel_asmnode);
  537. addstatement(newstatement,bodyexitcode);
  538. addstatement(newstatement,final_asmnode);
  539. end;
  540. do_firstpass(newblock);
  541. code:=newblock;
  542. aktfilepos:=oldfilepos;
  543. end;
  544. procedure clearrefs(p : tnamedindexitem;arg:pointer);
  545. begin
  546. if (tsym(p).typ in [localvarsym,paravarsym,globalvarsym]) then
  547. if tabstractvarsym(p).refs>1 then
  548. tabstractvarsym(p).refs:=1;
  549. end;
  550. procedure tcgprocinfo.generate_code;
  551. var
  552. oldprocinfo : tprocinfo;
  553. oldaktmaxfpuregisters : longint;
  554. oldfilepos : tfileposinfo;
  555. templist, symlist : Taasmoutput;
  556. headertai : tai;
  557. curralign : longint;
  558. begin
  559. { the initialization procedure can be empty, then we
  560. don't need to generate anything. When it was an empty
  561. procedure there would be at least a blocknode }
  562. if not assigned(code) then
  563. exit;
  564. { We need valid code }
  565. if Errorcount<>0 then
  566. exit;
  567. { The RA and Tempgen shall not be available yet }
  568. if assigned(tg) then
  569. internalerror(200309201);
  570. oldprocinfo:=current_procinfo;
  571. oldfilepos:=aktfilepos;
  572. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  573. current_procinfo:=self;
  574. aktfilepos:=entrypos;
  575. { get new labels }
  576. aktbreaklabel:=nil;
  577. aktcontinuelabel:=nil;
  578. templist:=Taasmoutput.create;
  579. symlist:=Taasmoutput.create;
  580. { add parast/localst to symtablestack }
  581. add_to_symtablestack;
  582. { when size optimization only count occurrence }
  583. if cs_littlesize in aktglobalswitches then
  584. cg.t_times:=1
  585. else
  586. { reference for repetition is 100 }
  587. cg.t_times:=100;
  588. { clear register count }
  589. symtablestack.foreach_static(@clearrefs,nil);
  590. symtablestack.next.foreach_static(@clearrefs,nil);
  591. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  592. if (procdef.localst.symtablelevel=main_program_level) and
  593. (not current_module.is_unit) then
  594. include(flags,pi_do_call);
  595. { set implicit_finally flag when there are locals/paras to be finalized }
  596. current_procinfo.procdef.parast.foreach_static(@check_finalize_paras,nil);
  597. current_procinfo.procdef.localst.foreach_static(@check_finalize_locals,nil);
  598. { firstpass everything }
  599. flowcontrol:=[];
  600. do_firstpass(code);
  601. if code.registersfpu>0 then
  602. include(current_procinfo.flags,pi_uses_fpu);
  603. { add implicit entry and exit code }
  604. add_entry_exit_code;
  605. { only do secondpass if there are no errors }
  606. if ErrorCount=0 then
  607. begin
  608. { set the start offset to the start of the temp area in the stack }
  609. tg:=ttgobj.create;
  610. { Create register allocator }
  611. cg.init_register_allocators;
  612. set_first_temp_offset;
  613. generate_parameter_info;
  614. { allocate the symbol associated with the procedure, so that }
  615. { references to itself are not treated as references to }
  616. { externals }
  617. aktfilepos:=entrypos;
  618. gen_proc_symbol(symlist);
  619. { Allocate space in temp/registers for parast and localst }
  620. aktfilepos:=entrypos;
  621. gen_alloc_symtable(aktproccode,procdef.parast);
  622. gen_alloc_symtable(aktproccode,procdef.localst);
  623. { Store temp offset for information about 'real' temps }
  624. tempstart:=tg.lasttemp;
  625. { Generate code to load register parameters in temps and insert local
  626. copies for values parameters. This must be done before the code for the
  627. body is generated because the localloc is updated.
  628. Note: The generated code will be inserted after the code generation of
  629. the body is finished, because only then the position is known }
  630. {$ifdef oldregvars}
  631. assign_regvars(code);
  632. {$endif oldreg}
  633. aktfilepos:=entrypos;
  634. gen_load_para_value(templist);
  635. { caller paraloc info is also necessary in the stackframe_entry
  636. code of the ppc (and possibly other processors) }
  637. if not procdef.has_paraloc_info then
  638. begin
  639. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  640. procdef.has_paraloc_info:=true;
  641. end;
  642. { generate code for the node tree }
  643. do_secondpass(code);
  644. aktproccode.concatlist(exprasmlist);
  645. {$ifdef i386}
  646. procdef.fpu_used:=code.registersfpu;
  647. {$endif i386}
  648. { The position of the loadpara_asmnode is now known }
  649. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  650. { first generate entry and initialize code with the correct
  651. position and switches }
  652. aktfilepos:=entrypos;
  653. aktlocalswitches:=entryswitches;
  654. gen_entry_code(templist);
  655. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  656. gen_initialize_code(templist);
  657. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  658. { now generate finalize and exit code with the correct position
  659. and switches }
  660. aktfilepos:=exitpos;
  661. aktlocalswitches:=exitswitches;
  662. gen_finalize_code(templist);
  663. { the finalcode must be concated if there was no position available,
  664. using insertlistafter will result in an insert at the start
  665. when currentai=nil }
  666. if assigned(final_asmnode.currenttai) then
  667. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  668. else
  669. aktproccode.concatlist(templist);
  670. { insert exit label at the correct position }
  671. cg.a_label(templist,aktexitlabel);
  672. if assigned(exitlabel_asmnode.currenttai) then
  673. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  674. else
  675. aktproccode.concatlist(templist);
  676. { exit code }
  677. gen_exit_code(templist);
  678. aktproccode.concatlist(templist);
  679. {$ifdef OLDREGVARS}
  680. { note: this must be done only after as much code as possible has }
  681. { been generated. The result is that when you ungetregister() a }
  682. { regvar, it will actually free the regvar (and alse free the }
  683. { the regvars at the same time). Doing this too early will }
  684. { confuse the register allocator, as the regvars will still be }
  685. { used. It should be done before loading the result regs (so }
  686. { they don't conflict with the regvars) and before }
  687. { gen_entry_code (that one has to be able to allocate the }
  688. { regvars again) (JM) }
  689. free_regvars(aktproccode);
  690. {$endif OLDREGVARS}
  691. { add code that will load the return value, this is not done
  692. for assembler routines when they didn't reference the result
  693. variable }
  694. gen_load_return_value(templist);
  695. aktproccode.concatlist(templist);
  696. { generate symbol and save end of header position }
  697. aktfilepos:=entrypos;
  698. headertai:=tai(symlist.last);
  699. { insert symbol }
  700. aktproccode.insertlist(symlist);
  701. { Free space in temp/registers for parast and localst, must be
  702. done after gen_entry_code }
  703. aktfilepos:=exitpos;
  704. gen_free_symtable(aktproccode,procdef.localst);
  705. gen_free_symtable(aktproccode,procdef.parast);
  706. { Already reserve all registers for stack checking code and
  707. generate the call to the helper function }
  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_call(templist);
  714. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  715. end;
  716. { The procedure body is finished, we can now
  717. allocate the registers }
  718. cg.do_register_allocation(aktproccode,headertai);
  719. { Add save and restore of used registers }
  720. aktfilepos:=entrypos;
  721. gen_save_used_regs(templist);
  722. aktproccode.insertlistafter(headertai,templist);
  723. aktfilepos:=exitpos;
  724. gen_restore_used_regs(aktproccode);
  725. { We know the size of the stack, now we can generate the
  726. parameter that is passed to the stack checking code }
  727. if (cs_check_stack in entryswitches) and
  728. not(po_assembler in procdef.procoptions) and
  729. (current_procinfo.procdef.proctypeoption<>potype_proginit) then
  730. begin
  731. aktfilepos:=entrypos;
  732. gen_stack_check_size_para(templist);
  733. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  734. end;
  735. { Add entry code (stack allocation) after header }
  736. aktfilepos:=entrypos;
  737. gen_proc_entry_code(templist);
  738. aktproccode.insertlistafter(headertai,templist);
  739. { Add exit code at the end }
  740. aktfilepos:=exitpos;
  741. gen_proc_exit_code(templist);
  742. aktproccode.concatlist(templist);
  743. { check if the implicit finally has been generated. The flag
  744. should already be set in pass1 }
  745. if (cs_implicit_exceptions in aktmoduleswitches) and
  746. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  747. (pi_needs_implicit_finally in flags) and
  748. not(pi_has_implicit_finally in flags) then
  749. internalerror(200405231);
  750. {$ifndef NoOpt}
  751. if not(cs_no_regalloc in aktglobalswitches) then
  752. begin
  753. if (cs_optimize in aktglobalswitches) and
  754. { do not optimize pure assembler procedures }
  755. not(pi_is_assembler in flags) then
  756. optimize(aktproccode);
  757. end;
  758. {$endif NoOpt}
  759. { Add end symbol and debug info }
  760. aktfilepos:=exitpos;
  761. gen_proc_symbol_end(templist);
  762. aktproccode.concatlist(templist);
  763. {$ifdef ARM}
  764. { because of the limited constant size of the arm, all data access is done pc relative }
  765. insertpcrelativedata(aktproccode,aktlocaldata);
  766. {$endif ARM}
  767. {$ifdef POWERPC}
  768. fixup_jmps(aktproccode);
  769. {$endif POWERPC}
  770. {$ifdef POWERPC64}
  771. fixup_jmps(aktproccode);
  772. {$endif POWERPC64}
  773. { insert line debuginfo }
  774. if (cs_debuginfo in aktmoduleswitches) or
  775. (cs_use_lineinfo in aktglobalswitches) then
  776. debuginfo.insertlineinfo(aktproccode);
  777. { gprof uses 16 byte granularity }
  778. if (cs_profile in aktmoduleswitches) then
  779. curralign:=16
  780. else
  781. curralign:=aktalignment.procalign;
  782. { add the procedure to the al_procedures }
  783. maybe_new_object_file(asmlist[al_procedures]);
  784. new_section(asmlist[al_procedures],sec_code,lower(procdef.mangledname),curralign);
  785. asmlist[al_procedures].concatlist(aktproccode);
  786. { save local data (casetable) also in the same file }
  787. if assigned(aktlocaldata) and
  788. (not aktlocaldata.empty) then
  789. asmlist[al_procedures].concatlist(aktlocaldata);
  790. { only now we can remove the temps }
  791. tg.resettempgen;
  792. { stop tempgen and ra }
  793. tg.free;
  794. cg.done_register_allocators;
  795. tg:=nil;
  796. end;
  797. { restore symtablestack }
  798. remove_from_symtablestack;
  799. { restore }
  800. templist.free;
  801. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  802. aktfilepos:=oldfilepos;
  803. current_procinfo:=oldprocinfo;
  804. end;
  805. procedure tcgprocinfo.add_to_symtablestack;
  806. var
  807. _class,hp : tobjectdef;
  808. begin
  809. { insert symtables for the class, but only if it is no nested function }
  810. if assigned(procdef._class) and
  811. not(assigned(parent) and
  812. assigned(parent.procdef) and
  813. assigned(parent.procdef._class)) then
  814. begin
  815. { insert them in the reverse order }
  816. hp:=nil;
  817. repeat
  818. _class:=procdef._class;
  819. while _class.childof<>hp do
  820. _class:=_class.childof;
  821. hp:=_class;
  822. _class.symtable.next:=symtablestack;
  823. symtablestack:=_class.symtable;
  824. until hp=procdef._class;
  825. end;
  826. { insert parasymtable in symtablestack when parsing
  827. a function }
  828. if procdef.parast.symtablelevel>=normal_function_level then
  829. begin
  830. procdef.parast.next:=symtablestack;
  831. symtablestack:=procdef.parast;
  832. end;
  833. procdef.localst.next:=symtablestack;
  834. symtablestack:=procdef.localst;
  835. end;
  836. procedure tcgprocinfo.remove_from_symtablestack;
  837. begin
  838. { remove localst/parast }
  839. if procdef.parast.symtablelevel>=normal_function_level then
  840. symtablestack:=symtablestack.next.next
  841. else
  842. symtablestack:=symtablestack.next;
  843. { remove class member symbol tables }
  844. while symtablestack.symtabletype=objectsymtable do
  845. symtablestack:=symtablestack.next;
  846. end;
  847. procedure tcgprocinfo.resetprocdef;
  848. begin
  849. { remove code tree, if not inline procedure }
  850. if assigned(code) then
  851. begin
  852. { the inline procedure has already got a copy of the tree
  853. stored in procdef.inlininginfo }
  854. code.free;
  855. code:=nil;
  856. end;
  857. end;
  858. function checknodeinlining(procdef: tprocdef): boolean;
  859. var
  860. i : integer;
  861. currpara : tparavarsym;
  862. begin
  863. result := false;
  864. if (pi_has_assembler_block in current_procinfo.flags) then
  865. exit;
  866. for i:=0 to procdef.paras.count-1 do
  867. begin
  868. currpara:=tparavarsym(procdef.paras[i]);
  869. { we can't handle formaldefs and special arrays (the latter may need a }
  870. { re-basing of the index, i.e. if you pass an array[1..10] as open array, }
  871. { you have to add 1 to all index operations if you directly inline it }
  872. if ((currpara.varspez in [vs_out,vs_var,vs_const]) and
  873. (currpara.vartype.def.deftype=formaldef)) or
  874. is_special_array(currpara.vartype.def) then
  875. exit;
  876. end;
  877. result:=true;
  878. end;
  879. procedure tcgprocinfo.parse_body;
  880. var
  881. oldprocinfo : tprocinfo;
  882. oldblock_type : tblock_type;
  883. begin
  884. oldprocinfo:=current_procinfo;
  885. oldblock_type:=block_type;
  886. { reset break and continue labels }
  887. block_type:=bt_body;
  888. current_procinfo:=self;
  889. { calculate the lexical level }
  890. if procdef.parast.symtablelevel>maxnesting then
  891. Message(parser_e_too_much_lexlevel);
  892. { static is also important for local procedures !! }
  893. if (po_staticmethod in procdef.procoptions) then
  894. allow_only_static:=true
  895. else if (procdef.parast.symtablelevel=normal_function_level) then
  896. allow_only_static:=false;
  897. {$ifdef state_tracking}
  898. { aktstate:=Tstate_storage.create;}
  899. {$endif state_tracking}
  900. { create a local symbol table for this routine }
  901. if not assigned(procdef.localst) then
  902. procdef.insert_localst;
  903. { add parast/localst to symtablestack }
  904. add_to_symtablestack;
  905. { constant symbols are inserted in this symboltable }
  906. constsymtable:=symtablestack;
  907. { save entry info }
  908. entrypos:=aktfilepos;
  909. entryswitches:=aktlocalswitches;
  910. { parse the code ... }
  911. code:=block(current_module.islibrary);
  912. { save exit info }
  913. exitswitches:=aktlocalswitches;
  914. exitpos:=last_endtoken_filepos;
  915. { the procedure is now defined }
  916. procdef.forwarddef:=false;
  917. if assigned(code) then
  918. begin
  919. { get a better entry point }
  920. entrypos:=code.fileinfo;
  921. { Finish type checking pass }
  922. do_resulttypepass(code);
  923. end;
  924. { Check for unused labels, forwards, symbols for procedures. Static
  925. symtable is checked in pmodules.
  926. The check must be done after the resulttypepass }
  927. if (Errorcount=0) and
  928. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  929. begin
  930. { check if forwards are resolved }
  931. tstoredsymtable(procdef.localst).check_forwards;
  932. { check if all labels are used }
  933. tstoredsymtable(procdef.localst).checklabels;
  934. { remove cross unit overloads }
  935. tstoredsymtable(procdef.localst).unchain_overloaded;
  936. { check for unused symbols, but only if there is no asm block }
  937. if not(pi_has_assembler_block in flags) then
  938. begin
  939. tstoredsymtable(procdef.localst).allsymbolsused;
  940. tstoredsymtable(procdef.parast).allsymbolsused;
  941. end;
  942. end;
  943. if (po_inline in procdef.procoptions) then
  944. begin
  945. { Can we inline this procedure? }
  946. if checknodeinlining(procdef) then
  947. begin
  948. new(procdef.inlininginfo);
  949. include(procdef.procoptions,po_has_inlininginfo);
  950. procdef.inlininginfo^.code:=code.getcopy;
  951. procdef.inlininginfo^.flags:=current_procinfo.flags;
  952. { The blocknode needs to set an exit label }
  953. if procdef.inlininginfo^.code.nodetype=blockn then
  954. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  955. end;
  956. end;
  957. { Print the node to tree.log }
  958. if paraprintnodetree=1 then
  959. printproc;
  960. { ... remove symbol tables }
  961. remove_from_symtablestack;
  962. {$ifdef state_tracking}
  963. { aktstate.destroy;}
  964. {$endif state_tracking}
  965. { reset to normal non static function }
  966. if (procdef.parast.symtablelevel=normal_function_level) then
  967. allow_only_static:=false;
  968. current_procinfo:=oldprocinfo;
  969. block_type:=oldblock_type;
  970. end;
  971. {****************************************************************************
  972. PROCEDURE/FUNCTION PARSING
  973. ****************************************************************************}
  974. procedure check_init_paras(p:tnamedindexitem;arg:pointer);
  975. begin
  976. if tsym(p).typ<>paravarsym then
  977. exit;
  978. with tparavarsym(p) do
  979. if (not is_class(vartype.def) and
  980. vartype.def.needs_inittable and
  981. (varspez in [vs_value,vs_out])) then
  982. include(current_procinfo.flags,pi_do_call);
  983. end;
  984. procedure read_proc;
  985. {
  986. Parses the procedure directives, then parses the procedure body, then
  987. generates the code for it
  988. }
  989. procedure do_generate_code(pi:tcgprocinfo);
  990. var
  991. hpi : tcgprocinfo;
  992. begin
  993. { generate code for this procedure }
  994. pi.generate_code;
  995. { process nested procs }
  996. hpi:=tcgprocinfo(pi.nestedprocs.first);
  997. while assigned(hpi) do
  998. begin
  999. do_generate_code(hpi);
  1000. hpi:=tcgprocinfo(hpi.next);
  1001. end;
  1002. pi.resetprocdef;
  1003. end;
  1004. var
  1005. old_current_procinfo : tprocinfo;
  1006. oldconstsymtable : tsymtable;
  1007. oldfailtokenmode : tmodeswitch;
  1008. pdflags : tpdflags;
  1009. pd : tprocdef;
  1010. isnestedproc : boolean;
  1011. s : string;
  1012. begin
  1013. { save old state }
  1014. oldconstsymtable:=constsymtable;
  1015. old_current_procinfo:=current_procinfo;
  1016. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1017. to an other procdef }
  1018. current_procinfo:=nil;
  1019. { parse procedure declaration }
  1020. if assigned(old_current_procinfo) and
  1021. assigned(old_current_procinfo.procdef) then
  1022. pd:=parse_proc_dec(old_current_procinfo.procdef._class)
  1023. else
  1024. pd:=parse_proc_dec(nil);
  1025. { set the default function options }
  1026. if parse_only then
  1027. begin
  1028. pd.forwarddef:=true;
  1029. { set also the interface flag, for better error message when the
  1030. implementation doesn't much this header }
  1031. pd.interfacedef:=true;
  1032. include(pd.procoptions,po_global);
  1033. pdflags:=[pd_interface];
  1034. end
  1035. else
  1036. begin
  1037. pdflags:=[pd_body];
  1038. if (not current_module.in_interface) then
  1039. include(pdflags,pd_implemen);
  1040. if (not current_module.is_unit) or
  1041. maybe_smartlink_symbol or
  1042. {
  1043. taking addresses of static procedures goes wrong
  1044. if they aren't global when pic is used (FK)
  1045. }
  1046. (cs_create_pic in aktmoduleswitches) then
  1047. include(pd.procoptions,po_global);
  1048. pd.forwarddef:=false;
  1049. end;
  1050. { parse the directives that may follow }
  1051. parse_proc_directives(pd,pdflags);
  1052. { hint directives, these can be separated by semicolons here,
  1053. that needs to be handled here with a loop (PFV) }
  1054. while try_consume_hintdirective(pd.symoptions) do
  1055. Consume(_SEMICOLON);
  1056. { Set calling convention }
  1057. handle_calling_convention(pd);
  1058. { search for forward declarations }
  1059. if not proc_add_definition(pd) then
  1060. begin
  1061. { A method must be forward defined (in the object declaration) }
  1062. if assigned(pd._class) and
  1063. (not assigned(old_current_procinfo.procdef._class)) then
  1064. begin
  1065. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1066. tprocsym(pd.procsym).write_parameter_lists(pd);
  1067. end
  1068. else
  1069. begin
  1070. { Give a better error if there is a forward def in the interface and only
  1071. a single implementation }
  1072. if (not pd.forwarddef) and
  1073. (not pd.interfacedef) and
  1074. (tprocsym(pd.procsym).procdef_count>1) and
  1075. tprocsym(pd.procsym).first_procdef.forwarddef and
  1076. tprocsym(pd.procsym).first_procdef.interfacedef and
  1077. not(tprocsym(pd.procsym).procdef_count>2) then
  1078. begin
  1079. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1080. tprocsym(pd.procsym).write_parameter_lists(pd);
  1081. end;
  1082. end;
  1083. end;
  1084. { Set mangled name }
  1085. proc_set_mangledname(pd);
  1086. { compile procedure when a body is needed }
  1087. if (pd_body in pdflags) then
  1088. begin
  1089. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1090. { create a new procedure }
  1091. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1092. current_module.procinfo:=current_procinfo;
  1093. current_procinfo.procdef:=pd;
  1094. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1095. { Insert mangledname }
  1096. pd.aliasnames.insert(pd.mangledname);
  1097. { Handle Export of this procedure }
  1098. if (po_exports in pd.procoptions) and
  1099. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1100. begin
  1101. pd.aliasnames.insert(pd.procsym.realname);
  1102. if cs_link_deffile in aktglobalswitches then
  1103. deffile.AddExport(pd.mangledname);
  1104. end;
  1105. { Insert result variables in the localst }
  1106. insert_funcret_local(pd);
  1107. { check if there are para's which require initing -> set }
  1108. { pi_do_call (if not yet set) }
  1109. if not(pi_do_call in current_procinfo.flags) then
  1110. pd.parast.foreach_static(@check_init_paras,nil);
  1111. { set _FAIL as keyword if constructor }
  1112. if (pd.proctypeoption=potype_constructor) then
  1113. begin
  1114. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1115. tokeninfo^[_FAIL].keyword:=m_all;
  1116. end;
  1117. tcgprocinfo(current_procinfo).parse_body;
  1118. { When it's a nested procedure then defer the code generation,
  1119. when back at normal function level then generate the code
  1120. for all defered nested procedures and the current procedure }
  1121. if isnestedproc then
  1122. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1123. else
  1124. begin
  1125. { We can't support inlining for procedures that have nested
  1126. procedures because the nested procedures use a fixed offset
  1127. for accessing locals in the parent procedure (PFV) }
  1128. if (po_inline in current_procinfo.procdef.procoptions) and
  1129. (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1130. begin
  1131. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1132. Message(parser_w_inlining_disabled);
  1133. current_procinfo.procdef.proccalloption:=pocall_default;
  1134. end;
  1135. do_generate_code(tcgprocinfo(current_procinfo));
  1136. end;
  1137. { reset _FAIL as _SELF normal }
  1138. if (pd.proctypeoption=potype_constructor) then
  1139. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1140. { release procinfo }
  1141. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1142. internalerror(200304274);
  1143. current_module.procinfo:=current_procinfo.parent;
  1144. if not isnestedproc then
  1145. current_procinfo.free;
  1146. consume(_SEMICOLON);
  1147. end
  1148. else
  1149. begin
  1150. { Handle imports }
  1151. if (po_external in pd.procoptions) then
  1152. begin
  1153. { External declared in implementation, and there was already a
  1154. forward (or interface) declaration then we need to generate
  1155. a stub that calls the external routine }
  1156. if (not pd.forwarddef) and
  1157. (pd.hasforward) and
  1158. not(
  1159. assigned(pd.import_dll) and
  1160. (target_info.system in [system_i386_win32,system_i386_wdosx,
  1161. system_i386_emx,system_i386_os2,system_arm_wince,system_i386_wince])
  1162. ) then
  1163. begin
  1164. s:=proc_get_importname(pd);
  1165. if s<>'' then
  1166. gen_external_stub(asmlist[al_procedures],pd,{$IFDEF POWERPC64}'.'+{$ENDIF}s);
  1167. end;
  1168. { Import DLL specified? }
  1169. if assigned(pd.import_dll) then
  1170. begin
  1171. { create importlib if not already done }
  1172. if not(current_module.uses_imports) then
  1173. begin
  1174. current_module.uses_imports:=true;
  1175. importlib.preparelib(current_module.realmodulename^);
  1176. end;
  1177. if assigned(pd.import_name) then
  1178. importlib.importprocedure(pd,pd.import_dll^,pd.import_nr,pd.import_name^)
  1179. else
  1180. importlib.importprocedure(pd,pd.import_dll^,pd.import_nr,'');
  1181. end
  1182. else
  1183. begin
  1184. { add import name to external list for DLL scanning }
  1185. if target_info.DllScanSupported then
  1186. current_module.externals.insert(tExternalsItem.create(proc_get_importname(pd)));
  1187. end;
  1188. end;
  1189. end;
  1190. { Restore old state }
  1191. constsymtable:=oldconstsymtable;
  1192. current_procinfo:=old_current_procinfo;
  1193. end;
  1194. {****************************************************************************
  1195. DECLARATION PARSING
  1196. ****************************************************************************}
  1197. { search in symtablestack for not complete classes }
  1198. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  1199. begin
  1200. if (tsym(p).typ=typesym) and
  1201. (ttypesym(p).restype.def.deftype=objectdef) and
  1202. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  1203. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1204. end;
  1205. procedure read_declarations(islibrary : boolean);
  1206. begin
  1207. repeat
  1208. if not assigned(current_procinfo) then
  1209. internalerror(200304251);
  1210. case token of
  1211. _LABEL:
  1212. label_dec;
  1213. _CONST:
  1214. const_dec;
  1215. _TYPE:
  1216. type_dec;
  1217. _VAR:
  1218. var_dec;
  1219. _THREADVAR:
  1220. threadvar_dec;
  1221. _CONSTRUCTOR,
  1222. _DESTRUCTOR,
  1223. _FUNCTION,
  1224. _PROCEDURE,
  1225. _OPERATOR,
  1226. _CLASS:
  1227. read_proc;
  1228. _EXPORTS:
  1229. begin
  1230. if not(assigned(current_procinfo.procdef.localst)) or
  1231. (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  1232. begin
  1233. Message(parser_e_syntax_error);
  1234. consume_all_until(_SEMICOLON);
  1235. end
  1236. else if islibrary or
  1237. (target_info.system in system_unit_program_exports) then
  1238. read_exports
  1239. else
  1240. begin
  1241. Message(parser_w_unsupported_feature);
  1242. consume(_BEGIN);
  1243. end;
  1244. end
  1245. else
  1246. begin
  1247. case idtoken of
  1248. _RESOURCESTRING :
  1249. begin
  1250. { m_class is needed, because the resourcestring
  1251. loading is in the ObjPas unit }
  1252. if (m_class in aktmodeswitches) then
  1253. resourcestring_dec
  1254. else
  1255. break;
  1256. end;
  1257. _PROPERTY:
  1258. begin
  1259. if (m_fpc in aktmodeswitches) then
  1260. property_dec
  1261. else
  1262. break;
  1263. end;
  1264. else
  1265. break;
  1266. end;
  1267. end;
  1268. end;
  1269. until false;
  1270. { check for incomplete class definitions, this is only required
  1271. for fpc modes }
  1272. if (m_fpc in aktmodeswitches) then
  1273. symtablestack.foreach_static(@check_forward_class,nil);
  1274. end;
  1275. procedure read_interface_declarations;
  1276. begin
  1277. repeat
  1278. case token of
  1279. _CONST :
  1280. const_dec;
  1281. _TYPE :
  1282. type_dec;
  1283. _VAR :
  1284. var_dec;
  1285. _THREADVAR :
  1286. threadvar_dec;
  1287. _FUNCTION,
  1288. _PROCEDURE,
  1289. _OPERATOR :
  1290. read_proc;
  1291. else
  1292. begin
  1293. case idtoken of
  1294. _RESOURCESTRING :
  1295. resourcestring_dec;
  1296. _PROPERTY:
  1297. begin
  1298. if (m_fpc in aktmodeswitches) then
  1299. property_dec
  1300. else
  1301. break;
  1302. end;
  1303. else
  1304. break;
  1305. end;
  1306. end;
  1307. end;
  1308. until false;
  1309. { check for incomplete class definitions, this is only required
  1310. for fpc modes }
  1311. if (m_fpc in aktmodeswitches) then
  1312. symtablestack.foreach_static(@check_forward_class,nil);
  1313. end;
  1314. end.