psub.pas 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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, symlist : 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. symlist:=Taasmoutput.create;
  584. { add parast/localst to symtablestack }
  585. add_to_symtablestack;
  586. { when size optimization only count occurrence }
  587. if cs_littlesize in aktglobalswitches then
  588. cg.t_times:=1
  589. else
  590. { reference for repetition is 100 }
  591. cg.t_times:=100;
  592. { clear register count }
  593. symtablestack.foreach_static(@clearrefs,nil);
  594. symtablestack.next.foreach_static(@clearrefs,nil);
  595. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  596. if (procdef.localst.symtablelevel=main_program_level) and
  597. (not current_module.is_unit) then
  598. include(flags,pi_do_call);
  599. { set implicit_finally flag when there are locals/paras to be finalized }
  600. current_procinfo.procdef.parast.foreach_static(@check_finalize_paras,nil);
  601. current_procinfo.procdef.localst.foreach_static(@check_finalize_locals,nil);
  602. { firstpass everything }
  603. flowcontrol:=[];
  604. do_firstpass(code);
  605. if code.registersfpu>0 then
  606. include(current_procinfo.flags,pi_uses_fpu);
  607. { add implicit entry and exit code }
  608. add_entry_exit_code;
  609. { only do secondpass if there are no errors }
  610. if (ErrorCount=0) then
  611. begin
  612. { set the start offset to the start of the temp area in the stack }
  613. tg:=ttgobj.create;
  614. { Create register allocator }
  615. cg.init_register_allocators;
  616. set_first_temp_offset;
  617. generate_parameter_info;
  618. { allocate the symbol associated with the procedure, so that }
  619. { references to itself are not treated as references to }
  620. { externals }
  621. aktfilepos:=entrypos;
  622. gen_proc_symbol(symlist);
  623. { Allocate space in temp/registers for parast and localst }
  624. aktfilepos:=entrypos;
  625. gen_alloc_symtable(aktproccode,procdef.parast);
  626. gen_alloc_symtable(aktproccode,procdef.localst);
  627. { Store temp offset for information about 'real' temps }
  628. tempstart:=tg.lasttemp;
  629. { Generate code to load register parameters in temps and insert local
  630. copies for values parameters. This must be done before the code for the
  631. body is generated because the localloc is updated.
  632. Note: The generated code will be inserted after the code generation of
  633. the body is finished, because only then the position is known }
  634. {$ifdef oldregvars}
  635. assign_regvars(code);
  636. {$endif oldreg}
  637. aktfilepos:=entrypos;
  638. gen_load_para_value(templist);
  639. { caller paraloc info is also necessary in the stackframe_entry
  640. code of the ppc (and possibly other processors) }
  641. if not procdef.has_paraloc_info then
  642. begin
  643. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  644. procdef.has_paraloc_info:=true;
  645. end;
  646. { generate code for the node tree }
  647. do_secondpass(code);
  648. aktproccode.concatlist(exprasmlist);
  649. {$ifdef i386}
  650. procdef.fpu_used:=code.registersfpu;
  651. {$endif i386}
  652. { The position of the loadpara_asmnode is now known }
  653. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  654. { first generate entry and initialize code with the correct
  655. position and switches }
  656. aktfilepos:=entrypos;
  657. aktlocalswitches:=entryswitches;
  658. gen_entry_code(templist);
  659. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  660. gen_initialize_code(templist);
  661. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  662. { now generate finalize and exit code with the correct position
  663. and switches }
  664. aktfilepos:=exitpos;
  665. aktlocalswitches:=exitswitches;
  666. gen_finalize_code(templist);
  667. { the finalcode must be concated if there was no position available,
  668. using insertlistafter will result in an insert at the start
  669. when currentai=nil }
  670. if assigned(final_asmnode.currenttai) then
  671. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  672. else
  673. aktproccode.concatlist(templist);
  674. { insert exit label at the correct position }
  675. cg.a_label(templist,aktexitlabel);
  676. if assigned(exitlabel_asmnode.currenttai) then
  677. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  678. else
  679. aktproccode.concatlist(templist);
  680. { exit code }
  681. gen_exit_code(templist);
  682. aktproccode.concatlist(templist);
  683. {$ifdef OLDREGVARS}
  684. { note: this must be done only after as much code as possible has }
  685. { been generated. The result is that when you ungetregister() a }
  686. { regvar, it will actually free the regvar (and alse free the }
  687. { the regvars at the same time). Doing this too early will }
  688. { confuse the register allocator, as the regvars will still be }
  689. { used. It should be done before loading the result regs (so }
  690. { they don't conflict with the regvars) and before }
  691. { gen_entry_code (that one has to be able to allocate the }
  692. { regvars again) (JM) }
  693. free_regvars(aktproccode);
  694. {$endif OLDREGVARS}
  695. { add code that will load the return value, this is not done
  696. for assembler routines when they didn't reference the result
  697. variable }
  698. gen_load_return_value(templist);
  699. aktproccode.concatlist(templist);
  700. { generate symbol and save end of header position }
  701. aktfilepos:=entrypos;
  702. headertai:=tai(symlist.last);
  703. { insert symbol }
  704. aktproccode.insertlist(symlist);
  705. { Free space in temp/registers for parast and localst, must be
  706. done after gen_entry_code }
  707. aktfilepos:=exitpos;
  708. gen_free_symtable(aktproccode,procdef.localst);
  709. gen_free_symtable(aktproccode,procdef.parast);
  710. { Already reserve all registers for stack checking code and
  711. generate the call to the helper function }
  712. if (cs_check_stack in entryswitches) and
  713. not(po_assembler in procdef.procoptions) and
  714. (current_procinfo.procdef.proctypeoption<>potype_proginit) then
  715. begin
  716. aktfilepos:=entrypos;
  717. gen_stack_check_call(templist);
  718. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  719. end;
  720. { The procedure body is finished, we can now
  721. allocate the registers }
  722. cg.do_register_allocation(aktproccode,headertai);
  723. { Add save and restore of used registers }
  724. aktfilepos:=entrypos;
  725. gen_save_used_regs(templist);
  726. aktproccode.insertlistafter(headertai,templist);
  727. aktfilepos:=exitpos;
  728. gen_restore_used_regs(aktproccode);
  729. { We know the size of the stack, now we can generate the
  730. parameter that is passed to the stack checking code }
  731. if (cs_check_stack in entryswitches) and
  732. not(po_assembler in procdef.procoptions) and
  733. (current_procinfo.procdef.proctypeoption<>potype_proginit) then
  734. begin
  735. aktfilepos:=entrypos;
  736. gen_stack_check_size_para(templist);
  737. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  738. end;
  739. { Add entry code (stack allocation) after header }
  740. aktfilepos:=entrypos;
  741. gen_proc_entry_code(templist);
  742. aktproccode.insertlistafter(headertai,templist);
  743. { Add exit code at the end }
  744. aktfilepos:=exitpos;
  745. gen_proc_exit_code(templist);
  746. aktproccode.concatlist(templist);
  747. { check if the implicit finally has been generated. The flag
  748. should already be set in pass1 }
  749. if (cs_implicit_exceptions in aktmoduleswitches) and
  750. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  751. (pi_needs_implicit_finally in flags) and
  752. not(pi_has_implicit_finally in flags) then
  753. internalerror(200405231);
  754. {$ifndef NoOpt}
  755. if not(cs_no_regalloc in aktglobalswitches) then
  756. begin
  757. if (cs_optimize in aktglobalswitches) and
  758. { do not optimize pure assembler procedures }
  759. not(pi_is_assembler in flags) then
  760. optimize(aktproccode);
  761. end;
  762. {$endif NoOpt}
  763. { Add end symbol and debug info }
  764. aktfilepos:=exitpos;
  765. gen_proc_symbol_end(templist);
  766. aktproccode.concatlist(templist);
  767. {$ifdef ARM}
  768. { because of the limited constant size of the arm, all data access is done pc relative }
  769. insertpcrelativedata(aktproccode,aktlocaldata);
  770. {$endif ARM}
  771. {$ifdef POWERPC}
  772. fixup_jmps(aktproccode);
  773. {$endif POWERPC}
  774. {$ifdef POWERPC64}
  775. fixup_jmps(aktproccode);
  776. {$endif POWERPC64}
  777. { insert line debuginfo }
  778. if (cs_debuginfo in aktmoduleswitches) or
  779. (cs_use_lineinfo in aktglobalswitches) then
  780. debuginfo.insertlineinfo(aktproccode);
  781. { gprof uses 16 byte granularity }
  782. if (cs_profile in aktmoduleswitches) then
  783. curralign:=16
  784. else
  785. curralign:=aktalignment.procalign;
  786. { add the procedure to the al_procedures }
  787. maybe_new_object_file(asmlist[al_procedures]);
  788. new_section(asmlist[al_procedures],sec_code,lower(procdef.mangledname),curralign);
  789. asmlist[al_procedures].concatlist(aktproccode);
  790. { save local data (casetable) also in the same file }
  791. if assigned(aktlocaldata) and
  792. (not aktlocaldata.empty) then
  793. asmlist[al_procedures].concatlist(aktlocaldata);
  794. { only now we can remove the temps }
  795. tg.resettempgen;
  796. { stop tempgen and ra }
  797. tg.free;
  798. cg.done_register_allocators;
  799. tg:=nil;
  800. end;
  801. { restore symtablestack }
  802. remove_from_symtablestack;
  803. { restore }
  804. templist.free;
  805. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  806. aktfilepos:=oldfilepos;
  807. current_procinfo:=oldprocinfo;
  808. end;
  809. procedure tcgprocinfo.add_to_symtablestack;
  810. var
  811. _class,hp : tobjectdef;
  812. begin
  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. oldconstsymtable : tsymtable;
  888. st : tsymtable;
  889. begin
  890. oldprocinfo:=current_procinfo;
  891. oldblock_type:=block_type;
  892. oldconstsymtable:=constsymtable;
  893. { reset break and continue labels }
  894. block_type:=bt_body;
  895. current_procinfo:=self;
  896. { calculate the lexical level }
  897. if procdef.parast.symtablelevel>maxnesting then
  898. Message(parser_e_too_much_lexlevel);
  899. { static is also important for local procedures !! }
  900. if (po_staticmethod in procdef.procoptions) then
  901. allow_only_static:=true
  902. else if (procdef.parast.symtablelevel=normal_function_level) then
  903. allow_only_static:=false;
  904. {$ifdef state_tracking}
  905. { aktstate:=Tstate_storage.create;}
  906. {$endif state_tracking}
  907. { create a local symbol table for this routine }
  908. if not assigned(procdef.localst) then
  909. procdef.insert_localst;
  910. { add parast/localst to symtablestack }
  911. add_to_symtablestack;
  912. { constant symbols are inserted in this symboltable }
  913. constsymtable:=symtablestack;
  914. { save entry info }
  915. entrypos:=aktfilepos;
  916. entryswitches:=aktlocalswitches;
  917. if (df_generic in procdef.defoptions) then
  918. begin
  919. { start token recorder for generic template }
  920. procdef.initgeneric;
  921. current_scanner.startrecordtokens(procdef.generictokenbuf);
  922. end;
  923. { parse the code ... }
  924. code:=block(current_module.islibrary);
  925. if (df_generic in procdef.defoptions) then
  926. begin
  927. { stop token recorder for generic template }
  928. current_scanner.stoprecordtokens;
  929. { Give a warning for accesses in the static symtable that aren't visible
  930. outside the current unit }
  931. st:=procdef.owner;
  932. while (st.symtabletype=objectsymtable) do
  933. st:=st.defowner.owner;
  934. if (pi_uses_static_symtable in flags) and
  935. (st.symtabletype<>staticsymtable) then
  936. Comment(V_Warning,'Global Generic template references static symtable');
  937. end;
  938. { save exit info }
  939. exitswitches:=aktlocalswitches;
  940. exitpos:=last_endtoken_filepos;
  941. { the procedure is now defined }
  942. procdef.forwarddef:=false;
  943. if assigned(code) then
  944. begin
  945. { get a better entry point }
  946. entrypos:=code.fileinfo;
  947. { Finish type checking pass }
  948. do_resulttypepass(code);
  949. end;
  950. { Check for unused labels, forwards, symbols for procedures. Static
  951. symtable is checked in pmodules.
  952. The check must be done after the resulttypepass }
  953. if (Errorcount=0) and
  954. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  955. begin
  956. { check if forwards are resolved }
  957. tstoredsymtable(procdef.localst).check_forwards;
  958. { check if all labels are used }
  959. tstoredsymtable(procdef.localst).checklabels;
  960. { remove cross unit overloads }
  961. tstoredsymtable(procdef.localst).unchain_overloaded;
  962. { check for unused symbols, but only if there is no asm block }
  963. if not(pi_has_assembler_block in flags) then
  964. begin
  965. tstoredsymtable(procdef.localst).allsymbolsused;
  966. tstoredsymtable(procdef.parast).allsymbolsused;
  967. end;
  968. end;
  969. if (po_inline in procdef.procoptions) then
  970. begin
  971. { Can we inline this procedure? }
  972. if checknodeinlining(procdef) then
  973. begin
  974. new(procdef.inlininginfo);
  975. include(procdef.procoptions,po_has_inlininginfo);
  976. procdef.inlininginfo^.code:=code.getcopy;
  977. procdef.inlininginfo^.flags:=current_procinfo.flags;
  978. { The blocknode needs to set an exit label }
  979. if procdef.inlininginfo^.code.nodetype=blockn then
  980. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  981. end;
  982. end;
  983. { Print the node to tree.log }
  984. if paraprintnodetree=1 then
  985. printproc;
  986. { ... remove symbol tables }
  987. remove_from_symtablestack;
  988. {$ifdef state_tracking}
  989. { aktstate.destroy;}
  990. {$endif state_tracking}
  991. { reset to normal non static function }
  992. if (procdef.parast.symtablelevel=normal_function_level) then
  993. allow_only_static:=false;
  994. current_procinfo:=oldprocinfo;
  995. { Restore old state }
  996. constsymtable:=oldconstsymtable;
  997. block_type:=oldblock_type;
  998. end;
  999. {****************************************************************************
  1000. PROCEDURE/FUNCTION PARSING
  1001. ****************************************************************************}
  1002. procedure check_init_paras(p:tnamedindexitem;arg:pointer);
  1003. begin
  1004. if tsym(p).typ<>paravarsym then
  1005. exit;
  1006. with tparavarsym(p) do
  1007. if (not is_class(vartype.def) and
  1008. vartype.def.needs_inittable and
  1009. (varspez in [vs_value,vs_out])) then
  1010. include(current_procinfo.flags,pi_do_call);
  1011. end;
  1012. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1013. {
  1014. Parses the procedure directives, then parses the procedure body, then
  1015. generates the code for it
  1016. }
  1017. procedure do_generate_code(pi:tcgprocinfo);
  1018. var
  1019. hpi : tcgprocinfo;
  1020. begin
  1021. { generate code for this procedure }
  1022. pi.generate_code;
  1023. { process nested procs }
  1024. hpi:=tcgprocinfo(pi.nestedprocs.first);
  1025. while assigned(hpi) do
  1026. begin
  1027. do_generate_code(hpi);
  1028. hpi:=tcgprocinfo(hpi.next);
  1029. end;
  1030. pi.resetprocdef;
  1031. end;
  1032. var
  1033. oldfailtokenmode : tmodeswitch;
  1034. isnestedproc : boolean;
  1035. begin
  1036. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1037. { create a new procedure }
  1038. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1039. current_module.procinfo:=current_procinfo;
  1040. current_procinfo.procdef:=pd;
  1041. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1042. { Insert mangledname }
  1043. pd.aliasnames.insert(pd.mangledname);
  1044. { Handle Export of this procedure }
  1045. if (po_exports in pd.procoptions) and
  1046. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1047. begin
  1048. pd.aliasnames.insert(pd.procsym.realname);
  1049. if cs_link_deffile in aktglobalswitches then
  1050. deffile.AddExport(pd.mangledname);
  1051. end;
  1052. { Insert result variables in the localst }
  1053. insert_funcret_local(pd);
  1054. { check if there are para's which require initing -> set }
  1055. { pi_do_call (if not yet set) }
  1056. if not(pi_do_call in current_procinfo.flags) then
  1057. pd.parast.foreach_static(@check_init_paras,nil);
  1058. { set _FAIL as keyword if constructor }
  1059. if (pd.proctypeoption=potype_constructor) then
  1060. begin
  1061. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1062. tokeninfo^[_FAIL].keyword:=m_all;
  1063. end;
  1064. tcgprocinfo(current_procinfo).parse_body;
  1065. { When it's a nested procedure then defer the code generation,
  1066. when back at normal function level then generate the code
  1067. for all defered nested procedures and the current procedure }
  1068. if isnestedproc then
  1069. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1070. else
  1071. begin
  1072. { We can't support inlining for procedures that have nested
  1073. procedures because the nested procedures use a fixed offset
  1074. for accessing locals in the parent procedure (PFV) }
  1075. if (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1076. begin
  1077. if (df_generic in current_procinfo.procdef.defoptions) then
  1078. {$warning TODO Add error message for nested procs in generics}
  1079. internalerror(200511151)
  1080. else if (po_inline in current_procinfo.procdef.procoptions) then
  1081. begin
  1082. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1083. Message(parser_w_inlining_disabled);
  1084. current_procinfo.procdef.proccalloption:=pocall_default;
  1085. end;
  1086. end;
  1087. if not(df_generic in current_procinfo.procdef.defoptions) then
  1088. do_generate_code(tcgprocinfo(current_procinfo));
  1089. end;
  1090. { reset _FAIL as _SELF normal }
  1091. if (pd.proctypeoption=potype_constructor) then
  1092. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1093. { release procinfo }
  1094. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1095. internalerror(200304274);
  1096. current_module.procinfo:=current_procinfo.parent;
  1097. if not isnestedproc then
  1098. current_procinfo.free;
  1099. { For specialization we didn't record the last semicolon. Moving this parsing
  1100. into the parse_body routine is not done because of having better file position
  1101. information available }
  1102. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1103. consume(_SEMICOLON);
  1104. end;
  1105. procedure read_proc;
  1106. {
  1107. Parses the procedure directives, then parses the procedure body, then
  1108. generates the code for it
  1109. }
  1110. procedure do_generate_code(pi:tcgprocinfo);
  1111. var
  1112. hpi : tcgprocinfo;
  1113. begin
  1114. { generate code for this procedure }
  1115. pi.generate_code;
  1116. { process nested procs }
  1117. hpi:=tcgprocinfo(pi.nestedprocs.first);
  1118. while assigned(hpi) do
  1119. begin
  1120. do_generate_code(hpi);
  1121. hpi:=tcgprocinfo(hpi.next);
  1122. end;
  1123. pi.resetprocdef;
  1124. end;
  1125. var
  1126. old_current_procinfo : tprocinfo;
  1127. pdflags : tpdflags;
  1128. pd : tprocdef;
  1129. s : string;
  1130. begin
  1131. { save old state }
  1132. old_current_procinfo:=current_procinfo;
  1133. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1134. to an other procdef }
  1135. current_procinfo:=nil;
  1136. { parse procedure declaration }
  1137. if assigned(old_current_procinfo) and
  1138. assigned(old_current_procinfo.procdef) then
  1139. pd:=parse_proc_dec(old_current_procinfo.procdef._class)
  1140. else
  1141. pd:=parse_proc_dec(nil);
  1142. { set the default function options }
  1143. if parse_only then
  1144. begin
  1145. pd.forwarddef:=true;
  1146. { set also the interface flag, for better error message when the
  1147. implementation doesn't much this header }
  1148. pd.interfacedef:=true;
  1149. include(pd.procoptions,po_global);
  1150. pdflags:=[pd_interface];
  1151. end
  1152. else
  1153. begin
  1154. pdflags:=[pd_body];
  1155. if (not current_module.in_interface) then
  1156. include(pdflags,pd_implemen);
  1157. if (not current_module.is_unit) or
  1158. maybe_smartlink_symbol or
  1159. {
  1160. taking addresses of static procedures goes wrong
  1161. if they aren't global when pic is used (FK)
  1162. }
  1163. (cs_create_pic in aktmoduleswitches) then
  1164. include(pd.procoptions,po_global);
  1165. pd.forwarddef:=false;
  1166. end;
  1167. { parse the directives that may follow }
  1168. parse_proc_directives(pd,pdflags);
  1169. { hint directives, these can be separated by semicolons here,
  1170. that needs to be handled here with a loop (PFV) }
  1171. while try_consume_hintdirective(pd.symoptions) do
  1172. Consume(_SEMICOLON);
  1173. { Set calling convention }
  1174. handle_calling_convention(pd);
  1175. { search for forward declarations }
  1176. if not proc_add_definition(pd) then
  1177. begin
  1178. { A method must be forward defined (in the object declaration) }
  1179. if assigned(pd._class) and
  1180. (not assigned(old_current_procinfo.procdef._class)) then
  1181. begin
  1182. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1183. tprocsym(pd.procsym).write_parameter_lists(pd);
  1184. end
  1185. else
  1186. begin
  1187. { Give a better error if there is a forward def in the interface and only
  1188. a single implementation }
  1189. if (not pd.forwarddef) and
  1190. (not pd.interfacedef) and
  1191. (tprocsym(pd.procsym).procdef_count>1) and
  1192. tprocsym(pd.procsym).first_procdef.forwarddef and
  1193. tprocsym(pd.procsym).first_procdef.interfacedef and
  1194. not(tprocsym(pd.procsym).procdef_count>2) then
  1195. begin
  1196. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1197. tprocsym(pd.procsym).write_parameter_lists(pd);
  1198. end;
  1199. end;
  1200. end;
  1201. { Set mangled name }
  1202. proc_set_mangledname(pd);
  1203. { compile procedure when a body is needed }
  1204. if (pd_body in pdflags) then
  1205. begin
  1206. read_proc_body(old_current_procinfo,pd);
  1207. end
  1208. else
  1209. begin
  1210. { Handle imports }
  1211. if (po_external in pd.procoptions) then
  1212. begin
  1213. { External declared in implementation, and there was already a
  1214. forward (or interface) declaration then we need to generate
  1215. a stub that calls the external routine }
  1216. if (not pd.forwarddef) and
  1217. (pd.hasforward) and
  1218. not(
  1219. assigned(pd.import_dll) and
  1220. (target_info.system in [system_i386_win32,system_i386_wdosx,
  1221. system_i386_emx,system_i386_os2,system_arm_wince,system_i386_wince])
  1222. ) then
  1223. begin
  1224. s:=proc_get_importname(pd);
  1225. if s<>'' then
  1226. gen_external_stub(asmlist[al_procedures],pd,{$IFDEF POWERPC64}'.'+{$ENDIF}s);
  1227. end;
  1228. { Import DLL specified? }
  1229. if assigned(pd.import_dll) then
  1230. begin
  1231. { create importlib if not already done }
  1232. if not(current_module.uses_imports) then
  1233. begin
  1234. current_module.uses_imports:=true;
  1235. importlib.preparelib(current_module.realmodulename^);
  1236. end;
  1237. if assigned(pd.import_name) then
  1238. importlib.importprocedure(pd,pd.import_dll^,pd.import_nr,pd.import_name^)
  1239. else
  1240. importlib.importprocedure(pd,pd.import_dll^,pd.import_nr,'');
  1241. end
  1242. else
  1243. begin
  1244. { add import name to external list for DLL scanning }
  1245. if target_info.DllScanSupported then
  1246. current_module.externals.insert(tExternalsItem.create(proc_get_importname(pd)));
  1247. end;
  1248. end;
  1249. end;
  1250. current_procinfo:=old_current_procinfo;
  1251. end;
  1252. {****************************************************************************
  1253. DECLARATION PARSING
  1254. ****************************************************************************}
  1255. { search in symtablestack for not complete classes }
  1256. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  1257. begin
  1258. if (tsym(p).typ=typesym) and
  1259. (ttypesym(p).restype.def.deftype=objectdef) and
  1260. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  1261. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1262. end;
  1263. procedure read_declarations(islibrary : boolean);
  1264. begin
  1265. repeat
  1266. if not assigned(current_procinfo) then
  1267. internalerror(200304251);
  1268. case token of
  1269. _LABEL:
  1270. label_dec;
  1271. _CONST:
  1272. const_dec;
  1273. _TYPE:
  1274. type_dec;
  1275. _VAR:
  1276. var_dec;
  1277. _THREADVAR:
  1278. threadvar_dec;
  1279. _CONSTRUCTOR,
  1280. _DESTRUCTOR,
  1281. _FUNCTION,
  1282. _PROCEDURE,
  1283. _OPERATOR,
  1284. _CLASS:
  1285. read_proc;
  1286. _EXPORTS:
  1287. begin
  1288. if not(assigned(current_procinfo.procdef.localst)) or
  1289. (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  1290. begin
  1291. Message(parser_e_syntax_error);
  1292. consume_all_until(_SEMICOLON);
  1293. end
  1294. else if islibrary or
  1295. (target_info.system in system_unit_program_exports) then
  1296. read_exports
  1297. else
  1298. begin
  1299. Message(parser_w_unsupported_feature);
  1300. consume(_BEGIN);
  1301. end;
  1302. end
  1303. else
  1304. begin
  1305. case idtoken of
  1306. _RESOURCESTRING :
  1307. begin
  1308. { m_class is needed, because the resourcestring
  1309. loading is in the ObjPas unit }
  1310. if (m_class in aktmodeswitches) then
  1311. resourcestring_dec
  1312. else
  1313. break;
  1314. end;
  1315. _PROPERTY:
  1316. begin
  1317. if (m_fpc in aktmodeswitches) then
  1318. property_dec
  1319. else
  1320. break;
  1321. end;
  1322. else
  1323. break;
  1324. end;
  1325. end;
  1326. end;
  1327. until false;
  1328. { check for incomplete class definitions, this is only required
  1329. for fpc modes }
  1330. if (m_fpc in aktmodeswitches) then
  1331. symtablestack.foreach_static(@check_forward_class,nil);
  1332. end;
  1333. procedure read_interface_declarations;
  1334. begin
  1335. repeat
  1336. case token of
  1337. _CONST :
  1338. const_dec;
  1339. _TYPE :
  1340. type_dec;
  1341. _VAR :
  1342. var_dec;
  1343. _THREADVAR :
  1344. threadvar_dec;
  1345. _FUNCTION,
  1346. _PROCEDURE,
  1347. _OPERATOR :
  1348. read_proc;
  1349. else
  1350. begin
  1351. case idtoken of
  1352. _RESOURCESTRING :
  1353. resourcestring_dec;
  1354. _PROPERTY:
  1355. begin
  1356. if (m_fpc in aktmodeswitches) then
  1357. property_dec
  1358. else
  1359. break;
  1360. end;
  1361. else
  1362. break;
  1363. end;
  1364. end;
  1365. end;
  1366. until false;
  1367. { check for incomplete class definitions, this is only required
  1368. for fpc modes }
  1369. if (m_fpc in aktmodeswitches) then
  1370. symtablestack.foreach_static(@check_forward_class,nil);
  1371. end;
  1372. {****************************************************************************
  1373. SPECIALIZATION BODY GENERATION
  1374. ****************************************************************************}
  1375. procedure specialize_objectdefs(p:tnamedindexitem;arg:pointer);
  1376. var
  1377. hp : tdef;
  1378. oldaktfilepos : tfileposinfo;
  1379. begin
  1380. if not((tsym(p).typ=typesym) and
  1381. (ttypesym(p).restype.def.deftype=objectdef) and
  1382. (df_specialization in ttypesym(p).restype.def.defoptions)
  1383. ) then
  1384. exit;
  1385. { definitions }
  1386. hp:=tdef(tobjectdef(ttypesym(p).restype.def).symtable.defindex.first);
  1387. while assigned(hp) do
  1388. begin
  1389. if hp.deftype=procdef then
  1390. begin
  1391. if not(
  1392. assigned(tprocdef(hp).genericdef) and
  1393. (tprocdef(hp).genericdef.deftype=procdef) and
  1394. assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf)
  1395. ) then
  1396. internalerror(200512111);
  1397. oldaktfilepos:=aktfilepos;
  1398. aktfilepos:=tprocdef(tprocdef(hp).genericdef).fileinfo;
  1399. akttokenpos:=aktfilepos;
  1400. current_scanner.startreplaytokens(tprocdef(tprocdef(hp).genericdef).generictokenbuf);
  1401. read_proc_body(nil,tprocdef(hp));
  1402. aktfilepos:=oldaktfilepos;
  1403. end;
  1404. hp:=tdef(hp.indexnext);
  1405. end;
  1406. end;
  1407. procedure generate_specialization_procs;
  1408. var
  1409. oldsymtablestack : tsymtable;
  1410. begin
  1411. if assigned(current_module.globalsymtable) then
  1412. current_module.globalsymtable.foreach_static(@specialize_objectdefs,nil);
  1413. if assigned(current_module.localsymtable) then
  1414. begin
  1415. oldsymtablestack:=symtablestack;
  1416. current_module.localsymtable.next:=symtablestack;
  1417. symtablestack:=current_module.localsymtable;
  1418. current_module.localsymtable.foreach_static(@specialize_objectdefs,nil);
  1419. symtablestack:=oldsymtablestack;
  1420. end;
  1421. end;
  1422. end.