psub.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  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 clearrefs(p : tnamedindexitem;arg:pointer);
  519. begin
  520. if (tsym(p).typ=varsym) then
  521. if tvarsym(p).refs>1 then
  522. tvarsym(p).refs:=1;
  523. end;
  524. procedure tcgprocinfo.generate_code;
  525. var
  526. oldprocinfo : tprocinfo;
  527. oldaktmaxfpuregisters : longint;
  528. oldfilepos : tfileposinfo;
  529. templist : Taasmoutput;
  530. headertai : tai;
  531. usesacc,
  532. usesfpu,
  533. usesacchi : boolean;
  534. begin
  535. { the initialization procedure can be empty, then we
  536. don't need to generate anything. When it was an empty
  537. procedure there would be at least a blocknode }
  538. if not assigned(code) then
  539. exit;
  540. { We need valid code }
  541. if Errorcount<>0 then
  542. exit;
  543. { The RA and Tempgen shall not be available yet }
  544. if assigned(tg) then
  545. internalerror(200309201);
  546. oldprocinfo:=current_procinfo;
  547. oldfilepos:=aktfilepos;
  548. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  549. current_procinfo:=self;
  550. { get new labels }
  551. aktbreaklabel:=nil;
  552. aktcontinuelabel:=nil;
  553. templist:=Taasmoutput.create;
  554. { add parast/localst to symtablestack }
  555. add_to_symtablestack;
  556. { when size optimization only count occurrence }
  557. if cs_littlesize in aktglobalswitches then
  558. cg.t_times:=1
  559. else
  560. { reference for repetition is 100 }
  561. cg.t_times:=100;
  562. { clear register count }
  563. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  564. symtablestack.next.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  565. { firstpass everything }
  566. flowcontrol:=[];
  567. do_firstpass(code);
  568. { only do secondpass if there are no errors }
  569. if ErrorCount=0 then
  570. begin
  571. { set the start offset to the start of the temp area in the stack }
  572. tg:=ttgobj.create;
  573. { Create register allocator }
  574. cg.init_register_allocators;
  575. current_procinfo.set_first_temp_offset;
  576. current_procinfo.generate_parameter_info;
  577. { Allocate space in temp/registers for parast and localst }
  578. aktfilepos:=entrypos;
  579. gen_alloc_parast(aktproccode,tparasymtable(current_procinfo.procdef.parast));
  580. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  581. gen_alloc_localst(aktproccode,tlocalsymtable(current_procinfo.procdef.localst));
  582. if (cs_asm_source in aktglobalswitches) then
  583. aktproccode.concat(Tai_comment.Create(strpnew('Temps start at '+std_regname(current_procinfo.framepointer)+
  584. tostr_with_plus(tg.lasttemp))));
  585. { Generate code to load register parameters in temps and insert local
  586. copies for values parameters. This must be done before the code for the
  587. body is generated because the localloc is updated.
  588. Note: The generated code will be inserted after the code generation of
  589. the body is finished, because only then the position is known }
  590. aktfilepos:=entrypos;
  591. gen_load_para_value(templist);
  592. { caller paraloc info is also necessary in the stackframe_entry
  593. code of the ppc (and possibly other processors) }
  594. if not procdef.has_paraloc_info then
  595. begin
  596. paramanager.create_paraloc_info(procdef,callerside);
  597. procdef.has_paraloc_info:=true;
  598. end;
  599. { generate code for the node tree }
  600. do_secondpass(code);
  601. current_procinfo.aktproccode.concatlist(exprasmlist);
  602. {$ifdef i386}
  603. procdef.fpu_used:=code.registersfpu;
  604. {$endif i386}
  605. { The position of the loadpara_asmnode is now known }
  606. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  607. { first generate entry and initialize code with the correct
  608. position and switches }
  609. aktfilepos:=entrypos;
  610. aktlocalswitches:=entryswitches;
  611. gen_entry_code(templist);
  612. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  613. gen_initialize_code(templist,false);
  614. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  615. { now generate finalize and exit code with the correct position
  616. and switches }
  617. aktfilepos:=exitpos;
  618. aktlocalswitches:=exitswitches;
  619. gen_finalize_code(templist,false);
  620. { the finalcode must be concated if there was no position available,
  621. using insertlistafter will result in an insert at the start
  622. when currentai=nil }
  623. if assigned(final_asmnode.currenttai) then
  624. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  625. else
  626. aktproccode.concatlist(templist);
  627. { insert exit label at the correct position }
  628. cg.a_label(templist,current_procinfo.aktexitlabel);
  629. if assigned(exitlabel_asmnode.currenttai) then
  630. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  631. else
  632. aktproccode.concatlist(templist);
  633. { exit code }
  634. gen_exit_code(templist);
  635. aktproccode.concatlist(templist);
  636. {$ifdef OLDREGVARS}
  637. { note: this must be done only after as much code as possible has }
  638. { been generated. The result is that when you ungetregister() a }
  639. { regvar, it will actually free the regvar (and alse free the }
  640. { the regvars at the same time). Doing this too early will }
  641. { confuse the register allocator, as the regvars will still be }
  642. { used. It should be done before loading the result regs (so }
  643. { they don't conflict with the regvars) and before }
  644. { gen_entry_code (that one has to be able to allocate the }
  645. { regvars again) (JM) }
  646. free_regvars(aktproccode);
  647. {$endif OLDREGVARS}
  648. { add code that will load the return value, this is not done
  649. for assembler routines when they didn't reference the result
  650. variable }
  651. usesacc:=false;
  652. usesfpu:=false;
  653. usesacchi:=false;
  654. gen_load_return_value(templist,usesacc,usesacchi,usesfpu);
  655. aktproccode.concatlist(templist);
  656. { generate symbol and save end of header position }
  657. aktfilepos:=entrypos;
  658. gen_proc_symbol(templist);
  659. headertai:=tai(templist.last);
  660. { insert symbol }
  661. aktproccode.insertlist(templist);
  662. { Free space in temp/registers for parast and localst, must be
  663. done after gen_entry_code }
  664. aktfilepos:=exitpos;
  665. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  666. gen_free_localst(aktproccode,tlocalsymtable(current_procinfo.procdef.localst));
  667. gen_free_parast(aktproccode,tparasymtable(current_procinfo.procdef.parast));
  668. { The procedure body is finished, we can now
  669. allocate the registers }
  670. if not(cs_no_regalloc in aktglobalswitches) then
  671. begin
  672. cg.do_register_allocation(aktproccode,headertai);
  673. (*
  674. {$ifndef NoOpt}
  675. if (cs_optimize in aktglobalswitches) and
  676. { do not optimize pure assembler procedures }
  677. not(pi_is_assembler in current_procinfo.flags) then
  678. optimize(aktproccode);
  679. {$endif NoOpt}
  680. *)
  681. end;
  682. { Add save and restore of used registers }
  683. aktfilepos:=entrypos;
  684. gen_save_used_regs(templist);
  685. aktproccode.insertlistafter(headertai,templist);
  686. aktfilepos:=exitpos;
  687. gen_restore_used_regs(aktproccode,usesacc,usesacchi,usesfpu);
  688. { Add stack allocation code after header }
  689. aktfilepos:=entrypos;
  690. gen_stackalloc_code(templist);
  691. aktproccode.insertlistafter(headertai,templist);
  692. { Add exit code at the end }
  693. aktfilepos:=exitpos;
  694. gen_stackfree_code(templist,usesacc,usesacchi);
  695. aktproccode.concatlist(templist);
  696. { Add end symbol and debug info }
  697. aktfilepos:=exitpos;
  698. gen_proc_symbol_end(templist);
  699. aktproccode.concatlist(templist);
  700. { save local data (casetable) also in the same file }
  701. if assigned(aktlocaldata) and
  702. (not aktlocaldata.empty) then
  703. begin
  704. { because of the limited constant size of the arm, all data access is done pc relative }
  705. if target_info.cpu=cpu_arm then
  706. aktproccode.concatlist(aktlocaldata)
  707. else
  708. begin
  709. aktproccode.concat(Tai_section.Create(sec_data));
  710. aktproccode.concatlist(aktlocaldata);
  711. aktproccode.concat(Tai_section.Create(sec_code));
  712. end;
  713. end;
  714. { add the procedure to the codesegment }
  715. if (cs_create_smart in aktmoduleswitches) then
  716. codesegment.concat(Tai_cut.Create);
  717. codesegment.concatlist(aktproccode);
  718. { only now we can remove the temps }
  719. tg.resettempgen;
  720. { stop tempgen and ra }
  721. tg.free;
  722. cg.done_register_allocators;
  723. tg:=nil;
  724. end;
  725. { restore symtablestack }
  726. remove_from_symtablestack;
  727. { restore }
  728. templist.free;
  729. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  730. aktfilepos:=oldfilepos;
  731. current_procinfo:=oldprocinfo;
  732. end;
  733. procedure tcgprocinfo.add_to_symtablestack;
  734. var
  735. _class,hp : tobjectdef;
  736. begin
  737. { insert symtables for the class, but only if it is no nested function }
  738. if assigned(procdef._class) and
  739. not(assigned(parent) and
  740. assigned(parent.procdef) and
  741. assigned(parent.procdef._class)) then
  742. begin
  743. { insert them in the reverse order }
  744. hp:=nil;
  745. repeat
  746. _class:=procdef._class;
  747. while _class.childof<>hp do
  748. _class:=_class.childof;
  749. hp:=_class;
  750. _class.symtable.next:=symtablestack;
  751. symtablestack:=_class.symtable;
  752. until hp=procdef._class;
  753. end;
  754. { insert parasymtable in symtablestack when parsing
  755. a function }
  756. if procdef.parast.symtablelevel>=normal_function_level then
  757. begin
  758. procdef.parast.next:=symtablestack;
  759. symtablestack:=procdef.parast;
  760. end;
  761. procdef.localst.next:=symtablestack;
  762. symtablestack:=procdef.localst;
  763. end;
  764. procedure tcgprocinfo.remove_from_symtablestack;
  765. begin
  766. { remove localst/parast }
  767. if procdef.parast.symtablelevel>=normal_function_level then
  768. symtablestack:=symtablestack.next.next
  769. else
  770. symtablestack:=symtablestack.next;
  771. { remove class member symbol tables }
  772. while symtablestack.symtabletype=objectsymtable do
  773. symtablestack:=symtablestack.next;
  774. end;
  775. procedure tcgprocinfo.resetprocdef;
  776. begin
  777. { the local symtables can be deleted, but the parast }
  778. { doesn't, (checking definitons when calling a }
  779. { function }
  780. { not for a inline procedure !! (PM) }
  781. { at lexlevel = 1 localst is the staticsymtable itself }
  782. { so no dispose here !! }
  783. if assigned(code) and
  784. not(cs_browser in aktmoduleswitches) and
  785. (procdef.proccalloption<>pocall_inline) then
  786. begin
  787. if procdef.parast.symtablelevel>=normal_function_level then
  788. procdef.localst.free;
  789. procdef.localst:=nil;
  790. end;
  791. { remove code tree, if not inline procedure }
  792. if assigned(code) then
  793. begin
  794. { the inline procedure has already got a copy of the tree
  795. stored in current_procinfo.procdef.code }
  796. code.free;
  797. code:=nil;
  798. if (procdef.proccalloption<>pocall_inline) then
  799. procdef.code:=nil;
  800. end;
  801. end;
  802. procedure tcgprocinfo.parse_body;
  803. var
  804. oldprocinfo : tprocinfo;
  805. begin
  806. oldprocinfo:=current_procinfo;
  807. current_procinfo:=self;
  808. { calculate the lexical level }
  809. if procdef.parast.symtablelevel>maxnesting then
  810. Message(parser_e_too_much_lexlevel);
  811. { static is also important for local procedures !! }
  812. if (po_staticmethod in procdef.procoptions) then
  813. allow_only_static:=true
  814. else if (procdef.parast.symtablelevel=normal_function_level) then
  815. allow_only_static:=false;
  816. { reset break and continue labels }
  817. block_type:=bt_general;
  818. {$ifdef state_tracking}
  819. { aktstate:=Tstate_storage.create;}
  820. {$endif state_tracking}
  821. { create a local symbol table for this routine }
  822. if not assigned(procdef.localst) then
  823. procdef.insert_localst;
  824. { add parast/localst to symtablestack }
  825. add_to_symtablestack;
  826. { constant symbols are inserted in this symboltable }
  827. constsymtable:=symtablestack;
  828. { save entry info }
  829. entrypos:=aktfilepos;
  830. entryswitches:=aktlocalswitches;
  831. { parse the code ... }
  832. code:=block(current_module.islibrary);
  833. { save exit info }
  834. exitswitches:=aktlocalswitches;
  835. exitpos:=last_endtoken_filepos;
  836. if assigned(code) then
  837. begin
  838. { get a better entry point }
  839. entrypos:=code.fileinfo;
  840. { the procedure is now defined }
  841. procdef.forwarddef:=false;
  842. if (Errorcount=0) then
  843. begin
  844. { add implicit entry and exit code }
  845. add_entry_exit_code;
  846. { check if forwards are resolved }
  847. tstoredsymtable(procdef.localst).check_forwards;
  848. { check if all labels are used }
  849. tstoredsymtable(procdef.localst).checklabels;
  850. { remove cross unit overloads }
  851. tstoredsymtable(procdef.localst).unchain_overloaded;
  852. end;
  853. { check for unused symbols, but only if there is no asm block }
  854. if not(pi_uses_asm in flags) then
  855. begin
  856. { not for unit init, becuase the var can be used in finalize,
  857. it will be done in proc_unit }
  858. if not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  859. tstoredsymtable(procdef.localst).allsymbolsused;
  860. tstoredsymtable(procdef.parast).allsymbolsused;
  861. end;
  862. { Finish type checking pass }
  863. do_resulttypepass(code);
  864. { Print the node to tree.log }
  865. if paraprintnodetree=1 then
  866. printnode_procdef(procdef);
  867. end;
  868. { store a copy of the original tree for inline, for
  869. normal procedures only store a reference to the
  870. current tree }
  871. if (procdef.proccalloption=pocall_inline) then
  872. procdef.code:=code.getcopy
  873. else
  874. procdef.code:=code;
  875. { ... remove symbol tables }
  876. remove_from_symtablestack;
  877. {$ifdef state_tracking}
  878. { aktstate.destroy;}
  879. {$endif state_tracking}
  880. { reset to normal non static function }
  881. if (current_procinfo.procdef.parast.symtablelevel=normal_function_level) then
  882. allow_only_static:=false;
  883. current_procinfo:=oldprocinfo;
  884. end;
  885. {****************************************************************************
  886. PROCEDURE/FUNCTION PARSING
  887. ****************************************************************************}
  888. procedure check_init_paras(p:tnamedindexitem;arg:pointer);
  889. begin
  890. if tsym(p).typ<>varsym then
  891. exit;
  892. with tvarsym(p) do
  893. if (not is_class(vartype.def) and
  894. vartype.def.needs_inittable and
  895. (varspez in [vs_value,vs_out])) then
  896. include(current_procinfo.flags,pi_do_call);
  897. end;
  898. procedure read_proc;
  899. {
  900. Parses the procedure directives, then parses the procedure body, then
  901. generates the code for it
  902. }
  903. procedure do_generate_code(pi:tcgprocinfo);
  904. var
  905. hpi : tcgprocinfo;
  906. begin
  907. { generate code for this procedure }
  908. pi.generate_code;
  909. { process nested procs }
  910. hpi:=tcgprocinfo(pi.nestedprocs.first);
  911. while assigned(hpi) do
  912. begin
  913. do_generate_code(hpi);
  914. hpi:=tcgprocinfo(hpi.next);
  915. end;
  916. pi.resetprocdef;
  917. end;
  918. var
  919. old_current_procinfo : tprocinfo;
  920. oldconstsymtable : tsymtable;
  921. oldfailtokenmode : tmodeswitch;
  922. pdflags : tpdflags;
  923. pd : tprocdef;
  924. isnestedproc : boolean;
  925. begin
  926. { save old state }
  927. oldconstsymtable:=constsymtable;
  928. old_current_procinfo:=current_procinfo;
  929. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  930. to an other procdef }
  931. current_procinfo:=nil;
  932. { parse procedure declaration }
  933. if assigned(old_current_procinfo) and
  934. assigned(old_current_procinfo.procdef) then
  935. pd:=parse_proc_dec(old_current_procinfo.procdef._class)
  936. else
  937. pd:=parse_proc_dec(nil);
  938. { set the default function options }
  939. if parse_only then
  940. begin
  941. pd.forwarddef:=true;
  942. { set also the interface flag, for better error message when the
  943. implementation doesn't much this header }
  944. pd.interfacedef:=true;
  945. include(pd.procoptions,po_public);
  946. pdflags:=[pd_interface];
  947. end
  948. else
  949. begin
  950. pdflags:=[pd_body];
  951. if (not current_module.in_interface) then
  952. include(pdflags,pd_implemen);
  953. if (not current_module.is_unit) or
  954. (cs_create_smart in aktmoduleswitches) then
  955. include(pd.procoptions,po_public);
  956. pd.forwarddef:=false;
  957. end;
  958. { parse the directives that may follow }
  959. parse_proc_directives(pd,pdflags);
  960. { hint directives, these can be separated by semicolons here,
  961. that needs to be handled here with a loop (PFV) }
  962. while try_consume_hintdirective(pd.symoptions) do
  963. Consume(_SEMICOLON);
  964. { Set calling convention }
  965. handle_calling_convention(pd);
  966. { everything of the proc definition is known, we can now
  967. calculate the parameters }
  968. calc_parast(pd);
  969. { search for forward declarations }
  970. if not proc_add_definition(pd) then
  971. begin
  972. { A method must be forward defined (in the object declaration) }
  973. if assigned(pd._class) and
  974. (not assigned(old_current_procinfo.procdef._class)) then
  975. begin
  976. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  977. tprocsym(pd.procsym).write_parameter_lists(pd);
  978. end
  979. else
  980. begin
  981. { Give a better error if there is a forward def in the interface and only
  982. a single implementation }
  983. if (not pd.forwarddef) and
  984. (not pd.interfacedef) and
  985. (tprocsym(pd.procsym).procdef_count>1) and
  986. tprocsym(pd.procsym).first_procdef.forwarddef and
  987. tprocsym(pd.procsym).first_procdef.interfacedef and
  988. not(tprocsym(pd.procsym).procdef_count>2) then
  989. begin
  990. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  991. tprocsym(pd.procsym).write_parameter_lists(pd);
  992. end;
  993. end;
  994. end;
  995. { compile procedure when a body is needed }
  996. if (pd_body in pdflags) then
  997. begin
  998. Message1(parser_d_procedure_start,pd.fullprocname(false));
  999. { create a new procedure }
  1000. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1001. current_module.procinfo:=current_procinfo;
  1002. current_procinfo.procdef:=pd;
  1003. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1004. { Insert mangledname }
  1005. pd.aliasnames.insert(pd.mangledname);
  1006. { Insert result variables in the localst }
  1007. insert_funcret_local(pd);
  1008. { check if there are para's which require initing -> set }
  1009. { pi_do_call (if not yet set) }
  1010. if not(pi_do_call in current_procinfo.flags) then
  1011. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_init_paras,nil);
  1012. { set _FAIL as keyword if constructor }
  1013. if (pd.proctypeoption=potype_constructor) then
  1014. begin
  1015. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1016. tokeninfo^[_FAIL].keyword:=m_all;
  1017. end;
  1018. tcgprocinfo(current_procinfo).parse_body;
  1019. { When it's a nested procedure then defer the code generation,
  1020. when back at normal function level then generate the code
  1021. for all defered nested procedures and the current procedure }
  1022. if isnestedproc then
  1023. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1024. else
  1025. begin
  1026. { We can't support inlining for procedures that have nested
  1027. procedures because the nested procedures use a fixed offset
  1028. for accessing locals in the parent procedure (PFV) }
  1029. if (current_procinfo.procdef.proccalloption=pocall_inline) and
  1030. (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1031. begin
  1032. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1033. Message(parser_w_inlining_disabled);
  1034. current_procinfo.procdef.proccalloption:=pocall_default;
  1035. end;
  1036. do_generate_code(tcgprocinfo(current_procinfo));
  1037. end;
  1038. { reset _FAIL as _SELF normal }
  1039. if (pd.proctypeoption=potype_constructor) then
  1040. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1041. { release procinfo }
  1042. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1043. internalerror(200304274);
  1044. current_module.procinfo:=current_procinfo.parent;
  1045. if not isnestedproc then
  1046. current_procinfo.free;
  1047. consume(_SEMICOLON);
  1048. end;
  1049. { Restore old state }
  1050. constsymtable:=oldconstsymtable;
  1051. current_procinfo:=old_current_procinfo;
  1052. end;
  1053. {****************************************************************************
  1054. DECLARATION PARSING
  1055. ****************************************************************************}
  1056. { search in symtablestack for not complete classes }
  1057. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  1058. begin
  1059. if (tsym(p).typ=typesym) and
  1060. (ttypesym(p).restype.def.deftype=objectdef) and
  1061. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  1062. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1063. end;
  1064. procedure read_declarations(islibrary : boolean);
  1065. begin
  1066. repeat
  1067. if not assigned(current_procinfo) then
  1068. internalerror(200304251);
  1069. case token of
  1070. _LABEL:
  1071. begin
  1072. label_dec;
  1073. end;
  1074. _CONST:
  1075. begin
  1076. const_dec;
  1077. end;
  1078. _TYPE:
  1079. begin
  1080. type_dec;
  1081. end;
  1082. _VAR:
  1083. var_dec;
  1084. _THREADVAR:
  1085. threadvar_dec;
  1086. _CONSTRUCTOR,
  1087. _DESTRUCTOR,
  1088. _FUNCTION,
  1089. _PROCEDURE,
  1090. _OPERATOR,
  1091. _CLASS:
  1092. read_proc;
  1093. _RESOURCESTRING:
  1094. resourcestring_dec;
  1095. _EXPORTS:
  1096. begin
  1097. if not(assigned(current_procinfo.procdef.localst)) or
  1098. (current_procinfo.procdef.localst.symtablelevel>main_program_level) or
  1099. (current_module.is_unit) then
  1100. begin
  1101. Message(parser_e_syntax_error);
  1102. consume_all_until(_SEMICOLON);
  1103. end
  1104. else if islibrary or
  1105. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware]) then
  1106. read_exports
  1107. else
  1108. begin
  1109. Message(parser_w_unsupported_feature);
  1110. consume(_BEGIN);
  1111. end;
  1112. end
  1113. else
  1114. break;
  1115. end;
  1116. until false;
  1117. { check for incomplete class definitions, this is only required
  1118. for fpc modes }
  1119. if (m_fpc in aktmodeswitches) then
  1120. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1121. end;
  1122. procedure read_interface_declarations;
  1123. begin
  1124. repeat
  1125. case token of
  1126. _CONST :
  1127. const_dec;
  1128. _TYPE :
  1129. type_dec;
  1130. _VAR :
  1131. var_dec;
  1132. _THREADVAR :
  1133. threadvar_dec;
  1134. _RESOURCESTRING:
  1135. resourcestring_dec;
  1136. _FUNCTION,
  1137. _PROCEDURE,
  1138. _OPERATOR :
  1139. read_proc;
  1140. else
  1141. break;
  1142. end;
  1143. until false;
  1144. { check for incomplete class definitions, this is only required
  1145. for fpc modes }
  1146. if (m_fpc in aktmodeswitches) then
  1147. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1148. end;
  1149. end.
  1150. {
  1151. $Log$
  1152. Revision 1.168 2003-10-30 16:22:40 peter
  1153. * call firstpass before allocation and codegeneration is started
  1154. * move leftover code from pass_2.generatecode() to psub
  1155. Revision 1.167 2003/10/24 17:40:23 peter
  1156. * cleanup of the entry and exit code insertion
  1157. Revision 1.166 2003/10/21 15:14:33 peter
  1158. * fixed memleak for initfinalcode
  1159. * exit from generatecode when there are already errors
  1160. Revision 1.165 2003/10/20 19:28:51 peter
  1161. * disable inlining when nested procedures are found
  1162. Revision 1.164 2003/10/19 01:34:30 florian
  1163. * some ppc stuff fixed
  1164. * memory leak fixed
  1165. Revision 1.163 2003/10/17 14:38:32 peter
  1166. * 64k registers supported
  1167. * fixed some memory leaks
  1168. Revision 1.162 2003/10/10 17:48:13 peter
  1169. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1170. * tregisteralloctor renamed to trgobj
  1171. * removed rgobj from a lot of units
  1172. * moved location_* and reference_* to cgobj
  1173. * first things for mmx register allocation
  1174. Revision 1.161 2003/10/09 21:31:37 daniel
  1175. * Register allocator splitted, ans abstract now
  1176. Revision 1.160 2003/10/09 15:20:56 peter
  1177. * self is not a token anymore. It is handled special when found
  1178. in a code block and when parsing an method
  1179. Revision 1.159 2003/10/07 15:17:07 peter
  1180. * inline supported again, LOC_REFERENCEs are used to pass the
  1181. parameters
  1182. * inlineparasymtable,inlinelocalsymtable removed
  1183. * exitlabel inserting fixed
  1184. Revision 1.158 2003/10/06 22:23:41 florian
  1185. + added basic olevariant support
  1186. Revision 1.157 2003/10/03 14:45:09 peter
  1187. * more proc directive for procvar fixes
  1188. Revision 1.156 2003/10/02 21:20:32 peter
  1189. * handle_calling_convention removed from parse_proc_directive to
  1190. separate call
  1191. Revision 1.155 2003/10/01 20:34:49 peter
  1192. * procinfo unit contains tprocinfo
  1193. * cginfo renamed to cgbase
  1194. * moved cgmessage to verbose
  1195. * fixed ppc and sparc compiles
  1196. Revision 1.154 2003/09/29 20:58:56 peter
  1197. * optimized releasing of registers
  1198. Revision 1.153 2003/09/28 17:55:04 peter
  1199. * parent framepointer changed to hidden parameter
  1200. * tloadparentfpnode added
  1201. Revision 1.152 2003/09/27 13:29:43 peter
  1202. * fix reported file position for not matched forwards
  1203. Revision 1.151 2003/09/25 21:25:13 peter
  1204. * remove allocate_intterupt_parameter, allocation is platform
  1205. dependent and needs to be done in create_paraloc_info
  1206. Revision 1.150 2003/09/25 16:19:32 peter
  1207. * fix filepositions
  1208. * insert spill temp allocations at the start of the proc
  1209. Revision 1.149 2003/09/23 17:56:06 peter
  1210. * locals and paras are allocated in the code generation
  1211. * tvarsym.localloc contains the location of para/local when
  1212. generating code for the current procedure
  1213. Revision 1.148 2003/09/14 19:18:10 peter
  1214. * remove obsolete code already in comments
  1215. Revision 1.147 2003/09/14 12:58:00 peter
  1216. * support mulitple overloads in implementation, this is delphi
  1217. compatible
  1218. * procsym only stores the overloads available in the interface
  1219. Revision 1.146 2003/09/12 19:07:42 daniel
  1220. * Fixed fast spilling functionality by re-adding the code that initializes
  1221. precoloured nodes to degree 255. I would like to play hangman on the one
  1222. who removed that code.
  1223. Revision 1.145 2003/09/10 19:14:31 daniel
  1224. * Failed attempt to restore broken fastspill functionality
  1225. Revision 1.144 2003/09/09 20:59:27 daniel
  1226. * Adding register allocation order
  1227. Revision 1.143 2003/09/09 15:55:44 peter
  1228. * use register with least interferences in spillregister
  1229. Revision 1.142 2003/09/07 22:09:35 peter
  1230. * preparations for different default calling conventions
  1231. * various RA fixes
  1232. Revision 1.141 2003/09/04 14:46:12 peter
  1233. * abort with IE when spilling requires > 20 loops
  1234. Revision 1.140 2003/09/03 15:55:01 peter
  1235. * NEWRA branch merged
  1236. Revision 1.139 2003/09/03 11:18:37 florian
  1237. * fixed arm concatcopy
  1238. + arm support in the common compiler sources added
  1239. * moved some generic cg code around
  1240. + tfputype added
  1241. * ...
  1242. Revision 1.138.2.1 2003/08/31 13:50:16 daniel
  1243. * Remove sorting and use pregenerated indexes
  1244. * Some work on making things compile
  1245. Revision 1.138 2003/08/20 17:48:49 peter
  1246. * fixed stackalloc to not allocate localst.datasize twice
  1247. * order of stackalloc code fixed for implicit init/final
  1248. Revision 1.137 2003/08/20 15:50:35 peter
  1249. * define NOOPT until optimizer is fixed
  1250. Revision 1.136 2003/08/20 09:07:00 daniel
  1251. * New register coding now mandatory, some more convert_registers calls
  1252. removed.
  1253. Revision 1.135 2003/08/20 07:48:03 daniel
  1254. * Made internal assembler use new register coding
  1255. Revision 1.134 2003/08/17 16:59:20 jonas
  1256. * fixed regvars so they work with newra (at least for ppc)
  1257. * fixed some volatile register bugs
  1258. + -dnotranslation option for -dnewra, which causes the registers not to
  1259. be translated from virtual to normal registers. Requires support in
  1260. the assembler writer as well, which is only implemented in aggas/
  1261. agppcgas currently
  1262. Revision 1.133 2003/07/23 11:04:15 jonas
  1263. * split en_exit_code into a part that may allocate a register and a part
  1264. that doesn't, so the former can be done before the register colouring
  1265. has been performed
  1266. Revision 1.132 2003/07/06 17:58:22 peter
  1267. * framepointer fixes for sparc
  1268. * parent framepointer code more generic
  1269. Revision 1.131 2003/07/06 15:31:21 daniel
  1270. * Fixed register allocator. *Lots* of fixes.
  1271. Revision 1.130 2003/07/05 20:15:24 jonas
  1272. * set pi_do_call if range/overflow checking is on
  1273. Revision 1.129 2003/06/17 16:34:44 jonas
  1274. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1275. * renamed all_intregisters to paramanager.get_volatile_registers_int(pocall_default) and made it
  1276. processor dependent
  1277. Revision 1.128 2003/06/14 14:53:50 jonas
  1278. * fixed newra cycle for x86
  1279. * added constants for indicating source and destination operands of the
  1280. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1281. Revision 1.127 2003/06/13 21:19:31 peter
  1282. * current_procdef removed, use current_procinfo.procdef instead
  1283. Revision 1.126 2003/06/12 16:43:07 peter
  1284. * newra compiles for sparc
  1285. Revision 1.125 2003/06/09 12:23:30 peter
  1286. * init/final of procedure data splitted from genentrycode
  1287. * use asmnode getposition to insert final at the correct position
  1288. als for the implicit try...finally
  1289. Revision 1.124 2003/06/07 19:37:43 jonas
  1290. * pi_do_call must always be set for the main program, since it always
  1291. ends with a call to FPC_DO_EXIT
  1292. Revision 1.123 2003/06/07 18:57:04 jonas
  1293. + added freeintparaloc
  1294. * ppc get/freeintparaloc now check whether the parameter regs are
  1295. properly allocated/deallocated (and get an extra list para)
  1296. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1297. * fixed lot of missing pi_do_call's
  1298. Revision 1.122 2003/06/03 13:01:59 daniel
  1299. * Register allocator finished
  1300. Revision 1.121 2003/05/31 20:23:39 jonas
  1301. * added pi_do_call if a procedure has a value shortstring parameter
  1302. (it's copied to the local stackframe with a helper)
  1303. Revision 1.120 2003/05/30 23:57:08 peter
  1304. * more sparc cleanup
  1305. * accumulator removed, splitted in function_return_reg (called) and
  1306. function_result_reg (caller)
  1307. Revision 1.119 2003/05/28 23:58:18 jonas
  1308. * added missing initialization of rg.usedintin,byproc
  1309. * ppc now also saves/restores used fpu registers
  1310. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  1311. i386
  1312. Revision 1.118 2003/05/26 21:17:18 peter
  1313. * procinlinenode removed
  1314. * aktexit2label removed, fast exit removed
  1315. + tcallnode.inlined_pass_2 added
  1316. Revision 1.117 2003/05/25 08:59:47 peter
  1317. * do not generate code when there was an error
  1318. Revision 1.116 2003/05/23 18:49:55 jonas
  1319. * generate code for parent procedure before that of nested procedures as
  1320. well (I only need pass_1 to be done for the ppc, but pass_1 and pass_2
  1321. are grouped and it doesn't hurt that pass_2 is done as well)
  1322. Revision 1.115 2003/05/22 21:31:35 peter
  1323. * defer codegeneration for nested procedures
  1324. Revision 1.114 2003/05/16 20:00:39 jonas
  1325. * powerpc nested procedure fixes, should work completely now if all
  1326. local variables of the parent procedure are declared before the
  1327. nested procedures are declared
  1328. Revision 1.113 2003/05/16 14:33:31 peter
  1329. * regvar fixes
  1330. Revision 1.112 2003/05/13 21:26:38 peter
  1331. * only call destructor in except block when there is a destructor
  1332. available
  1333. Revision 1.111 2003/05/13 19:14:41 peter
  1334. * failn removed
  1335. * inherited result code check moven to pexpr
  1336. Revision 1.110 2003/05/13 15:18:49 peter
  1337. * fixed various crashes
  1338. Revision 1.109 2003/05/11 21:37:03 peter
  1339. * moved implicit exception frame from ncgutil to psub
  1340. * constructor/destructor helpers moved from cobj/ncgutil to psub
  1341. Revision 1.108 2003/05/09 17:47:03 peter
  1342. * self moved to hidden parameter
  1343. * removed hdisposen,hnewn,selfn
  1344. Revision 1.107 2003/04/27 11:21:34 peter
  1345. * aktprocdef renamed to current_procinfo.procdef
  1346. * procinfo renamed to current_procinfo
  1347. * procinfo will now be stored in current_module so it can be
  1348. cleaned up properly
  1349. * gen_main_procsym changed to create_main_proc and release_main_proc
  1350. to also generate a tprocinfo structure
  1351. * fixed unit implicit initfinal
  1352. Revision 1.106 2003/04/27 07:29:50 peter
  1353. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1354. a new procdef declaration
  1355. * aktprocsym removed
  1356. * lexlevel removed, use symtable.symtablelevel instead
  1357. * implicit init/final code uses the normal genentry/genexit
  1358. * funcret state checking updated for new funcret handling
  1359. Revision 1.105 2003/04/26 00:31:42 peter
  1360. * set return_offset moved to after_header
  1361. Revision 1.104 2003/04/25 20:59:34 peter
  1362. * removed funcretn,funcretsym, function result is now in varsym
  1363. and aliases for result and function name are added using absolutesym
  1364. * vs_hidden parameter for funcret passed in parameter
  1365. * vs_hidden fixes
  1366. * writenode changed to printnode and released from extdebug
  1367. * -vp option added to generate a tree.log with the nodetree
  1368. * nicer printnode for statements, callnode
  1369. Revision 1.103 2003/04/24 13:03:01 florian
  1370. * comp is now written with its bit pattern to the ppu instead as an extended
  1371. Revision 1.102 2003/04/23 12:35:34 florian
  1372. * fixed several issues with powerpc
  1373. + applied a patch from Jonas for nested function calls (PowerPC only)
  1374. * ...
  1375. Revision 1.101 2003/04/22 14:33:38 peter
  1376. * removed some notes/hints
  1377. Revision 1.100 2003/04/22 13:47:08 peter
  1378. * fixed C style array of const
  1379. * fixed C array passing
  1380. * fixed left to right with high parameters
  1381. Revision 1.99 2003/04/22 10:09:35 daniel
  1382. + Implemented the actual register allocator
  1383. + Scratch registers unavailable when new register allocator used
  1384. + maybe_save/maybe_restore unavailable when new register allocator used
  1385. Revision 1.98 2003/04/17 07:50:24 daniel
  1386. * Some work on interference graph construction
  1387. Revision 1.97 2003/04/16 09:26:55 jonas
  1388. * assembler procedures now again get a stackframe if they have local
  1389. variables. No space is reserved for a function result however.
  1390. Also, the register parameters aren't automatically saved on the stack
  1391. anymore in assembler procedures.
  1392. Revision 1.96 2003/04/05 21:09:31 jonas
  1393. * several ppc/generic result offset related fixes. The "normal" result
  1394. offset seems now to be calculated correctly and a lot of duplicate
  1395. calculations have been removed. Nested functions accessing the parent's
  1396. function result don't work at all though :(
  1397. Revision 1.95 2003/04/02 16:11:34 peter
  1398. * give error when exports is not supported
  1399. Revision 1.94 2003/03/12 22:43:38 jonas
  1400. * more powerpc and generic fixes related to the new register allocator
  1401. Revision 1.93 2003/03/08 08:59:07 daniel
  1402. + $define newra will enable new register allocator
  1403. + getregisterint will return imaginary registers with $newra
  1404. + -sr switch added, will skip register allocation so you can see
  1405. the direct output of the code generator before register allocation
  1406. Revision 1.92 2003/02/19 22:00:14 daniel
  1407. * Code generator converted to new register notation
  1408. - Horribily outdated todo.txt removed
  1409. Revision 1.91 2003/01/09 21:52:37 peter
  1410. * merged some verbosity options.
  1411. * V_LineInfo is a verbosity flag to include line info
  1412. Revision 1.90 2003/01/09 20:40:59 daniel
  1413. * Converted some code in cgx86.pas to new register numbering
  1414. Revision 1.89 2003/01/09 15:49:56 daniel
  1415. * Added register conversion
  1416. Revision 1.88 2003/01/08 18:43:56 daniel
  1417. * Tregister changed into a record
  1418. Revision 1.87 2003/01/03 20:35:08 peter
  1419. * check also interfacedef when checking for matching forwarddef
  1420. Revision 1.86 2003/01/02 11:14:02 michael
  1421. + Patch from peter to support initial values for local variables
  1422. Revision 1.85 2002/12/29 18:59:34 peter
  1423. * fixed parsing of declarations before asm statement
  1424. Revision 1.84 2002/12/29 18:25:18 peter
  1425. * parse declarations before check _ASM token
  1426. Revision 1.83 2002/12/29 14:57:50 peter
  1427. * unit loading changed to first register units and load them
  1428. afterwards. This is needed to support uses xxx in yyy correctly
  1429. * unit dependency check fixed
  1430. Revision 1.82 2002/12/25 01:26:56 peter
  1431. * duplicate procsym-unitsym fix
  1432. Revision 1.81 2002/12/15 13:37:15 peter
  1433. * don't include uf_init for library. The code is already called and
  1434. does not need to be in the initfinal table
  1435. Revision 1.80 2002/12/07 14:27:09 carl
  1436. * 3% memory optimization
  1437. * changed some types
  1438. + added type checking with different size for call node and for
  1439. parameters
  1440. Revision 1.79 2002/11/25 18:43:32 carl
  1441. - removed the invalid if <> checking (Delphi is strange on this)
  1442. + implemented abstract warning on instance creation of class with
  1443. abstract methods.
  1444. * some error message cleanups
  1445. Revision 1.78 2002/11/25 17:43:23 peter
  1446. * splitted defbase in defutil,symutil,defcmp
  1447. * merged isconvertable and is_equal into compare_defs(_ext)
  1448. * made operator search faster by walking the list only once
  1449. Revision 1.77 2002/11/23 22:50:06 carl
  1450. * some small speed optimizations
  1451. + added several new warnings/hints
  1452. Revision 1.76 2002/11/18 17:31:58 peter
  1453. * pass proccalloption to ret_in_xxx and push_xxx functions
  1454. Revision 1.75 2002/11/17 16:31:57 carl
  1455. * memory optimization (3-4%) : cleanup of tai fields,
  1456. cleanup of tdef and tsym fields.
  1457. * make it work for m68k
  1458. Revision 1.74 2002/11/15 01:58:53 peter
  1459. * merged changes from 1.0.7 up to 04-11
  1460. - -V option for generating bug report tracing
  1461. - more tracing for option parsing
  1462. - errors for cdecl and high()
  1463. - win32 import stabs
  1464. - win32 records<=8 are returned in eax:edx (turned off by default)
  1465. - heaptrc update
  1466. - more info for temp management in .s file with EXTDEBUG
  1467. Revision 1.73 2002/11/09 15:32:30 carl
  1468. * noopt for non-i386 targets
  1469. Revision 1.72 2002/09/10 20:31:48 florian
  1470. * call to current_procinfo.after_header added
  1471. Revision 1.71 2002/09/07 15:25:07 peter
  1472. * old logs removed and tabs fixed
  1473. Revision 1.70 2002/09/03 16:26:27 daniel
  1474. * Make Tprocdef.defs protected
  1475. Revision 1.69 2002/08/25 19:25:20 peter
  1476. * sym.insert_in_data removed
  1477. * symtable.insertvardata/insertconstdata added
  1478. * removed insert_in_data call from symtable.insert, it needs to be
  1479. called separatly. This allows to deref the address calculation
  1480. * procedures now calculate the parast addresses after the procedure
  1481. directives are parsed. This fixes the cdecl parast problem
  1482. * push_addr_param has an extra argument that specifies if cdecl is used
  1483. or not
  1484. Revision 1.68 2002/08/17 09:23:41 florian
  1485. * first part of procinfo rewrite
  1486. Revision 1.67 2002/08/16 14:24:59 carl
  1487. * issameref() to test if two references are the same (then emit no opcodes)
  1488. + ret_in_reg to replace ret_in_acc
  1489. (fix some register allocation bugs at the same time)
  1490. + save_std_register now has an extra parameter which is the
  1491. usedinproc registers
  1492. Revision 1.66 2002/08/11 14:32:27 peter
  1493. * renamed current_library to objectlibrary
  1494. Revision 1.65 2002/08/11 13:24:13 peter
  1495. * saving of asmsymbols in ppu supported
  1496. * asmsymbollist global is removed and moved into a new class
  1497. tasmlibrarydata that will hold the info of a .a file which
  1498. corresponds with a single module. Added librarydata to tmodule
  1499. to keep the library info stored for the module. In the future the
  1500. objectfiles will also be stored to the tasmlibrarydata class
  1501. * all getlabel/newasmsymbol and friends are moved to the new class
  1502. Revision 1.64 2002/08/09 19:14:28 carl
  1503. * fixed stackframe parameter (should only contain local size),
  1504. set to zero currently
  1505. Revision 1.63 2002/08/06 20:55:22 florian
  1506. * first part of ppc calling conventions fix
  1507. Revision 1.62 2002/07/26 21:15:41 florian
  1508. * rewrote the system handling
  1509. Revision 1.61 2002/07/20 11:57:56 florian
  1510. * types.pas renamed to defbase.pas because D6 contains a types
  1511. unit so this would conflicts if D6 programms are compiled
  1512. + Willamette/SSE2 instructions to assembler added
  1513. Revision 1.60 2002/07/19 11:41:36 daniel
  1514. * State tracker work
  1515. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1516. allows the state tracker to change while nodes automatically into
  1517. repeat nodes.
  1518. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1519. 'not(a>b)' is optimized into 'a<=b'.
  1520. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1521. by removing the notn and later switchting the true and falselabels. The
  1522. same is done with 'repeat until not a'.
  1523. Revision 1.59 2002/07/15 18:03:15 florian
  1524. * readded removed changes
  1525. Revision 1.57 2002/07/11 14:41:28 florian
  1526. * start of the new generic parameter handling
  1527. Revision 1.58 2002/07/14 18:00:44 daniel
  1528. + Added the beginning of a state tracker. This will track the values of
  1529. variables through procedures and optimize things away.
  1530. Revision 1.56 2002/07/07 09:52:32 florian
  1531. * powerpc target fixed, very simple units can be compiled
  1532. * some basic stuff for better callparanode handling, far from being finished
  1533. Revision 1.55 2002/07/04 20:43:01 florian
  1534. * first x86-64 patches
  1535. Revision 1.54 2002/07/01 18:46:25 peter
  1536. * internal linker
  1537. * reorganized aasm layer
  1538. Revision 1.53 2002/05/18 13:34:14 peter
  1539. * readded missing revisions
  1540. Revision 1.52 2002/05/16 19:46:44 carl
  1541. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1542. + try to fix temp allocation (still in ifdef)
  1543. + generic constructor calls
  1544. + start of tassembler / tmodulebase class cleanup
  1545. Revision 1.51 2002/05/14 19:34:49 peter
  1546. * removed old logs and updated copyright year
  1547. }