psub.pas 57 KB

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