psub.pas 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465
  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. globals,
  22. node,nbas,
  23. symdef,procinfo,optdfa;
  24. type
  25. { tcgprocinfo }
  26. tcgprocinfo = class(tprocinfo)
  27. private
  28. procedure CreateInlineInfo;
  29. { returns the node which is the start of the user code, this is needed by the dfa }
  30. function GetUserCode: tnode;
  31. procedure maybe_add_constructor_wrapper(var tocode: tnode; withexceptblock: boolean);
  32. procedure add_entry_exit_code;
  33. procedure setup_tempgen;
  34. public
  35. { code for the subroutine as tree }
  36. code : tnode;
  37. { positions in the tree for init/final }
  38. entry_asmnode,
  39. loadpara_asmnode,
  40. exitlabel_asmnode,
  41. stackcheck_asmnode,
  42. init_asmnode,
  43. final_asmnode : tasmnode;
  44. final_used : boolean;
  45. dfabuilder : TDFABuilder;
  46. destructor destroy;override;
  47. procedure printproc(pass:string);
  48. procedure generate_code;
  49. procedure generate_code_tree;
  50. procedure generate_exceptfilter(nestedpi: tcgprocinfo);
  51. procedure resetprocdef;
  52. procedure add_to_symtablestack;
  53. procedure remove_from_symtablestack;
  54. procedure parse_body;
  55. function has_assembler_child : boolean;
  56. end;
  57. procedure printnode_reset;
  58. { reads the declaration blocks }
  59. procedure read_declarations(islibrary : boolean);
  60. { reads declarations in the interface part of a unit }
  61. procedure read_interface_declarations;
  62. { reads any routine in the implementation, or a non-method routine
  63. declaration in the interface (depending on whether or not parse_only is
  64. true) }
  65. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef);
  66. procedure generate_specialization_procs;
  67. implementation
  68. uses
  69. sysutils,
  70. { common }
  71. cutils,
  72. { global }
  73. globtype,tokens,verbose,comphook,constexp,
  74. systems,cpubase,aasmbase,aasmtai,aasmdata,
  75. { symtable }
  76. symconst,symbase,symsym,symtype,symtable,defutil,symcreat,
  77. paramgr,
  78. ppu,fmodule,
  79. { pass 1 }
  80. nutils,ngenutil,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  81. pass_1,
  82. {$ifdef state_tracking}
  83. nstate,
  84. {$endif state_tracking}
  85. { pass 2 }
  86. {$ifndef NOPASS2}
  87. pass_2,
  88. {$endif}
  89. { parser }
  90. scanner,gendef,
  91. pbase,pstatmnt,pdecl,pdecsub,pexports,pgenutil,pparautl,
  92. { codegen }
  93. tgobj,cgbase,cgobj,cgutils,hlcgobj,hlcgcpu,dbgbase,
  94. ncgutil,regvars,
  95. optbase,
  96. opttail,
  97. optcse,
  98. optloop,
  99. optconstprop,
  100. optdeadstore
  101. {$if defined(arm)}
  102. ,cpuinfo
  103. {$endif arm}
  104. {$ifndef NOOPT}
  105. {$ifdef i386}
  106. ,aopt386
  107. {$else i386}
  108. ,aopt
  109. {$endif i386}
  110. {$endif}
  111. ;
  112. function checknodeinlining(procdef: tprocdef): boolean;
  113. var
  114. i : integer;
  115. currpara : tparavarsym;
  116. begin
  117. result := false;
  118. if pi_has_assembler_block in current_procinfo.flags then
  119. begin
  120. Message1(parser_h_not_supported_for_inline,'assembler');
  121. Message(parser_h_inlining_disabled);
  122. exit;
  123. end;
  124. if pi_has_global_goto in current_procinfo.flags then
  125. begin
  126. Message1(parser_h_not_supported_for_inline,'global goto');
  127. Message(parser_h_inlining_disabled);
  128. exit;
  129. end;
  130. if pi_has_nested_exit in current_procinfo.flags then
  131. begin
  132. Message1(parser_h_not_supported_for_inline,'nested exit');
  133. Message(parser_h_inlining_disabled);
  134. exit;
  135. end;
  136. { the compiler cannot handle inherited in inlined subroutines because
  137. it tries to search for self in the symtable, however, the symtable
  138. is not available }
  139. if pi_has_inherited in current_procinfo.flags then
  140. begin
  141. Message1(parser_h_not_supported_for_inline,'inherited');
  142. Message(parser_h_inlining_disabled);
  143. exit;
  144. end;
  145. for i:=0 to procdef.paras.count-1 do
  146. begin
  147. currpara:=tparavarsym(procdef.paras[i]);
  148. case currpara.vardef.typ of
  149. formaldef :
  150. begin
  151. if (currpara.varspez in [vs_out,vs_var,vs_const,vs_constref]) then
  152. begin
  153. Message1(parser_h_not_supported_for_inline,'formal parameter');
  154. Message(parser_h_inlining_disabled);
  155. exit;
  156. end;
  157. end;
  158. arraydef :
  159. begin
  160. if is_array_of_const(currpara.vardef) or
  161. is_variant_array(currpara.vardef) then
  162. begin
  163. Message1(parser_h_not_supported_for_inline,'array of const');
  164. Message(parser_h_inlining_disabled);
  165. exit;
  166. end;
  167. { open arrays might need re-basing of the index, i.e. if you pass
  168. an array[1..10] as open array, you have to add 1 to all index operations
  169. if you directly inline it }
  170. if is_open_array(currpara.vardef) then
  171. begin
  172. Message1(parser_h_not_supported_for_inline,'open array');
  173. Message(parser_h_inlining_disabled);
  174. exit;
  175. end;
  176. end;
  177. end;
  178. end;
  179. result:=true;
  180. end;
  181. {****************************************************************************
  182. PROCEDURE/FUNCTION BODY PARSING
  183. ****************************************************************************}
  184. procedure initializedefaultvars(p:TObject;arg:pointer);
  185. var
  186. b : tblocknode;
  187. begin
  188. if tsym(p).typ<>localvarsym then
  189. exit;
  190. with tabstractnormalvarsym(p) do
  191. begin
  192. if (vo_is_default_var in varoptions) and (vardef.size>0) then
  193. begin
  194. b:=tblocknode(arg);
  195. b.left:=cstatementnode.create(
  196. ccallnode.createintern('fpc_zeromem',
  197. ccallparanode.create(
  198. cordconstnode.create(vardef.size,ptruinttype,false),
  199. ccallparanode.create(
  200. caddrnode.create_internal(
  201. cloadnode.create(tsym(p),tsym(p).owner)),
  202. nil
  203. )
  204. )
  205. ),
  206. b.left);
  207. end;
  208. end;
  209. end;
  210. procedure initializevars(p:TObject;arg:pointer);
  211. var
  212. b : tblocknode;
  213. begin
  214. if not (tsym(p).typ in [localvarsym,staticvarsym]) then
  215. exit;
  216. with tabstractnormalvarsym(p) do
  217. begin
  218. if assigned(defaultconstsym) then
  219. begin
  220. b:=tblocknode(arg);
  221. b.left:=cstatementnode.create(
  222. cassignmentnode.create(
  223. cloadnode.create(tsym(p),tsym(p).owner),
  224. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  225. b.left);
  226. end
  227. else
  228. initializedefaultvars(p,arg);
  229. end;
  230. end;
  231. procedure check_finalize_paras(p:TObject;arg:pointer);
  232. begin
  233. if (tsym(p).typ=paravarsym) then
  234. begin
  235. if tparavarsym(p).needs_finalization then
  236. include(current_procinfo.flags,pi_needs_implicit_finally);
  237. if (tparavarsym(p).varspez in [vs_value,vs_out]) and
  238. (cs_create_pic in current_settings.moduleswitches) and
  239. (tf_pic_uses_got in target_info.flags) and
  240. is_rtti_managed_type(tparavarsym(p).vardef) then
  241. include(current_procinfo.flags,pi_needs_got);
  242. end;
  243. end;
  244. procedure check_finalize_locals(p:TObject;arg:pointer);
  245. begin
  246. { include the result: it needs to be finalized in case an exception }
  247. { occurs }
  248. if (tsym(p).typ=localvarsym) and
  249. (tlocalvarsym(p).refs>0) and
  250. is_managed_type(tlocalvarsym(p).vardef) and
  251. not (vo_is_weakref in tlocalvarsym(p).varoptions) then
  252. begin
  253. include(current_procinfo.flags,pi_needs_implicit_finally);
  254. if is_rtti_managed_type(tlocalvarsym(p).vardef) and
  255. (cs_create_pic in current_settings.moduleswitches) and
  256. (tf_pic_uses_got in target_info.flags) then
  257. include(current_procinfo.flags,pi_needs_got);
  258. end;
  259. end;
  260. function block(islibrary : boolean) : tnode;
  261. var
  262. oldfilepos: tfileposinfo;
  263. begin
  264. { parse const,types and vars }
  265. read_declarations(islibrary);
  266. { do we have an assembler block without the po_assembler?
  267. we should allow this for Delphi compatibility (PFV) }
  268. if (token=_ASM) and (m_delphi in current_settings.modeswitches) then
  269. include(current_procinfo.procdef.procoptions,po_assembler);
  270. { Handle assembler block different }
  271. if (po_assembler in current_procinfo.procdef.procoptions) then
  272. begin
  273. block:=assembler_block;
  274. exit;
  275. end;
  276. {Unit initialization?.}
  277. if (
  278. assigned(current_procinfo.procdef.localst) and
  279. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  280. (current_module.is_unit or islibrary)
  281. ) then
  282. begin
  283. if (token=_END) then
  284. begin
  285. consume(_END);
  286. { We need at least a node, else the entry/exit code is not
  287. generated and thus no PASCALMAIN symbol which we need (PFV) }
  288. if islibrary then
  289. block:=cnothingnode.create
  290. else
  291. block:=nil;
  292. end
  293. else
  294. begin
  295. if token=_INITIALIZATION then
  296. begin
  297. { The library init code is already called and does not
  298. need to be in the initfinal table (PFV) }
  299. block:=statement_block(_INITIALIZATION);
  300. end
  301. else if token=_FINALIZATION then
  302. begin
  303. { when a unit has only a finalization section, we can come to this
  304. point when we try to read the nonh existing initalization section
  305. so we've to check if we are really try to parse the finalization }
  306. if current_procinfo.procdef.proctypeoption=potype_unitfinalize then
  307. block:=statement_block(_FINALIZATION)
  308. else
  309. block:=nil;
  310. end
  311. else
  312. block:=statement_block(_BEGIN);
  313. end;
  314. end
  315. else
  316. begin
  317. { parse routine body }
  318. block:=statement_block(_BEGIN);
  319. { initialized variables }
  320. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  321. begin
  322. { initialization of local variables with their initial
  323. values: part of function entry }
  324. oldfilepos:=current_filepos;
  325. current_filepos:=current_procinfo.entrypos;
  326. current_procinfo.procdef.localst.SymList.ForEachCall(@initializevars,block);
  327. current_filepos:=oldfilepos;
  328. end
  329. else if current_procinfo.procdef.localst.symtabletype=staticsymtable then
  330. begin
  331. { for program and unit initialization code we also need to
  332. initialize the local variables used of Default() }
  333. oldfilepos:=current_filepos;
  334. current_filepos:=current_procinfo.entrypos;
  335. current_procinfo.procdef.localst.SymList.ForEachCall(@initializedefaultvars,block);
  336. current_filepos:=oldfilepos;
  337. end;
  338. if assigned(current_procinfo.procdef.parentfpstruct) then
  339. begin
  340. { we only do this after the code has been parsed because
  341. otherwise for-loop counters moved to the struct cause
  342. errors; we still do it nevertheless to prevent false
  343. "unused" symbols warnings and to assist debug info
  344. generation }
  345. redirect_parentfpstruct_local_syms(current_procinfo.procdef);
  346. { finish the parentfpstruct (add padding, ...) }
  347. finish_parentfpstruct(current_procinfo.procdef);
  348. end;
  349. end;
  350. end;
  351. {****************************************************************************
  352. PROCEDURE/FUNCTION COMPILING
  353. ****************************************************************************}
  354. procedure printnode_reset;
  355. begin
  356. assign(printnodefile,treelogfilename);
  357. {$push}{$I-}
  358. rewrite(printnodefile);
  359. {$pop}
  360. if ioresult<>0 then
  361. begin
  362. Comment(V_Error,'Error creating '+treelogfilename);
  363. exit;
  364. end;
  365. close(printnodefile);
  366. end;
  367. procedure add_label_init(p:TObject;arg:pointer);
  368. begin
  369. if tstoredsym(p).typ=labelsym then
  370. begin
  371. addstatement(tstatementnode(arg^),
  372. cifnode.create(caddnode.create(equaln,
  373. ccallnode.createintern('fpc_setjmp',
  374. ccallparanode.create(cloadnode.create(tlabelsym(p).jumpbuf,tlabelsym(p).jumpbuf.owner),nil)),
  375. cordconstnode.create(1,sinttype,true))
  376. ,cgotonode.create(tlabelsym(p)),nil)
  377. );
  378. end;
  379. end;
  380. function generate_bodyentry_block:tnode;
  381. var
  382. srsym : tsym;
  383. para : tcallparanode;
  384. call : tcallnode;
  385. newstatement : tstatementnode;
  386. def : tabstractrecorddef;
  387. elsenode : tnode;
  388. offsetsym : tfieldvarsym;
  389. paranode : tcallparanode;
  390. begin
  391. result:=internalstatements(newstatement);
  392. if assigned(current_structdef) then
  393. begin
  394. { a constructor needs a help procedure }
  395. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  396. begin
  397. if is_class(current_structdef) or
  398. (
  399. is_objectpascal_helper(current_structdef) and
  400. is_class(tobjectdef(current_structdef).extendeddef)
  401. ) then
  402. begin
  403. if is_objectpascal_helper(current_structdef) then
  404. def:=tabstractrecorddef(tobjectdef(current_structdef).extendeddef)
  405. else
  406. def:=current_structdef;
  407. srsym:=search_struct_member(def,'NEWINSTANCE');
  408. if assigned(srsym) and
  409. (srsym.typ=procsym) then
  410. begin
  411. { if vmt=1 then newinstance }
  412. addstatement(newstatement,cifnode.create(
  413. caddnode.create_internal(equaln,
  414. ctypeconvnode.create_internal(
  415. load_vmt_pointer_node,
  416. voidpointertype),
  417. cpointerconstnode.create(1,voidpointertype)),
  418. cassignmentnode.create(
  419. ctypeconvnode.create_internal(
  420. load_self_pointer_node,
  421. voidpointertype),
  422. ccallnode.create(nil,tprocsym(srsym),srsym.owner,
  423. ctypeconvnode.create_internal(load_self_pointer_node,cclassrefdef.create(current_structdef)),
  424. [])),
  425. nil));
  426. end
  427. else
  428. internalerror(200305108);
  429. end
  430. else
  431. if is_object(current_structdef) then
  432. begin
  433. { parameter 3 : vmt_offset }
  434. { parameter 2 : address of pointer to vmt,
  435. this is required to allow setting the vmt to -1 to indicate
  436. that memory was allocated }
  437. { parameter 1 : self pointer }
  438. para:=ccallparanode.create(
  439. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  440. ccallparanode.create(
  441. ctypeconvnode.create_internal(
  442. load_vmt_pointer_node,
  443. voidpointertype),
  444. ccallparanode.create(
  445. ctypeconvnode.create_internal(
  446. load_self_pointer_node,
  447. voidpointertype),
  448. nil)));
  449. addstatement(newstatement,cassignmentnode.create(
  450. ctypeconvnode.create_internal(
  451. load_self_pointer_node,
  452. voidpointertype),
  453. ccallnode.createintern('fpc_help_constructor',para)));
  454. end
  455. else
  456. if is_javaclass(current_structdef) or
  457. ((target_info.system in systems_jvm) and
  458. is_record(current_structdef)) then
  459. begin
  460. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  461. not current_procinfo.ConstructorCallingConstructor then
  462. begin
  463. { call inherited constructor }
  464. if is_javaclass(current_structdef) then
  465. srsym:=search_struct_member_no_helper(tobjectdef(current_structdef).childof,'CREATE')
  466. else
  467. srsym:=search_struct_member_no_helper(java_fpcbaserecordtype,'CREATE');
  468. if assigned(srsym) and
  469. (srsym.typ=procsym) then
  470. begin
  471. call:=ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[cnf_inherited]);
  472. exclude(tcallnode(call).callnodeflags,cnf_return_value_used);
  473. addstatement(newstatement,call);
  474. end
  475. else
  476. internalerror(2011010312);
  477. end;
  478. end
  479. else
  480. if not is_record(current_structdef) and
  481. not (
  482. is_objectpascal_helper(current_structdef) and
  483. (tobjectdef(current_structdef).extendeddef.typ<>objectdef)
  484. ) then
  485. internalerror(200305103);
  486. { if self=nil then exit
  487. calling fail instead of exit is useless because
  488. there is nothing to dispose (PFV) }
  489. if is_class_or_object(current_structdef) then
  490. begin
  491. if is_class(current_structdef) and (oo_is_reference_counted in tobjectdef(current_structdef).objectoptions) then
  492. begin
  493. { if the instance was successfully created we need to
  494. force the reference count to 1 }
  495. offsetsym:=tfieldvarsym(tobjectdef(current_structdef).refcount_field);
  496. if not assigned(offsetsym) or (offsetsym.typ<>fieldvarsym) then
  497. internalerror(2014092301);
  498. elsenode:=cifnode.create(
  499. caddnode.create_internal(equaln,
  500. ctypeconvnode.create_internal(
  501. load_vmt_pointer_node,
  502. voidpointertype),
  503. cpointerconstnode.create(1,voidpointertype)),
  504. cassignmentnode.create(
  505. csubscriptnode.create(offsetsym,load_self_node),
  506. cordconstnode.create(1,s32inttype,false)
  507. ),
  508. nil);
  509. end
  510. else
  511. elsenode:=nil;
  512. addstatement(newstatement,cifnode.create(
  513. caddnode.create(equaln,
  514. load_self_pointer_node,
  515. cnilnode.create),
  516. cexitnode.create(nil),
  517. elsenode));
  518. end;
  519. end;
  520. { maybe call BeforeDestruction for classes }
  521. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  522. is_class(current_structdef) then
  523. begin
  524. srsym:=search_struct_member(current_structdef,'BEFOREDESTRUCTION');
  525. if assigned(srsym) and
  526. (srsym.typ=procsym) then
  527. begin
  528. { if vmt>0 then beforedestruction }
  529. addstatement(newstatement,cifnode.create(
  530. caddnode.create(gtn,
  531. ctypeconvnode.create_internal(
  532. load_vmt_pointer_node,ptrsinttype),
  533. ctypeconvnode.create_internal(
  534. cnilnode.create,ptrsinttype)),
  535. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  536. nil));
  537. end
  538. else
  539. internalerror(200305104);
  540. end;
  541. end;
  542. if m_non_local_goto in current_settings.modeswitches then
  543. tsymtable(current_procinfo.procdef.localst).SymList.ForEachCall(@add_label_init,@newstatement);
  544. end;
  545. function generate_bodyexit_block:tnode;
  546. var
  547. srsym : tsym;
  548. para : tcallparanode;
  549. newstatement : tstatementnode;
  550. oldlocalswitches: tlocalswitches;
  551. begin
  552. result:=internalstatements(newstatement);
  553. if assigned(current_structdef) then
  554. begin
  555. { Don't test self and the vmt here. The reason is that }
  556. { a constructor already checks whether these are valid }
  557. { before. Further, in case of TThread the thread may }
  558. { free the class instance right after AfterConstruction }
  559. { has been called, so it may no longer be valid (JM) }
  560. oldlocalswitches:=current_settings.localswitches;
  561. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  562. { a destructor needs a help procedure }
  563. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  564. begin
  565. if is_class(current_structdef) then
  566. begin
  567. srsym:=search_struct_member(current_structdef,'FREEINSTANCE');
  568. if assigned(srsym) and
  569. (srsym.typ=procsym) then
  570. begin
  571. { if self<>0 and vmt<>0 then freeinstance }
  572. addstatement(newstatement,cifnode.create(
  573. caddnode.create(andn,
  574. caddnode.create(unequaln,
  575. load_self_pointer_node,
  576. cnilnode.create),
  577. caddnode.create(unequaln,
  578. ctypeconvnode.create(
  579. load_vmt_pointer_node,
  580. voidpointertype),
  581. cpointerconstnode.create(0,voidpointertype))),
  582. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  583. nil));
  584. end
  585. else
  586. internalerror(200305108);
  587. end
  588. else
  589. if is_object(current_structdef) then
  590. begin
  591. { finalize object data, but only if not in inherited call }
  592. if is_managed_type(current_structdef) then
  593. begin
  594. addstatement(newstatement,cifnode.create(
  595. caddnode.create(unequaln,
  596. ctypeconvnode.create_internal(load_vmt_pointer_node,voidpointertype),
  597. cnilnode.create),
  598. cnodeutils.finalize_data_node(load_self_node),
  599. nil));
  600. end;
  601. { parameter 3 : vmt_offset }
  602. { parameter 2 : pointer to vmt }
  603. { parameter 1 : self pointer }
  604. para:=ccallparanode.create(
  605. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  606. ccallparanode.create(
  607. ctypeconvnode.create_internal(
  608. load_vmt_pointer_node,
  609. voidpointertype),
  610. ccallparanode.create(
  611. ctypeconvnode.create_internal(
  612. load_self_pointer_node,
  613. voidpointertype),
  614. nil)));
  615. addstatement(newstatement,
  616. ccallnode.createintern('fpc_help_destructor',para));
  617. end
  618. else if is_javaclass(current_structdef) then
  619. begin
  620. { nothing to do }
  621. end
  622. else
  623. internalerror(200305105);
  624. end;
  625. current_settings.localswitches:=oldlocalswitches;
  626. end;
  627. end;
  628. {****************************************************************************
  629. TCGProcInfo
  630. ****************************************************************************}
  631. destructor tcgprocinfo.destroy;
  632. begin
  633. if assigned(code) then
  634. code.free;
  635. if not final_used then
  636. final_asmnode.free;
  637. inherited destroy;
  638. end;
  639. procedure tcgprocinfo.printproc(pass:string);
  640. begin
  641. assign(printnodefile,treelogfilename);
  642. {$push}{$I-}
  643. append(printnodefile);
  644. if ioresult<>0 then
  645. rewrite(printnodefile);
  646. {$pop}
  647. if ioresult<>0 then
  648. begin
  649. Comment(V_Error,'Error creating '+treelogfilename);
  650. exit;
  651. end;
  652. writeln(printnodefile);
  653. writeln(printnodefile,'*******************************************************************************');
  654. writeln(printnodefile, pass);
  655. writeln(printnodefile,procdef.fullprocname(false));
  656. writeln(printnodefile,'*******************************************************************************');
  657. printnode(printnodefile,code);
  658. close(printnodefile);
  659. end;
  660. procedure tcgprocinfo.maybe_add_constructor_wrapper(var tocode: tnode; withexceptblock: boolean);
  661. var
  662. oldlocalswitches: tlocalswitches;
  663. offsetsym : tfieldvarsym;
  664. srsym: tsym;
  665. thenblock,
  666. afterconstructionblock,
  667. exceptblock,
  668. newblock: tblocknode;
  669. thenstatement,
  670. newstatement: tstatementnode;
  671. thennode : tnode;
  672. pd: tprocdef;
  673. begin
  674. if assigned(procdef.struct) and
  675. (procdef.proctypeoption=potype_constructor) then
  676. begin
  677. withexceptblock:=
  678. withexceptblock and
  679. not(target_info.system in systems_garbage_collected_managed_types);
  680. { Don't test self and the vmt here. See generate_bodyexit_block }
  681. { why (JM) }
  682. oldlocalswitches:=current_settings.localswitches;
  683. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  684. { call AfterConstruction for classes }
  685. if is_class(procdef.struct) then
  686. begin
  687. srsym:=search_struct_member(procdef.struct,'AFTERCONSTRUCTION');
  688. if assigned(srsym) and
  689. (srsym.typ=procsym) then
  690. begin
  691. current_filepos:=exitpos;
  692. afterconstructionblock:=internalstatements(newstatement);
  693. { first execute all constructor code. If no exception
  694. occurred then we will execute afterconstruction,
  695. otherwise we won't (the exception will jump over us) }
  696. addstatement(newstatement,tocode);
  697. { if implicit finally node wasn't created, then exit label and
  698. finalization code must be handled here and placed before
  699. afterconstruction }
  700. if not ((pi_needs_implicit_finally in flags) and
  701. (cs_implicit_exceptions in current_settings.moduleswitches)) then
  702. begin
  703. include(tocode.flags,nf_block_with_exit);
  704. addstatement(newstatement,final_asmnode);
  705. final_used:=true;
  706. end;
  707. thennode:=ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]);
  708. if oo_is_reference_counted in procdef.struct.objectoptions then
  709. begin
  710. thenblock:=internalstatements(thenstatement);
  711. addstatement(thenstatement,thennode);
  712. offsetsym:=tfieldvarsym(tobjectdef(current_structdef).refcount_field);
  713. if not assigned(offsetsym) or (offsetsym.typ<>fieldvarsym) then
  714. internalerror(2014092301);
  715. addstatement(thenstatement,ccallnode.createintern('fpc_refcountclass_decr_ref',
  716. ccallparanode.create(
  717. { tell the helper to not call Destroy }
  718. cordconstnode.create(-offsetsym.fieldoffset,s32inttype,false),
  719. ccallparanode.create(
  720. ctypeconvnode.create_internal(load_self_pointer_node,voidpointertype),
  721. nil))));
  722. thennode:=thenblock;
  723. end;
  724. { Self can be nil when fail is called }
  725. { if self<>nil and vmt<>nil then afterconstruction }
  726. addstatement(newstatement,cifnode.create(
  727. caddnode.create(andn,
  728. caddnode.create(unequaln,
  729. load_self_node,
  730. cnilnode.create),
  731. caddnode.create(unequaln,
  732. load_vmt_pointer_node,
  733. cnilnode.create)),
  734. thennode,
  735. nil));
  736. tocode:=afterconstructionblock;
  737. end
  738. else
  739. internalerror(200305106);
  740. end;
  741. if withexceptblock and (procdef.struct.typ=objectdef) then
  742. begin
  743. { Generate the implicit "fail" code for a constructor (destroy
  744. in case an exception happened) }
  745. pd:=tobjectdef(procdef.struct).find_destructor;
  746. { this will always be the case for classes, since tobject has
  747. a destructor }
  748. if assigned(pd) or is_object(procdef.struct) then
  749. begin
  750. current_filepos:=exitpos;
  751. exceptblock:=internalstatements(newstatement);
  752. { first free the instance if non-nil }
  753. if assigned(pd) then
  754. begin
  755. { if vmt<>0 then call destructor }
  756. addstatement(newstatement,
  757. cifnode.create(
  758. caddnode.create(unequaln,
  759. load_vmt_pointer_node,
  760. cnilnode.create),
  761. { cnf_create_failed -> don't call BeforeDestruction }
  762. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[cnf_create_failed]),
  763. nil));
  764. end
  765. else
  766. { object without destructor, call 'fail' helper }
  767. addstatement(newstatement,
  768. ccallnode.createintern('fpc_help_fail',
  769. ccallparanode.create(
  770. cordconstnode.create(tobjectdef(procdef.struct).vmt_offset,s32inttype,false),
  771. ccallparanode.create(
  772. ctypeconvnode.create_internal(
  773. load_vmt_pointer_node,
  774. voidpointertype),
  775. ccallparanode.create(
  776. ctypeconvnode.create_internal(
  777. load_self_pointer_node,
  778. voidpointertype),
  779. nil))))
  780. );
  781. { then re-raise the exception }
  782. addstatement(newstatement,craisenode.create(nil,nil,nil));
  783. current_filepos:=entrypos;
  784. newblock:=internalstatements(newstatement);
  785. { try
  786. tocode
  787. except
  788. exceptblock
  789. end
  790. }
  791. addstatement(newstatement,ctryexceptnode.create(
  792. tocode,
  793. nil,
  794. exceptblock));
  795. tocode:=newblock;
  796. end;
  797. end;
  798. current_settings.localswitches:=oldlocalswitches;
  799. end;
  800. end;
  801. procedure tcgprocinfo.add_entry_exit_code;
  802. var
  803. finalcode,
  804. bodyentrycode,
  805. bodyexitcode,
  806. wrappedbody,
  807. newblock : tnode;
  808. codestatement,
  809. newstatement : tstatementnode;
  810. oldfilepos : tfileposinfo;
  811. is_constructor: boolean;
  812. begin
  813. is_constructor:=assigned(procdef.struct) and
  814. (procdef.proctypeoption=potype_constructor);
  815. oldfilepos:=current_filepos;
  816. { Generate code/locations used at start of proc }
  817. current_filepos:=entrypos;
  818. entry_asmnode:=casmnode.create_get_position;
  819. loadpara_asmnode:=casmnode.create_get_position;
  820. stackcheck_asmnode:=casmnode.create_get_position;
  821. init_asmnode:=casmnode.create_get_position;
  822. bodyentrycode:=generate_bodyentry_block;
  823. { Generate code/locations used at end of proc }
  824. current_filepos:=exitpos;
  825. exitlabel_asmnode:=casmnode.create_get_position;
  826. final_asmnode:=casmnode.create_get_position;
  827. final_used:=false;
  828. bodyexitcode:=generate_bodyexit_block;
  829. { Generate procedure by combining init+body+final,
  830. depending on the implicit finally we need to add
  831. an try...finally...end wrapper }
  832. newblock:=internalstatements(newstatement);
  833. { initialization is common for all cases }
  834. addstatement(newstatement,loadpara_asmnode);
  835. addstatement(newstatement,stackcheck_asmnode);
  836. addstatement(newstatement,entry_asmnode);
  837. addstatement(newstatement,init_asmnode);
  838. addstatement(newstatement,bodyentrycode);
  839. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  840. (pi_needs_implicit_finally in flags) and
  841. { but it's useless in init/final code of units }
  842. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  843. not(po_assembler in procdef.procoptions) and
  844. not(target_info.system in systems_garbage_collected_managed_types) then
  845. begin
  846. { Any result of managed type must be returned in parameter }
  847. if is_managed_type(procdef.returndef) and
  848. (not paramanager.ret_in_param(procdef.returndef,procdef)) and
  849. (not is_class(procdef.returndef)) then
  850. InternalError(2013121301);
  851. { Generate special exception block only needed when
  852. implicit finaly is used }
  853. current_filepos:=exitpos;
  854. { Generate code that will be in the try...finally }
  855. finalcode:=internalstatements(codestatement);
  856. addstatement(codestatement,final_asmnode);
  857. final_used:=true;
  858. current_filepos:=entrypos;
  859. wrappedbody:=ctryfinallynode.create_implicit(
  860. code,
  861. finalcode,
  862. cnothingnode.create);
  863. { afterconstruction must be called after final_asmnode, because it
  864. has to execute after the temps have been finalised in case of a
  865. refcounted class (afterconstruction decreases the refcount
  866. without freeing the instance if the count becomes nil, while
  867. the finalising of the temps can free the instance) }
  868. maybe_add_constructor_wrapper(wrappedbody,true);
  869. addstatement(newstatement,wrappedbody);
  870. addstatement(newstatement,exitlabel_asmnode);
  871. addstatement(newstatement,bodyexitcode);
  872. { set flag the implicit finally has been generated }
  873. include(flags,pi_has_implicit_finally);
  874. end
  875. else
  876. begin
  877. { constructors need destroy-on-exception code even if they don't
  878. have managed variables/temps }
  879. maybe_add_constructor_wrapper(code,
  880. cs_implicit_exceptions in current_settings.moduleswitches);
  881. addstatement(newstatement,code);
  882. if assigned(nestedexitlabel) then
  883. addstatement(newstatement,clabelnode.create(cnothingnode.create,nestedexitlabel));
  884. addstatement(newstatement,exitlabel_asmnode);
  885. addstatement(newstatement,bodyexitcode);
  886. if not is_constructor then
  887. begin
  888. addstatement(newstatement,final_asmnode);
  889. final_used:=true;
  890. end;
  891. end;
  892. do_firstpass(newblock);
  893. code:=newblock;
  894. current_filepos:=oldfilepos;
  895. end;
  896. procedure clearrefs(p:TObject;arg:pointer);
  897. begin
  898. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  899. if tabstractvarsym(p).refs>1 then
  900. tabstractvarsym(p).refs:=1;
  901. end;
  902. procedure translate_registers(p:TObject;list:pointer);
  903. begin
  904. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  905. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  906. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  907. begin
  908. if not(cs_no_regalloc in current_settings.globalswitches) then
  909. begin
  910. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  911. if (tabstractnormalvarsym(p).localloc.registerhi<>NR_NO) then
  912. cg.translate_register(tabstractnormalvarsym(p).localloc.registerhi);
  913. end;
  914. if cs_asm_source in current_settings.globalswitches then
  915. begin
  916. TAsmList(list).concat(Tai_comment.Create(strpnew('Var '+tabstractnormalvarsym(p).realname+' located in register '+
  917. location_reg2string(tabstractnormalvarsym(p).localloc))));
  918. end;
  919. end;
  920. end;
  921. const
  922. exception_flags: array[boolean] of tprocinfoflags = (
  923. [],
  924. [pi_uses_exceptions,pi_needs_implicit_finally,pi_has_implicit_finally]
  925. );
  926. procedure tcgprocinfo.setup_tempgen;
  927. begin
  928. tg:=tgobjclass.create;
  929. {$if defined(i386) or defined(x86_64) or defined(arm)}
  930. {$if defined(arm)}
  931. { frame and stack pointer must be always the same on arm thumb so it makes no
  932. sense to fiddle with a frame pointer }
  933. if GenerateThumbCode then
  934. begin
  935. framepointer:=NR_STACK_POINTER_REG;
  936. tg.direction:=1;
  937. end
  938. else
  939. {$endif defined(arm)}
  940. begin
  941. { try to strip the stack frame }
  942. { set the framepointer to esp if:
  943. - no assembler directive, those are handled in assembler_block
  944. in pstatment.pas (for cases not caught by the Delphi
  945. exception below)
  946. - no exceptions are used
  947. - no pushes are used/esp modifications, could be:
  948. * outgoing parameters on the stack on non-fixed stack target
  949. * incoming parameters on the stack
  950. * open arrays
  951. - no inline assembler
  952. or
  953. - Delphi mode
  954. - assembler directive
  955. - no pushes are used/esp modifications, could be:
  956. * outgoing parameters on the stack
  957. * incoming parameters on the stack
  958. * open arrays
  959. - no local variables
  960. - stack frame cannot be optimized if using Win64 SEH
  961. (at least with the current state of our codegenerator).
  962. }
  963. if ((po_assembler in procdef.procoptions) and
  964. (m_delphi in current_settings.modeswitches) and
  965. { localst at main_program_level is a staticsymtable }
  966. (procdef.localst.symtablelevel<>main_program_level) and
  967. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  968. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  969. not(cs_generate_stackframes in current_settings.localswitches) and
  970. not(cs_profile in current_settings.moduleswitches) and
  971. not(po_assembler in procdef.procoptions) and
  972. not ((pi_has_stackparameter in flags)
  973. {$ifndef arm} { Outgoing parameter(s) on stack do not need stackframe on x86 targets
  974. with fixed stack. On ARM it fails, see bug #25050 }
  975. and (not paramanager.use_fixed_stack)
  976. {$endif arm}
  977. ) and
  978. ((flags*([pi_has_assembler_block,pi_is_assembler,
  979. pi_needs_stackframe]+
  980. exception_flags[(target_info.cpu=cpu_i386)
  981. {$ifndef DISABLE_WIN64_SEH}
  982. or (target_info.system=system_x86_64_win64)
  983. {$endif DISABLE_WIN64_SEH}
  984. ]))=[])
  985. )
  986. then
  987. begin
  988. { we need the parameter info here to determine if the procedure gets
  989. parameters on the stack
  990. calling generate_parameter_info doesn't hurt but it costs time
  991. (necessary to init para_stack_size)
  992. }
  993. generate_parameter_info;
  994. if not(procdef.stack_tainting_parameter(calleeside)) and
  995. not(has_assembler_child) and (para_stack_size=0) then
  996. begin
  997. { Only need to set the framepointer }
  998. framepointer:=NR_STACK_POINTER_REG;
  999. tg.direction:=1;
  1000. end
  1001. {$if defined(arm)}
  1002. { On arm, the stack frame size can be estimated to avoid using an extra frame pointer,
  1003. in case parameters are passed on the stack.
  1004. However, the draw back is, if the estimation fails, compilation will break later on
  1005. with an internal error, so this switch is not enabled by default yet. To overcome this,
  1006. multipass compilation of subroutines must be supported
  1007. }
  1008. else if (cs_opt_forcenostackframe in current_settings.optimizerswitches) and
  1009. not(has_assembler_child) then
  1010. begin
  1011. { Only need to set the framepointer }
  1012. framepointer:=NR_STACK_POINTER_REG;
  1013. tg.direction:=1;
  1014. include(flags,pi_estimatestacksize);
  1015. set_first_temp_offset;
  1016. procdef.has_paraloc_info:=callnoside;
  1017. generate_parameter_info;
  1018. exit;
  1019. end;
  1020. {$endif defined(arm)}
  1021. end;
  1022. end;
  1023. {$endif defined(x86) or defined(arm)}
  1024. { set the start offset to the start of the temp area in the stack }
  1025. set_first_temp_offset;
  1026. end;
  1027. function tcgprocinfo.has_assembler_child : boolean;
  1028. var
  1029. hp : tprocinfo;
  1030. begin
  1031. result:=false;
  1032. hp:=get_first_nestedproc;
  1033. while assigned(hp) do
  1034. begin
  1035. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  1036. begin
  1037. result:=true;
  1038. exit;
  1039. end;
  1040. hp:=tprocinfo(hp.next);
  1041. end;
  1042. end;
  1043. procedure tcgprocinfo.generate_code_tree;
  1044. var
  1045. hpi : tcgprocinfo;
  1046. begin
  1047. { generate code for this procedure }
  1048. generate_code;
  1049. { process nested procedures }
  1050. hpi:=tcgprocinfo(get_first_nestedproc);
  1051. while assigned(hpi) do
  1052. begin
  1053. hpi.generate_code_tree;
  1054. hpi:=tcgprocinfo(hpi.next);
  1055. end;
  1056. resetprocdef;
  1057. end;
  1058. { For SEH, the code from 'finally' blocks must be put into a separate procedures,
  1059. which can be called by OS during stack unwind. This resembles nested procedures,
  1060. but finalizer procedures do not have their own local variables and work directly
  1061. with the stack frame of parent. In particular, the tempgen must be shared, so
  1062. 1) finalizer procedure is able to finalize temps of the parent,
  1063. 2) if the finalizer procedure is complex enough to need its own temps, they are
  1064. allocated in stack frame of parent, so second-level finalizer procedures are
  1065. not needed.
  1066. Due to requirement of shared tempgen we cannot process finalizer as a regular nested
  1067. procedure (after the parent) and have to do it inline.
  1068. This is called by platform-specific tryfinallynodes during pass2.
  1069. Here we put away the codegen (which carries the register allocator state), process
  1070. the 'nested' procedure, then restore previous cg and continue processing the parent
  1071. procedure. generate_code() will create another cg, but not another tempgen because
  1072. setup_tempgen() is not called for potype_exceptfilter procedures. }
  1073. procedure tcgprocinfo.generate_exceptfilter(nestedpi: tcgprocinfo);
  1074. var
  1075. saved_cg: tcg;
  1076. saved_hlcg: thlcgobj;
  1077. {$ifdef cpu64bitalu}
  1078. saved_cg128 : tcg128;
  1079. {$else cpu64bitalu}
  1080. saved_cg64 : tcg64;
  1081. {$endif cpu64bitalu}
  1082. begin
  1083. if nestedpi.procdef.proctypeoption<>potype_exceptfilter then
  1084. InternalError(201201141);
  1085. { flush code generated this far }
  1086. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1087. { save the codegen }
  1088. saved_cg:=cg;
  1089. saved_hlcg:=hlcg;
  1090. cg:=nil;
  1091. hlcg:=nil;
  1092. {$ifdef cpu64bitalu}
  1093. saved_cg128:=cg128;
  1094. cg128:=nil;
  1095. {$else cpu64bitalu}
  1096. saved_cg64:=cg64;
  1097. cg64:=nil;
  1098. {$endif cpu64bitalu}
  1099. nestedpi.generate_code;
  1100. { prevents generating code the second time when processing nested procedures }
  1101. nestedpi.resetprocdef;
  1102. cg:=saved_cg;
  1103. hlcg:=saved_hlcg;
  1104. {$ifdef cpu64bitalu}
  1105. cg128:=saved_cg128;
  1106. {$else cpu64bitalu}
  1107. cg64:=saved_cg64;
  1108. {$endif cpu64bitalu}
  1109. add_reg_instruction_hook:[email protected]_reg_instruction;
  1110. end;
  1111. procedure TCGProcinfo.CreateInlineInfo;
  1112. begin
  1113. new(procdef.inlininginfo);
  1114. procdef.inlininginfo^.code:=code.getcopy;
  1115. procdef.inlininginfo^.flags:=flags;
  1116. { The blocknode needs to set an exit label }
  1117. if procdef.inlininginfo^.code.nodetype=blockn then
  1118. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1119. procdef.has_inlininginfo:=true;
  1120. end;
  1121. function searchusercode(var n: tnode; arg: pointer): foreachnoderesult;
  1122. begin
  1123. if nf_usercode_entry in n.flags then
  1124. begin
  1125. pnode(arg)^:=n;
  1126. result:=fen_norecurse_true
  1127. end
  1128. else
  1129. result:=fen_false;
  1130. end;
  1131. function TCGProcinfo.GetUserCode : tnode;
  1132. var
  1133. n : tnode;
  1134. begin
  1135. n:=nil;
  1136. foreachnodestatic(code,@searchusercode,@n);
  1137. if not(assigned(n)) then
  1138. internalerror(2013111004);
  1139. result:=n;
  1140. end;
  1141. procedure tcgprocinfo.generate_code;
  1142. var
  1143. old_current_procinfo : tprocinfo;
  1144. oldmaxfpuregisters : longint;
  1145. oldfilepos : tfileposinfo;
  1146. old_current_structdef : tabstractrecorddef;
  1147. templist : TAsmList;
  1148. headertai : tai;
  1149. i : integer;
  1150. varsym : tabstractnormalvarsym;
  1151. {RedoDFA : boolean;}
  1152. procedure delete_marker(anode: tasmnode);
  1153. var
  1154. ai: tai;
  1155. begin
  1156. if assigned(anode) then
  1157. begin
  1158. ai:=anode.currenttai;
  1159. if assigned(ai) then
  1160. begin
  1161. aktproccode.remove(ai);
  1162. ai.free;
  1163. anode.currenttai:=nil;
  1164. end;
  1165. end;
  1166. end;
  1167. begin
  1168. { the initialization procedure can be empty, then we
  1169. don't need to generate anything. When it was an empty
  1170. procedure there would be at least a blocknode }
  1171. if not assigned(code) then
  1172. exit;
  1173. { We need valid code }
  1174. if Errorcount<>0 then
  1175. exit;
  1176. { No code can be generated for generic template }
  1177. if (df_generic in procdef.defoptions) then
  1178. internalerror(200511152);
  1179. { For regular procedures the RA and Tempgen shall not be available yet,
  1180. but exception filters reuse Tempgen of parent }
  1181. if assigned(tg)<>(procdef.proctypeoption=potype_exceptfilter) then
  1182. internalerror(200309201);
  1183. old_current_procinfo:=current_procinfo;
  1184. oldfilepos:=current_filepos;
  1185. old_current_structdef:=current_structdef;
  1186. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  1187. current_procinfo:=self;
  1188. current_filepos:=entrypos;
  1189. current_structdef:=procdef.struct;
  1190. { store start of user code, it must be a block node, it will be used later one to
  1191. check variable lifeness }
  1192. include(code.flags,nf_usercode_entry);
  1193. { add wrapping code if necessary (initialization of typed constants on
  1194. some platforms, initing of local variables and out parameters with
  1195. trashing values, ...) }
  1196. { init/final code must be wrapped later (after code for main proc body
  1197. has been generated) }
  1198. if not(current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1199. code:=cnodeutils.wrap_proc_body(procdef,code);
  1200. { automatic inlining? }
  1201. if (cs_opt_autoinline in current_settings.optimizerswitches) and
  1202. { inlining not turned off? }
  1203. (cs_do_inline in current_settings.localswitches) and
  1204. { no inlining yet? }
  1205. not(procdef.has_inlininginfo) and not(has_nestedprocs) and
  1206. not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize,potype_constructor,
  1207. potype_destructor,potype_class_constructor,potype_class_destructor]) and
  1208. ((procdef.procoptions*[po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck])=[]) and
  1209. (not(procdef.proccalloption in [pocall_safecall])) and
  1210. { rough approximation if we should auto inline }
  1211. (node_count(code)<=10) then
  1212. begin
  1213. { Can we inline this procedure? }
  1214. if checknodeinlining(procdef) then
  1215. begin
  1216. Message1(cg_d_autoinlining,procdef.GetTypeName);
  1217. include(procdef.procoptions,po_inline);
  1218. CreateInlineInfo;
  1219. end;
  1220. end;
  1221. templist:=TAsmList.create;
  1222. { add parast/localst to symtablestack }
  1223. add_to_symtablestack;
  1224. { clear register count }
  1225. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  1226. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  1227. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  1228. if (procdef.localst.symtablelevel=main_program_level) and
  1229. (not current_module.is_unit) then
  1230. include(flags,pi_do_call);
  1231. { set implicit_finally flag when there are locals/paras to be finalized }
  1232. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  1233. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  1234. {$ifdef SUPPORT_SAFECALL}
  1235. { set implicit_finally flag for if procedure is safecall }
  1236. if (tf_safecall_exceptions in target_info.flags) and
  1237. (procdef.proccalloption=pocall_safecall) then
  1238. include(flags, pi_needs_implicit_finally);
  1239. {$endif}
  1240. { firstpass everything }
  1241. flowcontrol:=[];
  1242. do_firstpass(code);
  1243. {$if defined(i386) or defined(i8086)}
  1244. if node_resources_fpu(code)>0 then
  1245. include(flags,pi_uses_fpu);
  1246. {$endif i386 or i8086}
  1247. { Print the node to tree.log }
  1248. if paraprintnodetree=1 then
  1249. printproc( 'after the firstpass');
  1250. { do this before adding the entry code else the tail recursion recognition won't work,
  1251. if this causes troubles, it must be if'ed
  1252. }
  1253. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  1254. (pi_is_recursive in flags) then
  1255. do_opttail(code,procdef);
  1256. if cs_opt_constant_propagate in current_settings.optimizerswitches then
  1257. do_optconstpropagate(code);
  1258. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  1259. { creating dfa is not always possible }
  1260. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler])=[]) then
  1261. begin
  1262. dfabuilder:=TDFABuilder.Create;
  1263. dfabuilder.createdfainfo(code);
  1264. include(flags,pi_dfaavailable);
  1265. { when life info is available, we can give more sophisticated warning about uninitialized
  1266. variables ...
  1267. ... but not for the finalization section of a unit, we would need global dfa to handle
  1268. it properly }
  1269. if potype_unitfinalize<>procdef.proctypeoption then
  1270. { iterate through life info of the first node }
  1271. for i:=0 to dfabuilder.nodemap.count-1 do
  1272. begin
  1273. if DFASetIn(GetUserCode.optinfo^.life,i) then
  1274. CheckAndWarn(GetUserCode,tnode(dfabuilder.nodemap[i]));
  1275. end;
  1276. end;
  1277. if (pi_dfaavailable in flags) and (cs_opt_dead_store_eliminate in current_settings.optimizerswitches) then
  1278. do_optdeadstoreelim(code);
  1279. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  1280. { our induction variable strength reduction doesn't like
  1281. for loops with more than one entry }
  1282. and not(pi_has_label in flags) then
  1283. begin
  1284. {RedoDFA:=}OptimizeInductionVariables(code);
  1285. end;
  1286. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  1287. (procdef.proctypeoption in [potype_operator,potype_procedure,potype_function]) and
  1288. (code.nodetype=blockn) and (tblocknode(code).statements=nil) then
  1289. procdef.isempty:=true;
  1290. { add implicit entry and exit code }
  1291. add_entry_exit_code;
  1292. if cs_opt_nodecse in current_settings.optimizerswitches then
  1293. do_optcse(code);
  1294. { only do secondpass if there are no errors }
  1295. if (ErrorCount=0) then
  1296. begin
  1297. create_hlcodegen;
  1298. if (procdef.proctypeoption<>potype_exceptfilter) then
  1299. setup_tempgen;
  1300. { Create register allocator, must come after framepointer is known }
  1301. hlcg.init_register_allocators;
  1302. generate_parameter_info;
  1303. { allocate got register if needed }
  1304. allocate_got_register(aktproccode);
  1305. { Allocate space in temp/registers for parast and localst }
  1306. current_filepos:=entrypos;
  1307. gen_alloc_symtable(aktproccode,procdef,procdef.parast);
  1308. gen_alloc_symtable(aktproccode,procdef,procdef.localst);
  1309. { Store temp offset for information about 'real' temps }
  1310. tempstart:=tg.lasttemp;
  1311. { Generate code to load register parameters in temps and insert local
  1312. copies for values parameters. This must be done before the code for the
  1313. body is generated because the localloc is updated.
  1314. Note: The generated code will be inserted after the code generation of
  1315. the body is finished, because only then the position is known }
  1316. {$ifdef oldregvars}
  1317. assign_regvars(code);
  1318. {$endif oldreg}
  1319. current_filepos:=entrypos;
  1320. hlcg.gen_load_para_value(templist);
  1321. { caller paraloc info is also necessary in the stackframe_entry
  1322. code of the ppc (and possibly other processors) }
  1323. procdef.init_paraloc_info(callerside);
  1324. { generate code for the node tree }
  1325. do_secondpass(code);
  1326. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1327. { The position of the loadpara_asmnode is now known }
  1328. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  1329. { first generate entry and initialize code with the correct
  1330. position and switches }
  1331. current_filepos:=entrypos;
  1332. current_settings.localswitches:=entryswitches;
  1333. cg.set_regalloc_live_range_direction(rad_backwards);
  1334. hlcg.gen_entry_code(templist);
  1335. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  1336. hlcg.gen_initialize_code(templist);
  1337. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  1338. { now generate finalize and exit code with the correct position
  1339. and switches }
  1340. current_filepos:=exitpos;
  1341. current_settings.localswitches:=exitswitches;
  1342. cg.set_regalloc_live_range_direction(rad_forward);
  1343. if assigned(finalize_procinfo) then
  1344. generate_exceptfilter(tcgprocinfo(finalize_procinfo))
  1345. else
  1346. begin
  1347. hlcg.gen_finalize_code(templist);
  1348. { the finalcode must be concated if there was no position available,
  1349. using insertlistafter will result in an insert at the start
  1350. when currentai=nil }
  1351. if assigned(final_asmnode) and assigned(final_asmnode.currenttai) then
  1352. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  1353. else
  1354. aktproccode.concatlist(templist);
  1355. end;
  1356. { insert exit label at the correct position }
  1357. hlcg.a_label(templist,CurrExitLabel);
  1358. if assigned(exitlabel_asmnode.currenttai) then
  1359. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  1360. else
  1361. aktproccode.concatlist(templist);
  1362. { exit code }
  1363. hlcg.gen_exit_code(templist);
  1364. aktproccode.concatlist(templist);
  1365. {$ifdef OLDREGVARS}
  1366. { note: this must be done only after as much code as possible has }
  1367. { been generated. The result is that when you ungetregister() a }
  1368. { regvar, it will actually free the regvar (and alse free the }
  1369. { the regvars at the same time). Doing this too early will }
  1370. { confuse the register allocator, as the regvars will still be }
  1371. { used. It should be done before loading the result regs (so }
  1372. { they don't conflict with the regvars) and before }
  1373. { gen_entry_code (that one has to be able to allocate the }
  1374. { regvars again) (JM) }
  1375. free_regvars(aktproccode);
  1376. {$endif OLDREGVARS}
  1377. { generate symbol and save end of header position }
  1378. current_filepos:=entrypos;
  1379. hlcg.gen_proc_symbol(templist);
  1380. headertai:=tai(templist.last);
  1381. { insert symbol }
  1382. aktproccode.insertlist(templist);
  1383. { Free space in temp/registers for parast and localst, must be
  1384. done after gen_entry_code }
  1385. current_filepos:=exitpos;
  1386. { make sure the got/pic register doesn't get freed in the }
  1387. { middle of a loop }
  1388. if (cs_create_pic in current_settings.moduleswitches) and
  1389. (pi_needs_got in flags) and
  1390. (got<>NR_NO) then
  1391. cg.a_reg_sync(aktproccode,got);
  1392. gen_free_symtable(aktproccode,procdef.localst);
  1393. gen_free_symtable(aktproccode,procdef.parast);
  1394. { add code that will load the return value, this is not done
  1395. for assembler routines when they didn't reference the result
  1396. variable }
  1397. hlcg.gen_load_return_value(templist);
  1398. aktproccode.concatlist(templist);
  1399. { Already reserve all registers for stack checking code and
  1400. generate the call to the helper function }
  1401. if not(tf_no_generic_stackcheck in target_info.flags) and
  1402. (cs_check_stack in entryswitches) and
  1403. not(po_assembler in procdef.procoptions) and
  1404. (procdef.proctypeoption<>potype_proginit) then
  1405. begin
  1406. current_filepos:=entrypos;
  1407. gen_stack_check_call(templist);
  1408. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1409. end;
  1410. { this code (got loading) comes before everything which has }
  1411. { already been generated, so reset the info about already }
  1412. { backwards extended registers (so their live range can be }
  1413. { extended backwards even further if needed) }
  1414. { This code must be }
  1415. { a) generated after do_secondpass has been called }
  1416. { (because pi_needs_got may be set there) }
  1417. { b) generated before register allocation, because the }
  1418. { got/pic register can be a virtual one }
  1419. { c) inserted before the entry code, because the entry }
  1420. { code may need global symbols such as init rtti }
  1421. { d) inserted after the stackframe allocation, because }
  1422. { this register may have to be spilled }
  1423. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  1424. current_filepos:=entrypos;
  1425. { load got if necessary }
  1426. cg.g_maybe_got_init(templist);
  1427. aktproccode.insertlistafter(headertai,templist);
  1428. { re-enable if more code at the end is ever generated here
  1429. cg.set_regalloc_live_range_direction(rad_forward);
  1430. }
  1431. {$ifndef NoOpt}
  1432. {$ifndef i386}
  1433. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1434. { do not optimize pure assembler procedures }
  1435. not(pi_is_assembler in flags) then
  1436. preregallocschedule(aktproccode);
  1437. {$endif i386}
  1438. {$endif NoOpt}
  1439. { The procedure body is finished, we can now
  1440. allocate the registers }
  1441. cg.do_register_allocation(aktproccode,headertai);
  1442. { translate imag. register to their real counter parts
  1443. this is necessary for debuginfo and verbose assembler output
  1444. when SSA will be implented, this will be more complicated because we've to
  1445. maintain location lists }
  1446. procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  1447. procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  1448. if (cs_create_pic in current_settings.moduleswitches) and
  1449. (pi_needs_got in flags) and
  1450. not(cs_no_regalloc in current_settings.globalswitches) and
  1451. (got<>NR_NO) then
  1452. cg.translate_register(got);
  1453. { Add save and restore of used registers }
  1454. current_filepos:=entrypos;
  1455. gen_save_used_regs(templist);
  1456. { Remember the last instruction of register saving block
  1457. (may be =nil for e.g. assembler procedures) }
  1458. endprologue_ai:=templist.last;
  1459. aktproccode.insertlistafter(headertai,templist);
  1460. current_filepos:=exitpos;
  1461. gen_restore_used_regs(aktproccode);
  1462. { We know the size of the stack, now we can generate the
  1463. parameter that is passed to the stack checking code }
  1464. if not(tf_no_generic_stackcheck in target_info.flags) and
  1465. (cs_check_stack in entryswitches) and
  1466. not(po_assembler in procdef.procoptions) and
  1467. (procdef.proctypeoption<>potype_proginit) then
  1468. begin
  1469. current_filepos:=entrypos;
  1470. gen_stack_check_size_para(templist);
  1471. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1472. end;
  1473. { Add entry code (stack allocation) after header }
  1474. current_filepos:=entrypos;
  1475. gen_proc_entry_code(templist);
  1476. aktproccode.insertlistafter(headertai,templist);
  1477. {$ifdef SUPPORT_SAFECALL}
  1478. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  1479. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  1480. (tf_safecall_exceptions in target_info.flags) and
  1481. (procdef.proccalloption=pocall_safecall) then
  1482. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  1483. {$endif}
  1484. { Add exit code at the end }
  1485. current_filepos:=exitpos;
  1486. gen_proc_exit_code(templist);
  1487. aktproccode.concatlist(templist);
  1488. { check if the implicit finally has been generated. The flag
  1489. should already be set in pass1 }
  1490. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  1491. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  1492. (pi_needs_implicit_finally in flags) and
  1493. not(po_assembler in procdef.procoptions) and
  1494. not(pi_has_implicit_finally in flags) and
  1495. not(target_info.system in systems_garbage_collected_managed_types) then
  1496. internalerror(200405231);
  1497. { Position markers are only used to insert additional code after the secondpass
  1498. and before this point. They are of no use in optimizer. Instead of checking and
  1499. ignoring all over the optimizer, just remove them here. }
  1500. delete_marker(entry_asmnode);
  1501. delete_marker(loadpara_asmnode);
  1502. delete_marker(exitlabel_asmnode);
  1503. delete_marker(stackcheck_asmnode);
  1504. delete_marker(init_asmnode);
  1505. delete_marker(final_asmnode);
  1506. {$ifndef NoOpt}
  1507. if not(cs_no_regalloc in current_settings.globalswitches) then
  1508. begin
  1509. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1510. { do not optimize pure assembler procedures }
  1511. not(pi_is_assembler in flags) then
  1512. optimize(aktproccode);
  1513. {$ifndef i386}
  1514. { schedule after assembler optimization, it could have brought up
  1515. new schedule possibilities }
  1516. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1517. { do not optimize pure assembler procedures }
  1518. not(pi_is_assembler in flags) then
  1519. preregallocschedule(aktproccode);
  1520. {$endif i386}
  1521. end;
  1522. {$endif NoOpt}
  1523. { Perform target-specific processing if necessary }
  1524. postprocess_code;
  1525. { Add end symbol and debug info }
  1526. { this must be done after the pcrelativedata is appended else the distance calculation of
  1527. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  1528. so it should be inserted before the end symbol (FK)
  1529. }
  1530. current_filepos:=exitpos;
  1531. hlcg.gen_proc_symbol_end(templist);
  1532. aktproccode.concatlist(templist);
  1533. { insert line debuginfo }
  1534. if (cs_debuginfo in current_settings.moduleswitches) or
  1535. (cs_use_lineinfo in current_settings.globalswitches) then
  1536. current_debuginfo.insertlineinfo(aktproccode);
  1537. hlcg.record_generated_code_for_procdef(current_procinfo.procdef,aktproccode,aktlocaldata);
  1538. { only now we can remove the temps }
  1539. if (procdef.proctypeoption<>potype_exceptfilter) then
  1540. begin
  1541. tg.resettempgen;
  1542. tg.free;
  1543. tg:=nil;
  1544. end;
  1545. { stop tempgen and ra }
  1546. hlcg.done_register_allocators;
  1547. destroy_hlcodegen;
  1548. end;
  1549. dfabuilder.free;
  1550. { restore symtablestack }
  1551. remove_from_symtablestack;
  1552. { restore }
  1553. templist.free;
  1554. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1555. current_filepos:=oldfilepos;
  1556. current_structdef:=old_current_structdef;
  1557. current_procinfo:=old_current_procinfo;
  1558. end;
  1559. procedure tcgprocinfo.add_to_symtablestack;
  1560. begin
  1561. { insert symtables for the class, but only if it is no nested function }
  1562. if assigned(procdef.struct) and
  1563. not(assigned(parent) and
  1564. assigned(parent.procdef) and
  1565. assigned(parent.procdef.struct)) then
  1566. push_nested_hierarchy(procdef.struct);
  1567. { insert parasymtable in symtablestack when parsing
  1568. a function }
  1569. if procdef.parast.symtablelevel>=normal_function_level then
  1570. symtablestack.push(procdef.parast);
  1571. { insert localsymtable, except for the main procedure
  1572. (in that case the localst is the unit's static symtable,
  1573. which is already on the stack) }
  1574. if procdef.localst.symtablelevel>=normal_function_level then
  1575. symtablestack.push(procdef.localst);
  1576. end;
  1577. procedure tcgprocinfo.remove_from_symtablestack;
  1578. begin
  1579. { remove localsymtable }
  1580. if procdef.localst.symtablelevel>=normal_function_level then
  1581. symtablestack.pop(procdef.localst);
  1582. { remove parasymtable }
  1583. if procdef.parast.symtablelevel>=normal_function_level then
  1584. symtablestack.pop(procdef.parast);
  1585. { remove symtables for the class, but only if it is no nested function }
  1586. if assigned(procdef.struct) and
  1587. not(assigned(parent) and
  1588. assigned(parent.procdef) and
  1589. assigned(parent.procdef.struct)) then
  1590. pop_nested_hierarchy(procdef.struct);
  1591. end;
  1592. procedure tcgprocinfo.resetprocdef;
  1593. begin
  1594. { remove code tree, if not inline procedure }
  1595. if assigned(code) then
  1596. begin
  1597. { the inline procedure has already got a copy of the tree
  1598. stored in procdef.inlininginfo }
  1599. code.free;
  1600. code:=nil;
  1601. end;
  1602. end;
  1603. procedure tcgprocinfo.parse_body;
  1604. var
  1605. old_current_procinfo : tprocinfo;
  1606. old_block_type : tblock_type;
  1607. st : TSymtable;
  1608. old_current_structdef: tabstractrecorddef;
  1609. old_current_genericdef,
  1610. old_current_specializedef: tstoreddef;
  1611. old_parse_generic: boolean;
  1612. begin
  1613. old_current_procinfo:=current_procinfo;
  1614. old_block_type:=block_type;
  1615. old_current_structdef:=current_structdef;
  1616. old_current_genericdef:=current_genericdef;
  1617. old_current_specializedef:=current_specializedef;
  1618. old_parse_generic:=parse_generic;
  1619. current_procinfo:=self;
  1620. current_structdef:=procdef.struct;
  1621. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1622. begin
  1623. current_genericdef:=current_structdef;
  1624. parse_generic:=true;
  1625. end;
  1626. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1627. current_specializedef:=current_structdef;
  1628. { calculate the lexical level }
  1629. if procdef.parast.symtablelevel>maxnesting then
  1630. Message(parser_e_too_much_lexlevel);
  1631. block_type:=bt_body;
  1632. {$ifdef state_tracking}
  1633. { aktstate:=Tstate_storage.create;}
  1634. {$endif state_tracking}
  1635. { allocate the symbol for this procedure }
  1636. alloc_proc_symbol(procdef);
  1637. { add parast/localst to symtablestack }
  1638. add_to_symtablestack;
  1639. { save entry info }
  1640. entrypos:=current_filepos;
  1641. entryswitches:=current_settings.localswitches;
  1642. if (df_generic in procdef.defoptions) then
  1643. begin
  1644. { start token recorder for generic template }
  1645. procdef.initgeneric;
  1646. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1647. end;
  1648. { parse the code ... }
  1649. code:=block(current_module.islibrary);
  1650. if (df_generic in procdef.defoptions) then
  1651. begin
  1652. { stop token recorder for generic template }
  1653. current_scanner.stoprecordtokens;
  1654. { Give an error for accesses in the static symtable that aren't visible
  1655. outside the current unit }
  1656. st:=procdef.owner;
  1657. while (st.symtabletype=ObjectSymtable) do
  1658. st:=st.defowner.owner;
  1659. if (pi_uses_static_symtable in flags) and
  1660. (st.symtabletype<>staticsymtable) then
  1661. Comment(V_Error,'Global Generic template references static symtable');
  1662. end;
  1663. { save exit info }
  1664. exitswitches:=current_settings.localswitches;
  1665. exitpos:=last_endtoken_filepos;
  1666. { the procedure is now defined }
  1667. procdef.forwarddef:=false;
  1668. if assigned(code) then
  1669. begin
  1670. { get a better entry point }
  1671. entrypos:=code.fileinfo;
  1672. { Finish type checking pass }
  1673. do_typecheckpass(code);
  1674. if assigned(procdef.parentfpinitblock) then
  1675. begin
  1676. tblocknode(code).left:=cstatementnode.create(procdef.parentfpinitblock,tblocknode(code).left);
  1677. do_typecheckpass(tblocknode(code).left);
  1678. procdef.parentfpinitblock:=nil;
  1679. end;
  1680. end;
  1681. { Check for unused labels, forwards, symbols for procedures. Static
  1682. symtable is checked in pmodules.
  1683. The check must be done after the typecheckpass }
  1684. if (Errorcount=0) and
  1685. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1686. begin
  1687. { check if forwards are resolved }
  1688. tstoredsymtable(procdef.localst).check_forwards;
  1689. { check if all labels are used }
  1690. tstoredsymtable(procdef.localst).checklabels;
  1691. { check for unused symbols, but only if there is no asm block }
  1692. if not(pi_has_assembler_block in flags) then
  1693. begin
  1694. tstoredsymtable(procdef.localst).allsymbolsused;
  1695. tstoredsymtable(procdef.parast).allsymbolsused;
  1696. end;
  1697. end;
  1698. if (po_inline in procdef.procoptions) and
  1699. { Can we inline this procedure? }
  1700. checknodeinlining(procdef) then
  1701. CreateInlineInfo;
  1702. { Print the node to tree.log }
  1703. if paraprintnodetree=1 then
  1704. printproc( 'after parsing');
  1705. { ... remove symbol tables }
  1706. remove_from_symtablestack;
  1707. {$ifdef state_tracking}
  1708. { aktstate.destroy;}
  1709. {$endif state_tracking}
  1710. current_structdef:=old_current_structdef;
  1711. current_genericdef:=old_current_genericdef;
  1712. current_specializedef:=old_current_specializedef;
  1713. current_procinfo:=old_current_procinfo;
  1714. parse_generic:=old_parse_generic;
  1715. { Restore old state }
  1716. block_type:=old_block_type;
  1717. end;
  1718. {****************************************************************************
  1719. PROCEDURE/FUNCTION PARSING
  1720. ****************************************************************************}
  1721. procedure check_init_paras(p:TObject;arg:pointer);
  1722. begin
  1723. if tsym(p).typ<>paravarsym then
  1724. exit;
  1725. with tparavarsym(p) do
  1726. if (is_managed_type(vardef) and
  1727. (varspez in [vs_value,vs_out])) or
  1728. (is_shortstring(vardef) and
  1729. (varspez=vs_value)) then
  1730. include(current_procinfo.flags,pi_do_call);
  1731. end;
  1732. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1733. {
  1734. Parses the procedure directives, then parses the procedure body, then
  1735. generates the code for it
  1736. }
  1737. var
  1738. oldfailtokenmode : tmodeswitches;
  1739. isnestedproc : boolean;
  1740. begin
  1741. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1742. oldfailtokenmode:=[];
  1743. { create a new procedure }
  1744. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1745. current_module.procinfo:=current_procinfo;
  1746. current_procinfo.procdef:=pd;
  1747. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1748. { Insert mangledname }
  1749. pd.aliasnames.insert(pd.mangledname);
  1750. { Handle Export of this procedure }
  1751. if (po_exports in pd.procoptions) and
  1752. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1753. begin
  1754. pd.aliasnames.insert(pd.procsym.realname);
  1755. if cs_link_deffile in current_settings.globalswitches then
  1756. deffile.AddExport(pd.mangledname);
  1757. end;
  1758. { Insert result variables in the localst }
  1759. insert_funcret_local(pd);
  1760. { check if there are para's which require initing -> set }
  1761. { pi_do_call (if not yet set) }
  1762. if not(pi_do_call in current_procinfo.flags) then
  1763. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1764. { set _FAIL as keyword if constructor }
  1765. if (pd.proctypeoption=potype_constructor) then
  1766. begin
  1767. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1768. tokeninfo^[_FAIL].keyword:=alllanguagemodes;
  1769. end;
  1770. tcgprocinfo(current_procinfo).parse_body;
  1771. { We can't support inlining for procedures that have nested
  1772. procedures because the nested procedures use a fixed offset
  1773. for accessing locals in the parent procedure (PFV) }
  1774. if current_procinfo.has_nestedprocs then
  1775. begin
  1776. if (df_generic in current_procinfo.procdef.defoptions) then
  1777. Comment(V_Error,'Generic methods cannot have nested procedures')
  1778. else
  1779. if (po_inline in current_procinfo.procdef.procoptions) then
  1780. begin
  1781. Message1(parser_h_not_supported_for_inline,'nested procedures');
  1782. Message(parser_h_inlining_disabled);
  1783. exclude(current_procinfo.procdef.procoptions,po_inline);
  1784. end;
  1785. end;
  1786. { When it's a nested procedure then defer the code generation,
  1787. when back at normal function level then generate the code
  1788. for all defered nested procedures and the current procedure }
  1789. if not isnestedproc then
  1790. begin
  1791. if not(df_generic in current_procinfo.procdef.defoptions) then
  1792. tcgprocinfo(current_procinfo).generate_code_tree;
  1793. end;
  1794. { reset _FAIL as _SELF normal }
  1795. if (pd.proctypeoption=potype_constructor) then
  1796. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1797. { release procinfo }
  1798. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1799. internalerror(200304274);
  1800. current_module.procinfo:=current_procinfo.parent;
  1801. { For specialization we didn't record the last semicolon. Moving this parsing
  1802. into the parse_body routine is not done because of having better file position
  1803. information available }
  1804. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1805. consume(_SEMICOLON);
  1806. if not isnestedproc then
  1807. { current_procinfo is checked for nil later on }
  1808. freeandnil(current_procinfo);
  1809. end;
  1810. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef);
  1811. {
  1812. Parses the procedure directives, then parses the procedure body, then
  1813. generates the code for it
  1814. }
  1815. var
  1816. old_current_procinfo : tprocinfo;
  1817. old_current_structdef: tabstractrecorddef;
  1818. old_current_genericdef,
  1819. old_current_specializedef: tstoreddef;
  1820. pdflags : tpdflags;
  1821. pd,firstpd : tprocdef;
  1822. s : string;
  1823. begin
  1824. { save old state }
  1825. old_current_procinfo:=current_procinfo;
  1826. old_current_structdef:=current_structdef;
  1827. old_current_genericdef:=current_genericdef;
  1828. old_current_specializedef:=current_specializedef;
  1829. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1830. to another procdef }
  1831. current_procinfo:=nil;
  1832. current_structdef:=nil;
  1833. current_genericdef:=nil;
  1834. current_specializedef:=nil;
  1835. if not assigned(usefwpd) then
  1836. { parse procedure declaration }
  1837. pd:=parse_proc_dec(isclassmethod,old_current_structdef)
  1838. else
  1839. pd:=usefwpd;
  1840. { set the default function options }
  1841. if parse_only then
  1842. begin
  1843. pd.forwarddef:=true;
  1844. { set also the interface flag, for better error message when the
  1845. implementation doesn't much this header }
  1846. pd.interfacedef:=true;
  1847. include(pd.procoptions,po_global);
  1848. pdflags:=[pd_interface];
  1849. end
  1850. else
  1851. begin
  1852. pdflags:=[pd_body];
  1853. if (not current_module.in_interface) then
  1854. include(pdflags,pd_implemen);
  1855. if (not current_module.is_unit) or
  1856. create_smartlink then
  1857. include(pd.procoptions,po_global);
  1858. pd.forwarddef:=false;
  1859. end;
  1860. if not assigned(usefwpd) then
  1861. begin
  1862. { parse the directives that may follow }
  1863. parse_proc_directives(pd,pdflags);
  1864. { hint directives, these can be separated by semicolons here,
  1865. that needs to be handled here with a loop (PFV) }
  1866. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1867. Consume(_SEMICOLON);
  1868. { Set calling convention }
  1869. handle_calling_convention(pd);
  1870. end;
  1871. { search for forward declarations }
  1872. if not proc_add_definition(pd) then
  1873. begin
  1874. { A method must be forward defined (in the object declaration) }
  1875. if assigned(pd.struct) and
  1876. (not assigned(old_current_structdef)) then
  1877. begin
  1878. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1879. tprocsym(pd.procsym).write_parameter_lists(pd);
  1880. end
  1881. else
  1882. begin
  1883. { Give a better error if there is a forward def in the interface and only
  1884. a single implementation }
  1885. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1886. if (not pd.forwarddef) and
  1887. (not pd.interfacedef) and
  1888. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1889. firstpd.forwarddef and
  1890. firstpd.interfacedef and
  1891. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1892. { don't give an error if it may be an overload }
  1893. not(m_fpc in current_settings.modeswitches) and
  1894. (not(po_overload in pd.procoptions) or
  1895. not(po_overload in firstpd.procoptions)) then
  1896. begin
  1897. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1898. tprocsym(pd.procsym).write_parameter_lists(pd);
  1899. end;
  1900. end;
  1901. end;
  1902. { Set mangled name }
  1903. proc_set_mangledname(pd);
  1904. { compile procedure when a body is needed }
  1905. if (pd_body in pdflags) then
  1906. begin
  1907. read_proc_body(old_current_procinfo,pd);
  1908. end
  1909. else
  1910. begin
  1911. { Handle imports }
  1912. if (po_external in pd.procoptions) then
  1913. begin
  1914. { Import DLL specified? }
  1915. if assigned(pd.import_dll) then
  1916. begin
  1917. if assigned (pd.import_name) then
  1918. current_module.AddExternalImport(pd.import_dll^,
  1919. pd.import_name^,proc_get_importname(pd),
  1920. pd.import_nr,false,false)
  1921. else
  1922. current_module.AddExternalImport(pd.import_dll^,
  1923. proc_get_importname(pd),proc_get_importname(pd),
  1924. pd.import_nr,false,true);
  1925. end
  1926. else
  1927. begin
  1928. { add import name to external list for DLL scanning }
  1929. if tf_has_dllscanner in target_info.flags then
  1930. current_module.dllscannerinputlist.Add(proc_get_importname(pd),pd);
  1931. end;
  1932. { External declared in implementation, and there was already a
  1933. forward (or interface) declaration then we need to generate
  1934. a stub that calls the external routine }
  1935. if (not pd.forwarddef) and
  1936. (pd.hasforward)
  1937. { it is unclear to me what's the use of the following condition,
  1938. so commented out, see also issue #18371 (FK)
  1939. and
  1940. not(
  1941. assigned(pd.import_dll) and
  1942. (target_info.system in [system_i386_wdosx,
  1943. system_arm_wince,system_i386_wince])
  1944. ) } then
  1945. begin
  1946. s:=proc_get_importname(pd);
  1947. if s<>'' then
  1948. gen_external_stub(current_asmdata.asmlists[al_procedures],pd,s);
  1949. { remove the external stuff, so that the interface crc
  1950. doesn't change. This makes the function calls less
  1951. efficient, but it means that the interface doesn't
  1952. change if the function is ever redirected to another
  1953. function or implemented in the unit. }
  1954. pd.procoptions:=pd.procoptions-[po_external,po_has_importname,po_has_importdll];
  1955. stringdispose(pd.import_name);
  1956. stringdispose(pd.import_dll);
  1957. pd.import_nr:=0;
  1958. end;
  1959. end;
  1960. end;
  1961. { make sure that references to forward-declared functions are not }
  1962. { treated as references to external symbols, needed for darwin. }
  1963. { make sure we don't change the binding of real external symbols }
  1964. if not(po_external in pd.procoptions) then
  1965. begin
  1966. if (po_global in pd.procoptions) or
  1967. (cs_profile in current_settings.moduleswitches) then
  1968. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION)
  1969. else
  1970. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  1971. end;
  1972. current_structdef:=old_current_structdef;
  1973. current_genericdef:=old_current_genericdef;
  1974. current_specializedef:=old_current_specializedef;
  1975. current_procinfo:=old_current_procinfo;
  1976. end;
  1977. {****************************************************************************
  1978. DECLARATION PARSING
  1979. ****************************************************************************}
  1980. { search in symtablestack for not complete classes }
  1981. procedure check_forward_class(p:TObject;arg:pointer);
  1982. begin
  1983. if (tsym(p).typ=typesym) and
  1984. (ttypesym(p).typedef.typ=objectdef) and
  1985. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  1986. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1987. end;
  1988. procedure read_declarations(islibrary : boolean);
  1989. var
  1990. is_classdef:boolean;
  1991. begin
  1992. is_classdef:=false;
  1993. repeat
  1994. if not assigned(current_procinfo) then
  1995. internalerror(200304251);
  1996. case token of
  1997. _LABEL:
  1998. label_dec;
  1999. _CONST:
  2000. const_dec;
  2001. _TYPE:
  2002. type_dec;
  2003. _VAR:
  2004. var_dec;
  2005. _THREADVAR:
  2006. threadvar_dec;
  2007. _CLASS:
  2008. begin
  2009. is_classdef:=false;
  2010. if try_to_consume(_CLASS) then
  2011. begin
  2012. { class modifier is only allowed for procedures, functions, }
  2013. { constructors, destructors }
  2014. if not(token in [_FUNCTION,_PROCEDURE,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  2015. not((token=_ID) and (idtoken=_OPERATOR)) then
  2016. Message(parser_e_procedure_or_function_expected);
  2017. if is_interface(current_structdef) then
  2018. Message(parser_e_no_static_method_in_interfaces)
  2019. else
  2020. { class methods are also allowed for Objective-C protocols }
  2021. is_classdef:=true;
  2022. end;
  2023. end;
  2024. _CONSTRUCTOR,
  2025. _DESTRUCTOR,
  2026. _FUNCTION,
  2027. _PROCEDURE,
  2028. _OPERATOR:
  2029. begin
  2030. read_proc(is_classdef,nil);
  2031. is_classdef:=false;
  2032. end;
  2033. _EXPORTS:
  2034. begin
  2035. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  2036. begin
  2037. Message(parser_e_syntax_error);
  2038. consume_all_until(_SEMICOLON);
  2039. end
  2040. else if islibrary or
  2041. (target_info.system in systems_unit_program_exports) then
  2042. read_exports
  2043. else
  2044. begin
  2045. Message(parser_w_unsupported_feature);
  2046. consume(_BEGIN);
  2047. end;
  2048. end;
  2049. _PROPERTY:
  2050. begin
  2051. if (m_fpc in current_settings.modeswitches) then
  2052. property_dec
  2053. else
  2054. break;
  2055. end;
  2056. else
  2057. begin
  2058. case idtoken of
  2059. _RESOURCESTRING:
  2060. begin
  2061. { m_class is needed, because the resourcestring
  2062. loading is in the ObjPas unit }
  2063. { if (m_class in current_settings.modeswitches) then}
  2064. resourcestring_dec
  2065. { else
  2066. break;}
  2067. end;
  2068. _OPERATOR:
  2069. begin
  2070. if is_classdef then
  2071. begin
  2072. read_proc(is_classdef,nil);
  2073. is_classdef:=false;
  2074. end
  2075. else
  2076. break;
  2077. end;
  2078. else
  2079. break;
  2080. end;
  2081. end;
  2082. end;
  2083. until false;
  2084. { add implementations for synthetic method declarations added by
  2085. the compiler (not for unit/program init functions, their localst
  2086. is the staticst -> would duplicate the work done in pmodules) }
  2087. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  2088. add_synthetic_method_implementations(current_procinfo.procdef.localst);
  2089. { check for incomplete class definitions, this is only required
  2090. for fpc modes }
  2091. if (m_fpc in current_settings.modeswitches) then
  2092. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  2093. end;
  2094. procedure read_interface_declarations;
  2095. begin
  2096. repeat
  2097. case token of
  2098. _CONST :
  2099. const_dec;
  2100. _TYPE :
  2101. type_dec;
  2102. _VAR :
  2103. var_dec;
  2104. _THREADVAR :
  2105. threadvar_dec;
  2106. _FUNCTION,
  2107. _PROCEDURE,
  2108. _OPERATOR :
  2109. read_proc(false,nil);
  2110. else
  2111. begin
  2112. case idtoken of
  2113. _RESOURCESTRING :
  2114. resourcestring_dec;
  2115. _PROPERTY:
  2116. begin
  2117. if (m_fpc in current_settings.modeswitches) then
  2118. property_dec
  2119. else
  2120. break;
  2121. end;
  2122. else
  2123. break;
  2124. end;
  2125. end;
  2126. end;
  2127. until false;
  2128. { check for incomplete class definitions, this is only required
  2129. for fpc modes }
  2130. if (m_fpc in current_settings.modeswitches) then
  2131. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  2132. end;
  2133. {****************************************************************************
  2134. SPECIALIZATION BODY GENERATION
  2135. ****************************************************************************}
  2136. procedure specialize_objectdefs(p:TObject;arg:pointer);
  2137. var
  2138. oldcurrent_filepos : tfileposinfo;
  2139. specobj : tabstractrecorddef;
  2140. state : tspecializationstate;
  2141. procedure process_abstractrecorddef(def:tabstractrecorddef);
  2142. var
  2143. i : longint;
  2144. hp : tdef;
  2145. hmodule : tmodule;
  2146. begin
  2147. hmodule:=find_module_from_symtable(def.genericdef.owner);
  2148. if hmodule=nil then
  2149. internalerror(201202041);
  2150. for i:=0 to def.symtable.DefList.Count-1 do
  2151. begin
  2152. hp:=tdef(def.symtable.DefList[i]);
  2153. if hp.typ=procdef then
  2154. begin
  2155. { only generate the code if we need a body }
  2156. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  2157. continue;
  2158. if assigned(tprocdef(hp).genericdef) and
  2159. (tprocdef(hp).genericdef.typ=procdef) and
  2160. assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf) then
  2161. begin
  2162. oldcurrent_filepos:=current_filepos;
  2163. current_filepos:=tprocdef(tprocdef(hp).genericdef).fileinfo;
  2164. { use the index the module got from the current compilation process }
  2165. current_filepos.moduleindex:=hmodule.unit_index;
  2166. current_tokenpos:=current_filepos;
  2167. current_scanner.startreplaytokens(tprocdef(tprocdef(hp).genericdef).generictokenbuf);
  2168. read_proc_body(nil,tprocdef(hp));
  2169. current_filepos:=oldcurrent_filepos;
  2170. end
  2171. { synthetic routines will be implemented afterwards }
  2172. else if tprocdef(hp).synthetickind=tsk_none then
  2173. MessagePos1(tprocdef(hp).fileinfo,sym_e_forward_not_resolved,tprocdef(hp).fullprocname(false));
  2174. end
  2175. else
  2176. if hp.typ in [objectdef,recorddef] then
  2177. { generate code for subtypes as well }
  2178. process_abstractrecorddef(tabstractrecorddef(hp));
  2179. end;
  2180. end;
  2181. begin
  2182. if not((tsym(p).typ=typesym) and
  2183. (ttypesym(p).typedef.typesym=tsym(p)) and
  2184. (ttypesym(p).typedef.typ in [objectdef,recorddef]) and
  2185. (df_specialization in ttypesym(p).typedef.defoptions)
  2186. ) then
  2187. exit;
  2188. { Setup symtablestack a definition time }
  2189. specobj:=tabstractrecorddef(ttypesym(p).typedef);
  2190. if not (is_class_or_object(specobj) or is_record(specobj) or is_javaclass(specobj)) then
  2191. exit;
  2192. specialization_init(specobj.genericdef,state);
  2193. { procedure definitions for classes or objects }
  2194. process_abstractrecorddef(specobj);
  2195. specialization_done(state);
  2196. end;
  2197. procedure generate_specialization_procs;
  2198. begin
  2199. if assigned(current_module.globalsymtable) then
  2200. current_module.globalsymtable.SymList.WhileEachCall(@specialize_objectdefs,nil);
  2201. if assigned(current_module.localsymtable) then
  2202. current_module.localsymtable.SymList.WhileEachCall(@specialize_objectdefs,nil);
  2203. end;
  2204. end.