psub.pas 56 KB

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