psub.pas 65 KB

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