psub.pas 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  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,
  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(po_assembler in procdef.procoptions) and
  813. not(target_info.system in systems_garbage_collected_managed_types) then
  814. begin
  815. { Any result of managed type must be returned in parameter }
  816. if is_managed_type(procdef.returndef) and
  817. (not paramanager.ret_in_param(procdef.returndef,procdef)) and
  818. (not is_class(procdef.returndef)) then
  819. InternalError(2013121301);
  820. { Generate special exception block only needed when
  821. implicit finaly is used }
  822. current_filepos:=exitpos;
  823. { Generate code that will be in the try...finally }
  824. finalcode:=internalstatements(codestatement);
  825. addstatement(codestatement,final_asmnode);
  826. final_used:=true;
  827. current_filepos:=entrypos;
  828. wrappedbody:=ctryfinallynode.create_implicit(
  829. code,
  830. finalcode,
  831. cnothingnode.create);
  832. { afterconstruction must be called after final_asmnode, because it
  833. has to execute after the temps have been finalised in case of a
  834. refcounted class (afterconstruction decreases the refcount
  835. without freeing the instance if the count becomes nil, while
  836. the finalising of the temps can free the instance) }
  837. maybe_add_constructor_wrapper(wrappedbody,true);
  838. addstatement(newstatement,wrappedbody);
  839. addstatement(newstatement,exitlabel_asmnode);
  840. addstatement(newstatement,bodyexitcode);
  841. { set flag the implicit finally has been generated }
  842. include(flags,pi_has_implicit_finally);
  843. end
  844. else
  845. begin
  846. { constructors need destroy-on-exception code even if they don't
  847. have managed variables/temps }
  848. maybe_add_constructor_wrapper(code,
  849. cs_implicit_exceptions in current_settings.moduleswitches);
  850. addstatement(newstatement,code);
  851. if assigned(nestedexitlabel) then
  852. addstatement(newstatement,clabelnode.create(cnothingnode.create,nestedexitlabel));
  853. addstatement(newstatement,exitlabel_asmnode);
  854. addstatement(newstatement,bodyexitcode);
  855. if not is_constructor then
  856. begin
  857. addstatement(newstatement,final_asmnode);
  858. final_used:=true;
  859. end;
  860. end;
  861. do_firstpass(newblock);
  862. code:=newblock;
  863. current_filepos:=oldfilepos;
  864. end;
  865. procedure clearrefs(p:TObject;arg:pointer);
  866. begin
  867. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  868. if tabstractvarsym(p).refs>1 then
  869. tabstractvarsym(p).refs:=1;
  870. end;
  871. procedure translate_registers(p:TObject;list:pointer);
  872. begin
  873. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  874. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  875. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  876. begin
  877. if not(cs_no_regalloc in current_settings.globalswitches) then
  878. begin
  879. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  880. if (tabstractnormalvarsym(p).localloc.registerhi<>NR_NO) then
  881. cg.translate_register(tabstractnormalvarsym(p).localloc.registerhi);
  882. end;
  883. if cs_asm_source in current_settings.globalswitches then
  884. begin
  885. TAsmList(list).concat(Tai_comment.Create(strpnew('Var '+tabstractnormalvarsym(p).realname+' located in register '+
  886. location_reg2string(tabstractnormalvarsym(p).localloc))));
  887. end;
  888. end;
  889. end;
  890. const
  891. exception_flags: array[boolean] of tprocinfoflags = (
  892. [],
  893. [pi_uses_exceptions,pi_needs_implicit_finally,pi_has_implicit_finally]
  894. );
  895. procedure tcgprocinfo.setup_tempgen;
  896. begin
  897. tg:=tgobjclass.create;
  898. {$if defined(i386) or defined(x86_64) or defined(arm)}
  899. {$if defined(arm)}
  900. { frame and stack pointer must be always the same on arm thumb so it makes no
  901. sense to fiddle with a frame pointer }
  902. if GenerateThumbCode then
  903. begin
  904. framepointer:=NR_STACK_POINTER_REG;
  905. tg.direction:=1;
  906. end
  907. else
  908. {$endif defined(arm)}
  909. begin
  910. { try to strip the stack frame }
  911. { set the framepointer to esp if:
  912. - no assembler directive, those are handled in assembler_block
  913. in pstatment.pas (for cases not caught by the Delphi
  914. exception below)
  915. - no exceptions are used
  916. - no pushes are used/esp modifications, could be:
  917. * outgoing parameters on the stack on non-fixed stack target
  918. * incoming parameters on the stack
  919. * open arrays
  920. - no inline assembler
  921. or
  922. - Delphi mode
  923. - assembler directive
  924. - no pushes are used/esp modifications, could be:
  925. * outgoing parameters on the stack
  926. * incoming parameters on the stack
  927. * open arrays
  928. - no local variables
  929. - stack frame cannot be optimized if using Win64 SEH
  930. (at least with the current state of our codegenerator).
  931. }
  932. if ((po_assembler in procdef.procoptions) and
  933. (m_delphi in current_settings.modeswitches) and
  934. { localst at main_program_level is a staticsymtable }
  935. (procdef.localst.symtablelevel<>main_program_level) and
  936. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  937. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  938. not(cs_generate_stackframes in current_settings.localswitches) and
  939. not(cs_profile in current_settings.moduleswitches) and
  940. not(po_assembler in procdef.procoptions) and
  941. not ((pi_has_stackparameter in flags)
  942. {$ifndef arm} { Outgoing parameter(s) on stack do not need stackframe on x86 targets
  943. with fixed stack. On ARM it fails, see bug #25050 }
  944. and (not paramanager.use_fixed_stack)
  945. {$endif arm}
  946. ) and
  947. ((flags*([pi_has_assembler_block,pi_is_assembler,
  948. pi_needs_stackframe]+
  949. exception_flags[(target_info.cpu=cpu_i386)
  950. {$ifndef DISABLE_WIN64_SEH}
  951. or (target_info.system=system_x86_64_win64)
  952. {$endif DISABLE_WIN64_SEH}
  953. ]))=[])
  954. )
  955. then
  956. begin
  957. { we need the parameter info here to determine if the procedure gets
  958. parameters on the stack
  959. calling generate_parameter_info doesn't hurt but it costs time
  960. (necessary to init para_stack_size)
  961. }
  962. generate_parameter_info;
  963. if not(procdef.stack_tainting_parameter(calleeside)) and
  964. not(has_assembler_child) and (para_stack_size=0) then
  965. begin
  966. { Only need to set the framepointer }
  967. framepointer:=NR_STACK_POINTER_REG;
  968. tg.direction:=1;
  969. end
  970. {$if defined(arm)}
  971. { On arm, the stack frame size can be estimated to avoid using an extra frame pointer,
  972. in case parameters are passed on the stack.
  973. However, the draw back is, if the estimation fails, compilation will break later on
  974. with an internal error, so this switch is not enabled by default yet. To overcome this,
  975. multipass compilation of subroutines must be supported
  976. }
  977. else if (cs_opt_forcenostackframe in current_settings.optimizerswitches) and
  978. not(has_assembler_child) then
  979. begin
  980. { Only need to set the framepointer }
  981. framepointer:=NR_STACK_POINTER_REG;
  982. tg.direction:=1;
  983. include(flags,pi_estimatestacksize);
  984. set_first_temp_offset;
  985. procdef.has_paraloc_info:=callnoside;
  986. generate_parameter_info;
  987. exit;
  988. end;
  989. {$endif defined(arm)}
  990. end;
  991. end;
  992. {$endif defined(x86) or defined(arm)}
  993. { set the start offset to the start of the temp area in the stack }
  994. set_first_temp_offset;
  995. end;
  996. function tcgprocinfo.has_assembler_child : boolean;
  997. var
  998. hp : tprocinfo;
  999. begin
  1000. result:=false;
  1001. hp:=get_first_nestedproc;
  1002. while assigned(hp) do
  1003. begin
  1004. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  1005. begin
  1006. result:=true;
  1007. exit;
  1008. end;
  1009. hp:=tprocinfo(hp.next);
  1010. end;
  1011. end;
  1012. procedure tcgprocinfo.generate_code_tree;
  1013. var
  1014. hpi : tcgprocinfo;
  1015. begin
  1016. { generate code for this procedure }
  1017. generate_code;
  1018. { process nested procedures }
  1019. hpi:=tcgprocinfo(get_first_nestedproc);
  1020. while assigned(hpi) do
  1021. begin
  1022. hpi.generate_code_tree;
  1023. hpi:=tcgprocinfo(hpi.next);
  1024. end;
  1025. resetprocdef;
  1026. end;
  1027. { For SEH, the code from 'finally' blocks must be put into a separate procedures,
  1028. which can be called by OS during stack unwind. This resembles nested procedures,
  1029. but finalizer procedures do not have their own local variables and work directly
  1030. with the stack frame of parent. In particular, the tempgen must be shared, so
  1031. 1) finalizer procedure is able to finalize temps of the parent,
  1032. 2) if the finalizer procedure is complex enough to need its own temps, they are
  1033. allocated in stack frame of parent, so second-level finalizer procedures are
  1034. not needed.
  1035. Due to requirement of shared tempgen we cannot process finalizer as a regular nested
  1036. procedure (after the parent) and have to do it inline.
  1037. This is called by platform-specific tryfinallynodes during pass2.
  1038. Here we put away the codegen (which carries the register allocator state), process
  1039. the 'nested' procedure, then restore previous cg and continue processing the parent
  1040. procedure. generate_code() will create another cg, but not another tempgen because
  1041. setup_tempgen() is not called for potype_exceptfilter procedures. }
  1042. procedure tcgprocinfo.generate_exceptfilter(nestedpi: tcgprocinfo);
  1043. var
  1044. saved_cg: tcg;
  1045. saved_hlcg: thlcgobj;
  1046. {$ifdef cpu64bitalu}
  1047. saved_cg128 : tcg128;
  1048. {$else cpu64bitalu}
  1049. saved_cg64 : tcg64;
  1050. {$endif cpu64bitalu}
  1051. begin
  1052. if nestedpi.procdef.proctypeoption<>potype_exceptfilter then
  1053. InternalError(201201141);
  1054. { flush code generated this far }
  1055. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1056. { save the codegen }
  1057. saved_cg:=cg;
  1058. saved_hlcg:=hlcg;
  1059. cg:=nil;
  1060. hlcg:=nil;
  1061. {$ifdef cpu64bitalu}
  1062. saved_cg128:=cg128;
  1063. cg128:=nil;
  1064. {$else cpu64bitalu}
  1065. saved_cg64:=cg64;
  1066. cg64:=nil;
  1067. {$endif cpu64bitalu}
  1068. nestedpi.generate_code;
  1069. { prevents generating code the second time when processing nested procedures }
  1070. nestedpi.resetprocdef;
  1071. cg:=saved_cg;
  1072. hlcg:=saved_hlcg;
  1073. {$ifdef cpu64bitalu}
  1074. cg128:=saved_cg128;
  1075. {$else cpu64bitalu}
  1076. cg64:=saved_cg64;
  1077. {$endif cpu64bitalu}
  1078. add_reg_instruction_hook:[email protected]_reg_instruction;
  1079. end;
  1080. procedure TCGProcinfo.CreateInlineInfo;
  1081. begin
  1082. new(procdef.inlininginfo);
  1083. procdef.inlininginfo^.code:=code.getcopy;
  1084. procdef.inlininginfo^.flags:=flags;
  1085. { The blocknode needs to set an exit label }
  1086. if procdef.inlininginfo^.code.nodetype=blockn then
  1087. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1088. procdef.has_inlininginfo:=true;
  1089. end;
  1090. function searchusercode(var n: tnode; arg: pointer): foreachnoderesult;
  1091. begin
  1092. if nf_usercode_entry in n.flags then
  1093. begin
  1094. pnode(arg)^:=n;
  1095. result:=fen_norecurse_true
  1096. end
  1097. else
  1098. result:=fen_false;
  1099. end;
  1100. function TCGProcinfo.GetUserCode : tnode;
  1101. var
  1102. n : tnode;
  1103. begin
  1104. n:=nil;
  1105. foreachnodestatic(code,@searchusercode,@n);
  1106. if not(assigned(n)) then
  1107. internalerror(2013111004);
  1108. result:=n;
  1109. end;
  1110. procedure tcgprocinfo.generate_code;
  1111. var
  1112. old_current_procinfo : tprocinfo;
  1113. oldmaxfpuregisters : longint;
  1114. oldfilepos : tfileposinfo;
  1115. old_current_structdef : tabstractrecorddef;
  1116. templist : TAsmList;
  1117. headertai : tai;
  1118. i : integer;
  1119. varsym : tabstractnormalvarsym;
  1120. {RedoDFA : boolean;}
  1121. procedure delete_marker(anode: tasmnode);
  1122. var
  1123. ai: tai;
  1124. begin
  1125. if assigned(anode) then
  1126. begin
  1127. ai:=anode.currenttai;
  1128. if assigned(ai) then
  1129. begin
  1130. aktproccode.remove(ai);
  1131. ai.free;
  1132. anode.currenttai:=nil;
  1133. end;
  1134. end;
  1135. end;
  1136. begin
  1137. { the initialization procedure can be empty, then we
  1138. don't need to generate anything. When it was an empty
  1139. procedure there would be at least a blocknode }
  1140. if not assigned(code) then
  1141. exit;
  1142. { We need valid code }
  1143. if Errorcount<>0 then
  1144. exit;
  1145. { No code can be generated for generic template }
  1146. if (df_generic in procdef.defoptions) then
  1147. internalerror(200511152);
  1148. { For regular procedures the RA and Tempgen shall not be available yet,
  1149. but exception filters reuse Tempgen of parent }
  1150. if assigned(tg)<>(procdef.proctypeoption=potype_exceptfilter) then
  1151. internalerror(200309201);
  1152. old_current_procinfo:=current_procinfo;
  1153. oldfilepos:=current_filepos;
  1154. old_current_structdef:=current_structdef;
  1155. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  1156. current_procinfo:=self;
  1157. current_filepos:=entrypos;
  1158. current_structdef:=procdef.struct;
  1159. { store start of user code, it must be a block node, it will be used later one to
  1160. check variable lifeness }
  1161. include(code.flags,nf_usercode_entry);
  1162. { add wrapping code if necessary (initialization of typed constants on
  1163. some platforms, initing of local variables and out parameters with
  1164. trashing values, ...) }
  1165. { init/final code must be wrapped later (after code for main proc body
  1166. has been generated) }
  1167. if not(current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1168. code:=cnodeutils.wrap_proc_body(procdef,code);
  1169. { automatic inlining? }
  1170. if (cs_opt_autoinline in current_settings.optimizerswitches) and
  1171. { inlining not turned off? }
  1172. (cs_do_inline in current_settings.localswitches) and
  1173. { no inlining yet? }
  1174. not(procdef.has_inlininginfo) and not(has_nestedprocs) and
  1175. not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize,potype_constructor,
  1176. potype_destructor,potype_class_constructor,potype_class_destructor]) and
  1177. ((procdef.procoptions*[po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck])=[]) and
  1178. (not(procdef.proccalloption in [pocall_safecall])) and
  1179. { rough approximation if we should auto inline }
  1180. (node_count(code)<=10) then
  1181. begin
  1182. { Can we inline this procedure? }
  1183. if checknodeinlining(procdef) then
  1184. begin
  1185. Message1(cg_d_autoinlining,procdef.GetTypeName);
  1186. include(procdef.procoptions,po_inline);
  1187. CreateInlineInfo;
  1188. end;
  1189. end;
  1190. templist:=TAsmList.create;
  1191. { add parast/localst to symtablestack }
  1192. add_to_symtablestack;
  1193. { clear register count }
  1194. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  1195. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  1196. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  1197. if (procdef.localst.symtablelevel=main_program_level) and
  1198. (not current_module.is_unit) then
  1199. begin
  1200. include(flags,pi_do_call);
  1201. { the main program never returns due to the do_exit call }
  1202. if not(DLLsource) then
  1203. include(procdef.procoptions,po_noreturn);
  1204. end;
  1205. { set implicit_finally flag when there are locals/paras to be finalized }
  1206. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  1207. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  1208. {$ifdef SUPPORT_SAFECALL}
  1209. { set implicit_finally flag for if procedure is safecall }
  1210. if (tf_safecall_exceptions in target_info.flags) and
  1211. (procdef.proccalloption=pocall_safecall) then
  1212. include(flags, pi_needs_implicit_finally);
  1213. {$endif}
  1214. { firstpass everything }
  1215. flowcontrol:=[];
  1216. do_firstpass(code);
  1217. {$if defined(i386) or defined(i8086)}
  1218. if node_resources_fpu(code)>0 then
  1219. include(flags,pi_uses_fpu);
  1220. {$endif i386 or i8086}
  1221. { Print the node to tree.log }
  1222. if paraprintnodetree=1 then
  1223. printproc( 'after the firstpass');
  1224. { do this before adding the entry code else the tail recursion recognition won't work,
  1225. if this causes troubles, it must be if'ed
  1226. }
  1227. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  1228. (pi_is_recursive in flags) then
  1229. do_opttail(code,procdef);
  1230. if cs_opt_constant_propagate in current_settings.optimizerswitches then
  1231. do_optconstpropagate(code);
  1232. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  1233. { creating dfa is not always possible }
  1234. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler])=[]) then
  1235. begin
  1236. dfabuilder:=TDFABuilder.Create;
  1237. dfabuilder.createdfainfo(code);
  1238. include(flags,pi_dfaavailable);
  1239. { when life info is available, we can give more sophisticated warning about uninitialized
  1240. variables ...
  1241. ... but not for the finalization section of a unit, we would need global dfa to handle
  1242. it properly }
  1243. if potype_unitfinalize<>procdef.proctypeoption then
  1244. { iterate through life info of the first node }
  1245. for i:=0 to dfabuilder.nodemap.count-1 do
  1246. begin
  1247. if DFASetIn(GetUserCode.optinfo^.life,i) then
  1248. begin
  1249. { do not warn for certain parameters: }
  1250. if not((tnode(dfabuilder.nodemap[i]).nodetype=loadn) and (tloadnode(dfabuilder.nodemap[i]).symtableentry.typ=paravarsym) and
  1251. { do not warn about parameters passed by var }
  1252. (((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varspez=vs_var) and
  1253. { function result is passed by var but it must be initialized }
  1254. not(vo_is_funcret in tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions)) or
  1255. { do not warn about initialized hidden parameters }
  1256. ((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions*[vo_is_high_para,vo_is_parentfp,vo_is_result,vo_is_self])<>[]))) then
  1257. CheckAndWarn(GetUserCode,tnode(dfabuilder.nodemap[i]));
  1258. end
  1259. else
  1260. begin
  1261. if (tnode(dfabuilder.nodemap[i]).nodetype=loadn) and
  1262. (tloadnode(dfabuilder.nodemap[i]).symtableentry.typ in [staticvarsym,localvarsym]) then
  1263. tabstractnormalvarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).noregvarinitneeded:=true
  1264. end;
  1265. end;
  1266. end;
  1267. if (pi_dfaavailable in flags) and (cs_opt_dead_store_eliminate in current_settings.optimizerswitches) then
  1268. do_optdeadstoreelim(code);
  1269. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  1270. { our induction variable strength reduction doesn't like
  1271. for loops with more than one entry }
  1272. and not(pi_has_label in flags) then
  1273. begin
  1274. {RedoDFA:=}OptimizeInductionVariables(code);
  1275. end;
  1276. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  1277. (procdef.proctypeoption in [potype_operator,potype_procedure,potype_function]) and
  1278. (code.nodetype=blockn) and (tblocknode(code).statements=nil) then
  1279. procdef.isempty:=true;
  1280. { add implicit entry and exit code }
  1281. add_entry_exit_code;
  1282. if cs_opt_nodecse in current_settings.optimizerswitches then
  1283. do_optcse(code);
  1284. { only do secondpass if there are no errors }
  1285. if (ErrorCount=0) then
  1286. begin
  1287. create_hlcodegen;
  1288. if (procdef.proctypeoption<>potype_exceptfilter) then
  1289. setup_tempgen;
  1290. { Create register allocator, must come after framepointer is known }
  1291. hlcg.init_register_allocators;
  1292. generate_parameter_info;
  1293. { allocate got register if needed }
  1294. allocate_got_register(aktproccode);
  1295. { Allocate space in temp/registers for parast and localst }
  1296. current_filepos:=entrypos;
  1297. gen_alloc_symtable(aktproccode,procdef,procdef.parast);
  1298. gen_alloc_symtable(aktproccode,procdef,procdef.localst);
  1299. { Store temp offset for information about 'real' temps }
  1300. tempstart:=tg.lasttemp;
  1301. { Generate code to load register parameters in temps and insert local
  1302. copies for values parameters. This must be done before the code for the
  1303. body is generated because the localloc is updated.
  1304. Note: The generated code will be inserted after the code generation of
  1305. the body is finished, because only then the position is known }
  1306. {$ifdef oldregvars}
  1307. assign_regvars(code);
  1308. {$endif oldreg}
  1309. current_filepos:=entrypos;
  1310. hlcg.gen_load_para_value(templist);
  1311. { caller paraloc info is also necessary in the stackframe_entry
  1312. code of the ppc (and possibly other processors) }
  1313. procdef.init_paraloc_info(callerside);
  1314. { generate code for the node tree }
  1315. do_secondpass(code);
  1316. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1317. { The position of the loadpara_asmnode is now known }
  1318. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  1319. { first generate entry and initialize code with the correct
  1320. position and switches }
  1321. current_filepos:=entrypos;
  1322. current_settings.localswitches:=entryswitches;
  1323. cg.set_regalloc_live_range_direction(rad_backwards);
  1324. hlcg.gen_entry_code(templist);
  1325. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  1326. hlcg.gen_initialize_code(templist);
  1327. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  1328. { now generate finalize and exit code with the correct position
  1329. and switches }
  1330. current_filepos:=exitpos;
  1331. current_settings.localswitches:=exitswitches;
  1332. cg.set_regalloc_live_range_direction(rad_forward);
  1333. if assigned(finalize_procinfo) then
  1334. generate_exceptfilter(tcgprocinfo(finalize_procinfo))
  1335. else
  1336. begin
  1337. hlcg.gen_finalize_code(templist);
  1338. { the finalcode must be concated if there was no position available,
  1339. using insertlistafter will result in an insert at the start
  1340. when currentai=nil }
  1341. if assigned(final_asmnode) and assigned(final_asmnode.currenttai) then
  1342. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  1343. else
  1344. aktproccode.concatlist(templist);
  1345. end;
  1346. { insert exit label at the correct position }
  1347. hlcg.a_label(templist,CurrExitLabel);
  1348. if assigned(exitlabel_asmnode.currenttai) then
  1349. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  1350. else
  1351. aktproccode.concatlist(templist);
  1352. { exit code }
  1353. hlcg.gen_exit_code(templist);
  1354. aktproccode.concatlist(templist);
  1355. {$ifdef OLDREGVARS}
  1356. { note: this must be done only after as much code as possible has }
  1357. { been generated. The result is that when you ungetregister() a }
  1358. { regvar, it will actually free the regvar (and alse free the }
  1359. { the regvars at the same time). Doing this too early will }
  1360. { confuse the register allocator, as the regvars will still be }
  1361. { used. It should be done before loading the result regs (so }
  1362. { they don't conflict with the regvars) and before }
  1363. { gen_entry_code (that one has to be able to allocate the }
  1364. { regvars again) (JM) }
  1365. free_regvars(aktproccode);
  1366. {$endif OLDREGVARS}
  1367. { generate symbol and save end of header position }
  1368. current_filepos:=entrypos;
  1369. hlcg.gen_proc_symbol(templist);
  1370. headertai:=tai(templist.last);
  1371. { insert symbol }
  1372. aktproccode.insertlist(templist);
  1373. { Free space in temp/registers for parast and localst, must be
  1374. done after gen_entry_code }
  1375. current_filepos:=exitpos;
  1376. { make sure the got/pic register doesn't get freed in the }
  1377. { middle of a loop }
  1378. if (cs_create_pic in current_settings.moduleswitches) and
  1379. (pi_needs_got in flags) and
  1380. (got<>NR_NO) then
  1381. cg.a_reg_sync(aktproccode,got);
  1382. gen_free_symtable(aktproccode,procdef.localst);
  1383. gen_free_symtable(aktproccode,procdef.parast);
  1384. { add code that will load the return value, this is not done
  1385. for assembler routines when they didn't reference the result
  1386. variable }
  1387. hlcg.gen_load_return_value(templist);
  1388. aktproccode.concatlist(templist);
  1389. { Already reserve all registers for stack checking code and
  1390. generate the call to the helper function }
  1391. if not(tf_no_generic_stackcheck in target_info.flags) and
  1392. (cs_check_stack in entryswitches) and
  1393. not(po_assembler in procdef.procoptions) and
  1394. (procdef.proctypeoption<>potype_proginit) then
  1395. begin
  1396. current_filepos:=entrypos;
  1397. gen_stack_check_call(templist);
  1398. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1399. end;
  1400. { this code (got loading) comes before everything which has }
  1401. { already been generated, so reset the info about already }
  1402. { backwards extended registers (so their live range can be }
  1403. { extended backwards even further if needed) }
  1404. { This code must be }
  1405. { a) generated after do_secondpass has been called }
  1406. { (because pi_needs_got may be set there) }
  1407. { b) generated before register allocation, because the }
  1408. { got/pic register can be a virtual one }
  1409. { c) inserted before the entry code, because the entry }
  1410. { code may need global symbols such as init rtti }
  1411. { d) inserted after the stackframe allocation, because }
  1412. { this register may have to be spilled }
  1413. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  1414. current_filepos:=entrypos;
  1415. { load got if necessary }
  1416. cg.g_maybe_got_init(templist);
  1417. aktproccode.insertlistafter(headertai,templist);
  1418. { re-enable if more code at the end is ever generated here
  1419. cg.set_regalloc_live_range_direction(rad_forward);
  1420. }
  1421. {$ifndef NoOpt}
  1422. {$ifndef i386}
  1423. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1424. { do not optimize pure assembler procedures }
  1425. not(pi_is_assembler in flags) then
  1426. preregallocschedule(aktproccode);
  1427. {$endif i386}
  1428. {$endif NoOpt}
  1429. { The procedure body is finished, we can now
  1430. allocate the registers }
  1431. cg.do_register_allocation(aktproccode,headertai);
  1432. { translate imag. register to their real counter parts
  1433. this is necessary for debuginfo and verbose assembler output
  1434. when SSA will be implented, this will be more complicated because we've to
  1435. maintain location lists }
  1436. procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  1437. procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  1438. if (cs_create_pic in current_settings.moduleswitches) and
  1439. (pi_needs_got in flags) and
  1440. not(cs_no_regalloc in current_settings.globalswitches) and
  1441. (got<>NR_NO) then
  1442. cg.translate_register(got);
  1443. { Add save and restore of used registers }
  1444. current_filepos:=entrypos;
  1445. gen_save_used_regs(templist);
  1446. { Remember the last instruction of register saving block
  1447. (may be =nil for e.g. assembler procedures) }
  1448. endprologue_ai:=templist.last;
  1449. aktproccode.insertlistafter(headertai,templist);
  1450. current_filepos:=exitpos;
  1451. gen_restore_used_regs(aktproccode);
  1452. { We know the size of the stack, now we can generate the
  1453. parameter that is passed to the stack checking code }
  1454. if not(tf_no_generic_stackcheck in target_info.flags) and
  1455. (cs_check_stack in entryswitches) and
  1456. not(po_assembler in procdef.procoptions) and
  1457. (procdef.proctypeoption<>potype_proginit) then
  1458. begin
  1459. current_filepos:=entrypos;
  1460. gen_stack_check_size_para(templist);
  1461. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1462. end;
  1463. { Add entry code (stack allocation) after header }
  1464. current_filepos:=entrypos;
  1465. gen_proc_entry_code(templist);
  1466. aktproccode.insertlistafter(headertai,templist);
  1467. {$ifdef SUPPORT_SAFECALL}
  1468. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  1469. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  1470. (tf_safecall_exceptions in target_info.flags) and
  1471. (procdef.proccalloption=pocall_safecall) then
  1472. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  1473. {$endif}
  1474. { Add exit code at the end }
  1475. current_filepos:=exitpos;
  1476. gen_proc_exit_code(templist);
  1477. aktproccode.concatlist(templist);
  1478. { check if the implicit finally has been generated. The flag
  1479. should already be set in pass1 }
  1480. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  1481. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  1482. (pi_needs_implicit_finally in flags) and
  1483. not(po_assembler in procdef.procoptions) and
  1484. not(pi_has_implicit_finally in flags) and
  1485. not(target_info.system in systems_garbage_collected_managed_types) then
  1486. internalerror(200405231);
  1487. { Position markers are only used to insert additional code after the secondpass
  1488. and before this point. They are of no use in optimizer. Instead of checking and
  1489. ignoring all over the optimizer, just remove them here. }
  1490. delete_marker(entry_asmnode);
  1491. delete_marker(loadpara_asmnode);
  1492. delete_marker(exitlabel_asmnode);
  1493. delete_marker(stackcheck_asmnode);
  1494. delete_marker(init_asmnode);
  1495. delete_marker(final_asmnode);
  1496. {$ifndef NoOpt}
  1497. if not(cs_no_regalloc in current_settings.globalswitches) then
  1498. begin
  1499. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1500. { do not optimize pure assembler procedures }
  1501. not(pi_is_assembler in flags) then
  1502. optimize(aktproccode);
  1503. {$ifndef i386}
  1504. { schedule after assembler optimization, it could have brought up
  1505. new schedule possibilities }
  1506. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1507. { do not optimize pure assembler procedures }
  1508. not(pi_is_assembler in flags) then
  1509. preregallocschedule(aktproccode);
  1510. {$endif i386}
  1511. end;
  1512. {$endif NoOpt}
  1513. { Perform target-specific processing if necessary }
  1514. postprocess_code;
  1515. { Add end symbol and debug info }
  1516. { this must be done after the pcrelativedata is appended else the distance calculation of
  1517. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  1518. so it should be inserted before the end symbol (FK)
  1519. }
  1520. current_filepos:=exitpos;
  1521. hlcg.gen_proc_symbol_end(templist);
  1522. aktproccode.concatlist(templist);
  1523. { insert line debuginfo }
  1524. if (cs_debuginfo in current_settings.moduleswitches) or
  1525. (cs_use_lineinfo in current_settings.globalswitches) then
  1526. current_debuginfo.insertlineinfo(aktproccode);
  1527. hlcg.record_generated_code_for_procdef(current_procinfo.procdef,aktproccode,aktlocaldata);
  1528. { only now we can remove the temps }
  1529. if (procdef.proctypeoption<>potype_exceptfilter) then
  1530. begin
  1531. tg.resettempgen;
  1532. tg.free;
  1533. tg:=nil;
  1534. end;
  1535. { stop tempgen and ra }
  1536. hlcg.done_register_allocators;
  1537. destroy_hlcodegen;
  1538. end;
  1539. dfabuilder.free;
  1540. { restore symtablestack }
  1541. remove_from_symtablestack;
  1542. { restore }
  1543. templist.free;
  1544. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1545. current_filepos:=oldfilepos;
  1546. current_structdef:=old_current_structdef;
  1547. current_procinfo:=old_current_procinfo;
  1548. end;
  1549. procedure tcgprocinfo.add_to_symtablestack;
  1550. begin
  1551. { insert symtables for the class, but only if it is no nested function }
  1552. if assigned(procdef.struct) and
  1553. not(assigned(parent) and
  1554. assigned(parent.procdef) and
  1555. assigned(parent.procdef.struct)) then
  1556. push_nested_hierarchy(procdef.struct);
  1557. { insert parasymtable in symtablestack when parsing
  1558. a function }
  1559. if procdef.parast.symtablelevel>=normal_function_level then
  1560. symtablestack.push(procdef.parast);
  1561. { insert localsymtable, except for the main procedure
  1562. (in that case the localst is the unit's static symtable,
  1563. which is already on the stack) }
  1564. if procdef.localst.symtablelevel>=normal_function_level then
  1565. symtablestack.push(procdef.localst);
  1566. end;
  1567. procedure tcgprocinfo.remove_from_symtablestack;
  1568. begin
  1569. { remove localsymtable }
  1570. if procdef.localst.symtablelevel>=normal_function_level then
  1571. symtablestack.pop(procdef.localst);
  1572. { remove parasymtable }
  1573. if procdef.parast.symtablelevel>=normal_function_level then
  1574. symtablestack.pop(procdef.parast);
  1575. { remove symtables for the class, but only if it is no nested function }
  1576. if assigned(procdef.struct) and
  1577. not(assigned(parent) and
  1578. assigned(parent.procdef) and
  1579. assigned(parent.procdef.struct)) then
  1580. pop_nested_hierarchy(procdef.struct);
  1581. end;
  1582. procedure tcgprocinfo.resetprocdef;
  1583. begin
  1584. { remove code tree, if not inline procedure }
  1585. if assigned(code) then
  1586. begin
  1587. { the inline procedure has already got a copy of the tree
  1588. stored in procdef.inlininginfo }
  1589. code.free;
  1590. code:=nil;
  1591. end;
  1592. end;
  1593. procedure tcgprocinfo.parse_body;
  1594. var
  1595. old_current_procinfo : tprocinfo;
  1596. old_block_type : tblock_type;
  1597. st : TSymtable;
  1598. old_current_structdef: tabstractrecorddef;
  1599. old_current_genericdef,
  1600. old_current_specializedef: tstoreddef;
  1601. old_parse_generic: boolean;
  1602. begin
  1603. old_current_procinfo:=current_procinfo;
  1604. old_block_type:=block_type;
  1605. old_current_structdef:=current_structdef;
  1606. old_current_genericdef:=current_genericdef;
  1607. old_current_specializedef:=current_specializedef;
  1608. old_parse_generic:=parse_generic;
  1609. current_procinfo:=self;
  1610. current_structdef:=procdef.struct;
  1611. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1612. begin
  1613. current_genericdef:=current_structdef;
  1614. parse_generic:=true;
  1615. end;
  1616. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1617. current_specializedef:=current_structdef;
  1618. { calculate the lexical level }
  1619. if procdef.parast.symtablelevel>maxnesting then
  1620. Message(parser_e_too_much_lexlevel);
  1621. block_type:=bt_body;
  1622. {$ifdef state_tracking}
  1623. { aktstate:=Tstate_storage.create;}
  1624. {$endif state_tracking}
  1625. { allocate the symbol for this procedure }
  1626. alloc_proc_symbol(procdef);
  1627. { add parast/localst to symtablestack }
  1628. add_to_symtablestack;
  1629. { save entry info }
  1630. entrypos:=current_filepos;
  1631. entryswitches:=current_settings.localswitches;
  1632. if (df_generic in procdef.defoptions) then
  1633. begin
  1634. { start token recorder for generic template }
  1635. procdef.initgeneric;
  1636. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1637. end;
  1638. { parse the code ... }
  1639. code:=block(current_module.islibrary);
  1640. if (df_generic in procdef.defoptions) then
  1641. begin
  1642. { stop token recorder for generic template }
  1643. current_scanner.stoprecordtokens;
  1644. { Give an error for accesses in the static symtable that aren't visible
  1645. outside the current unit }
  1646. st:=procdef.owner;
  1647. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  1648. st:=st.defowner.owner;
  1649. if (pi_uses_static_symtable in flags) and
  1650. (st.symtabletype<>staticsymtable) then
  1651. Message(parser_e_global_generic_references_static);
  1652. end;
  1653. { save exit info }
  1654. exitswitches:=current_settings.localswitches;
  1655. exitpos:=last_endtoken_filepos;
  1656. { the procedure is now defined }
  1657. procdef.forwarddef:=false;
  1658. if assigned(code) then
  1659. begin
  1660. { get a better entry point }
  1661. entrypos:=code.fileinfo;
  1662. { Finish type checking pass }
  1663. { type checking makes no sense in a generic definition }
  1664. if not(df_generic in current_procinfo.procdef.defoptions) then
  1665. do_typecheckpass(code);
  1666. if assigned(procdef.parentfpinitblock) then
  1667. begin
  1668. tblocknode(code).left:=cstatementnode.create(procdef.parentfpinitblock,tblocknode(code).left);
  1669. do_typecheckpass(tblocknode(code).left);
  1670. procdef.parentfpinitblock:=nil;
  1671. end;
  1672. end;
  1673. { Check for unused labels, forwards, symbols for procedures. Static
  1674. symtable is checked in pmodules.
  1675. The check must be done after the typecheckpass }
  1676. if (Errorcount=0) and
  1677. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1678. begin
  1679. { check if forwards are resolved }
  1680. tstoredsymtable(procdef.localst).check_forwards;
  1681. { check if all labels are used }
  1682. tstoredsymtable(procdef.localst).checklabels;
  1683. { check for unused symbols, but only if there is no asm block }
  1684. if not(pi_has_assembler_block in flags) then
  1685. begin
  1686. tstoredsymtable(procdef.localst).allsymbolsused;
  1687. tstoredsymtable(procdef.parast).allsymbolsused;
  1688. end;
  1689. end;
  1690. if (po_inline in procdef.procoptions) and
  1691. { Can we inline this procedure? }
  1692. checknodeinlining(procdef) then
  1693. CreateInlineInfo;
  1694. { Print the node to tree.log }
  1695. if paraprintnodetree=1 then
  1696. printproc( 'after parsing');
  1697. { ... remove symbol tables }
  1698. remove_from_symtablestack;
  1699. {$ifdef state_tracking}
  1700. { aktstate.destroy;}
  1701. {$endif state_tracking}
  1702. current_structdef:=old_current_structdef;
  1703. current_genericdef:=old_current_genericdef;
  1704. current_specializedef:=old_current_specializedef;
  1705. current_procinfo:=old_current_procinfo;
  1706. parse_generic:=old_parse_generic;
  1707. { Restore old state }
  1708. block_type:=old_block_type;
  1709. end;
  1710. {****************************************************************************
  1711. PROCEDURE/FUNCTION PARSING
  1712. ****************************************************************************}
  1713. procedure check_init_paras(p:TObject;arg:pointer);
  1714. begin
  1715. if tsym(p).typ<>paravarsym then
  1716. exit;
  1717. with tparavarsym(p) do
  1718. if (is_managed_type(vardef) and
  1719. (varspez in [vs_value,vs_out])) or
  1720. (is_shortstring(vardef) and
  1721. (varspez=vs_value)) then
  1722. include(current_procinfo.flags,pi_do_call);
  1723. end;
  1724. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1725. {
  1726. Parses the procedure directives, then parses the procedure body, then
  1727. generates the code for it
  1728. }
  1729. var
  1730. oldfailtokenmode : tmodeswitches;
  1731. isnestedproc : boolean;
  1732. begin
  1733. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1734. oldfailtokenmode:=[];
  1735. { create a new procedure }
  1736. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1737. current_module.procinfo:=current_procinfo;
  1738. current_procinfo.procdef:=pd;
  1739. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1740. { Insert mangledname }
  1741. pd.aliasnames.insert(pd.mangledname);
  1742. { Handle Export of this procedure }
  1743. if (po_exports in pd.procoptions) and
  1744. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1745. begin
  1746. pd.aliasnames.insert(pd.procsym.realname);
  1747. if cs_link_deffile in current_settings.globalswitches then
  1748. deffile.AddExport(pd.mangledname);
  1749. end;
  1750. { Insert result variables in the localst }
  1751. insert_funcret_local(pd);
  1752. { check if there are para's which require initing -> set }
  1753. { pi_do_call (if not yet set) }
  1754. if not(pi_do_call in current_procinfo.flags) then
  1755. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1756. { set _FAIL as keyword if constructor }
  1757. if (pd.proctypeoption=potype_constructor) then
  1758. begin
  1759. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1760. tokeninfo^[_FAIL].keyword:=alllanguagemodes;
  1761. end;
  1762. tcgprocinfo(current_procinfo).parse_body;
  1763. { We can't support inlining for procedures that have nested
  1764. procedures because the nested procedures use a fixed offset
  1765. for accessing locals in the parent procedure (PFV) }
  1766. if current_procinfo.has_nestedprocs then
  1767. begin
  1768. if (df_generic in current_procinfo.procdef.defoptions) then
  1769. Comment(V_Error,'Generic methods cannot have nested procedures')
  1770. else
  1771. if (po_inline in current_procinfo.procdef.procoptions) then
  1772. begin
  1773. Message1(parser_h_not_supported_for_inline,'nested procedures');
  1774. Message(parser_h_inlining_disabled);
  1775. exclude(current_procinfo.procdef.procoptions,po_inline);
  1776. end;
  1777. end;
  1778. { When it's a nested procedure then defer the code generation,
  1779. when back at normal function level then generate the code
  1780. for all defered nested procedures and the current procedure }
  1781. if not isnestedproc then
  1782. begin
  1783. if not(df_generic in current_procinfo.procdef.defoptions) then
  1784. tcgprocinfo(current_procinfo).generate_code_tree;
  1785. end;
  1786. { reset _FAIL as _SELF normal }
  1787. if (pd.proctypeoption=potype_constructor) then
  1788. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1789. { release procinfo }
  1790. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1791. internalerror(200304274);
  1792. current_module.procinfo:=current_procinfo.parent;
  1793. { For specialization we didn't record the last semicolon. Moving this parsing
  1794. into the parse_body routine is not done because of having better file position
  1795. information available }
  1796. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1797. consume(_SEMICOLON);
  1798. if not isnestedproc then
  1799. { current_procinfo is checked for nil later on }
  1800. freeandnil(current_procinfo);
  1801. end;
  1802. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef);
  1803. {
  1804. Parses the procedure directives, then parses the procedure body, then
  1805. generates the code for it
  1806. }
  1807. var
  1808. old_current_procinfo : tprocinfo;
  1809. old_current_structdef: tabstractrecorddef;
  1810. old_current_genericdef,
  1811. old_current_specializedef: tstoreddef;
  1812. pdflags : tpdflags;
  1813. pd,firstpd : tprocdef;
  1814. s : string;
  1815. begin
  1816. { save old state }
  1817. old_current_procinfo:=current_procinfo;
  1818. old_current_structdef:=current_structdef;
  1819. old_current_genericdef:=current_genericdef;
  1820. old_current_specializedef:=current_specializedef;
  1821. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1822. to another procdef }
  1823. current_procinfo:=nil;
  1824. current_structdef:=nil;
  1825. current_genericdef:=nil;
  1826. current_specializedef:=nil;
  1827. if not assigned(usefwpd) then
  1828. { parse procedure declaration }
  1829. pd:=parse_proc_dec(isclassmethod,old_current_structdef)
  1830. else
  1831. pd:=usefwpd;
  1832. { set the default function options }
  1833. if parse_only then
  1834. begin
  1835. pd.forwarddef:=true;
  1836. { set also the interface flag, for better error message when the
  1837. implementation doesn't much this header }
  1838. pd.interfacedef:=true;
  1839. include(pd.procoptions,po_global);
  1840. pdflags:=[pd_interface];
  1841. end
  1842. else
  1843. begin
  1844. pdflags:=[pd_body];
  1845. if (not current_module.in_interface) then
  1846. include(pdflags,pd_implemen);
  1847. if (not current_module.is_unit) or
  1848. create_smartlink then
  1849. include(pd.procoptions,po_global);
  1850. pd.forwarddef:=false;
  1851. end;
  1852. if not assigned(usefwpd) then
  1853. begin
  1854. { parse the directives that may follow }
  1855. parse_proc_directives(pd,pdflags);
  1856. { hint directives, these can be separated by semicolons here,
  1857. that needs to be handled here with a loop (PFV) }
  1858. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1859. Consume(_SEMICOLON);
  1860. { Set calling convention }
  1861. handle_calling_convention(pd);
  1862. end;
  1863. { search for forward declarations }
  1864. if not proc_add_definition(pd) then
  1865. begin
  1866. { A method must be forward defined (in the object declaration) }
  1867. if assigned(pd.struct) and
  1868. (not assigned(old_current_structdef)) then
  1869. begin
  1870. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1871. tprocsym(pd.procsym).write_parameter_lists(pd);
  1872. end
  1873. else
  1874. begin
  1875. { Give a better error if there is a forward def in the interface and only
  1876. a single implementation }
  1877. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1878. if (not pd.forwarddef) and
  1879. (not pd.interfacedef) and
  1880. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1881. firstpd.forwarddef and
  1882. firstpd.interfacedef and
  1883. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1884. { don't give an error if it may be an overload }
  1885. not(m_fpc in current_settings.modeswitches) and
  1886. (not(po_overload in pd.procoptions) or
  1887. not(po_overload in firstpd.procoptions)) then
  1888. begin
  1889. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1890. tprocsym(pd.procsym).write_parameter_lists(pd);
  1891. end;
  1892. end;
  1893. end;
  1894. { Set mangled name }
  1895. proc_set_mangledname(pd);
  1896. { compile procedure when a body is needed }
  1897. if (pd_body in pdflags) then
  1898. begin
  1899. read_proc_body(old_current_procinfo,pd);
  1900. end
  1901. else
  1902. begin
  1903. { Handle imports }
  1904. if (po_external in pd.procoptions) then
  1905. begin
  1906. { Import DLL specified? }
  1907. if assigned(pd.import_dll) then
  1908. begin
  1909. if assigned (pd.import_name) then
  1910. current_module.AddExternalImport(pd.import_dll^,
  1911. pd.import_name^,proc_get_importname(pd),
  1912. pd.import_nr,false,false)
  1913. else
  1914. current_module.AddExternalImport(pd.import_dll^,
  1915. proc_get_importname(pd),proc_get_importname(pd),
  1916. pd.import_nr,false,true);
  1917. end
  1918. else
  1919. begin
  1920. { add import name to external list for DLL scanning }
  1921. if tf_has_dllscanner in target_info.flags then
  1922. current_module.dllscannerinputlist.Add(proc_get_importname(pd),pd);
  1923. end;
  1924. {$ifdef cpuhighleveltarget}
  1925. { it's hard to factor this out in a virtual method, because the
  1926. generic version (the one inside this ifdef) doesn't fit in
  1927. hlcgobj but in symcreat or here, while the other version
  1928. doesn't fit in symcreat (since it uses the code generator).
  1929. Maybe we need another class for this kind of code that could
  1930. either be symcreat- or hlcgobj-based
  1931. }
  1932. if (not pd.forwarddef) and
  1933. (pd.hasforward) and
  1934. (proc_get_importname(pd)<>'') then
  1935. call_through_new_name(pd,proc_get_importname(pd))
  1936. else
  1937. {$endif cpuhighleveltarget}
  1938. begin
  1939. create_hlcodegen;
  1940. hlcg.handle_external_proc(
  1941. current_asmdata.asmlists[al_procedures],
  1942. pd,
  1943. proc_get_importname(pd));
  1944. destroy_hlcodegen;
  1945. end
  1946. end;
  1947. end;
  1948. { make sure that references to forward-declared functions are not }
  1949. { treated as references to external symbols, needed for darwin. }
  1950. { make sure we don't change the binding of real external symbols }
  1951. if ([po_external,po_weakexternal]*pd.procoptions)=[] then
  1952. begin
  1953. if (po_global in pd.procoptions) or
  1954. (cs_profile in current_settings.moduleswitches) then
  1955. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION)
  1956. else
  1957. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  1958. end;
  1959. current_structdef:=old_current_structdef;
  1960. current_genericdef:=old_current_genericdef;
  1961. current_specializedef:=old_current_specializedef;
  1962. current_procinfo:=old_current_procinfo;
  1963. end;
  1964. {****************************************************************************
  1965. DECLARATION PARSING
  1966. ****************************************************************************}
  1967. { search in symtablestack for not complete classes }
  1968. procedure check_forward_class(p:TObject;arg:pointer);
  1969. begin
  1970. if (tsym(p).typ=typesym) and
  1971. (ttypesym(p).typedef.typ=objectdef) and
  1972. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  1973. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1974. end;
  1975. procedure read_declarations(islibrary : boolean);
  1976. var
  1977. is_classdef:boolean;
  1978. begin
  1979. is_classdef:=false;
  1980. repeat
  1981. if not assigned(current_procinfo) then
  1982. internalerror(200304251);
  1983. case token of
  1984. _LABEL:
  1985. label_dec;
  1986. _CONST:
  1987. const_dec;
  1988. _TYPE:
  1989. type_dec;
  1990. _VAR:
  1991. var_dec;
  1992. _THREADVAR:
  1993. threadvar_dec;
  1994. _CLASS:
  1995. begin
  1996. is_classdef:=false;
  1997. if try_to_consume(_CLASS) then
  1998. begin
  1999. { class modifier is only allowed for procedures, functions, }
  2000. { constructors, destructors }
  2001. if not(token in [_FUNCTION,_PROCEDURE,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  2002. not((token=_ID) and (idtoken=_OPERATOR)) then
  2003. Message(parser_e_procedure_or_function_expected);
  2004. if is_interface(current_structdef) then
  2005. Message(parser_e_no_static_method_in_interfaces)
  2006. else
  2007. { class methods are also allowed for Objective-C protocols }
  2008. is_classdef:=true;
  2009. end;
  2010. end;
  2011. _CONSTRUCTOR,
  2012. _DESTRUCTOR,
  2013. _FUNCTION,
  2014. _PROCEDURE,
  2015. _OPERATOR:
  2016. begin
  2017. read_proc(is_classdef,nil);
  2018. is_classdef:=false;
  2019. end;
  2020. _EXPORTS:
  2021. begin
  2022. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  2023. begin
  2024. Message(parser_e_syntax_error);
  2025. consume_all_until(_SEMICOLON);
  2026. end
  2027. else if islibrary or
  2028. (target_info.system in systems_unit_program_exports) then
  2029. read_exports
  2030. else
  2031. begin
  2032. Message(parser_w_unsupported_feature);
  2033. consume(_BEGIN);
  2034. end;
  2035. end;
  2036. _PROPERTY:
  2037. begin
  2038. if (m_fpc in current_settings.modeswitches) then
  2039. property_dec
  2040. else
  2041. break;
  2042. end;
  2043. else
  2044. begin
  2045. case idtoken of
  2046. _RESOURCESTRING:
  2047. begin
  2048. { m_class is needed, because the resourcestring
  2049. loading is in the ObjPas unit }
  2050. { if (m_class in current_settings.modeswitches) then}
  2051. resourcestring_dec
  2052. { else
  2053. break;}
  2054. end;
  2055. _OPERATOR:
  2056. begin
  2057. if is_classdef then
  2058. begin
  2059. read_proc(is_classdef,nil);
  2060. is_classdef:=false;
  2061. end
  2062. else
  2063. break;
  2064. end;
  2065. else
  2066. break;
  2067. end;
  2068. end;
  2069. end;
  2070. until false;
  2071. { add implementations for synthetic method declarations added by
  2072. the compiler (not for unit/program init functions, their localst
  2073. is the staticst -> would duplicate the work done in pmodules) }
  2074. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  2075. add_synthetic_method_implementations(current_procinfo.procdef.localst);
  2076. { check for incomplete class definitions, this is only required
  2077. for fpc modes }
  2078. if (m_fpc in current_settings.modeswitches) then
  2079. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  2080. end;
  2081. procedure read_interface_declarations;
  2082. begin
  2083. repeat
  2084. case token of
  2085. _CONST :
  2086. const_dec;
  2087. _TYPE :
  2088. type_dec;
  2089. _VAR :
  2090. var_dec;
  2091. _THREADVAR :
  2092. threadvar_dec;
  2093. _FUNCTION,
  2094. _PROCEDURE,
  2095. _OPERATOR :
  2096. read_proc(false,nil);
  2097. else
  2098. begin
  2099. case idtoken of
  2100. _RESOURCESTRING :
  2101. resourcestring_dec;
  2102. _PROPERTY:
  2103. begin
  2104. if (m_fpc in current_settings.modeswitches) then
  2105. property_dec
  2106. else
  2107. break;
  2108. end;
  2109. else
  2110. break;
  2111. end;
  2112. end;
  2113. end;
  2114. until false;
  2115. { check for incomplete class definitions, this is only required
  2116. for fpc modes }
  2117. if (m_fpc in current_settings.modeswitches) then
  2118. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  2119. end;
  2120. {****************************************************************************
  2121. SPECIALIZATION BODY GENERATION
  2122. ****************************************************************************}
  2123. procedure specialize_objectdefs(p:TObject;arg:pointer);
  2124. var
  2125. oldcurrent_filepos : tfileposinfo;
  2126. specobj : tabstractrecorddef;
  2127. state : tspecializationstate;
  2128. procedure process_abstractrecorddef(def:tabstractrecorddef);
  2129. var
  2130. i : longint;
  2131. hp : tdef;
  2132. hmodule : tmodule;
  2133. begin
  2134. hmodule:=find_module_from_symtable(def.genericdef.owner);
  2135. if hmodule=nil then
  2136. internalerror(201202041);
  2137. for i:=0 to def.symtable.DefList.Count-1 do
  2138. begin
  2139. hp:=tdef(def.symtable.DefList[i]);
  2140. if hp.typ=procdef then
  2141. begin
  2142. { only generate the code if we need a body }
  2143. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  2144. continue;
  2145. if assigned(tprocdef(hp).genericdef) and
  2146. (tprocdef(hp).genericdef.typ=procdef) and
  2147. assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf) then
  2148. begin
  2149. oldcurrent_filepos:=current_filepos;
  2150. current_filepos:=tprocdef(tprocdef(hp).genericdef).fileinfo;
  2151. { use the index the module got from the current compilation process }
  2152. current_filepos.moduleindex:=hmodule.unit_index;
  2153. current_tokenpos:=current_filepos;
  2154. current_scanner.startreplaytokens(tprocdef(tprocdef(hp).genericdef).generictokenbuf);
  2155. read_proc_body(nil,tprocdef(hp));
  2156. current_filepos:=oldcurrent_filepos;
  2157. end
  2158. { synthetic routines will be implemented afterwards }
  2159. else if tprocdef(hp).synthetickind=tsk_none then
  2160. MessagePos1(tprocdef(hp).fileinfo,sym_e_forward_not_resolved,tprocdef(hp).fullprocname(false));
  2161. end
  2162. else
  2163. if hp.typ in [objectdef,recorddef] then
  2164. { generate code for subtypes as well }
  2165. process_abstractrecorddef(tabstractrecorddef(hp));
  2166. end;
  2167. end;
  2168. begin
  2169. if not((tsym(p).typ=typesym) and
  2170. (ttypesym(p).typedef.typesym=tsym(p)) and
  2171. (ttypesym(p).typedef.typ in [objectdef,recorddef]) and
  2172. (df_specialization in ttypesym(p).typedef.defoptions)
  2173. ) then
  2174. exit;
  2175. { Setup symtablestack a definition time }
  2176. specobj:=tabstractrecorddef(ttypesym(p).typedef);
  2177. if not (is_class_or_object(specobj) or is_record(specobj) or is_javaclass(specobj)) then
  2178. exit;
  2179. specialization_init(specobj.genericdef,state);
  2180. { procedure definitions for classes or objects }
  2181. process_abstractrecorddef(specobj);
  2182. specialization_done(state);
  2183. end;
  2184. procedure generate_specialization_procs;
  2185. begin
  2186. if assigned(current_module.globalsymtable) then
  2187. current_module.globalsymtable.SymList.WhileEachCall(@specialize_objectdefs,nil);
  2188. if assigned(current_module.localsymtable) then
  2189. current_module.localsymtable.SymList.WhileEachCall(@specialize_objectdefs,nil);
  2190. end;
  2191. end.