psub.pas 62 KB

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