psub.pas 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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,optdfa;
  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. dfabuilder : TDFABuilder;
  41. constructor create(aparent:tprocinfo);override;
  42. destructor destroy;override;
  43. procedure printproc(pass:string);
  44. procedure generate_code;
  45. procedure resetprocdef;
  46. procedure add_to_symtablestack;
  47. procedure remove_from_symtablestack;
  48. procedure parse_body;
  49. function stack_tainting_parameter : boolean;
  50. function has_assembler_child : boolean;
  51. end;
  52. procedure printnode_reset;
  53. { reads the declaration blocks }
  54. procedure read_declarations(islibrary : boolean);
  55. { reads declarations in the interface part of a unit }
  56. procedure read_interface_declarations;
  57. procedure generate_specialization_procs;
  58. implementation
  59. uses
  60. sysutils,
  61. { common }
  62. cutils,
  63. { global }
  64. globtype,tokens,verbose,comphook,constexp,
  65. systems,
  66. { aasm }
  67. cpuinfo,cpubase,aasmbase,aasmtai,aasmdata,
  68. { symtable }
  69. symconst,symbase,symsym,symtype,symtable,defutil,
  70. paramgr,
  71. ppu,fmodule,
  72. { pass 1 }
  73. nutils,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  74. pass_1,
  75. {$ifdef state_tracking}
  76. nstate,
  77. {$endif state_tracking}
  78. { pass 2 }
  79. {$ifndef NOPASS2}
  80. pass_2,
  81. {$endif}
  82. { parser }
  83. scanner,import,gendef,
  84. pbase,pstatmnt,pdecl,pdecsub,pexports,
  85. { codegen }
  86. tgobj,cgbase,cgobj,cgcpu,dbgbase,
  87. ncgutil,regvars,
  88. optbase,
  89. opttail,
  90. optcse,optloop,
  91. optutils
  92. {$if defined(arm) or defined(powerpc) or defined(powerpc64)}
  93. ,aasmcpu
  94. {$endif arm}
  95. {$ifndef NOOPT}
  96. {$ifdef i386}
  97. ,aopt386
  98. {$else i386}
  99. ,aopt
  100. {$endif i386}
  101. {$endif}
  102. ;
  103. {****************************************************************************
  104. PROCEDURE/FUNCTION BODY PARSING
  105. ****************************************************************************}
  106. procedure initializevars(p:TObject;arg:pointer);
  107. var
  108. b : tblocknode;
  109. begin
  110. if not (tsym(p).typ in [localvarsym,staticvarsym]) then
  111. exit;
  112. with tabstractnormalvarsym(p) do
  113. begin
  114. if assigned(defaultconstsym) then
  115. begin
  116. b:=tblocknode(arg);
  117. b.left:=cstatementnode.create(
  118. cassignmentnode.create(
  119. cloadnode.create(tsym(p),tsym(p).owner),
  120. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  121. b.left);
  122. end;
  123. end;
  124. end;
  125. procedure check_finalize_paras(p:TObject;arg:pointer);
  126. begin
  127. if (tsym(p).typ=paravarsym) and
  128. (tparavarsym(p).varspez=vs_value) and
  129. not is_class(tparavarsym(p).vardef) and
  130. tparavarsym(p).vardef.needs_inittable then
  131. include(current_procinfo.flags,pi_needs_implicit_finally);
  132. end;
  133. procedure check_finalize_locals(p:TObject;arg:pointer);
  134. begin
  135. { include the result: it needs to be finalized in case an exception }
  136. { occurs }
  137. if (tsym(p).typ=localvarsym) and
  138. (tlocalvarsym(p).refs>0) and
  139. not(is_class(tlocalvarsym(p).vardef)) and
  140. tlocalvarsym(p).vardef.needs_inittable then
  141. include(current_procinfo.flags,pi_needs_implicit_finally);
  142. end;
  143. function block(islibrary : boolean) : tnode;
  144. begin
  145. { parse const,types and vars }
  146. read_declarations(islibrary);
  147. { do we have an assembler block without the po_assembler?
  148. we should allow this for Delphi compatibility (PFV) }
  149. if (token=_ASM) and (m_delphi in current_settings.modeswitches) then
  150. include(current_procinfo.procdef.procoptions,po_assembler);
  151. { Handle assembler block different }
  152. if (po_assembler in current_procinfo.procdef.procoptions) then
  153. begin
  154. block:=assembler_block;
  155. exit;
  156. end;
  157. {Unit initialization?.}
  158. if (
  159. assigned(current_procinfo.procdef.localst) and
  160. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  161. (current_module.is_unit)
  162. ) or
  163. islibrary then
  164. begin
  165. if (token=_END) then
  166. begin
  167. consume(_END);
  168. { We need at least a node, else the entry/exit code is not
  169. generated and thus no PASCALMAIN symbol which we need (PFV) }
  170. if islibrary then
  171. block:=cnothingnode.create
  172. else
  173. block:=nil;
  174. end
  175. else
  176. begin
  177. if token=_INITIALIZATION then
  178. begin
  179. { The library init code is already called and does not
  180. need to be in the initfinal table (PFV) }
  181. block:=statement_block(_INITIALIZATION);
  182. { optimize empty initialization block away }
  183. if (block.nodetype=blockn) and (tblocknode(block).left=nil) then
  184. FreeAndNil(block)
  185. else
  186. if not islibrary then
  187. current_module.flags:=current_module.flags or uf_init;
  188. end
  189. else if token=_FINALIZATION then
  190. begin
  191. { when a unit has only a finalization section, we can come to this
  192. point when we try to read the nonh existing initalization section
  193. so we've to check if we are really try to parse the finalization }
  194. if current_procinfo.procdef.proctypeoption=potype_unitfinalize then
  195. begin
  196. block:=statement_block(_FINALIZATION);
  197. { optimize empty finalization block away }
  198. if (block.nodetype=blockn) and (tblocknode(block).left=nil) then
  199. FreeAndNil(block)
  200. else
  201. current_module.flags:=current_module.flags or uf_finalize;
  202. end
  203. else
  204. block:=nil;
  205. end
  206. else
  207. begin
  208. { The library init code is already called and does not
  209. need to be in the initfinal table (PFV) }
  210. if not islibrary then
  211. current_module.flags:=current_module.flags or uf_init;
  212. block:=statement_block(_BEGIN);
  213. end;
  214. end;
  215. end
  216. else
  217. begin
  218. block:=statement_block(_BEGIN);
  219. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  220. current_procinfo.procdef.localst.SymList.ForEachCall(@initializevars,block);
  221. end;
  222. end;
  223. {****************************************************************************
  224. PROCEDURE/FUNCTION COMPILING
  225. ****************************************************************************}
  226. procedure printnode_reset;
  227. begin
  228. assign(printnodefile,treelogfilename);
  229. {$I-}
  230. rewrite(printnodefile);
  231. {$I+}
  232. if ioresult<>0 then
  233. begin
  234. Comment(V_Error,'Error creating '+treelogfilename);
  235. exit;
  236. end;
  237. close(printnodefile);
  238. end;
  239. function generate_bodyentry_block:tnode;
  240. var
  241. srsym : tsym;
  242. para : tcallparanode;
  243. newstatement : tstatementnode;
  244. begin
  245. result:=internalstatements(newstatement);
  246. if assigned(current_objectdef) then
  247. begin
  248. { a constructor needs a help procedure }
  249. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  250. begin
  251. if is_class(current_objectdef) then
  252. begin
  253. include(current_procinfo.flags,pi_needs_implicit_finally);
  254. srsym:=search_class_member(current_objectdef,'NEWINSTANCE');
  255. if assigned(srsym) and
  256. (srsym.typ=procsym) then
  257. begin
  258. { if vmt>1 then newinstance }
  259. addstatement(newstatement,cifnode.create(
  260. caddnode.create(gtn,
  261. ctypeconvnode.create_internal(
  262. load_vmt_pointer_node,
  263. voidpointertype),
  264. cpointerconstnode.create(1,voidpointertype)),
  265. cassignmentnode.create(
  266. ctypeconvnode.create_internal(
  267. load_self_pointer_node,
  268. voidpointertype),
  269. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_vmt_pointer_node,[])),
  270. nil));
  271. end
  272. else
  273. internalerror(200305108);
  274. end
  275. else
  276. if is_object(current_objectdef) then
  277. begin
  278. { parameter 3 : vmt_offset }
  279. { parameter 2 : address of pointer to vmt,
  280. this is required to allow setting the vmt to -1 to indicate
  281. that memory was allocated }
  282. { parameter 1 : self pointer }
  283. para:=ccallparanode.create(
  284. cordconstnode.create(current_objectdef.vmt_offset,s32inttype,false),
  285. ccallparanode.create(
  286. ctypeconvnode.create_internal(
  287. load_vmt_pointer_node,
  288. voidpointertype),
  289. ccallparanode.create(
  290. ctypeconvnode.create_internal(
  291. load_self_pointer_node,
  292. voidpointertype),
  293. nil)));
  294. addstatement(newstatement,cassignmentnode.create(
  295. ctypeconvnode.create_internal(
  296. load_self_pointer_node,
  297. voidpointertype),
  298. ccallnode.createintern('fpc_help_constructor',para)));
  299. end
  300. else
  301. internalerror(200305103);
  302. { if self=nil then exit
  303. calling fail instead of exit is useless because
  304. there is nothing to dispose (PFV) }
  305. addstatement(newstatement,cifnode.create(
  306. caddnode.create(equaln,
  307. load_self_pointer_node,
  308. cnilnode.create),
  309. cexitnode.create(nil),
  310. nil));
  311. end;
  312. { maybe call BeforeDestruction for classes }
  313. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  314. is_class(current_objectdef) then
  315. begin
  316. srsym:=search_class_member(current_objectdef,'BEFOREDESTRUCTION');
  317. if assigned(srsym) and
  318. (srsym.typ=procsym) then
  319. begin
  320. { if vmt>0 then beforedestruction }
  321. addstatement(newstatement,cifnode.create(
  322. caddnode.create(gtn,
  323. ctypeconvnode.create_internal(
  324. load_vmt_pointer_node,ptrsinttype),
  325. ctypeconvnode.create_internal(
  326. cnilnode.create,ptrsinttype)),
  327. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  328. nil));
  329. end
  330. else
  331. internalerror(200305104);
  332. end;
  333. end;
  334. end;
  335. function generate_bodyexit_block:tnode;
  336. var
  337. srsym : tsym;
  338. para : tcallparanode;
  339. newstatement : tstatementnode;
  340. oldlocalswitches: tlocalswitches;
  341. begin
  342. result:=internalstatements(newstatement);
  343. if assigned(current_objectdef) then
  344. begin
  345. { Don't test self and the vmt here. The reason is that }
  346. { a constructor already checks whether these are valid }
  347. { before. Further, in case of TThread the thread may }
  348. { free the class instance right after AfterConstruction }
  349. { has been called, so it may no longer be valid (JM) }
  350. oldlocalswitches:=current_settings.localswitches;
  351. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  352. { a destructor needs a help procedure }
  353. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  354. begin
  355. if is_class(current_objectdef) then
  356. begin
  357. srsym:=search_class_member(current_objectdef,'FREEINSTANCE');
  358. if assigned(srsym) and
  359. (srsym.typ=procsym) then
  360. begin
  361. { if self<>0 and vmt<>0 then freeinstance }
  362. addstatement(newstatement,cifnode.create(
  363. caddnode.create(andn,
  364. caddnode.create(unequaln,
  365. load_self_pointer_node,
  366. cnilnode.create),
  367. caddnode.create(unequaln,
  368. ctypeconvnode.create(
  369. load_vmt_pointer_node,
  370. voidpointertype),
  371. cpointerconstnode.create(0,voidpointertype))),
  372. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  373. nil));
  374. end
  375. else
  376. internalerror(200305108);
  377. end
  378. else
  379. if is_object(current_objectdef) then
  380. begin
  381. { finalize object data }
  382. if current_objectdef.needs_inittable then
  383. addstatement(newstatement,finalize_data_node(load_self_node));
  384. { parameter 3 : vmt_offset }
  385. { parameter 2 : pointer to vmt }
  386. { parameter 1 : self pointer }
  387. para:=ccallparanode.create(
  388. cordconstnode.create(current_objectdef.vmt_offset,s32inttype,false),
  389. ccallparanode.create(
  390. ctypeconvnode.create_internal(
  391. load_vmt_pointer_node,
  392. voidpointertype),
  393. ccallparanode.create(
  394. ctypeconvnode.create_internal(
  395. load_self_pointer_node,
  396. voidpointertype),
  397. nil)));
  398. addstatement(newstatement,
  399. ccallnode.createintern('fpc_help_destructor',para));
  400. end
  401. else
  402. internalerror(200305105);
  403. end;
  404. current_settings.localswitches:=oldlocalswitches;
  405. end;
  406. end;
  407. function generate_except_block:tnode;
  408. var
  409. pd : tprocdef;
  410. newstatement : tstatementnode;
  411. oldlocalswitches: tlocalswitches;
  412. { safecall handling }
  413. exceptobjnode,exceptaddrnode: ttempcreatenode;
  414. sym,exceptsym: tsym;
  415. begin
  416. generate_except_block:=internalstatements(newstatement);
  417. { a constructor needs call destructor (if available) when it
  418. is not inherited }
  419. if assigned(current_objectdef) and
  420. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  421. begin
  422. { Don't test self and the vmt here. See generate_bodyexit_block }
  423. { why (JM) }
  424. oldlocalswitches:=current_settings.localswitches;
  425. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  426. pd:=current_objectdef.find_destructor;
  427. if assigned(pd) then
  428. begin
  429. { if vmt<>0 then call destructor }
  430. addstatement(newstatement,cifnode.create(
  431. caddnode.create(unequaln,
  432. load_vmt_pointer_node,
  433. cnilnode.create),
  434. { cnf_create_failed -> don't call BeforeDestruction }
  435. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[cnf_create_failed]),
  436. nil));
  437. end;
  438. current_settings.localswitches:=oldlocalswitches;
  439. end
  440. else
  441. begin
  442. { no constructor }
  443. { must be the return value finalized before reraising the exception? }
  444. if (not is_void(current_procinfo.procdef.returndef)) and
  445. (current_procinfo.procdef.returndef.needs_inittable) and
  446. (not paramanager.ret_in_param(current_procinfo.procdef.returndef, current_procinfo.procdef.proccalloption)) and
  447. (not is_class(current_procinfo.procdef.returndef)) then
  448. addstatement(newstatement,finalize_data_node(load_result_node));
  449. {$if defined(x86) or defined(arm)}
  450. { safecall handling }
  451. if (target_info.system in systems_all_windows) and
  452. (current_procinfo.procdef.proccalloption=pocall_safecall) then
  453. begin
  454. { create a local hidden variable "safe_result" }
  455. { it will be used in ncgflw unit }
  456. { to set "real" result value for safecall routine }
  457. sym:=tlocalvarsym.create('$safe_result',vs_value,hresultdef,[]);
  458. include(sym.symoptions,sp_internal);
  459. current_procinfo.procdef.localst.insert(sym);
  460. { if safecall is used for a class method we need to call }
  461. { SafecallException virtual method }
  462. { In other case we return E_UNEXPECTED error value }
  463. if is_class(current_procinfo.procdef._class) then
  464. begin
  465. { temp variable to store exception address }
  466. exceptaddrnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,
  467. tt_persistent,true);
  468. addstatement(newstatement,exceptaddrnode);
  469. addstatement(newstatement,
  470. cassignmentnode.create(
  471. ctemprefnode.create(exceptaddrnode),
  472. ccallnode.createintern('fpc_getexceptionaddr',nil)));
  473. { temp variable to store popped up exception }
  474. exceptobjnode:=ctempcreatenode.create(class_tobject,class_tobject.size,
  475. tt_persistent,true);
  476. addstatement(newstatement,exceptobjnode);
  477. addstatement(newstatement,
  478. cassignmentnode.create(
  479. ctemprefnode.create(exceptobjnode),
  480. ccallnode.createintern('fpc_popobjectstack', nil)));
  481. exceptsym:=search_class_member(current_procinfo.procdef._class,'SAFECALLEXCEPTION');
  482. addstatement(newstatement,
  483. cassignmentnode.create(
  484. cloadnode.create(sym,sym.Owner),
  485. ccallnode.create(
  486. ccallparanode.create(ctemprefnode.create(exceptaddrnode),
  487. ccallparanode.create(ctemprefnode.create(exceptobjnode),nil)),
  488. tprocsym(exceptsym), tprocsym(exceptsym).owner,load_self_node,[])));
  489. addstatement(newstatement,ccallnode.createintern('fpc_destroyexception',
  490. ccallparanode.create(ctemprefnode.create(exceptobjnode),nil)));
  491. addstatement(newstatement,ctempdeletenode.create(exceptobjnode));
  492. addstatement(newstatement,ctempdeletenode.create(exceptaddrnode));
  493. end
  494. else
  495. begin
  496. { pop up and destroy an exception }
  497. addstatement(newstatement,ccallnode.createintern('fpc_destroyexception',
  498. ccallparanode.create(ccallnode.createintern('fpc_popobjectstack', nil),nil)));
  499. addstatement(newstatement,
  500. cassignmentnode.create(
  501. cloadnode.create(sym,sym.Owner),
  502. genintconstnode(HResult($8000FFFF))));
  503. end;
  504. end;
  505. {$endif}
  506. end;
  507. end;
  508. procedure maybe_add_afterconstruction(var tocode: tnode);
  509. var
  510. oldlocalswitches: tlocalswitches;
  511. srsym: tsym;
  512. newblock: tblocknode;
  513. newstatement: tstatementnode;
  514. begin
  515. { maybe call AfterConstruction for classes }
  516. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  517. is_class(current_objectdef) then
  518. begin
  519. srsym:=search_class_member(current_objectdef,'AFTERCONSTRUCTION');
  520. if assigned(srsym) and
  521. (srsym.typ=procsym) then
  522. begin
  523. { Don't test self and the vmt here. See }
  524. { generate_bodyexit_block why (JM) }
  525. oldlocalswitches:=current_settings.localswitches;
  526. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  527. newblock:=internalstatements(newstatement);
  528. addstatement(newstatement,tocode);
  529. { Self can be nil when fail is called }
  530. { if self<>nil and vmt<>nil then afterconstruction }
  531. addstatement(newstatement,cifnode.create(
  532. caddnode.create(andn,
  533. caddnode.create(unequaln,
  534. load_self_pointer_node,
  535. cnilnode.create),
  536. caddnode.create(unequaln,
  537. load_vmt_pointer_node,
  538. cnilnode.create)),
  539. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  540. nil));
  541. tocode:=newblock;
  542. current_settings.localswitches:=oldlocalswitches;
  543. end
  544. else
  545. internalerror(200305106);
  546. end;
  547. end;
  548. {****************************************************************************
  549. TCGProcInfo
  550. ****************************************************************************}
  551. constructor tcgprocinfo.create(aparent:tprocinfo);
  552. begin
  553. inherited Create(aparent);
  554. nestedprocs:=tlinkedlist.create;
  555. end;
  556. destructor tcgprocinfo.destroy;
  557. begin
  558. nestedprocs.free;
  559. if assigned(code) then
  560. code.free;
  561. inherited destroy;
  562. end;
  563. procedure tcgprocinfo.printproc(pass:string);
  564. begin
  565. assign(printnodefile,treelogfilename);
  566. {$I-}
  567. append(printnodefile);
  568. if ioresult<>0 then
  569. rewrite(printnodefile);
  570. {$I+}
  571. if ioresult<>0 then
  572. begin
  573. Comment(V_Error,'Error creating '+treelogfilename);
  574. exit;
  575. end;
  576. writeln(printnodefile);
  577. writeln(printnodefile,'*******************************************************************************');
  578. writeln(printnodefile, pass);
  579. writeln(printnodefile,procdef.fullprocname(false));
  580. writeln(printnodefile,'*******************************************************************************');
  581. printnode(printnodefile,code);
  582. close(printnodefile);
  583. end;
  584. procedure tcgprocinfo.add_entry_exit_code;
  585. var
  586. finalcode,
  587. bodyentrycode,
  588. bodyexitcode,
  589. exceptcode : tnode;
  590. newblock : tblocknode;
  591. codestatement,
  592. newstatement : tstatementnode;
  593. oldfilepos : tfileposinfo;
  594. begin
  595. oldfilepos:=current_filepos;
  596. { Generate code/locations used at start of proc }
  597. current_filepos:=entrypos;
  598. entry_asmnode:=casmnode.create_get_position;
  599. loadpara_asmnode:=casmnode.create_get_position;
  600. stackcheck_asmnode:=casmnode.create_get_position;
  601. init_asmnode:=casmnode.create_get_position;
  602. bodyentrycode:=generate_bodyentry_block;
  603. { Generate code/locations used at end of proc }
  604. current_filepos:=exitpos;
  605. exitlabel_asmnode:=casmnode.create_get_position;
  606. final_asmnode:=casmnode.create_get_position;
  607. bodyexitcode:=generate_bodyexit_block;
  608. maybe_add_afterconstruction(code);
  609. { Generate procedure by combining init+body+final,
  610. depending on the implicit finally we need to add
  611. an try...finally...end wrapper }
  612. newblock:=internalstatements(newstatement);
  613. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  614. (pi_needs_implicit_finally in flags) and
  615. { but it's useless in init/final code of units }
  616. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  617. begin
  618. { Generate special exception block only needed when
  619. implicit finaly is used }
  620. current_filepos:=exitpos;
  621. exceptcode:=generate_except_block;
  622. { Generate code that will be in the try...finally }
  623. finalcode:=internalstatements(codestatement);
  624. addstatement(codestatement,final_asmnode);
  625. { Initialize before try...finally...end frame }
  626. addstatement(newstatement,loadpara_asmnode);
  627. addstatement(newstatement,stackcheck_asmnode);
  628. addstatement(newstatement,entry_asmnode);
  629. addstatement(newstatement,init_asmnode);
  630. addstatement(newstatement,bodyentrycode);
  631. current_filepos:=entrypos;
  632. addstatement(newstatement,ctryfinallynode.create_implicit(
  633. code,
  634. finalcode,
  635. exceptcode));
  636. addstatement(newstatement,exitlabel_asmnode);
  637. addstatement(newstatement,bodyexitcode);
  638. { set flag the implicit finally has been generated }
  639. include(flags,pi_has_implicit_finally);
  640. end
  641. else
  642. begin
  643. addstatement(newstatement,loadpara_asmnode);
  644. addstatement(newstatement,stackcheck_asmnode);
  645. addstatement(newstatement,entry_asmnode);
  646. addstatement(newstatement,init_asmnode);
  647. addstatement(newstatement,bodyentrycode);
  648. addstatement(newstatement,code);
  649. addstatement(newstatement,exitlabel_asmnode);
  650. addstatement(newstatement,bodyexitcode);
  651. addstatement(newstatement,final_asmnode);
  652. end;
  653. do_firstpass(tnode(newblock));
  654. code:=newblock;
  655. current_filepos:=oldfilepos;
  656. end;
  657. procedure clearrefs(p:TObject;arg:pointer);
  658. begin
  659. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  660. if tabstractvarsym(p).refs>1 then
  661. tabstractvarsym(p).refs:=1;
  662. end;
  663. procedure translate_registers(p:TObject;list:pointer);
  664. begin
  665. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  666. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  667. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  668. begin
  669. if not(cs_no_regalloc in current_settings.globalswitches) then
  670. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  671. if cs_asm_source in current_settings.globalswitches then
  672. TAsmList(list).concat(Tai_comment.Create(strpnew('Var '+tabstractnormalvarsym(p).realname+' located in register '+
  673. std_regname(tabstractnormalvarsym(p).localloc.register))))
  674. end;
  675. end;
  676. procedure check_for_stack(p:TObject;arg:pointer);
  677. begin
  678. if tsym(p).typ=paravarsym then
  679. begin
  680. { check if there no parameter of the current procedure is stack dependend }
  681. if is_open_array(tparavarsym(p).vardef) or
  682. is_array_of_const(tparavarsym(p).vardef) then
  683. pboolean(arg)^:=true;
  684. if assigned(p) and
  685. assigned(tparavarsym(p).paraloc[calleeside].location) and
  686. (tparavarsym(p).paraloc[calleeside].location^.loc=LOC_REFERENCE) then
  687. pboolean(arg)^:=true;
  688. end;
  689. end;
  690. function tcgprocinfo.stack_tainting_parameter : boolean;
  691. begin
  692. result:=false;
  693. procdef.parast.SymList.ForEachCall(@check_for_stack,@result);
  694. end;
  695. function tcgprocinfo.has_assembler_child : boolean;
  696. var
  697. hp : tcgprocinfo;
  698. begin
  699. result:=false;
  700. hp:=tcgprocinfo(nestedprocs.first);
  701. while assigned(hp) do
  702. begin
  703. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  704. begin
  705. result:=true;
  706. exit;
  707. end;
  708. hp:=tcgprocinfo(hp.next);
  709. end;
  710. end;
  711. procedure tcgprocinfo.generate_code;
  712. var
  713. old_current_procinfo : tprocinfo;
  714. oldmaxfpuregisters : longint;
  715. oldfilepos : tfileposinfo;
  716. old_current_objectdef : tobjectdef;
  717. templist : TAsmList;
  718. headertai : tai;
  719. i : integer;
  720. varsym : tabstractnormalvarsym;
  721. {RedoDFA : boolean;}
  722. begin
  723. { the initialization procedure can be empty, then we
  724. don't need to generate anything. When it was an empty
  725. procedure there would be at least a blocknode }
  726. if not assigned(code) then
  727. exit;
  728. { We need valid code }
  729. if Errorcount<>0 then
  730. exit;
  731. { No code can be generated for generic template }
  732. if (df_generic in procdef.defoptions) then
  733. internalerror(200511152);
  734. { The RA and Tempgen shall not be available yet }
  735. if assigned(tg) then
  736. internalerror(200309201);
  737. old_current_procinfo:=current_procinfo;
  738. oldfilepos:=current_filepos;
  739. old_current_objectdef:=current_objectdef;
  740. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  741. current_procinfo:=self;
  742. current_filepos:=entrypos;
  743. current_objectdef:=procdef._class;
  744. templist:=TAsmList.create;
  745. { add parast/localst to symtablestack }
  746. add_to_symtablestack;
  747. { clear register count }
  748. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  749. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  750. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  751. if (procdef.localst.symtablelevel=main_program_level) and
  752. (not current_module.is_unit) then
  753. include(flags,pi_do_call);
  754. { set implicit_finally flag when there are locals/paras to be finalized }
  755. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  756. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  757. {$if defined(x86) or defined(arm)}
  758. { set implicit_finally flag for if procedure is safecall }
  759. if (target_info.system in systems_all_windows) and
  760. (procdef.proccalloption=pocall_safecall) then
  761. include(flags, pi_needs_implicit_finally);
  762. {$endif}
  763. { firstpass everything }
  764. flowcontrol:=[];
  765. do_firstpass(code);
  766. {$ifdef i386}
  767. procdef.fpu_used:=node_resources_fpu(code);
  768. if procdef.fpu_used>0 then
  769. include(flags,pi_uses_fpu);
  770. {$endif i386}
  771. { Print the node to tree.log }
  772. if paraprintnodetree=1 then
  773. printproc( 'after the firstpass');
  774. { do this before adding the entry code else the tail recursion recognition won't work,
  775. if this causes troubles, it must be if'ed
  776. }
  777. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  778. (pi_is_recursive in flags) then
  779. do_opttail(code,procdef);
  780. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  781. { creating dfa is not always possible }
  782. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler,
  783. pi_needs_implicit_finally,pi_has_implicit_finally])=[]) then
  784. begin
  785. dfabuilder:=TDFABuilder.Create;
  786. dfabuilder.createdfainfo(code);
  787. { when life info is available, we can give more sophisticated warning about unintialized
  788. variables }
  789. { iterate through life info of the first node }
  790. for i:=0 to dfabuilder.nodemap.count-1 do
  791. begin
  792. if DFASetIn(code.optinfo^.life,i) then
  793. case tnode(dfabuilder.nodemap[i]).nodetype of
  794. loadn:
  795. begin
  796. varsym:=tabstractnormalvarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry);
  797. { Give warning/note for living locals }
  798. if assigned(varsym.owner) and
  799. not(vo_is_external in varsym.varoptions) then
  800. begin
  801. if (vo_is_funcret in varsym.varoptions) then
  802. CGMessage(sym_w_function_result_uninitialized)
  803. else
  804. begin
  805. if (varsym.owner=procdef.localst) and not (vo_is_typed_const in varsym.varoptions) then
  806. CGMessage1(sym_w_uninitialized_local_variable,varsym.realname);
  807. end;
  808. end;
  809. end;
  810. end;
  811. end;
  812. include(flags,pi_dfaavailable);
  813. end;
  814. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  815. { our induction variable strength reduction doesn't like
  816. for loops with more than one entry }
  817. and not(pi_has_goto in current_procinfo.flags) then
  818. begin
  819. {RedoDFA:=}OptimizeInductionVariables(code);
  820. end;
  821. if cs_opt_nodecse in current_settings.optimizerswitches then
  822. do_optcse(code);
  823. { add implicit entry and exit code }
  824. add_entry_exit_code;
  825. { only do secondpass if there are no errors }
  826. if (ErrorCount=0) then
  827. begin
  828. create_codegen;
  829. { set the start offset to the start of the temp area in the stack }
  830. tg:=ttgobj.create;
  831. {$if defined(x86) or defined(arm)}
  832. { try to strip the stack frame }
  833. { set the framepointer to esp if:
  834. - no assembler directive, those are handled in assembler_block
  835. in pstatment.pas (for cases not caught by the Delphi
  836. exception below)
  837. - no exceptions are used
  838. - no pushes are used/esp modifications, could be:
  839. * outgoing parameters on the stack
  840. * incoming parameters on the stack
  841. * open arrays
  842. - no inline assembler
  843. or
  844. - Delphi mode
  845. - assembler directive
  846. - no pushes are used/esp modifications, could be:
  847. * outgoing parameters on the stack
  848. * incoming parameters on the stack
  849. * open arrays
  850. - no local variables
  851. }
  852. if ((po_assembler in procdef.procoptions) and
  853. (m_delphi in current_settings.modeswitches) and
  854. { localst at main_program_level is a staticsymtable }
  855. (procdef.localst.symtablelevel<>main_program_level) and
  856. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  857. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  858. not(cs_generate_stackframes in current_settings.localswitches) and
  859. not(po_assembler in procdef.procoptions) and
  860. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler,
  861. pi_needs_implicit_finally,pi_has_implicit_finally,pi_has_stackparameter,
  862. pi_needs_stackframe])=[])
  863. )
  864. then
  865. begin
  866. { we need the parameter info here to determine if the procedure gets
  867. parameters on the stack
  868. calling generate_parameter_info doesn't hurt but it costs time
  869. }
  870. generate_parameter_info;
  871. if not(stack_tainting_parameter) and
  872. not(has_assembler_child) and (para_stack_size=0) then
  873. begin
  874. { Only need to set the framepointer }
  875. framepointer:=NR_STACK_POINTER_REG;
  876. tg.direction:=1;
  877. end;
  878. end;
  879. {$endif}
  880. { Create register allocator }
  881. cg.init_register_allocators;
  882. set_first_temp_offset;
  883. generate_parameter_info;
  884. { allocate got register if needed }
  885. current_procinfo.allocate_got_register(aktproccode);
  886. { Allocate space in temp/registers for parast and localst }
  887. current_filepos:=entrypos;
  888. gen_alloc_symtable(aktproccode,procdef.parast);
  889. gen_alloc_symtable(aktproccode,procdef.localst);
  890. { Store temp offset for information about 'real' temps }
  891. tempstart:=tg.lasttemp;
  892. { Generate code to load register parameters in temps and insert local
  893. copies for values parameters. This must be done before the code for the
  894. body is generated because the localloc is updated.
  895. Note: The generated code will be inserted after the code generation of
  896. the body is finished, because only then the position is known }
  897. {$ifdef oldregvars}
  898. assign_regvars(code);
  899. {$endif oldreg}
  900. current_filepos:=entrypos;
  901. { record which registers are allocated here, since all code }
  902. { allocating registers comes after it }
  903. cg.set_regalloc_live_range_direction(rad_backwards);
  904. gen_load_para_value(templist);
  905. cg.set_regalloc_live_range_direction(rad_forward);
  906. { caller paraloc info is also necessary in the stackframe_entry
  907. code of the ppc (and possibly other processors) }
  908. if not procdef.has_paraloc_info then
  909. begin
  910. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  911. procdef.has_paraloc_info:=true;
  912. end;
  913. { generate code for the node tree }
  914. do_secondpass(code);
  915. aktproccode.concatlist(current_asmdata.CurrAsmList);
  916. { The position of the loadpara_asmnode is now known }
  917. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  918. { first generate entry and initialize code with the correct
  919. position and switches }
  920. current_filepos:=entrypos;
  921. current_settings.localswitches:=entryswitches;
  922. cg.set_regalloc_live_range_direction(rad_backwards);
  923. gen_entry_code(templist);
  924. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  925. gen_initialize_code(templist);
  926. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  927. { now generate finalize and exit code with the correct position
  928. and switches }
  929. current_filepos:=exitpos;
  930. current_settings.localswitches:=exitswitches;
  931. cg.set_regalloc_live_range_direction(rad_forward);
  932. gen_finalize_code(templist);
  933. { the finalcode must be concated if there was no position available,
  934. using insertlistafter will result in an insert at the start
  935. when currentai=nil }
  936. if assigned(final_asmnode.currenttai) then
  937. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  938. else
  939. aktproccode.concatlist(templist);
  940. { insert exit label at the correct position }
  941. cg.a_label(templist,CurrExitLabel);
  942. if assigned(exitlabel_asmnode.currenttai) then
  943. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  944. else
  945. aktproccode.concatlist(templist);
  946. { exit code }
  947. gen_exit_code(templist);
  948. aktproccode.concatlist(templist);
  949. {$ifdef OLDREGVARS}
  950. { note: this must be done only after as much code as possible has }
  951. { been generated. The result is that when you ungetregister() a }
  952. { regvar, it will actually free the regvar (and alse free the }
  953. { the regvars at the same time). Doing this too early will }
  954. { confuse the register allocator, as the regvars will still be }
  955. { used. It should be done before loading the result regs (so }
  956. { they don't conflict with the regvars) and before }
  957. { gen_entry_code (that one has to be able to allocate the }
  958. { regvars again) (JM) }
  959. free_regvars(aktproccode);
  960. {$endif OLDREGVARS}
  961. { generate symbol and save end of header position }
  962. current_filepos:=entrypos;
  963. gen_proc_symbol(templist);
  964. headertai:=tai(templist.last);
  965. { insert symbol }
  966. aktproccode.insertlist(templist);
  967. { Free space in temp/registers for parast and localst, must be
  968. done after gen_entry_code }
  969. current_filepos:=exitpos;
  970. { make sure the got/pic register doesn't get freed in the }
  971. { middle of a loop }
  972. if (cs_create_pic in current_settings.moduleswitches) and
  973. (pi_needs_got in current_procinfo.flags) and
  974. (current_procinfo.got<>NR_NO) then
  975. cg.a_reg_sync(aktproccode,current_procinfo.got);
  976. gen_free_symtable(aktproccode,procdef.localst);
  977. gen_free_symtable(aktproccode,procdef.parast);
  978. { add code that will load the return value, this is not done
  979. for assembler routines when they didn't reference the result
  980. variable }
  981. gen_load_return_value(templist);
  982. aktproccode.concatlist(templist);
  983. { Already reserve all registers for stack checking code and
  984. generate the call to the helper function }
  985. if not(tf_no_generic_stackcheck in target_info.flags) and
  986. (cs_check_stack in entryswitches) and
  987. not(po_assembler in procdef.procoptions) and
  988. (procdef.proctypeoption<>potype_proginit) then
  989. begin
  990. current_filepos:=entrypos;
  991. gen_stack_check_call(templist);
  992. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  993. end;
  994. { this code (got loading) comes before everything which has }
  995. { already been generated, so reset the info about already }
  996. { backwards extended registers (so their live range can be }
  997. { extended backwards even further if needed) }
  998. { This code must be }
  999. { a) generated after do_secondpass has been called }
  1000. { (because pi_needs_got may be set there) }
  1001. { b) generated before register allocation, because the }
  1002. { got/pic register can be a virtual one }
  1003. { c) inserted before the entry code, because the entry }
  1004. { code may need global symbols such as init rtti }
  1005. { d) inserted after the stackframe allocation, because }
  1006. { this register may have to be spilled }
  1007. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  1008. current_filepos:=entrypos;
  1009. { load got if necessary }
  1010. cg.g_maybe_got_init(templist);
  1011. aktproccode.insertlistafter(headertai,templist);
  1012. cg.set_regalloc_live_range_direction(rad_forward);
  1013. { The procedure body is finished, we can now
  1014. allocate the registers }
  1015. cg.do_register_allocation(aktproccode,headertai);
  1016. { translate imag. register to their real counter parts
  1017. this is necessary for debuginfo and verbose assembler output
  1018. when SSA will be implented, this will be more complicated because we've to
  1019. maintain location lists }
  1020. current_procinfo.procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  1021. current_procinfo.procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  1022. if (cs_create_pic in current_settings.moduleswitches) and
  1023. (pi_needs_got in current_procinfo.flags) and
  1024. not(cs_no_regalloc in current_settings.globalswitches) and
  1025. (current_procinfo.got<>NR_NO) then
  1026. cg.translate_register(current_procinfo.got);
  1027. { Add save and restore of used registers }
  1028. current_filepos:=entrypos;
  1029. gen_save_used_regs(templist);
  1030. aktproccode.insertlistafter(headertai,templist);
  1031. current_filepos:=exitpos;
  1032. gen_restore_used_regs(aktproccode);
  1033. { We know the size of the stack, now we can generate the
  1034. parameter that is passed to the stack checking code }
  1035. if not(tf_no_generic_stackcheck in target_info.flags) and
  1036. (cs_check_stack in entryswitches) and
  1037. not(po_assembler in procdef.procoptions) and
  1038. (procdef.proctypeoption<>potype_proginit) then
  1039. begin
  1040. current_filepos:=entrypos;
  1041. gen_stack_check_size_para(templist);
  1042. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1043. end;
  1044. { Add entry code (stack allocation) after header }
  1045. current_filepos:=entrypos;
  1046. gen_proc_entry_code(templist);
  1047. aktproccode.insertlistafter(headertai,templist);
  1048. {$if defined(x86) or defined(arm)}
  1049. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  1050. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  1051. (target_info.system in systems_all_windows) and
  1052. (procdef.proccalloption=pocall_safecall) then
  1053. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  1054. {$endif}
  1055. { Add exit code at the end }
  1056. current_filepos:=exitpos;
  1057. gen_proc_exit_code(templist);
  1058. aktproccode.concatlist(templist);
  1059. { check if the implicit finally has been generated. The flag
  1060. should already be set in pass1 }
  1061. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  1062. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  1063. (pi_needs_implicit_finally in flags) and
  1064. not(pi_has_implicit_finally in flags) then
  1065. internalerror(200405231);
  1066. {$ifndef NoOpt}
  1067. if not(cs_no_regalloc in current_settings.globalswitches) then
  1068. begin
  1069. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1070. { do not optimize pure assembler procedures }
  1071. not(pi_is_assembler in flags) then
  1072. optimize(aktproccode);
  1073. end;
  1074. {$endif NoOpt}
  1075. {$ifdef ARM}
  1076. { because of the limited constant size of the arm, all data access is done pc relative }
  1077. insertpcrelativedata(aktproccode,aktlocaldata);
  1078. {$endif ARM}
  1079. { Add end symbol and debug info }
  1080. { this must be done after the pcrelativedata is appended else the distance calculation of
  1081. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  1082. so it should be inserted before the end symbol (FK)
  1083. }
  1084. current_filepos:=exitpos;
  1085. gen_proc_symbol_end(templist);
  1086. aktproccode.concatlist(templist);
  1087. {$if defined(POWERPC) or defined(POWERPC64)}
  1088. fixup_jmps(aktproccode);
  1089. {$endif}
  1090. { insert line debuginfo }
  1091. if (cs_debuginfo in current_settings.moduleswitches) or
  1092. (cs_use_lineinfo in current_settings.globalswitches) then
  1093. current_debuginfo.insertlineinfo(aktproccode);
  1094. { add the procedure to the al_procedures }
  1095. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  1096. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(procdef.mangledname),getprocalign);
  1097. current_asmdata.asmlists[al_procedures].concatlist(aktproccode);
  1098. { save local data (casetable) also in the same file }
  1099. if assigned(aktlocaldata) and
  1100. (not aktlocaldata.empty) then
  1101. current_asmdata.asmlists[al_procedures].concatlist(aktlocaldata);
  1102. { only now we can remove the temps }
  1103. tg.resettempgen;
  1104. { stop tempgen and ra }
  1105. tg.free;
  1106. cg.done_register_allocators;
  1107. destroy_codegen;
  1108. tg:=nil;
  1109. end;
  1110. dfabuilder.free;
  1111. { restore symtablestack }
  1112. remove_from_symtablestack;
  1113. { restore }
  1114. templist.free;
  1115. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1116. current_filepos:=oldfilepos;
  1117. current_objectdef:=old_current_objectdef;
  1118. current_procinfo:=old_current_procinfo;
  1119. end;
  1120. procedure tcgprocinfo.add_to_symtablestack;
  1121. var
  1122. _class,hp : tobjectdef;
  1123. begin
  1124. { insert symtables for the class, but only if it is no nested function }
  1125. if assigned(procdef._class) and
  1126. not(assigned(parent) and
  1127. assigned(parent.procdef) and
  1128. assigned(parent.procdef._class)) then
  1129. begin
  1130. { insert them in the reverse order }
  1131. hp:=nil;
  1132. repeat
  1133. _class:=procdef._class;
  1134. while _class.childof<>hp do
  1135. _class:=_class.childof;
  1136. hp:=_class;
  1137. symtablestack.push(_class.symtable);
  1138. until hp=procdef._class;
  1139. end;
  1140. { insert parasymtable in symtablestack when parsing
  1141. a function }
  1142. if procdef.parast.symtablelevel>=normal_function_level then
  1143. symtablestack.push(procdef.parast);
  1144. { insert localsymtable, except for the main procedure
  1145. (in that case the localst is the unit's static symtable,
  1146. which is already on the stack) }
  1147. if procdef.localst.symtablelevel>=normal_function_level then
  1148. symtablestack.push(procdef.localst);
  1149. end;
  1150. procedure tcgprocinfo.remove_from_symtablestack;
  1151. var
  1152. _class : tobjectdef;
  1153. begin
  1154. { remove localsymtable }
  1155. if procdef.localst.symtablelevel>=normal_function_level then
  1156. symtablestack.pop(procdef.localst);
  1157. { remove parasymtable }
  1158. if procdef.parast.symtablelevel>=normal_function_level then
  1159. symtablestack.pop(procdef.parast);
  1160. { remove symtables for the class, but only if it is no nested function }
  1161. if assigned(procdef._class) and
  1162. not(assigned(parent) and
  1163. assigned(parent.procdef) and
  1164. assigned(parent.procdef._class)) then
  1165. begin
  1166. _class:=procdef._class;
  1167. while assigned(_class) do
  1168. begin
  1169. symtablestack.pop(_class.symtable);
  1170. _class:=_class.childof;
  1171. end;
  1172. end;
  1173. end;
  1174. procedure tcgprocinfo.resetprocdef;
  1175. begin
  1176. { remove code tree, if not inline procedure }
  1177. if assigned(code) then
  1178. begin
  1179. { the inline procedure has already got a copy of the tree
  1180. stored in procdef.inlininginfo }
  1181. code.free;
  1182. code:=nil;
  1183. end;
  1184. end;
  1185. function checknodeinlining(procdef: tprocdef): boolean;
  1186. var
  1187. i : integer;
  1188. currpara : tparavarsym;
  1189. begin
  1190. result := false;
  1191. if (pi_has_assembler_block in current_procinfo.flags) then
  1192. begin
  1193. Message1(parser_w_not_supported_for_inline,'assembler');
  1194. Message(parser_w_inlining_disabled);
  1195. exit;
  1196. end;
  1197. for i:=0 to procdef.paras.count-1 do
  1198. begin
  1199. currpara:=tparavarsym(procdef.paras[i]);
  1200. case currpara.vardef.typ of
  1201. formaldef :
  1202. begin
  1203. if (currpara.varspez in [vs_out,vs_var,vs_const]) then
  1204. begin
  1205. Message1(parser_w_not_supported_for_inline,'formal parameter');
  1206. Message(parser_w_inlining_disabled);
  1207. exit;
  1208. end;
  1209. end;
  1210. arraydef :
  1211. begin
  1212. if is_array_of_const(currpara.vardef) or
  1213. is_variant_array(currpara.vardef) then
  1214. begin
  1215. Message1(parser_w_not_supported_for_inline,'array of const');
  1216. Message(parser_w_inlining_disabled);
  1217. exit;
  1218. end;
  1219. { open arrays might need re-basing of the index, i.e. if you pass
  1220. an array[1..10] as open array, you have to add 1 to all index operations
  1221. if you directly inline it }
  1222. if is_open_array(currpara.vardef) then
  1223. begin
  1224. Message1(parser_w_not_supported_for_inline,'open array');
  1225. Message(parser_w_inlining_disabled);
  1226. exit;
  1227. end;
  1228. end;
  1229. end;
  1230. end;
  1231. result:=true;
  1232. end;
  1233. procedure tcgprocinfo.parse_body;
  1234. var
  1235. old_current_procinfo : tprocinfo;
  1236. old_block_type : tblock_type;
  1237. st : TSymtable;
  1238. old_current_objectdef : tobjectdef;
  1239. begin
  1240. old_current_procinfo:=current_procinfo;
  1241. old_block_type:=block_type;
  1242. old_current_objectdef:=current_objectdef;
  1243. current_procinfo:=self;
  1244. current_objectdef:=procdef._class;
  1245. { calculate the lexical level }
  1246. if procdef.parast.symtablelevel>maxnesting then
  1247. Message(parser_e_too_much_lexlevel);
  1248. block_type:=bt_body;
  1249. {$ifdef state_tracking}
  1250. { aktstate:=Tstate_storage.create;}
  1251. {$endif state_tracking}
  1252. { allocate the symbol for this procedure }
  1253. alloc_proc_symbol(procdef);
  1254. { add parast/localst to symtablestack }
  1255. add_to_symtablestack;
  1256. { save entry info }
  1257. entrypos:=current_filepos;
  1258. entryswitches:=current_settings.localswitches;
  1259. if (df_generic in procdef.defoptions) then
  1260. begin
  1261. { start token recorder for generic template }
  1262. procdef.initgeneric;
  1263. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1264. end;
  1265. { parse the code ... }
  1266. code:=block(current_module.islibrary);
  1267. if (df_generic in procdef.defoptions) then
  1268. begin
  1269. { stop token recorder for generic template }
  1270. current_scanner.stoprecordtokens;
  1271. { Give an error for accesses in the static symtable that aren't visible
  1272. outside the current unit }
  1273. st:=procdef.owner;
  1274. while (st.symtabletype=ObjectSymtable) do
  1275. st:=st.defowner.owner;
  1276. if (pi_uses_static_symtable in flags) and
  1277. (st.symtabletype<>staticsymtable) then
  1278. Comment(V_Error,'Global Generic template references static symtable');
  1279. end;
  1280. { save exit info }
  1281. exitswitches:=current_settings.localswitches;
  1282. exitpos:=last_endtoken_filepos;
  1283. { the procedure is now defined }
  1284. procdef.forwarddef:=false;
  1285. if assigned(code) then
  1286. begin
  1287. { get a better entry point }
  1288. entrypos:=code.fileinfo;
  1289. { Finish type checking pass }
  1290. do_typecheckpass(code);
  1291. end;
  1292. { Check for unused labels, forwards, symbols for procedures. Static
  1293. symtable is checked in pmodules.
  1294. The check must be done after the typecheckpass }
  1295. if (Errorcount=0) and
  1296. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1297. begin
  1298. { check if forwards are resolved }
  1299. tstoredsymtable(procdef.localst).check_forwards;
  1300. { check if all labels are used }
  1301. tstoredsymtable(procdef.localst).checklabels;
  1302. { check for unused symbols, but only if there is no asm block }
  1303. if not(pi_has_assembler_block in flags) then
  1304. begin
  1305. tstoredsymtable(procdef.localst).allsymbolsused;
  1306. tstoredsymtable(procdef.parast).allsymbolsused;
  1307. end;
  1308. end;
  1309. if (po_inline in procdef.procoptions) then
  1310. begin
  1311. { Can we inline this procedure? }
  1312. if checknodeinlining(procdef) then
  1313. begin
  1314. new(procdef.inlininginfo);
  1315. include(procdef.procoptions,po_has_inlininginfo);
  1316. procdef.inlininginfo^.code:=code.getcopy;
  1317. procdef.inlininginfo^.flags:=current_procinfo.flags;
  1318. { The blocknode needs to set an exit label }
  1319. if procdef.inlininginfo^.code.nodetype=blockn then
  1320. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1321. end;
  1322. end;
  1323. { Print the node to tree.log }
  1324. if paraprintnodetree=1 then
  1325. printproc( 'after parsing');
  1326. { ... remove symbol tables }
  1327. remove_from_symtablestack;
  1328. {$ifdef state_tracking}
  1329. { aktstate.destroy;}
  1330. {$endif state_tracking}
  1331. current_objectdef:=old_current_objectdef;
  1332. current_procinfo:=old_current_procinfo;
  1333. { Restore old state }
  1334. block_type:=old_block_type;
  1335. end;
  1336. {****************************************************************************
  1337. PROCEDURE/FUNCTION PARSING
  1338. ****************************************************************************}
  1339. procedure check_init_paras(p:TObject;arg:pointer);
  1340. begin
  1341. if tsym(p).typ<>paravarsym then
  1342. exit;
  1343. with tparavarsym(p) do
  1344. if (not is_class(vardef) and
  1345. vardef.needs_inittable and
  1346. (varspez in [vs_value,vs_out])) then
  1347. include(current_procinfo.flags,pi_do_call);
  1348. end;
  1349. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1350. {
  1351. Parses the procedure directives, then parses the procedure body, then
  1352. generates the code for it
  1353. }
  1354. procedure do_generate_code(pi:tcgprocinfo);
  1355. var
  1356. hpi : tcgprocinfo;
  1357. begin
  1358. { generate code for this procedure }
  1359. pi.generate_code;
  1360. { process nested procs }
  1361. hpi:=tcgprocinfo(pi.nestedprocs.first);
  1362. while assigned(hpi) do
  1363. begin
  1364. do_generate_code(hpi);
  1365. hpi:=tcgprocinfo(hpi.next);
  1366. end;
  1367. pi.resetprocdef;
  1368. end;
  1369. var
  1370. oldfailtokenmode : tmodeswitch;
  1371. isnestedproc : boolean;
  1372. begin
  1373. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1374. { create a new procedure }
  1375. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1376. current_module.procinfo:=current_procinfo;
  1377. current_procinfo.procdef:=pd;
  1378. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1379. { Insert mangledname }
  1380. pd.aliasnames.insert(pd.mangledname);
  1381. { Handle Export of this procedure }
  1382. if (po_exports in pd.procoptions) and
  1383. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1384. begin
  1385. pd.aliasnames.insert(pd.procsym.realname);
  1386. if cs_link_deffile in current_settings.globalswitches then
  1387. deffile.AddExport(pd.mangledname);
  1388. end;
  1389. { Insert result variables in the localst }
  1390. insert_funcret_local(pd);
  1391. { check if there are para's which require initing -> set }
  1392. { pi_do_call (if not yet set) }
  1393. if not(pi_do_call in current_procinfo.flags) then
  1394. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1395. { set _FAIL as keyword if constructor }
  1396. if (pd.proctypeoption=potype_constructor) then
  1397. begin
  1398. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1399. tokeninfo^[_FAIL].keyword:=m_all;
  1400. end;
  1401. tcgprocinfo(current_procinfo).parse_body;
  1402. { We can't support inlining for procedures that have nested
  1403. procedures because the nested procedures use a fixed offset
  1404. for accessing locals in the parent procedure (PFV) }
  1405. if (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1406. begin
  1407. if (df_generic in current_procinfo.procdef.defoptions) then
  1408. Comment(V_Error,'Generic methods cannot have nested procedures')
  1409. else
  1410. if (po_inline in current_procinfo.procdef.procoptions) then
  1411. begin
  1412. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1413. Message(parser_w_inlining_disabled);
  1414. exclude(current_procinfo.procdef.procoptions,po_inline);
  1415. end;
  1416. end;
  1417. { When it's a nested procedure then defer the code generation,
  1418. when back at normal function level then generate the code
  1419. for all defered nested procedures and the current procedure }
  1420. if isnestedproc then
  1421. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1422. else
  1423. begin
  1424. if not(df_generic in current_procinfo.procdef.defoptions) then
  1425. do_generate_code(tcgprocinfo(current_procinfo));
  1426. end;
  1427. { reset _FAIL as _SELF normal }
  1428. if (pd.proctypeoption=potype_constructor) then
  1429. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1430. { release procinfo }
  1431. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1432. internalerror(200304274);
  1433. current_module.procinfo:=current_procinfo.parent;
  1434. { For specialization we didn't record the last semicolon. Moving this parsing
  1435. into the parse_body routine is not done because of having better file position
  1436. information available }
  1437. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1438. consume(_SEMICOLON);
  1439. if not isnestedproc then
  1440. { current_procinfo is checked for nil later on }
  1441. freeandnil(current_procinfo);
  1442. end;
  1443. procedure read_proc(isclassmethod:boolean);
  1444. {
  1445. Parses the procedure directives, then parses the procedure body, then
  1446. generates the code for it
  1447. }
  1448. var
  1449. old_current_procinfo : tprocinfo;
  1450. old_current_objectdef : tobjectdef;
  1451. pdflags : tpdflags;
  1452. pd,firstpd : tprocdef;
  1453. s : string;
  1454. begin
  1455. { save old state }
  1456. old_current_procinfo:=current_procinfo;
  1457. old_current_objectdef:=current_objectdef;
  1458. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1459. to an other procdef }
  1460. current_procinfo:=nil;
  1461. current_objectdef:=nil;
  1462. { parse procedure declaration }
  1463. pd:=parse_proc_dec(isclassmethod, old_current_objectdef);
  1464. { set the default function options }
  1465. if parse_only then
  1466. begin
  1467. pd.forwarddef:=true;
  1468. { set also the interface flag, for better error message when the
  1469. implementation doesn't much this header }
  1470. pd.interfacedef:=true;
  1471. include(pd.procoptions,po_global);
  1472. pdflags:=[pd_interface];
  1473. end
  1474. else
  1475. begin
  1476. pdflags:=[pd_body];
  1477. if (not current_module.in_interface) then
  1478. include(pdflags,pd_implemen);
  1479. if (not current_module.is_unit) or
  1480. create_smartlink or
  1481. {
  1482. taking addresses of static procedures goes wrong
  1483. if they aren't global when pic is used (FK)
  1484. }
  1485. (cs_create_pic in current_settings.moduleswitches) then
  1486. include(pd.procoptions,po_global);
  1487. pd.forwarddef:=false;
  1488. end;
  1489. { parse the directives that may follow }
  1490. parse_proc_directives(pd,pdflags);
  1491. { hint directives, these can be separated by semicolons here,
  1492. that needs to be handled here with a loop (PFV) }
  1493. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1494. Consume(_SEMICOLON);
  1495. { Set calling convention }
  1496. handle_calling_convention(pd);
  1497. { search for forward declarations }
  1498. if not proc_add_definition(pd) then
  1499. begin
  1500. { A method must be forward defined (in the object declaration) }
  1501. if assigned(pd._class) and
  1502. (not assigned(old_current_objectdef)) then
  1503. begin
  1504. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1505. tprocsym(pd.procsym).write_parameter_lists(pd);
  1506. end
  1507. else
  1508. begin
  1509. { Give a better error if there is a forward def in the interface and only
  1510. a single implementation }
  1511. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1512. if (not pd.forwarddef) and
  1513. (not pd.interfacedef) and
  1514. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1515. firstpd.forwarddef and
  1516. firstpd.interfacedef and
  1517. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1518. { don't give an error if it may be an overload }
  1519. not(m_fpc in current_settings.modeswitches) and
  1520. (not(po_overload in pd.procoptions) or
  1521. not(po_overload in firstpd.procoptions)) then
  1522. begin
  1523. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1524. tprocsym(pd.procsym).write_parameter_lists(pd);
  1525. end;
  1526. end;
  1527. end;
  1528. { Set mangled name }
  1529. proc_set_mangledname(pd);
  1530. { compile procedure when a body is needed }
  1531. if (pd_body in pdflags) then
  1532. begin
  1533. read_proc_body(old_current_procinfo,pd);
  1534. end
  1535. else
  1536. begin
  1537. { Handle imports }
  1538. if (po_external in pd.procoptions) then
  1539. begin
  1540. { External declared in implementation, and there was already a
  1541. forward (or interface) declaration then we need to generate
  1542. a stub that calls the external routine }
  1543. if (not pd.forwarddef) and
  1544. (pd.hasforward) and
  1545. not(
  1546. assigned(pd.import_dll) and
  1547. (target_info.system in [system_i386_wdosx,
  1548. system_arm_wince,system_i386_wince])
  1549. ) then
  1550. begin
  1551. s:=proc_get_importname(pd);
  1552. if s<>'' then
  1553. gen_external_stub(current_asmdata.asmlists[al_procedures],pd,s);
  1554. end;
  1555. { Import DLL specified? }
  1556. if assigned(pd.import_dll) then
  1557. current_module.AddExternalImport(pd.import_dll^,proc_get_importname(pd),pd.import_nr,false,pd.import_name=nil)
  1558. else
  1559. begin
  1560. { add import name to external list for DLL scanning }
  1561. if tf_has_dllscanner in target_info.flags then
  1562. current_module.dllscannerinputlist.Add(proc_get_importname(pd),pd);
  1563. end;
  1564. end;
  1565. end;
  1566. { make sure that references to forward-declared functions are not }
  1567. { treated as references to external symbols, needed for darwin. }
  1568. { make sure we don't change the binding of real external symbols }
  1569. if not(po_external in pd.procoptions) then
  1570. begin
  1571. if (po_global in pd.procoptions) or
  1572. (cs_profile in current_settings.moduleswitches) then
  1573. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION)
  1574. else
  1575. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  1576. end;
  1577. current_objectdef:=old_current_objectdef;
  1578. current_procinfo:=old_current_procinfo;
  1579. end;
  1580. {****************************************************************************
  1581. DECLARATION PARSING
  1582. ****************************************************************************}
  1583. { search in symtablestack for not complete classes }
  1584. procedure check_forward_class(p:TObject;arg:pointer);
  1585. begin
  1586. if (tsym(p).typ=typesym) and
  1587. (ttypesym(p).typedef.typ=objectdef) and
  1588. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  1589. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1590. end;
  1591. procedure read_declarations(islibrary : boolean);
  1592. var
  1593. is_classdef:boolean;
  1594. begin
  1595. is_classdef:=false;
  1596. repeat
  1597. if not assigned(current_procinfo) then
  1598. internalerror(200304251);
  1599. case token of
  1600. _LABEL:
  1601. label_dec;
  1602. _CONST:
  1603. const_dec;
  1604. _TYPE:
  1605. type_dec;
  1606. _VAR:
  1607. var_dec;
  1608. _THREADVAR:
  1609. threadvar_dec;
  1610. _CLASS:
  1611. begin
  1612. is_classdef:=false;
  1613. if try_to_consume(_CLASS) then
  1614. begin
  1615. { class modifier is only allowed for procedures, functions, }
  1616. { constructors, destructors, fields and properties }
  1617. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  1618. Message(parser_e_procedure_or_function_expected);
  1619. if is_interface(current_objectdef) then
  1620. Message(parser_e_no_static_method_in_interfaces)
  1621. else
  1622. { class methods are also allowed for Objective-C protocols }
  1623. is_classdef:=true;
  1624. end;
  1625. end;
  1626. _CONSTRUCTOR,
  1627. _DESTRUCTOR,
  1628. _FUNCTION,
  1629. _PROCEDURE,
  1630. _OPERATOR:
  1631. begin
  1632. read_proc(is_classdef);
  1633. is_classdef:=false;
  1634. end;
  1635. _EXPORTS:
  1636. begin
  1637. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  1638. begin
  1639. Message(parser_e_syntax_error);
  1640. consume_all_until(_SEMICOLON);
  1641. end
  1642. else if islibrary or
  1643. (target_info.system in systems_unit_program_exports) then
  1644. read_exports
  1645. else
  1646. begin
  1647. Message(parser_w_unsupported_feature);
  1648. consume(_BEGIN);
  1649. end;
  1650. end
  1651. else
  1652. begin
  1653. case idtoken of
  1654. _RESOURCESTRING :
  1655. begin
  1656. { m_class is needed, because the resourcestring
  1657. loading is in the ObjPas unit }
  1658. { if (m_class in current_settings.modeswitches) then}
  1659. resourcestring_dec
  1660. { else
  1661. break;}
  1662. end;
  1663. _PROPERTY:
  1664. begin
  1665. if (m_fpc in current_settings.modeswitches) then
  1666. begin
  1667. property_dec(is_classdef);
  1668. is_classdef:=false;
  1669. end
  1670. else
  1671. break;
  1672. end;
  1673. else
  1674. break;
  1675. end;
  1676. end;
  1677. end;
  1678. until false;
  1679. { check for incomplete class definitions, this is only required
  1680. for fpc modes }
  1681. if (m_fpc in current_settings.modeswitches) then
  1682. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  1683. end;
  1684. procedure read_interface_declarations;
  1685. begin
  1686. repeat
  1687. case token of
  1688. _CONST :
  1689. const_dec;
  1690. _TYPE :
  1691. type_dec;
  1692. _VAR :
  1693. var_dec;
  1694. _THREADVAR :
  1695. threadvar_dec;
  1696. _FUNCTION,
  1697. _PROCEDURE,
  1698. _OPERATOR :
  1699. read_proc(false);
  1700. else
  1701. begin
  1702. case idtoken of
  1703. _RESOURCESTRING :
  1704. resourcestring_dec;
  1705. _PROPERTY:
  1706. begin
  1707. if (m_fpc in current_settings.modeswitches) then
  1708. property_dec(false)
  1709. else
  1710. break;
  1711. end;
  1712. else
  1713. break;
  1714. end;
  1715. end;
  1716. end;
  1717. until false;
  1718. { check for incomplete class definitions, this is only required
  1719. for fpc modes }
  1720. if (m_fpc in current_settings.modeswitches) then
  1721. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  1722. end;
  1723. {****************************************************************************
  1724. SPECIALIZATION BODY GENERATION
  1725. ****************************************************************************}
  1726. procedure specialize_objectdefs(p:TObject;arg:pointer);
  1727. var
  1728. i : longint;
  1729. hp : tdef;
  1730. oldcurrent_filepos : tfileposinfo;
  1731. oldsymtablestack : tsymtablestack;
  1732. pu : tused_unit;
  1733. hmodule : tmodule;
  1734. specobj : tobjectdef;
  1735. begin
  1736. if not((tsym(p).typ=typesym) and
  1737. (ttypesym(p).typedef.typesym=tsym(p)) and
  1738. (ttypesym(p).typedef.typ=objectdef) and
  1739. (df_specialization in ttypesym(p).typedef.defoptions)
  1740. ) then
  1741. exit;
  1742. { Setup symtablestack a definition time }
  1743. specobj:=tobjectdef(ttypesym(p).typedef);
  1744. oldsymtablestack:=symtablestack;
  1745. symtablestack:=tsymtablestack.create;
  1746. if not assigned(tobjectdef(ttypesym(p).typedef).genericdef) then
  1747. internalerror(200705151);
  1748. hmodule:=find_module_from_symtable(specobj.genericdef.owner);
  1749. if hmodule=nil then
  1750. internalerror(200705152);
  1751. pu:=tused_unit(hmodule.used_units.first);
  1752. while assigned(pu) do
  1753. begin
  1754. if not assigned(pu.u.globalsymtable) then
  1755. internalerror(200705153);
  1756. symtablestack.push(pu.u.globalsymtable);
  1757. pu:=tused_unit(pu.next);
  1758. end;
  1759. if assigned(hmodule.globalsymtable) then
  1760. symtablestack.push(hmodule.globalsymtable);
  1761. if assigned(hmodule.localsymtable) then
  1762. symtablestack.push(hmodule.localsymtable);
  1763. { procedure definitions for classes or objects }
  1764. if is_class(specobj) or is_object(specobj) then
  1765. begin
  1766. for i:=0 to specobj.symtable.DefList.Count-1 do
  1767. begin
  1768. hp:=tdef(specobj.symtable.DefList[i]);
  1769. if hp.typ=procdef then
  1770. begin
  1771. if assigned(tprocdef(hp).genericdef) and
  1772. (tprocdef(hp).genericdef.typ=procdef) and
  1773. assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf) then
  1774. begin
  1775. oldcurrent_filepos:=current_filepos;
  1776. current_filepos:=tprocdef(tprocdef(hp).genericdef).fileinfo;
  1777. { use the index the module got from the current compilation process }
  1778. current_filepos.moduleindex:=hmodule.unit_index;
  1779. current_tokenpos:=current_filepos;
  1780. current_scanner.startreplaytokens(tprocdef(tprocdef(hp).genericdef).generictokenbuf);
  1781. read_proc_body(nil,tprocdef(hp));
  1782. current_filepos:=oldcurrent_filepos;
  1783. end
  1784. else
  1785. MessagePos1(tprocdef(hp).fileinfo,sym_e_forward_not_resolved,tprocdef(hp).fullprocname(false));
  1786. end;
  1787. end;
  1788. end;
  1789. { Restore symtablestack }
  1790. symtablestack.free;
  1791. symtablestack:=oldsymtablestack;
  1792. end;
  1793. procedure generate_specialization_procs;
  1794. begin
  1795. if assigned(current_module.globalsymtable) then
  1796. current_module.globalsymtable.SymList.ForEachCall(@specialize_objectdefs,nil);
  1797. if assigned(current_module.localsymtable) then
  1798. current_module.localsymtable.SymList.ForEachCall(@specialize_objectdefs,nil);
  1799. end;
  1800. end.