psub.pas 41 KB

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