psub.pas 64 KB

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