psub.pas 79 KB

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