psub.pas 65 KB

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