psub.pas 98 KB

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