psub.pas 80 KB

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