psub.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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),varspez,vartype);
  486. vs.fileinfo:=fileinfo;
  487. if not assigned(pd.localst) then
  488. pd.insert_localst;
  489. pd.localst.insert(vs);
  490. pd.localst.insertvardata(vs);
  491. include(vs.varoptions,vo_is_local_copy);
  492. vs.varstate:=vs_assigned;
  493. localvarsym:=vs;
  494. inc(refs); { the para was used to set the local copy ! }
  495. { warnings only on local copy ! }
  496. varstate:=vs_used;
  497. end;
  498. end;
  499. end;
  500. procedure read_proc;
  501. {
  502. Parses the procedure directives, then parses the procedure body, then
  503. generates the code for it
  504. }
  505. var
  506. oldprocdef : tprocdef;
  507. old_current_procinfo : tprocinfo;
  508. oldconstsymtable : tsymtable;
  509. oldselftokenmode,
  510. oldfailtokenmode : tmodeswitch;
  511. pdflags : word;
  512. pd : tprocdef;
  513. begin
  514. { save old state }
  515. oldprocdef:=current_procdef;
  516. oldconstsymtable:=constsymtable;
  517. old_current_procinfo:=current_procinfo;
  518. { reset current_procdef to nil to be sure that nothing is writing
  519. to an other procdef }
  520. current_procdef:=nil;
  521. { create a new procedure }
  522. current_procinfo:=cprocinfo.create(old_current_procinfo);
  523. current_module.procinfo:=current_procinfo;
  524. { parse procedure declaration }
  525. if assigned(current_procinfo.parent) and
  526. assigned(current_procinfo.parent.procdef) then
  527. pd:=parse_proc_dec(current_procinfo.parent.procdef._class)
  528. else
  529. pd:=parse_proc_dec(nil);
  530. current_procinfo.procdef:=pd;
  531. { set the default function options }
  532. if parse_only then
  533. begin
  534. pd.forwarddef:=true;
  535. { set also the interface flag, for better error message when the
  536. implementation doesn't much this header }
  537. pd.interfacedef:=true;
  538. pdflags:=pd_interface;
  539. end
  540. else
  541. begin
  542. pdflags:=pd_body;
  543. if (not current_module.in_interface) then
  544. pdflags:=pdflags or pd_implemen;
  545. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  546. pdflags:=pdflags or pd_global;
  547. pd.forwarddef:=false;
  548. end;
  549. { parse the directives that may follow }
  550. parse_proc_directives(pd,pdflags);
  551. { hint directives, these can be separated by semicolons here,
  552. that needs to be handled here with a loop (PFV) }
  553. while try_consume_hintdirective(pd.symoptions) do
  554. Consume(_SEMICOLON);
  555. { everything of the proc definition is known, we can now
  556. calculate the parameters }
  557. calc_parast(pd);
  558. { search for forward declarations }
  559. if not proc_add_definition(pd) then
  560. begin
  561. { A method must be forward defined (in the object declaration) }
  562. if assigned(pd._class) and
  563. (not assigned(current_procinfo.parent.procdef._class)) then
  564. begin
  565. Message1(parser_e_header_dont_match_any_member,pd.fullprocname(false));
  566. tprocsym(pd.procsym).write_parameter_lists(pd);
  567. end
  568. else
  569. begin
  570. { Give a better error if there is a forward def in the interface and only
  571. a single implementation }
  572. if (not pd.forwarddef) and
  573. (not pd.interfacedef) and
  574. (tprocsym(pd.procsym).procdef_count>1) and
  575. tprocsym(pd.procsym).first_procdef.forwarddef and
  576. tprocsym(pd.procsym).first_procdef.interfacedef and
  577. not(tprocsym(pd.procsym).procdef_count>2) then
  578. begin
  579. Message1(parser_e_header_dont_match_forward,pd.fullprocname(false));
  580. tprocsym(pd.procsym).write_parameter_lists(pd);
  581. end
  582. else
  583. begin
  584. { check the global flag, for delphi this is not
  585. required }
  586. if not(m_delphi in aktmodeswitches) and
  587. (pi_is_global in current_procinfo.flags) then
  588. Message(parser_e_overloaded_must_be_all_global);
  589. end;
  590. end;
  591. end;
  592. { update procinfo, because the procdef can be
  593. changed by check_identical_proc (PFV) }
  594. current_procinfo.procdef:=pd;
  595. { compile procedure when a body is needed }
  596. if (pdflags and pd_body)<>0 then
  597. begin
  598. Message1(parser_d_procedure_start,pd.fullprocname(false));
  599. pd.aliasnames.insert(pd.mangledname);
  600. { Insert result variables in the localst }
  601. insert_funcret_local(pd);
  602. { Insert local copies for value para }
  603. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}insert_local_value_para,nil);
  604. { Update parameter information }
  605. current_procinfo.allocate_implicit_parameter;
  606. {$ifdef i386}
  607. { add implicit pushes for interrupt routines }
  608. if (po_interrupt in pd.procoptions) then
  609. current_procinfo.allocate_interrupt_stackframe;
  610. {$endif i386}
  611. { Calculate offsets }
  612. current_procinfo.after_header;
  613. { set _FAIL as keyword if constructor }
  614. if (pd.proctypeoption=potype_constructor) then
  615. begin
  616. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  617. tokeninfo^[_FAIL].keyword:=m_all;
  618. end;
  619. { set _SELF as keyword if methods }
  620. if assigned(pd._class) then
  621. begin
  622. oldselftokenmode:=tokeninfo^[_SELF].keyword;
  623. tokeninfo^[_SELF].keyword:=m_all;
  624. end;
  625. compile_proc_body(pd,((pdflags and pd_global)<>0),assigned(current_procinfo.parent.procdef._class));
  626. { reset _FAIL as _SELF normal }
  627. if (pd.proctypeoption=potype_constructor) then
  628. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  629. if assigned(pd._class) then
  630. tokeninfo^[_SELF].keyword:=oldselftokenmode;
  631. consume(_SEMICOLON);
  632. end;
  633. { release procinfo }
  634. if tprocinfo(current_module.procinfo)<>current_procinfo then
  635. internalerror(200304274);
  636. current_module.procinfo:=current_procinfo.parent;
  637. current_procinfo.free;
  638. { Restore old state }
  639. constsymtable:=oldconstsymtable;
  640. current_procdef:=oldprocdef;
  641. current_procinfo:=old_current_procinfo;
  642. end;
  643. {****************************************************************************
  644. DECLARATION PARSING
  645. ****************************************************************************}
  646. { search in symtablestack for not complete classes }
  647. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  648. begin
  649. if (tsym(p).typ=typesym) and
  650. (ttypesym(p).restype.def.deftype=objectdef) and
  651. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  652. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  653. end;
  654. procedure read_declarations(islibrary : boolean);
  655. procedure Not_supported_for_inline(t : ttoken);
  656. begin
  657. if (current_procdef.proccalloption=pocall_inline) then
  658. Begin
  659. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  660. Message(parser_w_inlining_disabled);
  661. current_procdef.proccalloption:=pocall_fpccall;
  662. End;
  663. end;
  664. begin
  665. repeat
  666. if not assigned(current_procdef) then
  667. internalerror(200304251);
  668. case token of
  669. _LABEL:
  670. begin
  671. Not_supported_for_inline(token);
  672. label_dec;
  673. end;
  674. _CONST:
  675. begin
  676. Not_supported_for_inline(token);
  677. const_dec;
  678. end;
  679. _TYPE:
  680. begin
  681. Not_supported_for_inline(token);
  682. type_dec;
  683. end;
  684. _VAR:
  685. var_dec;
  686. _THREADVAR:
  687. threadvar_dec;
  688. _CONSTRUCTOR,_DESTRUCTOR,
  689. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  690. begin
  691. Not_supported_for_inline(token);
  692. read_proc;
  693. end;
  694. _RESOURCESTRING:
  695. resourcestring_dec;
  696. _EXPORTS:
  697. begin
  698. Not_supported_for_inline(token);
  699. if not(assigned(current_procdef.localst)) or
  700. (current_procdef.localst.symtablelevel>main_program_level) or
  701. (current_module.is_unit) then
  702. begin
  703. Message(parser_e_syntax_error);
  704. consume_all_until(_SEMICOLON);
  705. end
  706. else if islibrary or
  707. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware]) then
  708. read_exports
  709. else
  710. begin
  711. Message(parser_w_unsupported_feature);
  712. consume(_BEGIN);
  713. end;
  714. end
  715. else break;
  716. end;
  717. until false;
  718. { check for incomplete class definitions, this is only required
  719. for fpc modes }
  720. if (m_fpc in aktmodeswitches) then
  721. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  722. end;
  723. procedure read_interface_declarations;
  724. begin
  725. repeat
  726. case token of
  727. _CONST :
  728. const_dec;
  729. _TYPE :
  730. type_dec;
  731. _VAR :
  732. var_dec;
  733. _THREADVAR :
  734. threadvar_dec;
  735. _RESOURCESTRING:
  736. resourcestring_dec;
  737. _FUNCTION,
  738. _PROCEDURE,
  739. _OPERATOR :
  740. read_proc;
  741. else
  742. break;
  743. end;
  744. until false;
  745. { check for incomplete class definitions, this is only required
  746. for fpc modes }
  747. if (m_fpc in aktmodeswitches) then
  748. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  749. end;
  750. end.
  751. {
  752. $Log$
  753. Revision 1.108 2003-05-09 17:47:03 peter
  754. * self moved to hidden parameter
  755. * removed hdisposen,hnewn,selfn
  756. Revision 1.107 2003/04/27 11:21:34 peter
  757. * aktprocdef renamed to current_procdef
  758. * procinfo renamed to current_procinfo
  759. * procinfo will now be stored in current_module so it can be
  760. cleaned up properly
  761. * gen_main_procsym changed to create_main_proc and release_main_proc
  762. to also generate a tprocinfo structure
  763. * fixed unit implicit initfinal
  764. Revision 1.106 2003/04/27 07:29:50 peter
  765. * current_procdef cleanup, current_procdef is now always nil when parsing
  766. a new procdef declaration
  767. * aktprocsym removed
  768. * lexlevel removed, use symtable.symtablelevel instead
  769. * implicit init/final code uses the normal genentry/genexit
  770. * funcret state checking updated for new funcret handling
  771. Revision 1.105 2003/04/26 00:31:42 peter
  772. * set return_offset moved to after_header
  773. Revision 1.104 2003/04/25 20:59:34 peter
  774. * removed funcretn,funcretsym, function result is now in varsym
  775. and aliases for result and function name are added using absolutesym
  776. * vs_hidden parameter for funcret passed in parameter
  777. * vs_hidden fixes
  778. * writenode changed to printnode and released from extdebug
  779. * -vp option added to generate a tree.log with the nodetree
  780. * nicer printnode for statements, callnode
  781. Revision 1.103 2003/04/24 13:03:01 florian
  782. * comp is now written with its bit pattern to the ppu instead as an extended
  783. Revision 1.102 2003/04/23 12:35:34 florian
  784. * fixed several issues with powerpc
  785. + applied a patch from Jonas for nested function calls (PowerPC only)
  786. * ...
  787. Revision 1.101 2003/04/22 14:33:38 peter
  788. * removed some notes/hints
  789. Revision 1.100 2003/04/22 13:47:08 peter
  790. * fixed C style array of const
  791. * fixed C array passing
  792. * fixed left to right with high parameters
  793. Revision 1.99 2003/04/22 10:09:35 daniel
  794. + Implemented the actual register allocator
  795. + Scratch registers unavailable when new register allocator used
  796. + maybe_save/maybe_restore unavailable when new register allocator used
  797. Revision 1.98 2003/04/17 07:50:24 daniel
  798. * Some work on interference graph construction
  799. Revision 1.97 2003/04/16 09:26:55 jonas
  800. * assembler procedures now again get a stackframe if they have local
  801. variables. No space is reserved for a function result however.
  802. Also, the register parameters aren't automatically saved on the stack
  803. anymore in assembler procedures.
  804. Revision 1.96 2003/04/05 21:09:31 jonas
  805. * several ppc/generic result offset related fixes. The "normal" result
  806. offset seems now to be calculated correctly and a lot of duplicate
  807. calculations have been removed. Nested functions accessing the parent's
  808. function result don't work at all though :(
  809. Revision 1.95 2003/04/02 16:11:34 peter
  810. * give error when exports is not supported
  811. Revision 1.94 2003/03/12 22:43:38 jonas
  812. * more powerpc and generic fixes related to the new register allocator
  813. Revision 1.93 2003/03/08 08:59:07 daniel
  814. + $define newra will enable new register allocator
  815. + getregisterint will return imaginary registers with $newra
  816. + -sr switch added, will skip register allocation so you can see
  817. the direct output of the code generator before register allocation
  818. Revision 1.92 2003/02/19 22:00:14 daniel
  819. * Code generator converted to new register notation
  820. - Horribily outdated todo.txt removed
  821. Revision 1.91 2003/01/09 21:52:37 peter
  822. * merged some verbosity options.
  823. * V_LineInfo is a verbosity flag to include line info
  824. Revision 1.90 2003/01/09 20:40:59 daniel
  825. * Converted some code in cgx86.pas to new register numbering
  826. Revision 1.89 2003/01/09 15:49:56 daniel
  827. * Added register conversion
  828. Revision 1.88 2003/01/08 18:43:56 daniel
  829. * Tregister changed into a record
  830. Revision 1.87 2003/01/03 20:35:08 peter
  831. * check also interfacedef when checking for matching forwarddef
  832. Revision 1.86 2003/01/02 11:14:02 michael
  833. + Patch from peter to support initial values for local variables
  834. Revision 1.85 2002/12/29 18:59:34 peter
  835. * fixed parsing of declarations before asm statement
  836. Revision 1.84 2002/12/29 18:25:18 peter
  837. * parse declarations before check _ASM token
  838. Revision 1.83 2002/12/29 14:57:50 peter
  839. * unit loading changed to first register units and load them
  840. afterwards. This is needed to support uses xxx in yyy correctly
  841. * unit dependency check fixed
  842. Revision 1.82 2002/12/25 01:26:56 peter
  843. * duplicate procsym-unitsym fix
  844. Revision 1.81 2002/12/15 13:37:15 peter
  845. * don't include uf_init for library. The code is already called and
  846. does not need to be in the initfinal table
  847. Revision 1.80 2002/12/07 14:27:09 carl
  848. * 3% memory optimization
  849. * changed some types
  850. + added type checking with different size for call node and for
  851. parameters
  852. Revision 1.79 2002/11/25 18:43:32 carl
  853. - removed the invalid if <> checking (Delphi is strange on this)
  854. + implemented abstract warning on instance creation of class with
  855. abstract methods.
  856. * some error message cleanups
  857. Revision 1.78 2002/11/25 17:43:23 peter
  858. * splitted defbase in defutil,symutil,defcmp
  859. * merged isconvertable and is_equal into compare_defs(_ext)
  860. * made operator search faster by walking the list only once
  861. Revision 1.77 2002/11/23 22:50:06 carl
  862. * some small speed optimizations
  863. + added several new warnings/hints
  864. Revision 1.76 2002/11/18 17:31:58 peter
  865. * pass proccalloption to ret_in_xxx and push_xxx functions
  866. Revision 1.75 2002/11/17 16:31:57 carl
  867. * memory optimization (3-4%) : cleanup of tai fields,
  868. cleanup of tdef and tsym fields.
  869. * make it work for m68k
  870. Revision 1.74 2002/11/15 01:58:53 peter
  871. * merged changes from 1.0.7 up to 04-11
  872. - -V option for generating bug report tracing
  873. - more tracing for option parsing
  874. - errors for cdecl and high()
  875. - win32 import stabs
  876. - win32 records<=8 are returned in eax:edx (turned off by default)
  877. - heaptrc update
  878. - more info for temp management in .s file with EXTDEBUG
  879. Revision 1.73 2002/11/09 15:32:30 carl
  880. * noopt for non-i386 targets
  881. Revision 1.72 2002/09/10 20:31:48 florian
  882. * call to current_procinfo.after_header added
  883. Revision 1.71 2002/09/07 15:25:07 peter
  884. * old logs removed and tabs fixed
  885. Revision 1.70 2002/09/03 16:26:27 daniel
  886. * Make Tprocdef.defs protected
  887. Revision 1.69 2002/08/25 19:25:20 peter
  888. * sym.insert_in_data removed
  889. * symtable.insertvardata/insertconstdata added
  890. * removed insert_in_data call from symtable.insert, it needs to be
  891. called separatly. This allows to deref the address calculation
  892. * procedures now calculate the parast addresses after the procedure
  893. directives are parsed. This fixes the cdecl parast problem
  894. * push_addr_param has an extra argument that specifies if cdecl is used
  895. or not
  896. Revision 1.68 2002/08/17 09:23:41 florian
  897. * first part of procinfo rewrite
  898. Revision 1.67 2002/08/16 14:24:59 carl
  899. * issameref() to test if two references are the same (then emit no opcodes)
  900. + ret_in_reg to replace ret_in_acc
  901. (fix some register allocation bugs at the same time)
  902. + save_std_register now has an extra parameter which is the
  903. usedinproc registers
  904. Revision 1.66 2002/08/11 14:32:27 peter
  905. * renamed current_library to objectlibrary
  906. Revision 1.65 2002/08/11 13:24:13 peter
  907. * saving of asmsymbols in ppu supported
  908. * asmsymbollist global is removed and moved into a new class
  909. tasmlibrarydata that will hold the info of a .a file which
  910. corresponds with a single module. Added librarydata to tmodule
  911. to keep the library info stored for the module. In the future the
  912. objectfiles will also be stored to the tasmlibrarydata class
  913. * all getlabel/newasmsymbol and friends are moved to the new class
  914. Revision 1.64 2002/08/09 19:14:28 carl
  915. * fixed stackframe parameter (should only contain local size),
  916. set to zero currently
  917. Revision 1.63 2002/08/06 20:55:22 florian
  918. * first part of ppc calling conventions fix
  919. Revision 1.62 2002/07/26 21:15:41 florian
  920. * rewrote the system handling
  921. Revision 1.61 2002/07/20 11:57:56 florian
  922. * types.pas renamed to defbase.pas because D6 contains a types
  923. unit so this would conflicts if D6 programms are compiled
  924. + Willamette/SSE2 instructions to assembler added
  925. Revision 1.60 2002/07/19 11:41:36 daniel
  926. * State tracker work
  927. * The whilen and repeatn are now completely unified into whilerepeatn. This
  928. allows the state tracker to change while nodes automatically into
  929. repeat nodes.
  930. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  931. 'not(a>b)' is optimized into 'a<=b'.
  932. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  933. by removing the notn and later switchting the true and falselabels. The
  934. same is done with 'repeat until not a'.
  935. Revision 1.59 2002/07/15 18:03:15 florian
  936. * readded removed changes
  937. Revision 1.57 2002/07/11 14:41:28 florian
  938. * start of the new generic parameter handling
  939. Revision 1.58 2002/07/14 18:00:44 daniel
  940. + Added the beginning of a state tracker. This will track the values of
  941. variables through procedures and optimize things away.
  942. Revision 1.56 2002/07/07 09:52:32 florian
  943. * powerpc target fixed, very simple units can be compiled
  944. * some basic stuff for better callparanode handling, far from being finished
  945. Revision 1.55 2002/07/04 20:43:01 florian
  946. * first x86-64 patches
  947. Revision 1.54 2002/07/01 18:46:25 peter
  948. * internal linker
  949. * reorganized aasm layer
  950. Revision 1.53 2002/05/18 13:34:14 peter
  951. * readded missing revisions
  952. Revision 1.52 2002/05/16 19:46:44 carl
  953. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  954. + try to fix temp allocation (still in ifdef)
  955. + generic constructor calls
  956. + start of tassembler / tmodulebase class cleanup
  957. Revision 1.51 2002/05/14 19:34:49 peter
  958. * removed old logs and updated copyright year
  959. }