psub.pas 81 KB

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