psub.pas 60 KB

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