psub.pas 64 KB

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