psub.pas 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing and codegeneration at subroutine level
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit psub;
  19. {$i fpcdefs.inc}
  20. {$define NOOPT}
  21. interface
  22. uses
  23. cclasses,globals,
  24. node,nbas,
  25. symdef,procinfo;
  26. type
  27. tcgprocinfo=class(tprocinfo)
  28. { code for the subroutine as tree }
  29. code : tnode;
  30. { positions in the tree for init/final }
  31. entry_asmnode,
  32. loadpara_asmnode,
  33. exitlabel_asmnode,
  34. init_asmnode,
  35. final_asmnode : tasmnode;
  36. { list to store the procinfo's of the nested procedures }
  37. nestedprocs : tlinkedlist;
  38. constructor create(aparent:tprocinfo);override;
  39. destructor destroy;override;
  40. procedure generate_code;
  41. procedure resetprocdef;
  42. procedure add_to_symtablestack;
  43. procedure remove_from_symtablestack;
  44. procedure parse_body;
  45. procedure add_entry_exit_code;
  46. end;
  47. procedure printnode_reset;
  48. { reads the declaration blocks }
  49. procedure read_declarations(islibrary : boolean);
  50. { reads declarations in the interface part of a unit }
  51. procedure read_interface_declarations;
  52. implementation
  53. uses
  54. { common }
  55. cutils,
  56. { global }
  57. globtype,tokens,verbose,comphook,
  58. systems,
  59. { aasm }
  60. cpubase,aasmtai,
  61. { symtable }
  62. symconst,symbase,symsym,symtype,symtable,defutil,
  63. paramgr,
  64. ppu,fmodule,
  65. { pass 1 }
  66. nutils,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  67. pass_1,
  68. {$ifdef state_tracking}
  69. nstate,
  70. {$endif state_tracking}
  71. { pass 2 }
  72. {$ifndef NOPASS2}
  73. pass_2,
  74. {$endif}
  75. { parser }
  76. scanner,
  77. pbase,pstatmnt,pdecl,pdecsub,pexports,
  78. { codegen }
  79. tgobj,cgobj,
  80. ncgutil,regvars
  81. {$ifndef NOOPT}
  82. {$ifdef i386}
  83. ,aopt386
  84. {$else i386}
  85. ,aoptcpu
  86. {$endif i386}
  87. {$endif}
  88. ;
  89. {****************************************************************************
  90. PROCEDURE/FUNCTION BODY PARSING
  91. ****************************************************************************}
  92. procedure initializevars(p:tnamedindexitem;arg:pointer);
  93. var
  94. b : tblocknode;
  95. begin
  96. if tsym(p).typ<>varsym then
  97. exit;
  98. with tvarsym(p) do
  99. begin
  100. if assigned(defaultconstsym) then
  101. begin
  102. b:=tblocknode(arg);
  103. b.left:=cstatementnode.create(
  104. cassignmentnode.create(
  105. cloadnode.create(tsym(p),tsym(p).owner),
  106. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  107. b.left);
  108. end;
  109. end;
  110. end;
  111. function block(islibrary : boolean) : tnode;
  112. begin
  113. { parse const,types and vars }
  114. read_declarations(islibrary);
  115. { do we have an assembler block without the po_assembler?
  116. we should allow this for Delphi compatibility (PFV) }
  117. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  118. include(current_procinfo.procdef.procoptions,po_assembler);
  119. { Handle assembler block different }
  120. if (po_assembler in current_procinfo.procdef.procoptions) then
  121. begin
  122. block:=assembler_block;
  123. exit;
  124. end;
  125. {Unit initialization?.}
  126. if (
  127. assigned(current_procinfo.procdef.localst) and
  128. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  129. (current_module.is_unit)
  130. ) or
  131. islibrary then
  132. begin
  133. if (token=_END) then
  134. begin
  135. consume(_END);
  136. { We need at least a node, else the entry/exit code is not
  137. generated and thus no PASCALMAIN symbol which we need (PFV) }
  138. if islibrary then
  139. block:=cnothingnode.create
  140. else
  141. block:=nil;
  142. end
  143. else
  144. begin
  145. if token=_INITIALIZATION then
  146. begin
  147. { The library init code is already called and does not
  148. need to be in the initfinal table (PFV) }
  149. if not islibrary then
  150. current_module.flags:=current_module.flags or uf_init;
  151. block:=statement_block(_INITIALIZATION);
  152. end
  153. else if (token=_FINALIZATION) then
  154. begin
  155. if (current_module.flags and uf_finalize)<>0 then
  156. block:=statement_block(_FINALIZATION)
  157. else
  158. begin
  159. { can we allow no INITIALIZATION for DLL ??
  160. I think it should work PM }
  161. block:=nil;
  162. exit;
  163. end;
  164. end
  165. else
  166. begin
  167. { The library init code is already called and does not
  168. need to be in the initfinal table (PFV) }
  169. if not islibrary then
  170. current_module.flags:=current_module.flags or uf_init;
  171. block:=statement_block(_BEGIN);
  172. end;
  173. end;
  174. end
  175. else
  176. begin
  177. block:=statement_block(_BEGIN);
  178. if symtablestack.symtabletype=localsymtable then
  179. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}initializevars,block);
  180. end;
  181. if (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  182. (not current_module.is_unit) then
  183. begin
  184. { there's always a call to FPC_DO_EXIT in the main program }
  185. include(current_procinfo.flags,pi_do_call);
  186. end;
  187. if ([cs_check_range,cs_check_overflow] * aktlocalswitches <> []) then
  188. include(current_procinfo.flags,pi_do_call);
  189. end;
  190. {****************************************************************************
  191. PROCEDURE/FUNCTION COMPILING
  192. ****************************************************************************}
  193. procedure printnode_reset;
  194. begin
  195. assign(printnodefile,treelogfilename);
  196. {$I-}
  197. rewrite(printnodefile);
  198. {$I+}
  199. if ioresult<>0 then
  200. begin
  201. Comment(V_Error,'Error creating '+treelogfilename);
  202. exit;
  203. end;
  204. close(printnodefile);
  205. end;
  206. procedure printnode_procdef(pd:tprocdef);
  207. begin
  208. assign(printnodefile,treelogfilename);
  209. {$I-}
  210. append(printnodefile);
  211. if ioresult<>0 then
  212. rewrite(printnodefile);
  213. {$I+}
  214. if ioresult<>0 then
  215. begin
  216. Comment(V_Error,'Error creating '+treelogfilename);
  217. exit;
  218. end;
  219. writeln(printnodefile);
  220. writeln(printnodefile,'*******************************************************************************');
  221. writeln(printnodefile,current_procinfo.procdef.fullprocname(false));
  222. writeln(printnodefile,'*******************************************************************************');
  223. printnode(printnodefile,pd.code);
  224. close(printnodefile);
  225. end;
  226. function generate_bodyentry_block:tnode;
  227. var
  228. srsym : tsym;
  229. para : tcallparanode;
  230. newstatement : tstatementnode;
  231. htype : ttype;
  232. begin
  233. result:=internalstatements(newstatement,true);
  234. if assigned(current_procinfo.procdef._class) then
  235. begin
  236. { a constructor needs a help procedure }
  237. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  238. begin
  239. if is_class(current_procinfo.procdef._class) then
  240. begin
  241. if (cs_implicit_exceptions in aktmoduleswitches) then
  242. include(current_procinfo.flags,pi_needs_implicit_finally);
  243. srsym:=search_class_member(current_procinfo.procdef._class,'NEWINSTANCE');
  244. if assigned(srsym) and
  245. (srsym.typ=procsym) then
  246. begin
  247. { if vmt<>0 then newinstance }
  248. addstatement(newstatement,cifnode.create(
  249. caddnode.create(unequaln,
  250. load_vmt_pointer_node,
  251. cnilnode.create),
  252. cassignmentnode.create(
  253. ctypeconvnode.create_explicit(
  254. load_self_pointer_node,
  255. voidpointertype),
  256. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_vmt_pointer_node)),
  257. nil));
  258. end
  259. else
  260. internalerror(200305108);
  261. end
  262. else
  263. if is_object(current_procinfo.procdef._class) then
  264. begin
  265. htype.setdef(current_procinfo.procdef._class);
  266. htype.setdef(tpointerdef.create(htype));
  267. { parameter 3 : vmt_offset }
  268. { parameter 2 : address of pointer to vmt,
  269. this is required to allow setting the vmt to -1 to indicate
  270. that memory was allocated }
  271. { parameter 1 : self pointer }
  272. para:=ccallparanode.create(
  273. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32bittype,false),
  274. ccallparanode.create(
  275. ctypeconvnode.create_explicit(
  276. load_vmt_pointer_node,
  277. voidpointertype),
  278. ccallparanode.create(
  279. ctypeconvnode.create_explicit(
  280. load_self_pointer_node,
  281. voidpointertype),
  282. nil)));
  283. addstatement(newstatement,cassignmentnode.create(
  284. ctypeconvnode.create_explicit(
  285. load_self_pointer_node,
  286. voidpointertype),
  287. ccallnode.createintern('fpc_help_constructor',para)));
  288. end
  289. else
  290. internalerror(200305103);
  291. { if self=nil then exit
  292. calling fail instead of exit is useless because
  293. there is nothing to dispose (PFV) }
  294. addstatement(newstatement,cifnode.create(
  295. caddnode.create(equaln,
  296. load_self_pointer_node,
  297. cnilnode.create),
  298. cexitnode.create(nil),
  299. nil));
  300. end;
  301. { maybe call BeforeDestruction for classes }
  302. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  303. is_class(current_procinfo.procdef._class) then
  304. begin
  305. srsym:=search_class_member(current_procinfo.procdef._class,'BEFOREDESTRUCTION');
  306. if assigned(srsym) and
  307. (srsym.typ=procsym) then
  308. begin
  309. { if vmt<>0 then beforedestruction }
  310. addstatement(newstatement,cifnode.create(
  311. caddnode.create(unequaln,
  312. load_vmt_pointer_node,
  313. cnilnode.create),
  314. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  315. nil));
  316. end
  317. else
  318. internalerror(200305104);
  319. end;
  320. end;
  321. end;
  322. function generate_bodyexit_block:tnode;
  323. var
  324. srsym : tsym;
  325. para : tcallparanode;
  326. newstatement : tstatementnode;
  327. begin
  328. result:=internalstatements(newstatement,true);
  329. if assigned(current_procinfo.procdef._class) then
  330. begin
  331. { maybe call AfterConstruction for classes }
  332. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  333. is_class(current_procinfo.procdef._class) then
  334. begin
  335. srsym:=search_class_member(current_procinfo.procdef._class,'AFTERCONSTRUCTION');
  336. if assigned(srsym) and
  337. (srsym.typ=procsym) then
  338. begin
  339. { Self can be nil when fail is called }
  340. { if self<>nil and vmt<>nil then afterconstruction }
  341. addstatement(newstatement,cifnode.create(
  342. caddnode.create(andn,
  343. caddnode.create(unequaln,
  344. load_self_pointer_node,
  345. cnilnode.create),
  346. caddnode.create(unequaln,
  347. load_vmt_pointer_node,
  348. cnilnode.create)),
  349. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  350. nil));
  351. end
  352. else
  353. internalerror(200305106);
  354. end;
  355. { a destructor needs a help procedure }
  356. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  357. begin
  358. if is_class(current_procinfo.procdef._class) then
  359. begin
  360. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  361. if assigned(srsym) and
  362. (srsym.typ=procsym) then
  363. begin
  364. { if self<>0 and vmt=1 then freeinstance }
  365. addstatement(newstatement,cifnode.create(
  366. caddnode.create(andn,
  367. caddnode.create(unequaln,
  368. load_self_pointer_node,
  369. cnilnode.create),
  370. caddnode.create(equaln,
  371. ctypeconvnode.create(
  372. load_vmt_pointer_node,
  373. voidpointertype),
  374. cpointerconstnode.create(1,voidpointertype))),
  375. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  376. nil));
  377. end
  378. else
  379. internalerror(200305108);
  380. end
  381. else
  382. if is_object(current_procinfo.procdef._class) then
  383. begin
  384. { finalize object data }
  385. if current_procinfo.procdef._class.needs_inittable then
  386. addstatement(newstatement,finalize_data_node(load_self_node));
  387. { parameter 3 : vmt_offset }
  388. { parameter 2 : pointer to vmt }
  389. { parameter 1 : self pointer }
  390. para:=ccallparanode.create(
  391. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32bittype,false),
  392. ccallparanode.create(
  393. ctypeconvnode.create_explicit(
  394. load_vmt_pointer_node,
  395. voidpointertype),
  396. ccallparanode.create(
  397. ctypeconvnode.create_explicit(
  398. load_self_pointer_node,
  399. voidpointertype),
  400. nil)));
  401. addstatement(newstatement,
  402. ccallnode.createintern('fpc_help_destructor',para));
  403. end
  404. else
  405. internalerror(200305105);
  406. end;
  407. end;
  408. end;
  409. function generate_except_block:tnode;
  410. var
  411. pd : tprocdef;
  412. newstatement : tstatementnode;
  413. begin
  414. generate_except_block:=internalstatements(newstatement,true);
  415. { a constructor needs call destructor (if available) when it
  416. is not inherited }
  417. if assigned(current_procinfo.procdef._class) and
  418. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  419. begin
  420. pd:=current_procinfo.procdef._class.searchdestructor;
  421. if assigned(pd) then
  422. begin
  423. { if vmt<>0 then call destructor }
  424. addstatement(newstatement,cifnode.create(
  425. caddnode.create(unequaln,
  426. load_vmt_pointer_node,
  427. cnilnode.create),
  428. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node),
  429. nil));
  430. end;
  431. end
  432. else
  433. begin
  434. { no constructor }
  435. { must be the return value finalized before reraising the exception? }
  436. if (not is_void(current_procinfo.procdef.rettype.def)) and
  437. (current_procinfo.procdef.rettype.def.needs_inittable) and
  438. (not is_class(current_procinfo.procdef.rettype.def)) then
  439. addstatement(newstatement,finalize_data_node(load_result_node));
  440. end;
  441. end;
  442. {****************************************************************************
  443. TCGProcInfo
  444. ****************************************************************************}
  445. constructor tcgprocinfo.create(aparent:tprocinfo);
  446. begin
  447. inherited Create(aparent);
  448. nestedprocs:=tlinkedlist.create;
  449. end;
  450. destructor tcgprocinfo.destroy;
  451. begin
  452. nestedprocs.free;
  453. if assigned(code) then
  454. code.free;
  455. inherited destroy;
  456. end;
  457. procedure tcgprocinfo.add_entry_exit_code;
  458. var
  459. finalcode,
  460. bodyentrycode,
  461. bodyexitcode,
  462. exceptcode : tnode;
  463. newblock : tblocknode;
  464. codestatement,
  465. newstatement : tstatementnode;
  466. oldfilepos : tfileposinfo;
  467. begin
  468. oldfilepos:=aktfilepos;
  469. { Generate code/locations used at start of proc }
  470. aktfilepos:=entrypos;
  471. entry_asmnode:=casmnode.create_get_position;
  472. loadpara_asmnode:=casmnode.create_get_position;
  473. init_asmnode:=casmnode.create_get_position;
  474. bodyentrycode:=generate_bodyentry_block;
  475. { Generate code/locations used at end of proc }
  476. aktfilepos:=exitpos;
  477. exitlabel_asmnode:=casmnode.create_get_position;
  478. final_asmnode:=casmnode.create_get_position;
  479. bodyexitcode:=generate_bodyexit_block;
  480. { Generate procedure by combining init+body+final,
  481. depending on the implicit finally we need to add
  482. an try...finally...end wrapper }
  483. newblock:=internalstatements(newstatement,true);
  484. if (pi_needs_implicit_finally in current_procinfo.flags) and
  485. { but it's useless in init/final code of units }
  486. not(current_procinfo.procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  487. begin
  488. { Generate special exception block only needed when
  489. implicit finaly is used }
  490. aktfilepos:=exitpos;
  491. exceptcode:=generate_except_block;
  492. { Generate code that will be in the try...finally }
  493. finalcode:=internalstatements(codestatement,true);
  494. addstatement(codestatement,bodyexitcode);
  495. addstatement(codestatement,final_asmnode);
  496. { Initialize before try...finally...end frame }
  497. addstatement(newstatement,entry_asmnode);
  498. addstatement(newstatement,loadpara_asmnode);
  499. addstatement(newstatement,init_asmnode);
  500. addstatement(newstatement,bodyentrycode);
  501. aktfilepos:=entrypos;
  502. addstatement(newstatement,ctryfinallynode.create_implicit(
  503. code,
  504. finalcode,
  505. exceptcode));
  506. addstatement(newstatement,exitlabel_asmnode);
  507. end
  508. else
  509. begin
  510. addstatement(newstatement,entry_asmnode);
  511. addstatement(newstatement,loadpara_asmnode);
  512. addstatement(newstatement,init_asmnode);
  513. addstatement(newstatement,bodyentrycode);
  514. addstatement(newstatement,code);
  515. addstatement(newstatement,exitlabel_asmnode);
  516. addstatement(newstatement,bodyexitcode);
  517. addstatement(newstatement,final_asmnode);
  518. end;
  519. resulttypepass(newblock);
  520. code:=newblock;
  521. aktfilepos:=oldfilepos;
  522. end;
  523. procedure clearrefs(p : tnamedindexitem;arg:pointer);
  524. begin
  525. if (tsym(p).typ=varsym) then
  526. if tvarsym(p).refs>1 then
  527. tvarsym(p).refs:=1;
  528. end;
  529. procedure tcgprocinfo.generate_code;
  530. var
  531. oldprocinfo : tprocinfo;
  532. oldaktmaxfpuregisters : longint;
  533. oldfilepos : tfileposinfo;
  534. templist : Taasmoutput;
  535. headertai : tai;
  536. usesacc,
  537. usesfpu,
  538. usesacchi : boolean;
  539. begin
  540. { the initialization procedure can be empty, then we
  541. don't need to generate anything. When it was an empty
  542. procedure there would be at least a blocknode }
  543. if not assigned(code) then
  544. exit;
  545. { We need valid code }
  546. if Errorcount<>0 then
  547. exit;
  548. { The RA and Tempgen shall not be available yet }
  549. if assigned(tg) then
  550. internalerror(200309201);
  551. oldprocinfo:=current_procinfo;
  552. oldfilepos:=aktfilepos;
  553. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  554. current_procinfo:=self;
  555. { get new labels }
  556. aktbreaklabel:=nil;
  557. aktcontinuelabel:=nil;
  558. templist:=Taasmoutput.create;
  559. { add parast/localst to symtablestack }
  560. add_to_symtablestack;
  561. { when size optimization only count occurrence }
  562. if cs_littlesize in aktglobalswitches then
  563. cg.t_times:=1
  564. else
  565. { reference for repetition is 100 }
  566. cg.t_times:=100;
  567. { clear register count }
  568. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  569. symtablestack.next.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  570. { firstpass everything }
  571. flowcontrol:=[];
  572. do_firstpass(code);
  573. { only do secondpass if there are no errors }
  574. if ErrorCount=0 then
  575. begin
  576. { set the start offset to the start of the temp area in the stack }
  577. tg:=ttgobj.create;
  578. { Create register allocator }
  579. cg.init_register_allocators;
  580. current_procinfo.set_first_temp_offset;
  581. current_procinfo.generate_parameter_info;
  582. { Allocate space in temp/registers for parast and localst }
  583. aktfilepos:=entrypos;
  584. gen_alloc_parast(aktproccode,tparasymtable(current_procinfo.procdef.parast));
  585. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  586. gen_alloc_localst(aktproccode,tlocalsymtable(current_procinfo.procdef.localst));
  587. if (cs_asm_source in aktglobalswitches) then
  588. aktproccode.concat(Tai_comment.Create(strpnew('Temps start at '+std_regname(current_procinfo.framepointer)+
  589. tostr_with_plus(tg.lasttemp))));
  590. { Generate code to load register parameters in temps and insert local
  591. copies for values parameters. This must be done before the code for the
  592. body is generated because the localloc is updated.
  593. Note: The generated code will be inserted after the code generation of
  594. the body is finished, because only then the position is known }
  595. aktfilepos:=entrypos;
  596. gen_load_para_value(templist);
  597. { caller paraloc info is also necessary in the stackframe_entry
  598. code of the ppc (and possibly other processors) }
  599. if not procdef.has_paraloc_info then
  600. begin
  601. paramanager.create_paraloc_info(procdef,callerside);
  602. procdef.has_paraloc_info:=true;
  603. end;
  604. { generate code for the node tree }
  605. do_secondpass(code);
  606. current_procinfo.aktproccode.concatlist(exprasmlist);
  607. {$ifdef i386}
  608. procdef.fpu_used:=code.registersfpu;
  609. {$endif i386}
  610. { The position of the loadpara_asmnode is now known }
  611. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  612. { first generate entry and initialize code with the correct
  613. position and switches }
  614. aktfilepos:=entrypos;
  615. aktlocalswitches:=entryswitches;
  616. gen_entry_code(templist);
  617. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  618. gen_initialize_code(templist,false);
  619. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  620. { now generate finalize and exit code with the correct position
  621. and switches }
  622. aktfilepos:=exitpos;
  623. aktlocalswitches:=exitswitches;
  624. gen_finalize_code(templist,false);
  625. { the finalcode must be concated if there was no position available,
  626. using insertlistafter will result in an insert at the start
  627. when currentai=nil }
  628. if assigned(final_asmnode.currenttai) then
  629. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  630. else
  631. aktproccode.concatlist(templist);
  632. { insert exit label at the correct position }
  633. cg.a_label(templist,current_procinfo.aktexitlabel);
  634. if assigned(exitlabel_asmnode.currenttai) then
  635. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  636. else
  637. aktproccode.concatlist(templist);
  638. { exit code }
  639. gen_exit_code(templist);
  640. aktproccode.concatlist(templist);
  641. {$ifdef OLDREGVARS}
  642. { note: this must be done only after as much code as possible has }
  643. { been generated. The result is that when you ungetregister() a }
  644. { regvar, it will actually free the regvar (and alse free the }
  645. { the regvars at the same time). Doing this too early will }
  646. { confuse the register allocator, as the regvars will still be }
  647. { used. It should be done before loading the result regs (so }
  648. { they don't conflict with the regvars) and before }
  649. { gen_entry_code (that one has to be able to allocate the }
  650. { regvars again) (JM) }
  651. free_regvars(aktproccode);
  652. {$endif OLDREGVARS}
  653. { add code that will load the return value, this is not done
  654. for assembler routines when they didn't reference the result
  655. variable }
  656. usesacc:=false;
  657. usesfpu:=false;
  658. usesacchi:=false;
  659. gen_load_return_value(templist,usesacc,usesacchi,usesfpu);
  660. aktproccode.concatlist(templist);
  661. { generate symbol and save end of header position }
  662. aktfilepos:=entrypos;
  663. gen_proc_symbol(templist);
  664. headertai:=tai(templist.last);
  665. { insert symbol }
  666. aktproccode.insertlist(templist);
  667. { Free space in temp/registers for parast and localst, must be
  668. done after gen_entry_code }
  669. aktfilepos:=exitpos;
  670. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  671. gen_free_localst(aktproccode,tlocalsymtable(current_procinfo.procdef.localst));
  672. gen_free_parast(aktproccode,tparasymtable(current_procinfo.procdef.parast));
  673. { The procedure body is finished, we can now
  674. allocate the registers }
  675. if not(cs_no_regalloc in aktglobalswitches) then
  676. begin
  677. cg.do_register_allocation(aktproccode,headertai);
  678. (*
  679. {$ifndef NoOpt}
  680. if (cs_optimize in aktglobalswitches) and
  681. { do not optimize pure assembler procedures }
  682. not(pi_is_assembler in current_procinfo.flags) then
  683. optimize(aktproccode);
  684. {$endif NoOpt}
  685. *)
  686. end;
  687. { Add save and restore of used registers }
  688. aktfilepos:=entrypos;
  689. gen_save_used_regs(templist);
  690. aktproccode.insertlistafter(headertai,templist);
  691. aktfilepos:=exitpos;
  692. gen_restore_used_regs(aktproccode,usesacc,usesacchi,usesfpu);
  693. { Add stack allocation code after header }
  694. aktfilepos:=entrypos;
  695. gen_stackalloc_code(templist);
  696. aktproccode.insertlistafter(headertai,templist);
  697. { Add exit code at the end }
  698. aktfilepos:=exitpos;
  699. gen_stackfree_code(templist,usesacc,usesacchi);
  700. aktproccode.concatlist(templist);
  701. { Add end symbol and debug info }
  702. aktfilepos:=exitpos;
  703. gen_proc_symbol_end(templist);
  704. aktproccode.concatlist(templist);
  705. { save local data (casetable) also in the same file }
  706. if assigned(aktlocaldata) and
  707. (not aktlocaldata.empty) then
  708. begin
  709. { because of the limited constant size of the arm, all data access is done pc relative }
  710. if target_info.cpu=cpu_arm then
  711. aktproccode.concatlist(aktlocaldata)
  712. else
  713. begin
  714. aktproccode.concat(Tai_section.Create(sec_data));
  715. aktproccode.concatlist(aktlocaldata);
  716. aktproccode.concat(Tai_section.Create(sec_code));
  717. end;
  718. end;
  719. { add the procedure to the codesegment }
  720. if (cs_create_smart in aktmoduleswitches) then
  721. codesegment.concat(Tai_cut.Create);
  722. codesegment.concatlist(aktproccode);
  723. { only now we can remove the temps }
  724. tg.resettempgen;
  725. { stop tempgen and ra }
  726. tg.free;
  727. cg.done_register_allocators;
  728. tg:=nil;
  729. end;
  730. { restore symtablestack }
  731. remove_from_symtablestack;
  732. { restore }
  733. templist.free;
  734. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  735. aktfilepos:=oldfilepos;
  736. current_procinfo:=oldprocinfo;
  737. end;
  738. procedure tcgprocinfo.add_to_symtablestack;
  739. var
  740. _class,hp : tobjectdef;
  741. begin
  742. { insert symtables for the class, but only if it is no nested function }
  743. if assigned(procdef._class) and
  744. not(assigned(parent) and
  745. assigned(parent.procdef) and
  746. assigned(parent.procdef._class)) then
  747. begin
  748. { insert them in the reverse order }
  749. hp:=nil;
  750. repeat
  751. _class:=procdef._class;
  752. while _class.childof<>hp do
  753. _class:=_class.childof;
  754. hp:=_class;
  755. _class.symtable.next:=symtablestack;
  756. symtablestack:=_class.symtable;
  757. until hp=procdef._class;
  758. end;
  759. { insert parasymtable in symtablestack when parsing
  760. a function }
  761. if procdef.parast.symtablelevel>=normal_function_level then
  762. begin
  763. procdef.parast.next:=symtablestack;
  764. symtablestack:=procdef.parast;
  765. end;
  766. procdef.localst.next:=symtablestack;
  767. symtablestack:=procdef.localst;
  768. end;
  769. procedure tcgprocinfo.remove_from_symtablestack;
  770. begin
  771. { remove localst/parast }
  772. if procdef.parast.symtablelevel>=normal_function_level then
  773. symtablestack:=symtablestack.next.next
  774. else
  775. symtablestack:=symtablestack.next;
  776. { remove class member symbol tables }
  777. while symtablestack.symtabletype=objectsymtable do
  778. symtablestack:=symtablestack.next;
  779. end;
  780. procedure tcgprocinfo.resetprocdef;
  781. begin
  782. { the local symtables can be deleted, but the parast }
  783. { doesn't, (checking definitons when calling a }
  784. { function }
  785. { not for a inline procedure !! (PM) }
  786. { at lexlevel = 1 localst is the staticsymtable itself }
  787. { so no dispose here !! }
  788. if assigned(code) and
  789. not(cs_browser in aktmoduleswitches) and
  790. (procdef.proccalloption<>pocall_inline) then
  791. begin
  792. if procdef.parast.symtablelevel>=normal_function_level then
  793. procdef.localst.free;
  794. procdef.localst:=nil;
  795. end;
  796. { remove code tree, if not inline procedure }
  797. if assigned(code) then
  798. begin
  799. { the inline procedure has already got a copy of the tree
  800. stored in current_procinfo.procdef.code }
  801. code.free;
  802. code:=nil;
  803. if (procdef.proccalloption<>pocall_inline) then
  804. procdef.code:=nil;
  805. end;
  806. end;
  807. procedure tcgprocinfo.parse_body;
  808. var
  809. oldprocinfo : tprocinfo;
  810. begin
  811. oldprocinfo:=current_procinfo;
  812. current_procinfo:=self;
  813. { calculate the lexical level }
  814. if procdef.parast.symtablelevel>maxnesting then
  815. Message(parser_e_too_much_lexlevel);
  816. { static is also important for local procedures !! }
  817. if (po_staticmethod in procdef.procoptions) then
  818. allow_only_static:=true
  819. else if (procdef.parast.symtablelevel=normal_function_level) then
  820. allow_only_static:=false;
  821. { reset break and continue labels }
  822. block_type:=bt_general;
  823. {$ifdef state_tracking}
  824. { aktstate:=Tstate_storage.create;}
  825. {$endif state_tracking}
  826. { create a local symbol table for this routine }
  827. if not assigned(procdef.localst) then
  828. procdef.insert_localst;
  829. { add parast/localst to symtablestack }
  830. add_to_symtablestack;
  831. { constant symbols are inserted in this symboltable }
  832. constsymtable:=symtablestack;
  833. { save entry info }
  834. entrypos:=aktfilepos;
  835. entryswitches:=aktlocalswitches;
  836. { parse the code ... }
  837. code:=block(current_module.islibrary);
  838. { save exit info }
  839. exitswitches:=aktlocalswitches;
  840. exitpos:=last_endtoken_filepos;
  841. if assigned(code) then
  842. begin
  843. { get a better entry point }
  844. entrypos:=code.fileinfo;
  845. { the procedure is now defined }
  846. procdef.forwarddef:=false;
  847. if (Errorcount=0) then
  848. begin
  849. { add implicit entry and exit code }
  850. add_entry_exit_code;
  851. { check if forwards are resolved }
  852. tstoredsymtable(procdef.localst).check_forwards;
  853. { check if all labels are used }
  854. tstoredsymtable(procdef.localst).checklabels;
  855. { remove cross unit overloads }
  856. tstoredsymtable(procdef.localst).unchain_overloaded;
  857. end;
  858. { check for unused symbols, but only if there is no asm block }
  859. if not(pi_uses_asm in flags) then
  860. begin
  861. { not for unit init, becuase the var can be used in finalize,
  862. it will be done in proc_unit }
  863. if not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  864. tstoredsymtable(procdef.localst).allsymbolsused;
  865. tstoredsymtable(procdef.parast).allsymbolsused;
  866. end;
  867. { Finish type checking pass }
  868. do_resulttypepass(code);
  869. { Print the node to tree.log }
  870. if paraprintnodetree=1 then
  871. printnode_procdef(procdef);
  872. end;
  873. { store a copy of the original tree for inline, for
  874. normal procedures only store a reference to the
  875. current tree }
  876. if (procdef.proccalloption=pocall_inline) then
  877. procdef.code:=code.getcopy
  878. else
  879. procdef.code:=code;
  880. { ... remove symbol tables }
  881. remove_from_symtablestack;
  882. {$ifdef state_tracking}
  883. { aktstate.destroy;}
  884. {$endif state_tracking}
  885. { reset to normal non static function }
  886. if (current_procinfo.procdef.parast.symtablelevel=normal_function_level) then
  887. allow_only_static:=false;
  888. current_procinfo:=oldprocinfo;
  889. end;
  890. {****************************************************************************
  891. PROCEDURE/FUNCTION PARSING
  892. ****************************************************************************}
  893. procedure check_init_paras(p:tnamedindexitem;arg:pointer);
  894. begin
  895. if tsym(p).typ<>varsym then
  896. exit;
  897. with tvarsym(p) do
  898. if (not is_class(vartype.def) and
  899. vartype.def.needs_inittable and
  900. (varspez in [vs_value,vs_out])) then
  901. include(current_procinfo.flags,pi_do_call);
  902. end;
  903. procedure read_proc;
  904. {
  905. Parses the procedure directives, then parses the procedure body, then
  906. generates the code for it
  907. }
  908. procedure do_generate_code(pi:tcgprocinfo);
  909. var
  910. hpi : tcgprocinfo;
  911. begin
  912. { generate code for this procedure }
  913. pi.generate_code;
  914. { process nested procs }
  915. hpi:=tcgprocinfo(pi.nestedprocs.first);
  916. while assigned(hpi) do
  917. begin
  918. do_generate_code(hpi);
  919. hpi:=tcgprocinfo(hpi.next);
  920. end;
  921. pi.resetprocdef;
  922. end;
  923. var
  924. old_current_procinfo : tprocinfo;
  925. oldconstsymtable : tsymtable;
  926. oldfailtokenmode : tmodeswitch;
  927. pdflags : tpdflags;
  928. pd : tprocdef;
  929. isnestedproc : boolean;
  930. begin
  931. { save old state }
  932. oldconstsymtable:=constsymtable;
  933. old_current_procinfo:=current_procinfo;
  934. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  935. to an other procdef }
  936. current_procinfo:=nil;
  937. { parse procedure declaration }
  938. if assigned(old_current_procinfo) and
  939. assigned(old_current_procinfo.procdef) then
  940. pd:=parse_proc_dec(old_current_procinfo.procdef._class)
  941. else
  942. pd:=parse_proc_dec(nil);
  943. { set the default function options }
  944. if parse_only then
  945. begin
  946. pd.forwarddef:=true;
  947. { set also the interface flag, for better error message when the
  948. implementation doesn't much this header }
  949. pd.interfacedef:=true;
  950. include(pd.procoptions,po_public);
  951. pdflags:=[pd_interface];
  952. end
  953. else
  954. begin
  955. pdflags:=[pd_body];
  956. if (not current_module.in_interface) then
  957. include(pdflags,pd_implemen);
  958. if (not current_module.is_unit) or
  959. (cs_create_smart in aktmoduleswitches) then
  960. include(pd.procoptions,po_public);
  961. pd.forwarddef:=false;
  962. end;
  963. { parse the directives that may follow }
  964. parse_proc_directives(pd,pdflags);
  965. { hint directives, these can be separated by semicolons here,
  966. that needs to be handled here with a loop (PFV) }
  967. while try_consume_hintdirective(pd.symoptions) do
  968. Consume(_SEMICOLON);
  969. { Set calling convention }
  970. handle_calling_convention(pd);
  971. { everything of the proc definition is known, we can now
  972. calculate the parameters }
  973. calc_parast(pd);
  974. { search for forward declarations }
  975. if not proc_add_definition(pd) then
  976. begin
  977. { A method must be forward defined (in the object declaration) }
  978. if assigned(pd._class) and
  979. (not assigned(old_current_procinfo.procdef._class)) then
  980. begin
  981. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  982. tprocsym(pd.procsym).write_parameter_lists(pd);
  983. end
  984. else
  985. begin
  986. { Give a better error if there is a forward def in the interface and only
  987. a single implementation }
  988. if (not pd.forwarddef) and
  989. (not pd.interfacedef) and
  990. (tprocsym(pd.procsym).procdef_count>1) and
  991. tprocsym(pd.procsym).first_procdef.forwarddef and
  992. tprocsym(pd.procsym).first_procdef.interfacedef and
  993. not(tprocsym(pd.procsym).procdef_count>2) then
  994. begin
  995. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  996. tprocsym(pd.procsym).write_parameter_lists(pd);
  997. end;
  998. end;
  999. end;
  1000. { compile procedure when a body is needed }
  1001. if (pd_body in pdflags) then
  1002. begin
  1003. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1004. { create a new procedure }
  1005. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1006. current_module.procinfo:=current_procinfo;
  1007. current_procinfo.procdef:=pd;
  1008. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1009. { Insert mangledname }
  1010. pd.aliasnames.insert(pd.mangledname);
  1011. { Insert result variables in the localst }
  1012. insert_funcret_local(pd);
  1013. { check if there are para's which require initing -> set }
  1014. { pi_do_call (if not yet set) }
  1015. if not(pi_do_call in current_procinfo.flags) then
  1016. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_init_paras,nil);
  1017. { set _FAIL as keyword if constructor }
  1018. if (pd.proctypeoption=potype_constructor) then
  1019. begin
  1020. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1021. tokeninfo^[_FAIL].keyword:=m_all;
  1022. end;
  1023. tcgprocinfo(current_procinfo).parse_body;
  1024. { When it's a nested procedure then defer the code generation,
  1025. when back at normal function level then generate the code
  1026. for all defered nested procedures and the current procedure }
  1027. if isnestedproc then
  1028. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1029. else
  1030. begin
  1031. { We can't support inlining for procedures that have nested
  1032. procedures because the nested procedures use a fixed offset
  1033. for accessing locals in the parent procedure (PFV) }
  1034. if (current_procinfo.procdef.proccalloption=pocall_inline) and
  1035. (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1036. begin
  1037. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1038. Message(parser_w_inlining_disabled);
  1039. current_procinfo.procdef.proccalloption:=pocall_default;
  1040. end;
  1041. do_generate_code(tcgprocinfo(current_procinfo));
  1042. end;
  1043. { reset _FAIL as _SELF normal }
  1044. if (pd.proctypeoption=potype_constructor) then
  1045. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1046. { release procinfo }
  1047. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1048. internalerror(200304274);
  1049. current_module.procinfo:=current_procinfo.parent;
  1050. if not isnestedproc then
  1051. current_procinfo.free;
  1052. consume(_SEMICOLON);
  1053. end;
  1054. { Restore old state }
  1055. constsymtable:=oldconstsymtable;
  1056. current_procinfo:=old_current_procinfo;
  1057. end;
  1058. {****************************************************************************
  1059. DECLARATION PARSING
  1060. ****************************************************************************}
  1061. { search in symtablestack for not complete classes }
  1062. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  1063. begin
  1064. if (tsym(p).typ=typesym) and
  1065. (ttypesym(p).restype.def.deftype=objectdef) and
  1066. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  1067. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1068. end;
  1069. procedure read_declarations(islibrary : boolean);
  1070. begin
  1071. repeat
  1072. if not assigned(current_procinfo) then
  1073. internalerror(200304251);
  1074. case token of
  1075. _LABEL:
  1076. begin
  1077. label_dec;
  1078. end;
  1079. _CONST:
  1080. begin
  1081. const_dec;
  1082. end;
  1083. _TYPE:
  1084. begin
  1085. type_dec;
  1086. end;
  1087. _VAR:
  1088. var_dec;
  1089. _THREADVAR:
  1090. threadvar_dec;
  1091. _CONSTRUCTOR,
  1092. _DESTRUCTOR,
  1093. _FUNCTION,
  1094. _PROCEDURE,
  1095. _OPERATOR,
  1096. _CLASS:
  1097. read_proc;
  1098. _RESOURCESTRING:
  1099. resourcestring_dec;
  1100. _EXPORTS:
  1101. begin
  1102. if not(assigned(current_procinfo.procdef.localst)) or
  1103. (current_procinfo.procdef.localst.symtablelevel>main_program_level) or
  1104. (current_module.is_unit) then
  1105. begin
  1106. Message(parser_e_syntax_error);
  1107. consume_all_until(_SEMICOLON);
  1108. end
  1109. else if islibrary or
  1110. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware]) then
  1111. read_exports
  1112. else
  1113. begin
  1114. Message(parser_w_unsupported_feature);
  1115. consume(_BEGIN);
  1116. end;
  1117. end
  1118. else
  1119. break;
  1120. end;
  1121. until false;
  1122. { check for incomplete class definitions, this is only required
  1123. for fpc modes }
  1124. if (m_fpc in aktmodeswitches) then
  1125. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1126. end;
  1127. procedure read_interface_declarations;
  1128. begin
  1129. repeat
  1130. case token of
  1131. _CONST :
  1132. const_dec;
  1133. _TYPE :
  1134. type_dec;
  1135. _VAR :
  1136. var_dec;
  1137. _THREADVAR :
  1138. threadvar_dec;
  1139. _RESOURCESTRING:
  1140. resourcestring_dec;
  1141. _FUNCTION,
  1142. _PROCEDURE,
  1143. _OPERATOR :
  1144. read_proc;
  1145. else
  1146. break;
  1147. end;
  1148. until false;
  1149. { check for incomplete class definitions, this is only required
  1150. for fpc modes }
  1151. if (m_fpc in aktmodeswitches) then
  1152. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1153. end;
  1154. end.
  1155. {
  1156. $Log$
  1157. Revision 1.169 2003-10-31 15:52:18 peter
  1158. * fix crash with fail in constructor
  1159. Revision 1.168 2003/10/30 16:22:40 peter
  1160. * call firstpass before allocation and codegeneration is started
  1161. * move leftover code from pass_2.generatecode() to psub
  1162. Revision 1.167 2003/10/24 17:40:23 peter
  1163. * cleanup of the entry and exit code insertion
  1164. Revision 1.166 2003/10/21 15:14:33 peter
  1165. * fixed memleak for initfinalcode
  1166. * exit from generatecode when there are already errors
  1167. Revision 1.165 2003/10/20 19:28:51 peter
  1168. * disable inlining when nested procedures are found
  1169. Revision 1.164 2003/10/19 01:34:30 florian
  1170. * some ppc stuff fixed
  1171. * memory leak fixed
  1172. Revision 1.163 2003/10/17 14:38:32 peter
  1173. * 64k registers supported
  1174. * fixed some memory leaks
  1175. Revision 1.162 2003/10/10 17:48:13 peter
  1176. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1177. * tregisteralloctor renamed to trgobj
  1178. * removed rgobj from a lot of units
  1179. * moved location_* and reference_* to cgobj
  1180. * first things for mmx register allocation
  1181. Revision 1.161 2003/10/09 21:31:37 daniel
  1182. * Register allocator splitted, ans abstract now
  1183. Revision 1.160 2003/10/09 15:20:56 peter
  1184. * self is not a token anymore. It is handled special when found
  1185. in a code block and when parsing an method
  1186. Revision 1.159 2003/10/07 15:17:07 peter
  1187. * inline supported again, LOC_REFERENCEs are used to pass the
  1188. parameters
  1189. * inlineparasymtable,inlinelocalsymtable removed
  1190. * exitlabel inserting fixed
  1191. Revision 1.158 2003/10/06 22:23:41 florian
  1192. + added basic olevariant support
  1193. Revision 1.157 2003/10/03 14:45:09 peter
  1194. * more proc directive for procvar fixes
  1195. Revision 1.156 2003/10/02 21:20:32 peter
  1196. * handle_calling_convention removed from parse_proc_directive to
  1197. separate call
  1198. Revision 1.155 2003/10/01 20:34:49 peter
  1199. * procinfo unit contains tprocinfo
  1200. * cginfo renamed to cgbase
  1201. * moved cgmessage to verbose
  1202. * fixed ppc and sparc compiles
  1203. Revision 1.154 2003/09/29 20:58:56 peter
  1204. * optimized releasing of registers
  1205. Revision 1.153 2003/09/28 17:55:04 peter
  1206. * parent framepointer changed to hidden parameter
  1207. * tloadparentfpnode added
  1208. Revision 1.152 2003/09/27 13:29:43 peter
  1209. * fix reported file position for not matched forwards
  1210. Revision 1.151 2003/09/25 21:25:13 peter
  1211. * remove allocate_intterupt_parameter, allocation is platform
  1212. dependent and needs to be done in create_paraloc_info
  1213. Revision 1.150 2003/09/25 16:19:32 peter
  1214. * fix filepositions
  1215. * insert spill temp allocations at the start of the proc
  1216. Revision 1.149 2003/09/23 17:56:06 peter
  1217. * locals and paras are allocated in the code generation
  1218. * tvarsym.localloc contains the location of para/local when
  1219. generating code for the current procedure
  1220. Revision 1.148 2003/09/14 19:18:10 peter
  1221. * remove obsolete code already in comments
  1222. Revision 1.147 2003/09/14 12:58:00 peter
  1223. * support mulitple overloads in implementation, this is delphi
  1224. compatible
  1225. * procsym only stores the overloads available in the interface
  1226. Revision 1.146 2003/09/12 19:07:42 daniel
  1227. * Fixed fast spilling functionality by re-adding the code that initializes
  1228. precoloured nodes to degree 255. I would like to play hangman on the one
  1229. who removed that code.
  1230. Revision 1.145 2003/09/10 19:14:31 daniel
  1231. * Failed attempt to restore broken fastspill functionality
  1232. Revision 1.144 2003/09/09 20:59:27 daniel
  1233. * Adding register allocation order
  1234. Revision 1.143 2003/09/09 15:55:44 peter
  1235. * use register with least interferences in spillregister
  1236. Revision 1.142 2003/09/07 22:09:35 peter
  1237. * preparations for different default calling conventions
  1238. * various RA fixes
  1239. Revision 1.141 2003/09/04 14:46:12 peter
  1240. * abort with IE when spilling requires > 20 loops
  1241. Revision 1.140 2003/09/03 15:55:01 peter
  1242. * NEWRA branch merged
  1243. Revision 1.139 2003/09/03 11:18:37 florian
  1244. * fixed arm concatcopy
  1245. + arm support in the common compiler sources added
  1246. * moved some generic cg code around
  1247. + tfputype added
  1248. * ...
  1249. Revision 1.138.2.1 2003/08/31 13:50:16 daniel
  1250. * Remove sorting and use pregenerated indexes
  1251. * Some work on making things compile
  1252. Revision 1.138 2003/08/20 17:48:49 peter
  1253. * fixed stackalloc to not allocate localst.datasize twice
  1254. * order of stackalloc code fixed for implicit init/final
  1255. Revision 1.137 2003/08/20 15:50:35 peter
  1256. * define NOOPT until optimizer is fixed
  1257. Revision 1.136 2003/08/20 09:07:00 daniel
  1258. * New register coding now mandatory, some more convert_registers calls
  1259. removed.
  1260. Revision 1.135 2003/08/20 07:48:03 daniel
  1261. * Made internal assembler use new register coding
  1262. Revision 1.134 2003/08/17 16:59:20 jonas
  1263. * fixed regvars so they work with newra (at least for ppc)
  1264. * fixed some volatile register bugs
  1265. + -dnotranslation option for -dnewra, which causes the registers not to
  1266. be translated from virtual to normal registers. Requires support in
  1267. the assembler writer as well, which is only implemented in aggas/
  1268. agppcgas currently
  1269. Revision 1.133 2003/07/23 11:04:15 jonas
  1270. * split en_exit_code into a part that may allocate a register and a part
  1271. that doesn't, so the former can be done before the register colouring
  1272. has been performed
  1273. Revision 1.132 2003/07/06 17:58:22 peter
  1274. * framepointer fixes for sparc
  1275. * parent framepointer code more generic
  1276. Revision 1.131 2003/07/06 15:31:21 daniel
  1277. * Fixed register allocator. *Lots* of fixes.
  1278. Revision 1.130 2003/07/05 20:15:24 jonas
  1279. * set pi_do_call if range/overflow checking is on
  1280. Revision 1.129 2003/06/17 16:34:44 jonas
  1281. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1282. * renamed all_intregisters to paramanager.get_volatile_registers_int(pocall_default) and made it
  1283. processor dependent
  1284. Revision 1.128 2003/06/14 14:53:50 jonas
  1285. * fixed newra cycle for x86
  1286. * added constants for indicating source and destination operands of the
  1287. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1288. Revision 1.127 2003/06/13 21:19:31 peter
  1289. * current_procdef removed, use current_procinfo.procdef instead
  1290. Revision 1.126 2003/06/12 16:43:07 peter
  1291. * newra compiles for sparc
  1292. Revision 1.125 2003/06/09 12:23:30 peter
  1293. * init/final of procedure data splitted from genentrycode
  1294. * use asmnode getposition to insert final at the correct position
  1295. als for the implicit try...finally
  1296. Revision 1.124 2003/06/07 19:37:43 jonas
  1297. * pi_do_call must always be set for the main program, since it always
  1298. ends with a call to FPC_DO_EXIT
  1299. Revision 1.123 2003/06/07 18:57:04 jonas
  1300. + added freeintparaloc
  1301. * ppc get/freeintparaloc now check whether the parameter regs are
  1302. properly allocated/deallocated (and get an extra list para)
  1303. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1304. * fixed lot of missing pi_do_call's
  1305. Revision 1.122 2003/06/03 13:01:59 daniel
  1306. * Register allocator finished
  1307. Revision 1.121 2003/05/31 20:23:39 jonas
  1308. * added pi_do_call if a procedure has a value shortstring parameter
  1309. (it's copied to the local stackframe with a helper)
  1310. Revision 1.120 2003/05/30 23:57:08 peter
  1311. * more sparc cleanup
  1312. * accumulator removed, splitted in function_return_reg (called) and
  1313. function_result_reg (caller)
  1314. Revision 1.119 2003/05/28 23:58:18 jonas
  1315. * added missing initialization of rg.usedintin,byproc
  1316. * ppc now also saves/restores used fpu registers
  1317. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  1318. i386
  1319. Revision 1.118 2003/05/26 21:17:18 peter
  1320. * procinlinenode removed
  1321. * aktexit2label removed, fast exit removed
  1322. + tcallnode.inlined_pass_2 added
  1323. Revision 1.117 2003/05/25 08:59:47 peter
  1324. * do not generate code when there was an error
  1325. Revision 1.116 2003/05/23 18:49:55 jonas
  1326. * generate code for parent procedure before that of nested procedures as
  1327. well (I only need pass_1 to be done for the ppc, but pass_1 and pass_2
  1328. are grouped and it doesn't hurt that pass_2 is done as well)
  1329. Revision 1.115 2003/05/22 21:31:35 peter
  1330. * defer codegeneration for nested procedures
  1331. Revision 1.114 2003/05/16 20:00:39 jonas
  1332. * powerpc nested procedure fixes, should work completely now if all
  1333. local variables of the parent procedure are declared before the
  1334. nested procedures are declared
  1335. Revision 1.113 2003/05/16 14:33:31 peter
  1336. * regvar fixes
  1337. Revision 1.112 2003/05/13 21:26:38 peter
  1338. * only call destructor in except block when there is a destructor
  1339. available
  1340. Revision 1.111 2003/05/13 19:14:41 peter
  1341. * failn removed
  1342. * inherited result code check moven to pexpr
  1343. Revision 1.110 2003/05/13 15:18:49 peter
  1344. * fixed various crashes
  1345. Revision 1.109 2003/05/11 21:37:03 peter
  1346. * moved implicit exception frame from ncgutil to psub
  1347. * constructor/destructor helpers moved from cobj/ncgutil to psub
  1348. Revision 1.108 2003/05/09 17:47:03 peter
  1349. * self moved to hidden parameter
  1350. * removed hdisposen,hnewn,selfn
  1351. Revision 1.107 2003/04/27 11:21:34 peter
  1352. * aktprocdef renamed to current_procinfo.procdef
  1353. * procinfo renamed to current_procinfo
  1354. * procinfo will now be stored in current_module so it can be
  1355. cleaned up properly
  1356. * gen_main_procsym changed to create_main_proc and release_main_proc
  1357. to also generate a tprocinfo structure
  1358. * fixed unit implicit initfinal
  1359. Revision 1.106 2003/04/27 07:29:50 peter
  1360. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1361. a new procdef declaration
  1362. * aktprocsym removed
  1363. * lexlevel removed, use symtable.symtablelevel instead
  1364. * implicit init/final code uses the normal genentry/genexit
  1365. * funcret state checking updated for new funcret handling
  1366. Revision 1.105 2003/04/26 00:31:42 peter
  1367. * set return_offset moved to after_header
  1368. Revision 1.104 2003/04/25 20:59:34 peter
  1369. * removed funcretn,funcretsym, function result is now in varsym
  1370. and aliases for result and function name are added using absolutesym
  1371. * vs_hidden parameter for funcret passed in parameter
  1372. * vs_hidden fixes
  1373. * writenode changed to printnode and released from extdebug
  1374. * -vp option added to generate a tree.log with the nodetree
  1375. * nicer printnode for statements, callnode
  1376. Revision 1.103 2003/04/24 13:03:01 florian
  1377. * comp is now written with its bit pattern to the ppu instead as an extended
  1378. Revision 1.102 2003/04/23 12:35:34 florian
  1379. * fixed several issues with powerpc
  1380. + applied a patch from Jonas for nested function calls (PowerPC only)
  1381. * ...
  1382. Revision 1.101 2003/04/22 14:33:38 peter
  1383. * removed some notes/hints
  1384. Revision 1.100 2003/04/22 13:47:08 peter
  1385. * fixed C style array of const
  1386. * fixed C array passing
  1387. * fixed left to right with high parameters
  1388. Revision 1.99 2003/04/22 10:09:35 daniel
  1389. + Implemented the actual register allocator
  1390. + Scratch registers unavailable when new register allocator used
  1391. + maybe_save/maybe_restore unavailable when new register allocator used
  1392. Revision 1.98 2003/04/17 07:50:24 daniel
  1393. * Some work on interference graph construction
  1394. Revision 1.97 2003/04/16 09:26:55 jonas
  1395. * assembler procedures now again get a stackframe if they have local
  1396. variables. No space is reserved for a function result however.
  1397. Also, the register parameters aren't automatically saved on the stack
  1398. anymore in assembler procedures.
  1399. Revision 1.96 2003/04/05 21:09:31 jonas
  1400. * several ppc/generic result offset related fixes. The "normal" result
  1401. offset seems now to be calculated correctly and a lot of duplicate
  1402. calculations have been removed. Nested functions accessing the parent's
  1403. function result don't work at all though :(
  1404. Revision 1.95 2003/04/02 16:11:34 peter
  1405. * give error when exports is not supported
  1406. Revision 1.94 2003/03/12 22:43:38 jonas
  1407. * more powerpc and generic fixes related to the new register allocator
  1408. Revision 1.93 2003/03/08 08:59:07 daniel
  1409. + $define newra will enable new register allocator
  1410. + getregisterint will return imaginary registers with $newra
  1411. + -sr switch added, will skip register allocation so you can see
  1412. the direct output of the code generator before register allocation
  1413. Revision 1.92 2003/02/19 22:00:14 daniel
  1414. * Code generator converted to new register notation
  1415. - Horribily outdated todo.txt removed
  1416. Revision 1.91 2003/01/09 21:52:37 peter
  1417. * merged some verbosity options.
  1418. * V_LineInfo is a verbosity flag to include line info
  1419. Revision 1.90 2003/01/09 20:40:59 daniel
  1420. * Converted some code in cgx86.pas to new register numbering
  1421. Revision 1.89 2003/01/09 15:49:56 daniel
  1422. * Added register conversion
  1423. Revision 1.88 2003/01/08 18:43:56 daniel
  1424. * Tregister changed into a record
  1425. Revision 1.87 2003/01/03 20:35:08 peter
  1426. * check also interfacedef when checking for matching forwarddef
  1427. Revision 1.86 2003/01/02 11:14:02 michael
  1428. + Patch from peter to support initial values for local variables
  1429. Revision 1.85 2002/12/29 18:59:34 peter
  1430. * fixed parsing of declarations before asm statement
  1431. Revision 1.84 2002/12/29 18:25:18 peter
  1432. * parse declarations before check _ASM token
  1433. Revision 1.83 2002/12/29 14:57:50 peter
  1434. * unit loading changed to first register units and load them
  1435. afterwards. This is needed to support uses xxx in yyy correctly
  1436. * unit dependency check fixed
  1437. Revision 1.82 2002/12/25 01:26:56 peter
  1438. * duplicate procsym-unitsym fix
  1439. Revision 1.81 2002/12/15 13:37:15 peter
  1440. * don't include uf_init for library. The code is already called and
  1441. does not need to be in the initfinal table
  1442. Revision 1.80 2002/12/07 14:27:09 carl
  1443. * 3% memory optimization
  1444. * changed some types
  1445. + added type checking with different size for call node and for
  1446. parameters
  1447. Revision 1.79 2002/11/25 18:43:32 carl
  1448. - removed the invalid if <> checking (Delphi is strange on this)
  1449. + implemented abstract warning on instance creation of class with
  1450. abstract methods.
  1451. * some error message cleanups
  1452. Revision 1.78 2002/11/25 17:43:23 peter
  1453. * splitted defbase in defutil,symutil,defcmp
  1454. * merged isconvertable and is_equal into compare_defs(_ext)
  1455. * made operator search faster by walking the list only once
  1456. Revision 1.77 2002/11/23 22:50:06 carl
  1457. * some small speed optimizations
  1458. + added several new warnings/hints
  1459. Revision 1.76 2002/11/18 17:31:58 peter
  1460. * pass proccalloption to ret_in_xxx and push_xxx functions
  1461. Revision 1.75 2002/11/17 16:31:57 carl
  1462. * memory optimization (3-4%) : cleanup of tai fields,
  1463. cleanup of tdef and tsym fields.
  1464. * make it work for m68k
  1465. Revision 1.74 2002/11/15 01:58:53 peter
  1466. * merged changes from 1.0.7 up to 04-11
  1467. - -V option for generating bug report tracing
  1468. - more tracing for option parsing
  1469. - errors for cdecl and high()
  1470. - win32 import stabs
  1471. - win32 records<=8 are returned in eax:edx (turned off by default)
  1472. - heaptrc update
  1473. - more info for temp management in .s file with EXTDEBUG
  1474. Revision 1.73 2002/11/09 15:32:30 carl
  1475. * noopt for non-i386 targets
  1476. Revision 1.72 2002/09/10 20:31:48 florian
  1477. * call to current_procinfo.after_header added
  1478. Revision 1.71 2002/09/07 15:25:07 peter
  1479. * old logs removed and tabs fixed
  1480. Revision 1.70 2002/09/03 16:26:27 daniel
  1481. * Make Tprocdef.defs protected
  1482. Revision 1.69 2002/08/25 19:25:20 peter
  1483. * sym.insert_in_data removed
  1484. * symtable.insertvardata/insertconstdata added
  1485. * removed insert_in_data call from symtable.insert, it needs to be
  1486. called separatly. This allows to deref the address calculation
  1487. * procedures now calculate the parast addresses after the procedure
  1488. directives are parsed. This fixes the cdecl parast problem
  1489. * push_addr_param has an extra argument that specifies if cdecl is used
  1490. or not
  1491. Revision 1.68 2002/08/17 09:23:41 florian
  1492. * first part of procinfo rewrite
  1493. Revision 1.67 2002/08/16 14:24:59 carl
  1494. * issameref() to test if two references are the same (then emit no opcodes)
  1495. + ret_in_reg to replace ret_in_acc
  1496. (fix some register allocation bugs at the same time)
  1497. + save_std_register now has an extra parameter which is the
  1498. usedinproc registers
  1499. Revision 1.66 2002/08/11 14:32:27 peter
  1500. * renamed current_library to objectlibrary
  1501. Revision 1.65 2002/08/11 13:24:13 peter
  1502. * saving of asmsymbols in ppu supported
  1503. * asmsymbollist global is removed and moved into a new class
  1504. tasmlibrarydata that will hold the info of a .a file which
  1505. corresponds with a single module. Added librarydata to tmodule
  1506. to keep the library info stored for the module. In the future the
  1507. objectfiles will also be stored to the tasmlibrarydata class
  1508. * all getlabel/newasmsymbol and friends are moved to the new class
  1509. Revision 1.64 2002/08/09 19:14:28 carl
  1510. * fixed stackframe parameter (should only contain local size),
  1511. set to zero currently
  1512. Revision 1.63 2002/08/06 20:55:22 florian
  1513. * first part of ppc calling conventions fix
  1514. Revision 1.62 2002/07/26 21:15:41 florian
  1515. * rewrote the system handling
  1516. Revision 1.61 2002/07/20 11:57:56 florian
  1517. * types.pas renamed to defbase.pas because D6 contains a types
  1518. unit so this would conflicts if D6 programms are compiled
  1519. + Willamette/SSE2 instructions to assembler added
  1520. Revision 1.60 2002/07/19 11:41:36 daniel
  1521. * State tracker work
  1522. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1523. allows the state tracker to change while nodes automatically into
  1524. repeat nodes.
  1525. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1526. 'not(a>b)' is optimized into 'a<=b'.
  1527. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1528. by removing the notn and later switchting the true and falselabels. The
  1529. same is done with 'repeat until not a'.
  1530. Revision 1.59 2002/07/15 18:03:15 florian
  1531. * readded removed changes
  1532. Revision 1.57 2002/07/11 14:41:28 florian
  1533. * start of the new generic parameter handling
  1534. Revision 1.58 2002/07/14 18:00:44 daniel
  1535. + Added the beginning of a state tracker. This will track the values of
  1536. variables through procedures and optimize things away.
  1537. Revision 1.56 2002/07/07 09:52:32 florian
  1538. * powerpc target fixed, very simple units can be compiled
  1539. * some basic stuff for better callparanode handling, far from being finished
  1540. Revision 1.55 2002/07/04 20:43:01 florian
  1541. * first x86-64 patches
  1542. Revision 1.54 2002/07/01 18:46:25 peter
  1543. * internal linker
  1544. * reorganized aasm layer
  1545. Revision 1.53 2002/05/18 13:34:14 peter
  1546. * readded missing revisions
  1547. Revision 1.52 2002/05/16 19:46:44 carl
  1548. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1549. + try to fix temp allocation (still in ifdef)
  1550. + generic constructor calls
  1551. + start of tassembler / tmodulebase class cleanup
  1552. Revision 1.51 2002/05/14 19:34:49 peter
  1553. * removed old logs and updated copyright year
  1554. }