psub.pas 53 KB

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