psub.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. interface
  21. uses
  22. symdef;
  23. procedure printnode_reset;
  24. procedure compile_proc_body(pd:tprocdef;make_global,parent_has_class:boolean);
  25. { reads the declaration blocks }
  26. procedure read_declarations(islibrary : boolean);
  27. { reads declarations in the interface part of a unit }
  28. procedure read_interface_declarations;
  29. implementation
  30. uses
  31. { common }
  32. cutils,cclasses,
  33. { global }
  34. globtype,globals,tokens,verbose,comphook,
  35. systems,
  36. { aasm }
  37. cpubase,cpuinfo,aasmbase,aasmtai,
  38. { symtable }
  39. symconst,symbase,symsym,symtype,symtable,defutil,
  40. paramgr,
  41. ppu,fmodule,
  42. { pass 1 }
  43. node,
  44. nbas,nld,
  45. pass_1,
  46. {$ifdef state_tracking}
  47. nstate,
  48. {$endif state_tracking}
  49. { pass 2 }
  50. {$ifndef NOPASS2}
  51. pass_2,
  52. {$endif}
  53. { parser }
  54. scanner,
  55. pbase,pstatmnt,pdecl,pdecsub,pexports,
  56. { codegen }
  57. tgobj,cgbase,rgobj,rgcpu,
  58. ncgutil
  59. {$ifndef NOOPT}
  60. {$ifdef i386}
  61. ,aopt386
  62. {$else i386}
  63. ,aoptcpu
  64. {$endif i386}
  65. {$endif}
  66. ;
  67. {****************************************************************************
  68. PROCEDURE/FUNCTION BODY PARSING
  69. ****************************************************************************}
  70. procedure initializevars(p:tnamedindexitem;arg:pointer);
  71. var
  72. b : tblocknode;
  73. begin
  74. if tsym(p).typ<>varsym then
  75. exit;
  76. with tvarsym(p) do
  77. begin
  78. if assigned(defaultconstsym) then
  79. begin
  80. b:=tblocknode(arg);
  81. b.left:=cstatementnode.create(
  82. cassignmentnode.create(
  83. cloadnode.create(tsym(p),tsym(p).owner),
  84. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  85. b.left);
  86. end;
  87. end;
  88. end;
  89. function block(islibrary : boolean) : tnode;
  90. begin
  91. { parse const,types and vars }
  92. read_declarations(islibrary);
  93. current_procinfo.handle_body_start;
  94. { do we have an assembler block without the po_assembler?
  95. we should allow this for Delphi compatibility (PFV) }
  96. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  97. include(current_procdef.procoptions,po_assembler);
  98. { Handle assembler block different }
  99. if (po_assembler in current_procdef.procoptions) then
  100. begin
  101. block:=assembler_block;
  102. exit;
  103. end;
  104. {Unit initialization?.}
  105. if (
  106. assigned(current_procdef.localst) and
  107. (current_procdef.localst.symtablelevel=main_program_level) and
  108. (current_module.is_unit)
  109. ) or
  110. islibrary then
  111. begin
  112. if (token=_END) then
  113. begin
  114. consume(_END);
  115. { We need at least a node, else the entry/exit code is not
  116. generated and thus no PASCALMAIN symbol which we need (PFV) }
  117. if islibrary then
  118. block:=cnothingnode.create
  119. else
  120. block:=nil;
  121. end
  122. else
  123. begin
  124. if token=_INITIALIZATION then
  125. begin
  126. { The library init code is already called and does not
  127. need to be in the initfinal table (PFV) }
  128. if not islibrary then
  129. current_module.flags:=current_module.flags or uf_init;
  130. block:=statement_block(_INITIALIZATION);
  131. end
  132. else if (token=_FINALIZATION) then
  133. begin
  134. if (current_module.flags and uf_finalize)<>0 then
  135. block:=statement_block(_FINALIZATION)
  136. else
  137. begin
  138. { can we allow no INITIALIZATION for DLL ??
  139. I think it should work PM }
  140. block:=nil;
  141. exit;
  142. end;
  143. end
  144. else
  145. begin
  146. { The library init code is already called and does not
  147. need to be in the initfinal table (PFV) }
  148. if not islibrary then
  149. current_module.flags:=current_module.flags or uf_init;
  150. block:=statement_block(_BEGIN);
  151. end;
  152. end;
  153. end
  154. else
  155. begin
  156. block:=statement_block(_BEGIN);
  157. if symtablestack.symtabletype=localsymtable then
  158. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}initializevars,block);
  159. end;
  160. end;
  161. {****************************************************************************
  162. PROCEDURE/FUNCTION COMPILING
  163. ****************************************************************************}
  164. procedure printnode_reset;
  165. begin
  166. assign(printnodefile,treelogfilename);
  167. {$I-}
  168. rewrite(printnodefile);
  169. {$I+}
  170. if ioresult<>0 then
  171. begin
  172. Comment(V_Error,'Error creating '+treelogfilename);
  173. exit;
  174. end;
  175. close(printnodefile);
  176. end;
  177. procedure printnode_procdef(pd:tprocdef);
  178. begin
  179. assign(printnodefile,treelogfilename);
  180. {$I-}
  181. append(printnodefile);
  182. if ioresult<>0 then
  183. rewrite(printnodefile);
  184. {$I+}
  185. if ioresult<>0 then
  186. begin
  187. Comment(V_Error,'Error creating '+treelogfilename);
  188. exit;
  189. end;
  190. writeln(printnodefile);
  191. writeln(printnodefile,'*******************************************************************************');
  192. writeln(printnodefile,current_procdef.fullprocname(false));
  193. writeln(printnodefile,'*******************************************************************************');
  194. printnode(printnodefile,pd.code);
  195. close(printnodefile);
  196. end;
  197. procedure compile_proc_body(pd:tprocdef;make_global,parent_has_class:boolean);
  198. {
  199. Compile the body of a procedure
  200. }
  201. var
  202. oldexitlabel,oldexit2label : tasmlabel;
  203. oldfaillabel,oldquickexitlabel:tasmlabel;
  204. _class,hp:tobjectdef;
  205. { switches can change inside the procedure }
  206. entryswitches, exitswitches : tlocalswitches;
  207. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  208. { code for the subroutine as tree }
  209. code:tnode;
  210. { true when no stackframe is required }
  211. nostackframe:boolean;
  212. { number of bytes which have to be cleared by RET }
  213. parasize:longint;
  214. { filepositions }
  215. entrypos,
  216. savepos,
  217. exitpos : tfileposinfo;
  218. oldprocdef : tprocdef;
  219. begin
  220. oldprocdef:=current_procdef;
  221. current_procdef:=pd;
  222. { calculate the lexical level }
  223. if current_procdef.parast.symtablelevel>maxnesting then
  224. Message(parser_e_too_much_lexlevel);
  225. { static is also important for local procedures !! }
  226. if (po_staticmethod in current_procdef.procoptions) then
  227. allow_only_static:=true
  228. else if (current_procdef.parast.symtablelevel=normal_function_level) then
  229. allow_only_static:=false;
  230. { save old labels }
  231. oldexitlabel:=aktexitlabel;
  232. oldexit2label:=aktexit2label;
  233. oldquickexitlabel:=quickexitlabel;
  234. oldfaillabel:=faillabel;
  235. { get new labels }
  236. objectlibrary.getlabel(aktexitlabel);
  237. objectlibrary.getlabel(aktexit2label);
  238. { exit for fail in constructors }
  239. if (current_procdef.proctypeoption=potype_constructor) then
  240. begin
  241. objectlibrary.getlabel(faillabel);
  242. objectlibrary.getlabel(quickexitlabel);
  243. end;
  244. { reset break and continue labels }
  245. block_type:=bt_general;
  246. aktbreaklabel:=nil;
  247. aktcontinuelabel:=nil;
  248. {$ifdef state_tracking}
  249. { aktstate:=Tstate_storage.create;}
  250. {$endif state_tracking}
  251. { insert symtables for the class, but only if it is no nested function }
  252. if assigned(current_procdef._class) and not(parent_has_class) then
  253. begin
  254. { insert them in the reverse order }
  255. hp:=nil;
  256. repeat
  257. _class:=current_procdef._class;
  258. while _class.childof<>hp do
  259. _class:=_class.childof;
  260. hp:=_class;
  261. _class.symtable.next:=symtablestack;
  262. symtablestack:=_class.symtable;
  263. until hp=current_procdef._class;
  264. end;
  265. { insert parasymtable in symtablestack when parsing
  266. a function }
  267. if current_procdef.parast.symtablelevel>=normal_function_level then
  268. begin
  269. current_procdef.parast.next:=symtablestack;
  270. symtablestack:=current_procdef.parast;
  271. end;
  272. { create a local symbol table for this routine }
  273. if not assigned(current_procdef.localst) then
  274. current_procdef.insert_localst;
  275. { insert localsymtable in symtablestack}
  276. current_procdef.localst.next:=symtablestack;
  277. symtablestack:=current_procdef.localst;
  278. { constant symbols are inserted in this symboltable }
  279. constsymtable:=symtablestack;
  280. { reset the temporary memory }
  281. rg.cleartempgen;
  282. rg.usedinproc:=[];
  283. rg.usedbyproc:=[];
  284. { save entry info }
  285. entrypos:=aktfilepos;
  286. entryswitches:=aktlocalswitches;
  287. localmaxfpuregisters:=aktmaxfpuregisters;
  288. { parse the code ... }
  289. code:=block(current_module.islibrary);
  290. { store a copy of the original tree for inline, for
  291. normal procedures only store a reference to the
  292. current tree }
  293. if (current_procdef.proccalloption=pocall_inline) then
  294. current_procdef.code:=code.getcopy
  295. else
  296. current_procdef.code:=code;
  297. { get a better entry point }
  298. if assigned(code) then
  299. entrypos:=code.fileinfo;
  300. { save exit info }
  301. exitswitches:=aktlocalswitches;
  302. exitpos:=last_endtoken_filepos;
  303. { save current filepos }
  304. savepos:=aktfilepos;
  305. {When we are called to compile the body of a unit, aktprocsym should
  306. point to the unit initialization. If the unit has no initialization,
  307. aktprocsym=nil. But in that case code=nil. Thus we should check for
  308. code=nil, when we use aktprocsym.}
  309. { set the start offset to the start of the temp area in the stack }
  310. tg.setfirsttemp(current_procinfo.firsttemp_offset);
  311. { ... and generate assembler }
  312. { but set the right switches for entry !! }
  313. aktlocalswitches:=entryswitches;
  314. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  315. aktmaxfpuregisters:=localmaxfpuregisters;
  316. if assigned(code) then
  317. begin
  318. { the procedure is now defined }
  319. current_procdef.forwarddef:=false;
  320. if paraprintnodetree=1 then
  321. printnode_procdef(current_procdef);
  322. { only generate the code if no type errors are found, else
  323. finish at least the type checking pass }
  324. {$ifndef NOPASS2}
  325. if (status.errorcount=0) then
  326. begin
  327. generatecode(code);
  328. { first generate entry code with the correct position and switches }
  329. aktfilepos:=entrypos;
  330. aktlocalswitches:=entryswitches;
  331. genentrycode(current_procinfo.aktentrycode,make_global,0,parasize,nostackframe,false);
  332. { FPC_POPADDRSTACK destroys all registers (JM) }
  333. if (pi_needs_implicit_finally in current_procinfo.flags) or
  334. (pi_uses_exceptions in current_procinfo.flags) then
  335. begin
  336. rg.usedinproc := ALL_REGISTERS;
  337. end;
  338. { now generate exit code with the correct position and switches }
  339. aktfilepos:=exitpos;
  340. aktlocalswitches:=exitswitches;
  341. genexitcode(current_procinfo.aktexitcode,parasize,nostackframe,false);
  342. { now all the registers used are known }
  343. current_procdef.usedintregisters:=rg.usedintinproc;
  344. current_procdef.usedotherregisters:=rg.usedinproc;
  345. current_procinfo.aktproccode.insertlist(current_procinfo.aktentrycode);
  346. current_procinfo.aktproccode.concatlist(current_procinfo.aktexitcode);
  347. {$ifdef newra}
  348. { rg.writegraph;}
  349. {$endif}
  350. if not(cs_no_regalloc in aktglobalswitches) then
  351. begin
  352. {$ifdef newra}
  353. {Do register allocation.}
  354. repeat
  355. rg.prepare_colouring;
  356. rg.colour_registers;
  357. rg.epilogue_colouring;
  358. {Are there spilled registers? We cannot do that yet.}
  359. if rg.spillednodes<>'' then
  360. internalerror(200304221);
  361. {if not try_fast_spill(rg) then
  362. slow_spill(rg);
  363. }
  364. until rg.spillednodes='';
  365. current_procinfo.aktproccode.translate_registers(rg.colour);
  366. current_procinfo.aktproccode.convert_registers;
  367. {$else newra}
  368. current_procinfo.aktproccode.convert_registers;
  369. {$ifndef NoOpt}
  370. if (cs_optimize in aktglobalswitches) and
  371. { do not optimize pure assembler procedures }
  372. not(pi_is_assembler in current_procinfo.flags) then
  373. optimize(current_procinfo.aktproccode);
  374. {$endif NoOpt}
  375. {$endif newra}
  376. end;
  377. { save local data (casetable) also in the same file }
  378. if assigned(current_procinfo.aktlocaldata) and
  379. (not current_procinfo.aktlocaldata.empty) then
  380. begin
  381. current_procinfo.aktproccode.concat(Tai_section.Create(sec_data));
  382. current_procinfo.aktproccode.concatlist(current_procinfo.aktlocaldata);
  383. current_procinfo.aktproccode.concat(Tai_section.Create(sec_code));
  384. end;
  385. { add the procedure to the codesegment }
  386. if (cs_create_smart in aktmoduleswitches) then
  387. codeSegment.concat(Tai_cut.Create);
  388. codeSegment.concatlist(current_procinfo.aktproccode);
  389. end
  390. else
  391. do_resulttypepass(code);
  392. {$else NOPASS2}
  393. do_resulttypepass(code);
  394. {$endif NOPASS2}
  395. end;
  396. { ... remove symbol tables }
  397. if current_procdef.parast.symtablelevel>=normal_function_level then
  398. symtablestack:=symtablestack.next.next
  399. else
  400. symtablestack:=symtablestack.next;
  401. { ... check for unused symbols }
  402. { but only if there is no asm block }
  403. if assigned(code) then
  404. begin
  405. if (Errorcount=0) then
  406. begin
  407. { check if forwards are resolved }
  408. tstoredsymtable(current_procdef.localst).check_forwards;
  409. { check if all labels are used }
  410. tstoredsymtable(current_procdef.localst).checklabels;
  411. { remove cross unit overloads }
  412. tstoredsymtable(current_procdef.localst).unchain_overloaded;
  413. end;
  414. if not(pi_uses_asm in current_procinfo.flags) then
  415. begin
  416. { not for unit init, becuase the var can be used in finalize,
  417. it will be done in proc_unit }
  418. if not(current_procdef.proctypeoption
  419. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  420. tstoredsymtable(current_procdef.localst).allsymbolsused;
  421. tstoredsymtable(current_procdef.parast).allsymbolsused;
  422. end;
  423. end;
  424. { the local symtables can be deleted, but the parast }
  425. { doesn't, (checking definitons when calling a }
  426. { function }
  427. { not for a inline procedure !! (PM) }
  428. { at lexlevel = 1 localst is the staticsymtable itself }
  429. { so no dispose here !! }
  430. if assigned(code) and
  431. not(cs_browser in aktmoduleswitches) and
  432. (current_procdef.proccalloption<>pocall_inline) then
  433. begin
  434. if current_procdef.parast.symtablelevel>=normal_function_level then
  435. current_procdef.localst.free;
  436. current_procdef.localst:=nil;
  437. end;
  438. { all registers can be used again }
  439. rg.resetusableregisters;
  440. { only now we can remove the temps }
  441. tg.resettempgen;
  442. { remove code tree, if not inline procedure }
  443. if assigned(code) then
  444. begin
  445. { the inline procedure has already got a copy of the tree
  446. stored in current_procdef.code }
  447. code.free;
  448. if (current_procdef.proccalloption<>pocall_inline) then
  449. current_procdef.code:=nil;
  450. end;
  451. { remove class member symbol tables }
  452. while symtablestack.symtabletype=objectsymtable do
  453. symtablestack:=symtablestack.next;
  454. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  455. {$ifdef state_tracking}
  456. { aktstate.destroy;}
  457. {$endif state_tracking}
  458. { restore filepos, the switches are already set }
  459. aktfilepos:=savepos;
  460. { restore labels }
  461. aktexitlabel:=oldexitlabel;
  462. aktexit2label:=oldexit2label;
  463. quickexitlabel:=oldquickexitlabel;
  464. faillabel:=oldfaillabel;
  465. { reset to normal non static function }
  466. if (current_procdef.parast.symtablelevel=normal_function_level) then
  467. allow_only_static:=false;
  468. current_procdef:=oldprocdef;
  469. end;
  470. {****************************************************************************
  471. PROCEDURE/FUNCTION PARSING
  472. ****************************************************************************}
  473. procedure insert_local_value_para(p:tnamedindexitem;arg:pointer);
  474. var
  475. vs : tvarsym;
  476. pd : tprocdef;
  477. begin
  478. if tsym(p).typ<>varsym then
  479. exit;
  480. with tvarsym(p) do
  481. begin
  482. if copy(name,1,3)='val' then
  483. begin
  484. pd:=tprocdef(owner.defowner);
  485. vs:=tvarsym.create(Copy(name,4,255),vartype);
  486. vs.fileinfo:=fileinfo;
  487. vs.varspez:=varspez;
  488. if not assigned(pd.localst) then
  489. pd.insert_localst;
  490. pd.localst.insert(vs);
  491. pd.localst.insertvardata(vs);
  492. include(vs.varoptions,vo_is_local_copy);
  493. vs.varstate:=vs_assigned;
  494. localvarsym:=vs;
  495. inc(refs); { the para was used to set the local copy ! }
  496. { warnings only on local copy ! }
  497. varstate:=vs_used;
  498. end;
  499. end;
  500. end;
  501. procedure read_proc;
  502. {
  503. Parses the procedure directives, then parses the procedure body, then
  504. generates the code for it
  505. }
  506. var
  507. oldprocdef : tprocdef;
  508. old_current_procinfo : tprocinfo;
  509. oldconstsymtable : tsymtable;
  510. oldselftokenmode,
  511. oldfailtokenmode : tmodeswitch;
  512. pdflags : word;
  513. pd : tprocdef;
  514. begin
  515. { save old state }
  516. oldprocdef:=current_procdef;
  517. oldconstsymtable:=constsymtable;
  518. old_current_procinfo:=current_procinfo;
  519. { reset current_procdef to nil to be sure that nothing is writing
  520. to an other procdef }
  521. current_procdef:=nil;
  522. { create a new procedure }
  523. current_procinfo:=cprocinfo.create(old_current_procinfo);
  524. current_module.procinfo:=current_procinfo;
  525. { parse procedure declaration }
  526. if assigned(current_procinfo.parent) and
  527. assigned(current_procinfo.parent.procdef) then
  528. pd:=parse_proc_dec(current_procinfo.parent.procdef._class)
  529. else
  530. pd:=parse_proc_dec(nil);
  531. current_procinfo.procdef:=pd;
  532. { set the default function options }
  533. if parse_only then
  534. begin
  535. pd.forwarddef:=true;
  536. { set also the interface flag, for better error message when the
  537. implementation doesn't much this header }
  538. pd.interfacedef:=true;
  539. pdflags:=pd_interface;
  540. end
  541. else
  542. begin
  543. pdflags:=pd_body;
  544. if (not current_module.in_interface) then
  545. pdflags:=pdflags or pd_implemen;
  546. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  547. pdflags:=pdflags or pd_global;
  548. pd.forwarddef:=false;
  549. end;
  550. { parse the directives that may follow }
  551. parse_proc_directives(pd,pdflags);
  552. { hint directives, these can be separated by semicolons here,
  553. that needs to be handled here with a loop (PFV) }
  554. while try_consume_hintdirective(pd.symoptions) do
  555. Consume(_SEMICOLON);
  556. { everything of the proc definition is known, we can now
  557. calculate the parameters }
  558. calc_parast(pd);
  559. { search for forward declarations }
  560. if not proc_add_definition(pd) then
  561. begin
  562. { A method must be forward defined (in the object declaration) }
  563. if assigned(pd._class) and
  564. (not assigned(current_procinfo.parent.procdef._class)) then
  565. begin
  566. Message1(parser_e_header_dont_match_any_member,pd.fullprocname(false));
  567. tprocsym(pd.procsym).write_parameter_lists(pd);
  568. end
  569. else
  570. begin
  571. { Give a better error if there is a forward def in the interface and only
  572. a single implementation }
  573. if (not pd.forwarddef) and
  574. (not pd.interfacedef) and
  575. (tprocsym(pd.procsym).procdef_count>1) and
  576. tprocsym(pd.procsym).first_procdef.forwarddef and
  577. tprocsym(pd.procsym).first_procdef.interfacedef and
  578. not(tprocsym(pd.procsym).procdef_count>2) then
  579. begin
  580. Message1(parser_e_header_dont_match_forward,pd.fullprocname(false));
  581. tprocsym(pd.procsym).write_parameter_lists(pd);
  582. end
  583. else
  584. begin
  585. { check the global flag, for delphi this is not
  586. required }
  587. if not(m_delphi in aktmodeswitches) and
  588. (pi_is_global in current_procinfo.flags) then
  589. Message(parser_e_overloaded_must_be_all_global);
  590. end;
  591. end;
  592. end;
  593. { update procinfo, because the procdef can be
  594. changed by check_identical_proc (PFV) }
  595. current_procinfo.procdef:=pd;
  596. { compile procedure when a body is needed }
  597. if (pdflags and pd_body)<>0 then
  598. begin
  599. Message1(parser_d_procedure_start,pd.fullprocname(false));
  600. pd.aliasnames.insert(pd.mangledname);
  601. { Insert result variables in the localst }
  602. insert_funcret_local(pd);
  603. { Insert local copies for value para }
  604. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}insert_local_value_para,nil);
  605. { Update parameter information }
  606. current_procinfo.allocate_implicit_parameter;
  607. {$ifdef i386}
  608. { add implicit pushes for interrupt routines }
  609. if (po_interrupt in pd.procoptions) then
  610. current_procinfo.allocate_interrupt_stackframe;
  611. {$endif i386}
  612. { Calculate offsets }
  613. current_procinfo.after_header;
  614. { set _FAIL as keyword if constructor }
  615. if (pd.proctypeoption=potype_constructor) then
  616. begin
  617. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  618. tokeninfo^[_FAIL].keyword:=m_all;
  619. end;
  620. { set _SELF as keyword if methods }
  621. if assigned(pd._class) then
  622. begin
  623. oldselftokenmode:=tokeninfo^[_SELF].keyword;
  624. tokeninfo^[_SELF].keyword:=m_all;
  625. end;
  626. compile_proc_body(pd,((pdflags and pd_global)<>0),assigned(current_procinfo.parent.procdef._class));
  627. { reset _FAIL as _SELF normal }
  628. if (pd.proctypeoption=potype_constructor) then
  629. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  630. if assigned(pd._class) then
  631. tokeninfo^[_SELF].keyword:=oldselftokenmode;
  632. consume(_SEMICOLON);
  633. end;
  634. { release procinfo }
  635. if tprocinfo(current_module.procinfo)<>current_procinfo then
  636. internalerror(200304274);
  637. current_module.procinfo:=current_procinfo.parent;
  638. current_procinfo.free;
  639. { Restore old state }
  640. constsymtable:=oldconstsymtable;
  641. current_procdef:=oldprocdef;
  642. current_procinfo:=old_current_procinfo;
  643. end;
  644. {****************************************************************************
  645. DECLARATION PARSING
  646. ****************************************************************************}
  647. { search in symtablestack for not complete classes }
  648. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  649. begin
  650. if (tsym(p).typ=typesym) and
  651. (ttypesym(p).restype.def.deftype=objectdef) and
  652. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  653. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  654. end;
  655. procedure read_declarations(islibrary : boolean);
  656. procedure Not_supported_for_inline(t : ttoken);
  657. begin
  658. if (current_procdef.proccalloption=pocall_inline) then
  659. Begin
  660. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  661. Message(parser_w_inlining_disabled);
  662. current_procdef.proccalloption:=pocall_fpccall;
  663. End;
  664. end;
  665. begin
  666. repeat
  667. if not assigned(current_procdef) then
  668. internalerror(200304251);
  669. case token of
  670. _LABEL:
  671. begin
  672. Not_supported_for_inline(token);
  673. label_dec;
  674. end;
  675. _CONST:
  676. begin
  677. Not_supported_for_inline(token);
  678. const_dec;
  679. end;
  680. _TYPE:
  681. begin
  682. Not_supported_for_inline(token);
  683. type_dec;
  684. end;
  685. _VAR:
  686. var_dec;
  687. _THREADVAR:
  688. threadvar_dec;
  689. _CONSTRUCTOR,_DESTRUCTOR,
  690. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  691. begin
  692. Not_supported_for_inline(token);
  693. read_proc;
  694. end;
  695. _RESOURCESTRING:
  696. resourcestring_dec;
  697. _EXPORTS:
  698. begin
  699. Not_supported_for_inline(token);
  700. if not(assigned(current_procdef.localst)) or
  701. (current_procdef.localst.symtablelevel>main_program_level) or
  702. (current_module.is_unit) then
  703. begin
  704. Message(parser_e_syntax_error);
  705. consume_all_until(_SEMICOLON);
  706. end
  707. else if islibrary or
  708. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware]) then
  709. read_exports
  710. else
  711. begin
  712. Message(parser_w_unsupported_feature);
  713. consume(_BEGIN);
  714. end;
  715. end
  716. else break;
  717. end;
  718. until false;
  719. { check for incomplete class definitions, this is only required
  720. for fpc modes }
  721. if (m_fpc in aktmodeswitches) then
  722. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  723. end;
  724. procedure read_interface_declarations;
  725. begin
  726. repeat
  727. case token of
  728. _CONST :
  729. const_dec;
  730. _TYPE :
  731. type_dec;
  732. _VAR :
  733. var_dec;
  734. _THREADVAR :
  735. threadvar_dec;
  736. _RESOURCESTRING:
  737. resourcestring_dec;
  738. _FUNCTION,
  739. _PROCEDURE,
  740. _OPERATOR :
  741. read_proc;
  742. else
  743. break;
  744. end;
  745. until false;
  746. { check for incomplete class definitions, this is only required
  747. for fpc modes }
  748. if (m_fpc in aktmodeswitches) then
  749. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  750. end;
  751. end.
  752. {
  753. $Log$
  754. Revision 1.107 2003-04-27 11:21:34 peter
  755. * aktprocdef renamed to current_procdef
  756. * procinfo renamed to current_procinfo
  757. * procinfo will now be stored in current_module so it can be
  758. cleaned up properly
  759. * gen_main_procsym changed to create_main_proc and release_main_proc
  760. to also generate a tprocinfo structure
  761. * fixed unit implicit initfinal
  762. Revision 1.106 2003/04/27 07:29:50 peter
  763. * current_procdef cleanup, current_procdef is now always nil when parsing
  764. a new procdef declaration
  765. * aktprocsym removed
  766. * lexlevel removed, use symtable.symtablelevel instead
  767. * implicit init/final code uses the normal genentry/genexit
  768. * funcret state checking updated for new funcret handling
  769. Revision 1.105 2003/04/26 00:31:42 peter
  770. * set return_offset moved to after_header
  771. Revision 1.104 2003/04/25 20:59:34 peter
  772. * removed funcretn,funcretsym, function result is now in varsym
  773. and aliases for result and function name are added using absolutesym
  774. * vs_hidden parameter for funcret passed in parameter
  775. * vs_hidden fixes
  776. * writenode changed to printnode and released from extdebug
  777. * -vp option added to generate a tree.log with the nodetree
  778. * nicer printnode for statements, callnode
  779. Revision 1.103 2003/04/24 13:03:01 florian
  780. * comp is now written with its bit pattern to the ppu instead as an extended
  781. Revision 1.102 2003/04/23 12:35:34 florian
  782. * fixed several issues with powerpc
  783. + applied a patch from Jonas for nested function calls (PowerPC only)
  784. * ...
  785. Revision 1.101 2003/04/22 14:33:38 peter
  786. * removed some notes/hints
  787. Revision 1.100 2003/04/22 13:47:08 peter
  788. * fixed C style array of const
  789. * fixed C array passing
  790. * fixed left to right with high parameters
  791. Revision 1.99 2003/04/22 10:09:35 daniel
  792. + Implemented the actual register allocator
  793. + Scratch registers unavailable when new register allocator used
  794. + maybe_save/maybe_restore unavailable when new register allocator used
  795. Revision 1.98 2003/04/17 07:50:24 daniel
  796. * Some work on interference graph construction
  797. Revision 1.97 2003/04/16 09:26:55 jonas
  798. * assembler procedures now again get a stackframe if they have local
  799. variables. No space is reserved for a function result however.
  800. Also, the register parameters aren't automatically saved on the stack
  801. anymore in assembler procedures.
  802. Revision 1.96 2003/04/05 21:09:31 jonas
  803. * several ppc/generic result offset related fixes. The "normal" result
  804. offset seems now to be calculated correctly and a lot of duplicate
  805. calculations have been removed. Nested functions accessing the parent's
  806. function result don't work at all though :(
  807. Revision 1.95 2003/04/02 16:11:34 peter
  808. * give error when exports is not supported
  809. Revision 1.94 2003/03/12 22:43:38 jonas
  810. * more powerpc and generic fixes related to the new register allocator
  811. Revision 1.93 2003/03/08 08:59:07 daniel
  812. + $define newra will enable new register allocator
  813. + getregisterint will return imaginary registers with $newra
  814. + -sr switch added, will skip register allocation so you can see
  815. the direct output of the code generator before register allocation
  816. Revision 1.92 2003/02/19 22:00:14 daniel
  817. * Code generator converted to new register notation
  818. - Horribily outdated todo.txt removed
  819. Revision 1.91 2003/01/09 21:52:37 peter
  820. * merged some verbosity options.
  821. * V_LineInfo is a verbosity flag to include line info
  822. Revision 1.90 2003/01/09 20:40:59 daniel
  823. * Converted some code in cgx86.pas to new register numbering
  824. Revision 1.89 2003/01/09 15:49:56 daniel
  825. * Added register conversion
  826. Revision 1.88 2003/01/08 18:43:56 daniel
  827. * Tregister changed into a record
  828. Revision 1.87 2003/01/03 20:35:08 peter
  829. * check also interfacedef when checking for matching forwarddef
  830. Revision 1.86 2003/01/02 11:14:02 michael
  831. + Patch from peter to support initial values for local variables
  832. Revision 1.85 2002/12/29 18:59:34 peter
  833. * fixed parsing of declarations before asm statement
  834. Revision 1.84 2002/12/29 18:25:18 peter
  835. * parse declarations before check _ASM token
  836. Revision 1.83 2002/12/29 14:57:50 peter
  837. * unit loading changed to first register units and load them
  838. afterwards. This is needed to support uses xxx in yyy correctly
  839. * unit dependency check fixed
  840. Revision 1.82 2002/12/25 01:26:56 peter
  841. * duplicate procsym-unitsym fix
  842. Revision 1.81 2002/12/15 13:37:15 peter
  843. * don't include uf_init for library. The code is already called and
  844. does not need to be in the initfinal table
  845. Revision 1.80 2002/12/07 14:27:09 carl
  846. * 3% memory optimization
  847. * changed some types
  848. + added type checking with different size for call node and for
  849. parameters
  850. Revision 1.79 2002/11/25 18:43:32 carl
  851. - removed the invalid if <> checking (Delphi is strange on this)
  852. + implemented abstract warning on instance creation of class with
  853. abstract methods.
  854. * some error message cleanups
  855. Revision 1.78 2002/11/25 17:43:23 peter
  856. * splitted defbase in defutil,symutil,defcmp
  857. * merged isconvertable and is_equal into compare_defs(_ext)
  858. * made operator search faster by walking the list only once
  859. Revision 1.77 2002/11/23 22:50:06 carl
  860. * some small speed optimizations
  861. + added several new warnings/hints
  862. Revision 1.76 2002/11/18 17:31:58 peter
  863. * pass proccalloption to ret_in_xxx and push_xxx functions
  864. Revision 1.75 2002/11/17 16:31:57 carl
  865. * memory optimization (3-4%) : cleanup of tai fields,
  866. cleanup of tdef and tsym fields.
  867. * make it work for m68k
  868. Revision 1.74 2002/11/15 01:58:53 peter
  869. * merged changes from 1.0.7 up to 04-11
  870. - -V option for generating bug report tracing
  871. - more tracing for option parsing
  872. - errors for cdecl and high()
  873. - win32 import stabs
  874. - win32 records<=8 are returned in eax:edx (turned off by default)
  875. - heaptrc update
  876. - more info for temp management in .s file with EXTDEBUG
  877. Revision 1.73 2002/11/09 15:32:30 carl
  878. * noopt for non-i386 targets
  879. Revision 1.72 2002/09/10 20:31:48 florian
  880. * call to current_procinfo.after_header added
  881. Revision 1.71 2002/09/07 15:25:07 peter
  882. * old logs removed and tabs fixed
  883. Revision 1.70 2002/09/03 16:26:27 daniel
  884. * Make Tprocdef.defs protected
  885. Revision 1.69 2002/08/25 19:25:20 peter
  886. * sym.insert_in_data removed
  887. * symtable.insertvardata/insertconstdata added
  888. * removed insert_in_data call from symtable.insert, it needs to be
  889. called separatly. This allows to deref the address calculation
  890. * procedures now calculate the parast addresses after the procedure
  891. directives are parsed. This fixes the cdecl parast problem
  892. * push_addr_param has an extra argument that specifies if cdecl is used
  893. or not
  894. Revision 1.68 2002/08/17 09:23:41 florian
  895. * first part of procinfo rewrite
  896. Revision 1.67 2002/08/16 14:24:59 carl
  897. * issameref() to test if two references are the same (then emit no opcodes)
  898. + ret_in_reg to replace ret_in_acc
  899. (fix some register allocation bugs at the same time)
  900. + save_std_register now has an extra parameter which is the
  901. usedinproc registers
  902. Revision 1.66 2002/08/11 14:32:27 peter
  903. * renamed current_library to objectlibrary
  904. Revision 1.65 2002/08/11 13:24:13 peter
  905. * saving of asmsymbols in ppu supported
  906. * asmsymbollist global is removed and moved into a new class
  907. tasmlibrarydata that will hold the info of a .a file which
  908. corresponds with a single module. Added librarydata to tmodule
  909. to keep the library info stored for the module. In the future the
  910. objectfiles will also be stored to the tasmlibrarydata class
  911. * all getlabel/newasmsymbol and friends are moved to the new class
  912. Revision 1.64 2002/08/09 19:14:28 carl
  913. * fixed stackframe parameter (should only contain local size),
  914. set to zero currently
  915. Revision 1.63 2002/08/06 20:55:22 florian
  916. * first part of ppc calling conventions fix
  917. Revision 1.62 2002/07/26 21:15:41 florian
  918. * rewrote the system handling
  919. Revision 1.61 2002/07/20 11:57:56 florian
  920. * types.pas renamed to defbase.pas because D6 contains a types
  921. unit so this would conflicts if D6 programms are compiled
  922. + Willamette/SSE2 instructions to assembler added
  923. Revision 1.60 2002/07/19 11:41:36 daniel
  924. * State tracker work
  925. * The whilen and repeatn are now completely unified into whilerepeatn. This
  926. allows the state tracker to change while nodes automatically into
  927. repeat nodes.
  928. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  929. 'not(a>b)' is optimized into 'a<=b'.
  930. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  931. by removing the notn and later switchting the true and falselabels. The
  932. same is done with 'repeat until not a'.
  933. Revision 1.59 2002/07/15 18:03:15 florian
  934. * readded removed changes
  935. Revision 1.57 2002/07/11 14:41:28 florian
  936. * start of the new generic parameter handling
  937. Revision 1.58 2002/07/14 18:00:44 daniel
  938. + Added the beginning of a state tracker. This will track the values of
  939. variables through procedures and optimize things away.
  940. Revision 1.56 2002/07/07 09:52:32 florian
  941. * powerpc target fixed, very simple units can be compiled
  942. * some basic stuff for better callparanode handling, far from being finished
  943. Revision 1.55 2002/07/04 20:43:01 florian
  944. * first x86-64 patches
  945. Revision 1.54 2002/07/01 18:46:25 peter
  946. * internal linker
  947. * reorganized aasm layer
  948. Revision 1.53 2002/05/18 13:34:14 peter
  949. * readded missing revisions
  950. Revision 1.52 2002/05/16 19:46:44 carl
  951. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  952. + try to fix temp allocation (still in ifdef)
  953. + generic constructor calls
  954. + start of tassembler / tmodulebase class cleanup
  955. Revision 1.51 2002/05/14 19:34:49 peter
  956. * removed old logs and updated copyright year
  957. }