psub.pas 62 KB

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