psub.pas 98 KB

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