psub.pas 100 KB

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