psub.pas 60 KB

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