psub.pas 59 KB

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